Repository navigation
Conversation
history --output-file and history --edit wrote their files in the locale's encoding, but run_script reads only ASCII or UTF-8 files. On a locale whose encoding is not UTF-8, such as cp949 on Korean Windows, a history with non-ASCII text could not be run again: run_script said the file "is not an ASCII or UTF-8 encoded text file". Both now write UTF-8. run_pyscript read the script in the locale's encoding, so a UTF-8 script with non-ASCII text failed with UnicodeDecodeError or ran with garbled strings. It now reads the script as UTF-8, the default encoding of Python source, and accepts a byte order mark.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1836 +/- ##
=======================================
Coverage 99.75% 99.75%
=======================================
Files 23 23
Lines 6644 6644
=======================================
Hits 6628 6628
Misses 16 16
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
There was a problem hiding this comment.
Thank you for this PR, @Alpenj, and for the detailed write-up and Korean-locale reproduction! This is a real bug on non-UTF-8 Windows locales, and the history half of the fix looks right. history -o and history -e now write the same encoding that run_script / utils.is_text_file() require. Every other text open() in cmd2/ already passes encoding="utf-8", so this closes the remaining gaps. Shadowing open in cmd2.cmd2 to force cp1252 is a nice way to make the tests meaningful on a UTF-8 CI runner.
I left one suggestion on the run_pyscript change, to honor PEP 263 coding declarations via tokenize.open(), and one small nit on the tests.
|
|
||
| try: | ||
| with open(expanded_filename) as f: | ||
| with open(expanded_filename, encoding="utf-8-sig") as f: |
There was a problem hiding this comment.
Python reads a source file as UTF-8 by default, but it also honors a BOM and a PEP 263 # -*- coding: ... -*- line. Hard-coding utf-8-sig covers the first two cases but ignores the coding line. So a script like this:
# -*- coding: latin-1 -*-
print("café") # saved as latin-1now fails with an unhandled UnicodeDecodeError. That error isn't an OSError, so the user never sees the Error 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:
import tokenize
...
try:
with tokenize.open(expanded_filename) as f:
py_code_to_run = f.read()
except (OSError, SyntaxError, UnicodeDecodeError) as ex:
self.perror(f"Error reading script file '{expanded_filename}': {ex}")
return Nonetokenize.detect_encoding() raises SyntaxError when the coding line names an unknown encoding, which is why it's in the except. I made this change locally: both test_run_pyscript_reads_utf8 cases still pass, and the latin-1 script above prints café. 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 the perror path?
|
|
||
| real_open = open | ||
|
|
||
| def open_cp1252(file, mode="r", *args, **kwargs): |
There was a problem hiding this comment.
Nit: this open_cp1252 duplicates the non_utf8_locale fixture in tests/test_history.py. Could you move that fixture to tests/conftest.py and use it in both test files?
On a locale whose encoding is not UTF-8 (for example cp949 on Korean Windows, or cp1252 on Western Windows before Python 3.15), three file paths in
cmd2.pyused the locale's encoding while the rest of cmd2 uses UTF-8:history --output-fileandhistory --editwrote the history in the locale's encoding.run_scriptreads only ASCII or UTF-8 files (utils.is_text_file()), so a history with non-ASCII text could not be run again. With--edit, the edited file is passed straight torun_script, so the commands never ran.run_pyscriptread the script in the locale's encoding. Python source files are UTF-8 by default, so a script with non-ASCII text failed withUnicodeDecodeErroror ran with garbled strings.Reproduction (Windows 11, Korean locale, Python 3.14,
locale.getpreferredencoding()= cp949)Before:
hist.txtcontainedb'say \xc7\xd1\xb1\xdb \xa8\xa9ukasz\r\n'(cp949).After:
hist.txtnow containsb'say \xed\x95\x9c\xea\xb8\x80 \xc5\x81ukasz\r\n'(UTF-8).Changes
history --output-fileandhistory --editopen their files withencoding="utf-8".run_pyscriptreads the script withencoding="utf-8-sig", so a script saved with a byte order mark (as some Windows editors do) also runs. Without stripping it,compile()fails withinvalid non-printable character U+FEFF.CHANGELOG.mdunder 4.4.4.Tests
The new tests make
open()andos.fdopen()incmd2.cmd2default to cp1252 when no encoding is given, so they fail on a UTF-8 CI runner too:tests/test_history.py::test_history_output_file_is_utf8: saves a non-ASCII history, checks the file is UTF-8, and runs it withrun_script.tests/test_history.py::test_history_edit_runs_non_ascii_commands:history -eruns a non-ASCII command.tests/test_run_pyscript.py::test_run_pyscript_reads_utf8[utf-8, utf-8-sig]: runs a UTF-8 script with and without a BOM.Results on Windows 11 (Python 3.14.3):
uv run pytest): 2882 passed, 210 skipped.tests/test_cmd2.py::test_run_script_with_utf8_filefailed in 1 of 2 full runs both with and without this change, and passes when run alone, so it looks flaky under xdist on Windows and unrelated.ruff check,ruff format --check,ty check, andprek runon the changed files pass.