1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
PYTHON test.py
######## test.py ########
from __future__ import print_function
import os.path
from Cython.Utils import is_cython_generated_file
from Cython.Compiler.Errors import CompileError
from Cython.Build.Dependencies import cythonize
# Make sure the source files are newer than the .c files, so that cythonize() regenerates them.
files = {}
for source_file in sorted(os.listdir(os.getcwd())):
if 'module' in source_file and (source_file.endswith(".pyx") or source_file.endswith(".py")):
c_file = files[source_file] = os.path.splitext(source_file)[0] + ".c"
os.utime(source_file, None)
assert not os.path.exists(c_file) or os.path.getmtime(source_file) >= os.path.getmtime(c_file)
for source_file, c_file in files.items():
print("Testing:", source_file, c_file)
assert is_cython_generated_file(c_file, allow_failed=True, if_not_found=True)
# cythonizing should (re)generate the file
cythonize(source_file, language_level=3)
assert is_cython_generated_file(c_file, if_not_found=False)
assert os.path.getmtime(source_file) <= os.path.getmtime(c_file)
# calling cythonize again should not rewrite the file
# (not asserting this here, but at least it shouldn't fail)
cythonize(source_file, language_level=3)
assert is_cython_generated_file(c_file, if_not_found=False)
assert os.path.getmtime(source_file) <= os.path.getmtime(c_file)
# But overwriting an unknown file should fail, even when requested multiple times.
for source_file in [
"refuse_to_overwrite.pyx",
"refuse_to_overwrite.py",
"compile_failure.pyx",
"refuse_to_overwrite_header.pyx",
"refuse_to_overwrite_api_header.pyx",
]:
if 'api_header' in source_file:
target_file = os.path.splitext(source_file)[0] + "_api.h"
elif 'header' in source_file:
target_file = os.path.splitext(source_file)[0] + ".h"
else:
target_file = os.path.splitext(source_file)[0] + ".c"
for _ in range(3):
os.utime(source_file, None)
assert not is_cython_generated_file(target_file)
try:
print("Testing:", source_file)
cythonize(source_file, language_level=3)
except CompileError:
print("REFUSED to overwrite %s, OK" % target_file)
assert not is_cython_generated_file(target_file)
else:
assert False, "FAILURE: Existing output file was overwritten for source file %s" % source_file
######## pymodule.c ########
#error Do not use this file, it is the result of a failed Cython compilation.
######## pymodule.py ########
"""
Overwriting a failed .py file result works
"""
######## cymodule.c ########
#error Do not use this file, it is the result of a failed Cython compilation.
######## cymodule.pyx ########
"""
Overwriting a failed .pyx file result works
"""
######## overwritten_cymodule.c ########
/* Generated by Cython 0.8.15 */
######## overwritten_cymodule.pyx ########
"""
Overwriting an outdated .c file works
"""
######## new_cymodule.pyx ########
"""
Creating a new .c file works
"""
######## new_pymodule.py ########
"""
Creating a new .c file works
"""
######## refuse_to_overwrite.c ########
static int external_function(int x) {
return x + 1;
}
######## refuse_to_overwrite.py ########
"""
Do not overwrite an unknown output file
"""
######## refuse_to_overwrite.pyx ########
"""
Do not overwrite an unknown output file
"""
######## compile_failure.c ########
static int external_function(int x) {
return x + 1;
}
######## compile_failure.pyx ########
"""
Do not overwrite an unknown output file even on compile failures.
"""
Not Python syntax!
######## write_module_header.pyx ########
cdef public int func():
return 1
######## overwrite_module_header.c ########
/* Generated by Cython 0.8.15 */
######## overwrite_module_header.pyx ########
cdef public int func():
return 1
######## refuse_to_overwrite_header.h ########
static int external_function(int x) {
return x + 1;
}
######## refuse_to_overwrite_header.pyx ########
cdef public int func():
return 1
######## write_module_api_header.pyx ########
cdef api int func():
return 1
######## overwrite_module_api_header.c ########
/* Generated by Cython 0.8.15 */
######## overwrite_module_api_header.pyx ########
cdef public int func():
return 1
######## refuse_to_overwrite_api_header_api.h ########
static int external_function(int x) {
return x + 1;
}
######## refuse_to_overwrite_api_header.pyx ########
cdef api int func():
return 1
|