Repository navigation
Conversation
`get_filename()` (and so `find_spec()`) read, decompressed and unmarshalled the whole `.pyc` just to learn the file name, and `exec_module()` then did it all again. Only the 16-byte header matters for picking the file: a bad or stale header falls back to the sibling `.py`, while errors in the marshalled body never do. Split the header checks out of `_unmarshal_code()` and have `get_filename()` run only those, reading (and decompressing) just enough of the member to cover the header. A `.pyc` with a valid header and a corrupt body now fails in `exec_module()` instead of `find_spec()`, with the same exception. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Documentation build overview
79 files changed ·
|
| m.loads.assert_called_once() | ||
| self.assertEqual(mod.get_file(), expected_path) | ||
|
|
||
| def test_corrupt_pyc_body_defers_to_exec(self): |
There was a problem hiding this comment.
Doesn't the test above help make sure the read is deferred? And do we need to state where the failure is picked up as much as making sure the exception is raised when going through all the steps for an import?
There was a problem hiding this comment.
answering all the test-related comments:
all the new tests were related, for some definition of "related", but they were an overkill covering engineered edge cases, so I cut those you flagged as unrelated.
the "lazy imports" comments are also moot - gone with the deleted tests.
Doesn't the test above help make sure the read is deferred?
the tests had overlap, which is gone now. I kept test_corrupt_pyc_body_defers_to_exec to cover the read deferral.
And do we need to state where the failure is picked up as much as making sure the exception is raised when going through all the steps for an import?
I think so, this guards the invariant introduced here - no error raised until the exec.
| except Exception: | ||
| if compress == _ZIP_DEFLATED: | ||
| raise | ||
| raise ZipImportError("could not decompress zstd data") |
There was a problem hiding this comment.
The globals list 4 decompression methods but this assumes only 2 and with a very direct fallthrough. Otherwise the comment for the globals is misleading.
There was a problem hiding this comment.
I dropped the global constants. they were somewhat misleading. I thought they would improve readability by replacing the magic numbers with named constants, but it just made it less clear because I needed to replace match-case with if-else's. so I went back to match-case with literal values, and used the same pattern in _decompress_prefix.
Keep a minimal subset that covers most of what the previous set covered
zipimporter.get_filename()(and thereforefind_spec()) read, decompressed and unmarshalled the whole.pycjust to determine the module's file name, andexec_module()then did it all again. Only the 16-byte header matters for picking the file: a bad or stale header falls back to the sibling.py, while errors in the marshalled body never do.This splits the header checks out of
_unmarshal_code()and hasget_filename()run only those, reading (and for deflate/zstd members, decompressing) just enough of the member to cover the header.Observable change: a
.pycwith a valid header and a corrupt body now fails inexec_module()instead offind_spec(), with the same exception. GH-153799 made the equivalent change for aSyntaxErrorin source modules.Benchmarks
Release build, Linux, warm page cache, timestamp-based pycs.
Importing 27 stdlib packages (148 modules) from a zip of ~350 stdlib modules with
.pycnext to.py, min of 40 runs:Per module,
find_spec()+get_code()for a single.pyc(in-process medians):find_spec()itself no longer scales with pyc size: ~0.1 ms regardless of size, versus 43 ms for a 5 MB pyc onmain.zstd gains less because
ZstdDecompressorconstruction dominates small reads and zstd decodes a whole block before producing output.