platform: import _wmi on first use
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 76/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- python
- Lĩnh vực
- backend, performance
Hướng nghiên cứu
Đọc Lib/platform.py, đặc biệt là phần nhập _wmi ở cấp mô-đun và _wmi_query(); issue có đề xuất thay đổi và hành vi đã đo được. Kiểm tra các bài kiểm thử test_platform và test_wmi, sau đó xác nhận rằng việc nhập platform không tải _wmi và các hàm trợ giúp Windows được nêu vẫn trả về cùng các giá trị.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- Python
- Star
- 77.9k
- Fork
- 37.1k
- Merge trung bình
- 17 giờ 43 phút
- Pull request đã merge (30 ngày)
- 675
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của python/cpython
-
type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
OS-windows performance stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
build OS-unsupported OS-windows type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 60/100
Maintainer thường phản hồi trong vòng 1 ngày
-
What's New in 3.12 credits the unstable C API tier to PEP 697 instead of PEP 689Có thể đã có người làm @TheLouisMahdi đã nhận 4 ngày trước. Đang mởdocs
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
python/cpython#161078 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
OS-windows type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của python/cpython
Issue tương tự
-
docs size:S
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
strawberry-graphql/strawberry#4794 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dataforgoodfr/cetolog#21 ·
-
security_citations_test fails against ecc 2026.11.6: SECURITY.md quotes ecc v2026.11.2's linesĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
btclib-org/btclib#2932 ·
Maintainer thường phản hồi trong vòng 1 ngày