Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/compat/_inspect.py @ 87:2a2c65a20a8b
Add Python libs and headers
author | Chris Cannam |
---|---|
date | Wed, 25 Feb 2015 14:05:22 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
86:413a9d26189e | 87:2a2c65a20a8b |
---|---|
1 """Subset of inspect module from upstream python | |
2 | |
3 We use this instead of upstream because upstream inspect is slow to import, and | |
4 significanly contributes to numpy import times. Importing this copy has almost | |
5 no overhead. | |
6 | |
7 """ | |
8 from __future__ import division, absolute_import, print_function | |
9 | |
10 import types | |
11 | |
12 __all__ = ['getargspec', 'formatargspec'] | |
13 | |
14 # ----------------------------------------------------------- type-checking | |
15 def ismethod(object): | |
16 """Return true if the object is an instance method. | |
17 | |
18 Instance method objects provide these attributes: | |
19 __doc__ documentation string | |
20 __name__ name with which this method was defined | |
21 im_class class object in which this method belongs | |
22 im_func function object containing implementation of method | |
23 im_self instance to which this method is bound, or None""" | |
24 return isinstance(object, types.MethodType) | |
25 | |
26 def isfunction(object): | |
27 """Return true if the object is a user-defined function. | |
28 | |
29 Function objects provide these attributes: | |
30 __doc__ documentation string | |
31 __name__ name with which this function was defined | |
32 func_code code object containing compiled function bytecode | |
33 func_defaults tuple of any default values for arguments | |
34 func_doc (same as __doc__) | |
35 func_globals global namespace in which this function was defined | |
36 func_name (same as __name__)""" | |
37 return isinstance(object, types.FunctionType) | |
38 | |
39 def iscode(object): | |
40 """Return true if the object is a code object. | |
41 | |
42 Code objects provide these attributes: | |
43 co_argcount number of arguments (not including * or ** args) | |
44 co_code string of raw compiled bytecode | |
45 co_consts tuple of constants used in the bytecode | |
46 co_filename name of file in which this code object was created | |
47 co_firstlineno number of first line in Python source code | |
48 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg | |
49 co_lnotab encoded mapping of line numbers to bytecode indices | |
50 co_name name with which this code object was defined | |
51 co_names tuple of names of local variables | |
52 co_nlocals number of local variables | |
53 co_stacksize virtual machine stack space required | |
54 co_varnames tuple of names of arguments and local variables""" | |
55 return isinstance(object, types.CodeType) | |
56 | |
57 # ------------------------------------------------ argument list extraction | |
58 # These constants are from Python's compile.h. | |
59 CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS = 1, 2, 4, 8 | |
60 | |
61 def getargs(co): | |
62 """Get information about the arguments accepted by a code object. | |
63 | |
64 Three things are returned: (args, varargs, varkw), where 'args' is | |
65 a list of argument names (possibly containing nested lists), and | |
66 'varargs' and 'varkw' are the names of the * and ** arguments or None.""" | |
67 | |
68 if not iscode(co): | |
69 raise TypeError('arg is not a code object') | |
70 | |
71 code = co.co_code | |
72 nargs = co.co_argcount | |
73 names = co.co_varnames | |
74 args = list(names[:nargs]) | |
75 step = 0 | |
76 | |
77 # The following acrobatics are for anonymous (tuple) arguments. | |
78 for i in range(nargs): | |
79 if args[i][:1] in ['', '.']: | |
80 stack, remain, count = [], [], [] | |
81 while step < len(code): | |
82 op = ord(code[step]) | |
83 step = step + 1 | |
84 if op >= dis.HAVE_ARGUMENT: | |
85 opname = dis.opname[op] | |
86 value = ord(code[step]) + ord(code[step+1])*256 | |
87 step = step + 2 | |
88 if opname in ['UNPACK_TUPLE', 'UNPACK_SEQUENCE']: | |
89 remain.append(value) | |
90 count.append(value) | |
91 elif opname == 'STORE_FAST': | |
92 stack.append(names[value]) | |
93 | |
94 # Special case for sublists of length 1: def foo((bar)) | |
95 # doesn't generate the UNPACK_TUPLE bytecode, so if | |
96 # `remain` is empty here, we have such a sublist. | |
97 if not remain: | |
98 stack[0] = [stack[0]] | |
99 break | |
100 else: | |
101 remain[-1] = remain[-1] - 1 | |
102 while remain[-1] == 0: | |
103 remain.pop() | |
104 size = count.pop() | |
105 stack[-size:] = [stack[-size:]] | |
106 if not remain: break | |
107 remain[-1] = remain[-1] - 1 | |
108 if not remain: break | |
109 args[i] = stack[0] | |
110 | |
111 varargs = None | |
112 if co.co_flags & CO_VARARGS: | |
113 varargs = co.co_varnames[nargs] | |
114 nargs = nargs + 1 | |
115 varkw = None | |
116 if co.co_flags & CO_VARKEYWORDS: | |
117 varkw = co.co_varnames[nargs] | |
118 return args, varargs, varkw | |
119 | |
120 def getargspec(func): | |
121 """Get the names and default values of a function's arguments. | |
122 | |
123 A tuple of four things is returned: (args, varargs, varkw, defaults). | |
124 'args' is a list of the argument names (it may contain nested lists). | |
125 'varargs' and 'varkw' are the names of the * and ** arguments or None. | |
126 'defaults' is an n-tuple of the default values of the last n arguments. | |
127 """ | |
128 | |
129 if ismethod(func): | |
130 func = func.__func__ | |
131 if not isfunction(func): | |
132 raise TypeError('arg is not a Python function') | |
133 args, varargs, varkw = getargs(func.__code__) | |
134 return args, varargs, varkw, func.__defaults__ | |
135 | |
136 def getargvalues(frame): | |
137 """Get information about arguments passed into a particular frame. | |
138 | |
139 A tuple of four things is returned: (args, varargs, varkw, locals). | |
140 'args' is a list of the argument names (it may contain nested lists). | |
141 'varargs' and 'varkw' are the names of the * and ** arguments or None. | |
142 'locals' is the locals dictionary of the given frame.""" | |
143 args, varargs, varkw = getargs(frame.f_code) | |
144 return args, varargs, varkw, frame.f_locals | |
145 | |
146 def joinseq(seq): | |
147 if len(seq) == 1: | |
148 return '(' + seq[0] + ',)' | |
149 else: | |
150 return '(' + ', '.join(seq) + ')' | |
151 | |
152 def strseq(object, convert, join=joinseq): | |
153 """Recursively walk a sequence, stringifying each element.""" | |
154 if type(object) in [list, tuple]: | |
155 return join([strseq(_o, convert, join) for _o in object]) | |
156 else: | |
157 return convert(object) | |
158 | |
159 def formatargspec(args, varargs=None, varkw=None, defaults=None, | |
160 formatarg=str, | |
161 formatvarargs=lambda name: '*' + name, | |
162 formatvarkw=lambda name: '**' + name, | |
163 formatvalue=lambda value: '=' + repr(value), | |
164 join=joinseq): | |
165 """Format an argument spec from the 4 values returned by getargspec. | |
166 | |
167 The first four arguments are (args, varargs, varkw, defaults). The | |
168 other four arguments are the corresponding optional formatting functions | |
169 that are called to turn names and values into strings. The ninth | |
170 argument is an optional function to format the sequence of arguments.""" | |
171 specs = [] | |
172 if defaults: | |
173 firstdefault = len(args) - len(defaults) | |
174 for i in range(len(args)): | |
175 spec = strseq(args[i], formatarg, join) | |
176 if defaults and i >= firstdefault: | |
177 spec = spec + formatvalue(defaults[i - firstdefault]) | |
178 specs.append(spec) | |
179 if varargs is not None: | |
180 specs.append(formatvarargs(varargs)) | |
181 if varkw is not None: | |
182 specs.append(formatvarkw(varkw)) | |
183 return '(' + ', '.join(specs) + ')' | |
184 | |
185 def formatargvalues(args, varargs, varkw, locals, | |
186 formatarg=str, | |
187 formatvarargs=lambda name: '*' + name, | |
188 formatvarkw=lambda name: '**' + name, | |
189 formatvalue=lambda value: '=' + repr(value), | |
190 join=joinseq): | |
191 """Format an argument spec from the 4 values returned by getargvalues. | |
192 | |
193 The first four arguments are (args, varargs, varkw, locals). The | |
194 next four arguments are the corresponding optional formatting functions | |
195 that are called to turn names and values into strings. The ninth | |
196 argument is an optional function to format the sequence of arguments.""" | |
197 def convert(name, locals=locals, | |
198 formatarg=formatarg, formatvalue=formatvalue): | |
199 return formatarg(name) + formatvalue(locals[name]) | |
200 specs = [] | |
201 for i in range(len(args)): | |
202 specs.append(strseq(args[i], convert, join)) | |
203 if varargs: | |
204 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs])) | |
205 if varkw: | |
206 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw])) | |
207 return '(' + string.join(specs, ', ') + ')' | |
208 | |
209 if __name__ == '__main__': | |
210 import inspect | |
211 def foo(x, y, z=None): | |
212 return None | |
213 | |
214 print(inspect.getargs(foo.__code__)) | |
215 print(getargs(foo.__code__)) | |
216 | |
217 print(inspect.getargspec(foo)) | |
218 print(getargspec(foo)) | |
219 | |
220 print(inspect.formatargspec(*inspect.getargspec(foo))) | |
221 print(formatargspec(*getargspec(foo))) |