Skip to content

Commit 6c7cddf

Browse files
committed
Python: py/import-deprecated-module handle backwards compatible code
1 parent e92d6c0 commit 6c7cddf

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

python/ql/src/Imports/DeprecatedModule.ql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ string replacement_message(string mod) {
7373
)
7474
}
7575

76-
from ImportExpr imp, string name
76+
from ImportExpr imp, string name, string instead
7777
where
7878
name = imp.getName() and
79-
deprecated_module(name, _, _, _)
79+
deprecated_module(name, instead, _, _) and
80+
not exists(Try try, ExceptStmt except | except = try.getAHandler()
81+
|
82+
except.getType().pointsTo(ClassValue::importError()) and
83+
except.containsInScope(imp)
84+
)
8085
select imp, deprecation_message(name) + replacement_message(name)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,4 +714,9 @@ module ClassValue {
714714
result = TBuiltinClassObject(Builtin::builtin("NameError"))
715715
}
716716

717+
/** Get the `ClassValue` for the `ImportError` class. */
718+
ClassValue importError() {
719+
result = TBuiltinClassObject(Builtin::builtin("ImportError"))
720+
}
721+
717722
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
| test.py:2:8:2:13 | ImportExpr | The rfc822 module was deprecated in version 2.3. Use email module instead. |
22
| test.py:3:8:3:16 | ImportExpr | The posixfile module was deprecated in version 1.5. Use email module instead. |
33
| test.py:8:16:8:18 | ImportExpr | The md5 module was deprecated in version 2.5. Use hashlib module instead. |
4-
| test.py:14:10:14:12 | ImportExpr | The md5 module was deprecated in version 2.5. Use hashlib module instead. |

python/ql/test/query-tests/Imports/deprecated/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Foo(object):
77
def foo(self):
88
import md5
99

10-
# TODO: Backwards compatible code, should not report
10+
# Backwards compatible code, should not report
1111
try:
1212
from hashlib import md5
1313
except ImportError:

0 commit comments

Comments
 (0)