Skip to content

gh-161513: Import _wmi lazily in platform - #161656

Open
azchohfi wants to merge 1 commit into
python:mainfrom
azchohfi:gh-161513-lazy-wmi
Open

azchohfi wants to merge 1 commit into
python:mainfrom
azchohfi:gh-161513-lazy-wmi

Conversation

@azchohfi

@azchohfi azchohfi commented Nov 9, 2026 •

Copy link
Copy Markdown

This replaces #161543, following @sobolevn's request and the feedback from the previous review.

The main change is to import _wmi lazily, so import platform no longer has to load it upfront. I've also made sure failed queries aren't cached, added a test using ensure_lazy_imports, and included the suggested NEWS entry.

I added a Windows-only guard around the lazy import. Without it, applications using a lazy import filter that rejects _wmi can fail to import platform on non-Windows systems. I reproduced this on Linux and verified that the guard fixes it.

On Windows 11, I measured import platform going from 27.3 ms to 16.4 ms over 100 alternating runs. I also checked that the results returned by platform remain unchanged.

test_platform passes on Windows and Linux, and test_wmi passes on Windows (it's skipped on Linux).

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

@Aniketsy Aniketsy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@azchohfi Thank you :) ... LGTM 👍

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

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants