Skip to content

Commit a23f7a6

Browse files
authored
Merge pull request #592 from markshannon/python-windows-import-root
Python: Fix up computation of import root path
2 parents 8cd4978 + d32e6b8 commit a23f7a6

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-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
)

python/ql/test/library-tests/taint/exception_traceback/test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ def foo():
2828

2929

3030
foo()
31+
32+
33+
#For test to find stdlib
34+
import os

0 commit comments

Comments
 (0)