platform: import _wmi on first use
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 76/100
- Issue 类型
- 功能
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- python
- 领域
- backend, performance
调研方向
阅读 Lib/platform.py,尤其是模块级的 _wmi 导入和 _wmi_query();issue 中包含一项拟议更改和测得的行为。检查 test_platform 和 test_wmi 测试,然后确认导入 platform 时不会加载 _wmi,并且所述 Windows 辅助函数仍返回相同的值。
由索引模型根据 Issue 内容生成。
描述
Feature or enhancement
Lib/platform.py imports _wmi at module level on Windows. _wmi.pyd loads oleaut32.dll and propsys.dll (with _wmi.pyd itself and their dependencies, 7 more images in the process), but WMI is only queried by win32_ver(), version(), release(), uname() and the processor/machine helpers. Code that only imports platform, or calls python_version() or system(), pays for it anyway. pytest is one example: it imports platform on every run.
Moving the import into _wmi_query() (keeping the "unavailable" state so a failed import or query is not retried):
| 60 runs each, perf_counter around the import | before | after | difference |
|---|---|---|---|
import platform |
18.6 ms | 12.9 ms | −5.5 ms [−6.0, −5.1] |
import pytest |
206 ms | 196 ms | −9.5 ms [−13.6, −4.7] |
(python-build-standalone 3.14.4, Windows 11 26100.) win32_ver(), uname(), machine(), version() and processor() return the same values; test_platform and test_wmi pass.
The change, against main (2781ffe1f2):
diff --git a/Lib/platform.py b/Lib/platform.py
index c87bf2f..375acc2 100644
--- a/Lib/platform.py
+++ b/Lib/platform.py
@@ -114,10 +114,10 @@ import sys
import functools
import itertools
lazy import warnings
-try:
- import _wmi
-except ImportError:
- _wmi = None
+# _wmi is imported on first use (see _wmi_query): loading it maps the OLE
+# and property-system DLLs, which most users of this module never need.
+# None means not imported yet, False means unavailable.
+_wmi = None
### Globals & Constants
@@ -332,6 +332,12 @@ def _syscmd_ver(system='', release='', version='',
def _wmi_query(table, *keys):
global _wmi
+ if _wmi is None:
+ try:
+ import _wmi as wmi
+ except ImportError:
+ wmi = False
+ _wmi = wmi
if not _wmi:
raise OSError("not supported")
table = {
@@ -344,7 +350,7 @@ def _wmi_query(table, *keys):
table,
)).split("\0")
except OSError:
- _wmi = None
+ _wmi = False
raise OSError("not supported")
split_data = (i.partition("=") for i in data)
dict_data = {i[0]: i[2] for i in split_data}
(PEP 810's lazy import doesn't fit here because the import sits in try/except ImportError.)
I used GitHub Copilot (Claude Opus 5.5 model) to trace this, build and time the prototype, and draft this issue. The measurements were run on my machine.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
- 主要语言
- Python
- 星标
- 77.9k
- 派生
- 37.1k
- 平均合并
- 17 小时 43 分钟
- 30 天内合并 PR
- 675
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/cpython 的其他 Issue
-
type-bug
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复
-
OS-windows performance stdlib type-feature
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
build OS-unsupported OS-windows type-bug
难度 2/5 1-3 小时 新手友好度 60/100
维护者通常 1 天内回复
-
What's New in 3.12 credits the unstable C API tier to PEP 697 instead of PEP 689可能已有人在做 @TheLouisMahdi 于 4 天前认领。 未关闭docs
难度 1/5 1 小时以内 新手友好度 82/100
python/cpython#161078 · 3 条评论 ·
维护者通常 1 天内回复
-
OS-windows type-bug
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
相似的 Issue
-
docs size:S
难度 1/5 1 小时以内 新手友好度 88/100
维护者通常 1 天内回复
-
documentation
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 74/100
strawberry-graphql/strawberry#4794 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
dataforgoodfr/cetolog#21 ·
-
难度 2/5 1-3 小时 新手友好度 76/100
btclib-org/btclib#2932 ·
维护者通常 1 天内回复