Skip to content

Commit 96e99f5

Browse files
authored
Merge pull request github#2976 from BekaValentine/python-objectapi-to-valueapi-emptyexcept
Python: ObjectAPI to ValueAPI: EmptyExcept
2 parents b51e2a9 + 3e36c67 commit 96e99f5

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

python/ql/src/Exceptions/EmptyExcept.ql

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ predicate no_comment(ExceptStmt ex) {
3131
}
3232

3333
predicate non_local_control_flow(ExceptStmt ex) {
34-
ex.getType().refersTo(theStopIterationType())
34+
ex.getType().pointsTo(ClassValue::stopIteration())
3535
}
3636

3737
predicate try_has_normal_exit(Try try) {
@@ -64,32 +64,29 @@ predicate subscript(Stmt s) {
6464
s.(Delete).getATarget() instanceof Subscript
6565
}
6666

67-
predicate encode_decode(Expr ex, ClassObject type) {
67+
predicate encode_decode(Call ex, ClassValue type) {
6868
exists(string name |
69-
ex.(Call).getFunc().(Attribute).getName() = name |
70-
name = "encode" and type = Object::builtin("UnicodeEncodeError")
69+
ex.getFunc().(Attribute).getName() = name |
70+
name = "encode" and type = ClassValue::unicodeEncodeError()
7171
or
72-
name = "decode" and type = Object::builtin("UnicodeDecodeError")
72+
name = "decode" and type = ClassValue::unicodeDecodeError()
7373
)
7474
}
7575

76-
predicate small_handler(ExceptStmt ex, Stmt s, ClassObject type) {
76+
predicate small_handler(ExceptStmt ex, Stmt s, ClassValue type) {
7777
not exists(ex.getTry().getStmt(1)) and
7878
s = ex.getTry().getStmt(0) and
79-
ex.getType().refersTo(type)
79+
ex.getType().pointsTo(type)
8080
}
8181

82-
/** Holds if this exception handler is sufficiently small in scope to not need a comment
83-
* as to what it is doing.
84-
*/
8582
predicate focussed_handler(ExceptStmt ex) {
86-
exists(Stmt s, ClassObject type |
83+
exists(Stmt s, ClassValue type |
8784
small_handler(ex, s, type) |
88-
subscript(s) and type.getAnImproperSuperType() = theLookupErrorType()
85+
subscript(s) and type.getASuperType() = ClassValue::lookupError()
8986
or
90-
attribute_access(s) and type = theAttributeErrorType()
87+
attribute_access(s) and type = ClassValue::attributeError()
9188
or
92-
s.(ExprStmt).getValue() instanceof Name and type = theNameErrorType()
89+
s.(ExprStmt).getValue() instanceof Name and type = ClassValue::nameError()
9390
or
9491
encode_decode(s.(ExprStmt).getValue(), type)
9592
)

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ module ClassValue {
848848

849849
/** Get the `ClassValue` for the `StopIteration` class. */
850850
ClassValue stopIteration() {
851-
result = TBuiltinClassObject(Builtin::special("StopIteration"))
851+
result = TBuiltinClassObject(Builtin::builtin("StopIteration"))
852852
}
853853

854854
/** Get the `ClassValue` for the class of modules. */
@@ -891,6 +891,11 @@ module ClassValue {
891891
result = TBuiltinClassObject(Builtin::builtin("KeyError"))
892892
}
893893

894+
/** Get the `ClassValue` for the `LookupError` class. */
895+
ClassValue lookupError() {
896+
result = TBuiltinClassObject(Builtin::builtin("LookupError"))
897+
}
898+
894899
/** Get the `ClassValue` for the `IOError` class. */
895900
ClassValue ioError() {
896901
result = TBuiltinClassObject(Builtin::builtin("IOError"))
@@ -905,5 +910,15 @@ module ClassValue {
905910
ClassValue importError() {
906911
result = TBuiltinClassObject(Builtin::builtin("ImportError"))
907912
}
913+
914+
/** Get the `ClassValue` for the `UnicodeEncodeError` class. */
915+
ClassValue unicodeEncodeError() {
916+
result = TBuiltinClassObject(Builtin::builtin("UnicodeEncodeError"))
917+
}
918+
919+
/** Get the `ClassValue` for the `UnicodeDecodeError` class. */
920+
ClassValue unicodeDecodeError() {
921+
result = TBuiltinClassObject(Builtin::builtin("UnicodeDecodeError"))
922+
}
908923

909924
}

0 commit comments

Comments
 (0)