Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
This test shows how we handle modules the shadow a module in the standard library.

We manually replicate the behavior of `codeql database create --source-root <src-dir>`, which will use `-R <src-dir>`. By default, the way qltest invokes the extractor will cause different behavior. Therefore, we also need to move our code outside of the top-level folder, and it lives in `code/`.

Because we have a `cmd.py` file, whenever the python interpreter sees `import cmd`, that is the file that will be used! --

* `python test_ok.py` works as intended, and prints `Foo`
* `python test_fail.py` raises an exception, since it imports `pdb.py` from the standard library, which (at least in Python 3.8) tries to import `cmd.py` from the standard library, but instead is served our `cmd.py` module. Therefore it fails with `AttributeError: module 'cmd' has no attribute 'Cmd'`
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import python

from ModuleValue project_module, ControlFlowNode ref, string part_of_project
where
exists(project_module.getScope().getFile().getRelativePath()) and
ref = project_module.getAReference() and
(
exists(ref.getLocation().getFile().getRelativePath()) and
part_of_project = "is part of this projects code"
or
not exists(ref.getLocation().getFile().getRelativePath()) and
part_of_project = "is NOT part of this projects code"
)
select "Local module '" + project_module.getName() + "' referenced in module '" +
ref.getEnclosingModule().getName() + "' which " + part_of_project
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo = "Foo"
print("my own cmd imported")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# we import `pdb` which import the `cmd` module from the standard library
# and allows us to set --max-import-depth=2, to make the test run fast
import pdb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from cmd import foo
print(foo)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
semmle-extractor-options: --max-import-depth=2 -R code/