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

lib: print Python executable path using UTF-8 #2995

Merged
merged 2 commits into from
Mar 13, 2024

Conversation

huseyinacacak-janea
Copy link
Contributor

Checklist
  • npm install && npm run lint && npm test passes
  • commit message follows commit guidelines
Description of change

The Python executable path may have non-ASCII characters, which can make the print function fail if the environment encoding is different. This PR fixes this issue by using stdout.buffer, which can be used with UTF-8 encoding for the output, regardless of the environment encoding.

Fixes: #2829
Refs: #2987

The Python executable path may have non-ASCII characters, which can make
the print function fail if the environment encoding is different. This fixes
this issue by using stdout.buffer, which can be used with UTF-8 encoding
for the output, regardless of the environment encoding.

Fixes: nodejs#2829
Copy link
Contributor

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

Using the Python REPL, can you create an example where print() fails but sys.stdout.buffer.write() succeeds?

% python3

>>> import sys
>>> msg = "Héllø"
>>> print(msg)
Héllø
>>> sys.stdout.buffer.write(msg.encode("utf-8"))
Héllø7
>>> msg.encode("utf-8")
b'H\xc3\xa9ll\xc3\xb8'

@huseyinacacak-janea
Copy link
Contributor Author

Using the Python REPL, can you create an example where print() fails but sys.stdout.buffer.write() succeeds?

% python3

>>> import sys
>>> msg = "Héllø"
>>> print(msg)
Héllø
>>> sys.stdout.buffer.write(msg.encode("utf-8"))
Héllø7
>>> msg.encode("utf-8")
b'H\xc3\xa9ll\xc3\xb8'

Here is my example REPL.

>>> import sys
>>> sys.stdout.reconfigure(encoding="cp850")
>>> msg = "ü"
>>> print(msg)
�
>>> sys.stdout.buffer.write(msg.encode("utf-8"))
ü2
>>>

This PR aims to print the executable with UTF-8 encoding, regardless of the environment encoding. As my environment is UTF-8 by default, I forced it to change the encoding in REPL by adding the line: sys.stdout.reconfigure(encoding="cp850")

@cclauss
Copy link
Contributor

cclauss commented Mar 13, 2024

Nice example! Thanks...

The trailing digit worried me, but now I see it is the return value of the number of bytes written.

>>> sys.stdout.buffer.write(msg.encode("utf-8"))
ü2
>>> _bytes_written = sys.stdout.buffer.write(msg.encode("utf-8"))
ü

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python path cannot handle Unicode characters like ü
3 participants