Debian Codegraph @

tests/build/cythonize_newer_files.srctree

raw
1 """
2 PYTHON test.py
3 """
4
5 ######## a.pyx ########
6
7 ######## test.py ########
8
9 import os.path
10 import time
11
12 from Cython.Utils import GENERATED_BY_MARKER_BYTES, clear_function_caches, clear_method_caches
13 from Cython.Build.Dependencies import cythonize, DependencyTree
14 import Cython.Build.Dependencies
15
16 getmtime = os.path.getmtime
17
18 def wait_for_newer_mtime(filename, old_mtime):
19 mtime = old_mtime
20 while mtime <= old_mtime:
21 os.utime(filename, None)
22 mtime = getmtime(filename)
23 return mtime
24
25
26 # test the mtime waiting itself
27 with open("test_file.txt", 'wb') as f:
28 pass
29 orig_mtime = getmtime("test_file.txt")
30 wait_for_newer_mtime("test_file.txt", orig_mtime)
31 assert orig_mtime < getmtime("test_file.txt")
32
33
34 def fresh_cythonize(*args):
35 clear_function_caches()
36 clear_method_caches(DependencyTree.timestamp)
37 Cython.Build.Dependencies._dep_tree = None
38 cythonize(*args)
39
40
41 assert not os.path.exists("a.c")
42
43 # new
44 fresh_cythonize("*.pyx")
45 assert os.path.isfile("a.c")
46 mtime = getmtime("a.c")
47
48 # already exists
49 fresh_cythonize("*.pyx")
50 assert mtime == getmtime("a.c")
51
52 # outdated
53 wait_for_newer_mtime("a.pyx", mtime)
54 assert mtime < getmtime("a.pyx")
55 fresh_cythonize("*.pyx")
56 new_mtime = getmtime("a.c")
57 assert mtime < new_mtime
58
59 # now up to date
60 fresh_cythonize("*.pyx")
61 assert new_mtime == getmtime("a.c")
62
63 # different Cython version (even though newer)
64 marker = b"/* Generated by Cython "
65 assert GENERATED_BY_MARKER_BYTES.startswith(marker) # safety belt
66 with open("a.c", "rb") as f:
67 content = f.read()
68
69 assert content.startswith(GENERATED_BY_MARKER_BYTES)
70 content = marker + b"123" + content[len(marker):]
71
72 with open("a.c", "wb") as f:
73 f.write(content)
74 wait_for_newer_mtime("a.c", new_mtime)
75
76 other_cython_mtime = getmtime("a.c")
77 assert mtime < new_mtime < other_cython_mtime
78
79 fresh_cythonize("*.pyx")
80 latest_mtime = getmtime("a.c")
81 assert mtime < new_mtime < other_cython_mtime <= latest_mtime
82
83 with open("a.c", "rb") as f:
84 assert f.read(len(GENERATED_BY_MARKER_BYTES)) == GENERATED_BY_MARKER_BYTES # file was rewritten
85
86 # now up to date
87 fresh_cythonize("*.pyx")
88 assert mtime < new_mtime < other_cython_mtime <= latest_mtime == getmtime("a.c")
89
90 # force regeneration with environment variable
91 os.environ["CYTHON_FORCE_REGEN"] = "1"
92 time.sleep(0.1)
93
94 assert latest_mtime == getmtime("a.c")
95 fresh_cythonize("*.pyx")
96 assert latest_mtime < getmtime("a.c")