diff options
author | Łukasz Langa <lukasz@langa.pl> | 2013-06-05 10:20:24 (GMT) |
---|---|---|
committer | Łukasz Langa <lukasz@langa.pl> | 2013-06-05 10:20:24 (GMT) |
commit | 6f69251980f385beac9d6f3e8ff4775bd37a1779 (patch) | |
tree | 0e0a667c980df9384ccc984292af51a9b70c6598 /Lib/pkgutil.py | |
parent | 072318b178f9824de5e0672218495f699dbdce44 (diff) | |
download | cpython-6f69251980f385beac9d6f3e8ff4775bd37a1779.zip cpython-6f69251980f385beac9d6f3e8ff4775bd37a1779.tar.gz cpython-6f69251980f385beac9d6f3e8ff4775bd37a1779.tar.bz2 |
Add reference implementation for PEP 443
PEP accepted: http://mail.python.org/pipermail/python-dev/2013-June/126734.html
Diffstat (limited to 'Lib/pkgutil.py')
-rw-r--r-- | Lib/pkgutil.py | 52 |
1 files changed, 6 insertions, 46 deletions
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py index 1901a5b..d2a6f1e 100644 --- a/Lib/pkgutil.py +++ b/Lib/pkgutil.py @@ -1,12 +1,13 @@ """Utilities to support packages.""" -import os -import sys -import importlib +from functools import singledispatch as simplegeneric import imp +import importlib +import os import os.path -from warnings import warn +import sys from types import ModuleType +from warnings import warn __all__ = [ 'get_importer', 'iter_importers', 'get_loader', 'find_loader', @@ -27,46 +28,6 @@ def read_code(stream): return marshal.load(stream) -def simplegeneric(func): - """Make a trivial single-dispatch generic function""" - registry = {} - def wrapper(*args, **kw): - ob = args[0] - try: - cls = ob.__class__ - except AttributeError: - cls = type(ob) - try: - mro = cls.__mro__ - except AttributeError: - try: - class cls(cls, object): - pass - mro = cls.__mro__[1:] - except TypeError: - mro = object, # must be an ExtensionClass or some such :( - for t in mro: - if t in registry: - return registry[t](*args, **kw) - else: - return func(*args, **kw) - try: - wrapper.__name__ = func.__name__ - except (TypeError, AttributeError): - pass # Python 2.3 doesn't allow functions to be renamed - - def register(typ, func=None): - if func is None: - return lambda f: register(typ, f) - registry[typ] = func - return func - - wrapper.__dict__ = func.__dict__ - wrapper.__doc__ = func.__doc__ - wrapper.register = register - return wrapper - - def walk_packages(path=None, prefix='', onerror=None): """Yields (module_loader, name, ispkg) for all modules recursively on path, or, if path is None, all accessible modules. @@ -148,13 +109,12 @@ def iter_modules(path=None, prefix=''): yield i, name, ispkg -#@simplegeneric +@simplegeneric def iter_importer_modules(importer, prefix=''): if not hasattr(importer, 'iter_modules'): return [] return importer.iter_modules(prefix) -iter_importer_modules = simplegeneric(iter_importer_modules) # Implement a file walker for the normal importlib path hook def _iter_file_finder_modules(importer, prefix=''): |