From a8a7a59ae83dda9bd5aec5a7978614ddff9fab75 Mon Sep 17 00:00:00 2001 From: Taus Date: Fri, 11 Nov 2022 14:47:35 +0000 Subject: [PATCH] Python: Add test for attribute name clash --- .../experimental/import-resolution/attr_clash/__init__.py | 6 ++++++ .../import-resolution/attr_clash/clashing_attr.py | 4 ++++ .../import-resolution/attr_clash/non_clashing_submodule.py | 4 ++++ python/ql/test/experimental/import-resolution/main.py | 7 ++++++- python/ql/test/experimental/import-resolution/trace.py | 3 +++ 5 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 python/ql/test/experimental/import-resolution/attr_clash/__init__.py create mode 100644 python/ql/test/experimental/import-resolution/attr_clash/clashing_attr.py create mode 100644 python/ql/test/experimental/import-resolution/attr_clash/non_clashing_submodule.py diff --git a/python/ql/test/experimental/import-resolution/attr_clash/__init__.py b/python/ql/test/experimental/import-resolution/attr_clash/__init__.py new file mode 100644 index 00000000000..d210fbd703b --- /dev/null +++ b/python/ql/test/experimental/import-resolution/attr_clash/__init__.py @@ -0,0 +1,6 @@ +from trace import * +enter(__file__) + +clashing_attr = "clashing_attr" + +exit(__file__) diff --git a/python/ql/test/experimental/import-resolution/attr_clash/clashing_attr.py b/python/ql/test/experimental/import-resolution/attr_clash/clashing_attr.py new file mode 100644 index 00000000000..7544594893b --- /dev/null +++ b/python/ql/test/experimental/import-resolution/attr_clash/clashing_attr.py @@ -0,0 +1,4 @@ +from trace import * +enter(__file__) + +exit(__file__) diff --git a/python/ql/test/experimental/import-resolution/attr_clash/non_clashing_submodule.py b/python/ql/test/experimental/import-resolution/attr_clash/non_clashing_submodule.py new file mode 100644 index 00000000000..7544594893b --- /dev/null +++ b/python/ql/test/experimental/import-resolution/attr_clash/non_clashing_submodule.py @@ -0,0 +1,4 @@ +from trace import * +enter(__file__) + +exit(__file__) diff --git a/python/ql/test/experimental/import-resolution/main.py b/python/ql/test/experimental/import-resolution/main.py index 801b081df0c..d2ad1b9e337 100644 --- a/python/ql/test/experimental/import-resolution/main.py +++ b/python/ql/test/experimental/import-resolution/main.py @@ -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, "", globals()) + exit(__file__) print() diff --git a/python/ql/test/experimental/import-resolution/trace.py b/python/ql/test/experimental/import-resolution/trace.py index 04e847304e9..90d8efba3fb 100644 --- a/python/ql/test/experimental/import-resolution/trace.py +++ b/python/ql/test/experimental/import-resolution/trace.py @@ -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("" if val != expected_value: print("Error: Expected {} to be {}, got {}".format(attr_path, expected_value, val)) _status = 1