summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2022-04-21 11:18:28 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2022-05-02 16:38:19 +0200
commit7022d23376ebf6c3bded2d87400c529113bea5ee (patch)
tree820cc7a506b8acf364c68f867ba0ba139d132c65
parent3b2fbaec045697d53bdd4435e59dbfc2b286df4b (diff)
downloadpylint-git-7022d23376ebf6c3bded2d87400c529113bea5ee.tar.gz
Fix crash when open is called with an integer mode (#6415)
Closes #6414
-rw-r--r--ChangeLog4
-rw-r--r--doc/whatsnew/2.13.rst5
-rw-r--r--pylint/checkers/stdlib.py2
-rw-r--r--tests/functional/u/unspecified_encoding_py38.py3
-rw-r--r--tests/functional/u/unspecified_encoding_py38.txt2
5 files changed, 15 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 9054bd22f..ee6e4b3f9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -20,6 +20,10 @@ 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?
diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst
index 00d134a8c..bd002d021 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 6eeaa20a9..daf932444 100644
--- a/pylint/checkers/stdlib.py
+++ b/pylint/checkers/stdlib.py
@@ -664,7 +664,7 @@ class StdlibChecker(DeprecatedMixin, BaseChecker):
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 20233f073..66b8523f4 100644
--- a/tests/functional/u/unspecified_encoding_py38.py
+++ b/tests/functional/u/unspecified_encoding_py38.py
@@ -159,3 +159,6 @@ Path(FILENAME).write_text("string") # [unspecified-encoding]
# 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 b3371299f..8cf19dee4 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