You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
zipimport reads and unmarshals pyc's twice per import #161644
Importing a module from a .pyc in a zip runs marshal.loads() on it twice: once in find_spec() (get_filename() → _get_module_code()) and throws the code object away, and again in exec_module().
get_filename() only needs the 16-byte header to decide whether to use the pyc or not. a bad header (magic, flags, stale timestamp/size, hash mismatch) makes zipimport fall back to the sibling .py. errors in the marshalled body don't, they just fail the import. we can validate the header and stop there.
while making this change would be user-visible (e.g., a pyc with a good header and a corrupt body will fail in exec_module() rather than find_spec()), this is similar to gh-153799 (issue gh-141276), so should be acceptable (especially if we don't backport this one).
testing this with a draft patch on a release build, avoiding the throw-away unmarshal yields up to 50% speedup in imports from pycs in zips, while making find_spec constant time instead of O(pyc size).
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Activity
itamaro commentedon Nov 8, 2026
I'll open a PR with more benchmarking details based on the draft patch I mentioned, if there are no objections
pythongh-161644: Only validate the pyc header in zipimporter.get_file…
pythongh-161644: Only validate the pyc header in zipimporter.get_file…
pythongh-161644: Only validate the pyc header in zipimporter.get_file…
pythongh-161644: Only validate the pyc header in zipimporter.get_file…
pythongh-161644: Avoid unmarshalling zipped .pyc files twice on import
pythongh-161644: Avoid unmarshalling zipped .pyc files twice on import
pythongh-161644: Avoid unmarshalling zipped .pyc files twice on import