Skip to content

Commit fe8b44f

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

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

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

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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(Expr source, ExceptStmt handler, Cfg::ControlFlowNode sourceCfg,
15+
Cfg::ControlFlowNode handlerEntry |
16+
sourceCfg.getNode() = source and
17+
handlerEntry = sourceCfg.getAnExceptionalSuccessor() and
18+
CfgImpl::astNodeToPyNode(handlerEntry.getAstNode()) = handler and
19+
location = source.getLocation() and
20+
element = source.toString() and
21+
tag = "exception-handler" and
22+
value = handler.getType().toString()
23+
)
24+
}
25+
}
26+
27+
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)