blob: 4298de6b60c267ca386d934a8867ed3802de3f99 [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
Larry Hastings2623c8c2014-02-08 22:15:29 -080042import token
Brett Cannoncb66eb02012-05-11 12:58:42 -040043import types
Brett Cannon2b88fcf2012-06-02 22:28:42 -040044import warnings
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070045import functools
Nick Coghlan2f92e542012-06-23 19:39:55 +100046import builtins
Raymond Hettingera1a992c2005-03-11 06:46:45 +000047from operator import attrgetter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070048from collections import namedtuple, OrderedDict
Nick Coghlan09c81232010-08-17 10:18:16 +000049
50# Create constants for the compiler flags in Include/code.h
51# We try to get them from dis to avoid duplication, but fall
Berker Peksagf23530f2014-10-19 18:04:38 +030052# back to hard-coding so the dependency is optional
Nick Coghlan09c81232010-08-17 10:18:16 +000053try:
54 from dis import COMPILER_FLAG_NAMES as _flag_names
Brett Cannoncd171c82013-07-04 17:43:24 -040055except ImportError:
Nick Coghlan09c81232010-08-17 10:18:16 +000056 CO_OPTIMIZED, CO_NEWLOCALS = 0x1, 0x2
57 CO_VARARGS, CO_VARKEYWORDS = 0x4, 0x8
58 CO_NESTED, CO_GENERATOR, CO_NOFREE = 0x10, 0x20, 0x40
59else:
60 mod_dict = globals()
61 for k, v in _flag_names.items():
62 mod_dict["CO_" + v] = k
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000063
Christian Heimesbe5b30b2008-03-03 19:18:51 +000064# See Include/object.h
65TPFLAGS_IS_ABSTRACT = 1 << 20
66
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000067# ----------------------------------------------------------- type-checking
68def ismodule(object):
69 """Return true if the object is a module.
70
71 Module objects provide these attributes:
Barry Warsaw28a691b2010-04-17 00:19:56 +000072 __cached__ pathname to byte compiled file
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000073 __doc__ documentation string
74 __file__ filename (missing for built-in modules)"""
Tim Peters28bc59f2001-09-16 08:40:16 +000075 return isinstance(object, types.ModuleType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000076
77def isclass(object):
78 """Return true if the object is a class.
79
80 Class objects provide these attributes:
81 __doc__ documentation string
82 __module__ name of module in which this class was defined"""
Benjamin Petersonc4656002009-01-17 22:41:18 +000083 return isinstance(object, type)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000084
85def ismethod(object):
86 """Return true if the object is an instance method.
87
88 Instance method objects provide these attributes:
89 __doc__ documentation string
90 __name__ name with which this method was defined
Christian Heimesff737952007-11-27 10:40:20 +000091 __func__ function object containing implementation of method
92 __self__ instance to which this method is bound"""
Tim Peters28bc59f2001-09-16 08:40:16 +000093 return isinstance(object, types.MethodType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000094
Tim Peters536d2262001-09-20 05:13:38 +000095def ismethoddescriptor(object):
Tim Petersf1d90b92001-09-20 05:47:55 +000096 """Return true if the object is a method descriptor.
97
98 But not if ismethod() or isclass() or isfunction() are true.
Tim Peters536d2262001-09-20 05:13:38 +000099
100 This is new in Python 2.2, and, for example, is true of int.__add__.
101 An object passing this test has a __get__ attribute but not a __set__
102 attribute, but beyond that the set of attributes varies. __name__ is
103 usually sensible, and __doc__ often is.
104
Tim Petersf1d90b92001-09-20 05:47:55 +0000105 Methods implemented via descriptors that also pass one of the other
106 tests return false from the ismethoddescriptor() test, simply because
107 the other tests promise more -- you can, e.g., count on having the
Christian Heimesff737952007-11-27 10:40:20 +0000108 __func__ attribute (etc) when an object passes ismethod()."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100109 if isclass(object) or ismethod(object) or isfunction(object):
110 # mutual exclusion
111 return False
112 tp = type(object)
113 return hasattr(tp, "__get__") and not hasattr(tp, "__set__")
Tim Peters536d2262001-09-20 05:13:38 +0000114
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000115def isdatadescriptor(object):
116 """Return true if the object is a data descriptor.
117
118 Data descriptors have both a __get__ and a __set__ attribute. Examples are
119 properties (defined in Python) and getsets and members (defined in C).
120 Typically, data descriptors will also have __name__ and __doc__ attributes
121 (properties, getsets, and members have both of these attributes), but this
122 is not guaranteed."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100123 if isclass(object) or ismethod(object) or isfunction(object):
124 # mutual exclusion
125 return False
126 tp = type(object)
127 return hasattr(tp, "__set__") and hasattr(tp, "__get__")
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000128
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000129if hasattr(types, 'MemberDescriptorType'):
130 # CPython and equivalent
131 def ismemberdescriptor(object):
132 """Return true if the object is a member descriptor.
133
134 Member descriptors are specialized descriptors defined in extension
135 modules."""
136 return isinstance(object, types.MemberDescriptorType)
137else:
138 # Other implementations
139 def ismemberdescriptor(object):
140 """Return true if the object is a member descriptor.
141
142 Member descriptors are specialized descriptors defined in extension
143 modules."""
144 return False
145
146if hasattr(types, 'GetSetDescriptorType'):
147 # CPython and equivalent
148 def isgetsetdescriptor(object):
149 """Return true if the object is a getset descriptor.
150
151 getset descriptors are specialized descriptors defined in extension
152 modules."""
153 return isinstance(object, types.GetSetDescriptorType)
154else:
155 # Other implementations
156 def isgetsetdescriptor(object):
157 """Return true if the object is a getset descriptor.
158
159 getset descriptors are specialized descriptors defined in extension
160 modules."""
161 return False
162
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000163def isfunction(object):
164 """Return true if the object is a user-defined function.
165
166 Function objects provide these attributes:
167 __doc__ documentation string
168 __name__ name with which this function was defined
Neal Norwitz221085d2007-02-25 20:55:47 +0000169 __code__ code object containing compiled function bytecode
170 __defaults__ tuple of any default values for arguments
171 __globals__ global namespace in which this function was defined
172 __annotations__ dict of parameter annotations
173 __kwdefaults__ dict of keyword only parameters with defaults"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000174 return isinstance(object, types.FunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000175
Christian Heimes7131fd92008-02-19 14:21:46 +0000176def isgeneratorfunction(object):
177 """Return true if the object is a user-defined generator function.
178
179 Generator function objects provides same attributes as functions.
180
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000181 See help(isfunction) for attributes listing."""
Georg Brandlb1441c72009-01-03 22:33:39 +0000182 return bool((isfunction(object) or ismethod(object)) and
183 object.__code__.co_flags & CO_GENERATOR)
Christian Heimes7131fd92008-02-19 14:21:46 +0000184
185def isgenerator(object):
186 """Return true if the object is a generator.
187
188 Generator objects provide these attributes:
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300189 __iter__ defined to support iteration over container
Christian Heimes7131fd92008-02-19 14:21:46 +0000190 close raises a new GeneratorExit exception inside the
191 generator to terminate the iteration
192 gi_code code object
193 gi_frame frame object or possibly None once the generator has
194 been exhausted
195 gi_running set to 1 when generator is executing, 0 otherwise
196 next return the next item from the container
197 send resumes the generator and "sends" a value that becomes
198 the result of the current yield-expression
199 throw used to raise an exception inside the generator"""
200 return isinstance(object, types.GeneratorType)
201
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000202def istraceback(object):
203 """Return true if the object is a traceback.
204
205 Traceback objects provide these attributes:
206 tb_frame frame object at this level
207 tb_lasti index of last attempted instruction in bytecode
208 tb_lineno current line number in Python source code
209 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000210 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000211
212def isframe(object):
213 """Return true if the object is a frame object.
214
215 Frame objects provide these attributes:
216 f_back next outer frame object (this frame's caller)
217 f_builtins built-in namespace seen by this frame
218 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000219 f_globals global namespace seen by this frame
220 f_lasti index of last attempted instruction in bytecode
221 f_lineno current line number in Python source code
222 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000223 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000224 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000225
226def iscode(object):
227 """Return true if the object is a code object.
228
229 Code objects provide these attributes:
230 co_argcount number of arguments (not including * or ** args)
231 co_code string of raw compiled bytecode
232 co_consts tuple of constants used in the bytecode
233 co_filename name of file in which this code object was created
234 co_firstlineno number of first line in Python source code
235 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
236 co_lnotab encoded mapping of line numbers to bytecode indices
237 co_name name with which this code object was defined
238 co_names tuple of names of local variables
239 co_nlocals number of local variables
240 co_stacksize virtual machine stack space required
241 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000242 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000243
244def isbuiltin(object):
245 """Return true if the object is a built-in function or method.
246
247 Built-in functions and methods provide these attributes:
248 __doc__ documentation string
249 __name__ original name of this function or method
250 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000251 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000252
253def isroutine(object):
254 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000255 return (isbuiltin(object)
256 or isfunction(object)
257 or ismethod(object)
258 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000259
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000260def isabstract(object):
261 """Return true if the object is an abstract base class (ABC)."""
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000262 return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT)
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000263
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000264def getmembers(object, predicate=None):
265 """Return all members of an object as (name, value) pairs sorted by name.
266 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100267 if isclass(object):
268 mro = (object,) + getmro(object)
269 else:
270 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000271 results = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700272 processed = set()
273 names = dir(object)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700274 # :dd any DynamicClassAttributes to the list of names if object is a class;
Ethan Furmane03ea372013-09-25 07:14:41 -0700275 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700276 # attribute with the same name as a DynamicClassAttribute exists
Ethan Furmane03ea372013-09-25 07:14:41 -0700277 try:
278 for base in object.__bases__:
279 for k, v in base.__dict__.items():
280 if isinstance(v, types.DynamicClassAttribute):
281 names.append(k)
282 except AttributeError:
283 pass
284 for key in names:
Ethan Furman63c141c2013-10-18 00:27:39 -0700285 # First try to get the value via getattr. Some descriptors don't
286 # like calling their __get__ (see bug #1785), so fall back to
287 # looking in the __dict__.
288 try:
289 value = getattr(object, key)
290 # handle the duplicate key
291 if key in processed:
292 raise AttributeError
293 except AttributeError:
294 for base in mro:
295 if key in base.__dict__:
296 value = base.__dict__[key]
297 break
298 else:
299 # could be a (currently) missing slot member, or a buggy
300 # __dir__; discard and move on
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100301 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000302 if not predicate or predicate(value):
303 results.append((key, value))
Ethan Furmane03ea372013-09-25 07:14:41 -0700304 processed.add(key)
305 results.sort(key=lambda pair: pair[0])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000306 return results
307
Christian Heimes25bb7832008-01-11 16:17:00 +0000308Attribute = namedtuple('Attribute', 'name kind defining_class object')
309
Tim Peters13b49d32001-09-23 02:00:29 +0000310def classify_class_attrs(cls):
311 """Return list of attribute-descriptor tuples.
312
313 For each name in dir(cls), the return list contains a 4-tuple
314 with these elements:
315
316 0. The name (a string).
317
318 1. The kind of attribute this is, one of these strings:
319 'class method' created via classmethod()
320 'static method' created via staticmethod()
321 'property' created via property()
Ethan Furmane03ea372013-09-25 07:14:41 -0700322 'method' any other flavor of method or descriptor
Tim Peters13b49d32001-09-23 02:00:29 +0000323 'data' not a method
324
325 2. The class which defined this attribute (a class).
326
Ethan Furmane03ea372013-09-25 07:14:41 -0700327 3. The object as obtained by calling getattr; if this fails, or if the
328 resulting object does not live anywhere in the class' mro (including
329 metaclasses) then the object is looked up in the defining class's
330 dict (found by walking the mro).
Ethan Furman668dede2013-09-14 18:53:26 -0700331
332 If one of the items in dir(cls) is stored in the metaclass it will now
333 be discovered and not have None be listed as the class in which it was
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700334 defined. Any items whose home class cannot be discovered are skipped.
Tim Peters13b49d32001-09-23 02:00:29 +0000335 """
336
337 mro = getmro(cls)
Ethan Furman668dede2013-09-14 18:53:26 -0700338 metamro = getmro(type(cls)) # for attributes stored in the metaclass
Ethan Furmane03ea372013-09-25 07:14:41 -0700339 metamro = tuple([cls for cls in metamro if cls not in (type, object)])
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700340 class_bases = (cls,) + mro
341 all_bases = class_bases + metamro
Tim Peters13b49d32001-09-23 02:00:29 +0000342 names = dir(cls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700343 # :dd any DynamicClassAttributes to the list of names;
Ethan Furmane03ea372013-09-25 07:14:41 -0700344 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700345 # attribute with the same name as a DynamicClassAttribute exists.
Ethan Furman63c141c2013-10-18 00:27:39 -0700346 for base in mro:
Ethan Furmane03ea372013-09-25 07:14:41 -0700347 for k, v in base.__dict__.items():
348 if isinstance(v, types.DynamicClassAttribute):
349 names.append(k)
Tim Peters13b49d32001-09-23 02:00:29 +0000350 result = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700351 processed = set()
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700352
Tim Peters13b49d32001-09-23 02:00:29 +0000353 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100354 # Get the object associated with the name, and where it was defined.
Ethan Furmane03ea372013-09-25 07:14:41 -0700355 # Normal objects will be looked up with both getattr and directly in
356 # its class' dict (in case getattr fails [bug #1785], and also to look
357 # for a docstring).
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700358 # For DynamicClassAttributes on the second pass we only look in the
Ethan Furmane03ea372013-09-25 07:14:41 -0700359 # class's dict.
360 #
Tim Peters13b49d32001-09-23 02:00:29 +0000361 # Getting an obj from the __dict__ sometimes reveals more than
362 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100363 homecls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700364 get_obj = None
365 dict_obj = None
Ethan Furmane03ea372013-09-25 07:14:41 -0700366 if name not in processed:
367 try:
Ethan Furmana8b07072013-10-18 01:22:08 -0700368 if name == '__dict__':
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700369 raise Exception("__dict__ is special, don't want the proxy")
Ethan Furmane03ea372013-09-25 07:14:41 -0700370 get_obj = getattr(cls, name)
371 except Exception as exc:
372 pass
373 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700374 homecls = getattr(get_obj, "__objclass__", homecls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700375 if homecls not in class_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700376 # if the resulting object does not live somewhere in the
Ethan Furman63c141c2013-10-18 00:27:39 -0700377 # mro, drop it and search the mro manually
Ethan Furmane03ea372013-09-25 07:14:41 -0700378 homecls = None
Ethan Furman63c141c2013-10-18 00:27:39 -0700379 last_cls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700380 # first look in the classes
381 for srch_cls in class_bases:
Ethan Furman63c141c2013-10-18 00:27:39 -0700382 srch_obj = getattr(srch_cls, name, None)
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400383 if srch_obj is get_obj:
Ethan Furman63c141c2013-10-18 00:27:39 -0700384 last_cls = srch_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700385 # then check the metaclasses
386 for srch_cls in metamro:
387 try:
388 srch_obj = srch_cls.__getattr__(cls, name)
389 except AttributeError:
390 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400391 if srch_obj is get_obj:
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700392 last_cls = srch_cls
Ethan Furman63c141c2013-10-18 00:27:39 -0700393 if last_cls is not None:
394 homecls = last_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700395 for base in all_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700396 if name in base.__dict__:
397 dict_obj = base.__dict__[name]
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700398 if homecls not in metamro:
399 homecls = base
Ethan Furmane03ea372013-09-25 07:14:41 -0700400 break
Ethan Furman63c141c2013-10-18 00:27:39 -0700401 if homecls is None:
402 # unable to locate the attribute anywhere, most likely due to
403 # buggy custom __dir__; discard and move on
404 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400405 obj = get_obj if get_obj is not None else dict_obj
Ethan Furmane03ea372013-09-25 07:14:41 -0700406 # Classify the object or its descriptor.
Ethan Furman63c141c2013-10-18 00:27:39 -0700407 if isinstance(dict_obj, staticmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000408 kind = "static method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700409 obj = dict_obj
Ethan Furman63c141c2013-10-18 00:27:39 -0700410 elif isinstance(dict_obj, classmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000411 kind = "class method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700412 obj = dict_obj
413 elif isinstance(dict_obj, property):
Tim Peters13b49d32001-09-23 02:00:29 +0000414 kind = "property"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700415 obj = dict_obj
Yury Selivanov0860a0b2014-01-31 14:28:44 -0500416 elif isroutine(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000417 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100418 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700419 kind = "data"
Christian Heimes25bb7832008-01-11 16:17:00 +0000420 result.append(Attribute(name, kind, homecls, obj))
Ethan Furmane03ea372013-09-25 07:14:41 -0700421 processed.add(name)
Tim Peters13b49d32001-09-23 02:00:29 +0000422 return result
423
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000424# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000425
426def getmro(cls):
427 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000428 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000429
Nick Coghlane8c45d62013-07-28 20:00:01 +1000430# -------------------------------------------------------- function helpers
431
432def unwrap(func, *, stop=None):
433 """Get the object wrapped by *func*.
434
435 Follows the chain of :attr:`__wrapped__` attributes returning the last
436 object in the chain.
437
438 *stop* is an optional callback accepting an object in the wrapper chain
439 as its sole argument that allows the unwrapping to be terminated early if
440 the callback returns a true value. If the callback never returns a true
441 value, the last object in the chain is returned as usual. For example,
442 :func:`signature` uses this to stop unwrapping if any object in the
443 chain has a ``__signature__`` attribute defined.
444
445 :exc:`ValueError` is raised if a cycle is encountered.
446
447 """
448 if stop is None:
449 def _is_wrapper(f):
450 return hasattr(f, '__wrapped__')
451 else:
452 def _is_wrapper(f):
453 return hasattr(f, '__wrapped__') and not stop(f)
454 f = func # remember the original func for error reporting
455 memo = {id(f)} # Memoise by id to tolerate non-hashable objects
456 while _is_wrapper(func):
457 func = func.__wrapped__
458 id_func = id(func)
459 if id_func in memo:
460 raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
461 memo.add(id_func)
462 return func
463
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000464# -------------------------------------------------- source code extraction
465def indentsize(line):
466 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000467 expline = line.expandtabs()
468 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000469
470def getdoc(object):
471 """Get the documentation string for an object.
472
473 All tabs are expanded to spaces. To clean up docstrings that are
474 indented to line up with blocks of code, any whitespace than can be
475 uniformly removed from the second line onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000476 try:
477 doc = object.__doc__
478 except AttributeError:
479 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000480 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000481 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000482 return cleandoc(doc)
483
484def cleandoc(doc):
485 """Clean up indentation from docstrings.
486
487 Any whitespace that can be uniformly removed from the second line
488 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000489 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000490 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000491 except UnicodeError:
492 return None
493 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000494 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000495 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000496 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000497 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000498 if content:
499 indent = len(line) - content
500 margin = min(margin, indent)
501 # Remove indentation.
502 if lines:
503 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000504 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000505 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000506 # Remove any trailing or leading blank lines.
507 while lines and not lines[-1]:
508 lines.pop()
509 while lines and not lines[0]:
510 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000511 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000512
513def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000514 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000515 if ismodule(object):
516 if hasattr(object, '__file__'):
517 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000518 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000519 if isclass(object):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500520 if hasattr(object, '__module__'):
521 object = sys.modules.get(object.__module__)
522 if hasattr(object, '__file__'):
523 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000524 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000525 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000526 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000527 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000528 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000529 if istraceback(object):
530 object = object.tb_frame
531 if isframe(object):
532 object = object.f_code
533 if iscode(object):
534 return object.co_filename
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000535 raise TypeError('{!r} is not a module, class, method, '
536 'function, traceback, frame, or code object'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000537
Christian Heimes25bb7832008-01-11 16:17:00 +0000538ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')
539
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000540def getmoduleinfo(path):
541 """Get the module name, suffix, mode, and module type for a given file."""
Brett Cannoncb66eb02012-05-11 12:58:42 -0400542 warnings.warn('inspect.getmoduleinfo() is deprecated', DeprecationWarning,
543 2)
Brett Cannone4f41de2013-06-16 13:13:40 -0400544 with warnings.catch_warnings():
545 warnings.simplefilter('ignore', PendingDeprecationWarning)
546 import imp
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000547 filename = os.path.basename(path)
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000548 suffixes = [(-len(suffix), suffix, mode, mtype)
549 for suffix, mode, mtype in imp.get_suffixes()]
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000550 suffixes.sort() # try longest suffixes first, in case they overlap
551 for neglen, suffix, mode, mtype in suffixes:
552 if filename[neglen:] == suffix:
Christian Heimes25bb7832008-01-11 16:17:00 +0000553 return ModuleInfo(filename[:neglen], suffix, mode, mtype)
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000554
555def getmodulename(path):
556 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000557 fname = os.path.basename(path)
558 # Check for paths that look like an actual module file
559 suffixes = [(-len(suffix), suffix)
560 for suffix in importlib.machinery.all_suffixes()]
561 suffixes.sort() # try longest suffixes first, in case they overlap
562 for neglen, suffix in suffixes:
563 if fname.endswith(suffix):
564 return fname[:neglen]
565 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000566
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000567def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000568 """Return the filename that can be used to locate an object's source.
569 Return None if no way can be identified to get the source.
570 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000571 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400572 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
573 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
574 if any(filename.endswith(s) for s in all_bytecode_suffixes):
575 filename = (os.path.splitext(filename)[0] +
576 importlib.machinery.SOURCE_SUFFIXES[0])
577 elif any(filename.endswith(s) for s in
578 importlib.machinery.EXTENSION_SUFFIXES):
579 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000580 if os.path.exists(filename):
581 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000582 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400583 if getattr(getmodule(object, filename), '__loader__', None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000584 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000585 # or it is in the linecache
586 if filename in linecache.cache:
587 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000588
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000589def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000590 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000591
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000592 The idea is for each object to have a unique origin, so this routine
593 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000594 if _filename is None:
595 _filename = getsourcefile(object) or getfile(object)
596 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000597
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000598modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000599_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000600
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000601def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000602 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000603 if ismodule(object):
604 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000605 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000606 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000607 # Try the filename to modulename cache
608 if _filename is not None and _filename in modulesbyfile:
609 return sys.modules.get(modulesbyfile[_filename])
610 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000611 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000612 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000613 except TypeError:
614 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000615 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000616 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000617 # Update the filename to module name cache and check yet again
618 # Copy sys.modules in order to cope with changes while iterating
Éric Araujoa74f8ef2011-11-29 16:58:53 +0100619 for modname, module in list(sys.modules.items()):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000620 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000621 f = module.__file__
622 if f == _filesbymodname.get(modname, None):
623 # Have already mapped this module, so skip it
624 continue
625 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000626 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000627 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000628 modulesbyfile[f] = modulesbyfile[
629 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000630 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000631 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000632 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000633 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000634 if not hasattr(object, '__name__'):
635 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000636 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000637 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000638 if mainobject is object:
639 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000640 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000641 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000642 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000643 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000644 if builtinobject is object:
645 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000646
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000647def findsource(object):
648 """Return the entire source file and starting line number for an object.
649
650 The argument may be a module, class, method, function, traceback, frame,
651 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200652 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000653 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500654
Yury Selivanov7de29682014-12-08 18:00:25 -0500655 file = getsourcefile(object)
656 if file:
657 # Invalidate cache if needed.
658 linecache.checkcache(file)
659 else:
660 file = getfile(object)
661 # Allow filenames in form of "<something>" to pass through.
662 # `doctest` monkeypatches `linecache` module to enable
663 # inspection, so let `linecache.getlines` to be called.
664 if not (file.startswith('<') and file.endswith('>')):
665 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500666
Thomas Wouters89f507f2006-12-13 04:49:30 +0000667 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000668 if module:
669 lines = linecache.getlines(file, module.__dict__)
670 else:
671 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000672 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200673 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000674
675 if ismodule(object):
676 return lines, 0
677
678 if isclass(object):
679 name = object.__name__
Thomas Wouters89f507f2006-12-13 04:49:30 +0000680 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
681 # make some effort to find the best matching class definition:
682 # use the one with the least indentation, which is the one
683 # that's most probably not inside a function definition.
684 candidates = []
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000685 for i in range(len(lines)):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000686 match = pat.match(lines[i])
687 if match:
688 # if it's at toplevel, it's already the best one
689 if lines[i][0] == 'c':
690 return lines, i
691 # else add whitespace to candidate list
692 candidates.append((match.group(1), i))
693 if candidates:
694 # this will sort by whitespace, and by line number,
695 # less whitespace first
696 candidates.sort()
697 return lines, candidates[0][1]
Jeremy Hyltonab919022003-06-27 18:41:20 +0000698 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200699 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000700
701 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000702 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000703 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000704 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000705 if istraceback(object):
706 object = object.tb_frame
707 if isframe(object):
708 object = object.f_code
709 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000710 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200711 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000712 lnum = object.co_firstlineno - 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000713 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000714 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000715 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000716 lnum = lnum - 1
717 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200718 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000719
720def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000721 """Get lines of comments immediately preceding an object's source code.
722
723 Returns None when source can't be found.
724 """
725 try:
726 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200727 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000728 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000729
730 if ismodule(object):
731 # Look for a comment block at the top of the file.
732 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000733 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000734 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000735 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000736 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000737 comments = []
738 end = start
739 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000740 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000741 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000742 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000743
744 # Look for a preceding block of comments at the same indentation.
745 elif lnum > 0:
746 indent = indentsize(lines[lnum])
747 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000748 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000749 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000750 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000751 if end > 0:
752 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000753 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000754 while comment[:1] == '#' and indentsize(lines[end]) == indent:
755 comments[:0] = [comment]
756 end = end - 1
757 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000758 comment = lines[end].expandtabs().lstrip()
759 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000760 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000761 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000762 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000763 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000764
Tim Peters4efb6e92001-06-29 23:51:08 +0000765class EndOfBlock(Exception): pass
766
767class BlockFinder:
768 """Provide a tokeneater() method to detect the end of a code block."""
769 def __init__(self):
770 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000771 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000772 self.started = False
773 self.passline = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000774 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000775
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000776 def tokeneater(self, type, token, srowcol, erowcol, line):
Tim Peters4efb6e92001-06-29 23:51:08 +0000777 if not self.started:
Armin Rigodd5c0232005-09-25 11:45:45 +0000778 # look for the first "def", "class" or "lambda"
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000779 if token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000780 if token == "lambda":
781 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000782 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000783 self.passline = True # skip to the end of the line
Tim Peters4efb6e92001-06-29 23:51:08 +0000784 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000785 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000786 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000787 if self.islambda: # lambdas always end at the first NEWLINE
788 raise EndOfBlock
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000789 elif self.passline:
790 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000791 elif type == tokenize.INDENT:
792 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000793 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000794 elif type == tokenize.DEDENT:
795 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000796 # the end of matching indent/dedent pairs end a block
797 # (note that this only works for "def"/"class" blocks,
798 # not e.g. for "if: else:" or "try: finally:" blocks)
799 if self.indent <= 0:
800 raise EndOfBlock
801 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
802 # any other token on the same indentation level end the previous
803 # block as well, except the pseudo-tokens COMMENT and NL.
804 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000805
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000806def getblock(lines):
807 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000808 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000809 try:
Trent Nelson428de652008-03-18 22:41:35 +0000810 tokens = tokenize.generate_tokens(iter(lines).__next__)
811 for _token in tokens:
812 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000813 except (EndOfBlock, IndentationError):
814 pass
815 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000816
817def getsourcelines(object):
818 """Return a list of source lines and starting line number for an object.
819
820 The argument may be a module, class, method, function, traceback, frame,
821 or code object. The source code is returned as a list of the lines
822 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200823 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000824 raised if the source code cannot be retrieved."""
825 lines, lnum = findsource(object)
826
827 if ismodule(object): return lines, 0
828 else: return getblock(lines[lnum:]), lnum + 1
829
830def getsource(object):
831 """Return the text of the source code for an object.
832
833 The argument may be a module, class, method, function, traceback, frame,
834 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200835 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000836 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000837 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000838
839# --------------------------------------------------- class tree extraction
840def walktree(classes, children, parent):
841 """Recursive helper function for getclasstree()."""
842 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +0000843 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000844 for c in classes:
845 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +0000846 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000847 results.append(walktree(children[c], children, c))
848 return results
849
Georg Brandl5ce83a02009-06-01 17:23:51 +0000850def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000851 """Arrange the given list of classes into a hierarchy of nested lists.
852
853 Where a nested list appears, it contains classes derived from the class
854 whose entry immediately precedes the list. Each entry is a 2-tuple
855 containing a class and a tuple of its base classes. If the 'unique'
856 argument is true, exactly one entry appears in the returned structure
857 for each class in the given list. Otherwise, classes using multiple
858 inheritance and their descendants will appear multiple times."""
859 children = {}
860 roots = []
861 for c in classes:
862 if c.__bases__:
863 for parent in c.__bases__:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000864 if not parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000865 children[parent] = []
Serhiy Storchaka362c1b52013-09-05 17:14:32 +0300866 if c not in children[parent]:
867 children[parent].append(c)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000868 if unique and parent in classes: break
869 elif c not in roots:
870 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +0000871 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000872 if parent not in classes:
873 roots.append(parent)
874 return walktree(roots, children, None)
875
876# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +0000877Arguments = namedtuple('Arguments', 'args, varargs, varkw')
878
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000879def getargs(co):
880 """Get information about the arguments accepted by a code object.
881
Guido van Rossum2e65f892007-02-28 22:03:49 +0000882 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000883 'args' is the list of argument names. Keyword-only arguments are
884 appended. 'varargs' and 'varkw' are the names of the * and **
885 arguments or None."""
Guido van Rossum2e65f892007-02-28 22:03:49 +0000886 args, varargs, kwonlyargs, varkw = _getfullargs(co)
Christian Heimes25bb7832008-01-11 16:17:00 +0000887 return Arguments(args + kwonlyargs, varargs, varkw)
Guido van Rossum2e65f892007-02-28 22:03:49 +0000888
889def _getfullargs(co):
890 """Get information about the arguments accepted by a code object.
891
892 Four things are returned: (args, varargs, kwonlyargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000893 'args' and 'kwonlyargs' are lists of argument names, and 'varargs'
894 and 'varkw' are the names of the * and ** arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +0000895
896 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000897 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000898
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000899 nargs = co.co_argcount
900 names = co.co_varnames
Guido van Rossum2e65f892007-02-28 22:03:49 +0000901 nkwargs = co.co_kwonlyargcount
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000902 args = list(names[:nargs])
Guido van Rossum2e65f892007-02-28 22:03:49 +0000903 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000904 step = 0
905
Guido van Rossum2e65f892007-02-28 22:03:49 +0000906 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000907 varargs = None
908 if co.co_flags & CO_VARARGS:
909 varargs = co.co_varnames[nargs]
910 nargs = nargs + 1
911 varkw = None
912 if co.co_flags & CO_VARKEYWORDS:
913 varkw = co.co_varnames[nargs]
Guido van Rossum2e65f892007-02-28 22:03:49 +0000914 return args, varargs, kwonlyargs, varkw
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000915
Christian Heimes25bb7832008-01-11 16:17:00 +0000916
917ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
918
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000919def getargspec(func):
920 """Get the names and default values of a function's arguments.
921
922 A tuple of four things is returned: (args, varargs, varkw, defaults).
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000923 'args' is a list of the argument names.
Guido van Rossum2e65f892007-02-28 22:03:49 +0000924 'args' will include keyword-only argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000925 'varargs' and 'varkw' are the names of the * and ** arguments or None.
Jeremy Hylton64967882003-06-27 18:14:39 +0000926 'defaults' is an n-tuple of the default values of the last n arguments.
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000927
Guido van Rossum2e65f892007-02-28 22:03:49 +0000928 Use the getfullargspec() API for Python-3000 code, as annotations
929 and keyword arguments are supported. getargspec() will raise ValueError
930 if the func has either annotations or keyword arguments.
931 """
932
933 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
934 getfullargspec(func)
935 if kwonlyargs or ann:
Collin Winterce36ad82007-08-30 01:19:48 +0000936 raise ValueError("Function has keyword-only arguments or annotations"
937 ", use getfullargspec() API which can support them")
Christian Heimes25bb7832008-01-11 16:17:00 +0000938 return ArgSpec(args, varargs, varkw, defaults)
939
940FullArgSpec = namedtuple('FullArgSpec',
Benjamin Peterson3d4ca742008-11-12 21:39:01 +0000941 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +0000942
943def getfullargspec(func):
Yury Selivanovd82eddc2014-01-29 11:24:39 -0500944 """Get the names and default values of a callable object's arguments.
Guido van Rossum2e65f892007-02-28 22:03:49 +0000945
Brett Cannon504d8852007-09-07 02:12:14 +0000946 A tuple of seven things is returned:
947 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults annotations).
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000948 'args' is a list of the argument names.
Guido van Rossum2e65f892007-02-28 22:03:49 +0000949 'varargs' and 'varkw' are the names of the * and ** arguments or None.
950 'defaults' is an n-tuple of the default values of the last n arguments.
951 'kwonlyargs' is a list of keyword-only argument names.
952 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
953 'annotations' is a dictionary mapping argument names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000954
Guido van Rossum2e65f892007-02-28 22:03:49 +0000955 The first four items in the tuple correspond to getargspec().
Jeremy Hylton64967882003-06-27 18:14:39 +0000956 """
957
Yury Selivanovff385b82014-02-19 16:27:23 -0500958 try:
959 # Re: `skip_bound_arg=False`
960 #
Yury Selivanovd82eddc2014-01-29 11:24:39 -0500961 # There is a notable difference in behaviour between getfullargspec
962 # and Signature: the former always returns 'self' parameter for bound
963 # methods, whereas the Signature always shows the actual calling
964 # signature of the passed object.
965 #
966 # To simulate this behaviour, we "unbind" bound methods, to trick
967 # inspect.signature to always return their first parameter ("self",
968 # usually)
Yury Selivanovd82eddc2014-01-29 11:24:39 -0500969
Yury Selivanovff385b82014-02-19 16:27:23 -0500970 # Re: `follow_wrapper_chains=False`
971 #
972 # getfullargspec() historically ignored __wrapped__ attributes,
973 # so we ensure that remains the case in 3.3+
Yury Selivanovd82eddc2014-01-29 11:24:39 -0500974
Yury Selivanovff385b82014-02-19 16:27:23 -0500975 sig = _signature_internal(func,
976 follow_wrapper_chains=False,
977 skip_bound_arg=False)
Yury Selivanovd82eddc2014-01-29 11:24:39 -0500978 except Exception as ex:
979 # Most of the times 'signature' will raise ValueError.
980 # But, it can also raise AttributeError, and, maybe something
981 # else. So to be fully backwards compatible, we catch all
982 # possible exceptions here, and reraise a TypeError.
983 raise TypeError('unsupported callable') from ex
984
985 args = []
986 varargs = None
987 varkw = None
988 kwonlyargs = []
989 defaults = ()
990 annotations = {}
991 defaults = ()
992 kwdefaults = {}
993
994 if sig.return_annotation is not sig.empty:
995 annotations['return'] = sig.return_annotation
996
997 for param in sig.parameters.values():
998 kind = param.kind
999 name = param.name
1000
1001 if kind is _POSITIONAL_ONLY:
1002 args.append(name)
1003 elif kind is _POSITIONAL_OR_KEYWORD:
1004 args.append(name)
1005 if param.default is not param.empty:
1006 defaults += (param.default,)
1007 elif kind is _VAR_POSITIONAL:
1008 varargs = name
1009 elif kind is _KEYWORD_ONLY:
1010 kwonlyargs.append(name)
1011 if param.default is not param.empty:
1012 kwdefaults[name] = param.default
1013 elif kind is _VAR_KEYWORD:
1014 varkw = name
1015
1016 if param.annotation is not param.empty:
1017 annotations[name] = param.annotation
1018
1019 if not kwdefaults:
1020 # compatibility with 'func.__kwdefaults__'
1021 kwdefaults = None
1022
1023 if not defaults:
1024 # compatibility with 'func.__defaults__'
1025 defaults = None
1026
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001027 return FullArgSpec(args, varargs, varkw, defaults,
1028 kwonlyargs, kwdefaults, annotations)
1029
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001030
Christian Heimes25bb7832008-01-11 16:17:00 +00001031ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
1032
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001033def getargvalues(frame):
1034 """Get information about arguments passed into a particular frame.
1035
1036 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001037 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001038 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1039 'locals' is the locals dictionary of the given frame."""
1040 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +00001041 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001042
Guido van Rossum2e65f892007-02-28 22:03:49 +00001043def formatannotation(annotation, base_module=None):
1044 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001045 if annotation.__module__ in ('builtins', base_module):
Guido van Rossum2e65f892007-02-28 22:03:49 +00001046 return annotation.__name__
1047 return annotation.__module__+'.'+annotation.__name__
1048 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001049
Guido van Rossum2e65f892007-02-28 22:03:49 +00001050def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001051 module = getattr(object, '__module__', None)
1052 def _formatannotation(annotation):
1053 return formatannotation(annotation, module)
1054 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001055
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001056def formatargspec(args, varargs=None, varkw=None, defaults=None,
Guido van Rossum2e65f892007-02-28 22:03:49 +00001057 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001058 formatarg=str,
1059 formatvarargs=lambda name: '*' + name,
1060 formatvarkw=lambda name: '**' + name,
1061 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001062 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001063 formatannotation=formatannotation):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001064 """Format an argument spec from the values returned by getargspec
Guido van Rossum2e65f892007-02-28 22:03:49 +00001065 or getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001066
Guido van Rossum2e65f892007-02-28 22:03:49 +00001067 The first seven arguments are (args, varargs, varkw, defaults,
1068 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1069 are the corresponding optional formatting functions that are called to
1070 turn names and values into strings. The last argument is an optional
1071 function to format the sequence of arguments."""
1072 def formatargandannotation(arg):
1073 result = formatarg(arg)
1074 if arg in annotations:
1075 result += ': ' + formatannotation(annotations[arg])
1076 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001077 specs = []
1078 if defaults:
1079 firstdefault = len(args) - len(defaults)
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001080 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001081 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001082 if defaults and i >= firstdefault:
1083 spec = spec + formatvalue(defaults[i - firstdefault])
1084 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001085 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001086 specs.append(formatvarargs(formatargandannotation(varargs)))
1087 else:
1088 if kwonlyargs:
1089 specs.append('*')
1090 if kwonlyargs:
1091 for kwonlyarg in kwonlyargs:
1092 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001093 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001094 spec += formatvalue(kwonlydefaults[kwonlyarg])
1095 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001096 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001097 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001098 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001099 if 'return' in annotations:
1100 result += formatreturns(formatannotation(annotations['return']))
1101 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001102
1103def formatargvalues(args, varargs, varkw, locals,
1104 formatarg=str,
1105 formatvarargs=lambda name: '*' + name,
1106 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001107 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001108 """Format an argument spec from the 4 values returned by getargvalues.
1109
1110 The first four arguments are (args, varargs, varkw, locals). The
1111 next four arguments are the corresponding optional formatting functions
1112 that are called to turn names and values into strings. The ninth
1113 argument is an optional function to format the sequence of arguments."""
1114 def convert(name, locals=locals,
1115 formatarg=formatarg, formatvalue=formatvalue):
1116 return formatarg(name) + formatvalue(locals[name])
1117 specs = []
1118 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001119 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001120 if varargs:
1121 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1122 if varkw:
1123 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001124 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001125
Benjamin Petersone109c702011-06-24 09:37:26 -05001126def _missing_arguments(f_name, argnames, pos, values):
1127 names = [repr(name) for name in argnames if name not in values]
1128 missing = len(names)
1129 if missing == 1:
1130 s = names[0]
1131 elif missing == 2:
1132 s = "{} and {}".format(*names)
1133 else:
Yury Selivanov2542b662014-03-27 18:42:52 -04001134 tail = ", {} and {}".format(*names[-2:])
Benjamin Petersone109c702011-06-24 09:37:26 -05001135 del names[-2:]
1136 s = ", ".join(names) + tail
1137 raise TypeError("%s() missing %i required %s argument%s: %s" %
1138 (f_name, missing,
1139 "positional" if pos else "keyword-only",
1140 "" if missing == 1 else "s", s))
1141
1142def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001143 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001144 kwonly_given = len([arg for arg in kwonly if arg in values])
1145 if varargs:
1146 plural = atleast != 1
1147 sig = "at least %d" % (atleast,)
1148 elif defcount:
1149 plural = True
1150 sig = "from %d to %d" % (atleast, len(args))
1151 else:
1152 plural = len(args) != 1
1153 sig = str(len(args))
1154 kwonly_sig = ""
1155 if kwonly_given:
1156 msg = " positional argument%s (and %d keyword-only argument%s)"
1157 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1158 "s" if kwonly_given != 1 else ""))
1159 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1160 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1161 "was" if given == 1 and not kwonly_given else "were"))
1162
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001163def getcallargs(*func_and_positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001164 """Get the mapping of arguments to values.
1165
1166 A dict is returned, with keys the function argument names (including the
1167 names of the * and ** arguments, if any), and values the respective bound
1168 values from 'positional' and 'named'."""
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001169 func = func_and_positional[0]
1170 positional = func_and_positional[1:]
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001171 spec = getfullargspec(func)
1172 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
1173 f_name = func.__name__
1174 arg2value = {}
1175
Benjamin Petersonb204a422011-06-05 22:04:07 -05001176
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001177 if ismethod(func) and func.__self__ is not None:
1178 # implicit 'self' (or 'cls' for classmethods) argument
1179 positional = (func.__self__,) + positional
1180 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001181 num_args = len(args)
1182 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001183
Benjamin Petersonb204a422011-06-05 22:04:07 -05001184 n = min(num_pos, num_args)
1185 for i in range(n):
1186 arg2value[args[i]] = positional[i]
1187 if varargs:
1188 arg2value[varargs] = tuple(positional[n:])
1189 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001190 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001191 arg2value[varkw] = {}
1192 for kw, value in named.items():
1193 if kw not in possible_kwargs:
1194 if not varkw:
1195 raise TypeError("%s() got an unexpected keyword argument %r" %
1196 (f_name, kw))
1197 arg2value[varkw][kw] = value
1198 continue
1199 if kw in arg2value:
1200 raise TypeError("%s() got multiple values for argument %r" %
1201 (f_name, kw))
1202 arg2value[kw] = value
1203 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001204 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1205 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001206 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001207 req = args[:num_args - num_defaults]
1208 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001209 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001210 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001211 for i, arg in enumerate(args[num_args - num_defaults:]):
1212 if arg not in arg2value:
1213 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001214 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001215 for kwarg in kwonlyargs:
1216 if kwarg not in arg2value:
Yury Selivanovb1d060b2014-03-27 18:23:03 -04001217 if kwonlydefaults and kwarg in kwonlydefaults:
Benjamin Petersone109c702011-06-24 09:37:26 -05001218 arg2value[kwarg] = kwonlydefaults[kwarg]
1219 else:
1220 missing += 1
1221 if missing:
1222 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001223 return arg2value
1224
Nick Coghlan2f92e542012-06-23 19:39:55 +10001225ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1226
1227def getclosurevars(func):
1228 """
1229 Get the mapping of free variables to their current values.
1230
Meador Inge8fda3592012-07-19 21:33:21 -05001231 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001232 and builtin references as seen by the body of the function. A final
1233 set of unbound names that could not be resolved is also provided.
1234 """
1235
1236 if ismethod(func):
1237 func = func.__func__
1238
1239 if not isfunction(func):
1240 raise TypeError("'{!r}' is not a Python function".format(func))
1241
1242 code = func.__code__
1243 # Nonlocal references are named in co_freevars and resolved
1244 # by looking them up in __closure__ by positional index
1245 if func.__closure__ is None:
1246 nonlocal_vars = {}
1247 else:
1248 nonlocal_vars = {
1249 var : cell.cell_contents
1250 for var, cell in zip(code.co_freevars, func.__closure__)
1251 }
1252
1253 # Global and builtin references are named in co_names and resolved
1254 # by looking them up in __globals__ or __builtins__
1255 global_ns = func.__globals__
1256 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1257 if ismodule(builtin_ns):
1258 builtin_ns = builtin_ns.__dict__
1259 global_vars = {}
1260 builtin_vars = {}
1261 unbound_names = set()
1262 for name in code.co_names:
1263 if name in ("None", "True", "False"):
1264 # Because these used to be builtins instead of keywords, they
1265 # may still show up as name references. We ignore them.
1266 continue
1267 try:
1268 global_vars[name] = global_ns[name]
1269 except KeyError:
1270 try:
1271 builtin_vars[name] = builtin_ns[name]
1272 except KeyError:
1273 unbound_names.add(name)
1274
1275 return ClosureVars(nonlocal_vars, global_vars,
1276 builtin_vars, unbound_names)
1277
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001278# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001279
1280Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1281
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001282def getframeinfo(frame, context=1):
1283 """Get information about a frame or traceback object.
1284
1285 A tuple of five things is returned: the filename, the line number of
1286 the current line, the function name, a list of lines of context from
1287 the source code, and the index of the current line within that list.
1288 The optional second argument specifies the number of lines of context
1289 to return, which are centered around the current line."""
1290 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001291 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001292 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001293 else:
1294 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001295 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001296 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001297
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001298 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001299 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001300 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001301 try:
1302 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001303 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001304 lines = index = None
1305 else:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001306 start = max(start, 1)
Raymond Hettingera0501712004-06-15 11:22:53 +00001307 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001308 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001309 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001310 else:
1311 lines = index = None
1312
Christian Heimes25bb7832008-01-11 16:17:00 +00001313 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001314
1315def getlineno(frame):
1316 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001317 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1318 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001319
1320def getouterframes(frame, context=1):
1321 """Get a list of records for a frame and all higher (calling) frames.
1322
1323 Each record contains a frame object, filename, line number, function
1324 name, a list of lines of context, and index within the context."""
1325 framelist = []
1326 while frame:
1327 framelist.append((frame,) + getframeinfo(frame, context))
1328 frame = frame.f_back
1329 return framelist
1330
1331def getinnerframes(tb, context=1):
1332 """Get a list of records for a traceback's frame and all lower frames.
1333
1334 Each record contains a frame object, filename, line number, function
1335 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001336 framelist = []
1337 while tb:
1338 framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
1339 tb = tb.tb_next
1340 return framelist
1341
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001342def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001343 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001344 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001345
1346def stack(context=1):
1347 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001348 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001349
1350def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001351 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001352 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001353
1354
1355# ------------------------------------------------ static version of getattr
1356
1357_sentinel = object()
1358
Michael Foorde5162652010-11-20 16:40:44 +00001359def _static_getmro(klass):
1360 return type.__dict__['__mro__'].__get__(klass)
1361
Michael Foord95fc51d2010-11-20 15:07:30 +00001362def _check_instance(obj, attr):
1363 instance_dict = {}
1364 try:
1365 instance_dict = object.__getattribute__(obj, "__dict__")
1366 except AttributeError:
1367 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001368 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001369
1370
1371def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001372 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001373 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001374 try:
1375 return entry.__dict__[attr]
1376 except KeyError:
1377 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001378 return _sentinel
1379
Michael Foord35184ed2010-11-20 16:58:30 +00001380def _is_type(obj):
1381 try:
1382 _static_getmro(obj)
1383 except TypeError:
1384 return False
1385 return True
1386
Michael Foorddcebe0f2011-03-15 19:20:44 -04001387def _shadowed_dict(klass):
1388 dict_attr = type.__dict__["__dict__"]
1389 for entry in _static_getmro(klass):
1390 try:
1391 class_dict = dict_attr.__get__(entry)["__dict__"]
1392 except KeyError:
1393 pass
1394 else:
1395 if not (type(class_dict) is types.GetSetDescriptorType and
1396 class_dict.__name__ == "__dict__" and
1397 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001398 return class_dict
1399 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001400
1401def getattr_static(obj, attr, default=_sentinel):
1402 """Retrieve attributes without triggering dynamic lookup via the
1403 descriptor protocol, __getattr__ or __getattribute__.
1404
1405 Note: this function may not be able to retrieve all attributes
1406 that getattr can fetch (like dynamically created attributes)
1407 and may find attributes that getattr can't (like descriptors
1408 that raise AttributeError). It can also return descriptor objects
1409 instead of instance members in some cases. See the
1410 documentation for details.
1411 """
1412 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001413 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001414 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001415 dict_attr = _shadowed_dict(klass)
1416 if (dict_attr is _sentinel or
1417 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001418 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001419 else:
1420 klass = obj
1421
1422 klass_result = _check_class(klass, attr)
1423
1424 if instance_result is not _sentinel and klass_result is not _sentinel:
1425 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1426 _check_class(type(klass_result), '__set__') is not _sentinel):
1427 return klass_result
1428
1429 if instance_result is not _sentinel:
1430 return instance_result
1431 if klass_result is not _sentinel:
1432 return klass_result
1433
1434 if obj is klass:
1435 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001436 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001437 if _shadowed_dict(type(entry)) is _sentinel:
1438 try:
1439 return entry.__dict__[attr]
1440 except KeyError:
1441 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001442 if default is not _sentinel:
1443 return default
1444 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001445
1446
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001447# ------------------------------------------------ generator introspection
1448
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001449GEN_CREATED = 'GEN_CREATED'
1450GEN_RUNNING = 'GEN_RUNNING'
1451GEN_SUSPENDED = 'GEN_SUSPENDED'
1452GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001453
1454def getgeneratorstate(generator):
1455 """Get current state of a generator-iterator.
1456
1457 Possible states are:
1458 GEN_CREATED: Waiting to start execution.
1459 GEN_RUNNING: Currently being executed by the interpreter.
1460 GEN_SUSPENDED: Currently suspended at a yield expression.
1461 GEN_CLOSED: Execution has completed.
1462 """
1463 if generator.gi_running:
1464 return GEN_RUNNING
1465 if generator.gi_frame is None:
1466 return GEN_CLOSED
1467 if generator.gi_frame.f_lasti == -1:
1468 return GEN_CREATED
1469 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001470
1471
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001472def getgeneratorlocals(generator):
1473 """
1474 Get the mapping of generator local variables to their current values.
1475
1476 A dict is returned, with the keys the local variable names and values the
1477 bound values."""
1478
1479 if not isgenerator(generator):
1480 raise TypeError("'{!r}' is not a Python generator".format(generator))
1481
1482 frame = getattr(generator, "gi_frame", None)
1483 if frame is not None:
1484 return generator.gi_frame.f_locals
1485 else:
1486 return {}
1487
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001488###############################################################################
1489### Function Signature Object (PEP 362)
1490###############################################################################
1491
1492
1493_WrapperDescriptor = type(type.__call__)
1494_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001495_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001496
1497_NonUserDefinedCallables = (_WrapperDescriptor,
1498 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001499 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001500 types.BuiltinFunctionType)
1501
1502
Yury Selivanov421f0c72014-01-29 12:05:40 -05001503def _signature_get_user_defined_method(cls, method_name):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001504 try:
1505 meth = getattr(cls, method_name)
1506 except AttributeError:
1507 return
1508 else:
1509 if not isinstance(meth, _NonUserDefinedCallables):
1510 # Once '__signature__' will be added to 'C'-level
1511 # callables, this check won't be necessary
1512 return meth
1513
1514
Yury Selivanov62560fb2014-01-28 12:26:24 -05001515def _signature_get_partial(wrapped_sig, partial, extra_args=()):
1516 # Internal helper to calculate how 'wrapped_sig' signature will
1517 # look like after applying a 'functools.partial' object (or alike)
1518 # on it.
1519
Yury Selivanov0fceaf42014-04-08 11:28:02 -04001520 old_params = wrapped_sig.parameters
1521 new_params = OrderedDict(old_params.items())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001522
1523 partial_args = partial.args or ()
1524 partial_keywords = partial.keywords or {}
1525
1526 if extra_args:
1527 partial_args = extra_args + partial_args
1528
1529 try:
1530 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1531 except TypeError as ex:
1532 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1533 raise ValueError(msg) from ex
1534
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001535
Yury Selivanov0fceaf42014-04-08 11:28:02 -04001536 transform_to_kwonly = False
1537 for param_name, param in old_params.items():
1538 try:
1539 arg_value = ba.arguments[param_name]
1540 except KeyError:
1541 pass
1542 else:
1543 if param.kind is _POSITIONAL_ONLY:
1544 # If positional-only parameter is bound by partial,
1545 # it effectively disappears from the signature
1546 new_params.pop(param_name)
1547 continue
1548
1549 if param.kind is _POSITIONAL_OR_KEYWORD:
1550 if param_name in partial_keywords:
1551 # This means that this parameter, and all parameters
1552 # after it should be keyword-only (and var-positional
1553 # should be removed). Here's why. Consider the following
1554 # function:
1555 # foo(a, b, *args, c):
1556 # pass
1557 #
1558 # "partial(foo, a='spam')" will have the following
1559 # signature: "(*, a='spam', b, c)". Because attempting
1560 # to call that partial with "(10, 20)" arguments will
1561 # raise a TypeError, saying that "a" argument received
1562 # multiple values.
1563 transform_to_kwonly = True
1564 # Set the new default value
1565 new_params[param_name] = param.replace(default=arg_value)
1566 else:
1567 # was passed as a positional argument
1568 new_params.pop(param.name)
1569 continue
1570
1571 if param.kind is _KEYWORD_ONLY:
1572 # Set the new default value
1573 new_params[param_name] = param.replace(default=arg_value)
1574
1575 if transform_to_kwonly:
1576 assert param.kind is not _POSITIONAL_ONLY
1577
1578 if param.kind is _POSITIONAL_OR_KEYWORD:
1579 new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1580 new_params[param_name] = new_param
1581 new_params.move_to_end(param_name)
1582 elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
1583 new_params.move_to_end(param_name)
1584 elif param.kind is _VAR_POSITIONAL:
1585 new_params.pop(param.name)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001586
1587 return wrapped_sig.replace(parameters=new_params.values())
1588
1589
Yury Selivanov62560fb2014-01-28 12:26:24 -05001590def _signature_bound_method(sig):
1591 # Internal helper to transform signatures for unbound
1592 # functions to bound methods
1593
1594 params = tuple(sig.parameters.values())
1595
1596 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1597 raise ValueError('invalid method signature')
1598
1599 kind = params[0].kind
1600 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1601 # Drop first parameter:
1602 # '(p1, p2[, ...])' -> '(p2[, ...])'
1603 params = params[1:]
1604 else:
1605 if kind is not _VAR_POSITIONAL:
1606 # Unless we add a new parameter type we never
1607 # get here
1608 raise ValueError('invalid argument type')
1609 # It's a var-positional parameter.
1610 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1611
1612 return sig.replace(parameters=params)
1613
1614
Yury Selivanovb77511d2014-01-29 10:46:14 -05001615def _signature_is_builtin(obj):
1616 # Internal helper to test if `obj` is a callable that might
1617 # support Argument Clinic's __text_signature__ protocol.
Yury Selivanov1d241832014-02-02 12:51:20 -05001618 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001619 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001620 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001621 # Can't test 'isinstance(type)' here, as it would
1622 # also be True for regular python classes
1623 obj in (type, object))
1624
1625
Yury Selivanov63da7c72014-01-31 14:48:37 -05001626def _signature_is_functionlike(obj):
1627 # Internal helper to test if `obj` is a duck type of FunctionType.
1628 # A good example of such objects are functions compiled with
1629 # Cython, which have all attributes that a pure Python function
1630 # would have, but have their code statically compiled.
1631
1632 if not callable(obj) or isclass(obj):
1633 # All function-like objects are obviously callables,
1634 # and not classes.
1635 return False
1636
1637 name = getattr(obj, '__name__', None)
1638 code = getattr(obj, '__code__', None)
1639 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
1640 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
1641 annotations = getattr(obj, '__annotations__', None)
1642
1643 return (isinstance(code, types.CodeType) and
1644 isinstance(name, str) and
1645 (defaults is None or isinstance(defaults, tuple)) and
1646 (kwdefaults is None or isinstance(kwdefaults, dict)) and
1647 isinstance(annotations, dict))
1648
1649
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001650def _signature_get_bound_param(spec):
1651 # Internal helper to get first parameter name from a
1652 # __text_signature__ of a builtin method, which should
1653 # be in the following format: '($param1, ...)'.
1654 # Assumptions are that the first argument won't have
1655 # a default value or an annotation.
1656
1657 assert spec.startswith('($')
1658
1659 pos = spec.find(',')
1660 if pos == -1:
1661 pos = spec.find(')')
1662
1663 cpos = spec.find(':')
1664 assert cpos == -1 or cpos > pos
1665
1666 cpos = spec.find('=')
1667 assert cpos == -1 or cpos > pos
1668
1669 return spec[2:pos]
1670
1671
Larry Hastings2623c8c2014-02-08 22:15:29 -08001672def _signature_strip_non_python_syntax(signature):
1673 """
1674 Takes a signature in Argument Clinic's extended signature format.
1675 Returns a tuple of three things:
1676 * that signature re-rendered in standard Python syntax,
1677 * the index of the "self" parameter (generally 0), or None if
1678 the function does not have a "self" parameter, and
1679 * the index of the last "positional only" parameter,
1680 or None if the signature has no positional-only parameters.
1681 """
1682
1683 if not signature:
1684 return signature, None, None
1685
1686 self_parameter = None
1687 last_positional_only = None
1688
1689 lines = [l.encode('ascii') for l in signature.split('\n')]
1690 generator = iter(lines).__next__
1691 token_stream = tokenize.tokenize(generator)
1692
1693 delayed_comma = False
1694 skip_next_comma = False
1695 text = []
1696 add = text.append
1697
1698 current_parameter = 0
1699 OP = token.OP
1700 ERRORTOKEN = token.ERRORTOKEN
1701
1702 # token stream always starts with ENCODING token, skip it
1703 t = next(token_stream)
1704 assert t.type == tokenize.ENCODING
1705
1706 for t in token_stream:
1707 type, string = t.type, t.string
1708
1709 if type == OP:
1710 if string == ',':
1711 if skip_next_comma:
1712 skip_next_comma = False
1713 else:
1714 assert not delayed_comma
1715 delayed_comma = True
1716 current_parameter += 1
1717 continue
1718
1719 if string == '/':
1720 assert not skip_next_comma
1721 assert last_positional_only is None
1722 skip_next_comma = True
1723 last_positional_only = current_parameter - 1
1724 continue
1725
1726 if (type == ERRORTOKEN) and (string == '$'):
1727 assert self_parameter is None
1728 self_parameter = current_parameter
1729 continue
1730
1731 if delayed_comma:
1732 delayed_comma = False
1733 if not ((type == OP) and (string == ')')):
1734 add(', ')
1735 add(string)
1736 if (string == ','):
1737 add(' ')
1738 clean_signature = ''.join(text)
1739 return clean_signature, self_parameter, last_positional_only
1740
1741
Yury Selivanovff385b82014-02-19 16:27:23 -05001742def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001743 # Internal helper to parse content of '__text_signature__'
1744 # and return a Signature based on it
1745 Parameter = cls._parameter_cls
1746
Larry Hastings2623c8c2014-02-08 22:15:29 -08001747 clean_signature, self_parameter, last_positional_only = \
1748 _signature_strip_non_python_syntax(s)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001749
Larry Hastings2623c8c2014-02-08 22:15:29 -08001750 program = "def foo" + clean_signature + ": pass"
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001751
1752 try:
Larry Hastings2623c8c2014-02-08 22:15:29 -08001753 module = ast.parse(program)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001754 except SyntaxError:
1755 module = None
1756
1757 if not isinstance(module, ast.Module):
1758 raise ValueError("{!r} builtin has invalid signature".format(obj))
1759
1760 f = module.body[0]
1761
1762 parameters = []
1763 empty = Parameter.empty
1764 invalid = object()
1765
1766 module = None
1767 module_dict = {}
1768 module_name = getattr(obj, '__module__', None)
1769 if module_name:
1770 module = sys.modules.get(module_name, None)
1771 if module:
1772 module_dict = module.__dict__
1773 sys_module_dict = sys.modules
1774
1775 def parse_name(node):
1776 assert isinstance(node, ast.arg)
1777 if node.annotation != None:
1778 raise ValueError("Annotations are not currently supported")
1779 return node.arg
1780
1781 def wrap_value(s):
1782 try:
1783 value = eval(s, module_dict)
1784 except NameError:
1785 try:
1786 value = eval(s, sys_module_dict)
1787 except NameError:
1788 raise RuntimeError()
1789
1790 if isinstance(value, str):
1791 return ast.Str(value)
1792 if isinstance(value, (int, float)):
1793 return ast.Num(value)
1794 if isinstance(value, bytes):
1795 return ast.Bytes(value)
1796 if value in (True, False, None):
1797 return ast.NameConstant(value)
1798 raise RuntimeError()
1799
1800 class RewriteSymbolics(ast.NodeTransformer):
1801 def visit_Attribute(self, node):
1802 a = []
1803 n = node
1804 while isinstance(n, ast.Attribute):
1805 a.append(n.attr)
1806 n = n.value
1807 if not isinstance(n, ast.Name):
1808 raise RuntimeError()
1809 a.append(n.id)
1810 value = ".".join(reversed(a))
1811 return wrap_value(value)
1812
1813 def visit_Name(self, node):
1814 if not isinstance(node.ctx, ast.Load):
1815 raise ValueError()
1816 return wrap_value(node.id)
1817
1818 def p(name_node, default_node, default=empty):
1819 name = parse_name(name_node)
1820 if name is invalid:
1821 return None
1822 if default_node and default_node is not _empty:
1823 try:
1824 default_node = RewriteSymbolics().visit(default_node)
1825 o = ast.literal_eval(default_node)
1826 except ValueError:
1827 o = invalid
1828 if o is invalid:
1829 return None
1830 default = o if o is not invalid else default
1831 parameters.append(Parameter(name, kind, default=default, annotation=empty))
1832
1833 # non-keyword-only parameters
1834 args = reversed(f.args.args)
1835 defaults = reversed(f.args.defaults)
1836 iter = itertools.zip_longest(args, defaults, fillvalue=None)
Larry Hastings2623c8c2014-02-08 22:15:29 -08001837 if last_positional_only is not None:
1838 kind = Parameter.POSITIONAL_ONLY
1839 else:
1840 kind = Parameter.POSITIONAL_OR_KEYWORD
1841 for i, (name, default) in enumerate(reversed(list(iter))):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001842 p(name, default)
Larry Hastings2623c8c2014-02-08 22:15:29 -08001843 if i == last_positional_only:
1844 kind = Parameter.POSITIONAL_OR_KEYWORD
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001845
1846 # *args
1847 if f.args.vararg:
1848 kind = Parameter.VAR_POSITIONAL
1849 p(f.args.vararg, empty)
1850
1851 # keyword-only arguments
1852 kind = Parameter.KEYWORD_ONLY
1853 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
1854 p(name, default)
1855
1856 # **kwargs
1857 if f.args.kwarg:
1858 kind = Parameter.VAR_KEYWORD
1859 p(f.args.kwarg, empty)
1860
Larry Hastings2623c8c2014-02-08 22:15:29 -08001861 if self_parameter is not None:
Yury Selivanovd224b6a2014-02-21 01:32:42 -05001862 # Possibly strip the bound argument:
1863 # - We *always* strip first bound argument if
1864 # it is a module.
1865 # - We don't strip first bound argument if
1866 # skip_bound_arg is False.
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001867 assert parameters
Yury Selivanovd224b6a2014-02-21 01:32:42 -05001868 _self = getattr(obj, '__self__', None)
1869 self_isbound = _self is not None
1870 self_ismodule = ismodule(_self)
1871 if self_isbound and (self_ismodule or skip_bound_arg):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001872 parameters.pop(0)
1873 else:
1874 # for builtins, self parameter is always positional-only!
1875 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
1876 parameters[0] = p
1877
1878 return cls(parameters, return_annotation=cls.empty)
1879
1880
Yury Selivanovff385b82014-02-19 16:27:23 -05001881def _signature_from_builtin(cls, func, skip_bound_arg=True):
1882 # Internal helper function to get signature for
1883 # builtin callables
1884 if not _signature_is_builtin(func):
1885 raise TypeError("{!r} is not a Python builtin "
1886 "function".format(func))
1887
1888 s = getattr(func, "__text_signature__", None)
1889 if not s:
1890 raise ValueError("no signature found for builtin {!r}".format(func))
1891
1892 return _signature_fromstr(cls, func, s, skip_bound_arg)
1893
1894
1895def _signature_internal(obj, follow_wrapper_chains=True, skip_bound_arg=True):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001896
1897 if not callable(obj):
1898 raise TypeError('{!r} is not a callable object'.format(obj))
1899
1900 if isinstance(obj, types.MethodType):
1901 # In this case we skip the first parameter of the underlying
1902 # function (usually `self` or `cls`).
Yury Selivanovff385b82014-02-19 16:27:23 -05001903 sig = _signature_internal(obj.__func__,
1904 follow_wrapper_chains,
1905 skip_bound_arg)
1906 if skip_bound_arg:
1907 return _signature_bound_method(sig)
1908 else:
1909 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001910
Nick Coghlane8c45d62013-07-28 20:00:01 +10001911 # Was this function wrapped by a decorator?
Yury Selivanovff385b82014-02-19 16:27:23 -05001912 if follow_wrapper_chains:
1913 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
Yury Selivanov46c759d2015-05-27 21:56:53 -04001914 if isinstance(obj, types.MethodType):
1915 # If the unwrapped object is a *method*, we might want to
1916 # skip its first parameter (self).
1917 # See test_signature_wrapped_bound_method for details.
1918 return _signature_internal(
1919 obj,
1920 follow_wrapper_chains=follow_wrapper_chains,
1921 skip_bound_arg=skip_bound_arg)
Nick Coghlane8c45d62013-07-28 20:00:01 +10001922
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001923 try:
1924 sig = obj.__signature__
1925 except AttributeError:
1926 pass
1927 else:
1928 if sig is not None:
Yury Selivanovc0f964f2014-06-23 10:21:04 -07001929 if not isinstance(sig, Signature):
1930 raise TypeError(
1931 'unexpected object {!r} in __signature__ '
1932 'attribute'.format(sig))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001933 return sig
1934
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001935 try:
1936 partialmethod = obj._partialmethod
1937 except AttributeError:
1938 pass
1939 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05001940 if isinstance(partialmethod, functools.partialmethod):
1941 # Unbound partialmethod (see functools.partialmethod)
1942 # This means, that we need to calculate the signature
1943 # as if it's a regular partial object, but taking into
1944 # account that the first positional argument
1945 # (usually `self`, or `cls`) will not be passed
1946 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001947
Yury Selivanovff385b82014-02-19 16:27:23 -05001948 wrapped_sig = _signature_internal(partialmethod.func,
1949 follow_wrapper_chains,
1950 skip_bound_arg)
Yury Selivanov0486f812014-01-29 12:18:59 -05001951 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001952
Yury Selivanov0486f812014-01-29 12:18:59 -05001953 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
1954 new_params = (first_wrapped_param,) + tuple(sig.parameters.values())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001955
Yury Selivanov0486f812014-01-29 12:18:59 -05001956 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001957
Yury Selivanov63da7c72014-01-31 14:48:37 -05001958 if isfunction(obj) or _signature_is_functionlike(obj):
1959 # If it's a pure Python function, or an object that is duck type
1960 # of a Python function (Cython functions, for instance), then:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001961 return Signature.from_function(obj)
1962
Yury Selivanov8dfb4572014-02-21 18:30:53 -05001963 if _signature_is_builtin(obj):
1964 return _signature_from_builtin(Signature, obj,
1965 skip_bound_arg=skip_bound_arg)
1966
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001967 if isinstance(obj, functools.partial):
Yury Selivanovff385b82014-02-19 16:27:23 -05001968 wrapped_sig = _signature_internal(obj.func,
1969 follow_wrapper_chains,
1970 skip_bound_arg)
Yury Selivanov62560fb2014-01-28 12:26:24 -05001971 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001972
1973 sig = None
1974 if isinstance(obj, type):
1975 # obj is a class or a metaclass
1976
1977 # First, let's see if it has an overloaded __call__ defined
1978 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05001979 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001980 if call is not None:
Yury Selivanovff385b82014-02-19 16:27:23 -05001981 sig = _signature_internal(call,
1982 follow_wrapper_chains,
1983 skip_bound_arg)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001984 else:
1985 # Now we check if the 'obj' class has a '__new__' method
Yury Selivanov421f0c72014-01-29 12:05:40 -05001986 new = _signature_get_user_defined_method(obj, '__new__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001987 if new is not None:
Yury Selivanovff385b82014-02-19 16:27:23 -05001988 sig = _signature_internal(new,
1989 follow_wrapper_chains,
1990 skip_bound_arg)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001991 else:
1992 # Finally, we should have at least __init__ implemented
Yury Selivanov421f0c72014-01-29 12:05:40 -05001993 init = _signature_get_user_defined_method(obj, '__init__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001994 if init is not None:
Yury Selivanovff385b82014-02-19 16:27:23 -05001995 sig = _signature_internal(init,
1996 follow_wrapper_chains,
1997 skip_bound_arg)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05001998
1999 if sig is None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002000 # At this point we know, that `obj` is a class, with no user-
2001 # defined '__init__', '__new__', or class-level '__call__'
2002
Larry Hastings2623c8c2014-02-08 22:15:29 -08002003 for base in obj.__mro__[:-1]:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002004 # Since '__text_signature__' is implemented as a
2005 # descriptor that extracts text signature from the
2006 # class docstring, if 'obj' is derived from a builtin
2007 # class, its own '__text_signature__' may be 'None'.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002008 # Therefore, we go through the MRO (except the last
2009 # class in there, which is 'object') to find the first
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002010 # class with non-empty text signature.
2011 try:
2012 text_sig = base.__text_signature__
2013 except AttributeError:
2014 pass
2015 else:
2016 if text_sig:
2017 # If 'obj' class has a __text_signature__ attribute:
2018 # return a signature based on it
2019 return _signature_fromstr(Signature, obj, text_sig)
2020
2021 # No '__text_signature__' was found for the 'obj' class.
2022 # Last option is to check if its '__init__' is
2023 # object.__init__ or type.__init__.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002024 if type not in obj.__mro__:
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002025 # We have a class (not metaclass), but no user-defined
2026 # __init__ or __new__ for it
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002027 if obj.__init__ is object.__init__:
2028 # Return a signature of 'object' builtin.
2029 return signature(object)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002030
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002031 elif not isinstance(obj, _NonUserDefinedCallables):
2032 # An object with __call__
2033 # We also check that the 'obj' is not an instance of
2034 # _WrapperDescriptor or _MethodWrapper to avoid
2035 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05002036 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002037 if call is not None:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002038 try:
Yury Selivanovff385b82014-02-19 16:27:23 -05002039 sig = _signature_internal(call,
2040 follow_wrapper_chains,
2041 skip_bound_arg)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002042 except ValueError as ex:
2043 msg = 'no signature found for {!r}'.format(obj)
2044 raise ValueError(msg) from ex
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002045
2046 if sig is not None:
2047 # For classes and objects we skip the first parameter of their
2048 # __call__, __new__, or __init__ methods
Yury Selivanovff385b82014-02-19 16:27:23 -05002049 if skip_bound_arg:
2050 return _signature_bound_method(sig)
2051 else:
2052 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002053
2054 if isinstance(obj, types.BuiltinFunctionType):
2055 # Raise a nicer error message for builtins
2056 msg = 'no signature found for builtin function {!r}'.format(obj)
2057 raise ValueError(msg)
2058
2059 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2060
Yury Selivanovff385b82014-02-19 16:27:23 -05002061def signature(obj):
2062 '''Get a signature object for the passed callable.'''
2063 return _signature_internal(obj)
2064
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002065
2066class _void:
2067 '''A private marker - used in Parameter & Signature'''
2068
2069
2070class _empty:
2071 pass
2072
2073
2074class _ParameterKind(int):
2075 def __new__(self, *args, name):
2076 obj = int.__new__(self, *args)
2077 obj._name = name
2078 return obj
2079
2080 def __str__(self):
2081 return self._name
2082
2083 def __repr__(self):
2084 return '<_ParameterKind: {!r}>'.format(self._name)
2085
2086
2087_POSITIONAL_ONLY = _ParameterKind(0, name='POSITIONAL_ONLY')
2088_POSITIONAL_OR_KEYWORD = _ParameterKind(1, name='POSITIONAL_OR_KEYWORD')
2089_VAR_POSITIONAL = _ParameterKind(2, name='VAR_POSITIONAL')
2090_KEYWORD_ONLY = _ParameterKind(3, name='KEYWORD_ONLY')
2091_VAR_KEYWORD = _ParameterKind(4, name='VAR_KEYWORD')
2092
2093
2094class Parameter:
2095 '''Represents a parameter in a function signature.
2096
2097 Has the following public attributes:
2098
2099 * name : str
2100 The name of the parameter as a string.
2101 * default : object
2102 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002103 parameter has no default value, this attribute is set to
2104 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002105 * annotation
2106 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002107 parameter has no annotation, this attribute is set to
2108 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002109 * kind : str
2110 Describes how argument values are bound to the parameter.
2111 Possible values: `Parameter.POSITIONAL_ONLY`,
2112 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
2113 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
2114 '''
2115
Yury Selivanov0fceaf42014-04-08 11:28:02 -04002116 __slots__ = ('_name', '_kind', '_default', '_annotation')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002117
2118 POSITIONAL_ONLY = _POSITIONAL_ONLY
2119 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
2120 VAR_POSITIONAL = _VAR_POSITIONAL
2121 KEYWORD_ONLY = _KEYWORD_ONLY
2122 VAR_KEYWORD = _VAR_KEYWORD
2123
2124 empty = _empty
2125
Yury Selivanov0fceaf42014-04-08 11:28:02 -04002126 def __init__(self, name, kind, *, default=_empty, annotation=_empty):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002127
2128 if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
2129 _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
2130 raise ValueError("invalid value for 'Parameter.kind' attribute")
2131 self._kind = kind
2132
2133 if default is not _empty:
2134 if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2135 msg = '{} parameters cannot have default values'.format(kind)
2136 raise ValueError(msg)
2137 self._default = default
2138 self._annotation = annotation
2139
Yury Selivanov2393dca2014-01-27 15:07:58 -05002140 if name is _empty:
2141 raise ValueError('name is a required attribute for Parameter')
2142
2143 if not isinstance(name, str):
2144 raise TypeError("name must be a str, not a {!r}".format(name))
2145
2146 if not name.isidentifier():
2147 raise ValueError('{!r} is not a valid parameter name'.format(name))
2148
2149 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002150
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002151 @property
2152 def name(self):
2153 return self._name
2154
2155 @property
2156 def default(self):
2157 return self._default
2158
2159 @property
2160 def annotation(self):
2161 return self._annotation
2162
2163 @property
2164 def kind(self):
2165 return self._kind
2166
Yury Selivanov0fceaf42014-04-08 11:28:02 -04002167 def replace(self, *, name=_void, kind=_void,
2168 annotation=_void, default=_void):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002169 '''Creates a customized copy of the Parameter.'''
2170
2171 if name is _void:
2172 name = self._name
2173
2174 if kind is _void:
2175 kind = self._kind
2176
2177 if annotation is _void:
2178 annotation = self._annotation
2179
2180 if default is _void:
2181 default = self._default
2182
Yury Selivanov0fceaf42014-04-08 11:28:02 -04002183 return type(self)(name, kind, default=default, annotation=annotation)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002184
2185 def __str__(self):
2186 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002187 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002188
2189 # Add annotation and default value
2190 if self._annotation is not _empty:
2191 formatted = '{}:{}'.format(formatted,
2192 formatannotation(self._annotation))
2193
2194 if self._default is not _empty:
2195 formatted = '{}={}'.format(formatted, repr(self._default))
2196
2197 if kind == _VAR_POSITIONAL:
2198 formatted = '*' + formatted
2199 elif kind == _VAR_KEYWORD:
2200 formatted = '**' + formatted
2201
2202 return formatted
2203
2204 def __repr__(self):
2205 return '<{} at {:#x} {!r}>'.format(self.__class__.__name__,
2206 id(self), self.name)
2207
2208 def __eq__(self, other):
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002209 if not isinstance(other, Parameter):
2210 return NotImplemented
2211 return (self._name == other._name and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002212 self._kind == other._kind and
2213 self._default == other._default and
2214 self._annotation == other._annotation)
2215
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002216
2217class BoundArguments:
2218 '''Result of `Signature.bind` call. Holds the mapping of arguments
2219 to the function's parameters.
2220
2221 Has the following public attributes:
2222
2223 * arguments : OrderedDict
2224 An ordered mutable mapping of parameters' names to arguments' values.
2225 Does not contain arguments' default values.
2226 * signature : Signature
2227 The Signature object that created this instance.
2228 * args : tuple
2229 Tuple of positional arguments values.
2230 * kwargs : dict
2231 Dict of keyword arguments values.
2232 '''
2233
2234 def __init__(self, signature, arguments):
2235 self.arguments = arguments
2236 self._signature = signature
2237
2238 @property
2239 def signature(self):
2240 return self._signature
2241
2242 @property
2243 def args(self):
2244 args = []
2245 for param_name, param in self._signature.parameters.items():
Yury Selivanov0fceaf42014-04-08 11:28:02 -04002246 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002247 break
2248
2249 try:
2250 arg = self.arguments[param_name]
2251 except KeyError:
2252 # We're done here. Other arguments
2253 # will be mapped in 'BoundArguments.kwargs'
2254 break
2255 else:
2256 if param.kind == _VAR_POSITIONAL:
2257 # *args
2258 args.extend(arg)
2259 else:
2260 # plain argument
2261 args.append(arg)
2262
2263 return tuple(args)
2264
2265 @property
2266 def kwargs(self):
2267 kwargs = {}
2268 kwargs_started = False
2269 for param_name, param in self._signature.parameters.items():
2270 if not kwargs_started:
Yury Selivanov0fceaf42014-04-08 11:28:02 -04002271 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002272 kwargs_started = True
2273 else:
2274 if param_name not in self.arguments:
2275 kwargs_started = True
2276 continue
2277
2278 if not kwargs_started:
2279 continue
2280
2281 try:
2282 arg = self.arguments[param_name]
2283 except KeyError:
2284 pass
2285 else:
2286 if param.kind == _VAR_KEYWORD:
2287 # **kwargs
2288 kwargs.update(arg)
2289 else:
2290 # plain keyword argument
2291 kwargs[param_name] = arg
2292
2293 return kwargs
2294
2295 def __eq__(self, other):
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002296 if not isinstance(other, BoundArguments):
2297 return NotImplemented
2298 return (self.signature == other.signature and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002299 self.arguments == other.arguments)
2300
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002301
2302class Signature:
2303 '''A Signature object represents the overall signature of a function.
2304 It stores a Parameter object for each parameter accepted by the
2305 function, as well as information specific to the function itself.
2306
2307 A Signature object has the following public attributes and methods:
2308
2309 * parameters : OrderedDict
2310 An ordered mapping of parameters' names to the corresponding
2311 Parameter objects (keyword-only arguments are in the same order
2312 as listed in `code.co_varnames`).
2313 * return_annotation : object
2314 The annotation for the return type of the function if specified.
2315 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002316 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002317 * bind(*args, **kwargs) -> BoundArguments
2318 Creates a mapping from positional and keyword arguments to
2319 parameters.
2320 * bind_partial(*args, **kwargs) -> BoundArguments
2321 Creates a partial mapping from positional and keyword arguments
2322 to parameters (simulating 'functools.partial' behavior.)
2323 '''
2324
2325 __slots__ = ('_return_annotation', '_parameters')
2326
2327 _parameter_cls = Parameter
2328 _bound_arguments_cls = BoundArguments
2329
2330 empty = _empty
2331
2332 def __init__(self, parameters=None, *, return_annotation=_empty,
2333 __validate_parameters__=True):
2334 '''Constructs Signature from the given list of Parameter
2335 objects and 'return_annotation'. All arguments are optional.
2336 '''
2337
2338 if parameters is None:
2339 params = OrderedDict()
2340 else:
2341 if __validate_parameters__:
2342 params = OrderedDict()
2343 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002344 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002345
2346 for idx, param in enumerate(parameters):
2347 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002348 name = param.name
2349
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002350 if kind < top_kind:
Yury Selivanov0fceaf42014-04-08 11:28:02 -04002351 msg = 'wrong parameter order: {!r} before {!r}'
Yury Selivanov2393dca2014-01-27 15:07:58 -05002352 msg = msg.format(top_kind, kind)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002353 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002354 elif kind > top_kind:
2355 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002356 top_kind = kind
2357
Yury Selivanov0fceaf42014-04-08 11:28:02 -04002358 if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
Yury Selivanov07a9e452014-01-29 10:58:16 -05002359 if param.default is _empty:
2360 if kind_defaults:
2361 # No default for this parameter, but the
2362 # previous parameter of the same kind had
2363 # a default
2364 msg = 'non-default argument follows default ' \
2365 'argument'
2366 raise ValueError(msg)
2367 else:
2368 # There is a default for this parameter.
2369 kind_defaults = True
2370
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002371 if name in params:
2372 msg = 'duplicate parameter name: {!r}'.format(name)
2373 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002374
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002375 params[name] = param
2376 else:
2377 params = OrderedDict(((param.name, param)
2378 for param in parameters))
2379
2380 self._parameters = types.MappingProxyType(params)
2381 self._return_annotation = return_annotation
2382
2383 @classmethod
2384 def from_function(cls, func):
2385 '''Constructs Signature for the given python function'''
2386
Yury Selivanov5334bcd2014-01-31 15:21:51 -05002387 is_duck_function = False
2388 if not isfunction(func):
2389 if _signature_is_functionlike(func):
2390 is_duck_function = True
2391 else:
2392 # If it's not a pure Python function, and not a duck type
2393 # of pure function:
2394 raise TypeError('{!r} is not a Python function'.format(func))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002395
2396 Parameter = cls._parameter_cls
2397
2398 # Parameter information.
2399 func_code = func.__code__
2400 pos_count = func_code.co_argcount
2401 arg_names = func_code.co_varnames
2402 positional = tuple(arg_names[:pos_count])
2403 keyword_only_count = func_code.co_kwonlyargcount
2404 keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)]
2405 annotations = func.__annotations__
2406 defaults = func.__defaults__
2407 kwdefaults = func.__kwdefaults__
2408
2409 if defaults:
2410 pos_default_count = len(defaults)
2411 else:
2412 pos_default_count = 0
2413
2414 parameters = []
2415
2416 # Non-keyword-only parameters w/o defaults.
2417 non_default_count = pos_count - pos_default_count
2418 for name in positional[:non_default_count]:
2419 annotation = annotations.get(name, _empty)
2420 parameters.append(Parameter(name, annotation=annotation,
2421 kind=_POSITIONAL_OR_KEYWORD))
2422
2423 # ... w/ defaults.
2424 for offset, name in enumerate(positional[non_default_count:]):
2425 annotation = annotations.get(name, _empty)
2426 parameters.append(Parameter(name, annotation=annotation,
2427 kind=_POSITIONAL_OR_KEYWORD,
2428 default=defaults[offset]))
2429
2430 # *args
Yury Selivanov89ca85c2014-01-29 16:50:40 -05002431 if func_code.co_flags & CO_VARARGS:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002432 name = arg_names[pos_count + keyword_only_count]
2433 annotation = annotations.get(name, _empty)
2434 parameters.append(Parameter(name, annotation=annotation,
2435 kind=_VAR_POSITIONAL))
2436
2437 # Keyword-only parameters.
2438 for name in keyword_only:
2439 default = _empty
2440 if kwdefaults is not None:
2441 default = kwdefaults.get(name, _empty)
2442
2443 annotation = annotations.get(name, _empty)
2444 parameters.append(Parameter(name, annotation=annotation,
2445 kind=_KEYWORD_ONLY,
2446 default=default))
2447 # **kwargs
Yury Selivanov89ca85c2014-01-29 16:50:40 -05002448 if func_code.co_flags & CO_VARKEYWORDS:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002449 index = pos_count + keyword_only_count
Yury Selivanov89ca85c2014-01-29 16:50:40 -05002450 if func_code.co_flags & CO_VARARGS:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002451 index += 1
2452
2453 name = arg_names[index]
2454 annotation = annotations.get(name, _empty)
2455 parameters.append(Parameter(name, annotation=annotation,
2456 kind=_VAR_KEYWORD))
2457
Yury Selivanov5334bcd2014-01-31 15:21:51 -05002458 # Is 'func' is a pure Python function - don't validate the
2459 # parameters list (for correct order and defaults), it should be OK.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002460 return cls(parameters,
2461 return_annotation=annotations.get('return', _empty),
Yury Selivanov5334bcd2014-01-31 15:21:51 -05002462 __validate_parameters__=is_duck_function)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002463
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002464 @classmethod
2465 def from_builtin(cls, func):
Yury Selivanovff385b82014-02-19 16:27:23 -05002466 return _signature_from_builtin(cls, func)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002467
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002468 @property
2469 def parameters(self):
2470 return self._parameters
2471
2472 @property
2473 def return_annotation(self):
2474 return self._return_annotation
2475
2476 def replace(self, *, parameters=_void, return_annotation=_void):
2477 '''Creates a customized copy of the Signature.
2478 Pass 'parameters' and/or 'return_annotation' arguments
2479 to override them in the new copy.
2480 '''
2481
2482 if parameters is _void:
2483 parameters = self.parameters.values()
2484
2485 if return_annotation is _void:
2486 return_annotation = self._return_annotation
2487
2488 return type(self)(parameters,
2489 return_annotation=return_annotation)
2490
2491 def __eq__(self, other):
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002492 if not isinstance(other, Signature):
2493 return NotImplemented
2494 if (self.return_annotation != other.return_annotation or
2495 len(self.parameters) != len(other.parameters)):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002496 return False
2497
2498 other_positions = {param: idx
2499 for idx, param in enumerate(other.parameters.keys())}
2500
2501 for idx, (param_name, param) in enumerate(self.parameters.items()):
2502 if param.kind == _KEYWORD_ONLY:
2503 try:
2504 other_param = other.parameters[param_name]
2505 except KeyError:
2506 return False
2507 else:
2508 if param != other_param:
2509 return False
2510 else:
2511 try:
2512 other_idx = other_positions[param_name]
2513 except KeyError:
2514 return False
2515 else:
2516 if (idx != other_idx or
2517 param != other.parameters[param_name]):
2518 return False
2519
2520 return True
2521
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002522 def _bind(self, args, kwargs, *, partial=False):
2523 '''Private method. Don't use directly.'''
2524
2525 arguments = OrderedDict()
2526
2527 parameters = iter(self.parameters.values())
2528 parameters_ex = ()
2529 arg_vals = iter(args)
2530
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002531 while True:
2532 # Let's iterate through the positional arguments and corresponding
2533 # parameters
2534 try:
2535 arg_val = next(arg_vals)
2536 except StopIteration:
2537 # No more positional arguments
2538 try:
2539 param = next(parameters)
2540 except StopIteration:
2541 # No more parameters. That's it. Just need to check that
2542 # we have no `kwargs` after this while loop
2543 break
2544 else:
2545 if param.kind == _VAR_POSITIONAL:
2546 # That's OK, just empty *args. Let's start parsing
2547 # kwargs
2548 break
2549 elif param.name in kwargs:
2550 if param.kind == _POSITIONAL_ONLY:
2551 msg = '{arg!r} parameter is positional only, ' \
2552 'but was passed as a keyword'
2553 msg = msg.format(arg=param.name)
2554 raise TypeError(msg) from None
2555 parameters_ex = (param,)
2556 break
2557 elif (param.kind == _VAR_KEYWORD or
2558 param.default is not _empty):
2559 # That's fine too - we have a default value for this
2560 # parameter. So, lets start parsing `kwargs`, starting
2561 # with the current parameter
2562 parameters_ex = (param,)
2563 break
2564 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002565 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
2566 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002567 if partial:
2568 parameters_ex = (param,)
2569 break
2570 else:
2571 msg = '{arg!r} parameter lacking default value'
2572 msg = msg.format(arg=param.name)
2573 raise TypeError(msg) from None
2574 else:
2575 # We have a positional argument to process
2576 try:
2577 param = next(parameters)
2578 except StopIteration:
2579 raise TypeError('too many positional arguments') from None
2580 else:
2581 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
2582 # Looks like we have no parameter for this positional
2583 # argument
2584 raise TypeError('too many positional arguments')
2585
2586 if param.kind == _VAR_POSITIONAL:
2587 # We have an '*args'-like argument, let's fill it with
2588 # all positional arguments we have left and move on to
2589 # the next phase
2590 values = [arg_val]
2591 values.extend(arg_vals)
2592 arguments[param.name] = tuple(values)
2593 break
2594
2595 if param.name in kwargs:
2596 raise TypeError('multiple values for argument '
2597 '{arg!r}'.format(arg=param.name))
2598
2599 arguments[param.name] = arg_val
2600
2601 # Now, we iterate through the remaining parameters to process
2602 # keyword arguments
2603 kwargs_param = None
2604 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002605 if param.kind == _VAR_KEYWORD:
2606 # Memorize that we have a '**kwargs'-like parameter
2607 kwargs_param = param
2608 continue
2609
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002610 if param.kind == _VAR_POSITIONAL:
2611 # Named arguments don't refer to '*args'-like parameters.
2612 # We only arrive here if the positional arguments ended
2613 # before reaching the last parameter before *args.
2614 continue
2615
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002616 param_name = param.name
2617 try:
2618 arg_val = kwargs.pop(param_name)
2619 except KeyError:
2620 # We have no value for this parameter. It's fine though,
2621 # if it has a default value, or it is an '*args'-like
2622 # parameter, left alone by the processing of positional
2623 # arguments.
2624 if (not partial and param.kind != _VAR_POSITIONAL and
2625 param.default is _empty):
2626 raise TypeError('{arg!r} parameter lacking default value'. \
2627 format(arg=param_name)) from None
2628
2629 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05002630 if param.kind == _POSITIONAL_ONLY:
2631 # This should never happen in case of a properly built
2632 # Signature object (but let's have this check here
2633 # to ensure correct behaviour just in case)
2634 raise TypeError('{arg!r} parameter is positional only, '
2635 'but was passed as a keyword'. \
2636 format(arg=param.name))
2637
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002638 arguments[param_name] = arg_val
2639
2640 if kwargs:
2641 if kwargs_param is not None:
2642 # Process our '**kwargs'-like parameter
2643 arguments[kwargs_param.name] = kwargs
2644 else:
2645 raise TypeError('too many keyword arguments')
2646
2647 return self._bound_arguments_cls(self, arguments)
2648
Yury Selivanovc45873e2014-01-29 12:10:27 -05002649 def bind(*args, **kwargs):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002650 '''Get a BoundArguments object, that maps the passed `args`
2651 and `kwargs` to the function's signature. Raises `TypeError`
2652 if the passed arguments can not be bound.
2653 '''
Yury Selivanovc45873e2014-01-29 12:10:27 -05002654 return args[0]._bind(args[1:], kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002655
Yury Selivanovc45873e2014-01-29 12:10:27 -05002656 def bind_partial(*args, **kwargs):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002657 '''Get a BoundArguments object, that partially maps the
2658 passed `args` and `kwargs` to the function's signature.
2659 Raises `TypeError` if the passed arguments can not be bound.
2660 '''
Yury Selivanovc45873e2014-01-29 12:10:27 -05002661 return args[0]._bind(args[1:], kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002662
2663 def __str__(self):
2664 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05002665 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002666 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05002667 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002668 formatted = str(param)
2669
2670 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002671
2672 if kind == _POSITIONAL_ONLY:
2673 render_pos_only_separator = True
2674 elif render_pos_only_separator:
2675 # It's not a positional-only parameter, and the flag
2676 # is set to 'True' (there were pos-only params before.)
2677 result.append('/')
2678 render_pos_only_separator = False
2679
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002680 if kind == _VAR_POSITIONAL:
2681 # OK, we have an '*args'-like parameter, so we won't need
2682 # a '*' to separate keyword-only arguments
2683 render_kw_only_separator = False
2684 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
2685 # We have a keyword-only parameter to render and we haven't
2686 # rendered an '*args'-like parameter before, so add a '*'
2687 # separator to the parameters list ("foo(arg1, *, arg2)" case)
2688 result.append('*')
2689 # This condition should be only triggered once, so
2690 # reset the flag
2691 render_kw_only_separator = False
2692
2693 result.append(formatted)
2694
Yury Selivanov2393dca2014-01-27 15:07:58 -05002695 if render_pos_only_separator:
2696 # There were only positional-only parameters, hence the
2697 # flag was not reset to 'False'
2698 result.append('/')
2699
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002700 rendered = '({})'.format(', '.join(result))
2701
2702 if self.return_annotation is not _empty:
2703 anno = formatannotation(self.return_annotation)
2704 rendered += ' -> {}'.format(anno)
2705
2706 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10002707
2708def _main():
2709 """ Logic for inspecting an object given at command line """
2710 import argparse
2711 import importlib
2712
2713 parser = argparse.ArgumentParser()
2714 parser.add_argument(
2715 'object',
2716 help="The object to be analysed. "
2717 "It supports the 'module:qualname' syntax")
2718 parser.add_argument(
2719 '-d', '--details', action='store_true',
2720 help='Display info about the module rather than its source code')
2721
2722 args = parser.parse_args()
2723
2724 target = args.object
2725 mod_name, has_attrs, attrs = target.partition(":")
2726 try:
2727 obj = module = importlib.import_module(mod_name)
2728 except Exception as exc:
2729 msg = "Failed to import {} ({}: {})".format(mod_name,
2730 type(exc).__name__,
2731 exc)
2732 print(msg, file=sys.stderr)
2733 exit(2)
2734
2735 if has_attrs:
2736 parts = attrs.split(".")
2737 obj = module
2738 for part in parts:
2739 obj = getattr(obj, part)
2740
2741 if module.__name__ in sys.builtin_module_names:
2742 print("Can't get info for builtin modules.", file=sys.stderr)
2743 exit(1)
2744
2745 if args.details:
2746 print('Target: {}'.format(target))
2747 print('Origin: {}'.format(getsourcefile(module)))
2748 print('Cached: {}'.format(module.__cached__))
2749 if obj is module:
2750 print('Loader: {}'.format(repr(module.__loader__)))
2751 if hasattr(module, '__path__'):
2752 print('Submodule search path: {}'.format(module.__path__))
2753 else:
2754 try:
2755 __, lineno = findsource(obj)
2756 except Exception:
2757 pass
2758 else:
2759 print('Line: {}'.format(lineno))
2760
2761 print('\n')
2762 else:
2763 print(getsource(obj))
2764
2765
2766if __name__ == "__main__":
2767 _main()