diff --git a/Doc/library/symtable.rst b/Doc/library/symtable.rst index c9521d649b893e..927d5651d5ba40 100644 --- a/Doc/library/symtable.rst +++ b/Doc/library/symtable.rst @@ -192,5 +192,5 @@ Examining Symbol Tables .. method:: get_namespace() - Return the namespace bound to this name. If more than one namespace is - bound, :exc:`ValueError` is raised. + Return the namespace bound to this name. If more than one or no namespace + is bound to this name, a :exc:`ValueError` is raised. diff --git a/Lib/symtable.py b/Lib/symtable.py index 98db1e2557d37f..0f0d00871dfbbe 100644 --- a/Lib/symtable.py +++ b/Lib/symtable.py @@ -306,11 +306,15 @@ def get_namespaces(self): 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 a30e53496039be..819354e4eee9b5 100644 --- a/Lib/test/test_symtable.py +++ b/Lib/test/test_symtable.py @@ -159,6 +159,10 @@ def test_namespaces(self): 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())
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: