Repository navigation
Exceptions when using cz from ISO8859-1 Terminal #956
Description
Description
When issuing 'cz info' from a ISO8859-1 encoded Terminal I get the following exception:
[xx@XXXXXX:~/tmp/test-git-repo (master +)] $ cz info
Traceback (most recent call last):
File "/home/user/pb/venv_commitizen/bin/cz", line 8, in <module>
sys.exit(main())
^^^^^^
File "/home/user/pb/venv_commitizen/lib/python3.11/site-packages/commitizen/cli.py", line 570, in main
args.func(conf, arguments)()
File "/home/user/pb/venv_commitizen/lib/python3.11/site-packages/commitizen/commands/info.py", line 13, in __call__
out.write(self.cz.info())
File "/home/user/pb/venv_commitizen/lib/python3.11/site-packages/commitizen/out.py", line 13, in write
print(value, *args)
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2019' in position 1068: ordinal not in range(256)
I was running into that problem on MacOS (Ventura 13.6.3) and AlmaLinux 8.9.
Same exception also happens when issuing 'cz init' quite at the end of the process:
[xx@xxxxxxxx:~/tmp/test-git-repo (master +)] $ cz init
Welcome to commitizen!
Answer the questions to configure your project.
For further configuration visit:
https://commitizen-tools.github.io/commitizen/config/
? Please choose a supported config file: .cz.toml
? Please choose a cz (commit rule): (default: cz_conventional_commits) cz_conventional_commits
? Choose the source of the version: commitizen: Fetch and set version in commitizen config (default)
No Existing Tag. Set tag to v0.0.1
? Choose version scheme: semver
? Please enter the correct version format: (default: "$version")
? Create changelog automatically on bump Yes
? Keep major version zero (0.x) during breaking changes Yes
? What types of pre-commit hook you want to install? (Leave blank if you don't want to install) done
You can bump the version running:
cz bump
Traceback (most recent call last):
File "/home/user/pb/venv_commitizen/bin/cz", line 8, in <module>
sys.exit(main())
^^^^^^
File "/home/user/pb/venv_commitizen/lib/python3.11/site-packages/commitizen/cli.py", line 570, in main
args.func(conf, arguments)()
File "/home/user/pb/venv_commitizen/lib/python3.11/site-packages/commitizen/commands/init.py", line 150, in __call__
out.success("Configuration complete \U0001f680")
File "/home/user/pb/venv_commitizen/lib/python3.11/site-packages/commitizen/out.py", line 28, in success
line(message)
File "/home/user/pb/venv_commitizen/lib/python3.11/site-packages/commitizen/out.py", line 18, in line
print(value, *args, **kwargs)
UnicodeEncodeError: 'latin-1' codec can't encode character '\U0001f680' in position 28: ordinal not in range(256)
This probably happens also with other non UTF-8 encoded Terminals.
We have quite a few machines that still run (due to the application running on them) in ISO8859-1 Encoding
Steps to reproduce
- Start new Terminal
- Set LANG to 'de_CH.ISO8859-1' with 'export LANG=de_CH.ISO8859-1'
- issue 'cz info'
Current behavior
Exception / Crash
Desired behavior
optimum:
Proper output with reencoding also with other Terminal Encodings than UTF-8. (This is due the fact that we have to use ISO8859-1 Terminal for development and therefore also use git and cz inside these terminals)
Acceptable but not desireable:
Prevent cz from being used with non UTF-8 Termial: warn and exit at start instead of throwing an exception in the middle of the work.
Screenshots
No response
Environment
cz version --report
Commitizen Version: 3.13.0
Python Version: 3.11.6 (main, Oct 3 2023, 17:06:54) [GCC 8.5.0 20210514 (Red Hat 8.5.0-18)]
Operating System: Linux
Activity
Lee-W commentedon May 20, 2024
Hi @keenonkites , thanks for filing this issue. I just tested with the following commands without encountering issues.
export LANG=de_CH.ISO8859-1 cz infoI also tried to install 3.13.0 and change terminal encoding. Could you please check if it still happens? If so, could you please provider another way to reproduce? Thanks!
keenonkites commentedon May 21, 2024
I've just tested it on my mac (where cz is installed via brew) as well as on a AlmaLinux VM (where cz is installed via pip) with the newest version 3.26.0 and it still happens on both systems.
to be mentioned facts:
Below you see result of the third version (UTF8 Term, cz info, changing Lang, czinfo:
I can't think of anything else how to produce it as for me it's more an 'avoiding' the problem, but question from my end:
Do you have de_CH.ISO8859-1 installed on your test system ? If the local is not installed you probably fall back automatically to UTF8 anyway.
keenonkites commentedon May 21, 2024
In the case of issuing the command cz info the crash is caused by the single quote on line 24 from "commit’s" in the file commitizen/cz/conventional_commits/conventional_commits_info.txt.
Taking this out from the source code prevents cz from crashing for 'cz info' with non-utf terminals. But there are other non-ascii characters in other sections also that crashes cz with other commands (cz init, e.g., as mentioned in the original posting).
To make the application non-utf save I think the functions for producing the output in the file commitizen/out.py have to be written in a way that allows masking/rewriting characters that are not safe for the actual encoding... or at least prevent crashes and print out proper error messages.
Lee-W commentedon May 21, 2024
Thanks for updating! Will take a deeper look after I come back.
bearomorphism commentedon May 9, 2026
Triage from #1964: Confirmed still valid.
commitizen/out.py(lines 7-9) only reconfigures stdout encoding onwin32. On Linux/macOS withLANG=de_CH.ISO8859-1,print()raisesUnicodeEncodeErrorfor emojis and other non-Latin1 characters. Fix: extend the reconfigure to all platforms, or wrapprintcalls in an encoding-tolerant helper that falls back to escape sequences.bearomorphism commentedon May 9, 2026
Verification update (re #1964)
I simulated the failure mode against current master (4.15.1) by wrapping
sys.stdoutin a Latin-1TextIOWrapper(since I'm on Windows and can't setLANG=de_CH.ISO8859-1in the OS sense):cz.info()→UnicodeEncodeError: 'latin-1' codec can't encode character '\u2019'out.success("Configuration complete \U0001f680")— →UnicodeEncodeError: 'latin-1' codec can't encode character '\U0001f680'The relevant code, unchanged since the original report:
Only Windows triggers the stdout reconfigure; non-Windows users with non-UTF-8
LANGstill hit the original error.Verdict: STILL VALID.
Suggested fix: extend the reconfigure to all platforms when the current encoding can't represent the characters commitizen produces. Something like:
(Using
errors="replace"guards against terminals that genuinely can't render the bytes — better than crashing mid-flow.)