-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-89545: Adds internal _wmi module on Windows for directly querying OS properties #96289
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
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6b22600
gh-89545: Adds internal _wmi module on Windows for directly querying …
zooba 3bd7f9d
Merge remote-tracking branch 'cpython/main' into gh-89545
zooba ba6ad69
Regenerate global objects
zooba 39a8555
Improve error handling and block non-SELECT queries
zooba 7db086b
Improved error handling and audit tests
zooba da37548
Update platform module to use _wmi
zooba 1264330
Update NEWS
zooba 6179c2f
Merge remote-tracking branch 'cpython/main' into gh-89545
zooba 8a8e950
Only use version when it's not empty
zooba a77841f
Apply suggestions from code review
zooba 9002d8e
Additional PR feedback
zooba aef3a4d
Update sys.getwindowsversion to use better calculation
zooba 295a0ac
Merge remote-tracking branch 'cpython/main' into gh-89545
zooba 6b32d88
Add extra null separator between instances
zooba 04330f8
Update PC/_wmimodule.cpp
zooba c3f9e9c
Add missing error handling
zooba 98e3080
Merge remote-tracking branch 'cpython/main' into gh-89545
zooba 70b2518
Remove WMI usage from sys.getwindowsversion
zooba 653f152
Merge branch 'main' into gh-89545
zooba 9d77094
Apply suggestions from code review
zooba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Test the internal _wmi module on Windows | ||
# This is used by the platform module, and potentially others | ||
|
||
import re | ||
import sys | ||
import unittest | ||
from test.support import import_helper | ||
|
||
|
||
# Do this first so test will be skipped if module doesn't exist | ||
_wmi = import_helper.import_module('_wmi', required_on=['win']) | ||
|
||
|
||
class WmiTests(unittest.TestCase): | ||
def test_wmi_query_os_version(self): | ||
r = _wmi.exec_query("SELECT Version FROM Win32_OperatingSystem").split("\0") | ||
self.assertEqual(1, len(r)) | ||
k, eq, v = r[0].partition("=") | ||
self.assertEqual("=", eq, r[0]) | ||
self.assertEqual("Version", k, r[0]) | ||
# Best we can check for the version is that it's digits, dot, digits, anything | ||
# Otherwise, we are likely checking the result of the query against itself | ||
self.assertTrue(re.match(r"\d+\.\d+.+$", v), r[0]) | ||
|
||
def test_wmi_query_repeated(self): | ||
# Repeated queries should not break | ||
for _ in range(10): | ||
self.test_wmi_query_os_version() | ||
|
||
def test_wmi_query_error(self): | ||
# Invalid queries fail with OSError | ||
try: | ||
_wmi.exec_query("SELECT InvalidColumnName FROM InvalidTableName") | ||
except OSError as ex: | ||
if ex.winerror & 0xFFFFFFFF == 0x80041010: | ||
# This is the expected error code. All others should fail the test | ||
return | ||
self.fail("Expected OSError") | ||
|
||
def test_wmi_query_repeated_error(self): | ||
for _ in range(10): | ||
self.test_wmi_query_error() | ||
|
||
def test_wmi_query_not_select(self): | ||
# Queries other than SELECT are blocked to avoid potential exploits | ||
with self.assertRaises(ValueError): | ||
_wmi.exec_query("not select, just in case someone tries something") | ||
|
||
def test_wmi_query_overflow(self): | ||
# Ensure very big queries fail | ||
# Test multiple times to ensure consistency | ||
for _ in range(2): | ||
with self.assertRaises(OSError): | ||
_wmi.exec_query("SELECT * FROM CIM_DataFile") |
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Windows/2022-08-26-00-11-18.gh-issue-89545.zmJMY_.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Updates :mod:`platform` to use native WMI queries to determine OS version, type, and architecture. | ||
zooba marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.