Skip to content

Commit

Permalink
Create a simple substitute for functools.wraps to use in importlib._b…
Browse files Browse the repository at this point in the history
…ootstrap.
  • Loading branch information
brettcannon committed Feb 7, 2009
1 parent 534b2cd commit 51d8bfc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 0 additions & 2 deletions Lib/importlib/NOTES
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
to do
/////

* Backport a poor-man's functools.wraps.

* Implement PEP 302 protocol for loaders (should just be a matter of testing).

+ Built-in.
Expand Down
13 changes: 10 additions & 3 deletions Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ def __exit__(self, *args):
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):
"""Set __package__ on the returned module."""
def wrapper(*args, **kwargs):
Expand All @@ -99,6 +106,7 @@ def wrapper(*args, **kwargs):
if not hasattr(module, '__path__'):
module.__package__ = module.__package__.rpartition('.')[0]
return module
wrap(wrapper, fxn)
return wrapper


Expand Down Expand Up @@ -213,9 +221,7 @@ def inner(self, name, *args, **kwargs):
if self._name != name:
raise ImportError("loader cannot handle %s" % name)
return method(self, name, *args, **kwargs)
inner.__name__ = method.__name__
inner.__doc__ = method.__doc__
inner.__dict__.update(method.__dict__)
wrap(inner, method)
return inner


Expand Down Expand Up @@ -318,6 +324,7 @@ def decorated(self, fullname):
elif hasattr(module, attr):
delattr(module, attr)
raise
wrap(decorated, fxn)
return decorated


Expand Down

0 comments on commit 51d8bfc

Please sign in to comment.