Skip to content

Commit f69d4b4

Browse files
bitgluesigmavirus24
authored andcommitted
Avoid traceback when exception is del-ed in except (#67)
Fixes a regression introduced by 2a698f8. This would fail with a KeyError: try: pass except Exception as e: del e Fixes lp:1578903
1 parent 29914fc commit f69d4b4

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*.egg-info
33
*.pyc
44
.tox
5+
/.cache/
56
/dist/
67
/build/
78
docs/_build

pyflakes/checker.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,4 +1212,10 @@ def EXCEPTHANDLER(self, node):
12121212
# if the except: block is never entered. This will cause an
12131213
# "undefined name" error raised if the checked code tries to
12141214
# use the name afterwards.
1215-
del self.scope[node.name]
1215+
#
1216+
# Unless it's been removed already. Then do nothing.
1217+
1218+
try:
1219+
del self.scope[node.name]
1220+
except KeyError:
1221+
pass

pyflakes/test/test_undefined_names.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ def test_undefinedExceptionNameObscuringLocalVariableFalsePositive1(self):
9797
exc
9898
''')
9999

100+
def test_delExceptionInExcept(self):
101+
"""The exception name can be deleted in the except: block."""
102+
self.flakes('''
103+
try:
104+
pass
105+
except Exception as exc:
106+
del exc
107+
''')
108+
100109
def test_undefinedExceptionNameObscuringLocalVariableFalsePositive2(self):
101110
"""Exception names obscure locals, can't be used after. Unless.
102111

0 commit comments

Comments
 (0)