8000 gh-112278: reduce time cost for platform module when no permission to WMI on Windows by aisk · Pull Request #112658 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-112278: reduce time cost for platform module when no permission to WMI on Windows #112658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor _wmi_query to handle unsupport condition
  • Loading branch information
aisk committed Dec 5, 2023
commit f1ee7976ed6fb39bf3a73299e5e2f996e4a9d479
47 changes: 24 additions & 23 deletions Lib/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@
import sys
import functools
import itertools
try:
import _wmi
except ImportError:
_wmi = None

### Globals & Constants

Expand Down Expand Up @@ -312,30 +316,27 @@ def _syscmd_ver(system='', release='', version='',
version = _norm_version(version)
return system, release, version

try:
import _wmi
except ImportError:
def _wmi_query(table, *keys):

def _wmi_query(table, *keys):
if not _wmi:
raise OSError("not supported")
else:
def _wmi_query(table, *keys):
table = {
"OS": "Win32_OperatingSystem",
"CPU": "Win32_Processor",
}[table]
try:
data = _wmi.exec_query("SELECT {} FROM {}".format(
",".join(keys),
table,
)).split("\0")
except OSError:
global _wmi_query
def _wmi_query(table, *keys):
raise OSError("not supported")
raise OSError("not supported")
split_data = (i.partition("=") for i in data)
dict_data = {i[0]: i[2] for i in split_data}
return (dict_data[k] for k in keys)

table = {
"OS": "Win32_OperatingSystem",
"CPU": "Win32_Processor",
}[table]
try:
data = _wmi.exec_query("SELECT {} FROM {}".format(
",".join(keys),
table,
)).split("\0")
except OSError:
global _wmi
_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}
return (dict_data[k] for k in keys)


_WIN32_CLIENT_RELEASES = [
Expand Down
0