From d50a2ec57e8341961213cb0ac4c6b95a537de410 Mon Sep 17 00:00:00 2001 From: Alexandre Zollinger Chohfi Date: Sun, 8 Nov 2026 11:21:02 -0800 Subject: [PATCH 1/2] gh-161513: Import _wmi lazily in platform --- Lib/platform.py | 13 +++++++------ Lib/test/test_platform.py | 6 +++++- .../2026-11-08-19-11-28.gh-issue-161513.LdU7T4.rst | 1 + 3 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-08-19-11-28.gh-issue-161513.LdU7T4.rst diff --git a/Lib/platform.py b/Lib/platform.py index c87bf2fca37e03..c8f67c66d4d7b5 100644 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -114,9 +114,9 @@ import functools import itertools lazy import warnings -try: - import _wmi -except ImportError: +if sys.platform == 'win32': + lazy import _wmi +else: _wmi = None ### Globals & Constants @@ -331,8 +331,10 @@ def _syscmd_ver(system='', release='', version='', def _wmi_query(table, *keys): - global _wmi - if not _wmi: + try: + if _wmi is None: # Also forces the deferred import. + raise OSError("not supported") + except ImportError: raise OSError("not supported") table = { "OS": "Win32_OperatingSystem", @@ -344,7 +346,6 @@ def _wmi_query(table, *keys): table, )).split("\0") except OSError: - _wmi = None raise OSError("not supported") split_data = (i.partition("=") for i in data) dict_data = {i[0]: i[2] for i in split_data} diff --git a/Lib/test/test_platform.py b/Lib/test/test_platform.py index cf3ca9021eea3f..77d6b3bce4f66b 100644 --- a/Lib/test/test_platform.py +++ b/Lib/test/test_platform.py @@ -11,7 +11,7 @@ from unittest import mock from test import support -from test.support import os_helper, warnings_helper +from test.support import import_helper, os_helper, warnings_helper try: # Some of the iOS tests need ctypes to operate. @@ -436,6 +436,10 @@ def test_win32_ver_on_non_windows(self): res = platform.win32_ver(release, version, csd, ptype) self.assertSequenceEqual(res, (release, version, csd, ptype), seq_type=tuple) + @support.cpython_only + def test_wmi_is_lazily_imported(self): + import_helper.ensure_lazy_imports('platform', {'_wmi'}) + def test_mac_ver(self): res = platform.mac_ver() diff --git a/Misc/NEWS.d/next/Library/2026-11-08-19-11-28.gh-issue-161513.LdU7T4.rst b/Misc/NEWS.d/next/Library/2026-11-08-19-11-28.gh-issue-161513.LdU7T4.rst new file mode 100644 index 00000000000000..8e2360be613141 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-08-19-11-28.gh-issue-161513.LdU7T4.rst @@ -0,0 +1 @@ +:mod:`platform` is now about 30% faster to import on Windows. From 261012390b1c6d240db8e09e162209e33b14c4df Mon Sep 17 00:00:00 2001 From: Alexandre Zollinger Chohfi Date: Fri, 27 Nov 2026 08:22:52 -0800 Subject: [PATCH 2/2] Keep caching a failed WMI query Without it, every call waits on WMI again when the user can't query it. --- Lib/platform.py | 2 ++ Lib/test/test_platform.py | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/Lib/platform.py b/Lib/platform.py index c8f67c66d4d7b5..8dacf0cc5b8513 100644 --- a/Lib/platform.py +++ b/Lib/platform.py @@ -331,6 +331,7 @@ def _syscmd_ver(system='', release='', version='', def _wmi_query(table, *keys): + global _wmi try: if _wmi is None: # Also forces the deferred import. raise OSError("not supported") @@ -346,6 +347,7 @@ def _wmi_query(table, *keys): table, )).split("\0") except OSError: + _wmi = None raise OSError("not supported") split_data = (i.partition("=") for i in data) dict_data = {i[0]: i[2] for i in split_data} diff --git a/Lib/test/test_platform.py b/Lib/test/test_platform.py index 77d6b3bce4f66b..0824a10f725aa4 100644 --- a/Lib/test/test_platform.py +++ b/Lib/test/test_platform.py @@ -440,6 +440,19 @@ def test_win32_ver_on_non_windows(self): def test_wmi_is_lazily_imported(self): import_helper.ensure_lazy_imports('platform', {'_wmi'}) + def test_wmi_query_failure_is_cached(self): + calls = [] + class FakeWmi: + def exec_query(self, query): + calls.append(query) + raise OSError("access denied") + + with support.swap_attr(platform, '_wmi', FakeWmi()): + for _ in range(2): + with self.assertRaises(OSError): + platform._wmi_query("OS", "Version") + self.assertEqual(len(calls), 1) + def test_mac_ver(self): res = platform.mac_ver()