From ab3ad0629c9107063c40ffb831546fe20478a69b Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Thu, 21 Apr 2022 11:18:28 +0200 Subject: [PATCH] Fix crash when open is called with an integer mode (#6415) Closes #6414 --- ChangeLog | 10 ++++++++++ doc/whatsnew/2.13.rst | 5 +++++ pylint/checkers/stdlib.py | 2 +- tests/functional/u/unspecified_encoding_py38.py | 3 +++ tests/functional/u/unspecified_encoding_py38.txt | 2 ++ 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index d07386b515..1730ca97a9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -230,6 +230,16 @@ Release date: TBA (Ie. not necessarily at the end) +What's New in Pylint 2.13.8? +============================ +Release date: TBA + +* Fix a crash when linting a file that passes an integer ``mode=`` to + ``open`` + + Closes #6414 + + What's New in Pylint 2.13.7? ============================ Release date: 2022-04-20 diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst index 99a2fcd72b..d353ef7064 100644 --- a/doc/whatsnew/2.13.rst +++ b/doc/whatsnew/2.13.rst @@ -610,3 +610,8 @@ Other Changes * Only raise ``not-callable`` when all the inferred values of a property are not callable. Closes #5931 + +* Fix a crash when linting a file that passes an integer ``mode=`` to + ``open`` + + Closes #6414 diff --git a/pylint/checkers/stdlib.py b/pylint/checkers/stdlib.py index 24ef53cbe5..9b6ce32a19 100644 --- a/pylint/checkers/stdlib.py +++ b/pylint/checkers/stdlib.py @@ -659,7 +659,7 @@ def _check_open_encoded(self, node: nodes.Call, open_module: str) -> None: if ( not mode_arg or isinstance(mode_arg, nodes.Const) - and (not mode_arg.value or "b" not in mode_arg.value) + and (not mode_arg.value or "b" not in str(mode_arg.value)) ): encoding_arg = None try: diff --git a/tests/functional/u/unspecified_encoding_py38.py b/tests/functional/u/unspecified_encoding_py38.py index 20233f0734..66b8523f4c 100644 --- a/tests/functional/u/unspecified_encoding_py38.py +++ b/tests/functional/u/unspecified_encoding_py38.py @@ -159,3 +159,6 @@ class IOArgs: # Test for crash reported in https://github.com/PyCQA/pylint/issues/5731 open(FILENAME, mode=None) # [bad-open-mode, unspecified-encoding] + +# Test for crash reported in https://github.com/PyCQA/pylint/issues/6414 +open('foo', mode=2) # [bad-open-mode, unspecified-encoding] diff --git a/tests/functional/u/unspecified_encoding_py38.txt b/tests/functional/u/unspecified_encoding_py38.txt index b3371299f5..8cf19dee45 100644 --- a/tests/functional/u/unspecified_encoding_py38.txt +++ b/tests/functional/u/unspecified_encoding_py38.txt @@ -29,3 +29,5 @@ unspecified-encoding:155:0:155:26::Using open without explicitly specifying an e unspecified-encoding:158:0:158:35::Using open without explicitly specifying an encoding:UNDEFINED bad-open-mode:161:0:161:25::"""None"" is not a valid mode for open.":UNDEFINED unspecified-encoding:161:0:161:25::Using open without explicitly specifying an encoding:UNDEFINED +bad-open-mode:164:0:164:19::"""2"" is not a valid mode for open.":UNDEFINED +unspecified-encoding:164:0:164:19::Using open without explicitly specifying an encoding:UNDEFINED