Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 4 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 10 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 4 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.