Cython 3.3.0b1__py3-none-any.whl → 3.3.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- Cython/Build/Cache.py +84 -26
- Cython/Build/Cythonize.py +3 -0
- Cython/Build/Dependencies.py +4 -3
- Cython/Build/SharedModule.py +18 -3
- Cython/Build/Tests/TestCyCache.py +286 -5
- Cython/Compiler/Builtin.py +23 -5
- Cython/Compiler/CmdLine.py +1 -2
- Cython/Compiler/Code.py +4 -2
- Cython/Compiler/Errors.py +1 -0
- Cython/Compiler/ExprNodes.py +69 -45
- Cython/Compiler/FlowControl.py +2 -0
- Cython/Compiler/Main.py +26 -8
- Cython/Compiler/MemoryView.py +7 -1
- Cython/Compiler/ModuleNode.py +23 -13
- Cython/Compiler/Nodes.py +40 -11
- Cython/Compiler/Optimize.py +32 -29
- Cython/Compiler/Options.py +3 -2
- Cython/Compiler/ParseTreeTransforms.py +5 -5
- Cython/Compiler/Pipeline.py +3 -0
- Cython/Compiler/PyrexTypes.py +23 -10
- Cython/Compiler/Symtab.py +1 -1
- Cython/Compiler/Tests/TestBuiltin.py +41 -6
- Cython/Compiler/TypeInference.py +40 -3
- Cython/Compiler/TypeSlots.py +7 -6
- Cython/Compiler/UtilityCode.py +18 -3
- Cython/Includes/cpython/array.pxd +12 -5
- Cython/Includes/libc/stdbool.pxd +5 -0
- Cython/Runtime/refnanny.pyx +1 -1
- Cython/Shadow.py +1 -3
- Cython/TestUtils.py +14 -10
- Cython/Tests/TestCythonUtils.py +37 -2
- Cython/Utility/AsyncGen.c +1 -1
- Cython/Utility/Complex.c +12 -3
- Cython/Utility/Coroutine.c +3 -1
- Cython/Utility/CppConvert.pyx +61 -59
- Cython/Utility/CythonFunction.c +105 -147
- Cython/Utility/ExtensionTypes.c +4 -14
- Cython/Utility/FunctionArguments.c +3 -3
- Cython/Utility/MatchCase.c +9 -5
- Cython/Utility/ModuleSetupCode.c +77 -15
- Cython/Utility/ObjectHandling.c +1 -1
- Cython/Utility/Optimize.c +28 -17
- Cython/Utility/Profile.c +1 -1
- Cython/Utility/StringTools.c +1 -1
- Cython/Utility/TString.c +1 -1
- Cython/Utility/TypeConversion.c +2 -1
- Cython/Utility/UFuncs.pyx +5 -5
- Cython/Utility/arrayarray.h +18 -0
- Cython/Utils.py +17 -15
- cython-3.3.2.dist-info/METADATA +115 -0
- {cython-3.3.0b1.dist-info → cython-3.3.2.dist-info}/RECORD +54 -53
- cython-3.3.0b1.dist-info/METADATA +0 -286
- {cython-3.3.0b1.dist-info → cython-3.3.2.dist-info}/WHEEL +0 -0
- {cython-3.3.0b1.dist-info → cython-3.3.2.dist-info}/entry_points.txt +0 -0
- {cython-3.3.0b1.dist-info → cython-3.3.2.dist-info}/top_level.txt +0 -0
Cython/Build/Cache.py
CHANGED
|
@@ -2,10 +2,12 @@ from dataclasses import dataclass
|
|
|
2
2
|
import sys
|
|
3
3
|
import os
|
|
4
4
|
import hashlib
|
|
5
|
+
import operator
|
|
5
6
|
import shutil
|
|
6
7
|
import subprocess
|
|
7
|
-
from ..Utils import safe_makedirs, cached_function
|
|
8
8
|
import zipfile
|
|
9
|
+
|
|
10
|
+
from ..Utils import cached_function
|
|
9
11
|
from .. import __version__
|
|
10
12
|
|
|
11
13
|
try:
|
|
@@ -27,6 +29,7 @@ except ImportError:
|
|
|
27
29
|
zip_ext = ".zip"
|
|
28
30
|
|
|
29
31
|
MAX_CACHE_SIZE = 1024 * 1024 * 100
|
|
32
|
+
DEFAULT_DELETE_RATIO = 0.85
|
|
30
33
|
|
|
31
34
|
join_path = cached_function(os.path.join)
|
|
32
35
|
|
|
@@ -92,8 +95,8 @@ class Cache:
|
|
|
92
95
|
else:
|
|
93
96
|
self.path = path
|
|
94
97
|
self.cache_size = cache_size if cache_size is not None else MAX_CACHE_SIZE
|
|
95
|
-
|
|
96
|
-
|
|
98
|
+
# Make sure the cache directory exists (and allow for concurrent creation).
|
|
99
|
+
os.makedirs(self.path, exist_ok=True)
|
|
97
100
|
|
|
98
101
|
def transitive_fingerprint(
|
|
99
102
|
self, filename, dependencies, compilation_options, flags=FingerprintFlags()
|
|
@@ -128,31 +131,39 @@ class Cache:
|
|
|
128
131
|
)
|
|
129
132
|
|
|
130
133
|
def lookup_cache(self, c_file, fingerprint):
|
|
134
|
+
"""Look up a file in the cache and update its access/modification time.
|
|
135
|
+
|
|
136
|
+
Return the compressed file path of the cached file.
|
|
137
|
+
"""
|
|
131
138
|
# Cython-generated c files are highly compressible.
|
|
132
139
|
# (E.g. a compression ratio of about 10 for Sage).
|
|
133
|
-
|
|
134
|
-
safe_makedirs(self.path)
|
|
140
|
+
os.makedirs(self.path, exist_ok=True)
|
|
135
141
|
gz_fingerprint_file = self.fingerprint_file(c_file, fingerprint, gzip_ext)
|
|
136
|
-
|
|
142
|
+
# Single OS call to avoid races between check and time update.
|
|
143
|
+
try:
|
|
144
|
+
os.utime(gz_fingerprint_file, None)
|
|
137
145
|
return gz_fingerprint_file
|
|
146
|
+
except FileNotFoundError:
|
|
147
|
+
pass
|
|
138
148
|
zip_fingerprint_file = self.fingerprint_file(c_file, fingerprint, zip_ext)
|
|
139
|
-
|
|
149
|
+
try:
|
|
150
|
+
os.utime(zip_fingerprint_file, None)
|
|
140
151
|
return zip_fingerprint_file
|
|
152
|
+
except FileNotFoundError:
|
|
153
|
+
pass
|
|
141
154
|
return None
|
|
142
155
|
|
|
143
156
|
def load_from_cache(self, c_file, cached):
|
|
144
157
|
ext = os.path.splitext(cached)[1]
|
|
145
158
|
if ext == gzip_ext:
|
|
146
159
|
os.utime(cached, None)
|
|
147
|
-
with gzip_open(cached, "rb") as g:
|
|
148
|
-
|
|
149
|
-
shutil.copyfileobj(g, f)
|
|
160
|
+
with gzip_open(cached, "rb") as g, open(c_file, "wb") as f:
|
|
161
|
+
shutil.copyfileobj(g, f)
|
|
150
162
|
elif ext == zip_ext:
|
|
151
163
|
os.utime(cached, None)
|
|
152
164
|
dirname = os.path.dirname(c_file)
|
|
153
165
|
with zipfile.ZipFile(cached) as z:
|
|
154
|
-
|
|
155
|
-
z.extract(artifact, dirname)
|
|
166
|
+
z.extractall(dirname)
|
|
156
167
|
else:
|
|
157
168
|
raise ValueError(f"Unsupported cache file extension: {ext}")
|
|
158
169
|
|
|
@@ -172,8 +183,51 @@ class Cache:
|
|
|
172
183
|
zip.write(artifact, os.path.basename(artifact))
|
|
173
184
|
os.rename(fingerprint_file + ".tmp", fingerprint_file)
|
|
174
185
|
|
|
175
|
-
def
|
|
186
|
+
def _list_cache_files(self):
|
|
187
|
+
total_size = 0
|
|
188
|
+
cache_files = []
|
|
189
|
+
with os.scandir(self.path) as entries:
|
|
190
|
+
for entry in entries:
|
|
191
|
+
if entry.name.endswith('.tmp'):
|
|
192
|
+
# Ignore temp files that might still be used by other Cython processes.
|
|
193
|
+
continue
|
|
194
|
+
try:
|
|
195
|
+
stat = entry.stat(follow_symlinks=False)
|
|
196
|
+
except FileNotFoundError:
|
|
197
|
+
# File was removed concurrently.
|
|
198
|
+
continue
|
|
199
|
+
|
|
200
|
+
file_size = stat.st_size
|
|
201
|
+
total_size += file_size
|
|
202
|
+
cache_files.append((entry.path, file_size, stat.st_atime))
|
|
203
|
+
|
|
204
|
+
return total_size, cache_files
|
|
205
|
+
|
|
206
|
+
def _sort_files_by_atime(self, cache_files):
|
|
207
|
+
cache_files.sort(key=operator.itemgetter(2, 1))
|
|
208
|
+
|
|
209
|
+
def _delete_cache_files(self, files_to_delete, total_size, max_cache_size):
|
|
210
|
+
for file_path, size, _ in files_to_delete:
|
|
211
|
+
if total_size < max_cache_size:
|
|
212
|
+
return
|
|
213
|
+
try:
|
|
214
|
+
os.unlink(file_path)
|
|
215
|
+
total_size -= size
|
|
216
|
+
except FileNotFoundError:
|
|
217
|
+
# Concurrently deleted. Nothing left to delete for us.
|
|
218
|
+
total_size -= size
|
|
219
|
+
except OSError:
|
|
220
|
+
# Concurrent access on Windows? Keep the file, cannot delete it right now.
|
|
221
|
+
pass
|
|
222
|
+
|
|
223
|
+
def _cachedir_looks_small_enough(self):
|
|
224
|
+
if sys.platform == "win32":
|
|
225
|
+
# sysinternals du exists and behaves mostly like unix du.
|
|
226
|
+
# However, at least on Arm, it sometimes modifies the access time of the contained files
|
|
227
|
+
# which makes it completely unsuitable for a quick cache size check.
|
|
228
|
+
return False
|
|
176
229
|
try:
|
|
230
|
+
# Quick check for total directory size (on unixy systems).
|
|
177
231
|
completed_process = subprocess.run(
|
|
178
232
|
["du", "-s", "-k", os.path.abspath(self.path)], stdout=subprocess.PIPE
|
|
179
233
|
)
|
|
@@ -181,19 +235,23 @@ class Cache:
|
|
|
181
235
|
if completed_process.returncode == 0:
|
|
182
236
|
total_size = 1024 * int(stdout.strip().split()[0])
|
|
183
237
|
if total_size < self.cache_size:
|
|
184
|
-
return
|
|
238
|
+
return True
|
|
185
239
|
except (OSError, ValueError):
|
|
186
240
|
pass
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
241
|
+
return False
|
|
242
|
+
|
|
243
|
+
def cleanup_cache(self, ratio=DEFAULT_DELETE_RATIO):
|
|
244
|
+
"""Reduce the total file size in the cache to ratio*cache_size
|
|
245
|
+
by evicting the least recently accessed entries.
|
|
246
|
+
"""
|
|
247
|
+
if self._cachedir_looks_small_enough():
|
|
248
|
+
return
|
|
249
|
+
|
|
250
|
+
total_size, cache_files = self._list_cache_files()
|
|
251
|
+
|
|
194
252
|
if total_size > self.cache_size:
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
253
|
+
# Sort oldest access time first (then smallest size) to delete least recently used entries.
|
|
254
|
+
self._sort_files_by_atime(cache_files)
|
|
255
|
+
|
|
256
|
+
max_cache_size = int(self.cache_size * ratio)
|
|
257
|
+
self._delete_cache_files(cache_files, total_size, max_cache_size)
|
Cython/Build/Cythonize.py
CHANGED
|
@@ -79,6 +79,9 @@ def _interruptible_pool(pool_cm):
|
|
|
79
79
|
|
|
80
80
|
|
|
81
81
|
def _build(ext_modules, parallel):
|
|
82
|
+
if parallel is None and sys.implementation.name == 'graalpy':
|
|
83
|
+
# Each GraalPy worker has a substantial memory footprint.
|
|
84
|
+
parallel = 1
|
|
82
85
|
modcount = sum(len(modules) for _, modules in ext_modules)
|
|
83
86
|
if not modcount:
|
|
84
87
|
return
|
Cython/Build/Dependencies.py
CHANGED
|
@@ -9,6 +9,7 @@ from os.path import relpath as _relpath
|
|
|
9
9
|
from .Cache import Cache, FingerprintFlags
|
|
10
10
|
|
|
11
11
|
from collections.abc import Iterable
|
|
12
|
+
from functools import partial
|
|
12
13
|
|
|
13
14
|
try:
|
|
14
15
|
import pythran
|
|
@@ -17,7 +18,7 @@ except Exception:
|
|
|
17
18
|
|
|
18
19
|
from .. import Utils
|
|
19
20
|
from ..Utils import (cached_function, cached_method, path_exists,
|
|
20
|
-
|
|
21
|
+
copy_file_to_dir_if_newer, is_package_dir, write_depfile)
|
|
21
22
|
from ..Compiler import Errors
|
|
22
23
|
from ..Compiler.Main import Context
|
|
23
24
|
from ..Compiler import Options
|
|
@@ -26,7 +27,7 @@ from ..Compiler.Options import (CompilationOptions, default_options,
|
|
|
26
27
|
|
|
27
28
|
join_path = cached_function(os.path.join)
|
|
28
29
|
copy_once_if_newer = cached_function(copy_file_to_dir_if_newer)
|
|
29
|
-
safe_makedirs_once = cached_function(
|
|
30
|
+
safe_makedirs_once = cached_function(partial(os.makedirs, exist_ok=True))
|
|
30
31
|
|
|
31
32
|
|
|
32
33
|
@cython.cfunc
|
|
@@ -961,7 +962,7 @@ def cythonize(module_list, exclude=None, nthreads=0, aliases=None, quiet=False,
|
|
|
961
962
|
if 'include_path' not in options:
|
|
962
963
|
options['include_path'] = ['.']
|
|
963
964
|
if 'common_utility_include_dir' in options:
|
|
964
|
-
|
|
965
|
+
os.makedirs(options['common_utility_include_dir'], exist_ok=True)
|
|
965
966
|
|
|
966
967
|
depfile = options.pop('depfile', None)
|
|
967
968
|
|
Cython/Build/SharedModule.py
CHANGED
|
@@ -46,7 +46,7 @@ def _iter_exports(selected_features=None):
|
|
|
46
46
|
UtilityCode = Code.UtilityCode
|
|
47
47
|
match_special = UtilityCode.get_special_comment_matcher('/')
|
|
48
48
|
|
|
49
|
-
for c_utility_file in os.listdir(Code.get_utility_dir()):
|
|
49
|
+
for c_utility_file in sorted(os.listdir(Code.get_utility_dir())):
|
|
50
50
|
if not c_utility_file.endswith('.c'):
|
|
51
51
|
continue
|
|
52
52
|
|
|
@@ -95,6 +95,13 @@ def create_shared_library_pipeline(context, scope, options, result, selected_fea
|
|
|
95
95
|
tree = parse(compsrc)
|
|
96
96
|
|
|
97
97
|
if selected_features is None or 'MemoryView' in selected_features:
|
|
98
|
+
if options.compiler_directives.get('subinterpreters_compatible', 'no') != 'no':
|
|
99
|
+
import sys
|
|
100
|
+
print(
|
|
101
|
+
"WARNING: Shared module built with 'subinterpreters_compatible'. "
|
|
102
|
+
"This is currently unlikely to work correctly due to memoryview code.",
|
|
103
|
+
file=sys.stderr
|
|
104
|
+
)
|
|
98
105
|
tree.scope.use_utility_code(
|
|
99
106
|
MemoryView.get_view_utility_code(options.shared_utility_qualified_name))
|
|
100
107
|
|
|
@@ -139,8 +146,8 @@ def generate_shared_module(options):
|
|
|
139
146
|
Errors.reset()
|
|
140
147
|
|
|
141
148
|
dest_c_file = options.shared_c_file_path
|
|
142
|
-
|
|
143
|
-
|
|
149
|
+
module_name = options.module_name or os.path.splitext(os.path.basename(dest_c_file))[0]
|
|
150
|
+
pyx_file = f'{module_name.replace(".", "/")}.pyx'
|
|
144
151
|
|
|
145
152
|
selected_features = None
|
|
146
153
|
if options.shared_utility_features_enabled or options.shared_utility_features_disabled:
|
|
@@ -149,12 +156,20 @@ def generate_shared_module(options):
|
|
|
149
156
|
options.shared_utility_features_disabled,
|
|
150
157
|
)
|
|
151
158
|
|
|
159
|
+
# We know what code is being included so we can set good default compatibility settings ourself
|
|
160
|
+
options.compiler_directives.setdefault('freethreading_compatible', True)
|
|
161
|
+
options.compiler_directives.setdefault(
|
|
162
|
+
'subinterpreters_compatible',
|
|
163
|
+
"no" if selected_features is None or 'MemoryView' in selected_features else "own_gil"
|
|
164
|
+
)
|
|
165
|
+
|
|
152
166
|
context = Main.Context.from_options(options)
|
|
153
167
|
scope = Symtab.ModuleScope('MemoryView', parent_module = None, context = context, is_package=False)
|
|
154
168
|
|
|
155
169
|
source_desc = SharedUtilitySourceDescriptor(pyx_file)
|
|
156
170
|
comp_src = Main.CompilationSource(source_desc, EncodedString(module_name), os.getcwd())
|
|
157
171
|
result = Main.create_default_resultobj(comp_src, options)
|
|
172
|
+
result.c_file = dest_c_file
|
|
158
173
|
|
|
159
174
|
pipeline = create_shared_library_pipeline(
|
|
160
175
|
context, scope, options, result,
|
|
@@ -2,25 +2,30 @@ import difflib
|
|
|
2
2
|
import glob
|
|
3
3
|
import gzip
|
|
4
4
|
import os
|
|
5
|
-
import sys
|
|
6
5
|
import tempfile
|
|
6
|
+
import time
|
|
7
7
|
import unittest
|
|
8
8
|
|
|
9
9
|
import Cython.Build.Dependencies
|
|
10
10
|
import Cython.Compiler.Main
|
|
11
11
|
import Cython.Utils
|
|
12
12
|
from Cython.TestUtils import CythonTest
|
|
13
|
+
from Cython.Build.Cache import Cache
|
|
13
14
|
|
|
14
15
|
|
|
15
16
|
class TestCyCache(CythonTest):
|
|
16
17
|
|
|
17
18
|
def setUp(self):
|
|
18
19
|
CythonTest.setUp(self)
|
|
19
|
-
self.temp_dir = tempfile.
|
|
20
|
+
self.temp_dir = tempfile.TemporaryDirectory(
|
|
20
21
|
prefix='cycache-test',
|
|
21
|
-
dir='TEST_TMP' if os.path.isdir('TEST_TMP') else None
|
|
22
|
-
|
|
23
|
-
self.
|
|
22
|
+
dir='TEST_TMP' if os.path.isdir('TEST_TMP') else None
|
|
23
|
+
)
|
|
24
|
+
self.src_dir = tempfile.mkdtemp(prefix='src', dir=self.temp_dir.name)
|
|
25
|
+
self.cache_dir = tempfile.mkdtemp(prefix='cache', dir=self.temp_dir.name)
|
|
26
|
+
|
|
27
|
+
def tearDown(self):
|
|
28
|
+
self.temp_dir.cleanup()
|
|
24
29
|
|
|
25
30
|
def cache_files(self, file_glob):
|
|
26
31
|
return glob.glob(os.path.join(self.cache_dir, file_glob))
|
|
@@ -83,6 +88,33 @@ class TestCyCache(CythonTest):
|
|
|
83
88
|
def test_cycache_switch_compile(self):
|
|
84
89
|
self._test_cycache_switch(self.fresh_compile)
|
|
85
90
|
|
|
91
|
+
def _test_cycache_fingerprint(self, compilation_method, **kwargs):
|
|
92
|
+
content = 'value = 1\n'
|
|
93
|
+
a_pyx = os.path.join(self.src_dir, 'a.pyx')
|
|
94
|
+
a_c = a_pyx[:-4] + '.c'
|
|
95
|
+
|
|
96
|
+
with open(a_pyx, 'w') as f:
|
|
97
|
+
f.write(content)
|
|
98
|
+
|
|
99
|
+
compilation_method(a_pyx, cache=self.cache_dir)
|
|
100
|
+
with open(a_c) as f:
|
|
101
|
+
a_contents1 = f.read()
|
|
102
|
+
os.unlink(a_c)
|
|
103
|
+
|
|
104
|
+
compilation_method(a_pyx, cache=self.cache_dir, **kwargs)
|
|
105
|
+
|
|
106
|
+
with open(a_c) as f:
|
|
107
|
+
a_contents2 = f.read()
|
|
108
|
+
os.unlink(a_c)
|
|
109
|
+
|
|
110
|
+
self.assertNotEqual(a_contents1, a_contents2, 'C file not changed!')
|
|
111
|
+
|
|
112
|
+
def test_cycache_fingerprint_cythonize(self):
|
|
113
|
+
self._test_cycache_fingerprint(self.fresh_cythonize, compiler_directives={'linetrace': True})
|
|
114
|
+
|
|
115
|
+
def test_cycache_fingerprint_compile(self):
|
|
116
|
+
self._test_cycache_fingerprint(self.fresh_compile, compiler_directives={'linetrace': True})
|
|
117
|
+
|
|
86
118
|
def _test_cycache_uses_cache(self, compilation_method):
|
|
87
119
|
a_pyx = os.path.join(self.src_dir, 'a.pyx')
|
|
88
120
|
a_c = a_pyx[:-4] + '.c'
|
|
@@ -188,8 +220,257 @@ class TestCyCache(CythonTest):
|
|
|
188
220
|
compilation_method(hash_pyx, cache=self.cache_dir, cplus=False, show_version=True)
|
|
189
221
|
|
|
190
222
|
self.assertEqual(2, len(self.cache_files('options.c*')))
|
|
223
|
+
|
|
191
224
|
def test_options_invalidation_cythonize(self):
|
|
192
225
|
self._test_options_invalidation(self.fresh_cythonize)
|
|
193
226
|
|
|
194
227
|
def test_options_invalidation_compile(self):
|
|
195
228
|
self._test_options_invalidation(self.fresh_compile)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
class TestCyCacheCleanup(unittest.TestCase):
|
|
232
|
+
|
|
233
|
+
def setUp(self):
|
|
234
|
+
self.temp_dir = tempfile.TemporaryDirectory()
|
|
235
|
+
self.cache_dir = self.temp_dir.name
|
|
236
|
+
|
|
237
|
+
# Set a small cache size to trigger cleanup easily
|
|
238
|
+
self.cache_size = 10_000 # 10 KB
|
|
239
|
+
self.cache = Cache(self.cache_dir, self.cache_size)
|
|
240
|
+
|
|
241
|
+
def tearDown(self):
|
|
242
|
+
self.temp_dir.cleanup()
|
|
243
|
+
|
|
244
|
+
def _create_cache_file(self, filename, size_bytes, access_time=None):
|
|
245
|
+
"""
|
|
246
|
+
Create a cache file with specified size and access time.
|
|
247
|
+
|
|
248
|
+
Args:
|
|
249
|
+
filename: Name of the file to create
|
|
250
|
+
size_bytes: Size of the file in bytes
|
|
251
|
+
access_time: Unix timestamp for file access time (uses current time if None)
|
|
252
|
+
"""
|
|
253
|
+
file_path = os.path.join(self.cache_dir, filename)
|
|
254
|
+
|
|
255
|
+
# Create file with specified size
|
|
256
|
+
with open(file_path, 'wb') as f:
|
|
257
|
+
f.write(b'x' * size_bytes)
|
|
258
|
+
|
|
259
|
+
# Set access time if provided
|
|
260
|
+
if access_time is not None:
|
|
261
|
+
os.utime(file_path, (access_time, access_time))
|
|
262
|
+
else:
|
|
263
|
+
self.assertTrue(os.path.exists(file_path), f"{file_path} should exist after creation")
|
|
264
|
+
|
|
265
|
+
return file_path
|
|
266
|
+
|
|
267
|
+
def test_cleanup_deletes_oldest_files_first(self):
|
|
268
|
+
"""
|
|
269
|
+
Validate that cleanup_cache() deletes files with older access times
|
|
270
|
+
before more recently accessed files.
|
|
271
|
+
"""
|
|
272
|
+
# Get current time and create files with staggered access times
|
|
273
|
+
base_time = time.time()
|
|
274
|
+
|
|
275
|
+
# Create files with progressively newer access times
|
|
276
|
+
file1 = self._create_cache_file('file1.cache', 3000, base_time - 1000)
|
|
277
|
+
file2 = self._create_cache_file('file2.cache', 3000, base_time - 500)
|
|
278
|
+
file3 = self._create_cache_file('file3.cache', 3000, base_time - 100)
|
|
279
|
+
file4 = self._create_cache_file('file4.cache', 3000, base_time)
|
|
280
|
+
|
|
281
|
+
# Run cleanup
|
|
282
|
+
self.cache.cleanup_cache()
|
|
283
|
+
|
|
284
|
+
# Oldest file should be deleted first
|
|
285
|
+
self.assertFalse(os.path.exists(file1), "Oldest file should be deleted first")
|
|
286
|
+
|
|
287
|
+
# Files should still exist or be deleted based on access time order
|
|
288
|
+
# and remaining size relative to cache_size * ratio
|
|
289
|
+
remaining_files = [file2, file3, file4]
|
|
290
|
+
existing_count = sum(1 for f in remaining_files if os.path.exists(f))
|
|
291
|
+
self.assertGreaterEqual(existing_count, 1, "Some files should persist")
|
|
292
|
+
|
|
293
|
+
# Verify that the ratio threshold has been met
|
|
294
|
+
_, remaining_cache_files = self.cache._list_cache_files()
|
|
295
|
+
remaining_size = sum(size for _, size, _ in remaining_cache_files)
|
|
296
|
+
self.assertLess(
|
|
297
|
+
remaining_size,
|
|
298
|
+
self.cache_size * 0.85,
|
|
299
|
+
"Remaining cache size should be below 85% of cache_size limit"
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
def test_cleanup_skips_temp_files(self):
|
|
303
|
+
"""Verify that .tmp files are not deleted during cleanup."""
|
|
304
|
+
base_time = time.time()
|
|
305
|
+
|
|
306
|
+
# Create regular and temp files
|
|
307
|
+
regular_file = self._create_cache_file('old_file.cache', 12000, base_time - 1000)
|
|
308
|
+
temp_file = self._create_cache_file('temp_file.tmp', 12000, base_time - 2000)
|
|
309
|
+
|
|
310
|
+
# Run cleanup
|
|
311
|
+
self.cache.cleanup_cache()
|
|
312
|
+
|
|
313
|
+
# Temp file should still exist even though it's older
|
|
314
|
+
self.assertTrue(
|
|
315
|
+
os.path.exists(temp_file),
|
|
316
|
+
"Temporary .tmp files should not be deleted"
|
|
317
|
+
)
|
|
318
|
+
self.assertFalse(
|
|
319
|
+
os.path.exists(regular_file),
|
|
320
|
+
"No files were deleted by cleanup"
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
def test_cleanup_stops_at_ratio_threshold(self):
|
|
324
|
+
"""
|
|
325
|
+
Verify that cleanup_cache() stops deleting once total size
|
|
326
|
+
falls below cache_size * ratio.
|
|
327
|
+
"""
|
|
328
|
+
base_time = time.time()
|
|
329
|
+
|
|
330
|
+
# Create files with clear access time ordering
|
|
331
|
+
self._create_cache_file('file1.cache', 2000, base_time - 1000)
|
|
332
|
+
self._create_cache_file('file2.cache', 2000, base_time - 800)
|
|
333
|
+
self._create_cache_file('file3.cache', 2000, base_time - 600)
|
|
334
|
+
self._create_cache_file('file4.cache', 2000, base_time - 400)
|
|
335
|
+
self._create_cache_file('file5.cache', 2000, base_time - 200)
|
|
336
|
+
self._create_cache_file('file6.cache', 2000, base_time)
|
|
337
|
+
|
|
338
|
+
# Run cleanup with custom ratio.
|
|
339
|
+
ratio = 0.70
|
|
340
|
+
self.cache.cleanup_cache(ratio=ratio)
|
|
341
|
+
|
|
342
|
+
# Remaining size should be less than cache_size * ratio.
|
|
343
|
+
_, remaining_files = self.cache._list_cache_files()
|
|
344
|
+
remaining_size = sum(size for _, size, _ in remaining_files)
|
|
345
|
+
|
|
346
|
+
max_cache_size = int(self.cache_size * ratio)
|
|
347
|
+
self.assertLess(
|
|
348
|
+
remaining_size,
|
|
349
|
+
max_cache_size,
|
|
350
|
+
f"Remaining size {remaining_size} should be less than "
|
|
351
|
+
f"cache_size * ratio ({max_cache_size})"
|
|
352
|
+
)
|
|
353
|
+
|
|
354
|
+
def test_cleanup_no_action_when_under_limit(self):
|
|
355
|
+
"""
|
|
356
|
+
Verify that cleanup_cache() does not delete files when
|
|
357
|
+
total cache size is already under the limit.
|
|
358
|
+
"""
|
|
359
|
+
base_time = time.time()
|
|
360
|
+
|
|
361
|
+
# Create small files that don't exceed cache_size.
|
|
362
|
+
file1 = self._create_cache_file('file1.cache', 1000, base_time - 1000)
|
|
363
|
+
file2 = self._create_cache_file('file2.cache', 1000, base_time)
|
|
364
|
+
|
|
365
|
+
# Run cleanup.
|
|
366
|
+
self.cache.cleanup_cache()
|
|
367
|
+
|
|
368
|
+
# Both files should still exist.
|
|
369
|
+
self.assertTrue(os.path.exists(file1), "Files should not be deleted when under limit")
|
|
370
|
+
self.assertTrue(os.path.exists(file2), "Files should not be deleted when under limit")
|
|
371
|
+
|
|
372
|
+
def test_cleanup_no_action_when_at_limit(self):
|
|
373
|
+
"""
|
|
374
|
+
Verify that cleanup_cache() does not delete files when
|
|
375
|
+
total cache size is only at the limit.
|
|
376
|
+
"""
|
|
377
|
+
base_time = time.time()
|
|
378
|
+
|
|
379
|
+
file1 = self._create_cache_file('file1.cache', 3000, base_time - 1000)
|
|
380
|
+
file2 = self._create_cache_file('file2.cache', 3000, base_time)
|
|
381
|
+
file3 = self._create_cache_file('file2.cache', 4000, base_time)
|
|
382
|
+
|
|
383
|
+
# Run cleanup.
|
|
384
|
+
self.cache.cleanup_cache()
|
|
385
|
+
|
|
386
|
+
# All files should still exist.
|
|
387
|
+
self.assertTrue(os.path.exists(file1), "Files should not be deleted when not over limit")
|
|
388
|
+
self.assertTrue(os.path.exists(file2), "Files should not be deleted when not over limit")
|
|
389
|
+
self.assertTrue(os.path.exists(file3), "Files should not be deleted when not over limit")
|
|
390
|
+
|
|
391
|
+
def test_cleanup_preserves_recent_files(self):
|
|
392
|
+
"""
|
|
393
|
+
Verify that cleanup_cache() preserves the most recently accessed files.
|
|
394
|
+
"""
|
|
395
|
+
base_time = time.time()
|
|
396
|
+
|
|
397
|
+
# Create files: one very old, several recent ones
|
|
398
|
+
old_file = self._create_cache_file('old.cache', 4000, base_time - 10000)
|
|
399
|
+
recent_file1 = self._create_cache_file('recent1.cache', 4000, base_time - 10)
|
|
400
|
+
recent_file2 = self._create_cache_file('recent2.cache', 4000, base_time)
|
|
401
|
+
|
|
402
|
+
# Run cleanup.
|
|
403
|
+
self.cache.cleanup_cache()
|
|
404
|
+
|
|
405
|
+
# Old file should be deleted.
|
|
406
|
+
self.assertFalse(os.path.exists(old_file), "Old file should be deleted")
|
|
407
|
+
|
|
408
|
+
# At least one recent file should still exist.
|
|
409
|
+
files_exist = os.path.exists(recent_file1) or os.path.exists(recent_file2)
|
|
410
|
+
self.assertTrue(
|
|
411
|
+
files_exist,
|
|
412
|
+
"At least one recently accessed file should be preserved"
|
|
413
|
+
)
|
|
414
|
+
|
|
415
|
+
def test_cleanup_handles_concurrent_file_deletion(self):
|
|
416
|
+
"""
|
|
417
|
+
Verify that cleanup_cache() handles concurrent file deletion gracefully.
|
|
418
|
+
|
|
419
|
+
This test simulates a race condition where a file is deleted by another
|
|
420
|
+
process between _list_cache_files() and _delete_cache_files().
|
|
421
|
+
"""
|
|
422
|
+
base_time = time.time()
|
|
423
|
+
ratio = 85 # %
|
|
424
|
+
max_cache_size = int(self.cache_size * ratio / 100)
|
|
425
|
+
|
|
426
|
+
# Create cache files with staggered access times to exceed cache_size.
|
|
427
|
+
file1 = self._create_cache_file('file1.cache', 3000, base_time - 1000)
|
|
428
|
+
file2 = self._create_cache_file('file2.cache', 3000, base_time - 500)
|
|
429
|
+
file3 = self._create_cache_file('file3.cache', 3000, base_time - 100)
|
|
430
|
+
file4 = self._create_cache_file('file4.cache', 3000, base_time)
|
|
431
|
+
|
|
432
|
+
# Verify all files exist.
|
|
433
|
+
self.assertTrue(os.path.exists(file1))
|
|
434
|
+
self.assertTrue(os.path.exists(file2))
|
|
435
|
+
self.assertTrue(os.path.exists(file3))
|
|
436
|
+
self.assertTrue(os.path.exists(file4))
|
|
437
|
+
|
|
438
|
+
# Get the sorted list of files that cleanup_cache() would process.
|
|
439
|
+
_, cache_files = self.cache._list_cache_files()
|
|
440
|
+
self.cache._sort_files_by_atime(cache_files)
|
|
441
|
+
|
|
442
|
+
# The oldest file should be first.
|
|
443
|
+
oldest_path, _, _ = cache_files[0]
|
|
444
|
+
self.assertEqual(oldest_path, file1, "file1 should be the oldest")
|
|
445
|
+
|
|
446
|
+
total_size, _ = self.cache._list_cache_files()
|
|
447
|
+
|
|
448
|
+
# Simulate concurrent deletion: delete the oldest file before _delete_cache_files processes it.
|
|
449
|
+
os.unlink(oldest_path)
|
|
450
|
+
self.assertFalse(os.path.exists(oldest_path))
|
|
451
|
+
|
|
452
|
+
# Call _delete_cache_files directly with the pre-computed list.
|
|
453
|
+
# This isolates the deletion logic and allows us to test the race condition.
|
|
454
|
+
try:
|
|
455
|
+
self.cache._delete_cache_files(cache_files, total_size, max_cache_size)
|
|
456
|
+
except FileNotFoundError:
|
|
457
|
+
self.fail(
|
|
458
|
+
"_delete_cache_files() should handle FileNotFoundError from concurrent "
|
|
459
|
+
"deletion gracefully without propagating the exception"
|
|
460
|
+
)
|
|
461
|
+
|
|
462
|
+
# Verify that cleanup continued and deleted additional files.
|
|
463
|
+
remaining_total_size, remaining_files = self.cache._list_cache_files()
|
|
464
|
+
self.assertLessEqual(
|
|
465
|
+
remaining_total_size,
|
|
466
|
+
max_cache_size,
|
|
467
|
+
f"Cache should be cleaned up to below {ratio}% despite concurrent deletions"
|
|
468
|
+
)
|
|
469
|
+
|
|
470
|
+
# Verify that files were actually deleted.
|
|
471
|
+
deleted_count = 4 - len(remaining_files)
|
|
472
|
+
self.assertGreater(
|
|
473
|
+
deleted_count,
|
|
474
|
+
0,
|
|
475
|
+
"Cleanup should have deleted files to reach the target ratio"
|
|
476
|
+
)
|
Cython/Compiler/Builtin.py
CHANGED
|
@@ -668,7 +668,8 @@ inferred_method_return_types = {
|
|
|
668
668
|
get='I',
|
|
669
669
|
keys='dict_keys[K]',
|
|
670
670
|
values='dict_values[I]',
|
|
671
|
-
items='dict_items[tuple[K,I]]'
|
|
671
|
+
items='dict_items[tuple[K,I]]',
|
|
672
|
+
setdefault='I',
|
|
672
673
|
),
|
|
673
674
|
'frozendict': dict(
|
|
674
675
|
copy='T',
|
|
@@ -718,16 +719,33 @@ def _parse_signature(pos, env, builtin_type, return_signature: str) -> PyrexType
|
|
|
718
719
|
return _parse_atomic_signature(builtin_type, return_signature)
|
|
719
720
|
|
|
720
721
|
|
|
721
|
-
def find_return_type_of_builtin_method(pos, env, builtin_type, method_name) -> PyrexTypes.PyrexType:
|
|
722
|
+
def find_return_type_of_builtin_method(pos, env, builtin_type, method_name, args=None) -> tuple[PyrexTypes.PyrexType, bool]:
|
|
723
|
+
# Returns a tuple: (return type, may be None)
|
|
722
724
|
container_type = builtin_type.get_container_type()
|
|
723
725
|
type_name = container_type.name if container_type else builtin_type.name
|
|
724
726
|
if type_name in inferred_method_return_types:
|
|
725
727
|
methods = inferred_method_return_types[type_name]
|
|
726
728
|
if method_name in methods:
|
|
727
729
|
return_type_signature: str = methods[method_name]
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
730
|
+
return_type = _parse_signature(pos, env, builtin_type, return_type_signature)
|
|
731
|
+
if type_name == 'dict' and method_name in ('pop', 'get', 'setdefault'):
|
|
732
|
+
# These are special in that they accept a second 'default' argument that
|
|
733
|
+
# may determine the return type.
|
|
734
|
+
if args is None or len(args) == 0:
|
|
735
|
+
# unusual call
|
|
736
|
+
return PyrexTypes.py_object_type, True
|
|
737
|
+
if len(args) == 1:
|
|
738
|
+
# Method called without second parameter - default value is None
|
|
739
|
+
return return_type, True
|
|
740
|
+
if len(args) > 2:
|
|
741
|
+
# Unusual call, possibly a runtime error.
|
|
742
|
+
return PyrexTypes.py_object_type, True
|
|
743
|
+
# Method called with 'default' parameter.
|
|
744
|
+
default_type = args[1].type or args[1].infer_type(env)
|
|
745
|
+
return_type = PyrexTypes.independent_spanning_type(return_type, default_type)
|
|
746
|
+
return return_type, return_type is PyrexTypes.py_object_type
|
|
747
|
+
return return_type, False
|
|
748
|
+
return PyrexTypes.py_object_type, True
|
|
731
749
|
|
|
732
750
|
|
|
733
751
|
unsafe_compile_time_methods = {
|
Cython/Compiler/CmdLine.py
CHANGED
|
@@ -20,8 +20,7 @@ Environment variables:
|
|
|
20
20
|
|
|
21
21
|
class ParseDirectivesAction(Action):
|
|
22
22
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
23
|
-
old_directives = dict(getattr(namespace, self.dest,
|
|
24
|
-
Options.get_directive_defaults()))
|
|
23
|
+
old_directives = dict(getattr(namespace, self.dest, {}))
|
|
25
24
|
directives = Options.parse_directive_list(
|
|
26
25
|
values, relaxed_bool=True, current_settings=old_directives)
|
|
27
26
|
setattr(namespace, self.dest, directives)
|