Skip to content

Commit 990d1c1

Browse files
authored
Merge pull request #2802 from RasmusWL/python-fix-fp-py/import-own-module
Python: Fix FP for py/import own module
2 parents 0628625 + 1f76284 commit 990d1c1

File tree

13 files changed

+49
-3
lines changed

13 files changed

+49
-3
lines changed

python/ql/src/Imports/ModuleImportsItself.ql

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212

1313
import python
1414

15-
predicate modules_imports_itself(Import i, ModuleValue m) {
15+
predicate modules_imports_itself(ImportingStmt i, ModuleValue m) {
1616
i.getEnclosingModule() = m.getScope() and
17-
m.importedAs(i.getAnImportedModuleName())
17+
m = max(string s, ModuleValue m_ |
18+
s = i.getAnImportedModuleName() and
19+
m_.importedAs(s)
20+
|
21+
m_ order by s.length()
22+
)
1823
}
1924

20-
from Import i, ModuleValue m
25+
from ImportingStmt i, ModuleValue m
2126
where modules_imports_itself(i, m)
2227
select i, "The module '" + m.getName() + "' imports itself."
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
| imports_test.py:4:1:4:19 | Import | Module 'test_module2' is imported with both 'import' and 'import from' |
2+
| pkg_notok/__init__.py:4:1:4:16 | Import | Module 'pkg_notok' is imported with both 'import' and 'import from' |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
| imports_test.py:8:1:8:19 | Import | The module 'imports_test' imports itself. |
2+
| pkg_notok/__init__.py:4:1:4:16 | Import | The module 'pkg_notok' imports itself. |
3+
| pkg_notok/__init__.py:12:1:12:25 | Import | The module 'pkg_notok' imports itself. |
4+
| pkg_notok/__init__.py:13:1:13:37 | Import | The module 'pkg_notok' imports itself. |
5+
| pkg_notok/__init__.py:14:1:14:23 | from pkg_notok import * | The module 'pkg_notok' imports itself. |

python/ql/test/query-tests/Imports/PyCheckerTests/imports_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
#Module imports itself
88
import imports_test
99

10+
import pkg_ok
11+
import pkg_notok
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options: --max-import-depth=2
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Foo(object):
2+
pass
3+
4+
import pkg_notok
5+
6+
# This import is a bit tricky. It will make `bar` available in as `pkg_notok.bar` as a
7+
# side effect (see https://docs.python.org/3/reference/import.html#submodules), but the
8+
# *import* will add a binding to `pkg_notok` to the current scope -- so technically the
9+
# module imports itself.
10+
import pkg_notok.bar
11+
12+
from pkg_notok import Foo
13+
from pkg_notok import Foo as NotOkFoo
14+
from pkg_notok import *
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Bar():
2+
pass
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pkg_ok.foo1 as foo1
2+
3+
from pkg_ok import foo2
4+
from pkg_ok.foo3 import Foo3
5+
6+
from . import foo4
7+
from .foo5 import Foo5
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Foo1():
2+
pass
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Foo2():
2+
pass

0 commit comments

Comments
 (0)