[Python-checkins] bpo-42355: symtable.get_namespace() now checks whether there are multiple or any namespaces found (GH-23278)
isidentical
webhook-mailer at python.org
Sun Jul 18 08:56:14 EDT 2021
https://github.com/python/cpython/commit/a045991f60b51636a784623dda7ad84b5b2c6b73
commit: a045991f60b51636a784623dda7ad84b5b2c6b73
branch: main
author: Batuhan Taskaya <batuhan at python.org>
committer: isidentical <isidentical at gmail.com>
date: 2021-07-18T15:56:09+03:00
summary:
bpo-42355: symtable.get_namespace() now checks whether there are multiple or any namespaces found (GH-23278)
files:
M Doc/library/symtable.rst
M Lib/symtable.py
M Lib/test/test_symtable.py
diff --git a/Doc/library/symtable.rst b/Doc/library/symtable.rst
index e364232247c200..0264f891cc8c06 100644
--- a/Doc/library/symtable.rst
+++ b/Doc/library/symtable.rst
@@ -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.
diff --git a/Lib/symtable.py b/Lib/symtable.py
index 922854bf669b3c..75ff0921f4c0db 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())
More information about the Python-checkins
mailing list