Skip to content

Commit ce68eec

Browse files
committed
Compare update_lib contents and benchmark CPython parsing consistently
Assisted-by: Codex:GPT-6
1 parent 0dca682 commit ce68eec

4 files changed

Lines changed: 57 additions & 8 deletions

File tree

‎benches/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ in two ways:
5454
1. The time to parse the file to AST
5555
2. The time it takes to execute the file
5656

57+
Parsing stops at an AST in both implementations. The CPython reference uses
58+
`compile(..., flags=ast.PyCF_ONLY_AST)` without compiling bytecode.
59+
5760
Execution includes compilation and runs each iteration with a fresh namespace whose `__name__`
5861
is `"__main__"`, so script entry points run in both RustPython and CPython.
5962

‎benches/execution.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,14 @@ pub fn benchmark_file_parsing(
9595
let builtins = pyo3::types::PyModule::import(py, "builtins")
9696
.expect("Failed to import builtins");
9797
let compile = builtins.getattr("compile").expect("no compile in builtins");
98+
let ast = pyo3::types::PyModule::import(py, "ast").expect("Failed to import ast");
99+
let only_ast: i32 = ast
100+
.getattr("PyCF_ONLY_AST")
101+
.and_then(|flag| flag.extract())
102+
.expect("no PyCF_ONLY_AST in ast");
98103
b.iter(|| {
99104
let x = compile
100-
.call1((contents, name, "exec"))
105+
.call1((contents, name, "exec", only_ast))
101106
.expect("Failed to parse code");
102107
black_box(x);
103108
})

‎scripts/update_lib/file_utils.py‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,29 @@ def get_cpython_dir(src_path: pathlib.Path) -> pathlib.Path:
222222
# === Comparison utilities ===
223223

224224

225+
def _files_are_same(left: pathlib.Path, right: pathlib.Path) -> bool:
226+
# Recheck contents even when size and timestamps match, including after a
227+
# previous comparison. filecmp caches results by those metadata alone.
228+
with left.open("rb") as left_file, right.open("rb") as right_file:
229+
while True:
230+
chunk = left_file.read(65536)
231+
if chunk != right_file.read(65536):
232+
return False
233+
if not chunk:
234+
return True
235+
236+
225237
def _dircmp_is_same(dcmp: filecmp.dircmp) -> bool:
226238
"""Recursively check if two directories are identical."""
227-
if dcmp.diff_files or dcmp.left_only or dcmp.right_only:
239+
if dcmp.left_only or dcmp.right_only or dcmp.common_funny:
228240
return False
229241

242+
for name in dcmp.common_files:
243+
if not _files_are_same(
244+
pathlib.Path(dcmp.left) / name, pathlib.Path(dcmp.right) / name
245+
):
246+
return False
247+
230248
for subdir in dcmp.subdirs.values():
231249
if not _dircmp_is_same(subdir):
232250
return False
@@ -236,14 +254,17 @@ def _dircmp_is_same(dcmp: filecmp.dircmp) -> bool:
236254

237255
def compare_paths(cpython_path: pathlib.Path, local_path: pathlib.Path) -> bool:
238256
"""Compare a CPython path with a local path (file or directory)."""
239-
if not local_path.exists():
257+
if not cpython_path.exists() or not local_path.exists():
240258
return False
241259

242-
if cpython_path.is_file():
243-
return filecmp.cmp(cpython_path, local_path, shallow=False)
244-
245-
dcmp = filecmp.dircmp(cpython_path, local_path)
246-
return _dircmp_is_same(dcmp)
260+
try:
261+
if cpython_path.is_file():
262+
return local_path.is_file() and _files_are_same(cpython_path, local_path)
263+
if not cpython_path.is_dir() or not local_path.is_dir():
264+
return False
265+
return _dircmp_is_same(filecmp.dircmp(cpython_path, local_path))
266+
except OSError:
267+
return False
247268

248269

249270
def compare_file_contents(

‎scripts/update_lib/tests/test_deps.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,26 @@ def test_filters_nonexistent(self):
325325
class TestDircmpIsSame(unittest.TestCase):
326326
"""Tests for _dircmp_is_same function."""
327327

328+
def test_matching_metadata(self):
329+
import os
330+
331+
from update_lib.file_utils import compare_paths
332+
333+
with tempfile.TemporaryDirectory() as tmpdir:
334+
left, right = (pathlib.Path(tmpdir) / name for name in ("left", "right"))
335+
for directory in (left, right):
336+
(directory / "nested").mkdir(parents=True)
337+
(directory / "nested/module.py").write_bytes(b"x = 1\n")
338+
source, destination = (p / "nested/module.py" for p in (left, right))
339+
timestamp = source.stat().st_mtime_ns
340+
os.utime(destination, ns=(timestamp, timestamp))
341+
self.assertTrue(compare_paths(source, destination))
342+
self.assertTrue(compare_paths(left, right))
343+
destination.write_bytes(b"x = 2\n")
344+
os.utime(destination, ns=(timestamp, timestamp))
345+
self.assertFalse(compare_paths(source, destination))
346+
self.assertFalse(compare_paths(left, right))
347+
328348
def test_identical_directories(self):
329349
"""Test that identical directories return True."""
330350
import filecmp

0 commit comments

Comments
 (0)