Skip to content

Commit a045991

Browse files
authored
bpo-42355: symtable.get_namespace() now checks whether there are multiple or any namespaces found (GH-23278)
1 parent 8f50f44 commit a045991

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

Doc/library/symtable.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,5 +194,5 @@ Examining Symbol Tables
194194

195195
.. method:: get_namespace()
196196

197-
Return the namespace bound to this name. If more than one namespace is
198-
bound, :exc:`ValueError` is raised.
197+
Return the namespace bound to this name. If more than one or no namespace
198+
is bound to this name, a :exc:`ValueError` is raised.

Lib/symtable.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,15 @@ def get_namespaces(self):
306306
def get_namespace(self):
307307
"""Return the single namespace bound to this name.
308308
309-
Raises ValueError if the name is bound to multiple namespaces.
309+
Raises ValueError if the name is bound to multiple namespaces
310+
or no namespace.
310311
"""
311-
if len(self.__namespaces) != 1:
312+
if len(self.__namespaces) == 0:
313+
raise ValueError("name is not bound to any namespaces")
314+
elif len(self.__namespaces) > 1:
312315
raise ValueError("name is bound to multiple namespaces")
313-
return self.__namespaces[0]
316+
else:
317+
return self.__namespaces[0]
314318

315319
if __name__ == "__main__":
316320
import os, sys

Lib/test/test_symtable.py

+4
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ def test_namespaces(self):
159159
self.assertEqual(len(ns_test.get_namespaces()), 2)
160160
self.assertRaises(ValueError, ns_test.get_namespace)
161161

162+
ns_test_2 = self.top.lookup("glob")
163+
self.assertEqual(len(ns_test_2.get_namespaces()), 0)
164+
self.assertRaises(ValueError, ns_test_2.get_namespace)
165+
162166
def test_assigned(self):
163167
self.assertTrue(self.spam.lookup("x").is_assigned())
164168
self.assertTrue(self.spam.lookup("bar").is_assigned())

0 commit comments

Comments
 (0)