Merge pull request #592 from markshannon/python-windows-import-root

Python: Fix up computation of import root path
This commit is contained in:
Taus
2018-12-04 11:11:59 +01:00
committed by GitHub
4 changed files with 21 additions and 11 deletions

View File

@@ -339,9 +339,21 @@ abstract class Container extends @container {
/** Holds if this folder is the root folder for the standard library. */
predicate isStdLibRoot(int major, int minor) {
allowable_version(major, minor) and
this.isImportRoot() and
this.getBaseName().regexpMatch("python" + major + "." + minor)
major = major_version() and minor = minor_version() and
this.isStdLibRoot()
}
/** Holds if this folder is the root folder for the standard library. */
predicate isStdLibRoot() {
/* Look for a standard lib module and find its import path
* We use `os` as it is the most likely to be imported and
* `tty` because it is small for testing.
*/
exists(Module m |
m.getName() = "os" or m.getName() = "tty"
|
m.getFile().getImportRoot() = this
)
}
/** Gets the path element from which this container would be loaded. */
@@ -375,12 +387,6 @@ private string get_path(string name) {
py_flags_versioned(name, result, _)
}
private predicate allowable_version(int major, int minor) {
major = 2 and minor in [6..7]
or
major = 3 and minor in [3..6]
}
class Location extends @location {
/** Gets the file for this location */

View File

@@ -158,7 +158,7 @@ class Module extends Module_, Scope, AstNode {
/** Holds if this module is in the standard library */
predicate inStdLib() {
this.inStdLib(_, _)
this.getLoadPath().isStdLibRoot()
}
override

View File

@@ -50,7 +50,7 @@ class DistPackage extends ExternalPackage {
parent = this.(ModuleObject).getPath().getParent() and
parent.isImportRoot() and
/* Not in standard library */
not parent.isStdLibRoot(_, _) and
not parent.isStdLibRoot() and
/* Not in the source */
not exists(parent.getRelativePath())
)

View File

@@ -28,3 +28,7 @@ def foo():
foo()
#For test to find stdlib
import os