Skip to content

Commit 183c619

Browse files
yoffCopilot
andcommitted
Python: test shared CFG exception reachability
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent c69ba9a commit 183c619

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

python/ql/test/library-tests/ControlFlow/shared-cfg-exceptions/ExceptionReachabilityTest.expected

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Inline-expectations test for exception-handler reachability in the shared CFG.
3+
*/
4+
5+
import python
6+
import semmle.python.controlflow.internal.AstNodeImpl as CfgImpl
7+
import semmle.python.controlflow.internal.Cfg as Cfg
8+
import utils.test.InlineExpectationsTest
9+
10+
module ExceptionReachabilityTest implements TestSig {
11+
string getARelevantTag() { result = "exception-handler" }
12+
13+
predicate hasActualResult(Location location, string element, string tag, string value) {
14+
exists(
15+
Expr source, ExceptStmt handler, Cfg::ControlFlowNode sourceCfg,
16+
Cfg::ControlFlowNode handlerEntry
17+
|
18+
sourceCfg.getNode() = source and
19+
handlerEntry = sourceCfg.getAnExceptionalSuccessor() and
20+
CfgImpl::astNodeToPyNode(handlerEntry.getAstNode()) = handler and
21+
location = source.getLocation() and
22+
element = source.toString() and
23+
tag = "exception-handler" and
24+
value = handler.getType().toString()
25+
)
26+
}
27+
}
28+
29+
import MakeTest<ExceptionReachabilityTest>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def generator():
2+
try:
3+
yield # $ MISSING: exception-handler=GeneratorExit
4+
except GeneratorExit:
5+
return
6+
7+
8+
def load_module():
9+
try:
10+
import unavailable_module # $ MISSING: exception-handler=ImportError
11+
except ImportError:
12+
return None

0 commit comments

Comments
 (0)