@@ -186,6 +186,61 @@ def test_walk_packages_raises_on_string_or_bytes_input(self):
186186 with self .assertRaises ((TypeError , ValueError )):
187187 list (pkgutil .walk_packages (bytes_input ))
188188
189+ def test_name_resolution (self ):
190+ import logging
191+ import logging .handlers
192+
193+ success_cases = (
194+ ('os' , os ),
195+ ('os.path' , os .path ),
196+ ('os.path:pathsep' , os .path .pathsep ),
197+ ('logging' , logging ),
198+ ('logging:' , logging ),
199+ ('logging.handlers' , logging .handlers ),
200+ ('logging.handlers:' , logging .handlers ),
201+ ('logging.handlers:SysLogHandler' , logging .handlers .SysLogHandler ),
202+ ('logging.handlers.SysLogHandler' , logging .handlers .SysLogHandler ),
203+ ('logging.handlers:SysLogHandler.LOG_ALERT' ,
204+ logging .handlers .SysLogHandler .LOG_ALERT ),
205+ ('logging.handlers.SysLogHandler.LOG_ALERT' ,
206+ logging .handlers .SysLogHandler .LOG_ALERT ),
207+ ('builtins.int' , int ),
208+ ('builtins:int' , int ),
209+ ('builtins.int.from_bytes' , int .from_bytes ),
210+ ('builtins:int.from_bytes' , int .from_bytes ),
211+ ('builtins.ZeroDivisionError' , ZeroDivisionError ),
212+ ('builtins:ZeroDivisionError' , ZeroDivisionError ),
213+ ('os:path' , os .path ),
214+ )
215+
216+ failure_cases = (
217+ (None , TypeError ),
218+ (1 , TypeError ),
219+ (2.0 , TypeError ),
220+ (True , TypeError ),
221+ ('' , ValueError ),
222+ ('?abc' , ValueError ),
223+ ('abc/foo' , ValueError ),
224+ ('foo' , ImportError ),
225+ ('os.foo' , AttributeError ),
226+ ('os.foo:' , ImportError ),
227+ ('os.pth:pathsep' , ImportError ),
228+ ('logging.handlers:NoSuchHandler' , AttributeError ),
229+ ('logging.handlers:SysLogHandler.NO_SUCH_VALUE' , AttributeError ),
230+ ('logging.handlers.SysLogHandler.NO_SUCH_VALUE' , AttributeError ),
231+ ('ZeroDivisionError' , ImportError ),
232+ )
233+
234+ for s , expected in success_cases :
235+ with self .subTest (s = s ):
236+ o = pkgutil .resolve_name (s )
237+ self .assertEqual (o , expected )
238+
239+ for s , exc in failure_cases :
240+ with self .subTest (s = s ):
241+ with self .assertRaises (exc ):
242+ pkgutil .resolve_name (s )
243+
189244
190245class PkgutilPEP302Tests (unittest .TestCase ):
191246
0 commit comments