]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42355: symtable.get_namespace() now checks whether there are multiple or any...
authorBatuhan Taskaya <batuhan@python.org>
Sun, 18 Jul 2021 12:56:09 +0000 (15:56 +0300)
committerGitHub <noreply@github.com>
Sun, 18 Jul 2021 12:56:09 +0000 (15:56 +0300)
Doc/library/symtable.rst
Lib/symtable.py
Lib/test/test_symtable.py

index e364232247c2004fda1b5534c9658c5e985e1e96..0264f891cc8c066ab12bf72349528f6ee9e40479 100644 (file)
@@ -194,5 +194,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.
index 922854bf669b3ca92ba829c920491476bd2b5a28..75ff0921f4c0db7e2e12af19b0611fc6504c3b20 100644 (file)
@@ -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
index a30e53496039be4f2aecf49e364edd98dc86a69e..819354e4eee9b5e03852d34daf1b17691f9b6f1b 100644 (file)
@@ -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())