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

Use UTF-8 encoding for log file. #10072

Merged
merged 2 commits into from
Jul 4, 2021
Merged

Use UTF-8 encoding for log file. #10072

merged 2 commits into from
Jul 4, 2021

Conversation

methane
Copy link
Contributor

@methane methane commented Jun 16, 2021

Fixes #10071.

@uranusjr
Copy link
Member

Is there a way to only do this to new log files? If there is an existing file written not using UTF-8, this will I think pip will crash with this patch since it won’t be able to open the existing file for appending.

@DiddiLeija
Copy link
Member

Is there a way to only do this to new log files? If there is an existing file written not using UTF-8, this will I think pip will crash with this patch since it won’t be able to open the existing file for appending.

Agree. There are many ways to make this crash. But if you just write the new files with UTF-8, but giving the option to dismiss that and read another kind of encoding, that's fine for me.

Example: On logging.py, if you use a try/except to attempt the UTF-8 encoding, but giving the option to ignore (and use a warning to tell the user, if you want to) could help.

@methane
Copy link
Contributor Author

methane commented Jun 17, 2021

Is there a way to only do this to new log files?

It is very difficult. We need to check the encoding of existing file.
But note that the default log file is /dev/null. It is very rare.

If there is an existing file written not using UTF-8, this will I think pip will crash with this patch since it won’t be able to open the existing file for appending.

pip won't crash.

>>> with open("x.txt", "w", encoding="cp932") as f:
...     f.write("こんにちは\n")
...
6
>>> with open("x.txt", "a", encoding="utf-8") as f:
...     f.write("こんにちは\n")
...
6
>>> with open("x.txt", "rb") as f:
...     print(f.read())
...
b'\x82\xb1\x82\xf1\x82\xc9\x82\xbf\x82\xcd\n\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf\n'
>>>

@uranusjr
Copy link
Member

pip won't crash.

Interesting! I did not know this. So encoding errors only happen lazily when something actually needs to be encoded/decoded (i.e. on read()). TIL.

@DiddiLeija
Copy link
Member

Interesting! I did not know this.

Me neither. But after you explain it, I really like the idea.

@pradyunsg pradyunsg merged commit 156f71b into pypa:main Jul 4, 2021
@pradyunsg
Copy link
Member

Thanks @methane! ^>^

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use UTF-8 for log file.
4 participants