bandit icon indicating copy to clipboard operation
bandit copied to clipboard

Pre-commit hook unable to encode non ascii characters

Open austinyu opened this issue 7 months ago • 0 comments

Describe the bug

When running bandit pre-commit, if the source code contain non ascii characters, pre-commit hook will throw an ugly error message that it could not encode those characters.

Reproduction steps

put these two files under one folder

  • .pre-commit-config.yaml
# pre-commit autoupdate

repos:
  - repo: https://github.com/PyCQA/bandit
    rev: 1.8.3
    hooks:
      - id: bandit
        args: [-c, pyproject.toml]
        additional_dependencies: ["bandit[toml]"]
  • foo.py
print("❌ Failed to copy workspace file 🌟")
# '\U0001f31f'
print("🌟 Welcome back")

Run pre-commit install Run pre-commit run --all-files

Error:

bandit...................................................................Failed
- hook id: bandit
- exit code: 1

[main]  INFO    profile include tests: None
[main]  INFO    profile exclude tests: None
[main]  INFO    cli include tests: None
[main]  INFO    cli exclude tests: None
[main]  INFO    using config: pyproject.toml
[main]  INFO    running on Python 3.12.3
Traceback (most recent call last):
  File "C:\Users\admin\.cache\pre-commit\repo71c1_i5v\py_env-python3\Lib\site-packages\bandit\core\manager.py", line 186, in output_results
    report_func(
  File "C:\Users\admin\.cache\pre-commit\repo71c1_i5v\py_env-python3\Lib\site-packages\bandit\formatters\text.py", line 195, in report
    wrapped_file.write(result)
UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f31f' in position 554: illegal multibyte sequence

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\admin\.cache\pre-commit\repo71c1_i5v\py_env-python3\Scripts\bandit.EXE\__main__.py", line 7, in <module>
  File "C:\Users\admin\.cache\pre-commit\repo71c1_i5v\py_env-python3\Lib\site-packages\bandit\cli\main.py", line 678, in main
    b_mgr.output_results(
  File "C:\Users\admin\.cache\pre-commit\repo71c1_i5v\py_env-python3\Lib\site-packages\bandit\core\manager.py", line 195, in output_results
    raise RuntimeError(
RuntimeError: Unable to output report using 'txt' formatter: 'gbk' codec can't encode character '\U0001f31f' in position 554: illegal multibyte sequence

Expected behavior

No error thrown.

Bandit version

1.8.3 (Default)

Python version

3.12

Additional context

No response

austinyu avatar Mar 26 '25 03:03 austinyu