FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-45382: test.pythoninfo: set wmic.exe encoding to OEM by vstinner · Pull Request #30890 · python/cpython · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 3 additions & 0 deletions Lib/test/pythoninfo.py
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
Expand Up @@ -731,9 +731,12 @@ def collect_windows(info_add):

import subprocess
try:
# When wmic.exe output is redirected to a pipe,
# it uses the OEM code page
proc = subprocess.Popen(["wmic", "os", "get", "Caption,Version", "/value"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="oem",
Comment on lines +734 to +739

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Some of Microsoft's tools support formatting output as XML or JSON, which can generally be parsed more reliably. For example:

import subprocess
import xml.etree.ElementTree as ET

p = subprocess.run('wmic.exe os get Caption,Version /format:rawxml', 
        capture_output=True, encoding='oem')
root = ET.fromstring(p.stdout)
caption = root.find('.//PROPERTY[@NAME="Caption"]/VALUE').text
version = root.find('.//PROPERTY[@NAME="Version"]/VALUE').text

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Please propose a PR if you prefer to parse XML than plain text. I don't want to invest too much time in this code, since I'm not using Windows :-)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Well, maybe a more "correct" implementation is required if the function enters the stdlib platform module, but this PR is only about test.pythoninfo which is only used ... for tests ;-)

text=True)
output, stderr = proc.communicate()
if proc.returncode:
Expand Down

Back | FazBrowse Home | New Git URL