Skip to content

Commit 02ae0fb

Browse files
authored
Merge pull request github#2965 from BekaValentine/python-objectapi-to-valueapi-raising
Python: ObjectAPI to ValueAPI: Raising
2 parents 6f508bc + a8ae843 commit 02ae0fb

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
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(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: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import python
22

33
/** Whether the raise statement 'r' raises 'type' from origin 'orig' */
4-
predicate type_or_typeof(Raise r, ClassObject type, AstNode orig) {
4+
predicate type_or_typeof(Raise r, ClassValue type, AstNode orig) {
55
exists(Expr exception |
66
exception = r.getRaised() |
7-
exception.refersTo(type, _, orig)
7+
exception.pointsTo(type, orig)
88
or
9-
not exists(ClassObject exc_type | exception.refersTo(exc_type)) and
10-
not type = theTypeType() and // First value is an unknown exception type
11-
exception.refersTo(_, type, orig)
9+
not exists(ClassValue exc_type | exception.pointsTo(exc_type)) and
10+
not type = ClassValue::type() and // First value is an unknown exception type
11+
exists(Value val | exception.pointsTo(val, orig) |
12+
val.getClass() = type
13+
)
1214
)
1315

1416
}

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)