Skip to content

Commit 8e6c16d

Browse files
committed
Python: Fix import root computation for windows and 3.7
1 parent 1956cd8 commit 8e6c16d

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

python/ql/src/semmle/python/Files.qll

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,21 @@ abstract class Container extends @container {
339339

340340
/** Holds if this folder is the root folder for the standard library. */
341341
predicate isStdLibRoot(int major, int minor) {
342-
allowable_version(major, minor) and
343-
this.isImportRoot() and
344-
this.getBaseName().regexpMatch("python" + major + "." + minor)
342+
major = major_version() and minor = minor_version() and
343+
this.isStdLibRoot()
344+
}
345+
346+
/** Holds if this folder is the root folder for the standard library. */
347+
predicate isStdLibRoot() {
348+
/* Look for a standard lib module and find its import path
349+
* We use `os` as it is the most likely to be imported and
350+
* `tty` because it is small for testing.
351+
*/
352+
exists(Module m |
353+
m.getName() = "os" or m.getName() = "tty"
354+
|
355+
m.getFile().getImportRoot() = this
356+
)
345357
}
346358

347359
/** Gets the path element from which this container would be loaded. */
@@ -375,12 +387,6 @@ private string get_path(string name) {
375387
py_flags_versioned(name, result, _)
376388
}
377389

378-
private predicate allowable_version(int major, int minor) {
379-
major = 2 and minor in [6..7]
380-
or
381-
major = 3 and minor in [3..6]
382-
}
383-
384390
class Location extends @location {
385391

386392
/** Gets the file for this location */

python/ql/src/semmle/python/Module.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class Module extends Module_, Scope, AstNode {
158158

159159
/** Holds if this module is in the standard library */
160160
predicate inStdLib() {
161-
this.inStdLib(_, _)
161+
this.getLoadPath().isStdLibRoot()
162162
}
163163

164164
override

python/ql/src/semmle/python/dependencies/TechInventory.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class DistPackage extends ExternalPackage {
5050
parent = this.(ModuleObject).getPath().getParent() and
5151
parent.isImportRoot() and
5252
/* Not in standard library */
53-
not parent.isStdLibRoot(_, _) and
53+
not parent.isStdLibRoot() and
5454
/* Not in the source */
5555
not exists(parent.getRelativePath())
5656
)

0 commit comments

Comments
 (0)