Commit 51d8bfc8 authored by Brett Cannon's avatar Brett Cannon

Create a simple substitute for functools.wraps to use in importlib._bootstrap.

parent 534b2cd1
to do to do
///// /////
* Backport a poor-man's functools.wraps.
* Implement PEP 302 protocol for loaders (should just be a matter of testing). * Implement PEP 302 protocol for loaders (should just be a matter of testing).
+ Built-in. + Built-in.
......
...@@ -90,6 +90,13 @@ class closing: ...@@ -90,6 +90,13 @@ class closing:
self.obj.close() self.obj.close()
def wrap(new, old):
"""Simple substitute for functools.wraps."""
for replace in ['__module__', '__name__', '__doc__']:
setattr(new, replace, getattr(old, replace))
new.__dict__.update(old.__dict__)
def set___package__(fxn): def set___package__(fxn):
"""Set __package__ on the returned module.""" """Set __package__ on the returned module."""
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
...@@ -99,6 +106,7 @@ def set___package__(fxn): ...@@ -99,6 +106,7 @@ def set___package__(fxn):
if not hasattr(module, '__path__'): if not hasattr(module, '__path__'):
module.__package__ = module.__package__.rpartition('.')[0] module.__package__ = module.__package__.rpartition('.')[0]
return module return module
wrap(wrapper, fxn)
return wrapper return wrapper
...@@ -213,9 +221,7 @@ def check_name(method): ...@@ -213,9 +221,7 @@ def check_name(method):
if self._name != name: if self._name != name:
raise ImportError("loader cannot handle %s" % name) raise ImportError("loader cannot handle %s" % name)
return method(self, name, *args, **kwargs) return method(self, name, *args, **kwargs)
inner.__name__ = method.__name__ wrap(inner, method)
inner.__doc__ = method.__doc__
inner.__dict__.update(method.__dict__)
return inner return inner
...@@ -318,6 +324,7 @@ def get_module(fxn): ...@@ -318,6 +324,7 @@ def get_module(fxn):
elif hasattr(module, attr): elif hasattr(module, attr):
delattr(module, attr) delattr(module, attr)
raise raise
wrap(decorated, fxn)
return decorated return decorated
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment