Skip to content

Commit

Permalink
fix: decode bytes not str
Browse files Browse the repository at this point in the history
proper fix for nodejs#118
Special thanks to @cclauss
  • Loading branch information
owl-from-hogvarts committed Sep 7, 2022
1 parent baef940 commit 67393c1
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pylib/gyp/easy_xml.py
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
xml_string = xml_string.encode(encoding) # bytes --> str

This comment has been minimized.

Copy link
@cclauss

cclauss Sep 8, 2022

str.encode() exists and bytes.decode() exists but the opposites do NOT exist. This is on purpose.

https://docs.python.org/3/howto/unicode.html#converting-to-bytes

The opposite method of bytes.decode() is str.encode(), which returns a bytes representation of the Unicode string, encoded in the requested encoding.

A way to remember this is "We decode bytes so that humans can read the strings and we encode strings so that computers can read the bytes."


# Get the old content
try:
Expand Down

0 comments on commit 67393c1

Please sign in to comment.