Skip to content

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

Merged
lukekarrys merged 2 commits into
nodejs:mainfrom
JaneaSystems:huseyin-python-encoding-utf-8
Mar 13, 2024
Merged

lib: print Python executable path using UTF-8#2995
lukekarrys merged 2 commits into
nodejs:mainfrom
JaneaSystems:huseyin-python-encoding-utf-8

Conversation

@huseyinacacak-janea

Copy link
Copy Markdown
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

@cclauss cclauss left a comment

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.

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'

Comment thread lib/find-python.js
@huseyinacacak-janea

Copy link
Copy Markdown
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

cclauss commented Mar 13, 2024

Copy link
Copy Markdown
Contributor

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"))
ü

@lukekarrys
lukekarrys merged commit c472912 into nodejs:main Mar 13, 2024
lukekarrys pushed a commit that referenced this pull request Dec 2, 2024
* lib: print Python executable path using UTF-8

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: #2829

* fixup! lib: print Python executable path using 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