Skip to content

Commit 8ad121f

Browse files
Fix crash when providing None to mode arg of open() (#5732)
1 parent a8f81db commit 8ad121f

File tree

5 files changed

+16
-1
lines changed

5 files changed

+16
-1
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ Release date: TBA
5050

5151
Closes #3781
5252

53+
* Fixed a crash in ``unspecified-encoding`` checker when providing ``None``
54+
to the ``mode`` argument of an ``open()`` call.
55+
56+
Closes #5731
57+
5358
* Added ``lru-cache-decorating-method`` checker with checks for the use of ``functools.lru_cache``
5459
on class methods. This is unrecommended as it creates memory leaks by never letting the instance
5560
getting garbage collected.

doc/whatsnew/2.13.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ Other Changes
156156

157157
Closes #5479
158158

159+
* Fixed a crash in ``unspecified-encoding`` checker when providing ``None``
160+
to the ``mode`` argument of an ``open()`` call.
161+
162+
Closes #5731
163+
159164
* Fix false negative for ``consider-iterating-dictionary`` during membership checks encapsulated in iterables
160165
or ``not in`` checks
161166

pylint/checkers/stdlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ def _check_open_encoded(self, node: nodes.Call, open_module: str) -> None:
671671
if (
672672
not mode_arg
673673
or isinstance(mode_arg, nodes.Const)
674-
and "b" not in mode_arg.value
674+
and (not mode_arg.value or "b" not in mode_arg.value)
675675
):
676676
encoding_arg = None
677677
try:

tests/functional/u/unspecified_encoding_py38.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,6 @@ class IOArgs:
156156

157157
Path(FILENAME).write_text("string", "utf-8")
158158
Path(FILENAME).write_text("string") # [unspecified-encoding]
159+
160+
# Test for crash reported in https://github.com/PyCQA/pylint/issues/5731
161+
open(FILENAME, mode=None) # [bad-open-mode, unspecified-encoding]

tests/functional/u/unspecified_encoding_py38.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ unspecified-encoding:149:0:149:23::Using open without explicitly specifying an e
2727
unspecified-encoding:152:0:152:28::Using open without explicitly specifying an encoding:UNDEFINED
2828
unspecified-encoding:155:0:155:26::Using open without explicitly specifying an encoding:UNDEFINED
2929
unspecified-encoding:158:0:158:35::Using open without explicitly specifying an encoding:UNDEFINED
30+
bad-open-mode:161:0:161:25::"""%s"" is not a valid mode for open.":UNDEFINED
31+
unspecified-encoding:161:0:161:25::Using open without explicitly specifying an encoding:UNDEFINED

0 commit comments

Comments
 (0)