|
35 | 35 |
|
36 | 36 | import collections
|
37 | 37 | import inspect
|
38 |
| -from inspect import getfullargspec |
39 | 38 | import itertools
|
40 | 39 | import operator
|
41 | 40 | import re
|
42 | 41 | import sys
|
43 | 42 |
|
44 | 43 | __version__ = '4.0.10'
|
45 | 44 |
|
| 45 | +if sys.version_info >= (3,): |
| 46 | + from inspect import getfullargspec |
46 | 47 |
|
47 |
| -def get_init(cls): |
48 |
| - return cls.__init__ |
49 | 48 |
|
| 49 | + def get_init(cls): |
| 50 | + return cls.__init__ |
| 51 | +else: |
| 52 | + class getfullargspec(object): |
| 53 | + "A quick and dirty replacement for getfullargspec for Python 2.X" |
| 54 | + |
| 55 | + def __init__(self, f): |
| 56 | + self.args, self.varargs, self.varkw, self.defaults = \ |
| 57 | + inspect.getargspec(f) |
| 58 | + self.kwonlyargs = [] |
| 59 | + self.kwonlydefaults = None |
| 60 | + |
| 61 | + def __iter__(self): |
| 62 | + yield self.args |
| 63 | + yield self.varargs |
| 64 | + yield self.varkw |
| 65 | + yield self.defaults |
| 66 | + |
| 67 | + getargspec = inspect.getargspec |
| 68 | + |
| 69 | + |
| 70 | + def get_init(cls): |
| 71 | + return cls.__init__.__func__ |
50 | 72 |
|
51 | 73 | # getargspec has been deprecated in Python 3.5
|
52 | 74 | ArgSpec = collections.namedtuple(
|
@@ -91,21 +113,26 @@ def __init__(self, func=None, name=None, signature=None,
|
91 | 113 | setattr(self, a, getattr(argspec, a))
|
92 | 114 | for i, arg in enumerate(self.args):
|
93 | 115 | setattr(self, 'arg%d' % i, arg)
|
94 |
| - allargs = list(self.args) |
95 |
| - allshortargs = list(self.args) |
96 |
| - if self.varargs: |
97 |
| - allargs.append('*' + self.varargs) |
98 |
| - allshortargs.append('*' + self.varargs) |
99 |
| - elif self.kwonlyargs: |
100 |
| - allargs.append('*') # single star syntax |
101 |
| - for a in self.kwonlyargs: |
102 |
| - allargs.append('%s=None' % a) |
103 |
| - allshortargs.append('%s=%s' % (a, a)) |
104 |
| - if self.varkw: |
105 |
| - allargs.append('**' + self.varkw) |
106 |
| - allshortargs.append('**' + self.varkw) |
107 |
| - self.signature = ', '.join(allargs) |
108 |
| - self.shortsignature = ', '.join(allshortargs) |
| 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) |
109 | 136 | self.dict = func.__dict__.copy()
|
110 | 137 | # func=None happens when decorating a caller
|
111 | 138 | if name:
|
|
0 commit comments