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