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