mirror of
https://github.com/github/codeql.git
synced 2025-12-24 04:36:35 +01:00
Python: Add test for attribute name clash
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
from trace import *
|
||||
enter(__file__)
|
||||
|
||||
clashing_attr = "clashing_attr"
|
||||
|
||||
exit(__file__)
|
||||
@@ -0,0 +1,4 @@
|
||||
from trace import *
|
||||
enter(__file__)
|
||||
|
||||
exit(__file__)
|
||||
@@ -0,0 +1,4 @@
|
||||
from trace import *
|
||||
enter(__file__)
|
||||
|
||||
exit(__file__)
|
||||
@@ -35,7 +35,7 @@ import foo as foo_alias #$ imports=foo as=foo_alias
|
||||
check("foo_alias.foo_attr", foo_alias.foo_attr, "foo_attr", globals()) #$ prints=foo_attr
|
||||
|
||||
# A reference to a reexported module
|
||||
check("foo.bar_reexported.bar_attr", foo.bar_reexported.bar_attr, "bar_attr", globals()) #$ prints=bar_attr
|
||||
check("foo.bar_reexported.bar_attr", foo.bar_reexported.bar_attr, "bar_attr", globals()) #$ MISSING: prints=bar_attr
|
||||
|
||||
# A simple "import from" statement.
|
||||
from bar import bar_attr
|
||||
@@ -73,6 +73,11 @@ if sys.version_info[0] >= 3:
|
||||
from namespace_package.namespace_module import namespace_module_attr
|
||||
check("namespace_module_attr", namespace_module_attr, "namespace_module_attr", globals()) #$ prints=namespace_module_attr
|
||||
|
||||
|
||||
from attr_clash import clashing_attr, non_clashing_submodule #$ imports=attr_clash.clashing_attr as=clashing_attr imports=attr_clash.non_clashing_submodule as=non_clashing_submodule
|
||||
check("clashing_attr", clashing_attr, "clashing_attr", globals()) #$ prints=clashing_attr
|
||||
check("non_clashing_submodule", non_clashing_submodule, "<module attr_clash.non_clashing_submodule>", globals())
|
||||
|
||||
exit(__file__)
|
||||
|
||||
print()
|
||||
|
||||
@@ -24,6 +24,7 @@ def status():
|
||||
return _status
|
||||
|
||||
def check(attr_path, actual_value, expected_value, bindings):
|
||||
global _status
|
||||
parts = attr_path.split(".")
|
||||
base, parts = parts[0], parts[1:]
|
||||
if base not in bindings:
|
||||
@@ -40,6 +41,8 @@ def check(attr_path, actual_value, expected_value, bindings):
|
||||
if val != actual_value:
|
||||
print("Error: Value at path {} and actual value are out of sync! {} != {}".format(attr_path, val, actual_value))
|
||||
_status = 1
|
||||
if str(val).startswith("<module"):
|
||||
val = "<module " + val.__name__ + ">"
|
||||
if val != expected_value:
|
||||
print("Error: Expected {} to be {}, got {}".format(attr_path, expected_value, val))
|
||||
_status = 1
|
||||
|
||||
Reference in New Issue
Block a user