Skip to content

gh-161941: Freeze the encodings modules imported at startup - #161951

Open
eendebakpt wants to merge 1 commit into
python:mainfrom
eendebakpt:startup-freeze-encodings-rebased
Open

eendebakpt wants to merge 1 commit into
python:mainfrom
eendebakpt:startup-freeze-encodings-rebased

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Freeze encodings, encodings.aliases, encodings.utf_8 and the platform codec modules again. FrozenImporter now checks that the directory exists. If it does not, the location of a standard library package is looked up on sys.path with PathFinder, so its non-frozen submodules are found wherever the standard library is, including inside a zip file. Frozen packages outside the standard library are not affected. Importing encodings from disk was a large part of interpreter startup on Windows.

Benchmark on the issue.

Freeze encodings, encodings.aliases, encodings.utf_8 and the platform
codec modules again. They were unfrozen because a partially frozen
package broke the import of its non-frozen submodules with a zipped
standard library: FrozenImporter set the package's __path__ to the
package directory in sys._stdlib_dir, which does not exist in that
layout (sys._stdlib_dir is still set, to a missing directory), so
encodings.oem and friends could not be found.

FrozenImporter now checks that the directory exists. If it does not,
the location of a standard library package is looked up on sys.path
with PathFinder, so its non-frozen submodules are found wherever the
standard library is, including inside a zip file. Frozen packages
outside the standard library are not affected.

Importing encodings from disk was the single largest item of
interpreter startup on Windows: it needs the sys.path directory
listings and four .pyc files before the first codec lookup.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35876902 | 📁 Comparing 67c66c4 against main (2c2d801)

  🔍 Preview build  

2 files changed
± whatsnew/3.16.html
± whatsnew/changelog.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant