Skip to content
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

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

Closed
raininboat opened this issue Oct 21, 2022 · 3 comments · Fixed by #7663
Closed

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

raininboat opened this issue Oct 21, 2022 · 3 comments · Fixed by #7663
Labels
Crash 💥 A bug that makes pylint crash Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@raininboat
Copy link

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

@raininboat raininboat added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Oct 21, 2022
@Pierre-Sassoulas Pierre-Sassoulas added Crash 💥 A bug that makes pylint crash Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Oct 21, 2022
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.15.6 milestone Oct 21, 2022
@Pierre-Sassoulas
Copy link
Member

Thank you for opening the issue. It's very similar to #3860, seems like the fix was fragile.

@clavedeluna
Copy link
Collaborator

I can work on this over the next couple days. I can also see #7097 didn't add a test, so I can add that. @Pierre-Sassoulas is testing on modules best done via functional tests, or follow the examples in test_self.py which seem to dynamically create a module or load an existing module?

@Pierre-Sassoulas
Copy link
Member

Pierre-Sassoulas commented Oct 21, 2022

I think I tried to add a test at first in 865b1b8 but it seems I did not manage to reproduce later on (as something was fixed in astroid). So I would say this kind of crash is best tested with mocks in pylint. Due to the nature of the crash we're going to also want to fix astroid and add tests there. Edit: It seems in this issue the error is different and come from pylint (bom = UNICODE_BOMS[encoding] in pylint/checkers/unicode.py) so functional would probably work but there are a lot of unit tests for the unicode checker so the choice is yours,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Crash 💥 A bug that makes pylint crash Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants