Skip to content

Commit 642b18d

Browse files
Fix crash on plistlib.py
1 parent e2459c0 commit 642b18d

3 files changed

Lines changed: 10 additions & 13 deletions

File tree

pylint/checkers/exceptions.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,9 @@ def _check_raise_missing_from(self, node: nodes.Raise) -> None:
336336
# The `except` doesn't have an `as exception:` part, meaning there's no way that
337337
# the `raise` is raising the same exception.
338338
class_of_old_error = "Exception"
339-
if isinstance(containing_except_node.type, nodes.Name):
340-
class_of_old_error = containing_except_node.type.name
341-
elif isinstance(containing_except_node.type, nodes.Tuple):
342-
# I.e. 'except (ZeroDivisionError, ValueError, ArithmeticError)'
343-
exceptions = ", ".join(n.name for n in containing_except_node.type.elts)
344-
class_of_old_error = f"({exceptions})"
339+
if isinstance(containing_except_node.type, (nodes.Name, nodes.Tuple)):
340+
# 'except ZeroDivisionError' or 'except (ZeroDivisionError, ValueError)'
341+
class_of_old_error = containing_except_node.type.as_string()
345342
self.add_message(
346343
"raise-missing-from",
347344
node=node,

tests/functional/r/raise_missing_from.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@
7474
# +1: [raise-missing-from]
7575
raise KeyError(whatever, overever=12)
7676

77-
78-
################################################################################
79-
# Negatives (Same cases as above, except with `from`):
77+
try:
78+
# Taken from https://github.com/python/cpython/blob/3.10/Lib/plistlib.py#L459
79+
pass
80+
except (OSError, IndexError, struct.error, OverflowError,
81+
ValueError):
82+
raise InvalidFileException() # [raise-missing-from]
8083

8184
try:
8285
1 / 0
@@ -123,10 +126,6 @@
123126
except ZeroDivisionError as e:
124127
raise KeyError(whatever, whatever=whatever) from foo
125128

126-
127-
################################################################################
128-
# Other negatives:
129-
130129
try:
131130
1 / 0
132131
except ZeroDivisionError:

tests/functional/r/raise_missing_from.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ raise-missing-from:54:4:54:20::Consider explicitly re-raising using 'raise KeyEr
88
raise-missing-from:60:4:60:47::Consider explicitly re-raising using 'raise KeyError(whatever, whatever=whatever) from e':HIGH
99
raise-missing-from:72:16:72:59::Consider explicitly re-raising using 'except (ZeroDivisionError, ValueError, KeyError) as exc' and 'raise KeyError(whatever, whatever=whatever) from exc':HIGH
1010
raise-missing-from:75:8:75:45::Consider explicitly re-raising using 'except (ZeroDivisionError, ValueError, KeyError) as exc' and 'raise KeyError(whatever, overever=12) from exc':HIGH
11+
raise-missing-from:82:4:82:32::Consider explicitly re-raising using 'except (OSError, IndexError, struct.error, OverflowError, ValueError) as exc' and 'raise InvalidFileException() from exc':HIGH

0 commit comments

Comments
 (0)