File tree 3 files changed +13
-5
lines changed
3 files changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -194,5 +194,5 @@ Examining Symbol Tables
194
194
195
195
.. method :: get_namespace()
196
196
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.
Original file line number Diff line number Diff line change @@ -306,11 +306,15 @@ def get_namespaces(self):
306
306
def get_namespace (self ):
307
307
"""Return the single namespace bound to this name.
308
308
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.
310
311
"""
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 :
312
315
raise ValueError ("name is bound to multiple namespaces" )
313
- return self .__namespaces [0 ]
316
+ else :
317
+ return self .__namespaces [0 ]
314
318
315
319
if __name__ == "__main__" :
316
320
import os , sys
Original file line number Diff line number Diff line change @@ -159,6 +159,10 @@ def test_namespaces(self):
159
159
self .assertEqual (len (ns_test .get_namespaces ()), 2 )
160
160
self .assertRaises (ValueError , ns_test .get_namespace )
161
161
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
+
162
166
def test_assigned (self ):
163
167
self .assertTrue (self .spam .lookup ("x" ).is_assigned ())
164
168
self .assertTrue (self .spam .lookup ("bar" ).is_assigned ())
You can’t perform that action at this time.
0 commit comments