Skip to content

gh-161510: Lazily import time and encodings._win_cp_codecs at startup - #161732

Open
azchohfi wants to merge 2 commits into
python:mainfrom
azchohfi:gh-161510-lazy-startup
Open

azchohfi wants to merge 2 commits into
python:mainfrom
azchohfi:gh-161510-lazy-startup

Conversation

@azchohfi

@azchohfi azchohfi commented Nov 10, 2026 •

Copy link
Copy Markdown

As discussed in the issue, this uses lazy imports for two modules. time in zipimport is only needed by _parse_dostime. create_win32_code_page_codec in encodings, on Windows only, is needed only for code pages without a codec module.

@aisk is right about time. Logging, subprocess, asyncio, the REPL (through _pyrepl.commands), pip and pytest all import it anyway, so that half only helps short commands like python -c one-liners and scripts that don't use those modules. _win_cp_codecs is different: it stayed unloaded in every case I tried, including the REPL, pip, pytest and asyncio, because it's only needed when a code page has no Python codec.

On Windows 11, python -c pass got about 1.7 ms faster (release build of main, 200 alternating runs).

I added a test for each with ensure_lazy_imports, and both fail without the change. zipimport is frozen, so its test checks that time isn't imported at startup. test_codecs, test_zipimport, test_zipimport_support, test_lazy_import, test_embed, test_frozen, test_time, test_sys and test_cmd_line pass on Windows and Linux.

AI disclosure: I used GitHub Copilot to help with testing and benchmarking.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are internal details that we shouldn't document.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, removed. Could you add the skip news label?

@bedevere-app

bedevere-app Bot commented Nov 10, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@aisk

aisk commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

@StanFromIreland can we merge this now, if it looks good to you?

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants