(self, func=None, name=None, signature=None,
defaults=None, doc=None, module=None, funcdict=None)
| 96 | _compile_count = itertools.count() |
| 97 | |
| 98 | def __init__(self, func=None, name=None, signature=None, |
| 99 | defaults=None, doc=None, module=None, funcdict=None): |
| 100 | self.shortsignature = signature |
| 101 | if func: |
| 102 | # func can be a class or a callable, but not an instance method |
| 103 | self.name = func.__name__ |
| 104 | if self.name == '<lambda>': # small hack for lambda functions |
| 105 | self.name = '_lambda_' |
| 106 | self.doc = func.__doc__ |
| 107 | self.module = func.__module__ |
| 108 | if inspect.isfunction(func): |
| 109 | argspec = getfullargspec(func) |
| 110 | self.annotations = getattr(func, '__annotations__', {}) |
| 111 | for a in ('args', 'varargs', 'varkw', 'defaults', 'kwonlyargs', |
| 112 | 'kwonlydefaults'): |
| 113 | setattr(self, a, getattr(argspec, a)) |
| 114 | for i, arg in enumerate(self.args): |
| 115 | setattr(self, 'arg%d' % i, arg) |
| 116 | if sys.version_info < (3,): # easy way |
| 117 | self.shortsignature = self.signature = ( |
| 118 | inspect.formatargspec( |
| 119 | formatvalue=lambda val: "", *argspec)[1:-1]) |
| 120 | else: # Python 3 way |
| 121 | allargs = list(self.args) |
| 122 | allshortargs = list(self.args) |
| 123 | if self.varargs: |
| 124 | allargs.append('*' + self.varargs) |
| 125 | allshortargs.append('*' + self.varargs) |
| 126 | elif self.kwonlyargs: |
| 127 | allargs.append('*') # single star syntax |
| 128 | for a in self.kwonlyargs: |
| 129 | allargs.append('%s=None' % a) |
| 130 | allshortargs.append('%s=%s' % (a, a)) |
| 131 | if self.varkw: |
| 132 | allargs.append('**' + self.varkw) |
| 133 | allshortargs.append('**' + self.varkw) |
| 134 | self.signature = ', '.join(allargs) |
| 135 | self.shortsignature = ', '.join(allshortargs) |
| 136 | self.dict = func.__dict__.copy() |
| 137 | # func=None happens when decorating a caller |
| 138 | if name: |
| 139 | self.name = name |
| 140 | if signature is not None: |
| 141 | self.signature = signature |
| 142 | if defaults: |
| 143 | self.defaults = defaults |
| 144 | if doc: |
| 145 | self.doc = doc |
| 146 | if module: |
| 147 | self.module = module |
| 148 | if funcdict: |
| 149 | self.dict = funcdict |
| 150 | # check existence required attributes |
| 151 | assert hasattr(self, 'name') |
| 152 | if not hasattr(self, 'signature'): |
| 153 | raise TypeError('You are decorating a non function: %s' % func) |
| 154 | |
| 155 | def update(self, func, **kw): |
no test coverage detected