mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
Add reference implementation for PEP 443
PEP accepted: http://mail.python.org/pipermail/python-dev/2013-June/126734.html
This commit is contained in:
parent
072318b178
commit
6f69251980
6 changed files with 614 additions and 55 deletions
|
@ -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=''):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue