Skip to content

Crash when illegal file encoding utf in the first line #7661

@raininboat

Description

@raininboat

Bug description

When parsing the following file:

# -*- encoding: utf -*-

pylint crashed with a AstroidError and with the following stacktrace:

Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 782, in _lint_file
    check_astroid_module(module)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1052, in check_astroid_module
    retval = self._check_astroid_module(
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 1098, in _check_astroid_module
    raw_checker.process_module(node)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pylint/checkers/unicode.py", line 531, in process_module
    line = _remove_bom(line, codec)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pylint/checkers/unicode.py", line 223, in _remove_bom
    bom = UNICODE_BOMS[encoding]
KeyError: 'utf'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 747, in _lint_files
    self._lint_file(fileitem, module, check_astroid_module)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pylint/lint/pylinter.py", line 784, in _lint_file
    raise astroid.AstroidError from e
astroid.exceptions.AstroidError

Configuration

No response

Command used

pylint main.py

Pylint output

************* Module main
main.py:1:0: C2503: PEP8 recommends UTF-8 as encoding for Python files (bad-file-encoding)
main.py:1:0: F0002: main.py: Fatal error while checking 'main.py'. Please open an issue in our bug tracker so we address this. There is a pre-filled template that you can use in '/home/ubuntu/.cache/pylint/pylint-crash-2022-10-22-01-14-12.txt'. (astroid-error)

Expected behavior

pylint should show the error about the illegal file encoding utf and do not crash

Pylint version

pylint 2.15.4
astroid 2.12.12
Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
[GCC 9.4.0]

OS / Environment

Linux 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Ubuntu 20.04 LTS

Additional dependencies

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Crash 💥A bug that makes pylint crashNeeds PRThis issue is accepted, sufficiently specified and now needs an implementation

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions