diff options
| author | Michele Simionato <michele.simionato@gmail.com> | 2019-03-16 10:49:56 +0100 |
|---|---|---|
| committer | Michele Simionato <michele.simionato@gmail.com> | 2019-03-16 10:49:56 +0100 |
| commit | 746a4bc06f8397f7bf7402b97e89dcba1c9edeec (patch) | |
| tree | d5b77b2357ff25ea1358fd0cb265d6641e4fca28 | |
| parent | 78034a4e9b643e3b64fc6af8848f8f2ea660fde9 (diff) | |
| download | python-decorator-git-746a4bc06f8397f7bf7402b97e89dcba1c9edeec.tar.gz | |
More work
| -rw-r--r-- | src/decorator.py | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/decorator.py b/src/decorator.py index f12de69..01a1751 100644 --- a/src/decorator.py +++ b/src/decorator.py @@ -278,6 +278,12 @@ class Decorator(object): return '<%s %s%r>' % (self.__class__.__name__, self.caller.__name__, self.args) + def kwargs(self): + nargs = self.caller.__code__.co_argcount + argvalues = self.caller.__defaults__ or () + argnames = self.caller.__code__.co_varnames[nargs-len(argvalues):nargs] + return dict(zip(argnames, argvalues)) + def decorator(caller, _func=None): """decorator(caller) converts a caller function into a decorator""" @@ -291,17 +297,13 @@ def decorator(caller, _func=None): doc = 'decorator(%s) converts functions/generators into ' \ 'factories of %s objects' % (caller.__name__, caller.__name__) elif inspect.isfunction(caller): - if caller.__name__ == '<lambda>': - name = '_lambda_' - else: - name = caller.__name__ + name = caller.__name__ doc = caller.__doc__ - nargs = caller.__code__.co_argcount - ndefs = len(caller.__defaults__ or ()) - defaultargs = ', '.join(caller.__code__.co_varnames[nargs-ndefs:nargs]) + kw = Decorator(caller).kwargs() + defaults = tuple(kw.values()) + defaultargs = ', '.join(kw.keys()) if defaultargs: defaultargs += ',' - defaults = caller.__defaults__ else: # assume caller is an object with a __call__ method name = caller.__class__.__name__.lower() doc = caller.__call__.__doc__ @@ -312,7 +314,7 @@ def decorator(caller, _func=None): 'return _decorate_(func, _call, (%s))' % (defaultargs, defaultargs), evaldict, doc=doc, module=caller.__module__, __wrapped__=caller) if defaults: - dec.__defaults__ = (None,) + defaults + dec.__defaults__ = (None,) + defaults or () dec.arg = partial(Decorator, caller) return dec |
