Skip to content

Commit 49a3dd6

Browse files
committed
Python: Clean up version handling
Depends on an internal PR.
1 parent 334c41c commit 49a3dd6

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

python/ql/lib/semmle/python/Constants.qll

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,34 @@
33
import python
44

55
/** the Python major version number */
6-
int major_version() {
7-
explicit_major_version(result)
8-
or
9-
not explicit_major_version(_) and
10-
/* If there is more than one version, prefer 2 for backwards compatibility */
11-
(if py_flags_versioned("version.major", "2", "2") then result = 2 else result = 3)
12-
}
6+
int major_version() { full_python_analysis_version(result, _, _) }
137

148
/** the Python minor version number */
15-
int minor_version() {
16-
exists(string v | py_flags_versioned("version.minor", v, major_version().toString()) |
17-
result = v.toInt()
18-
)
19-
}
9+
int minor_version() { full_python_analysis_version(_, result, _) }
2010

2111
/** the Python micro version number */
22-
int micro_version() {
23-
exists(string v | py_flags_versioned("version.micro", v, major_version().toString()) |
24-
result = v.toInt()
25-
)
12+
int micro_version() { full_python_analysis_version(_, _, result) }
13+
14+
/** Gets the latest supported minor version for the given major version. */
15+
private int latest_supported_minor_version(int major) {
16+
major = 2 and result = 7
17+
or
18+
major = 3 and result = 11
2619
}
2720

28-
private predicate explicit_major_version(int v) {
29-
exists(string version | py_flags_versioned("language.version", version, _) |
30-
version.charAt(0) = "2" and v = 2
31-
or
32-
version.charAt(0) = "3" and v = 3
21+
private predicate full_python_analysis_version(int major, int minor, int micro) {
22+
exists(string version_string | py_flags_versioned("language.version", version_string, _) |
23+
major = version_string.regexpFind("\\d+", 0, _).toInt() and
24+
(
25+
minor = version_string.regexpFind("\\d+", 1, _).toInt()
26+
or
27+
not exists(version_string.regexpFind("\\d+", 1, _)) and
28+
minor = latest_supported_minor_version(major)
29+
) and
30+
(
31+
micro = version_string.regexpFind("\\d+", 2, _).toInt()
32+
or
33+
not exists(version_string.regexpFind("\\d+", 2, _)) and micro = 0
34+
)
3335
)
3436
}

python/ql/lib/semmle/python/types/Builtins.qll

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,7 @@ class Builtin extends @py_cobject {
111111
}
112112

113113
module Builtin {
114-
Builtin builtinModule() {
115-
py_special_objects(result, "builtin_module_2") and major_version() = 2
116-
or
117-
py_special_objects(result, "builtin_module_3") and major_version() = 3
118-
}
114+
Builtin builtinModule() { py_special_objects(result, "builtin_module") }
119115

120116
Builtin builtin(string name) { result = builtinModule().getMember(name) }
121117

0 commit comments

Comments
 (0)