Skip to content
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

fix: decode bytes not str (easy_xml.py) #160

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
5 changes: 4 additions & 1 deletion pylib/gyp/easy_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ def WriteXmlIfChanged(content, path, encoding="utf-8", pretty=False,

default_encoding = locale.getdefaultlocale()[1]
if default_encoding and default_encoding.upper() != encoding.upper():
xml_string = xml_string.encode(encoding)
if sys.platform == "win32":
if isinstance(xml_string, str):
xml_string = xml_string.decode("cp1251") # str --> bytes
Copy link
Member

Choose a reason for hiding this comment

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

This seems incorrect to me. XmlToString always returns a str and a str object has no attribute decode.

Copy link
Contributor

Choose a reason for hiding this comment

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

Correct: str.encode() and bytes.decode() exist but the opposites are undefined.

>>> for s_or_b in ("s", b"b"):
...     for method_name in ("encode", "decode"):
...         print(f"{s_or_b}: {method_name} is {getattr(s_or_b, method_name, None)}")
...
s: encode is <built-in method encode of str object at 0x103cbe928>
s: decode is None
b'b': encode is None
b'b': decode is <built-in method decode of bytes object at 0x103cb4bf0>

xml_string = xml_string.encode(encoding) # bytes --> str

# Get the old content
try:
Expand Down