diff options
author | Batuhan Taskaya <batuhan@python.org> | 2021-07-18 12:56:09 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-18 12:56:09 (GMT) |
commit | a045991f60b51636a784623dda7ad84b5b2c6b73 (patch) | |
tree | 3b778d4c70034327e1066aee0060638d93ce11ad /Lib | |
parent | 8f50f44592190b5a8cb115f0d58d577036e68308 (diff) | |
download | cpython-a045991f60b51636a784623dda7ad84b5b2c6b73.zip cpython-a045991f60b51636a784623dda7ad84b5b2c6b73.tar.gz cpython-a045991f60b51636a784623dda7ad84b5b2c6b73.tar.bz2 |
bpo-42355: symtable.get_namespace() now checks whether there are multiple or any namespaces found (GH-23278)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/symtable.py | 10 | ||||
-rw-r--r-- | Lib/test/test_symtable.py | 4 |
2 files changed, 11 insertions, 3 deletions
diff --git a/Lib/symtable.py b/Lib/symtable.py index 922854b..75ff092 100644 --- a/Lib/symtable.py +++ b/Lib/symtable.py @@ -306,11 +306,15 @@ class Symbol: def get_namespace(self): """Return the single namespace bound to this name. - Raises ValueError if the name is bound to multiple namespaces. + Raises ValueError if the name is bound to multiple namespaces + or no namespace. """ - if len(self.__namespaces) != 1: + if len(self.__namespaces) == 0: + raise ValueError("name is not bound to any namespaces") + elif len(self.__namespaces) > 1: raise ValueError("name is bound to multiple namespaces") - return self.__namespaces[0] + else: + return self.__namespaces[0] if __name__ == "__main__": import os, sys diff --git a/Lib/test/test_symtable.py b/Lib/test/test_symtable.py index a30e534..819354e 100644 --- a/Lib/test/test_symtable.py +++ b/Lib/test/test_symtable.py @@ -159,6 +159,10 @@ class SymtableTest(unittest.TestCase): self.assertEqual(len(ns_test.get_namespaces()), 2) self.assertRaises(ValueError, ns_test.get_namespace) + ns_test_2 = self.top.lookup("glob") + self.assertEqual(len(ns_test_2.get_namespaces()), 0) + self.assertRaises(ValueError, ns_test_2.get_namespace) + def test_assigned(self): self.assertTrue(self.spam.lookup("x").is_assigned()) self.assertTrue(self.spam.lookup("bar").is_assigned()) |