Skip to content

Commit 81c0bbe

Browse files
committed
gh-161513: Resolve _wmi lazily in platform.py
Adopt the 'lazy import _wmi' form suggested by sobolevn. A failed lazy import is retried on every access, so _wmi_query() binds _wmi = None on ImportError to record that it is unavailable. A failed WMI query still sets _wmi = False so it is not retried.
1 parent 1b92fa8 commit 81c0bbe

3 files changed

Lines changed: 27 additions & 9 deletions

File tree

‎Lib/platform.py‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@
114114
import functools
115115
import itertools
116116
lazy import warnings
117-
# _wmi is imported on first use (see _wmi_query): loading it maps the OLE
117+
# _wmi is resolved on first use (see _wmi_query): loading it maps the OLE
118118
# and property-system DLLs, which most users of this module never need.
119-
# None means not imported yet, False means unavailable.
120-
_wmi = None
119+
lazy import _wmi
121120

122121
### Globals & Constants
123122

@@ -332,12 +331,12 @@ def _syscmd_ver(system='', release='', version='',
332331

333332
def _wmi_query(table, *keys):
334333
global _wmi
335-
if _wmi is None:
336-
try:
337-
import _wmi as wmi
338-
except ImportError:
339-
wmi = False
340-
_wmi = wmi
334+
try:
335+
# Force the deferred import. A failed lazy import would be retried
336+
# on every access, so bind None to record that it is unavailable.
337+
_wmi
338+
except ImportError:
339+
_wmi = None
341340
if not _wmi:
342341
raise OSError("not supported")
343342
table = {

‎Lib/test/test_platform.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,22 @@ def test_win32_ver_on_non_windows(self):
436436
res = platform.win32_ver(release, version, csd, ptype)
437437
self.assertSequenceEqual(res, (release, version, csd, ptype), seq_type=tuple)
438438

439+
@unittest.skipUnless(support.MS_WINDOWS, 'This test only makes sense on Windows')
440+
def test_wmi_is_lazily_imported(self):
441+
# gh-161513: importing platform must not import the _wmi
442+
# extension module; it is resolved on the first WMI query.
443+
# A subprocess is used to get a clean sys.modules.
444+
code = (
445+
"import platform, sys\n"
446+
"assert '_wmi' not in sys.modules\n"
447+
"platform.win32_ver()\n"
448+
"assert '_wmi' in sys.modules or not platform._wmi\n"
449+
)
450+
p = subprocess.run([sys.executable, "-I", "-c", code],
451+
capture_output=True, text=True)
452+
if p.returncode != 0:
453+
self.fail(p.stderr)
454+
439455
def test_mac_ver(self):
440456
res = platform.mac_ver()
441457

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:mod:`platform` no longer imports the ``_wmi`` extension module eagerly on
2+
Windows. It is now resolved on first use, speeding up ``import platform``
3+
by not loading the OLE and property-system DLLs when WMI is not needed.

0 commit comments

Comments
 (0)