12
12
*/
13
13
14
14
import python
15
+ import semmle.python.ApiGraphs
15
16
16
17
predicate empty_except ( ExceptStmt ex ) {
17
18
not exists ( Stmt s | s = ex .getAStmt ( ) and not s instanceof Pass )
@@ -28,7 +29,7 @@ predicate no_comment(ExceptStmt ex) {
28
29
}
29
30
30
31
predicate non_local_control_flow ( ExceptStmt ex ) {
31
- ex .getType ( ) . pointsTo ( ClassValue :: stopIteration ( ) )
32
+ ex .getType ( ) = API :: builtin ( "StopIteration" ) . getAValueReachableFromSource ( ) . asExpr ( )
32
33
}
33
34
34
35
predicate try_has_normal_exit ( Try try ) {
@@ -61,27 +62,32 @@ predicate subscript(Stmt s) {
61
62
s .( Delete ) .getATarget ( ) instanceof Subscript
62
63
}
63
64
64
- predicate encode_decode ( Call ex , ClassValue type ) {
65
+ predicate encode_decode ( Call ex , Expr type ) {
65
66
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 ( )
67
69
or
68
- name = "decode" and type = ClassValue:: unicodeDecodeError ( )
70
+ name = "decode" and
71
+ type = API:: builtin ( "UnicodeDecodeError" ) .getAValueReachableFromSource ( ) .asExpr ( )
69
72
)
70
73
}
71
74
72
- predicate small_handler ( ExceptStmt ex , Stmt s , ClassValue type ) {
75
+ predicate small_handler ( ExceptStmt ex , Stmt s , Expr type ) {
73
76
not exists ( ex .getTry ( ) .getStmt ( 1 ) ) and
74
77
s = ex .getTry ( ) .getStmt ( 0 ) and
75
- ex .getType ( ) . pointsTo ( type )
78
+ ex .getType ( ) = type
76
79
}
77
80
78
81
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 ( )
81
85
or
82
- attribute_access ( s ) and type = ClassValue:: attributeError ( )
86
+ attribute_access ( s ) and
87
+ type = API:: builtin ( "AttributeError" ) .getAValueReachableFromSource ( ) .asExpr ( )
83
88
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 ( )
85
91
or
86
92
encode_decode ( s .( ExprStmt ) .getValue ( ) , type )
87
93
)
0 commit comments