Skip to content

bpo-42355: symtable.get_namespace() now checks whether there are multiple or any namespaces found #23278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Doc/library/symtable.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 7 additions & 3 deletions Lib/symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down