summaryrefslogtreecommitdiff
path: root/tests/build/cythonize_newer_files.srctree
blob: d2f65e2d6201e384e891c545df78a3a28472e43c (plain)
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
"""
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")