Skip to content

Commit a7e394b

Browse files
committed
python: SSA for names in except*
1 parent 30b58e7 commit a7e394b

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

python/ql/lib/semmle/python/essa/Definitions.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ abstract class SsaSourceVariable extends @py_variable {
6969
or
7070
SsaSource::exception_capture(this, def)
7171
or
72+
SsaSource::exception_group_capture(this, def)
73+
or
7274
SsaSource::with_definition(this, def)
7375
or
7476
SsaSource::pattern_capture_definition(this, def)

python/ql/lib/semmle/python/essa/Essa.qll

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,14 +511,14 @@ class AssignmentDefinition extends EssaNodeDefinition {
511511
override string getAPrimaryQlClass() { result = "AssignmentDefinition" }
512512
}
513513

514-
/** A capture of a raised exception `except ExceptionType ex:` */
514+
/** A capture of a raised exception `except ExceptionType as ex:` */
515515
class ExceptionCapture extends EssaNodeDefinition {
516516
ExceptionCapture() {
517517
SsaSource::exception_capture(this.getSourceVariable(), this.getDefiningNode())
518518
}
519519

520520
ControlFlowNode getType() {
521-
exists(ExceptFlowNode ex |
521+
exists(ExceptGroupFlowNode ex |
522522
ex.getName() = this.getDefiningNode() and
523523
result = ex.getType()
524524
)
@@ -529,6 +529,24 @@ class ExceptionCapture extends EssaNodeDefinition {
529529
override string getAPrimaryQlClass() { result = "ExceptionCapture" }
530530
}
531531

532+
/** A capture of a raised exception group `except* ExceptionType as ex:` */
533+
class ExceptionGroupCapture extends EssaNodeDefinition {
534+
ExceptionGroupCapture() {
535+
SsaSource::exception_group_capture(this.getSourceVariable(), this.getDefiningNode())
536+
}
537+
538+
ControlFlowNode getType() {
539+
exists(ExceptGroupFlowNode ex |
540+
ex.getName() = this.getDefiningNode() and
541+
result = ex.getType()
542+
)
543+
}
544+
545+
override string getRepresentation() { result = "except* " + this.getSourceVariable().getName() }
546+
547+
override string getAPrimaryQlClass() { result = "ExceptionGroupCapture" }
548+
}
549+
532550
/** An assignment to a variable as part of a multiple assignment `..., v, ... = val` */
533551
class MultiAssignmentDefinition extends EssaNodeDefinition {
534552
MultiAssignmentDefinition() {

python/ql/lib/semmle/python/essa/SsaDefinitions.qll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ module SsaSource {
3030
exists(ExceptFlowNode ex | ex.getName() = defn)
3131
}
3232

33+
/** Holds if `v` is defined by assignment of the captured exception group. */
34+
cached
35+
predicate exception_group_capture(Variable v, NameNode defn) {
36+
defn.defines(v) and
37+
exists(ExceptGroupFlowNode ex | ex.getName() = defn)
38+
}
39+
3340
/** Holds if `v` is defined by a with statement. */
3441
cached
3542
predicate with_definition(Variable v, ControlFlowNode defn) {

0 commit comments

Comments
 (0)