platform: import _wmi on first use

未關閉 適合新手
#161,513 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
76/100
Issue 類型
功能
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
python

研究方向

閱讀 Lib/platform.py,尤其是模組層級的 _wmi 匯入和 _wmi_query();issue 中包含一項提議的變更和測得的行為。檢查 test_platform 和 test_wmi 測試,然後確認匯入 platform 時不會載入 _wmi,且所述的 Windows 輔助函式仍傳回相同的值。

由索引模型根據 Issue 內容生成。

描述

OS-windows performance stdlib type-feature
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

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。