Skip to content

Commit 03ae783

Browse files
authored
Merge pull request github#2711 from RasmusWL/python-fix-import-deprecated-module
Python: fix alerts for py/import-deprecated-module
2 parents df3ac49 + 4ca72de commit 03ae783

File tree

5 files changed

+51
-25
lines changed

5 files changed

+51
-25
lines changed

python/ql/src/Imports/DeprecatedModule.ql

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212

1313
import python
1414

15-
15+
/**
16+
* The module `name` was deprecated in Python version `major`.`minor`,
17+
* and module `instead` should be used instead (or `instead = "no replacement"`)
18+
*/
1619
predicate deprecated_module(string name, string instead, int major, int minor) {
17-
name = "posixfile" and instead = "email" and major = 1 and minor = 5
20+
name = "posixfile" and instead = "fcntl" and major = 1 and minor = 5
1821
or
1922
name = "gopherlib" and instead = "no replacement" and major = 2 and minor = 5
2023
or
@@ -34,40 +37,49 @@ predicate deprecated_module(string name, string instead, int major, int minor) {
3437
or
3538
name = "rotor" and instead = "no replacement" and major = 2 and minor = 4
3639
or
37-
name = "statcache" and instead = "no replacement" and major = 2 and minor = 2
40+
name = "statcache" and instead = "no replacement" and major = 2 and minor = 2
3841
or
39-
name = "mpz" and instead = "a third party" and major = 2 and minor = 2
42+
name = "mpz" and instead = "a third party" and major = 2 and minor = 2
4043
or
4144
name = "xreadlines" and instead = "no replacement" and major = 2 and minor = 3
4245
or
4346
name = "multifile" and instead = "email" and major = 2 and minor = 5
4447
or
45-
name = "sets" and instead = "builtins" and major = 2 and minor = 6
48+
name = "sets" and instead = "builtins" and major = 2 and minor = 6
4649
or
4750
name = "buildtools" and instead = "no replacement" and major = 2 and minor = 3
4851
or
49-
name = "cfmfile" and instead = "no replacement" and major = 2 and minor = 4
52+
name = "cfmfile" and instead = "no replacement" and major = 2 and minor = 4
5053
or
5154
name = "macfs" and instead = "no replacement" and major = 2 and minor = 3
5255
or
53-
name = "md5" and instead = "hashlib" and major = 2 and minor = 5
56+
name = "md5" and instead = "hashlib" and major = 2 and minor = 5
5457
or
55-
name = "sha" and instead = "hashlib" and major = 2 and minor = 5
58+
name = "sha" and instead = "hashlib" and major = 2 and minor = 5
5659
}
5760

5861
string deprecation_message(string mod) {
59-
exists(int major, int minor | deprecated_module(mod, _, major, minor) |
60-
result = "The " + mod + " module was deprecated in version " + major.toString() + "." + minor.toString() + ".")
62+
exists(int major, int minor | deprecated_module(mod, _, major, minor) |
63+
result = "The " + mod + " module was deprecated in version " + major.toString() + "." +
64+
minor.toString() + "."
65+
)
6166
}
6267

6368
string replacement_message(string mod) {
64-
exists(string instead | deprecated_module(mod, instead, _, _) |
65-
result = " Use " + instead + " module instead." and not instead = "no replacement"
66-
or
67-
result = "" and instead = "no replacement"
68-
)
69+
exists(string instead | deprecated_module(mod, instead, _, _) |
70+
result = " Use " + instead + " module instead." and not instead = "no replacement"
71+
or
72+
result = "" and instead = "no replacement"
73+
)
6974
}
7075

71-
from ImportExpr imp, Stmt s, Expr e
72-
where s.getASubExpression() = e and (e = imp or e.contains(imp))
73-
select s, deprecation_message(imp.getName()) + replacement_message(imp.getName())
76+
from ImportExpr imp, string name, string instead
77+
where
78+
name = imp.getName() and
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+
)
85+
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
@@ -749,4 +749,9 @@ module ClassValue {
749749
result = TBuiltinClassObject(Builtin::builtin("NameError"))
750750
}
751751

752+
/** Get the `ClassValue` for the `ImportError` class. */
753+
ClassValue importError() {
754+
result = TBuiltinClassObject(Builtin::builtin("ImportError"))
755+
}
756+
752757
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
| test.py:5:1:5:13 | Import | The rfc822 module was deprecated in version 2.3. Use email module instead. |
2-
| test.py:6:1:6:16 | Import | The posixfile module was deprecated in version 1.5. Use email module instead. |
1+
| test.py:2:8:2:13 | ImportExpr | The rfc822 module was deprecated in version 2.3. Use email module instead. |
2+
| test.py:3:8:3:16 | ImportExpr | The posixfile module was deprecated in version 1.5. Use fcntl module instead. |
3+
| test.py:8:16:8:18 | ImportExpr | The md5 module was deprecated in version 2.5. Use hashlib module instead. |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Imports/DeprecatedModule.ql
1+
Imports/DeprecatedModule.ql
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
2-
3-
4-
#Some deprecated modules
1+
# Some deprecated modules
52
import rfc822
63
import posixfile
4+
5+
# We should only report a bad import once
6+
class Foo(object):
7+
def foo(self):
8+
import md5
9+
10+
# Backwards compatible code, should not report
11+
try:
12+
from hashlib import md5
13+
except ImportError:
14+
from md5 import md5

0 commit comments

Comments
 (0)