mirror of
https://github.com/github/codeql.git
synced 2026-05-01 03:35:13 +02:00
Python: Add tests for Module.declaredInAll
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
| all_dynamic.py:0:0:0:0 | Module all_dynamic | foo |
|
||||
| all_list.py:0:0:0:0 | Module all_list | bar |
|
||||
| all_list.py:0:0:0:0 | Module all_list | foo |
|
||||
@@ -0,0 +1,5 @@
|
||||
import python
|
||||
|
||||
from Module mod, string name
|
||||
where mod.declaredInAll(name)
|
||||
select mod, name
|
||||
@@ -0,0 +1,8 @@
|
||||
foo = "foo"
|
||||
bar = "bar"
|
||||
baz = "baz"
|
||||
|
||||
|
||||
__all__ = ["foo"]
|
||||
|
||||
__all__ += ["bar"]
|
||||
6
python/ql/test/library-tests/modules/__all__/all_list.py
Normal file
6
python/ql/test/library-tests/modules/__all__/all_list.py
Normal file
@@ -0,0 +1,6 @@
|
||||
foo = "foo"
|
||||
bar = "bar"
|
||||
baz = "baz"
|
||||
|
||||
|
||||
__all__ = ["foo", "bar"]
|
||||
6
python/ql/test/library-tests/modules/__all__/all_set.py
Normal file
6
python/ql/test/library-tests/modules/__all__/all_set.py
Normal file
@@ -0,0 +1,6 @@
|
||||
foo = "foo"
|
||||
bar = "bar"
|
||||
baz = "baz"
|
||||
|
||||
|
||||
__all__ = {"foo", "bar"}
|
||||
@@ -0,0 +1,6 @@
|
||||
foo = "foo"
|
||||
bar = "bar"
|
||||
baz = "baz"
|
||||
|
||||
|
||||
__all__ = ("foo", "bar")
|
||||
74
python/ql/test/library-tests/modules/__all__/main.py
Normal file
74
python/ql/test/library-tests/modules/__all__/main.py
Normal file
@@ -0,0 +1,74 @@
|
||||
# This file showcases how imports work with `__all__`.
|
||||
#
|
||||
# TL;DR; in `from <module> import *`, if `__all__` is defined in `<module>`, then only
|
||||
# the names in `__all__` will be imported -- otherwise all names that doesn't begin with
|
||||
# an underscore will be imported.
|
||||
#
|
||||
# If `__all__` is defined, `__all__` must be a sequence for `from <module> import *` to work.
|
||||
#
|
||||
# https://docs.python.org/3/reference/simple_stmts.html#the-import-statement
|
||||
# https://docs.python.org/3/glossary.html#term-sequence
|
||||
|
||||
print("import *")
|
||||
print("---")
|
||||
|
||||
from no_all import *
|
||||
print("no_all.py")
|
||||
print(" foo={!r}".format(foo))
|
||||
print(" bar={!r}".format(bar))
|
||||
print(" baz={!r}".format(baz))
|
||||
del foo, bar, baz
|
||||
|
||||
from all_list import *
|
||||
print("all_list.py")
|
||||
print(" foo={!r}".format(foo))
|
||||
print(" bar={!r}".format(bar))
|
||||
try:
|
||||
print(" baz={!r}".format(baz))
|
||||
except NameError:
|
||||
print(" baz not imported")
|
||||
del foo, bar
|
||||
|
||||
from all_tuple import *
|
||||
print("all_tuple.py")
|
||||
print(" foo={!r}".format(foo))
|
||||
print(" bar={!r}".format(bar))
|
||||
try:
|
||||
print(" baz={!r}".format(baz))
|
||||
except NameError:
|
||||
print(" baz not imported")
|
||||
del foo, bar
|
||||
|
||||
from all_dynamic import *
|
||||
print("all_dynamic.py")
|
||||
print(" foo={!r}".format(foo))
|
||||
print(" bar={!r}".format(bar))
|
||||
try:
|
||||
print(" baz={!r}".format(baz))
|
||||
except NameError:
|
||||
print(" baz not imported")
|
||||
del foo, bar
|
||||
|
||||
|
||||
# Example of wrong definition of `__all__`, where it is not a sequence.
|
||||
try:
|
||||
from all_set import *
|
||||
except TypeError as e:
|
||||
assert str(e) == "'set' object does not support indexing"
|
||||
print("from all_set import * could not be imported:", e)
|
||||
|
||||
print("")
|
||||
print("Direct reference on module")
|
||||
print("---")
|
||||
# Direct reference always works, no matter how `__all__` is set.
|
||||
import no_all
|
||||
import all_list
|
||||
import all_tuple
|
||||
import all_dynamic
|
||||
import all_set
|
||||
|
||||
for mod in [no_all, all_list, all_tuple, all_dynamic, all_set]:
|
||||
print("{}.py".format(mod.__name__))
|
||||
print(" foo={!r}".format(mod.foo))
|
||||
print(" bar={!r}".format(mod.bar))
|
||||
print(" baz={!r}".format(mod.baz))
|
||||
3
python/ql/test/library-tests/modules/__all__/no_all.py
Normal file
3
python/ql/test/library-tests/modules/__all__/no_all.py
Normal file
@@ -0,0 +1,3 @@
|
||||
foo = "foo"
|
||||
bar = "bar"
|
||||
baz = "baz"
|
||||
Reference in New Issue
Block a user