Skip to content

Commit ad13fba

Browse files
committed
Python: Add tests
A slightly complicated test setup. I wanted to both make sure I captured the semantics of Python and also the fact that the kinds of global flow we expect to see are indeed present. The code is executable, and prints out both when the execution reaches certain files, and also what values are assigned to the various attributes that are referenced throughout the program. These values are validated in the test as well. My original version used introspection to avoid referencing attributes directly (thus enabling better error diagnostics), but unfortunately that made it so that the model couldn't follow what was going on. The current setup is a bit clunky (and Python's scoping rules makes it especially so -- cf. the explicit calls to `globals` and `locals`), but I think it does the job okay.
1 parent 651afaf commit ad13fba

File tree

12 files changed

+258
-0
lines changed

12 files changed

+258
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from trace import *
2+
enter(__file__)
3+
4+
bar_attr = "bar_attr"
5+
6+
exit(__file__)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from trace import *
2+
enter(__file__)
3+
4+
# A simple attribute. Used in main.py
5+
foo_attr = "foo_attr"
6+
7+
# A private attribute. Accessible from main.py despite this.
8+
__private_foo_attr = "__private_foo_attr"
9+
10+
# A reexport of bar under a new name. Used in main.py
11+
import bar as bar_reexported #$ imports=bar as=bar_reexported
12+
check("bar_reexported.bar_attr", bar_reexported.bar_attr, "bar_attr", globals()) #$ prints=bar_attr
13+
14+
exit(__file__)

python/ql/test/experimental/import-resolution/importflow.expected

Whitespace-only changes.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import python
2+
import semmle.python.dataflow.new.DataFlow
3+
import semmle.python.ApiGraphs
4+
import TestUtilities.InlineExpectationsTest
5+
6+
private class SourceString extends DataFlow::Node {
7+
string contents;
8+
9+
SourceString() {
10+
this.asExpr().(StrConst).getText() = contents and
11+
this.asExpr().getParent() instanceof Assign
12+
}
13+
14+
string getContents() { result = contents }
15+
}
16+
17+
private class ImportConfiguration extends DataFlow::Configuration {
18+
ImportConfiguration() { this = "ImportConfiguration" }
19+
20+
override predicate isSource(DataFlow::Node source) { source instanceof SourceString }
21+
22+
override predicate isSink(DataFlow::Node sink) {
23+
sink = API::moduleImport("trace").getMember("check").getACall().getArg(1)
24+
}
25+
}
26+
27+
class ResolutionTest extends InlineExpectationsTest {
28+
ResolutionTest() { this = "ResolutionTest" }
29+
30+
override string getARelevantTag() { result = "import" }
31+
32+
override predicate hasActualResult(Location location, string element, string tag, string value) {
33+
exists(DataFlow::PathNode source, DataFlow::PathNode sink, ImportConfiguration config |
34+
config.hasFlowPath(source, sink) and
35+
tag = "prints" and
36+
location = sink.getNode().getLocation() and
37+
value = source.getNode().(SourceString).getContents() and
38+
element = sink.getNode().toString()
39+
)
40+
}
41+
}

python/ql/test/experimental/import-resolution/imports.expected

Whitespace-only changes.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import python
2+
import TestUtilities.InlineExpectationsTest
3+
import semmle.python.dataflow.new.DataFlow
4+
import semmle.python.dataflow.new.internal.ImportResolution
5+
6+
private class ImmediateModuleRef extends DataFlow::Node {
7+
Module mod;
8+
string alias;
9+
10+
ImmediateModuleRef() {
11+
this = ImportResolution::getImmediateModuleReference(mod) and
12+
not mod.getName() in ["__future__", "trace"] and
13+
this.asExpr() = any(Alias a | alias = a.getAsname().(Name).getId()).getAsname()
14+
}
15+
16+
Module getModule() { result = mod }
17+
18+
string getAsname() { result = alias }
19+
}
20+
21+
class ImportTest extends InlineExpectationsTest {
22+
ImportTest() { this = "ImportTest" }
23+
24+
override string getARelevantTag() { result = "imports" }
25+
26+
override predicate hasActualResult(Location location, string element, string tag, string value) {
27+
exists(ImmediateModuleRef ref |
28+
tag = "imports" and
29+
location = ref.getLocation() and
30+
value = ref.getModule().getName() and
31+
element = ref.toString()
32+
)
33+
}
34+
}
35+
36+
class AliasTest extends InlineExpectationsTest {
37+
AliasTest() { this = "AliasTest" }
38+
39+
override string getARelevantTag() { result = "as" }
40+
41+
override predicate hasActualResult(Location location, string element, string tag, string value) {
42+
exists(ImmediateModuleRef ref |
43+
tag = "as" and
44+
location = ref.getLocation() and
45+
value = ref.getAsname() and
46+
element = ref.toString()
47+
)
48+
}
49+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#! /usr/bin/env python3
2+
3+
from __future__ import print_function
4+
import sys
5+
from trace import *
6+
enter(__file__)
7+
8+
# A simple import. Binds foo to the foo module
9+
import foo #$ imports=foo as=foo
10+
check("foo.foo_attr", foo.foo_attr, "foo_attr", globals()) #$ prints=foo_attr
11+
12+
# Private attributes are still accessible.
13+
check("foo.__private_foo_attr", foo.__private_foo_attr, "__private_foo_attr", globals()) #$ prints=__private_foo_attr
14+
15+
# An aliased import, binding foo to foo_alias
16+
import foo as foo_alias #$ imports=foo as=foo_alias
17+
check("foo_alias.foo_attr", foo_alias.foo_attr, "foo_attr", globals()) #$ prints=foo_attr
18+
19+
# A reference to a reexported module
20+
check("foo.bar_reexported.bar_attr", foo.bar_reexported.bar_attr, "bar_attr", globals()) #$ prints=bar_attr
21+
22+
# A simple "import from" statement.
23+
from bar import bar_attr
24+
check("bar_attr", bar_attr, "bar_attr", globals()) #$ prints=bar_attr
25+
26+
# Importing an attribute from a subpackage of a package.
27+
from package.subpackage import subpackage_attr
28+
check("subpackage_attr", subpackage_attr, "subpackage_attr", globals()) #$ prints=subpackage_attr
29+
30+
# Importing a package attribute under an alias.
31+
from package import package_attr as package_attr_alias
32+
check("package_attr_alias", package_attr_alias, "package_attr", globals()) #$ prints=package_attr
33+
34+
# Importing a subpackage under an alias.
35+
from package import subpackage as aliased_subpackage #$ imports=package.subpackage.__init__ as=aliased_subpackage
36+
check("aliased_subpackage.subpackage_attr", aliased_subpackage.subpackage_attr, "subpackage_attr", globals()) #$ prints=subpackage_attr
37+
38+
def local_import():
39+
# Same as above, but in a local scope.
40+
import package.subpackage as local_subpackage #$ imports=package.subpackage.__init__ as=local_subpackage
41+
check("local_subpackage.subpackage_attr", local_subpackage.subpackage_attr, "subpackage_attr", locals()) #$ prints=subpackage_attr
42+
43+
local_import()
44+
45+
# Importing a subpacking using `import` and binding it to a name.
46+
import package.subpackage as aliased_subpackage #$ imports=package.subpackage.__init__ as=aliased_subpackage
47+
check("aliased_subpackage.subpackage_attr", aliased_subpackage.subpackage_attr, "subpackage_attr", globals()) #$ prints=subpackage_attr
48+
49+
# Importing without binding instead binds the top level name.
50+
import package.subpackage #$ imports=package.__init__ as=package
51+
check("package.package_attr", package.package_attr, "package_attr", globals()) #$ prints=package_attr
52+
53+
if sys.version_info[0] >= 3:
54+
# Importing from a namespace module.
55+
from namespace_package.namespace_module import namespace_module_attr
56+
check("namespace_module_attr", namespace_module_attr, "namespace_module_attr", globals()) #$ prints=namespace_module_attr
57+
58+
exit(__file__)
59+
60+
print()
61+
62+
if status() == 0:
63+
print("PASS")
64+
else:
65+
print("FAIL")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from trace import *
2+
enter(__file__)
3+
4+
namespace_module_attr = "namespace_module_attr"
5+
6+
exit(__file__)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from trace import *
2+
enter(__file__)
3+
4+
attr_used_in_subpackage = "attr_used_in_subpackage"
5+
package_attr = "package_attr"
6+
7+
exit(__file__)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from trace import *
2+
enter(__file__)
3+
4+
subpackage_attr = "subpackage_attr"
5+
6+
# Importing an attribute from the parent package.
7+
from .. import attr_used_in_subpackage as imported_attr
8+
check("imported_attr", imported_attr, "attr_used_in_subpackage", globals()) #$ prints=attr_used_in_subpackage
9+
10+
# Importing an irrelevant attribute from a sibling module binds the name to the module.
11+
from .submodule import irrelevant_attr
12+
check("submodule.submodule_attr", submodule.submodule_attr, "submodule_attr", globals()) #$ prints=submodule_attr
13+
14+
exit(__file__)

0 commit comments

Comments
 (0)