blob: 4e33a22e310b6a94d5ca64e6825cca07c09bf38b [file] [log] [blame]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001"""Get useful information from live Python objects.
2
3This module encapsulates the interface provided by the internal special
Neal Norwitz221085d2007-02-25 20:55:47 +00004attributes (co_*, im_*, tb_*, etc.) in a friendlier fashion.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00005It also provides some help for examining source code and class layout.
6
7Here are some of the useful functions provided by this module:
8
Christian Heimes7131fd92008-02-19 14:21:46 +00009 ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
10 isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(),
11 isroutine() - check object types
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000012 getmembers() - get members of an object that satisfy a given condition
13
14 getfile(), getsourcefile(), getsource() - find an object's source code
15 getdoc(), getcomments() - get documentation on an object
16 getmodule() - determine the module that an object came from
17 getclasstree() - arrange classes so as to represent their hierarchy
18
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +000019 getargspec(), getargvalues(), getcallargs() - get info about function arguments
Guido van Rossum2e65f892007-02-28 22:03:49 +000020 getfullargspec() - same, with support for Python-3000 features
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000021 formatargspec(), formatargvalues() - format an argument spec
22 getouterframes(), getinnerframes() - get info about frames
23 currentframe() - get the current stack frame
24 stack(), trace() - get info about frames on the stack or in a traceback
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070025
26 signature() - get a Signature object for the callable
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000027"""
28
29# This module is in the public domain. No warranties.
30
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070031__author__ = ('Ka-Ping Yee <ping@lfw.org>',
32 'Yury Selivanov <yselivanov@sprymix.com>')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000033
Larry Hastings44e2eaa2013-11-23 15:37:55 -080034import ast
Brett Cannoncb66eb02012-05-11 12:58:42 -040035import importlib.machinery
36import itertools
Christian Heimes7131fd92008-02-19 14:21:46 +000037import linecache
Brett Cannoncb66eb02012-05-11 12:58:42 -040038import os
39import re
40import sys
41import tokenize
42import types
Brett Cannon2b88fcf2012-06-02 22:28:42 -040043import warnings
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070044import functools
Nick Coghlan2f92e542012-06-23 19:39:55 +100045import builtins
Raymond Hettingera1a992c2005-03-11 06:46:45 +000046from operator import attrgetter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070047from collections import namedtuple, OrderedDict
Nick Coghlan09c81232010-08-17 10:18:16 +000048
49# Create constants for the compiler flags in Include/code.h
50# We try to get them from dis to avoid duplication, but fall
Ezio Melotti30b9d5d2013-08-17 15:50:46 +030051# back to hardcoding so the dependency is optional
Nick Coghlan09c81232010-08-17 10:18:16 +000052try:
53 from dis import COMPILER_FLAG_NAMES as _flag_names
Brett Cannoncd171c82013-07-04 17:43:24 -040054except ImportError:
Nick Coghlan09c81232010-08-17 10:18:16 +000055 CO_OPTIMIZED, CO_NEWLOCALS = 0x1, 0x2
56 CO_VARARGS, CO_VARKEYWORDS = 0x4, 0x8
57 CO_NESTED, CO_GENERATOR, CO_NOFREE = 0x10, 0x20, 0x40
58else:
59 mod_dict = globals()
60 for k, v in _flag_names.items():
61 mod_dict["CO_" + v] = k
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000062
Christian Heimesbe5b30b2008-03-03 19:18:51 +000063# See Include/object.h
64TPFLAGS_IS_ABSTRACT = 1 << 20
65
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000066# ----------------------------------------------------------- type-checking
67def ismodule(object):
68 """Return true if the object is a module.
69
70 Module objects provide these attributes:
Barry Warsaw28a691b2010-04-17 00:19:56 +000071 __cached__ pathname to byte compiled file
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000072 __doc__ documentation string
73 __file__ filename (missing for built-in modules)"""
Tim Peters28bc59f2001-09-16 08:40:16 +000074 return isinstance(object, types.ModuleType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000075
76def isclass(object):
77 """Return true if the object is a class.
78
79 Class objects provide these attributes:
80 __doc__ documentation string
81 __module__ name of module in which this class was defined"""
Benjamin Petersonc4656002009-01-17 22:41:18 +000082 return isinstance(object, type)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000083
84def ismethod(object):
85 """Return true if the object is an instance method.
86
87 Instance method objects provide these attributes:
88 __doc__ documentation string
89 __name__ name with which this method was defined
Christian Heimesff737952007-11-27 10:40:20 +000090 __func__ function object containing implementation of method
91 __self__ instance to which this method is bound"""
Tim Peters28bc59f2001-09-16 08:40:16 +000092 return isinstance(object, types.MethodType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000093
Tim Peters536d2262001-09-20 05:13:38 +000094def ismethoddescriptor(object):
Tim Petersf1d90b92001-09-20 05:47:55 +000095 """Return true if the object is a method descriptor.
96
97 But not if ismethod() or isclass() or isfunction() are true.
Tim Peters536d2262001-09-20 05:13:38 +000098
99 This is new in Python 2.2, and, for example, is true of int.__add__.
100 An object passing this test has a __get__ attribute but not a __set__
101 attribute, but beyond that the set of attributes varies. __name__ is
102 usually sensible, and __doc__ often is.
103
Tim Petersf1d90b92001-09-20 05:47:55 +0000104 Methods implemented via descriptors that also pass one of the other
105 tests return false from the ismethoddescriptor() test, simply because
106 the other tests promise more -- you can, e.g., count on having the
Christian Heimesff737952007-11-27 10:40:20 +0000107 __func__ attribute (etc) when an object passes ismethod()."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100108 if isclass(object) or ismethod(object) or isfunction(object):
109 # mutual exclusion
110 return False
111 tp = type(object)
112 return hasattr(tp, "__get__") and not hasattr(tp, "__set__")
Tim Peters536d2262001-09-20 05:13:38 +0000113
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000114def isdatadescriptor(object):
115 """Return true if the object is a data descriptor.
116
117 Data descriptors have both a __get__ and a __set__ attribute. Examples are
118 properties (defined in Python) and getsets and members (defined in C).
119 Typically, data descriptors will also have __name__ and __doc__ attributes
120 (properties, getsets, and members have both of these attributes), but this
121 is not guaranteed."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100122 if isclass(object) or ismethod(object) or isfunction(object):
123 # mutual exclusion
124 return False
125 tp = type(object)
126 return hasattr(tp, "__set__") and hasattr(tp, "__get__")
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000127
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000128if hasattr(types, 'MemberDescriptorType'):
129 # CPython and equivalent
130 def ismemberdescriptor(object):
131 """Return true if the object is a member descriptor.
132
133 Member descriptors are specialized descriptors defined in extension
134 modules."""
135 return isinstance(object, types.MemberDescriptorType)
136else:
137 # Other implementations
138 def ismemberdescriptor(object):
139 """Return true if the object is a member descriptor.
140
141 Member descriptors are specialized descriptors defined in extension
142 modules."""
143 return False
144
145if hasattr(types, 'GetSetDescriptorType'):
146 # CPython and equivalent
147 def isgetsetdescriptor(object):
148 """Return true if the object is a getset descriptor.
149
150 getset descriptors are specialized descriptors defined in extension
151 modules."""
152 return isinstance(object, types.GetSetDescriptorType)
153else:
154 # Other implementations
155 def isgetsetdescriptor(object):
156 """Return true if the object is a getset descriptor.
157
158 getset descriptors are specialized descriptors defined in extension
159 modules."""
160 return False
161
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000162def isfunction(object):
163 """Return true if the object is a user-defined function.
164
165 Function objects provide these attributes:
166 __doc__ documentation string
167 __name__ name with which this function was defined
Neal Norwitz221085d2007-02-25 20:55:47 +0000168 __code__ code object containing compiled function bytecode
169 __defaults__ tuple of any default values for arguments
170 __globals__ global namespace in which this function was defined
171 __annotations__ dict of parameter annotations
172 __kwdefaults__ dict of keyword only parameters with defaults"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000173 return isinstance(object, types.FunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000174
Christian Heimes7131fd92008-02-19 14:21:46 +0000175def isgeneratorfunction(object):
176 """Return true if the object is a user-defined generator function.
177
178 Generator function objects provides same attributes as functions.
179
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000180 See help(isfunction) for attributes listing."""
Georg Brandlb1441c72009-01-03 22:33:39 +0000181 return bool((isfunction(object) or ismethod(object)) and
182 object.__code__.co_flags & CO_GENERATOR)
Christian Heimes7131fd92008-02-19 14:21:46 +0000183
184def isgenerator(object):
185 """Return true if the object is a generator.
186
187 Generator objects provide these attributes:
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300188 __iter__ defined to support iteration over container
Christian Heimes7131fd92008-02-19 14:21:46 +0000189 close raises a new GeneratorExit exception inside the
190 generator to terminate the iteration
191 gi_code code object
192 gi_frame frame object or possibly None once the generator has
193 been exhausted
194 gi_running set to 1 when generator is executing, 0 otherwise
195 next return the next item from the container
196 send resumes the generator and "sends" a value that becomes
197 the result of the current yield-expression
198 throw used to raise an exception inside the generator"""
199 return isinstance(object, types.GeneratorType)
200
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000201def istraceback(object):
202 """Return true if the object is a traceback.
203
204 Traceback objects provide these attributes:
205 tb_frame frame object at this level
206 tb_lasti index of last attempted instruction in bytecode
207 tb_lineno current line number in Python source code
208 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000209 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000210
211def isframe(object):
212 """Return true if the object is a frame object.
213
214 Frame objects provide these attributes:
215 f_back next outer frame object (this frame's caller)
216 f_builtins built-in namespace seen by this frame
217 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000218 f_globals global namespace seen by this frame
219 f_lasti index of last attempted instruction in bytecode
220 f_lineno current line number in Python source code
221 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000222 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000223 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000224
225def iscode(object):
226 """Return true if the object is a code object.
227
228 Code objects provide these attributes:
229 co_argcount number of arguments (not including * or ** args)
230 co_code string of raw compiled bytecode
231 co_consts tuple of constants used in the bytecode
232 co_filename name of file in which this code object was created
233 co_firstlineno number of first line in Python source code
234 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
235 co_lnotab encoded mapping of line numbers to bytecode indices
236 co_name name with which this code object was defined
237 co_names tuple of names of local variables
238 co_nlocals number of local variables
239 co_stacksize virtual machine stack space required
240 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000241 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000242
243def isbuiltin(object):
244 """Return true if the object is a built-in function or method.
245
246 Built-in functions and methods provide these attributes:
247 __doc__ documentation string
248 __name__ original name of this function or method
249 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000250 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000251
252def isroutine(object):
253 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000254 return (isbuiltin(object)
255 or isfunction(object)
256 or ismethod(object)
257 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000258
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000259def isabstract(object):
260 """Return true if the object is an abstract base class (ABC)."""
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000261 return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT)
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000262
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000263def getmembers(object, predicate=None):
264 """Return all members of an object as (name, value) pairs sorted by name.
265 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100266 if isclass(object):
267 mro = (object,) + getmro(object)
268 else:
269 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000270 results = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700271 processed = set()
272 names = dir(object)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700273 # :dd any DynamicClassAttributes to the list of names if object is a class;
Ethan Furmane03ea372013-09-25 07:14:41 -0700274 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700275 # attribute with the same name as a DynamicClassAttribute exists
Ethan Furmane03ea372013-09-25 07:14:41 -0700276 try:
277 for base in object.__bases__:
278 for k, v in base.__dict__.items():
279 if isinstance(v, types.DynamicClassAttribute):
280 names.append(k)
281 except AttributeError:
282 pass
283 for key in names:
Ethan Furman63c141c2013-10-18 00:27:39 -0700284 # First try to get the value via getattr. Some descriptors don't
285 # like calling their __get__ (see bug #1785), so fall back to
286 # looking in the __dict__.
287 try:
288 value = getattr(object, key)
289 # handle the duplicate key
290 if key in processed:
291 raise AttributeError
292 except AttributeError:
293 for base in mro:
294 if key in base.__dict__:
295 value = base.__dict__[key]
296 break
297 else:
298 # could be a (currently) missing slot member, or a buggy
299 # __dir__; discard and move on
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100300 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000301 if not predicate or predicate(value):
302 results.append((key, value))
Ethan Furmane03ea372013-09-25 07:14:41 -0700303 processed.add(key)
304 results.sort(key=lambda pair: pair[0])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000305 return results
306
Christian Heimes25bb7832008-01-11 16:17:00 +0000307Attribute = namedtuple('Attribute', 'name kind defining_class object')
308
Tim Peters13b49d32001-09-23 02:00:29 +0000309def classify_class_attrs(cls):
310 """Return list of attribute-descriptor tuples.
311
312 For each name in dir(cls), the return list contains a 4-tuple
313 with these elements:
314
315 0. The name (a string).
316
317 1. The kind of attribute this is, one of these strings:
318 'class method' created via classmethod()
319 'static method' created via staticmethod()
320 'property' created via property()
Ethan Furmane03ea372013-09-25 07:14:41 -0700321 'method' any other flavor of method or descriptor
Tim Peters13b49d32001-09-23 02:00:29 +0000322 'data' not a method
323
324 2. The class which defined this attribute (a class).
325
Ethan Furmane03ea372013-09-25 07:14:41 -0700326 3. The object as obtained by calling getattr; if this fails, or if the
327 resulting object does not live anywhere in the class' mro (including
328 metaclasses) then the object is looked up in the defining class's
329 dict (found by walking the mro).
Ethan Furman668dede2013-09-14 18:53:26 -0700330
331 If one of the items in dir(cls) is stored in the metaclass it will now
332 be discovered and not have None be listed as the class in which it was
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700333 defined. Any items whose home class cannot be discovered are skipped.
Tim Peters13b49d32001-09-23 02:00:29 +0000334 """
335
336 mro = getmro(cls)
Ethan Furman668dede2013-09-14 18:53:26 -0700337 metamro = getmro(type(cls)) # for attributes stored in the metaclass
Ethan Furmane03ea372013-09-25 07:14:41 -0700338 metamro = tuple([cls for cls in metamro if cls not in (type, object)])
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700339 class_bases = (cls,) + mro
340 all_bases = class_bases + metamro
Tim Peters13b49d32001-09-23 02:00:29 +0000341 names = dir(cls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700342 # :dd any DynamicClassAttributes to the list of names;
Ethan Furmane03ea372013-09-25 07:14:41 -0700343 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700344 # attribute with the same name as a DynamicClassAttribute exists.
Ethan Furman63c141c2013-10-18 00:27:39 -0700345 for base in mro:
Ethan Furmane03ea372013-09-25 07:14:41 -0700346 for k, v in base.__dict__.items():
347 if isinstance(v, types.DynamicClassAttribute):
348 names.append(k)
Tim Peters13b49d32001-09-23 02:00:29 +0000349 result = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700350 processed = set()
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700351
Tim Peters13b49d32001-09-23 02:00:29 +0000352 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100353 # Get the object associated with the name, and where it was defined.
Ethan Furmane03ea372013-09-25 07:14:41 -0700354 # Normal objects will be looked up with both getattr and directly in
355 # its class' dict (in case getattr fails [bug #1785], and also to look
356 # for a docstring).
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700357 # For DynamicClassAttributes on the second pass we only look in the
Ethan Furmane03ea372013-09-25 07:14:41 -0700358 # class's dict.
359 #
Tim Peters13b49d32001-09-23 02:00:29 +0000360 # Getting an obj from the __dict__ sometimes reveals more than
361 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100362 homecls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700363 get_obj = None
364 dict_obj = None
Ethan Furmane03ea372013-09-25 07:14:41 -0700365 if name not in processed:
366 try:
Ethan Furmana8b07072013-10-18 01:22:08 -0700367 if name == '__dict__':
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700368 raise Exception("__dict__ is special, don't want the proxy")
Ethan Furmane03ea372013-09-25 07:14:41 -0700369 get_obj = getattr(cls, name)
370 except Exception as exc:
371 pass
372 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700373 homecls = getattr(get_obj, "__objclass__", homecls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700374 if homecls not in class_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700375 # if the resulting object does not live somewhere in the
Ethan Furman63c141c2013-10-18 00:27:39 -0700376 # mro, drop it and search the mro manually
Ethan Furmane03ea372013-09-25 07:14:41 -0700377 homecls = None
Ethan Furman63c141c2013-10-18 00:27:39 -0700378 last_cls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700379 # first look in the classes
380 for srch_cls in class_bases:
Ethan Furman63c141c2013-10-18 00:27:39 -0700381 srch_obj = getattr(srch_cls, name, None)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700382 if srch_obj == get_obj:
Ethan Furman63c141c2013-10-18 00:27:39 -0700383 last_cls = srch_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700384 # then check the metaclasses
385 for srch_cls in metamro:
386 try:
387 srch_obj = srch_cls.__getattr__(cls, name)
388 except AttributeError:
389 continue
390 if srch_obj == get_obj:
391 last_cls = srch_cls
Ethan Furman63c141c2013-10-18 00:27:39 -0700392 if last_cls is not None:
393 homecls = last_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700394 for base in all_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700395 if name in base.__dict__:
396 dict_obj = base.__dict__[name]
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700397 if homecls not in metamro:
398 homecls = base
Ethan Furmane03ea372013-09-25 07:14:41 -0700399 break
Ethan Furman63c141c2013-10-18 00:27:39 -0700400 if homecls is None:
401 # unable to locate the attribute anywhere, most likely due to
402 # buggy custom __dir__; discard and move on
403 continue
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700404 obj = get_obj or dict_obj
Ethan Furmane03ea372013-09-25 07:14:41 -0700405 # Classify the object or its descriptor.
Ethan Furman63c141c2013-10-18 00:27:39 -0700406 if isinstance(dict_obj, staticmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000407 kind = "static method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700408 obj = dict_obj
Ethan Furman63c141c2013-10-18 00:27:39 -0700409 elif isinstance(dict_obj, classmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000410 kind = "class method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700411 obj = dict_obj
412 elif isinstance(dict_obj, property):
Tim Peters13b49d32001-09-23 02:00:29 +0000413 kind = "property"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700414 obj = dict_obj
Yury Selivanov0860a0b2014-01-31 14:28:44 -0500415 elif isroutine(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000416 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100417 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700418 kind = "data"
Christian Heimes25bb7832008-01-11 16:17:00 +0000419 result.append(Attribute(name, kind, homecls, obj))
Ethan Furmane03ea372013-09-25 07:14:41 -0700420 processed.add(name)
Tim Peters13b49d32001-09-23 02:00:29 +0000421 return result
422
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000423# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000424
425def getmro(cls):
426 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000427 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000428
Nick Coghlane8c45d62013-07-28 20:00:01 +1000429# -------------------------------------------------------- function helpers
430
431def unwrap(func, *, stop=None):
432 """Get the object wrapped by *func*.
433
434 Follows the chain of :attr:`__wrapped__` attributes returning the last
435 object in the chain.
436
437 *stop* is an optional callback accepting an object in the wrapper chain
438 as its sole argument that allows the unwrapping to be terminated early if
439 the callback returns a true value. If the callback never returns a true
440 value, the last object in the chain is returned as usual. For example,
441 :func:`signature` uses this to stop unwrapping if any object in the
442 chain has a ``__signature__`` attribute defined.
443
444 :exc:`ValueError` is raised if a cycle is encountered.
445
446 """
447 if stop is None:
448 def _is_wrapper(f):
449 return hasattr(f, '__wrapped__')
450 else:
451 def _is_wrapper(f):
452 return hasattr(f, '__wrapped__') and not stop(f)
453 f = func # remember the original func for error reporting
454 memo = {id(f)} # Memoise by id to tolerate non-hashable objects
455 while _is_wrapper(func):
456 func = func.__wrapped__
457 id_func = id(func)
458 if id_func in memo:
459 raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
460 memo.add(id_func)
461 return func
462
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000463# -------------------------------------------------- source code extraction
464def indentsize(line):
465 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000466 expline = line.expandtabs()
467 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000468
469def getdoc(object):
470 """Get the documentation string for an object.
471
472 All tabs are expanded to spaces. To clean up docstrings that are
473 indented to line up with blocks of code, any whitespace than can be
474 uniformly removed from the second line onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000475 try:
476 doc = object.__doc__
477 except AttributeError:
478 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000479 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000480 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000481 return cleandoc(doc)
482
483def cleandoc(doc):
484 """Clean up indentation from docstrings.
485
486 Any whitespace that can be uniformly removed from the second line
487 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000488 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000489 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000490 except UnicodeError:
491 return None
492 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000493 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000494 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000495 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000496 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000497 if content:
498 indent = len(line) - content
499 margin = min(margin, indent)
500 # Remove indentation.
501 if lines:
502 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000503 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000504 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000505 # Remove any trailing or leading blank lines.
506 while lines and not lines[-1]:
507 lines.pop()
508 while lines and not lines[0]:
509 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000510 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000511
512def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000513 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000514 if ismodule(object):
515 if hasattr(object, '__file__'):
516 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000517 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000518 if isclass(object):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500519 if hasattr(object, '__module__'):
520 object = sys.modules.get(object.__module__)
521 if hasattr(object, '__file__'):
522 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000523 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000524 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000525 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000526 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000527 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000528 if istraceback(object):
529 object = object.tb_frame
530 if isframe(object):
531 object = object.f_code
532 if iscode(object):
533 return object.co_filename
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000534 raise TypeError('{!r} is not a module, class, method, '
535 'function, traceback, frame, or code object'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000536
Christian Heimes25bb7832008-01-11 16:17:00 +0000537ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')
538
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000539def getmoduleinfo(path):
540 """Get the module name, suffix, mode, and module type for a given file."""
Brett Cannoncb66eb02012-05-11 12:58:42 -0400541 warnings.warn('inspect.getmoduleinfo() is deprecated', DeprecationWarning,
542 2)
Brett Cannone4f41de2013-06-16 13:13:40 -0400543 with warnings.catch_warnings():
544 warnings.simplefilter('ignore', PendingDeprecationWarning)
545 import imp
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000546 filename = os.path.basename(path)
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000547 suffixes = [(-len(suffix), suffix, mode, mtype)
548 for suffix, mode, mtype in imp.get_suffixes()]
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000549 suffixes.sort() # try longest suffixes first, in case they overlap
550 for neglen, suffix, mode, mtype in suffixes:
551 if filename[neglen:] == suffix:
Christian Heimes25bb7832008-01-11 16:17:00 +0000552 return ModuleInfo(filename[:neglen], suffix, mode, mtype)
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000553
554def getmodulename(path):
555 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000556 fname = os.path.basename(path)
557 # Check for paths that look like an actual module file
558 suffixes = [(-len(suffix), suffix)
559 for suffix in importlib.machinery.all_suffixes()]
560 suffixes.sort() # try longest suffixes first, in case they overlap
561 for neglen, suffix in suffixes:
562 if fname.endswith(suffix):
563 return fname[:neglen]
564 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000565
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000566def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000567 """Return the filename that can be used to locate an object's source.
568 Return None if no way can be identified to get the source.
569 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000570 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400571 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
572 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
573 if any(filename.endswith(s) for s in all_bytecode_suffixes):
574 filename = (os.path.splitext(filename)[0] +
575 importlib.machinery.SOURCE_SUFFIXES[0])
576 elif any(filename.endswith(s) for s in
577 importlib.machinery.EXTENSION_SUFFIXES):
578 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000579 if os.path.exists(filename):
580 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000581 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400582 if getattr(getmodule(object, filename), '__loader__', None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000583 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000584 # or it is in the linecache
585 if filename in linecache.cache:
586 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000587
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000588def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000589 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000590
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000591 The idea is for each object to have a unique origin, so this routine
592 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000593 if _filename is None:
594 _filename = getsourcefile(object) or getfile(object)
595 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000596
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000597modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000598_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000599
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000600def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000601 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000602 if ismodule(object):
603 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000604 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000605 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000606 # Try the filename to modulename cache
607 if _filename is not None and _filename in modulesbyfile:
608 return sys.modules.get(modulesbyfile[_filename])
609 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000610 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000611 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000612 except TypeError:
613 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000614 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000615 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000616 # Update the filename to module name cache and check yet again
617 # Copy sys.modules in order to cope with changes while iterating
Éric Araujoa74f8ef2011-11-29 16:58:53 +0100618 for modname, module in list(sys.modules.items()):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000619 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000620 f = module.__file__
621 if f == _filesbymodname.get(modname, None):
622 # Have already mapped this module, so skip it
623 continue
624 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000625 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000626 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000627 modulesbyfile[f] = modulesbyfile[
628 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000629 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000630 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000631 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000632 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000633 if not hasattr(object, '__name__'):
634 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000635 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000636 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000637 if mainobject is object:
638 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000639 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000640 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000641 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000642 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000643 if builtinobject is object:
644 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000645
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000646def findsource(object):
647 """Return the entire source file and starting line number for an object.
648
649 The argument may be a module, class, method, function, traceback, frame,
650 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200651 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000652 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500653
654 file = getfile(object)
655 sourcefile = getsourcefile(object)
Ezio Melotti1b145922013-03-30 05:17:24 +0200656 if not sourcefile and file[:1] + file[-1:] != '<>':
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200657 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500658 file = sourcefile if sourcefile else file
659
Thomas Wouters89f507f2006-12-13 04:49:30 +0000660 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000661 if module:
662 lines = linecache.getlines(file, module.__dict__)
663 else:
664 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000665 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200666 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000667
668 if ismodule(object):
669 return lines, 0
670
671 if isclass(object):
672 name = object.__name__
Thomas Wouters89f507f2006-12-13 04:49:30 +0000673 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
674 # make some effort to find the best matching class definition:
675 # use the one with the least indentation, which is the one
676 # that's most probably not inside a function definition.
677 candidates = []
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000678 for i in range(len(lines)):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000679 match = pat.match(lines[i])
680 if match:
681 # if it's at toplevel, it's already the best one
682 if lines[i][0] == 'c':
683 return lines, i
684 # else add whitespace to candidate list
685 candidates.append((match.group(1), i))
686 if candidates:
687 # this will sort by whitespace, and by line number,
688 # less whitespace first
689 candidates.sort()
690 return lines, candidates[0][1]
Jeremy Hyltonab919022003-06-27 18:41:20 +0000691 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200692 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000693
694 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000695 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000696 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000697 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000698 if istraceback(object):
699 object = object.tb_frame
700 if isframe(object):
701 object = object.f_code
702 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000703 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200704 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000705 lnum = object.co_firstlineno - 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000706 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000707 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000708 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000709 lnum = lnum - 1
710 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200711 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000712
713def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000714 """Get lines of comments immediately preceding an object's source code.
715
716 Returns None when source can't be found.
717 """
718 try:
719 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200720 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000721 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000722
723 if ismodule(object):
724 # Look for a comment block at the top of the file.
725 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000726 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000727 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000728 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000729 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000730 comments = []
731 end = start
732 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000733 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000734 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000735 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000736
737 # Look for a preceding block of comments at the same indentation.
738 elif lnum > 0:
739 indent = indentsize(lines[lnum])
740 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000741 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000742 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000743 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000744 if end > 0:
745 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000746 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000747 while comment[:1] == '#' and indentsize(lines[end]) == indent:
748 comments[:0] = [comment]
749 end = end - 1
750 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000751 comment = lines[end].expandtabs().lstrip()
752 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000753 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000754 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000755 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000756 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000757
Tim Peters4efb6e92001-06-29 23:51:08 +0000758class EndOfBlock(Exception): pass
759
760class BlockFinder:
761 """Provide a tokeneater() method to detect the end of a code block."""
762 def __init__(self):
763 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000764 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000765 self.started = False
766 self.passline = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000767 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000768
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000769 def tokeneater(self, type, token, srowcol, erowcol, line):
Tim Peters4efb6e92001-06-29 23:51:08 +0000770 if not self.started:
Armin Rigodd5c0232005-09-25 11:45:45 +0000771 # look for the first "def", "class" or "lambda"
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000772 if token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000773 if token == "lambda":
774 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000775 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000776 self.passline = True # skip to the end of the line
Tim Peters4efb6e92001-06-29 23:51:08 +0000777 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000778 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000779 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000780 if self.islambda: # lambdas always end at the first NEWLINE
781 raise EndOfBlock
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000782 elif self.passline:
783 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000784 elif type == tokenize.INDENT:
785 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000786 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000787 elif type == tokenize.DEDENT:
788 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000789 # the end of matching indent/dedent pairs end a block
790 # (note that this only works for "def"/"class" blocks,
791 # not e.g. for "if: else:" or "try: finally:" blocks)
792 if self.indent <= 0:
793 raise EndOfBlock
794 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
795 # any other token on the same indentation level end the previous
796 # block as well, except the pseudo-tokens COMMENT and NL.
797 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000798
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000799def getblock(lines):
800 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000801 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000802 try:
Trent Nelson428de652008-03-18 22:41:35 +0000803 tokens = tokenize.generate_tokens(iter(lines).__next__)
804 for _token in tokens:
805 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000806 except (EndOfBlock, IndentationError):
807 pass
808 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000809
810def getsourcelines(object):
811 """Return a list of source lines and starting line number for an object.
812
813 The argument may be a module, class, method, function, traceback, frame,
814 or code object. The source code is returned as a list of the lines
815 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200816 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000817 raised if the source code cannot be retrieved."""
818 lines, lnum = findsource(object)
819
820 if ismodule(object): return lines, 0
821 else: return getblock(lines[lnum:]), lnum + 1
822
823def getsource(object):
824 """Return the text of the source code for an object.
825
826 The argument may be a module, class, method, function, traceback, frame,
827 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200828 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000829 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000830 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000831
832# --------------------------------------------------- class tree extraction
833def walktree(classes, children, parent):
834 """Recursive helper function for getclasstree()."""
835 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +0000836 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000837 for c in classes:
838 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +0000839 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000840 results.append(walktree(children[c], children, c))
841 return results
842
Georg Brandl5ce83a02009-06-01 17:23:51 +0000843def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000844 """Arrange the given list of classes into a hierarchy of nested lists.
845
846 Where a nested list appears, it contains classes derived from the class
847 whose entry immediately precedes the list. Each entry is a 2-tuple
848 containing a class and a tuple of its base classes. If the 'unique'
849 argument is true, exactly one entry appears in the returned structure
850 for each class in the given list. Otherwise, classes using multiple
851 inheritance and their descendants will appear multiple times."""
852 children = {}
853 roots = []
854 for c in classes:
855 if c.__bases__:
856 for parent in c.__bases__:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000857 if not parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000858 children[parent] = []
Serhiy Storchaka362c1b52013-09-05 17:14:32 +0300859 if c not in children[parent]:
860 children[parent].append(c)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000861 if unique and parent in classes: break
862 elif c not in roots:
863 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +0000864 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000865 if parent not in classes:
866 roots.append(parent)
867 return walktree(roots, children, None)
868
869# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +0000870Arguments = namedtuple('Arguments', 'args, varargs, varkw')
871
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000872def getargs(co):
873 """Get information about the arguments accepted by a code object.
874
Guido van Rossum2e65f892007-02-28 22:03:49 +0000875 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000876 'args' is the list of argument names. Keyword-only arguments are
877 appended. 'varargs' and 'varkw' are the names of the * and **
878 arguments or None."""
Guido van Rossum2e65f892007-02-28 22:03:49 +0000879 args, varargs, kwonlyargs, varkw = _getfullargs(co)
Christian Heimes25bb7832008-01-11 16:17:00 +0000880 return Arguments(args + kwonlyargs, varargs, varkw)
Guido van Rossum2e65f892007-02-28 22:03:49 +0000881
882def _getfullargs(co):
883 """Get information about the arguments accepted by a code object.
884
885 Four things are returned: (args, varargs, kwonlyargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000886 'args' and 'kwonlyargs' are lists of argument names, and 'varargs'
887 and 'varkw' are the names of the * and ** arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +0000888
889 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000890 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000891
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000892 nargs = co.co_argcount
893 names = co.co_varnames
Guido van Rossum2e65f892007-02-28 22:03:49 +0000894 nkwargs = co.co_kwonlyargcount
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000895 args = list(names[:nargs])
Guido van Rossum2e65f892007-02-28 22:03:49 +0000896 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000897 step = 0
898
Guido van Rossum2e65f892007-02-28 22:03:49 +0000899 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000900 varargs = None
901 if co.co_flags & CO_VARARGS:
902 varargs = co.co_varnames[nargs]
903 nargs = nargs + 1
904 varkw = None
905 if co.co_flags & CO_VARKEYWORDS:
906 varkw = co.co_varnames[nargs]
Guido van Rossum2e65f892007-02-28 22:03:49 +0000907 return args, varargs, kwonlyargs, varkw
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000908
Christian Heimes25bb7832008-01-11 16:17:00 +0000909
910ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
911
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000912def getargspec(func):
913 """Get the names and default values of a function's arguments.
914
915 A tuple of four things is returned: (args, varargs, varkw, defaults).
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000916 'args' is a list of the argument names.
Guido van Rossum2e65f892007-02-28 22:03:49 +0000917 'args' will include keyword-only argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000918 'varargs' and 'varkw' are the names of the * and ** arguments or None.
Jeremy Hylton64967882003-06-27 18:14:39 +0000919 'defaults' is an n-tuple of the default values of the last n arguments.
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000920
Guido van Rossum2e65f892007-02-28 22:03:49 +0000921 Use the getfullargspec() API for Python-3000 code, as annotations
922 and keyword arguments are supported. getargspec() will raise ValueError
923 if the func has either annotations or keyword arguments.
924 """
925
926 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
927 getfullargspec(func)
928 if kwonlyargs or ann:
Collin Winterce36ad82007-08-30 01:19:48 +0000929 raise ValueError("Function has keyword-only arguments or annotations"
930 ", use getfullargspec() API which can support them")
Christian Heimes25bb7832008-01-11 16:17:00 +0000931 return ArgSpec(args, varargs, varkw, defaults)
932
933FullArgSpec = namedtuple('FullArgSpec',
Benjamin Peterson3d4ca742008-11-12 21:39:01 +0000934 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +0000935
936def getfullargspec(func):
Yury Selivanovd82eddc2014-01-29 11:24:39 -0500937 """Get the names and default values of a callable object's arguments.
Guido van Rossum2e65f892007-02-28 22:03:49 +0000938
Brett Cannon504d8852007-09-07 02:12:14 +0000939 A tuple of seven things is returned:
940 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults annotations).
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000941 'args' is a list of the argument names.
Guido van Rossum2e65f892007-02-28 22:03:49 +0000942 'varargs' and 'varkw' are the names of the * and ** arguments or None.
943 'defaults' is an n-tuple of the default values of the last n arguments.
944 'kwonlyargs' is a list of keyword-only argument names.
945 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
946 'annotations' is a dictionary mapping argument names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000947
Guido van Rossum2e65f892007-02-28 22:03:49 +0000948 The first four items in the tuple correspond to getargspec().
Jeremy Hylton64967882003-06-27 18:14:39 +0000949 """
950
Yury Selivanovd82eddc2014-01-29 11:24:39 -0500951 builtin_method_param = None
952
Jeremy Hylton64967882003-06-27 18:14:39 +0000953 if ismethod(func):
Yury Selivanovd82eddc2014-01-29 11:24:39 -0500954 # There is a notable difference in behaviour between getfullargspec
955 # and Signature: the former always returns 'self' parameter for bound
956 # methods, whereas the Signature always shows the actual calling
957 # signature of the passed object.
958 #
959 # To simulate this behaviour, we "unbind" bound methods, to trick
960 # inspect.signature to always return their first parameter ("self",
961 # usually)
Christian Heimesff737952007-11-27 10:40:20 +0000962 func = func.__func__
Yury Selivanovd82eddc2014-01-29 11:24:39 -0500963
964 elif isbuiltin(func):
965 # We have a builtin function or method. For that, we check the
966 # special '__text_signature__' attribute, provided by the
967 # Argument Clinic. If it's a method, we'll need to make sure
968 # that its first parameter (usually "self") is always returned
969 # (see the previous comment).
970 text_signature = getattr(func, '__text_signature__', None)
971 if text_signature and text_signature.startswith('($'):
972 builtin_method_param = _signature_get_bound_param(text_signature)
973
974 try:
975 sig = signature(func)
976 except Exception as ex:
977 # Most of the times 'signature' will raise ValueError.
978 # But, it can also raise AttributeError, and, maybe something
979 # else. So to be fully backwards compatible, we catch all
980 # possible exceptions here, and reraise a TypeError.
981 raise TypeError('unsupported callable') from ex
982
983 args = []
984 varargs = None
985 varkw = None
986 kwonlyargs = []
987 defaults = ()
988 annotations = {}
989 defaults = ()
990 kwdefaults = {}
991
992 if sig.return_annotation is not sig.empty:
993 annotations['return'] = sig.return_annotation
994
995 for param in sig.parameters.values():
996 kind = param.kind
997 name = param.name
998
999 if kind is _POSITIONAL_ONLY:
1000 args.append(name)
1001 elif kind is _POSITIONAL_OR_KEYWORD:
1002 args.append(name)
1003 if param.default is not param.empty:
1004 defaults += (param.default,)
1005 elif kind is _VAR_POSITIONAL:
1006 varargs = name
1007 elif kind is _KEYWORD_ONLY:
1008 kwonlyargs.append(name)
1009 if param.default is not param.empty:
1010 kwdefaults[name] = param.default
1011 elif kind is _VAR_KEYWORD:
1012 varkw = name
1013
1014 if param.annotation is not param.empty:
1015 annotations[name] = param.annotation
1016
1017 if not kwdefaults:
1018 # compatibility with 'func.__kwdefaults__'
1019 kwdefaults = None
1020
1021 if not defaults:
1022 # compatibility with 'func.__defaults__'
1023 defaults = None
1024
1025 if builtin_method_param and (not args or args[0] != builtin_method_param):
1026 # `func` is a method, and we always need to return its
1027 # first parameter -- usually "self" (to be backwards
1028 # compatible with the previous implementation of
1029 # getfullargspec)
1030 args.insert(0, builtin_method_param)
1031
1032 return FullArgSpec(args, varargs, varkw, defaults,
1033 kwonlyargs, kwdefaults, annotations)
1034
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001035
Christian Heimes25bb7832008-01-11 16:17:00 +00001036ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
1037
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001038def getargvalues(frame):
1039 """Get information about arguments passed into a particular frame.
1040
1041 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001042 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001043 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1044 'locals' is the locals dictionary of the given frame."""
1045 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +00001046 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001047
Guido van Rossum2e65f892007-02-28 22:03:49 +00001048def formatannotation(annotation, base_module=None):
1049 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001050 if annotation.__module__ in ('builtins', base_module):
Guido van Rossum2e65f892007-02-28 22:03:49 +00001051 return annotation.__name__
1052 return annotation.__module__+'.'+annotation.__name__
1053 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001054
Guido van Rossum2e65f892007-02-28 22:03:49 +00001055def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001056 module = getattr(object, '__module__', None)
1057 def _formatannotation(annotation):
1058 return formatannotation(annotation, module)
1059 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001060
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001061def formatargspec(args, varargs=None, varkw=None, defaults=None,
Guido van Rossum2e65f892007-02-28 22:03:49 +00001062 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001063 formatarg=str,
1064 formatvarargs=lambda name: '*' + name,
1065 formatvarkw=lambda name: '**' + name,
1066 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001067 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001068 formatannotation=formatannotation):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001069 """Format an argument spec from the values returned by getargspec
Guido van Rossum2e65f892007-02-28 22:03:49 +00001070 or getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001071
Guido van Rossum2e65f892007-02-28 22:03:49 +00001072 The first seven arguments are (args, varargs, varkw, defaults,
1073 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1074 are the corresponding optional formatting functions that are called to
1075 turn names and values into strings. The last argument is an optional
1076 function to format the sequence of arguments."""
1077 def formatargandannotation(arg):
1078 result = formatarg(arg)
1079 if arg in annotations:
1080 result += ': ' + formatannotation(annotations[arg])
1081 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001082 specs = []
1083 if defaults:
1084 firstdefault = len(args) - len(defaults)
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001085 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001086 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001087 if defaults and i >= firstdefault:
1088 spec = spec + formatvalue(defaults[i - firstdefault])
1089 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001090 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001091 specs.append(formatvarargs(formatargandannotation(varargs)))
1092 else:
1093 if kwonlyargs:
1094 specs.append('*')
1095 if kwonlyargs:
1096 for kwonlyarg in kwonlyargs:
1097 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001098 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001099 spec += formatvalue(kwonlydefaults[kwonlyarg])
1100 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001101 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001102 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001103 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001104 if 'return' in annotations:
1105 result += formatreturns(formatannotation(annotations['return']))
1106 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001107
1108def formatargvalues(args, varargs, varkw, locals,
1109 formatarg=str,
1110 formatvarargs=lambda name: '*' + name,
1111 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001112 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001113 """Format an argument spec from the 4 values returned by getargvalues.
1114
1115 The first four arguments are (args, varargs, varkw, locals). The
1116 next four arguments are the corresponding optional formatting functions
1117 that are called to turn names and values into strings. The ninth
1118 argument is an optional function to format the sequence of arguments."""
1119 def convert(name, locals=locals,
1120 formatarg=formatarg, formatvalue=formatvalue):
1121 return formatarg(name) + formatvalue(locals[name])
1122 specs = []
1123 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001124 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001125 if varargs:
1126 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1127 if varkw:
1128 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001129 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001130
Benjamin Petersone109c702011-06-24 09:37:26 -05001131def _missing_arguments(f_name, argnames, pos, values):
1132 names = [repr(name) for name in argnames if name not in values]
1133 missing = len(names)
1134 if missing == 1:
1135 s = names[0]
1136 elif missing == 2:
1137 s = "{} and {}".format(*names)
1138 else:
1139 tail = ", {} and {}".format(names[-2:])
1140 del names[-2:]
1141 s = ", ".join(names) + tail
1142 raise TypeError("%s() missing %i required %s argument%s: %s" %
1143 (f_name, missing,
1144 "positional" if pos else "keyword-only",
1145 "" if missing == 1 else "s", s))
1146
1147def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001148 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001149 kwonly_given = len([arg for arg in kwonly if arg in values])
1150 if varargs:
1151 plural = atleast != 1
1152 sig = "at least %d" % (atleast,)
1153 elif defcount:
1154 plural = True
1155 sig = "from %d to %d" % (atleast, len(args))
1156 else:
1157 plural = len(args) != 1
1158 sig = str(len(args))
1159 kwonly_sig = ""
1160 if kwonly_given:
1161 msg = " positional argument%s (and %d keyword-only argument%s)"
1162 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1163 "s" if kwonly_given != 1 else ""))
1164 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1165 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1166 "was" if given == 1 and not kwonly_given else "were"))
1167
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001168def getcallargs(*func_and_positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001169 """Get the mapping of arguments to values.
1170
1171 A dict is returned, with keys the function argument names (including the
1172 names of the * and ** arguments, if any), and values the respective bound
1173 values from 'positional' and 'named'."""
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001174 func = func_and_positional[0]
1175 positional = func_and_positional[1:]
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001176 spec = getfullargspec(func)
1177 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
1178 f_name = func.__name__
1179 arg2value = {}
1180
Benjamin Petersonb204a422011-06-05 22:04:07 -05001181
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001182 if ismethod(func) and func.__self__ is not None:
1183 # implicit 'self' (or 'cls' for classmethods) argument
1184 positional = (func.__self__,) + positional
1185 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001186 num_args = len(args)
1187 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001188
Benjamin Petersonb204a422011-06-05 22:04:07 -05001189 n = min(num_pos, num_args)
1190 for i in range(n):
1191 arg2value[args[i]] = positional[i]
1192 if varargs:
1193 arg2value[varargs] = tuple(positional[n:])
1194 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001195 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001196 arg2value[varkw] = {}
1197 for kw, value in named.items():
1198 if kw not in possible_kwargs:
1199 if not varkw:
1200 raise TypeError("%s() got an unexpected keyword argument %r" %
1201 (f_name, kw))
1202 arg2value[varkw][kw] = value
1203 continue
1204 if kw in arg2value:
1205 raise TypeError("%s() got multiple values for argument %r" %
1206 (f_name, kw))
1207 arg2value[kw] = value
1208 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001209 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1210 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001211 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001212 req = args[:num_args - num_defaults]
1213 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001214 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001215 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001216 for i, arg in enumerate(args[num_args - num_defaults:]):
1217 if arg not in arg2value:
1218 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001219 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001220 for kwarg in kwonlyargs:
1221 if kwarg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001222 if kwarg in kwonlydefaults:
1223 arg2value[kwarg] = kwonlydefaults[kwarg]
1224 else:
1225 missing += 1
1226 if missing:
1227 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001228 return arg2value
1229
Nick Coghlan2f92e542012-06-23 19:39:55 +10001230ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1231
1232def getclosurevars(func):
1233 """
1234 Get the mapping of free variables to their current values.
1235
Meador Inge8fda3592012-07-19 21:33:21 -05001236 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001237 and builtin references as seen by the body of the function. A final
1238 set of unbound names that could not be resolved is also provided.
1239 """
1240
1241 if ismethod(func):
1242 func = func.__func__
1243
1244 if not isfunction(func):
1245 raise TypeError("'{!r}' is not a Python function".format(func))
1246
1247 code = func.__code__
1248 # Nonlocal references are named in co_freevars and resolved
1249 # by looking them up in __closure__ by positional index
1250 if func.__closure__ is None:
1251 nonlocal_vars = {}
1252 else:
1253 nonlocal_vars = {
1254 var : cell.cell_contents
1255 for var, cell in zip(code.co_freevars, func.__closure__)
1256 }
1257
1258 # Global and builtin references are named in co_names and resolved
1259 # by looking them up in __globals__ or __builtins__
1260 global_ns = func.__globals__
1261 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1262 if ismodule(builtin_ns):
1263 builtin_ns = builtin_ns.__dict__
1264 global_vars = {}
1265 builtin_vars = {}
1266 unbound_names = set()
1267 for name in code.co_names:
1268 if name in ("None", "True", "False"):
1269 # Because these used to be builtins instead of keywords, they
1270 # may still show up as name references. We ignore them.
1271 continue
1272 try:
1273 global_vars[name] = global_ns[name]
1274 except KeyError:
1275 try:
1276 builtin_vars[name] = builtin_ns[name]
1277 except KeyError:
1278 unbound_names.add(name)
1279
1280 return ClosureVars(nonlocal_vars, global_vars,
1281 builtin_vars, unbound_names)
1282
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001283# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001284
1285Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1286
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001287def getframeinfo(frame, context=1):
1288 """Get information about a frame or traceback object.
1289
1290 A tuple of five things is returned: the filename, the line number of
1291 the current line, the function name, a list of lines of context from
1292 the source code, and the index of the current line within that list.
1293 The optional second argument specifies the number of lines of context
1294 to return, which are centered around the current line."""
1295 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001296 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001297 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001298 else:
1299 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001300 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001301 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001302
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001303 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001304 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001305 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001306 try:
1307 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001308 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001309 lines = index = None
1310 else:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001311 start = max(start, 1)
Raymond Hettingera0501712004-06-15 11:22:53 +00001312 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001313 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001314 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001315 else:
1316 lines = index = None
1317
Christian Heimes25bb7832008-01-11 16:17:00 +00001318 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001319
1320def getlineno(frame):
1321 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001322 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1323 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001324
1325def getouterframes(frame, context=1):
1326 """Get a list of records for a frame and all higher (calling) frames.
1327
1328 Each record contains a frame object, filename, line number, function
1329 name, a list of lines of context, and index within the context."""
1330 framelist = []
1331 while frame:
1332 framelist.append((frame,) + getframeinfo(frame, context))
1333 frame = frame.f_back
1334 return framelist
1335
1336def getinnerframes(tb, context=1):
1337 """Get a list of records for a traceback's frame and all lower frames.
1338
1339 Each record contains a frame object, filename, line number, function
1340 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001341 framelist = []
1342 while tb:
1343 framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
1344 tb = tb.tb_next
1345 return framelist
1346
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001347def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001348 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001349 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001350
1351def stack(context=1):
1352 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001353 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001354
1355def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001356 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001357 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001358
1359
1360# ------------------------------------------------ static version of getattr
1361
1362_sentinel = object()
1363
Michael Foorde5162652010-11-20 16:40:44 +00001364def _static_getmro(klass):
1365 return type.__dict__['__mro__'].__get__(klass)
1366
Michael Foord95fc51d2010-11-20 15:07:30 +00001367def _check_instance(obj, attr):
1368 instance_dict = {}
1369 try:
1370 instance_dict = object.__getattribute__(obj, "__dict__")
1371 except AttributeError:
1372 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001373 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001374
1375
1376def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001377 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001378 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001379 try:
1380 return entry.__dict__[attr]
1381 except KeyError:
1382 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001383 return _sentinel
1384
Michael Foord35184ed2010-11-20 16:58:30 +00001385def _is_type(obj):
1386 try:
1387 _static_getmro(obj)
1388 except TypeError:
1389 return False
1390 return True
1391
Michael Foorddcebe0f2011-03-15 19:20:44 -04001392def _shadowed_dict(klass):
1393 dict_attr = type.__dict__["__dict__"]
1394 for entry in _static_getmro(klass):
1395 try:
1396 class_dict = dict_attr.__get__(entry)["__dict__"]
1397 except KeyError:
1398 pass
1399 else:
1400 if not (type(class_dict) is types.GetSetDescriptorType and
1401 class_dict.__name__ == "__dict__" and
1402 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001403 return class_dict
1404 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001405
1406def getattr_static(obj, attr, default=_sentinel):
1407 """Retrieve attributes without triggering dynamic lookup via the
1408 descriptor protocol, __getattr__ or __getattribute__.
1409
1410 Note: this function may not be able to retrieve all attributes
1411 that getattr can fetch (like dynamically created attributes)
1412 and may find attributes that getattr can't (like descriptors
1413 that raise AttributeError). It can also return descriptor objects
1414 instead of instance members in some cases. See the
1415 documentation for details.
1416 """
1417 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001418 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001419 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001420 dict_attr = _shadowed_dict(klass)
1421 if (dict_attr is _sentinel or
1422 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001423 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001424 else:
1425 klass = obj
1426
1427 klass_result = _check_class(klass, attr)
1428
1429 if instance_result is not _sentinel and klass_result is not _sentinel:
1430 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1431 _check_class(type(klass_result), '__set__') is not _sentinel):
1432 return klass_result
1433
1434 if instance_result is not _sentinel:
1435 return instance_result
1436 if klass_result is not _sentinel:
1437 return klass_result
1438
1439 if obj is klass:
1440 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001441 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001442 if _shadowed_dict(type(entry)) is _sentinel:
1443 try:
1444 return entry.__dict__[attr]
1445 except KeyError:
1446 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001447 if default is not _sentinel:
1448 return default
1449 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001450
1451
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001452# ------------------------------------------------ generator introspection
1453
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001454GEN_CREATED = 'GEN_CREATED'
1455GEN_RUNNING = 'GEN_RUNNING'
1456GEN_SUSPENDED = 'GEN_SUSPENDED'
1457GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001458
1459def getgeneratorstate(generator):
1460 """Get current state of a generator-iterator.
1461
1462 Possible states are:
1463 GEN_CREATED: Waiting to start execution.
1464 GEN_RUNNING: Currently being executed by the interpreter.
1465 GEN_SUSPENDED: Currently suspended at a yield expression.
1466 GEN_CLOSED: Execution has completed.
1467 """
1468 if generator.gi_running:
1469 return GEN_RUNNING
1470 if generator.gi_frame is None:
1471 return GEN_CLOSED
1472 if generator.gi_frame.f_lasti == -1:
1473 return GEN_CREATED
1474 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001475
1476
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001477def getgeneratorlocals(generator):
1478 """
1479 Get the mapping of generator local variables to their current values.
1480
1481 A dict is returned, with the keys the local variable names and values the
1482 bound values."""
1483
1484 if not isgenerator(generator):
1485 raise TypeError("'{!r}' is not a Python generator".format(generator))
1486
1487 frame = getattr(generator, "gi_frame", None)
1488 if frame is not None:
1489 return generator.gi_frame.f_locals
1490 else:
1491 return {}
1492
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001493###############################################################################
1494### Function Signature Object (PEP 362)
1495###############################################################################
1496
1497
1498_WrapperDescriptor = type(type.__call__)
1499_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001500_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001501
1502_NonUserDefinedCallables = (_WrapperDescriptor,
1503 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001504 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001505 types.BuiltinFunctionType)
1506
1507
Yury Selivanov421f0c72014-01-29 12:05:40 -05001508def _signature_get_user_defined_method(cls, method_name):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001509 try:
1510 meth = getattr(cls, method_name)
1511 except AttributeError:
1512 return
1513 else:
1514 if not isinstance(meth, _NonUserDefinedCallables):
1515 # Once '__signature__' will be added to 'C'-level
1516 # callables, this check won't be necessary
1517 return meth
1518
1519
Yury Selivanov62560fb2014-01-28 12:26:24 -05001520def _signature_get_partial(wrapped_sig, partial, extra_args=()):
1521 # Internal helper to calculate how 'wrapped_sig' signature will
1522 # look like after applying a 'functools.partial' object (or alike)
1523 # on it.
1524
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001525 new_params = OrderedDict(wrapped_sig.parameters.items())
1526
1527 partial_args = partial.args or ()
1528 partial_keywords = partial.keywords or {}
1529
1530 if extra_args:
1531 partial_args = extra_args + partial_args
1532
1533 try:
1534 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1535 except TypeError as ex:
1536 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1537 raise ValueError(msg) from ex
1538
1539 for arg_name, arg_value in ba.arguments.items():
1540 param = new_params[arg_name]
1541 if arg_name in partial_keywords:
1542 # We set a new default value, because the following code
1543 # is correct:
1544 #
1545 # >>> def foo(a): print(a)
1546 # >>> print(partial(partial(foo, a=10), a=20)())
1547 # 20
1548 # >>> print(partial(partial(foo, a=10), a=20)(a=30))
1549 # 30
1550 #
1551 # So, with 'partial' objects, passing a keyword argument is
1552 # like setting a new default value for the corresponding
1553 # parameter
1554 #
1555 # We also mark this parameter with '_partial_kwarg'
1556 # flag. Later, in '_bind', the 'default' value of this
1557 # parameter will be added to 'kwargs', to simulate
1558 # the 'functools.partial' real call.
1559 new_params[arg_name] = param.replace(default=arg_value,
1560 _partial_kwarg=True)
1561
1562 elif (param.kind not in (_VAR_KEYWORD, _VAR_POSITIONAL) and
1563 not param._partial_kwarg):
1564 new_params.pop(arg_name)
1565
1566 return wrapped_sig.replace(parameters=new_params.values())
1567
1568
Yury Selivanov62560fb2014-01-28 12:26:24 -05001569def _signature_bound_method(sig):
1570 # Internal helper to transform signatures for unbound
1571 # functions to bound methods
1572
1573 params = tuple(sig.parameters.values())
1574
1575 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1576 raise ValueError('invalid method signature')
1577
1578 kind = params[0].kind
1579 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1580 # Drop first parameter:
1581 # '(p1, p2[, ...])' -> '(p2[, ...])'
1582 params = params[1:]
1583 else:
1584 if kind is not _VAR_POSITIONAL:
1585 # Unless we add a new parameter type we never
1586 # get here
1587 raise ValueError('invalid argument type')
1588 # It's a var-positional parameter.
1589 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1590
1591 return sig.replace(parameters=params)
1592
1593
Yury Selivanovb77511d2014-01-29 10:46:14 -05001594def _signature_is_builtin(obj):
1595 # Internal helper to test if `obj` is a callable that might
1596 # support Argument Clinic's __text_signature__ protocol.
Yury Selivanov1d241832014-02-02 12:51:20 -05001597 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001598 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001599 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001600 # Can't test 'isinstance(type)' here, as it would
1601 # also be True for regular python classes
1602 obj in (type, object))
1603
1604
Yury Selivanov63da7c72014-01-31 14:48:37 -05001605def _signature_is_functionlike(obj):
1606 # Internal helper to test if `obj` is a duck type of FunctionType.
1607 # A good example of such objects are functions compiled with
1608 # Cython, which have all attributes that a pure Python function
1609 # would have, but have their code statically compiled.
1610
1611 if not callable(obj) or isclass(obj):
1612 # All function-like objects are obviously callables,
1613 # and not classes.
1614 return False
1615
1616 name = getattr(obj, '__name__', None)
1617 code = getattr(obj, '__code__', None)
1618 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
1619 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
1620 annotations = getattr(obj, '__annotations__', None)
1621
1622 return (isinstance(code, types.CodeType) and
1623 isinstance(name, str) and
1624 (defaults is None or isinstance(defaults, tuple)) and
1625 (kwdefaults is None or isinstance(kwdefaults, dict)) and
1626 isinstance(annotations, dict))
1627
1628
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001629def _signature_get_bound_param(spec):
1630 # Internal helper to get first parameter name from a
1631 # __text_signature__ of a builtin method, which should
1632 # be in the following format: '($param1, ...)'.
1633 # Assumptions are that the first argument won't have
1634 # a default value or an annotation.
1635
1636 assert spec.startswith('($')
1637
1638 pos = spec.find(',')
1639 if pos == -1:
1640 pos = spec.find(')')
1641
1642 cpos = spec.find(':')
1643 assert cpos == -1 or cpos > pos
1644
1645 cpos = spec.find('=')
1646 assert cpos == -1 or cpos > pos
1647
1648 return spec[2:pos]
1649
1650
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001651def signature(obj):
1652 '''Get a signature object for the passed callable.'''
1653
1654 if not callable(obj):
1655 raise TypeError('{!r} is not a callable object'.format(obj))
1656
1657 if isinstance(obj, types.MethodType):
1658 # In this case we skip the first parameter of the underlying
1659 # function (usually `self` or `cls`).
1660 sig = signature(obj.__func__)
Yury Selivanov62560fb2014-01-28 12:26:24 -05001661 return _signature_bound_method(sig)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001662
Nick Coghlane8c45d62013-07-28 20:00:01 +10001663 # Was this function wrapped by a decorator?
1664 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
1665
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001666 try:
1667 sig = obj.__signature__
1668 except AttributeError:
1669 pass
1670 else:
1671 if sig is not None:
1672 return sig
1673
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001674 try:
1675 partialmethod = obj._partialmethod
1676 except AttributeError:
1677 pass
1678 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05001679 if isinstance(partialmethod, functools.partialmethod):
1680 # Unbound partialmethod (see functools.partialmethod)
1681 # This means, that we need to calculate the signature
1682 # as if it's a regular partial object, but taking into
1683 # account that the first positional argument
1684 # (usually `self`, or `cls`) will not be passed
1685 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001686
Yury Selivanov0486f812014-01-29 12:18:59 -05001687 wrapped_sig = signature(partialmethod.func)
1688 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001689
Yury Selivanov0486f812014-01-29 12:18:59 -05001690 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
1691 new_params = (first_wrapped_param,) + tuple(sig.parameters.values())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001692
Yury Selivanov0486f812014-01-29 12:18:59 -05001693 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001694
Yury Selivanov76c6c592014-01-29 10:52:57 -05001695 if _signature_is_builtin(obj):
1696 return Signature.from_builtin(obj)
1697
Yury Selivanov63da7c72014-01-31 14:48:37 -05001698 if isfunction(obj) or _signature_is_functionlike(obj):
1699 # If it's a pure Python function, or an object that is duck type
1700 # of a Python function (Cython functions, for instance), then:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001701 return Signature.from_function(obj)
1702
1703 if isinstance(obj, functools.partial):
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001704 wrapped_sig = signature(obj.func)
Yury Selivanov62560fb2014-01-28 12:26:24 -05001705 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001706
1707 sig = None
1708 if isinstance(obj, type):
1709 # obj is a class or a metaclass
1710
1711 # First, let's see if it has an overloaded __call__ defined
1712 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05001713 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001714 if call is not None:
1715 sig = signature(call)
1716 else:
1717 # Now we check if the 'obj' class has a '__new__' method
Yury Selivanov421f0c72014-01-29 12:05:40 -05001718 new = _signature_get_user_defined_method(obj, '__new__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001719 if new is not None:
1720 sig = signature(new)
1721 else:
1722 # Finally, we should have at least __init__ implemented
Yury Selivanov421f0c72014-01-29 12:05:40 -05001723 init = _signature_get_user_defined_method(obj, '__init__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001724 if init is not None:
1725 sig = signature(init)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05001726
1727 if sig is None:
1728 if type in obj.__mro__:
1729 # 'obj' is a metaclass without user-defined __init__
1730 # or __new__. Return a signature of 'type' builtin.
1731 return signature(type)
1732 else:
1733 # We have a class (not metaclass), but no user-defined
1734 # __init__ or __new__ for it
1735 return signature(object)
1736
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001737 elif not isinstance(obj, _NonUserDefinedCallables):
1738 # An object with __call__
1739 # We also check that the 'obj' is not an instance of
1740 # _WrapperDescriptor or _MethodWrapper to avoid
1741 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05001742 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001743 if call is not None:
1744 sig = signature(call)
1745
1746 if sig is not None:
1747 # For classes and objects we skip the first parameter of their
1748 # __call__, __new__, or __init__ methods
Yury Selivanov62560fb2014-01-28 12:26:24 -05001749 return _signature_bound_method(sig)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001750
1751 if isinstance(obj, types.BuiltinFunctionType):
1752 # Raise a nicer error message for builtins
1753 msg = 'no signature found for builtin function {!r}'.format(obj)
1754 raise ValueError(msg)
1755
1756 raise ValueError('callable {!r} is not supported by signature'.format(obj))
1757
1758
1759class _void:
1760 '''A private marker - used in Parameter & Signature'''
1761
1762
1763class _empty:
1764 pass
1765
1766
1767class _ParameterKind(int):
1768 def __new__(self, *args, name):
1769 obj = int.__new__(self, *args)
1770 obj._name = name
1771 return obj
1772
1773 def __str__(self):
1774 return self._name
1775
1776 def __repr__(self):
1777 return '<_ParameterKind: {!r}>'.format(self._name)
1778
1779
1780_POSITIONAL_ONLY = _ParameterKind(0, name='POSITIONAL_ONLY')
1781_POSITIONAL_OR_KEYWORD = _ParameterKind(1, name='POSITIONAL_OR_KEYWORD')
1782_VAR_POSITIONAL = _ParameterKind(2, name='VAR_POSITIONAL')
1783_KEYWORD_ONLY = _ParameterKind(3, name='KEYWORD_ONLY')
1784_VAR_KEYWORD = _ParameterKind(4, name='VAR_KEYWORD')
1785
1786
1787class Parameter:
1788 '''Represents a parameter in a function signature.
1789
1790 Has the following public attributes:
1791
1792 * name : str
1793 The name of the parameter as a string.
1794 * default : object
1795 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05001796 parameter has no default value, this attribute is set to
1797 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001798 * annotation
1799 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05001800 parameter has no annotation, this attribute is set to
1801 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001802 * kind : str
1803 Describes how argument values are bound to the parameter.
1804 Possible values: `Parameter.POSITIONAL_ONLY`,
1805 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
1806 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
1807 '''
1808
1809 __slots__ = ('_name', '_kind', '_default', '_annotation', '_partial_kwarg')
1810
1811 POSITIONAL_ONLY = _POSITIONAL_ONLY
1812 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
1813 VAR_POSITIONAL = _VAR_POSITIONAL
1814 KEYWORD_ONLY = _KEYWORD_ONLY
1815 VAR_KEYWORD = _VAR_KEYWORD
1816
1817 empty = _empty
1818
1819 def __init__(self, name, kind, *, default=_empty, annotation=_empty,
1820 _partial_kwarg=False):
1821
1822 if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
1823 _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
1824 raise ValueError("invalid value for 'Parameter.kind' attribute")
1825 self._kind = kind
1826
1827 if default is not _empty:
1828 if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
1829 msg = '{} parameters cannot have default values'.format(kind)
1830 raise ValueError(msg)
1831 self._default = default
1832 self._annotation = annotation
1833
Yury Selivanov2393dca2014-01-27 15:07:58 -05001834 if name is _empty:
1835 raise ValueError('name is a required attribute for Parameter')
1836
1837 if not isinstance(name, str):
1838 raise TypeError("name must be a str, not a {!r}".format(name))
1839
1840 if not name.isidentifier():
1841 raise ValueError('{!r} is not a valid parameter name'.format(name))
1842
1843 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001844
1845 self._partial_kwarg = _partial_kwarg
1846
1847 @property
1848 def name(self):
1849 return self._name
1850
1851 @property
1852 def default(self):
1853 return self._default
1854
1855 @property
1856 def annotation(self):
1857 return self._annotation
1858
1859 @property
1860 def kind(self):
1861 return self._kind
1862
1863 def replace(self, *, name=_void, kind=_void, annotation=_void,
1864 default=_void, _partial_kwarg=_void):
1865 '''Creates a customized copy of the Parameter.'''
1866
1867 if name is _void:
1868 name = self._name
1869
1870 if kind is _void:
1871 kind = self._kind
1872
1873 if annotation is _void:
1874 annotation = self._annotation
1875
1876 if default is _void:
1877 default = self._default
1878
1879 if _partial_kwarg is _void:
1880 _partial_kwarg = self._partial_kwarg
1881
1882 return type(self)(name, kind, default=default, annotation=annotation,
1883 _partial_kwarg=_partial_kwarg)
1884
1885 def __str__(self):
1886 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001887 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001888
1889 # Add annotation and default value
1890 if self._annotation is not _empty:
1891 formatted = '{}:{}'.format(formatted,
1892 formatannotation(self._annotation))
1893
1894 if self._default is not _empty:
1895 formatted = '{}={}'.format(formatted, repr(self._default))
1896
1897 if kind == _VAR_POSITIONAL:
1898 formatted = '*' + formatted
1899 elif kind == _VAR_KEYWORD:
1900 formatted = '**' + formatted
1901
1902 return formatted
1903
1904 def __repr__(self):
1905 return '<{} at {:#x} {!r}>'.format(self.__class__.__name__,
1906 id(self), self.name)
1907
1908 def __eq__(self, other):
Yury Selivanov0ba5f0d2014-01-31 15:30:30 -05001909 # NB: We deliberately do not compare '_partial_kwarg' attributes
1910 # here. Imagine we have a following situation:
1911 #
1912 # def foo(a, b=1): pass
1913 # def bar(a, b): pass
1914 # bar2 = functools.partial(bar, b=1)
1915 #
1916 # For the above scenario, signatures for `foo` and `bar2` should
1917 # be equal. '_partial_kwarg' attribute is an internal flag, to
1918 # distinguish between keyword parameters with defaults and
1919 # keyword parameters which got their defaults from functools.partial
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001920 return (issubclass(other.__class__, Parameter) and
1921 self._name == other._name and
1922 self._kind == other._kind and
1923 self._default == other._default and
1924 self._annotation == other._annotation)
1925
1926 def __ne__(self, other):
1927 return not self.__eq__(other)
1928
1929
1930class BoundArguments:
1931 '''Result of `Signature.bind` call. Holds the mapping of arguments
1932 to the function's parameters.
1933
1934 Has the following public attributes:
1935
1936 * arguments : OrderedDict
1937 An ordered mutable mapping of parameters' names to arguments' values.
1938 Does not contain arguments' default values.
1939 * signature : Signature
1940 The Signature object that created this instance.
1941 * args : tuple
1942 Tuple of positional arguments values.
1943 * kwargs : dict
1944 Dict of keyword arguments values.
1945 '''
1946
1947 def __init__(self, signature, arguments):
1948 self.arguments = arguments
1949 self._signature = signature
1950
1951 @property
1952 def signature(self):
1953 return self._signature
1954
1955 @property
1956 def args(self):
1957 args = []
1958 for param_name, param in self._signature.parameters.items():
1959 if (param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY) or
1960 param._partial_kwarg):
1961 # Keyword arguments mapped by 'functools.partial'
1962 # (Parameter._partial_kwarg is True) are mapped
1963 # in 'BoundArguments.kwargs', along with VAR_KEYWORD &
1964 # KEYWORD_ONLY
1965 break
1966
1967 try:
1968 arg = self.arguments[param_name]
1969 except KeyError:
1970 # We're done here. Other arguments
1971 # will be mapped in 'BoundArguments.kwargs'
1972 break
1973 else:
1974 if param.kind == _VAR_POSITIONAL:
1975 # *args
1976 args.extend(arg)
1977 else:
1978 # plain argument
1979 args.append(arg)
1980
1981 return tuple(args)
1982
1983 @property
1984 def kwargs(self):
1985 kwargs = {}
1986 kwargs_started = False
1987 for param_name, param in self._signature.parameters.items():
1988 if not kwargs_started:
1989 if (param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY) or
1990 param._partial_kwarg):
1991 kwargs_started = True
1992 else:
1993 if param_name not in self.arguments:
1994 kwargs_started = True
1995 continue
1996
1997 if not kwargs_started:
1998 continue
1999
2000 try:
2001 arg = self.arguments[param_name]
2002 except KeyError:
2003 pass
2004 else:
2005 if param.kind == _VAR_KEYWORD:
2006 # **kwargs
2007 kwargs.update(arg)
2008 else:
2009 # plain keyword argument
2010 kwargs[param_name] = arg
2011
2012 return kwargs
2013
2014 def __eq__(self, other):
2015 return (issubclass(other.__class__, BoundArguments) and
2016 self.signature == other.signature and
2017 self.arguments == other.arguments)
2018
2019 def __ne__(self, other):
2020 return not self.__eq__(other)
2021
2022
2023class Signature:
2024 '''A Signature object represents the overall signature of a function.
2025 It stores a Parameter object for each parameter accepted by the
2026 function, as well as information specific to the function itself.
2027
2028 A Signature object has the following public attributes and methods:
2029
2030 * parameters : OrderedDict
2031 An ordered mapping of parameters' names to the corresponding
2032 Parameter objects (keyword-only arguments are in the same order
2033 as listed in `code.co_varnames`).
2034 * return_annotation : object
2035 The annotation for the return type of the function if specified.
2036 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002037 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002038 * bind(*args, **kwargs) -> BoundArguments
2039 Creates a mapping from positional and keyword arguments to
2040 parameters.
2041 * bind_partial(*args, **kwargs) -> BoundArguments
2042 Creates a partial mapping from positional and keyword arguments
2043 to parameters (simulating 'functools.partial' behavior.)
2044 '''
2045
2046 __slots__ = ('_return_annotation', '_parameters')
2047
2048 _parameter_cls = Parameter
2049 _bound_arguments_cls = BoundArguments
2050
2051 empty = _empty
2052
2053 def __init__(self, parameters=None, *, return_annotation=_empty,
2054 __validate_parameters__=True):
2055 '''Constructs Signature from the given list of Parameter
2056 objects and 'return_annotation'. All arguments are optional.
2057 '''
2058
2059 if parameters is None:
2060 params = OrderedDict()
2061 else:
2062 if __validate_parameters__:
2063 params = OrderedDict()
2064 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002065 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002066
2067 for idx, param in enumerate(parameters):
2068 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002069 name = param.name
2070
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002071 if kind < top_kind:
2072 msg = 'wrong parameter order: {} before {}'
Yury Selivanov2393dca2014-01-27 15:07:58 -05002073 msg = msg.format(top_kind, kind)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002074 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002075 elif kind > top_kind:
2076 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002077 top_kind = kind
2078
Yury Selivanov07a9e452014-01-29 10:58:16 -05002079 if (kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD) and
2080 not param._partial_kwarg):
2081 # If we have a positional-only or positional-or-keyword
2082 # parameter, that does not have its default value set
2083 # by 'functools.partial' or other "partial" signature:
2084 if param.default is _empty:
2085 if kind_defaults:
2086 # No default for this parameter, but the
2087 # previous parameter of the same kind had
2088 # a default
2089 msg = 'non-default argument follows default ' \
2090 'argument'
2091 raise ValueError(msg)
2092 else:
2093 # There is a default for this parameter.
2094 kind_defaults = True
2095
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002096 if name in params:
2097 msg = 'duplicate parameter name: {!r}'.format(name)
2098 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002099
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002100 params[name] = param
2101 else:
2102 params = OrderedDict(((param.name, param)
2103 for param in parameters))
2104
2105 self._parameters = types.MappingProxyType(params)
2106 self._return_annotation = return_annotation
2107
2108 @classmethod
2109 def from_function(cls, func):
2110 '''Constructs Signature for the given python function'''
2111
Yury Selivanov5334bcd2014-01-31 15:21:51 -05002112 is_duck_function = False
2113 if not isfunction(func):
2114 if _signature_is_functionlike(func):
2115 is_duck_function = True
2116 else:
2117 # If it's not a pure Python function, and not a duck type
2118 # of pure function:
2119 raise TypeError('{!r} is not a Python function'.format(func))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002120
2121 Parameter = cls._parameter_cls
2122
2123 # Parameter information.
2124 func_code = func.__code__
2125 pos_count = func_code.co_argcount
2126 arg_names = func_code.co_varnames
2127 positional = tuple(arg_names[:pos_count])
2128 keyword_only_count = func_code.co_kwonlyargcount
2129 keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)]
2130 annotations = func.__annotations__
2131 defaults = func.__defaults__
2132 kwdefaults = func.__kwdefaults__
2133
2134 if defaults:
2135 pos_default_count = len(defaults)
2136 else:
2137 pos_default_count = 0
2138
2139 parameters = []
2140
2141 # Non-keyword-only parameters w/o defaults.
2142 non_default_count = pos_count - pos_default_count
2143 for name in positional[:non_default_count]:
2144 annotation = annotations.get(name, _empty)
2145 parameters.append(Parameter(name, annotation=annotation,
2146 kind=_POSITIONAL_OR_KEYWORD))
2147
2148 # ... w/ defaults.
2149 for offset, name in enumerate(positional[non_default_count:]):
2150 annotation = annotations.get(name, _empty)
2151 parameters.append(Parameter(name, annotation=annotation,
2152 kind=_POSITIONAL_OR_KEYWORD,
2153 default=defaults[offset]))
2154
2155 # *args
Yury Selivanov89ca85c2014-01-29 16:50:40 -05002156 if func_code.co_flags & CO_VARARGS:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002157 name = arg_names[pos_count + keyword_only_count]
2158 annotation = annotations.get(name, _empty)
2159 parameters.append(Parameter(name, annotation=annotation,
2160 kind=_VAR_POSITIONAL))
2161
2162 # Keyword-only parameters.
2163 for name in keyword_only:
2164 default = _empty
2165 if kwdefaults is not None:
2166 default = kwdefaults.get(name, _empty)
2167
2168 annotation = annotations.get(name, _empty)
2169 parameters.append(Parameter(name, annotation=annotation,
2170 kind=_KEYWORD_ONLY,
2171 default=default))
2172 # **kwargs
Yury Selivanov89ca85c2014-01-29 16:50:40 -05002173 if func_code.co_flags & CO_VARKEYWORDS:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002174 index = pos_count + keyword_only_count
Yury Selivanov89ca85c2014-01-29 16:50:40 -05002175 if func_code.co_flags & CO_VARARGS:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002176 index += 1
2177
2178 name = arg_names[index]
2179 annotation = annotations.get(name, _empty)
2180 parameters.append(Parameter(name, annotation=annotation,
2181 kind=_VAR_KEYWORD))
2182
Yury Selivanov5334bcd2014-01-31 15:21:51 -05002183 # Is 'func' is a pure Python function - don't validate the
2184 # parameters list (for correct order and defaults), it should be OK.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002185 return cls(parameters,
2186 return_annotation=annotations.get('return', _empty),
Yury Selivanov5334bcd2014-01-31 15:21:51 -05002187 __validate_parameters__=is_duck_function)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002188
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002189 @classmethod
2190 def from_builtin(cls, func):
Yury Selivanovb77511d2014-01-29 10:46:14 -05002191 if not _signature_is_builtin(func):
2192 raise TypeError("{!r} is not a Python builtin "
2193 "function".format(func))
2194
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002195 s = getattr(func, "__text_signature__", None)
2196 if not s:
Yury Selivanovb77511d2014-01-29 10:46:14 -05002197 raise ValueError("no signature found for builtin {!r}".format(func))
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002198
Larry Hastings2a727912014-01-16 11:32:01 -08002199 Parameter = cls._parameter_cls
2200
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002201 if s.endswith("/)"):
2202 kind = Parameter.POSITIONAL_ONLY
2203 s = s[:-2] + ')'
2204 else:
2205 kind = Parameter.POSITIONAL_OR_KEYWORD
2206
Larry Hastings581ee362014-01-28 05:00:08 -08002207 first_parameter_is_self = s.startswith("($")
2208 if first_parameter_is_self:
2209 s = '(' + s[2:]
2210
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002211 s = "def foo" + s + ": pass"
2212
2213 try:
2214 module = ast.parse(s)
2215 except SyntaxError:
Yury Selivanovb77511d2014-01-29 10:46:14 -05002216 module = None
2217
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002218 if not isinstance(module, ast.Module):
Yury Selivanovb77511d2014-01-29 10:46:14 -05002219 raise ValueError("{!r} builtin has invalid signature".format(func))
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002220
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002221 f = module.body[0]
2222
2223 parameters = []
2224 empty = Parameter.empty
Larry Hastings16c51912014-01-07 11:53:01 -08002225 invalid = object()
2226
Larry Hastings2a727912014-01-16 11:32:01 -08002227 module = None
2228 module_dict = {}
2229 module_name = getattr(func, '__module__', None)
2230 if module_name:
2231 module = sys.modules.get(module_name, None)
2232 if module:
2233 module_dict = module.__dict__
2234 sys_module_dict = sys.modules
Larry Hastings16c51912014-01-07 11:53:01 -08002235
Larry Hastings2a727912014-01-16 11:32:01 -08002236 def parse_name(node):
2237 assert isinstance(node, ast.arg)
2238 if node.annotation != None:
2239 raise ValueError("Annotations are not currently supported")
2240 return node.arg
Larry Hastings16c51912014-01-07 11:53:01 -08002241
Larry Hastings2a727912014-01-16 11:32:01 -08002242 def wrap_value(s):
2243 try:
2244 value = eval(s, module_dict)
2245 except NameError:
2246 try:
2247 value = eval(s, sys_module_dict)
2248 except NameError:
2249 raise RuntimeError()
Larry Hastings16c51912014-01-07 11:53:01 -08002250
Larry Hastings2a727912014-01-16 11:32:01 -08002251 if isinstance(value, str):
2252 return ast.Str(value)
2253 if isinstance(value, (int, float)):
2254 return ast.Num(value)
2255 if isinstance(value, bytes):
2256 return ast.Bytes(value)
2257 if value in (True, False, None):
2258 return ast.NameConstant(value)
2259 raise RuntimeError()
Larry Hastings16c51912014-01-07 11:53:01 -08002260
Larry Hastings2a727912014-01-16 11:32:01 -08002261 class RewriteSymbolics(ast.NodeTransformer):
2262 def visit_Attribute(self, node):
2263 a = []
2264 n = node
2265 while isinstance(n, ast.Attribute):
2266 a.append(n.attr)
2267 n = n.value
2268 if not isinstance(n, ast.Name):
2269 raise RuntimeError()
2270 a.append(n.id)
2271 value = ".".join(reversed(a))
2272 return wrap_value(value)
2273
2274 def visit_Name(self, node):
Larry Hastings16c51912014-01-07 11:53:01 -08002275 if not isinstance(node.ctx, ast.Load):
Larry Hastings2a727912014-01-16 11:32:01 -08002276 raise ValueError()
2277 return wrap_value(node.id)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002278
2279 def p(name_node, default_node, default=empty):
Larry Hastings2a727912014-01-16 11:32:01 -08002280 name = parse_name(name_node)
Larry Hastings16c51912014-01-07 11:53:01 -08002281 if name is invalid:
2282 return None
Larry Hastings5c661892014-01-24 06:17:25 -08002283 if default_node and default_node is not _empty:
Larry Hastings2a727912014-01-16 11:32:01 -08002284 try:
2285 default_node = RewriteSymbolics().visit(default_node)
2286 o = ast.literal_eval(default_node)
2287 except ValueError:
2288 o = invalid
Larry Hastings16c51912014-01-07 11:53:01 -08002289 if o is invalid:
2290 return None
2291 default = o if o is not invalid else default
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002292 parameters.append(Parameter(name, kind, default=default, annotation=empty))
2293
2294 # non-keyword-only parameters
Larry Hastings16c51912014-01-07 11:53:01 -08002295 args = reversed(f.args.args)
2296 defaults = reversed(f.args.defaults)
2297 iter = itertools.zip_longest(args, defaults, fillvalue=None)
2298 for name, default in reversed(list(iter)):
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002299 p(name, default)
2300
2301 # *args
2302 if f.args.vararg:
2303 kind = Parameter.VAR_POSITIONAL
2304 p(f.args.vararg, empty)
2305
2306 # keyword-only arguments
2307 kind = Parameter.KEYWORD_ONLY
2308 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
2309 p(name, default)
2310
2311 # **kwargs
2312 if f.args.kwarg:
2313 kind = Parameter.VAR_KEYWORD
2314 p(f.args.kwarg, empty)
2315
Larry Hastings581ee362014-01-28 05:00:08 -08002316 if first_parameter_is_self:
2317 assert parameters
2318 if getattr(func, '__self__', None):
2319 # strip off self, it's already been bound
2320 parameters.pop(0)
Larry Hastings5c661892014-01-24 06:17:25 -08002321 else:
2322 # for builtins, self parameter is always positional-only!
2323 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
2324 parameters[0] = p
2325
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002326 return cls(parameters, return_annotation=cls.empty)
2327
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002328 @property
2329 def parameters(self):
2330 return self._parameters
2331
2332 @property
2333 def return_annotation(self):
2334 return self._return_annotation
2335
2336 def replace(self, *, parameters=_void, return_annotation=_void):
2337 '''Creates a customized copy of the Signature.
2338 Pass 'parameters' and/or 'return_annotation' arguments
2339 to override them in the new copy.
2340 '''
2341
2342 if parameters is _void:
2343 parameters = self.parameters.values()
2344
2345 if return_annotation is _void:
2346 return_annotation = self._return_annotation
2347
2348 return type(self)(parameters,
2349 return_annotation=return_annotation)
2350
2351 def __eq__(self, other):
2352 if (not issubclass(type(other), Signature) or
2353 self.return_annotation != other.return_annotation or
2354 len(self.parameters) != len(other.parameters)):
2355 return False
2356
2357 other_positions = {param: idx
2358 for idx, param in enumerate(other.parameters.keys())}
2359
2360 for idx, (param_name, param) in enumerate(self.parameters.items()):
2361 if param.kind == _KEYWORD_ONLY:
2362 try:
2363 other_param = other.parameters[param_name]
2364 except KeyError:
2365 return False
2366 else:
2367 if param != other_param:
2368 return False
2369 else:
2370 try:
2371 other_idx = other_positions[param_name]
2372 except KeyError:
2373 return False
2374 else:
2375 if (idx != other_idx or
2376 param != other.parameters[param_name]):
2377 return False
2378
2379 return True
2380
2381 def __ne__(self, other):
2382 return not self.__eq__(other)
2383
2384 def _bind(self, args, kwargs, *, partial=False):
2385 '''Private method. Don't use directly.'''
2386
2387 arguments = OrderedDict()
2388
2389 parameters = iter(self.parameters.values())
2390 parameters_ex = ()
2391 arg_vals = iter(args)
2392
2393 if partial:
2394 # Support for binding arguments to 'functools.partial' objects.
2395 # See 'functools.partial' case in 'signature()' implementation
2396 # for details.
2397 for param_name, param in self.parameters.items():
2398 if (param._partial_kwarg and param_name not in kwargs):
2399 # Simulating 'functools.partial' behavior
2400 kwargs[param_name] = param.default
2401
2402 while True:
2403 # Let's iterate through the positional arguments and corresponding
2404 # parameters
2405 try:
2406 arg_val = next(arg_vals)
2407 except StopIteration:
2408 # No more positional arguments
2409 try:
2410 param = next(parameters)
2411 except StopIteration:
2412 # No more parameters. That's it. Just need to check that
2413 # we have no `kwargs` after this while loop
2414 break
2415 else:
2416 if param.kind == _VAR_POSITIONAL:
2417 # That's OK, just empty *args. Let's start parsing
2418 # kwargs
2419 break
2420 elif param.name in kwargs:
2421 if param.kind == _POSITIONAL_ONLY:
2422 msg = '{arg!r} parameter is positional only, ' \
2423 'but was passed as a keyword'
2424 msg = msg.format(arg=param.name)
2425 raise TypeError(msg) from None
2426 parameters_ex = (param,)
2427 break
2428 elif (param.kind == _VAR_KEYWORD or
2429 param.default is not _empty):
2430 # That's fine too - we have a default value for this
2431 # parameter. So, lets start parsing `kwargs`, starting
2432 # with the current parameter
2433 parameters_ex = (param,)
2434 break
2435 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002436 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
2437 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002438 if partial:
2439 parameters_ex = (param,)
2440 break
2441 else:
2442 msg = '{arg!r} parameter lacking default value'
2443 msg = msg.format(arg=param.name)
2444 raise TypeError(msg) from None
2445 else:
2446 # We have a positional argument to process
2447 try:
2448 param = next(parameters)
2449 except StopIteration:
2450 raise TypeError('too many positional arguments') from None
2451 else:
2452 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
2453 # Looks like we have no parameter for this positional
2454 # argument
2455 raise TypeError('too many positional arguments')
2456
2457 if param.kind == _VAR_POSITIONAL:
2458 # We have an '*args'-like argument, let's fill it with
2459 # all positional arguments we have left and move on to
2460 # the next phase
2461 values = [arg_val]
2462 values.extend(arg_vals)
2463 arguments[param.name] = tuple(values)
2464 break
2465
2466 if param.name in kwargs:
2467 raise TypeError('multiple values for argument '
2468 '{arg!r}'.format(arg=param.name))
2469
2470 arguments[param.name] = arg_val
2471
2472 # Now, we iterate through the remaining parameters to process
2473 # keyword arguments
2474 kwargs_param = None
2475 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002476 if param.kind == _VAR_KEYWORD:
2477 # Memorize that we have a '**kwargs'-like parameter
2478 kwargs_param = param
2479 continue
2480
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002481 if param.kind == _VAR_POSITIONAL:
2482 # Named arguments don't refer to '*args'-like parameters.
2483 # We only arrive here if the positional arguments ended
2484 # before reaching the last parameter before *args.
2485 continue
2486
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002487 param_name = param.name
2488 try:
2489 arg_val = kwargs.pop(param_name)
2490 except KeyError:
2491 # We have no value for this parameter. It's fine though,
2492 # if it has a default value, or it is an '*args'-like
2493 # parameter, left alone by the processing of positional
2494 # arguments.
2495 if (not partial and param.kind != _VAR_POSITIONAL and
2496 param.default is _empty):
2497 raise TypeError('{arg!r} parameter lacking default value'. \
2498 format(arg=param_name)) from None
2499
2500 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05002501 if param.kind == _POSITIONAL_ONLY:
2502 # This should never happen in case of a properly built
2503 # Signature object (but let's have this check here
2504 # to ensure correct behaviour just in case)
2505 raise TypeError('{arg!r} parameter is positional only, '
2506 'but was passed as a keyword'. \
2507 format(arg=param.name))
2508
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002509 arguments[param_name] = arg_val
2510
2511 if kwargs:
2512 if kwargs_param is not None:
2513 # Process our '**kwargs'-like parameter
2514 arguments[kwargs_param.name] = kwargs
2515 else:
2516 raise TypeError('too many keyword arguments')
2517
2518 return self._bound_arguments_cls(self, arguments)
2519
Yury Selivanovc45873e2014-01-29 12:10:27 -05002520 def bind(*args, **kwargs):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002521 '''Get a BoundArguments object, that maps the passed `args`
2522 and `kwargs` to the function's signature. Raises `TypeError`
2523 if the passed arguments can not be bound.
2524 '''
Yury Selivanovc45873e2014-01-29 12:10:27 -05002525 return args[0]._bind(args[1:], kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002526
Yury Selivanovc45873e2014-01-29 12:10:27 -05002527 def bind_partial(*args, **kwargs):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002528 '''Get a BoundArguments object, that partially maps the
2529 passed `args` and `kwargs` to the function's signature.
2530 Raises `TypeError` if the passed arguments can not be bound.
2531 '''
Yury Selivanovc45873e2014-01-29 12:10:27 -05002532 return args[0]._bind(args[1:], kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002533
2534 def __str__(self):
2535 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05002536 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002537 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05002538 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002539 formatted = str(param)
2540
2541 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002542
2543 if kind == _POSITIONAL_ONLY:
2544 render_pos_only_separator = True
2545 elif render_pos_only_separator:
2546 # It's not a positional-only parameter, and the flag
2547 # is set to 'True' (there were pos-only params before.)
2548 result.append('/')
2549 render_pos_only_separator = False
2550
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002551 if kind == _VAR_POSITIONAL:
2552 # OK, we have an '*args'-like parameter, so we won't need
2553 # a '*' to separate keyword-only arguments
2554 render_kw_only_separator = False
2555 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
2556 # We have a keyword-only parameter to render and we haven't
2557 # rendered an '*args'-like parameter before, so add a '*'
2558 # separator to the parameters list ("foo(arg1, *, arg2)" case)
2559 result.append('*')
2560 # This condition should be only triggered once, so
2561 # reset the flag
2562 render_kw_only_separator = False
2563
2564 result.append(formatted)
2565
Yury Selivanov2393dca2014-01-27 15:07:58 -05002566 if render_pos_only_separator:
2567 # There were only positional-only parameters, hence the
2568 # flag was not reset to 'False'
2569 result.append('/')
2570
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002571 rendered = '({})'.format(', '.join(result))
2572
2573 if self.return_annotation is not _empty:
2574 anno = formatannotation(self.return_annotation)
2575 rendered += ' -> {}'.format(anno)
2576
2577 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10002578
2579def _main():
2580 """ Logic for inspecting an object given at command line """
2581 import argparse
2582 import importlib
2583
2584 parser = argparse.ArgumentParser()
2585 parser.add_argument(
2586 'object',
2587 help="The object to be analysed. "
2588 "It supports the 'module:qualname' syntax")
2589 parser.add_argument(
2590 '-d', '--details', action='store_true',
2591 help='Display info about the module rather than its source code')
2592
2593 args = parser.parse_args()
2594
2595 target = args.object
2596 mod_name, has_attrs, attrs = target.partition(":")
2597 try:
2598 obj = module = importlib.import_module(mod_name)
2599 except Exception as exc:
2600 msg = "Failed to import {} ({}: {})".format(mod_name,
2601 type(exc).__name__,
2602 exc)
2603 print(msg, file=sys.stderr)
2604 exit(2)
2605
2606 if has_attrs:
2607 parts = attrs.split(".")
2608 obj = module
2609 for part in parts:
2610 obj = getattr(obj, part)
2611
2612 if module.__name__ in sys.builtin_module_names:
2613 print("Can't get info for builtin modules.", file=sys.stderr)
2614 exit(1)
2615
2616 if args.details:
2617 print('Target: {}'.format(target))
2618 print('Origin: {}'.format(getsourcefile(module)))
2619 print('Cached: {}'.format(module.__cached__))
2620 if obj is module:
2621 print('Loader: {}'.format(repr(module.__loader__)))
2622 if hasattr(module, '__path__'):
2623 print('Submodule search path: {}'.format(module.__path__))
2624 else:
2625 try:
2626 __, lineno = findsource(obj)
2627 except Exception:
2628 pass
2629 else:
2630 print('Line: {}'.format(lineno))
2631
2632 print('\n')
2633 else:
2634 print(getsource(obj))
2635
2636
2637if __name__ == "__main__":
2638 _main()