summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorVinay Sajip <vinay_sajip@yahoo.co.uk>2020-02-14 22:02:13 (GMT)
committerGitHub <noreply@github.com>2020-02-14 22:02:13 (GMT)
commit1ed61617a4a6632905ad6a0b440cd2cafb8b6414 (patch)
treebfe15c8f526f05a0c214d409bb94e64e750442a4 /Lib/test
parent9aeb0ef9309384099e2f23bcee2240fbc096568e (diff)
downloadcpython-1ed61617a4a6632905ad6a0b440cd2cafb8b6414.zip
cpython-1ed61617a4a6632905ad6a0b440cd2cafb8b6414.tar.gz
cpython-1ed61617a4a6632905ad6a0b440cd2cafb8b6414.tar.bz2
bpo-12915: Add pkgutil.resolve_name (GH-18310)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_pkgutil.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py
index 2887ce6..906150b 100644
--- a/Lib/test/test_pkgutil.py
+++ b/Lib/test/test_pkgutil.py
@@ -186,6 +186,61 @@ class PkgutilTests(unittest.TestCase):
with self.assertRaises((TypeError, ValueError)):
list(pkgutil.walk_packages(bytes_input))
+ def test_name_resolution(self):
+ import logging
+ import logging.handlers
+
+ success_cases = (
+ ('os', os),
+ ('os.path', os.path),
+ ('os.path:pathsep', os.path.pathsep),
+ ('logging', logging),
+ ('logging:', logging),
+ ('logging.handlers', logging.handlers),
+ ('logging.handlers:', logging.handlers),
+ ('logging.handlers:SysLogHandler', logging.handlers.SysLogHandler),
+ ('logging.handlers.SysLogHandler', logging.handlers.SysLogHandler),
+ ('logging.handlers:SysLogHandler.LOG_ALERT',
+ logging.handlers.SysLogHandler.LOG_ALERT),
+ ('logging.handlers.SysLogHandler.LOG_ALERT',
+ logging.handlers.SysLogHandler.LOG_ALERT),
+ ('builtins.int', int),
+ ('builtins:int', int),
+ ('builtins.int.from_bytes', int.from_bytes),
+ ('builtins:int.from_bytes', int.from_bytes),
+ ('builtins.ZeroDivisionError', ZeroDivisionError),
+ ('builtins:ZeroDivisionError', ZeroDivisionError),
+ ('os:path', os.path),
+ )
+
+ failure_cases = (
+ (None, TypeError),
+ (1, TypeError),
+ (2.0, TypeError),
+ (True, TypeError),
+ ('', ValueError),
+ ('?abc', ValueError),
+ ('abc/foo', ValueError),
+ ('foo', ImportError),
+ ('os.foo', AttributeError),
+ ('os.foo:', ImportError),
+ ('os.pth:pathsep', ImportError),
+ ('logging.handlers:NoSuchHandler', AttributeError),
+ ('logging.handlers:SysLogHandler.NO_SUCH_VALUE', AttributeError),
+ ('logging.handlers.SysLogHandler.NO_SUCH_VALUE', AttributeError),
+ ('ZeroDivisionError', ImportError),
+ )
+
+ for s, expected in success_cases:
+ with self.subTest(s=s):
+ o = pkgutil.resolve_name(s)
+ self.assertEqual(o, expected)
+
+ for s, exc in failure_cases:
+ with self.subTest(s=s):
+ with self.assertRaises(exc):
+ pkgutil.resolve_name(s)
+
class PkgutilPEP302Tests(unittest.TestCase):