Skip to content
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
change the _wmi_query implementation to dummy if it failed first time
  • Loading branch information
aisk committed Dec 3, 2023
commit d1630d1dd9cac9ba57f1fe3ab9a29111efd9ecb6
16 changes: 11 additions & 5 deletions Lib/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,24 @@ def _syscmd_ver(system='', release='', version='',
try:
import _wmi
except ImportError:
def _wmi_query(*keys):
def _wmi_query(table, *keys):
raise OSError("not supported")
else:
def _wmi_query(table, *keys):
table = {
"OS": "Win32_OperatingSystem",
"CPU": "Win32_Processor",
}[table]
data = _wmi.exec_query("SELECT {} FROM {}".format(
",".join(keys),
table,
)).split("\0")
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")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's restructure this import block to make _wmi = None when it fails to import, and then if we fail to query it can clear out _wmi and subsequent calls will act as if it were never imported.

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)
Expand Down