Skip to content

Commit 1b92fa8

Browse files
committed
fix: platform: import _wmi on first use
Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com>
1 parent 301643a commit 1b92fa8

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

‎Lib/platform.py‎

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

122122
### Globals & Constants
123123

@@ -332,6 +332,12 @@ def _syscmd_ver(system='', release='', version='',
332332

333333
def _wmi_query(table, *keys):
334334
global _wmi
335+
if _wmi is None:
336+
try:
337+
import _wmi as wmi
338+
except ImportError:
339+
wmi = False
340+
_wmi = wmi
335341
if not _wmi:
336342
raise OSError("not supported")
337343
table = {
@@ -344,7 +350,7 @@ def _wmi_query(table, *keys):
344350
table,
345351
)).split("\0")
346352
except OSError:
347-
_wmi = None
353+
_wmi = False
348354
raise OSError("not supported")
349355
split_data = (i.partition("=") for i in data)
350356
dict_data = {i[0]: i[2] for i in split_data}

0 commit comments

Comments
 (0)