Repository navigation
Write history files and read pyscripts as UTF-8 #1836
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
Open
Alpenj
wants to merge
1
commit into
python-cmd2:main
Choose a base branch
from
Alpenj:fix-history-pyscript-utf8
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -262,3 +262,26 @@ def test_run_pyscript_print_redirection(base_app, request, tmp_path, capsys) -> | |
| # Verify the remaining output when to the correct stream | ||
| assert "this goes to sys.stdout" in out | ||
| assert "this goes to sys.stderr" in err | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("encoding", ["utf-8", "utf-8-sig"]) | ||
| def test_run_pyscript_reads_utf8(base_app, tmp_path, monkeypatch, encoding) -> None: | ||
| import cmd2.cmd2 | ||
|
|
||
| real_open = open | ||
|
|
||
| def open_cp1252(file, mode="r", *args, **kwargs): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this |
||
| # Without an explicit encoding, open() uses cp1252, as it does on a non-UTF-8 Windows locale | ||
| if "b" not in mode and not args: | ||
| kwargs.setdefault("encoding", "cp1252") | ||
| return real_open(file, mode, *args, **kwargs) | ||
|
|
||
| monkeypatch.setattr(cmd2.cmd2, "open", open_cp1252, raising=False) | ||
| python_script = tmp_path / "script.py" | ||
| python_script.write_text('print("Łukasz ✨")\n', encoding=encoding) | ||
|
|
||
| out, err = run_cmd(base_app, f'run_pyscript "{python_script}"') | ||
|
|
||
| assert err == [] | ||
| assert out == ["Łukasz ✨"] | ||
| assert base_app.last_result is True | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python reads a source file as UTF-8 by default, but it also honors a BOM and a PEP 263
# -*- coding: ... -*-line. Hard-codingutf-8-sigcovers the first two cases but ignores the coding line. So a script like this:now fails with an unhandled
UnicodeDecodeError. That error isn't anOSError, so the user never sees theError reading script file ...message and gets a generic exception instead. On a cp1252 locale, that script used to run.tokenize.open()reads a file the way Python does: BOM first, then the coding line, otherwise UTF-8. It also strips the BOM, so it can replace the current call directly:tokenize.detect_encoding()raisesSyntaxErrorwhen the coding line names an unknown encoding, which is why it's in theexcept. I made this change locally: bothtest_run_pyscript_reads_utf8cases still pass, and the latin-1 script above printscafé. Could you also add a test for a script with a coding line, and one for a script that can't be decoded and so goes through theperrorpath?