Skip to content

Commit 8b7fad8

Browse files
authored
Merge pull request #6283 from tausbn/python-fix-exceptstmt-gettype
Python: Fix `ExceptStmt::getType`
2 parents 49f5f1e + 5ac32f1 commit 8b7fad8

File tree

2 files changed

+15
-0
lines changed
  • python/ql

2 files changed

+15
-0
lines changed

python/ql/lib/semmle/python/Stmts.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ class ExceptStmt extends ExceptStmt_ {
153153
override Stmt getASubStatement() { result = this.getAStmt() }
154154

155155
override Stmt getLastStatement() { result = this.getBody().getLastItem().getLastStatement() }
156+
157+
override Expr getType() {
158+
result = super.getType() and not result instanceof Tuple
159+
or
160+
result = super.getType().(Tuple).getAnElt()
161+
}
156162
}
157163

158164
/** An assert statement, such as `assert a == b, "A is not equal to b"` */

python/ql/test/query-tests/Exceptions/generators/test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,12 @@ def ok5(seq):
5353

5454
def ok6(seq):
5555
yield next(iter([]), default='foo')
56+
57+
# Handling for multiple exception types, one of which is `StopIteration`
58+
# Reported as a false positive in github/codeql#6227
59+
def ok7(seq, ctx):
60+
try:
61+
with ctx:
62+
yield next(iter)
63+
except (StopIteration, MemoryError):
64+
return

0 commit comments

Comments
 (0)