Skip to content

Commit e40bb6a

Browse files
authored
Merge pull request github#12029 from github/tausbn/python-clean-up-version-handling
Python: Clean up version handling
2 parents 2f8c9a5 + 1b30043 commit e40bb6a

File tree

5 files changed

+33
-28
lines changed

5 files changed

+33
-28
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
category: breaking
3+
---
4+
- Python 2 is no longer supported for extracting databases using the CodeQL CLI. As a consequence,
5+
the previously deprecated support for `pyxl` and `spitfire` templates has also been removed. When
6+
extracting Python 2 code, having Python 2 installed is still recommended, as this ensures the
7+
correct version of the Python standard library is extracted.

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

python/ql/test/2/library-tests/six/pointsto.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
| six.moves.urllib | Package six.moves.urllib |
77
| six.moves.urllib.parse | Module six.moves.urllib_parse |
88
| six.moves.urllib.parse.urlsplit | Function urlsplit |
9-
| six.moves.zip | builtin-class itertools.izip |
9+
| six.moves.zip | Builtin-function zip |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| bad_encoding.py:11:19:11:19 | Encoding Error | 'utf8' codec can't decode byte 0x82 in position 82: invalid start byte |
1+
| bad_encoding.py:11:19:11:19 | Encoding Error | 'utf-8' codec can't decode byte 0x82 in position 82: invalid start byte |

0 commit comments

Comments
 (0)