Merge pull request #2802 from RasmusWL/python-fix-fp-py/import-own-module

Python: Fix FP for py/import own module
This commit is contained in:
Taus
2020-02-17 11:23:11 +01:00
committed by GitHub
13 changed files with 49 additions and 3 deletions

View File

@@ -12,11 +12,16 @@
import python
predicate modules_imports_itself(Import i, ModuleValue m) {
predicate modules_imports_itself(ImportingStmt i, ModuleValue m) {
i.getEnclosingModule() = m.getScope() and
m.importedAs(i.getAnImportedModuleName())
m = max(string s, ModuleValue m_ |
s = i.getAnImportedModuleName() and
m_.importedAs(s)
|
m_ order by s.length()
)
}
from Import i, ModuleValue m
from ImportingStmt i, ModuleValue m
where modules_imports_itself(i, m)
select i, "The module '" + m.getName() + "' imports itself."

View File

@@ -1 +1,2 @@
| imports_test.py:4:1:4:19 | Import | Module 'test_module2' is imported with both 'import' and 'import from' |
| pkg_notok/__init__.py:4:1:4:16 | Import | Module 'pkg_notok' is imported with both 'import' and 'import from' |

View File

@@ -1 +1,5 @@
| imports_test.py:8:1:8:19 | Import | The module 'imports_test' imports itself. |
| pkg_notok/__init__.py:4:1:4:16 | Import | The module 'pkg_notok' imports itself. |
| pkg_notok/__init__.py:12:1:12:25 | Import | The module 'pkg_notok' imports itself. |
| pkg_notok/__init__.py:13:1:13:37 | Import | The module 'pkg_notok' imports itself. |
| pkg_notok/__init__.py:14:1:14:23 | from pkg_notok import * | The module 'pkg_notok' imports itself. |

View File

@@ -7,3 +7,5 @@ from test_module2 import func
#Module imports itself
import imports_test
import pkg_ok
import pkg_notok

View File

@@ -0,0 +1 @@
semmle-extractor-options: --max-import-depth=2

View File

@@ -0,0 +1,14 @@
class Foo(object):
pass
import pkg_notok
# This import is a bit tricky. It will make `bar` available in as `pkg_notok.bar` as a
# side effect (see https://docs.python.org/3/reference/import.html#submodules), but the
# *import* will add a binding to `pkg_notok` to the current scope -- so technically the
# module imports itself.
import pkg_notok.bar
from pkg_notok import Foo
from pkg_notok import Foo as NotOkFoo
from pkg_notok import *

View File

@@ -0,0 +1,2 @@
class Bar():
pass

View File

@@ -0,0 +1,7 @@
import pkg_ok.foo1 as foo1
from pkg_ok import foo2
from pkg_ok.foo3 import Foo3
from . import foo4
from .foo5 import Foo5

View File

@@ -0,0 +1,2 @@
class Foo1():
pass

View File

@@ -0,0 +1,2 @@
class Foo2():
pass

View File

@@ -0,0 +1,2 @@
class Foo3():
pass

View File

@@ -0,0 +1,2 @@
class Foo4():
pass

View File

@@ -0,0 +1,2 @@
class Foo5():
pass