Skip to content

Commit c19574b

Browse files
authored
Merge pull request #10267 from yoff/python/port-EmptyExcept
python: Rewrite EmptyExcept from `points-to` to API graph
2 parents 1fe9b3f + 1d2d28b commit c19574b

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

python/ql/src/Exceptions/EmptyExcept.ql

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
import python
15+
import semmle.python.ApiGraphs
1516

1617
predicate empty_except(ExceptStmt ex) {
1718
not exists(Stmt s | s = ex.getAStmt() and not s instanceof Pass)
@@ -28,7 +29,7 @@ predicate no_comment(ExceptStmt ex) {
2829
}
2930

3031
predicate non_local_control_flow(ExceptStmt ex) {
31-
ex.getType().pointsTo(ClassValue::stopIteration())
32+
ex.getType() = API::builtin("StopIteration").getAValueReachableFromSource().asExpr()
3233
}
3334

3435
predicate try_has_normal_exit(Try try) {
@@ -61,27 +62,32 @@ predicate subscript(Stmt s) {
6162
s.(Delete).getATarget() instanceof Subscript
6263
}
6364

64-
predicate encode_decode(Call ex, ClassValue type) {
65+
predicate encode_decode(Call ex, Expr type) {
6566
exists(string name | ex.getFunc().(Attribute).getName() = name |
66-
name = "encode" and type = ClassValue::unicodeEncodeError()
67+
name = "encode" and
68+
type = API::builtin("UnicodeEncodeError").getAValueReachableFromSource().asExpr()
6769
or
68-
name = "decode" and type = ClassValue::unicodeDecodeError()
70+
name = "decode" and
71+
type = API::builtin("UnicodeDecodeError").getAValueReachableFromSource().asExpr()
6972
)
7073
}
7174

72-
predicate small_handler(ExceptStmt ex, Stmt s, ClassValue type) {
75+
predicate small_handler(ExceptStmt ex, Stmt s, Expr type) {
7376
not exists(ex.getTry().getStmt(1)) and
7477
s = ex.getTry().getStmt(0) and
75-
ex.getType().pointsTo(type)
78+
ex.getType() = type
7679
}
7780

7881
predicate focussed_handler(ExceptStmt ex) {
79-
exists(Stmt s, ClassValue type | small_handler(ex, s, type) |
80-
subscript(s) and type.getASuperType() = ClassValue::lookupError()
82+
exists(Stmt s, Expr type | small_handler(ex, s, type) |
83+
subscript(s) and
84+
type = API::builtin("IndexError").getASubclass*().getAValueReachableFromSource().asExpr()
8185
or
82-
attribute_access(s) and type = ClassValue::attributeError()
86+
attribute_access(s) and
87+
type = API::builtin("AttributeError").getAValueReachableFromSource().asExpr()
8388
or
84-
s.(ExprStmt).getValue() instanceof Name and type = ClassValue::nameError()
89+
s.(ExprStmt).getValue() instanceof Name and
90+
type = API::builtin("NameError").getAValueReachableFromSource().asExpr()
8591
or
8692
encode_decode(s.(ExprStmt).getValue(), type)
8793
)

0 commit comments

Comments
 (0)