""" PYTHON test.py """ ######## a.pyx ######## ######## test.py ######## import os.path import time from Cython.Utils import GENERATED_BY_MARKER_BYTES, clear_function_caches, clear_method_caches from Cython.Build.Dependencies import cythonize, DependencyTree import Cython.Build.Dependencies getmtime = os.path.getmtime def wait_for_newer_mtime(filename, old_mtime): mtime = old_mtime while mtime <= old_mtime: os.utime(filename, None) mtime = getmtime(filename) return mtime # test the mtime waiting itself with open("test_file.txt", 'wb') as f: pass orig_mtime = getmtime("test_file.txt") wait_for_newer_mtime("test_file.txt", orig_mtime) assert orig_mtime < getmtime("test_file.txt") def fresh_cythonize(*args): clear_function_caches() clear_method_caches(DependencyTree.timestamp) Cython.Build.Dependencies._dep_tree = None cythonize(*args) assert not os.path.exists("a.c") # new fresh_cythonize("*.pyx") assert os.path.isfile("a.c") mtime = getmtime("a.c") # already exists fresh_cythonize("*.pyx") assert mtime == getmtime("a.c") # outdated wait_for_newer_mtime("a.pyx", mtime) assert mtime < getmtime("a.pyx") fresh_cythonize("*.pyx") new_mtime = getmtime("a.c") assert mtime < new_mtime # now up to date fresh_cythonize("*.pyx") assert new_mtime == getmtime("a.c") # different Cython version (even though newer) marker = b"/* Generated by Cython " assert GENERATED_BY_MARKER_BYTES.startswith(marker) # safety belt with open("a.c", "rb") as f: content = f.read() assert content.startswith(GENERATED_BY_MARKER_BYTES) content = marker + b"123" + content[len(marker):] with open("a.c", "wb") as f: f.write(content) wait_for_newer_mtime("a.c", new_mtime) other_cython_mtime = getmtime("a.c") assert mtime < new_mtime < other_cython_mtime fresh_cythonize("*.pyx") latest_mtime = getmtime("a.c") assert mtime < new_mtime < other_cython_mtime <= latest_mtime with open("a.c", "rb") as f: assert f.read(len(GENERATED_BY_MARKER_BYTES)) == GENERATED_BY_MARKER_BYTES # file was rewritten # now up to date fresh_cythonize("*.pyx") assert mtime < new_mtime < other_cython_mtime <= latest_mtime == getmtime("a.c") # force regeneration with environment variable os.environ["CYTHON_FORCE_REGEN"] = "1" time.sleep(0.1) assert latest_mtime == getmtime("a.c") fresh_cythonize("*.pyx") assert latest_mtime < getmtime("a.c")