Skip to content

Commit 2a3ce9a

Browse files
committed
Use the _wmi module in test.pythoninfo
On Windows, replace wmic command with _wmi module to get the operating system caption and version. The wmic tool is deprecated since January 2024: https://techcommunity.microsoft.com/blog/windows-itpro-blog/wmi-command-line-wmic-utility-deprecation-next-steps/4039242 For example, it's no longer installed in Windows images on GitHub Action. Fix also run_command(): no longer try to spawn a subprocess if the platform doesn't support subprocess. It avoids logging run_command() errors.
1 parent efcfb1a commit 2a3ce9a

1 file changed

Lines changed: 34 additions & 16 deletions

File tree

Lib/test/pythoninfo.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,12 @@ def format_attr(attr, value):
452452

453453
def run_command(cmd, check=True, **kwargs):
454454
import subprocess
455+
from test.support import has_subprocess_support
456+
457+
if not has_subprocess_support:
458+
# subprocess is not supported by the current platform
459+
return ''
460+
455461
timeout = COMMAND_TIMEOUT
456462

457463
cmd_str = ' '.join(cmd)
@@ -963,6 +969,25 @@ def winreg_query(path):
963969
return None
964970

965971

972+
def wmi_get_os():
973+
try:
974+
import _wmi
975+
except ImportError:
976+
return
977+
978+
query = "SELECT Caption, Version FROM Win32_OperatingSystem"
979+
try:
980+
data = _wmi.exec_query(query)
981+
except OSError:
982+
return
983+
984+
dict_data = {}
985+
for item in data.split("\0"):
986+
key, _, value = item.partition("=")
987+
dict_data[key] = value
988+
return dict_data
989+
990+
966991
def collect_windows(info_add):
967992
if not MS_WINDOWS:
968993
# Code specific to Windows
@@ -1009,22 +1034,15 @@ def collect_windows(info_add):
10091034
call_func(info_add, 'windows.ansi_code_page', _winapi, 'GetACP')
10101035
call_func(info_add, 'windows.oem_code_page', _winapi, 'GetOEMCP')
10111036

1012-
# windows.version_caption: "wmic os get Caption,Version /value" command
1013-
output = run_command(["wmic", "os", "get", "Caption,Version", "/value"],
1014-
# When wmic.exe output is redirected to a pipe,
1015-
# it uses the OEM code page
1016-
encoding="oem")
1017-
if output:
1018-
for line in output.splitlines():
1019-
line = line.strip()
1020-
if line.startswith('Caption='):
1021-
line = line.removeprefix('Caption=').strip()
1022-
if line:
1023-
info_add('windows.version_caption', line)
1024-
elif line.startswith('Version='):
1025-
line = line.removeprefix('Version=').strip()
1026-
if line:
1027-
info_add('windows.version', line)
1037+
# Get operating system caption and version using WMI
1038+
data = wmi_get_os()
1039+
if data:
1040+
caption = data.get('Caption', '')
1041+
if caption:
1042+
info_add('windows.version_caption', caption)
1043+
version = data.get('Version', '')
1044+
if version:
1045+
info_add('windows.version', version)
10281046

10291047
# windows.ver: "ver" command
10301048
output = run_command(["ver"], shell=True)

0 commit comments

Comments
 (0)