mirror of https://github.com/python/cpython
bpo-42355: symtable.get_namespace() now checks whether there are multiple or any namespaces found (GH-23278)
This commit is contained in:
parent
8f50f44592
commit
a045991f60
|
@ -194,5 +194,5 @@ Examining Symbol Tables
|
||||||
|
|
||||||
.. method:: get_namespace()
|
.. method:: get_namespace()
|
||||||
|
|
||||||
Return the namespace bound to this name. If more than one namespace is
|
Return the namespace bound to this name. If more than one or no namespace
|
||||||
bound, :exc:`ValueError` is raised.
|
is bound to this name, a :exc:`ValueError` is raised.
|
||||||
|
|
|
@ -306,11 +306,15 @@ class Symbol:
|
||||||
def get_namespace(self):
|
def get_namespace(self):
|
||||||
"""Return the single namespace bound to this name.
|
"""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")
|
raise ValueError("name is bound to multiple namespaces")
|
||||||
return self.__namespaces[0]
|
else:
|
||||||
|
return self.__namespaces[0]
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import os, sys
|
import os, sys
|
||||||
|
|
|
@ -159,6 +159,10 @@ class SymtableTest(unittest.TestCase):
|
||||||
self.assertEqual(len(ns_test.get_namespaces()), 2)
|
self.assertEqual(len(ns_test.get_namespaces()), 2)
|
||||||
self.assertRaises(ValueError, ns_test.get_namespace)
|
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):
|
def test_assigned(self):
|
||||||
self.assertTrue(self.spam.lookup("x").is_assigned())
|
self.assertTrue(self.spam.lookup("x").is_assigned())
|
||||||
self.assertTrue(self.spam.lookup("bar").is_assigned())
|
self.assertTrue(self.spam.lookup("bar").is_assigned())
|
||||||
|
|
Loading…
Reference in New Issue