Skip to content

Commit 7161ca5

Browse files
committed
Python: Adds modernizations and moves query over to them
1 parent 0dcd52b commit 7161ca5

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

python/ql/src/Exceptions/IllegalRaise.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import python
1515
import Raising
1616
import Exceptions.NotImplemented
1717

18-
from Raise r, ClassObject t
19-
where type_or_typeof_objectapi(r, t, _) and not t.isLegalExceptionType() and not t.failedInference() and not use_of_not_implemented_in_raise(r, _)
18+
from Raise r, ClassValue t
19+
where type_or_typeof(r, t, _) and not t.isLegalExceptionType() and not t.failedInference(_) and not use_of_not_implemented_in_raise(r, _)
2020
select r, "Illegal class '" + t.getName() + "' raised; will result in a TypeError being raised instead."
2121

python/ql/src/Exceptions/Raising.qll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,18 @@ predicate type_or_typeof_objectapi(Raise r, ClassObject type, AstNode orig) {
1212
)
1313

1414
}
15+
16+
/** Whether the raise statement 'r' raises 'type' from origin 'orig' */
17+
predicate type_or_typeof(Raise r, ClassValue type, AstNode orig) {
18+
exists(Expr exception |
19+
exception = r.getRaised() |
20+
exception.pointsTo(type, orig)
21+
or
22+
not exists(ClassValue exc_type | exception.pointsTo(exc_type)) and
23+
not type = ClassValue::type() and // First value is an unknown exception type
24+
exists(Value val | exception.pointsTo(val, orig) |
25+
val.getClass() = type
26+
)
27+
)
28+
29+
}

python/ql/src/semmle/python/objects/ObjectAPI.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,15 @@ class ClassValue extends Value {
481481
predicate declaresAttribute(string name) {
482482
this.(ClassObjectInternal).getClassDeclaration().declaresAttribute(name)
483483
}
484+
485+
/** Whether this class is a legal exception class.
486+
* What constitutes a legal exception class differs between major versions */
487+
predicate isLegalExceptionType() {
488+
not this.isNewStyle() or
489+
this.getASuperType() = ClassValue::baseException()
490+
or
491+
major_version() = 2 and this = ClassValue::tuple()
492+
}
484493

485494
}
486495

0 commit comments

Comments
 (0)