blob: e52d86e5cc9cc46a4a4954d0d63c43b2f0ec2f9b [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
Yury Selivanov0cf3ed62014-04-01 10:17:08 -040020 getfullargspec() - same, with support for Python 3 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
Antoine Pitroua8723a02015-04-15 00:41:29 +020035import dis
Yury Selivanov75445082015-05-11 22:57:16 -040036import collections.abc
Yury Selivanov21e83a52014-03-27 11:23:13 -040037import enum
Brett Cannoncb66eb02012-05-11 12:58:42 -040038import importlib.machinery
39import itertools
Christian Heimes7131fd92008-02-19 14:21:46 +000040import linecache
Brett Cannoncb66eb02012-05-11 12:58:42 -040041import os
42import re
43import sys
44import tokenize
Larry Hastings2623c8c2014-02-08 22:15:29 -080045import token
Brett Cannoncb66eb02012-05-11 12:58:42 -040046import types
Brett Cannon2b88fcf2012-06-02 22:28:42 -040047import warnings
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070048import functools
Nick Coghlan2f92e542012-06-23 19:39:55 +100049import builtins
Raymond Hettingera1a992c2005-03-11 06:46:45 +000050from operator import attrgetter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070051from collections import namedtuple, OrderedDict
Nick Coghlan09c81232010-08-17 10:18:16 +000052
53# Create constants for the compiler flags in Include/code.h
Antoine Pitroua8723a02015-04-15 00:41:29 +020054# We try to get them from dis to avoid duplication
55mod_dict = globals()
56for k, v in dis.COMPILER_FLAG_NAMES.items():
57 mod_dict["CO_" + v] = k
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000058
Christian Heimesbe5b30b2008-03-03 19:18:51 +000059# See Include/object.h
60TPFLAGS_IS_ABSTRACT = 1 << 20
61
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000062# ----------------------------------------------------------- type-checking
63def ismodule(object):
64 """Return true if the object is a module.
65
66 Module objects provide these attributes:
Barry Warsaw28a691b2010-04-17 00:19:56 +000067 __cached__ pathname to byte compiled file
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000068 __doc__ documentation string
69 __file__ filename (missing for built-in modules)"""
Tim Peters28bc59f2001-09-16 08:40:16 +000070 return isinstance(object, types.ModuleType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000071
72def isclass(object):
73 """Return true if the object is a class.
74
75 Class objects provide these attributes:
76 __doc__ documentation string
77 __module__ name of module in which this class was defined"""
Benjamin Petersonc4656002009-01-17 22:41:18 +000078 return isinstance(object, type)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000079
80def ismethod(object):
81 """Return true if the object is an instance method.
82
83 Instance method objects provide these attributes:
84 __doc__ documentation string
85 __name__ name with which this method was defined
Christian Heimesff737952007-11-27 10:40:20 +000086 __func__ function object containing implementation of method
87 __self__ instance to which this method is bound"""
Tim Peters28bc59f2001-09-16 08:40:16 +000088 return isinstance(object, types.MethodType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000089
Tim Peters536d2262001-09-20 05:13:38 +000090def ismethoddescriptor(object):
Tim Petersf1d90b92001-09-20 05:47:55 +000091 """Return true if the object is a method descriptor.
92
93 But not if ismethod() or isclass() or isfunction() are true.
Tim Peters536d2262001-09-20 05:13:38 +000094
95 This is new in Python 2.2, and, for example, is true of int.__add__.
96 An object passing this test has a __get__ attribute but not a __set__
97 attribute, but beyond that the set of attributes varies. __name__ is
98 usually sensible, and __doc__ often is.
99
Tim Petersf1d90b92001-09-20 05:47:55 +0000100 Methods implemented via descriptors that also pass one of the other
101 tests return false from the ismethoddescriptor() test, simply because
102 the other tests promise more -- you can, e.g., count on having the
Christian Heimesff737952007-11-27 10:40:20 +0000103 __func__ attribute (etc) when an object passes ismethod()."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100104 if isclass(object) or ismethod(object) or isfunction(object):
105 # mutual exclusion
106 return False
107 tp = type(object)
108 return hasattr(tp, "__get__") and not hasattr(tp, "__set__")
Tim Peters536d2262001-09-20 05:13:38 +0000109
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000110def isdatadescriptor(object):
111 """Return true if the object is a data descriptor.
112
113 Data descriptors have both a __get__ and a __set__ attribute. Examples are
114 properties (defined in Python) and getsets and members (defined in C).
115 Typically, data descriptors will also have __name__ and __doc__ attributes
116 (properties, getsets, and members have both of these attributes), but this
117 is not guaranteed."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100118 if isclass(object) or ismethod(object) or isfunction(object):
119 # mutual exclusion
120 return False
121 tp = type(object)
122 return hasattr(tp, "__set__") and hasattr(tp, "__get__")
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000123
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000124if hasattr(types, 'MemberDescriptorType'):
125 # CPython and equivalent
126 def ismemberdescriptor(object):
127 """Return true if the object is a member descriptor.
128
129 Member descriptors are specialized descriptors defined in extension
130 modules."""
131 return isinstance(object, types.MemberDescriptorType)
132else:
133 # Other implementations
134 def ismemberdescriptor(object):
135 """Return true if the object is a member descriptor.
136
137 Member descriptors are specialized descriptors defined in extension
138 modules."""
139 return False
140
141if hasattr(types, 'GetSetDescriptorType'):
142 # CPython and equivalent
143 def isgetsetdescriptor(object):
144 """Return true if the object is a getset descriptor.
145
146 getset descriptors are specialized descriptors defined in extension
147 modules."""
148 return isinstance(object, types.GetSetDescriptorType)
149else:
150 # Other implementations
151 def isgetsetdescriptor(object):
152 """Return true if the object is a getset descriptor.
153
154 getset descriptors are specialized descriptors defined in extension
155 modules."""
156 return False
157
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000158def isfunction(object):
159 """Return true if the object is a user-defined function.
160
161 Function objects provide these attributes:
162 __doc__ documentation string
163 __name__ name with which this function was defined
Neal Norwitz221085d2007-02-25 20:55:47 +0000164 __code__ code object containing compiled function bytecode
165 __defaults__ tuple of any default values for arguments
166 __globals__ global namespace in which this function was defined
167 __annotations__ dict of parameter annotations
168 __kwdefaults__ dict of keyword only parameters with defaults"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000169 return isinstance(object, types.FunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000170
Christian Heimes7131fd92008-02-19 14:21:46 +0000171def isgeneratorfunction(object):
172 """Return true if the object is a user-defined generator function.
173
174 Generator function objects provides same attributes as functions.
175
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000176 See help(isfunction) for attributes listing."""
Georg Brandlb1441c72009-01-03 22:33:39 +0000177 return bool((isfunction(object) or ismethod(object)) and
Yury Selivanov75445082015-05-11 22:57:16 -0400178 object.__code__.co_flags & CO_GENERATOR and
179 not object.__code__.co_flags & CO_COROUTINE)
180
181def iscoroutinefunction(object):
182 """Return true if the object is a coroutine function.
183
184 Coroutine functions are defined with "async def" syntax,
185 or generators decorated with "types.coroutine".
186 """
187 return bool((isfunction(object) or ismethod(object)) and
188 object.__code__.co_flags & (CO_ITERABLE_COROUTINE |
189 CO_COROUTINE))
190
191def isawaitable(object):
192 """Return true if the object can be used in "await" expression."""
193 return isinstance(object, collections.abc.Awaitable)
Christian Heimes7131fd92008-02-19 14:21:46 +0000194
195def isgenerator(object):
196 """Return true if the object is a generator.
197
198 Generator objects provide these attributes:
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300199 __iter__ defined to support iteration over container
Christian Heimes7131fd92008-02-19 14:21:46 +0000200 close raises a new GeneratorExit exception inside the
201 generator to terminate the iteration
202 gi_code code object
203 gi_frame frame object or possibly None once the generator has
204 been exhausted
205 gi_running set to 1 when generator is executing, 0 otherwise
206 next return the next item from the container
207 send resumes the generator and "sends" a value that becomes
208 the result of the current yield-expression
209 throw used to raise an exception inside the generator"""
Yury Selivanov75445082015-05-11 22:57:16 -0400210 return (isinstance(object, types.GeneratorType) and
211 not object.gi_code.co_flags & CO_COROUTINE)
212
213def iscoroutine(object):
214 """Return true if the object is a coroutine."""
215 return (isinstance(object, types.GeneratorType) and
216 object.gi_code.co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))
Christian Heimes7131fd92008-02-19 14:21:46 +0000217
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000218def istraceback(object):
219 """Return true if the object is a traceback.
220
221 Traceback objects provide these attributes:
222 tb_frame frame object at this level
223 tb_lasti index of last attempted instruction in bytecode
224 tb_lineno current line number in Python source code
225 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000226 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000227
228def isframe(object):
229 """Return true if the object is a frame object.
230
231 Frame objects provide these attributes:
232 f_back next outer frame object (this frame's caller)
233 f_builtins built-in namespace seen by this frame
234 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000235 f_globals global namespace seen by this frame
236 f_lasti index of last attempted instruction in bytecode
237 f_lineno current line number in Python source code
238 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000239 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000240 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000241
242def iscode(object):
243 """Return true if the object is a code object.
244
245 Code objects provide these attributes:
246 co_argcount number of arguments (not including * or ** args)
247 co_code string of raw compiled bytecode
248 co_consts tuple of constants used in the bytecode
249 co_filename name of file in which this code object was created
250 co_firstlineno number of first line in Python source code
251 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
252 co_lnotab encoded mapping of line numbers to bytecode indices
253 co_name name with which this code object was defined
254 co_names tuple of names of local variables
255 co_nlocals number of local variables
256 co_stacksize virtual machine stack space required
257 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000258 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000259
260def isbuiltin(object):
261 """Return true if the object is a built-in function or method.
262
263 Built-in functions and methods provide these attributes:
264 __doc__ documentation string
265 __name__ original name of this function or method
266 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000267 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000268
269def isroutine(object):
270 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000271 return (isbuiltin(object)
272 or isfunction(object)
273 or ismethod(object)
274 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000275
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000276def isabstract(object):
277 """Return true if the object is an abstract base class (ABC)."""
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000278 return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT)
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000279
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000280def getmembers(object, predicate=None):
281 """Return all members of an object as (name, value) pairs sorted by name.
282 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100283 if isclass(object):
284 mro = (object,) + getmro(object)
285 else:
286 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000287 results = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700288 processed = set()
289 names = dir(object)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700290 # :dd any DynamicClassAttributes to the list of names if object is a class;
Ethan Furmane03ea372013-09-25 07:14:41 -0700291 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700292 # attribute with the same name as a DynamicClassAttribute exists
Ethan Furmane03ea372013-09-25 07:14:41 -0700293 try:
294 for base in object.__bases__:
295 for k, v in base.__dict__.items():
296 if isinstance(v, types.DynamicClassAttribute):
297 names.append(k)
298 except AttributeError:
299 pass
300 for key in names:
Ethan Furman63c141c2013-10-18 00:27:39 -0700301 # First try to get the value via getattr. Some descriptors don't
302 # like calling their __get__ (see bug #1785), so fall back to
303 # looking in the __dict__.
304 try:
305 value = getattr(object, key)
306 # handle the duplicate key
307 if key in processed:
308 raise AttributeError
309 except AttributeError:
310 for base in mro:
311 if key in base.__dict__:
312 value = base.__dict__[key]
313 break
314 else:
315 # could be a (currently) missing slot member, or a buggy
316 # __dir__; discard and move on
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100317 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000318 if not predicate or predicate(value):
319 results.append((key, value))
Ethan Furmane03ea372013-09-25 07:14:41 -0700320 processed.add(key)
321 results.sort(key=lambda pair: pair[0])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000322 return results
323
Christian Heimes25bb7832008-01-11 16:17:00 +0000324Attribute = namedtuple('Attribute', 'name kind defining_class object')
325
Tim Peters13b49d32001-09-23 02:00:29 +0000326def classify_class_attrs(cls):
327 """Return list of attribute-descriptor tuples.
328
329 For each name in dir(cls), the return list contains a 4-tuple
330 with these elements:
331
332 0. The name (a string).
333
334 1. The kind of attribute this is, one of these strings:
335 'class method' created via classmethod()
336 'static method' created via staticmethod()
337 'property' created via property()
Ethan Furmane03ea372013-09-25 07:14:41 -0700338 'method' any other flavor of method or descriptor
Tim Peters13b49d32001-09-23 02:00:29 +0000339 'data' not a method
340
341 2. The class which defined this attribute (a class).
342
Ethan Furmane03ea372013-09-25 07:14:41 -0700343 3. The object as obtained by calling getattr; if this fails, or if the
344 resulting object does not live anywhere in the class' mro (including
345 metaclasses) then the object is looked up in the defining class's
346 dict (found by walking the mro).
Ethan Furman668dede2013-09-14 18:53:26 -0700347
348 If one of the items in dir(cls) is stored in the metaclass it will now
349 be discovered and not have None be listed as the class in which it was
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700350 defined. Any items whose home class cannot be discovered are skipped.
Tim Peters13b49d32001-09-23 02:00:29 +0000351 """
352
353 mro = getmro(cls)
Ethan Furman668dede2013-09-14 18:53:26 -0700354 metamro = getmro(type(cls)) # for attributes stored in the metaclass
Ethan Furmane03ea372013-09-25 07:14:41 -0700355 metamro = tuple([cls for cls in metamro if cls not in (type, object)])
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700356 class_bases = (cls,) + mro
357 all_bases = class_bases + metamro
Tim Peters13b49d32001-09-23 02:00:29 +0000358 names = dir(cls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700359 # :dd any DynamicClassAttributes to the list of names;
Ethan Furmane03ea372013-09-25 07:14:41 -0700360 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700361 # attribute with the same name as a DynamicClassAttribute exists.
Ethan Furman63c141c2013-10-18 00:27:39 -0700362 for base in mro:
Ethan Furmane03ea372013-09-25 07:14:41 -0700363 for k, v in base.__dict__.items():
364 if isinstance(v, types.DynamicClassAttribute):
365 names.append(k)
Tim Peters13b49d32001-09-23 02:00:29 +0000366 result = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700367 processed = set()
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700368
Tim Peters13b49d32001-09-23 02:00:29 +0000369 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100370 # Get the object associated with the name, and where it was defined.
Ethan Furmane03ea372013-09-25 07:14:41 -0700371 # Normal objects will be looked up with both getattr and directly in
372 # its class' dict (in case getattr fails [bug #1785], and also to look
373 # for a docstring).
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700374 # For DynamicClassAttributes on the second pass we only look in the
Ethan Furmane03ea372013-09-25 07:14:41 -0700375 # class's dict.
376 #
Tim Peters13b49d32001-09-23 02:00:29 +0000377 # Getting an obj from the __dict__ sometimes reveals more than
378 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100379 homecls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700380 get_obj = None
381 dict_obj = None
Ethan Furmane03ea372013-09-25 07:14:41 -0700382 if name not in processed:
383 try:
Ethan Furmana8b07072013-10-18 01:22:08 -0700384 if name == '__dict__':
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700385 raise Exception("__dict__ is special, don't want the proxy")
Ethan Furmane03ea372013-09-25 07:14:41 -0700386 get_obj = getattr(cls, name)
387 except Exception as exc:
388 pass
389 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700390 homecls = getattr(get_obj, "__objclass__", homecls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700391 if homecls not in class_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700392 # if the resulting object does not live somewhere in the
Ethan Furman63c141c2013-10-18 00:27:39 -0700393 # mro, drop it and search the mro manually
Ethan Furmane03ea372013-09-25 07:14:41 -0700394 homecls = None
Ethan Furman63c141c2013-10-18 00:27:39 -0700395 last_cls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700396 # first look in the classes
397 for srch_cls in class_bases:
Ethan Furman63c141c2013-10-18 00:27:39 -0700398 srch_obj = getattr(srch_cls, name, None)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700399 if srch_obj == get_obj:
Ethan Furman63c141c2013-10-18 00:27:39 -0700400 last_cls = srch_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700401 # then check the metaclasses
402 for srch_cls in metamro:
403 try:
404 srch_obj = srch_cls.__getattr__(cls, name)
405 except AttributeError:
406 continue
407 if srch_obj == get_obj:
408 last_cls = srch_cls
Ethan Furman63c141c2013-10-18 00:27:39 -0700409 if last_cls is not None:
410 homecls = last_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700411 for base in all_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700412 if name in base.__dict__:
413 dict_obj = base.__dict__[name]
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700414 if homecls not in metamro:
415 homecls = base
Ethan Furmane03ea372013-09-25 07:14:41 -0700416 break
Ethan Furman63c141c2013-10-18 00:27:39 -0700417 if homecls is None:
418 # unable to locate the attribute anywhere, most likely due to
419 # buggy custom __dir__; discard and move on
420 continue
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700421 obj = get_obj or dict_obj
Ethan Furmane03ea372013-09-25 07:14:41 -0700422 # Classify the object or its descriptor.
Ethan Furman63c141c2013-10-18 00:27:39 -0700423 if isinstance(dict_obj, staticmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000424 kind = "static method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700425 obj = dict_obj
Ethan Furman63c141c2013-10-18 00:27:39 -0700426 elif isinstance(dict_obj, classmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000427 kind = "class method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700428 obj = dict_obj
429 elif isinstance(dict_obj, property):
Tim Peters13b49d32001-09-23 02:00:29 +0000430 kind = "property"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700431 obj = dict_obj
Yury Selivanov0860a0b2014-01-31 14:28:44 -0500432 elif isroutine(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000433 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100434 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700435 kind = "data"
Christian Heimes25bb7832008-01-11 16:17:00 +0000436 result.append(Attribute(name, kind, homecls, obj))
Ethan Furmane03ea372013-09-25 07:14:41 -0700437 processed.add(name)
Tim Peters13b49d32001-09-23 02:00:29 +0000438 return result
439
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000440# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000441
442def getmro(cls):
443 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000444 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000445
Nick Coghlane8c45d62013-07-28 20:00:01 +1000446# -------------------------------------------------------- function helpers
447
448def unwrap(func, *, stop=None):
449 """Get the object wrapped by *func*.
450
451 Follows the chain of :attr:`__wrapped__` attributes returning the last
452 object in the chain.
453
454 *stop* is an optional callback accepting an object in the wrapper chain
455 as its sole argument that allows the unwrapping to be terminated early if
456 the callback returns a true value. If the callback never returns a true
457 value, the last object in the chain is returned as usual. For example,
458 :func:`signature` uses this to stop unwrapping if any object in the
459 chain has a ``__signature__`` attribute defined.
460
461 :exc:`ValueError` is raised if a cycle is encountered.
462
463 """
464 if stop is None:
465 def _is_wrapper(f):
466 return hasattr(f, '__wrapped__')
467 else:
468 def _is_wrapper(f):
469 return hasattr(f, '__wrapped__') and not stop(f)
470 f = func # remember the original func for error reporting
471 memo = {id(f)} # Memoise by id to tolerate non-hashable objects
472 while _is_wrapper(func):
473 func = func.__wrapped__
474 id_func = id(func)
475 if id_func in memo:
476 raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
477 memo.add(id_func)
478 return func
479
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000480# -------------------------------------------------- source code extraction
481def indentsize(line):
482 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000483 expline = line.expandtabs()
484 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000485
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300486def _findclass(func):
487 cls = sys.modules.get(func.__module__)
488 if cls is None:
489 return None
490 for name in func.__qualname__.split('.')[:-1]:
491 cls = getattr(cls, name)
492 if not isclass(cls):
493 return None
494 return cls
495
496def _finddoc(obj):
497 if isclass(obj):
498 for base in obj.__mro__:
499 if base is not object:
500 try:
501 doc = base.__doc__
502 except AttributeError:
503 continue
504 if doc is not None:
505 return doc
506 return None
507
508 if ismethod(obj):
509 name = obj.__func__.__name__
510 self = obj.__self__
511 if (isclass(self) and
512 getattr(getattr(self, name, None), '__func__') is obj.__func__):
513 # classmethod
514 cls = self
515 else:
516 cls = self.__class__
517 elif isfunction(obj):
518 name = obj.__name__
519 cls = _findclass(obj)
520 if cls is None or getattr(cls, name) is not obj:
521 return None
522 elif isbuiltin(obj):
523 name = obj.__name__
524 self = obj.__self__
525 if (isclass(self) and
526 self.__qualname__ + '.' + name == obj.__qualname__):
527 # classmethod
528 cls = self
529 else:
530 cls = self.__class__
531 elif ismethoddescriptor(obj) or isdatadescriptor(obj):
532 name = obj.__name__
533 cls = obj.__objclass__
534 if getattr(cls, name) is not obj:
535 return None
536 elif isinstance(obj, property):
537 func = f.fget
538 name = func.__name__
539 cls = _findclass(func)
540 if cls is None or getattr(cls, name) is not obj:
541 return None
542 else:
543 return None
544
545 for base in cls.__mro__:
546 try:
547 doc = getattr(base, name).__doc__
548 except AttributeError:
549 continue
550 if doc is not None:
551 return doc
552 return None
553
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000554def getdoc(object):
555 """Get the documentation string for an object.
556
557 All tabs are expanded to spaces. To clean up docstrings that are
558 indented to line up with blocks of code, any whitespace than can be
559 uniformly removed from the second line onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000560 try:
561 doc = object.__doc__
562 except AttributeError:
563 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300564 if doc is None:
565 try:
566 doc = _finddoc(object)
567 except (AttributeError, TypeError):
568 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000569 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000570 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000571 return cleandoc(doc)
572
573def cleandoc(doc):
574 """Clean up indentation from docstrings.
575
576 Any whitespace that can be uniformly removed from the second line
577 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000578 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000579 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000580 except UnicodeError:
581 return None
582 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000583 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000584 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000585 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000586 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000587 if content:
588 indent = len(line) - content
589 margin = min(margin, indent)
590 # Remove indentation.
591 if lines:
592 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000593 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000594 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000595 # Remove any trailing or leading blank lines.
596 while lines and not lines[-1]:
597 lines.pop()
598 while lines and not lines[0]:
599 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000600 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000601
602def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000603 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000604 if ismodule(object):
605 if hasattr(object, '__file__'):
606 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000607 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000608 if isclass(object):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500609 if hasattr(object, '__module__'):
610 object = sys.modules.get(object.__module__)
611 if hasattr(object, '__file__'):
612 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000613 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000614 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000615 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000616 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000617 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000618 if istraceback(object):
619 object = object.tb_frame
620 if isframe(object):
621 object = object.f_code
622 if iscode(object):
623 return object.co_filename
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000624 raise TypeError('{!r} is not a module, class, method, '
625 'function, traceback, frame, or code object'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000626
Christian Heimes25bb7832008-01-11 16:17:00 +0000627ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')
628
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000629def getmoduleinfo(path):
630 """Get the module name, suffix, mode, and module type for a given file."""
Brett Cannoncb66eb02012-05-11 12:58:42 -0400631 warnings.warn('inspect.getmoduleinfo() is deprecated', DeprecationWarning,
632 2)
Brett Cannone4f41de2013-06-16 13:13:40 -0400633 with warnings.catch_warnings():
634 warnings.simplefilter('ignore', PendingDeprecationWarning)
635 import imp
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000636 filename = os.path.basename(path)
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000637 suffixes = [(-len(suffix), suffix, mode, mtype)
638 for suffix, mode, mtype in imp.get_suffixes()]
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000639 suffixes.sort() # try longest suffixes first, in case they overlap
640 for neglen, suffix, mode, mtype in suffixes:
641 if filename[neglen:] == suffix:
Christian Heimes25bb7832008-01-11 16:17:00 +0000642 return ModuleInfo(filename[:neglen], suffix, mode, mtype)
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000643
644def getmodulename(path):
645 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000646 fname = os.path.basename(path)
647 # Check for paths that look like an actual module file
648 suffixes = [(-len(suffix), suffix)
649 for suffix in importlib.machinery.all_suffixes()]
650 suffixes.sort() # try longest suffixes first, in case they overlap
651 for neglen, suffix in suffixes:
652 if fname.endswith(suffix):
653 return fname[:neglen]
654 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000655
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000656def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000657 """Return the filename that can be used to locate an object's source.
658 Return None if no way can be identified to get the source.
659 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000660 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400661 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
662 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
663 if any(filename.endswith(s) for s in all_bytecode_suffixes):
664 filename = (os.path.splitext(filename)[0] +
665 importlib.machinery.SOURCE_SUFFIXES[0])
666 elif any(filename.endswith(s) for s in
667 importlib.machinery.EXTENSION_SUFFIXES):
668 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000669 if os.path.exists(filename):
670 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000671 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400672 if getattr(getmodule(object, filename), '__loader__', None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000673 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000674 # or it is in the linecache
675 if filename in linecache.cache:
676 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000677
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000678def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000679 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000680
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000681 The idea is for each object to have a unique origin, so this routine
682 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000683 if _filename is None:
684 _filename = getsourcefile(object) or getfile(object)
685 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000686
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000687modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000688_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000689
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000690def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000691 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000692 if ismodule(object):
693 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000694 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000695 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000696 # Try the filename to modulename cache
697 if _filename is not None and _filename in modulesbyfile:
698 return sys.modules.get(modulesbyfile[_filename])
699 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000700 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000701 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000702 except TypeError:
703 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000704 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000705 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000706 # Update the filename to module name cache and check yet again
707 # Copy sys.modules in order to cope with changes while iterating
Éric Araujoa74f8ef2011-11-29 16:58:53 +0100708 for modname, module in list(sys.modules.items()):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000709 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000710 f = module.__file__
711 if f == _filesbymodname.get(modname, None):
712 # Have already mapped this module, so skip it
713 continue
714 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000715 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000716 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000717 modulesbyfile[f] = modulesbyfile[
718 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000719 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000720 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000721 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000722 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000723 if not hasattr(object, '__name__'):
724 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000725 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000726 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000727 if mainobject is object:
728 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000729 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000730 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000731 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000732 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000733 if builtinobject is object:
734 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000735
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000736def findsource(object):
737 """Return the entire source file and starting line number for an object.
738
739 The argument may be a module, class, method, function, traceback, frame,
740 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200741 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000742 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500743
Yury Selivanovef1e7502014-12-08 16:05:34 -0500744 file = getsourcefile(object)
745 if file:
746 # Invalidate cache if needed.
747 linecache.checkcache(file)
748 else:
749 file = getfile(object)
750 # Allow filenames in form of "<something>" to pass through.
751 # `doctest` monkeypatches `linecache` module to enable
752 # inspection, so let `linecache.getlines` to be called.
753 if not (file.startswith('<') and file.endswith('>')):
754 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500755
Thomas Wouters89f507f2006-12-13 04:49:30 +0000756 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000757 if module:
758 lines = linecache.getlines(file, module.__dict__)
759 else:
760 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000761 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200762 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000763
764 if ismodule(object):
765 return lines, 0
766
767 if isclass(object):
768 name = object.__name__
Thomas Wouters89f507f2006-12-13 04:49:30 +0000769 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
770 # make some effort to find the best matching class definition:
771 # use the one with the least indentation, which is the one
772 # that's most probably not inside a function definition.
773 candidates = []
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000774 for i in range(len(lines)):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000775 match = pat.match(lines[i])
776 if match:
777 # if it's at toplevel, it's already the best one
778 if lines[i][0] == 'c':
779 return lines, i
780 # else add whitespace to candidate list
781 candidates.append((match.group(1), i))
782 if candidates:
783 # this will sort by whitespace, and by line number,
784 # less whitespace first
785 candidates.sort()
786 return lines, candidates[0][1]
Jeremy Hyltonab919022003-06-27 18:41:20 +0000787 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200788 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000789
790 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000791 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000792 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000793 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000794 if istraceback(object):
795 object = object.tb_frame
796 if isframe(object):
797 object = object.f_code
798 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000799 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200800 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000801 lnum = object.co_firstlineno - 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000802 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000803 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000804 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000805 lnum = lnum - 1
806 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200807 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000808
809def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000810 """Get lines of comments immediately preceding an object's source code.
811
812 Returns None when source can't be found.
813 """
814 try:
815 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200816 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000817 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000818
819 if ismodule(object):
820 # Look for a comment block at the top of the file.
821 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000822 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000823 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000824 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000825 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000826 comments = []
827 end = start
828 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000829 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000830 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000831 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000832
833 # Look for a preceding block of comments at the same indentation.
834 elif lnum > 0:
835 indent = indentsize(lines[lnum])
836 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000837 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000838 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000839 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000840 if end > 0:
841 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000842 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000843 while comment[:1] == '#' and indentsize(lines[end]) == indent:
844 comments[:0] = [comment]
845 end = end - 1
846 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000847 comment = lines[end].expandtabs().lstrip()
848 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000849 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000850 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000851 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000852 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000853
Tim Peters4efb6e92001-06-29 23:51:08 +0000854class EndOfBlock(Exception): pass
855
856class BlockFinder:
857 """Provide a tokeneater() method to detect the end of a code block."""
858 def __init__(self):
859 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000860 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000861 self.started = False
862 self.passline = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000863 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000864
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000865 def tokeneater(self, type, token, srowcol, erowcol, line):
Tim Peters4efb6e92001-06-29 23:51:08 +0000866 if not self.started:
Armin Rigodd5c0232005-09-25 11:45:45 +0000867 # look for the first "def", "class" or "lambda"
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000868 if token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000869 if token == "lambda":
870 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000871 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000872 self.passline = True # skip to the end of the line
Tim Peters4efb6e92001-06-29 23:51:08 +0000873 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000874 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000875 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000876 if self.islambda: # lambdas always end at the first NEWLINE
877 raise EndOfBlock
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000878 elif self.passline:
879 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000880 elif type == tokenize.INDENT:
881 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000882 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000883 elif type == tokenize.DEDENT:
884 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000885 # the end of matching indent/dedent pairs end a block
886 # (note that this only works for "def"/"class" blocks,
887 # not e.g. for "if: else:" or "try: finally:" blocks)
888 if self.indent <= 0:
889 raise EndOfBlock
890 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
891 # any other token on the same indentation level end the previous
892 # block as well, except the pseudo-tokens COMMENT and NL.
893 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000894
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000895def getblock(lines):
896 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000897 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000898 try:
Trent Nelson428de652008-03-18 22:41:35 +0000899 tokens = tokenize.generate_tokens(iter(lines).__next__)
900 for _token in tokens:
901 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000902 except (EndOfBlock, IndentationError):
903 pass
904 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000905
Antoine Pitroua8723a02015-04-15 00:41:29 +0200906def _line_number_helper(code_obj, lines, lnum):
907 """Return a list of source lines and starting line number for a code object.
908
909 The arguments must be a code object with lines and lnum from findsource.
910 """
911 _, end_line = list(dis.findlinestarts(code_obj))[-1]
912 return lines[lnum:end_line], lnum + 1
913
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000914def getsourcelines(object):
915 """Return a list of source lines and starting line number for an object.
916
917 The argument may be a module, class, method, function, traceback, frame,
918 or code object. The source code is returned as a list of the lines
919 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200920 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000921 raised if the source code cannot be retrieved."""
Yury Selivanov081bbf62014-09-26 17:34:54 -0400922 object = unwrap(object)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000923 lines, lnum = findsource(object)
924
Antoine Pitroua8723a02015-04-15 00:41:29 +0200925 if ismodule(object):
926 return lines, 0
927 elif iscode(object):
928 return _line_number_helper(object, lines, lnum)
929 elif isfunction(object):
930 return _line_number_helper(object.__code__, lines, lnum)
931 elif ismethod(object):
932 return _line_number_helper(object.__func__.__code__, lines, lnum)
933 else:
934 return getblock(lines[lnum:]), lnum + 1
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000935
936def getsource(object):
937 """Return the text of the source code for an object.
938
939 The argument may be a module, class, method, function, traceback, frame,
940 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200941 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000942 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000943 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000944
945# --------------------------------------------------- class tree extraction
946def walktree(classes, children, parent):
947 """Recursive helper function for getclasstree()."""
948 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +0000949 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000950 for c in classes:
951 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +0000952 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000953 results.append(walktree(children[c], children, c))
954 return results
955
Georg Brandl5ce83a02009-06-01 17:23:51 +0000956def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000957 """Arrange the given list of classes into a hierarchy of nested lists.
958
959 Where a nested list appears, it contains classes derived from the class
960 whose entry immediately precedes the list. Each entry is a 2-tuple
961 containing a class and a tuple of its base classes. If the 'unique'
962 argument is true, exactly one entry appears in the returned structure
963 for each class in the given list. Otherwise, classes using multiple
964 inheritance and their descendants will appear multiple times."""
965 children = {}
966 roots = []
967 for c in classes:
968 if c.__bases__:
969 for parent in c.__bases__:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000970 if not parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000971 children[parent] = []
Serhiy Storchaka362c1b52013-09-05 17:14:32 +0300972 if c not in children[parent]:
973 children[parent].append(c)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000974 if unique and parent in classes: break
975 elif c not in roots:
976 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +0000977 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000978 if parent not in classes:
979 roots.append(parent)
980 return walktree(roots, children, None)
981
982# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +0000983Arguments = namedtuple('Arguments', 'args, varargs, varkw')
984
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000985def getargs(co):
986 """Get information about the arguments accepted by a code object.
987
Guido van Rossum2e65f892007-02-28 22:03:49 +0000988 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000989 'args' is the list of argument names. Keyword-only arguments are
990 appended. 'varargs' and 'varkw' are the names of the * and **
991 arguments or None."""
Guido van Rossum2e65f892007-02-28 22:03:49 +0000992 args, varargs, kwonlyargs, varkw = _getfullargs(co)
Christian Heimes25bb7832008-01-11 16:17:00 +0000993 return Arguments(args + kwonlyargs, varargs, varkw)
Guido van Rossum2e65f892007-02-28 22:03:49 +0000994
995def _getfullargs(co):
996 """Get information about the arguments accepted by a code object.
997
998 Four things are returned: (args, varargs, kwonlyargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000999 'args' and 'kwonlyargs' are lists of argument names, and 'varargs'
1000 and 'varkw' are the names of the * and ** arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +00001001
1002 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001003 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001004
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001005 nargs = co.co_argcount
1006 names = co.co_varnames
Guido van Rossum2e65f892007-02-28 22:03:49 +00001007 nkwargs = co.co_kwonlyargcount
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001008 args = list(names[:nargs])
Guido van Rossum2e65f892007-02-28 22:03:49 +00001009 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001010 step = 0
1011
Guido van Rossum2e65f892007-02-28 22:03:49 +00001012 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001013 varargs = None
1014 if co.co_flags & CO_VARARGS:
1015 varargs = co.co_varnames[nargs]
1016 nargs = nargs + 1
1017 varkw = None
1018 if co.co_flags & CO_VARKEYWORDS:
1019 varkw = co.co_varnames[nargs]
Guido van Rossum2e65f892007-02-28 22:03:49 +00001020 return args, varargs, kwonlyargs, varkw
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001021
Christian Heimes25bb7832008-01-11 16:17:00 +00001022
1023ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
1024
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001025def getargspec(func):
1026 """Get the names and default values of a function's arguments.
1027
Guido van Rossume82881c2014-07-15 12:29:11 -07001028 A tuple of four things is returned: (args, varargs, keywords, defaults).
1029 'args' is a list of the argument names, including keyword-only argument names.
1030 'varargs' and 'keywords' are the names of the * and ** arguments or None.
Jeremy Hylton64967882003-06-27 18:14:39 +00001031 'defaults' is an n-tuple of the default values of the last n arguments.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001032
Yury Selivanov0cf3ed62014-04-01 10:17:08 -04001033 Use the getfullargspec() API for Python 3 code, as annotations
Guido van Rossum2e65f892007-02-28 22:03:49 +00001034 and keyword arguments are supported. getargspec() will raise ValueError
1035 if the func has either annotations or keyword arguments.
1036 """
1037
1038 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
1039 getfullargspec(func)
1040 if kwonlyargs or ann:
Collin Winterce36ad82007-08-30 01:19:48 +00001041 raise ValueError("Function has keyword-only arguments or annotations"
1042 ", use getfullargspec() API which can support them")
Christian Heimes25bb7832008-01-11 16:17:00 +00001043 return ArgSpec(args, varargs, varkw, defaults)
1044
1045FullArgSpec = namedtuple('FullArgSpec',
Benjamin Peterson3d4ca742008-11-12 21:39:01 +00001046 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001047
1048def getfullargspec(func):
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001049 """Get the names and default values of a callable object's arguments.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001050
Brett Cannon504d8852007-09-07 02:12:14 +00001051 A tuple of seven things is returned:
1052 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults annotations).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001053 'args' is a list of the argument names.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001054 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1055 'defaults' is an n-tuple of the default values of the last n arguments.
1056 'kwonlyargs' is a list of keyword-only argument names.
1057 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
1058 'annotations' is a dictionary mapping argument names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001059
Guido van Rossum2e65f892007-02-28 22:03:49 +00001060 The first four items in the tuple correspond to getargspec().
Jeremy Hylton64967882003-06-27 18:14:39 +00001061 """
1062
Yury Selivanov57d240e2014-02-19 16:27:23 -05001063 try:
1064 # Re: `skip_bound_arg=False`
1065 #
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001066 # There is a notable difference in behaviour between getfullargspec
1067 # and Signature: the former always returns 'self' parameter for bound
1068 # methods, whereas the Signature always shows the actual calling
1069 # signature of the passed object.
1070 #
1071 # To simulate this behaviour, we "unbind" bound methods, to trick
1072 # inspect.signature to always return their first parameter ("self",
1073 # usually)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001074
Yury Selivanov57d240e2014-02-19 16:27:23 -05001075 # Re: `follow_wrapper_chains=False`
1076 #
1077 # getfullargspec() historically ignored __wrapped__ attributes,
1078 # so we ensure that remains the case in 3.3+
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001079
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001080 sig = _signature_from_callable(func,
1081 follow_wrapper_chains=False,
1082 skip_bound_arg=False,
1083 sigcls=Signature)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001084 except Exception as ex:
1085 # Most of the times 'signature' will raise ValueError.
1086 # But, it can also raise AttributeError, and, maybe something
1087 # else. So to be fully backwards compatible, we catch all
1088 # possible exceptions here, and reraise a TypeError.
1089 raise TypeError('unsupported callable') from ex
1090
1091 args = []
1092 varargs = None
1093 varkw = None
1094 kwonlyargs = []
1095 defaults = ()
1096 annotations = {}
1097 defaults = ()
1098 kwdefaults = {}
1099
1100 if sig.return_annotation is not sig.empty:
1101 annotations['return'] = sig.return_annotation
1102
1103 for param in sig.parameters.values():
1104 kind = param.kind
1105 name = param.name
1106
1107 if kind is _POSITIONAL_ONLY:
1108 args.append(name)
1109 elif kind is _POSITIONAL_OR_KEYWORD:
1110 args.append(name)
1111 if param.default is not param.empty:
1112 defaults += (param.default,)
1113 elif kind is _VAR_POSITIONAL:
1114 varargs = name
1115 elif kind is _KEYWORD_ONLY:
1116 kwonlyargs.append(name)
1117 if param.default is not param.empty:
1118 kwdefaults[name] = param.default
1119 elif kind is _VAR_KEYWORD:
1120 varkw = name
1121
1122 if param.annotation is not param.empty:
1123 annotations[name] = param.annotation
1124
1125 if not kwdefaults:
1126 # compatibility with 'func.__kwdefaults__'
1127 kwdefaults = None
1128
1129 if not defaults:
1130 # compatibility with 'func.__defaults__'
1131 defaults = None
1132
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001133 return FullArgSpec(args, varargs, varkw, defaults,
1134 kwonlyargs, kwdefaults, annotations)
1135
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001136
Christian Heimes25bb7832008-01-11 16:17:00 +00001137ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
1138
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001139def getargvalues(frame):
1140 """Get information about arguments passed into a particular frame.
1141
1142 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001143 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001144 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1145 'locals' is the locals dictionary of the given frame."""
1146 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +00001147 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001148
Guido van Rossum2e65f892007-02-28 22:03:49 +00001149def formatannotation(annotation, base_module=None):
1150 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001151 if annotation.__module__ in ('builtins', base_module):
Serhiy Storchaka521e5862014-07-22 15:00:37 +03001152 return annotation.__qualname__
1153 return annotation.__module__+'.'+annotation.__qualname__
Guido van Rossum2e65f892007-02-28 22:03:49 +00001154 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001155
Guido van Rossum2e65f892007-02-28 22:03:49 +00001156def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001157 module = getattr(object, '__module__', None)
1158 def _formatannotation(annotation):
1159 return formatannotation(annotation, module)
1160 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001161
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001162def formatargspec(args, varargs=None, varkw=None, defaults=None,
Guido van Rossum2e65f892007-02-28 22:03:49 +00001163 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001164 formatarg=str,
1165 formatvarargs=lambda name: '*' + name,
1166 formatvarkw=lambda name: '**' + name,
1167 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001168 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001169 formatannotation=formatannotation):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001170 """Format an argument spec from the values returned by getargspec
Guido van Rossum2e65f892007-02-28 22:03:49 +00001171 or getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001172
Guido van Rossum2e65f892007-02-28 22:03:49 +00001173 The first seven arguments are (args, varargs, varkw, defaults,
1174 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1175 are the corresponding optional formatting functions that are called to
1176 turn names and values into strings. The last argument is an optional
1177 function to format the sequence of arguments."""
1178 def formatargandannotation(arg):
1179 result = formatarg(arg)
1180 if arg in annotations:
1181 result += ': ' + formatannotation(annotations[arg])
1182 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001183 specs = []
1184 if defaults:
1185 firstdefault = len(args) - len(defaults)
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001186 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001187 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001188 if defaults and i >= firstdefault:
1189 spec = spec + formatvalue(defaults[i - firstdefault])
1190 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001191 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001192 specs.append(formatvarargs(formatargandannotation(varargs)))
1193 else:
1194 if kwonlyargs:
1195 specs.append('*')
1196 if kwonlyargs:
1197 for kwonlyarg in kwonlyargs:
1198 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001199 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001200 spec += formatvalue(kwonlydefaults[kwonlyarg])
1201 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001202 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001203 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001204 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001205 if 'return' in annotations:
1206 result += formatreturns(formatannotation(annotations['return']))
1207 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001208
1209def formatargvalues(args, varargs, varkw, locals,
1210 formatarg=str,
1211 formatvarargs=lambda name: '*' + name,
1212 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001213 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001214 """Format an argument spec from the 4 values returned by getargvalues.
1215
1216 The first four arguments are (args, varargs, varkw, locals). The
1217 next four arguments are the corresponding optional formatting functions
1218 that are called to turn names and values into strings. The ninth
1219 argument is an optional function to format the sequence of arguments."""
1220 def convert(name, locals=locals,
1221 formatarg=formatarg, formatvalue=formatvalue):
1222 return formatarg(name) + formatvalue(locals[name])
1223 specs = []
1224 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001225 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001226 if varargs:
1227 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1228 if varkw:
1229 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001230 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001231
Benjamin Petersone109c702011-06-24 09:37:26 -05001232def _missing_arguments(f_name, argnames, pos, values):
1233 names = [repr(name) for name in argnames if name not in values]
1234 missing = len(names)
1235 if missing == 1:
1236 s = names[0]
1237 elif missing == 2:
1238 s = "{} and {}".format(*names)
1239 else:
Yury Selivanovdccfa132014-03-27 18:42:52 -04001240 tail = ", {} and {}".format(*names[-2:])
Benjamin Petersone109c702011-06-24 09:37:26 -05001241 del names[-2:]
1242 s = ", ".join(names) + tail
1243 raise TypeError("%s() missing %i required %s argument%s: %s" %
1244 (f_name, missing,
1245 "positional" if pos else "keyword-only",
1246 "" if missing == 1 else "s", s))
1247
1248def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001249 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001250 kwonly_given = len([arg for arg in kwonly if arg in values])
1251 if varargs:
1252 plural = atleast != 1
1253 sig = "at least %d" % (atleast,)
1254 elif defcount:
1255 plural = True
1256 sig = "from %d to %d" % (atleast, len(args))
1257 else:
1258 plural = len(args) != 1
1259 sig = str(len(args))
1260 kwonly_sig = ""
1261 if kwonly_given:
1262 msg = " positional argument%s (and %d keyword-only argument%s)"
1263 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1264 "s" if kwonly_given != 1 else ""))
1265 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1266 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1267 "was" if given == 1 and not kwonly_given else "were"))
1268
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001269def getcallargs(*func_and_positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001270 """Get the mapping of arguments to values.
1271
1272 A dict is returned, with keys the function argument names (including the
1273 names of the * and ** arguments, if any), and values the respective bound
1274 values from 'positional' and 'named'."""
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001275 func = func_and_positional[0]
1276 positional = func_and_positional[1:]
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001277 spec = getfullargspec(func)
1278 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
1279 f_name = func.__name__
1280 arg2value = {}
1281
Benjamin Petersonb204a422011-06-05 22:04:07 -05001282
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001283 if ismethod(func) and func.__self__ is not None:
1284 # implicit 'self' (or 'cls' for classmethods) argument
1285 positional = (func.__self__,) + positional
1286 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001287 num_args = len(args)
1288 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001289
Benjamin Petersonb204a422011-06-05 22:04:07 -05001290 n = min(num_pos, num_args)
1291 for i in range(n):
1292 arg2value[args[i]] = positional[i]
1293 if varargs:
1294 arg2value[varargs] = tuple(positional[n:])
1295 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001296 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001297 arg2value[varkw] = {}
1298 for kw, value in named.items():
1299 if kw not in possible_kwargs:
1300 if not varkw:
1301 raise TypeError("%s() got an unexpected keyword argument %r" %
1302 (f_name, kw))
1303 arg2value[varkw][kw] = value
1304 continue
1305 if kw in arg2value:
1306 raise TypeError("%s() got multiple values for argument %r" %
1307 (f_name, kw))
1308 arg2value[kw] = value
1309 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001310 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1311 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001312 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001313 req = args[:num_args - num_defaults]
1314 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001315 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001316 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001317 for i, arg in enumerate(args[num_args - num_defaults:]):
1318 if arg not in arg2value:
1319 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001320 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001321 for kwarg in kwonlyargs:
1322 if kwarg not in arg2value:
Yury Selivanov875df202014-03-27 18:23:03 -04001323 if kwonlydefaults and kwarg in kwonlydefaults:
Benjamin Petersone109c702011-06-24 09:37:26 -05001324 arg2value[kwarg] = kwonlydefaults[kwarg]
1325 else:
1326 missing += 1
1327 if missing:
1328 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001329 return arg2value
1330
Nick Coghlan2f92e542012-06-23 19:39:55 +10001331ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1332
1333def getclosurevars(func):
1334 """
1335 Get the mapping of free variables to their current values.
1336
Meador Inge8fda3592012-07-19 21:33:21 -05001337 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001338 and builtin references as seen by the body of the function. A final
1339 set of unbound names that could not be resolved is also provided.
1340 """
1341
1342 if ismethod(func):
1343 func = func.__func__
1344
1345 if not isfunction(func):
1346 raise TypeError("'{!r}' is not a Python function".format(func))
1347
1348 code = func.__code__
1349 # Nonlocal references are named in co_freevars and resolved
1350 # by looking them up in __closure__ by positional index
1351 if func.__closure__ is None:
1352 nonlocal_vars = {}
1353 else:
1354 nonlocal_vars = {
1355 var : cell.cell_contents
1356 for var, cell in zip(code.co_freevars, func.__closure__)
1357 }
1358
1359 # Global and builtin references are named in co_names and resolved
1360 # by looking them up in __globals__ or __builtins__
1361 global_ns = func.__globals__
1362 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1363 if ismodule(builtin_ns):
1364 builtin_ns = builtin_ns.__dict__
1365 global_vars = {}
1366 builtin_vars = {}
1367 unbound_names = set()
1368 for name in code.co_names:
1369 if name in ("None", "True", "False"):
1370 # Because these used to be builtins instead of keywords, they
1371 # may still show up as name references. We ignore them.
1372 continue
1373 try:
1374 global_vars[name] = global_ns[name]
1375 except KeyError:
1376 try:
1377 builtin_vars[name] = builtin_ns[name]
1378 except KeyError:
1379 unbound_names.add(name)
1380
1381 return ClosureVars(nonlocal_vars, global_vars,
1382 builtin_vars, unbound_names)
1383
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001384# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001385
1386Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1387
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001388def getframeinfo(frame, context=1):
1389 """Get information about a frame or traceback object.
1390
1391 A tuple of five things is returned: the filename, the line number of
1392 the current line, the function name, a list of lines of context from
1393 the source code, and the index of the current line within that list.
1394 The optional second argument specifies the number of lines of context
1395 to return, which are centered around the current line."""
1396 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001397 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001398 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001399 else:
1400 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001401 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001402 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001403
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001404 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001405 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001406 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001407 try:
1408 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001409 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001410 lines = index = None
1411 else:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001412 start = max(start, 1)
Raymond Hettingera0501712004-06-15 11:22:53 +00001413 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001414 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001415 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001416 else:
1417 lines = index = None
1418
Christian Heimes25bb7832008-01-11 16:17:00 +00001419 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001420
1421def getlineno(frame):
1422 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001423 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1424 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001425
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001426FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
1427
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001428def getouterframes(frame, context=1):
1429 """Get a list of records for a frame and all higher (calling) frames.
1430
1431 Each record contains a frame object, filename, line number, function
1432 name, a list of lines of context, and index within the context."""
1433 framelist = []
1434 while frame:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001435 frameinfo = (frame,) + getframeinfo(frame, context)
1436 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001437 frame = frame.f_back
1438 return framelist
1439
1440def getinnerframes(tb, context=1):
1441 """Get a list of records for a traceback's frame and all lower frames.
1442
1443 Each record contains a frame object, filename, line number, function
1444 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001445 framelist = []
1446 while tb:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001447 frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
1448 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001449 tb = tb.tb_next
1450 return framelist
1451
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001452def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001453 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001454 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001455
1456def stack(context=1):
1457 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001458 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001459
1460def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001461 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001462 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001463
1464
1465# ------------------------------------------------ static version of getattr
1466
1467_sentinel = object()
1468
Michael Foorde5162652010-11-20 16:40:44 +00001469def _static_getmro(klass):
1470 return type.__dict__['__mro__'].__get__(klass)
1471
Michael Foord95fc51d2010-11-20 15:07:30 +00001472def _check_instance(obj, attr):
1473 instance_dict = {}
1474 try:
1475 instance_dict = object.__getattribute__(obj, "__dict__")
1476 except AttributeError:
1477 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001478 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001479
1480
1481def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001482 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001483 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001484 try:
1485 return entry.__dict__[attr]
1486 except KeyError:
1487 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001488 return _sentinel
1489
Michael Foord35184ed2010-11-20 16:58:30 +00001490def _is_type(obj):
1491 try:
1492 _static_getmro(obj)
1493 except TypeError:
1494 return False
1495 return True
1496
Michael Foorddcebe0f2011-03-15 19:20:44 -04001497def _shadowed_dict(klass):
1498 dict_attr = type.__dict__["__dict__"]
1499 for entry in _static_getmro(klass):
1500 try:
1501 class_dict = dict_attr.__get__(entry)["__dict__"]
1502 except KeyError:
1503 pass
1504 else:
1505 if not (type(class_dict) is types.GetSetDescriptorType and
1506 class_dict.__name__ == "__dict__" and
1507 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001508 return class_dict
1509 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001510
1511def getattr_static(obj, attr, default=_sentinel):
1512 """Retrieve attributes without triggering dynamic lookup via the
1513 descriptor protocol, __getattr__ or __getattribute__.
1514
1515 Note: this function may not be able to retrieve all attributes
1516 that getattr can fetch (like dynamically created attributes)
1517 and may find attributes that getattr can't (like descriptors
1518 that raise AttributeError). It can also return descriptor objects
1519 instead of instance members in some cases. See the
1520 documentation for details.
1521 """
1522 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001523 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001524 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001525 dict_attr = _shadowed_dict(klass)
1526 if (dict_attr is _sentinel or
1527 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001528 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001529 else:
1530 klass = obj
1531
1532 klass_result = _check_class(klass, attr)
1533
1534 if instance_result is not _sentinel and klass_result is not _sentinel:
1535 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1536 _check_class(type(klass_result), '__set__') is not _sentinel):
1537 return klass_result
1538
1539 if instance_result is not _sentinel:
1540 return instance_result
1541 if klass_result is not _sentinel:
1542 return klass_result
1543
1544 if obj is klass:
1545 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001546 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001547 if _shadowed_dict(type(entry)) is _sentinel:
1548 try:
1549 return entry.__dict__[attr]
1550 except KeyError:
1551 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001552 if default is not _sentinel:
1553 return default
1554 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001555
1556
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001557# ------------------------------------------------ generator introspection
1558
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001559GEN_CREATED = 'GEN_CREATED'
1560GEN_RUNNING = 'GEN_RUNNING'
1561GEN_SUSPENDED = 'GEN_SUSPENDED'
1562GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001563
1564def getgeneratorstate(generator):
1565 """Get current state of a generator-iterator.
1566
1567 Possible states are:
1568 GEN_CREATED: Waiting to start execution.
1569 GEN_RUNNING: Currently being executed by the interpreter.
1570 GEN_SUSPENDED: Currently suspended at a yield expression.
1571 GEN_CLOSED: Execution has completed.
1572 """
1573 if generator.gi_running:
1574 return GEN_RUNNING
1575 if generator.gi_frame is None:
1576 return GEN_CLOSED
1577 if generator.gi_frame.f_lasti == -1:
1578 return GEN_CREATED
1579 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001580
1581
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001582def getgeneratorlocals(generator):
1583 """
1584 Get the mapping of generator local variables to their current values.
1585
1586 A dict is returned, with the keys the local variable names and values the
1587 bound values."""
1588
1589 if not isgenerator(generator):
1590 raise TypeError("'{!r}' is not a Python generator".format(generator))
1591
1592 frame = getattr(generator, "gi_frame", None)
1593 if frame is not None:
1594 return generator.gi_frame.f_locals
1595 else:
1596 return {}
1597
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001598###############################################################################
1599### Function Signature Object (PEP 362)
1600###############################################################################
1601
1602
1603_WrapperDescriptor = type(type.__call__)
1604_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001605_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001606
1607_NonUserDefinedCallables = (_WrapperDescriptor,
1608 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001609 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001610 types.BuiltinFunctionType)
1611
1612
Yury Selivanov421f0c72014-01-29 12:05:40 -05001613def _signature_get_user_defined_method(cls, method_name):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001614 """Private helper. Checks if ``cls`` has an attribute
1615 named ``method_name`` and returns it only if it is a
1616 pure python function.
1617 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001618 try:
1619 meth = getattr(cls, method_name)
1620 except AttributeError:
1621 return
1622 else:
1623 if not isinstance(meth, _NonUserDefinedCallables):
1624 # Once '__signature__' will be added to 'C'-level
1625 # callables, this check won't be necessary
1626 return meth
1627
1628
Yury Selivanov62560fb2014-01-28 12:26:24 -05001629def _signature_get_partial(wrapped_sig, partial, extra_args=()):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001630 """Private helper to calculate how 'wrapped_sig' signature will
1631 look like after applying a 'functools.partial' object (or alike)
1632 on it.
1633 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001634
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001635 old_params = wrapped_sig.parameters
1636 new_params = OrderedDict(old_params.items())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001637
1638 partial_args = partial.args or ()
1639 partial_keywords = partial.keywords or {}
1640
1641 if extra_args:
1642 partial_args = extra_args + partial_args
1643
1644 try:
1645 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1646 except TypeError as ex:
1647 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1648 raise ValueError(msg) from ex
1649
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001650
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001651 transform_to_kwonly = False
1652 for param_name, param in old_params.items():
1653 try:
1654 arg_value = ba.arguments[param_name]
1655 except KeyError:
1656 pass
1657 else:
1658 if param.kind is _POSITIONAL_ONLY:
1659 # If positional-only parameter is bound by partial,
1660 # it effectively disappears from the signature
1661 new_params.pop(param_name)
1662 continue
1663
1664 if param.kind is _POSITIONAL_OR_KEYWORD:
1665 if param_name in partial_keywords:
1666 # This means that this parameter, and all parameters
1667 # after it should be keyword-only (and var-positional
1668 # should be removed). Here's why. Consider the following
1669 # function:
1670 # foo(a, b, *args, c):
1671 # pass
1672 #
1673 # "partial(foo, a='spam')" will have the following
1674 # signature: "(*, a='spam', b, c)". Because attempting
1675 # to call that partial with "(10, 20)" arguments will
1676 # raise a TypeError, saying that "a" argument received
1677 # multiple values.
1678 transform_to_kwonly = True
1679 # Set the new default value
1680 new_params[param_name] = param.replace(default=arg_value)
1681 else:
1682 # was passed as a positional argument
1683 new_params.pop(param.name)
1684 continue
1685
1686 if param.kind is _KEYWORD_ONLY:
1687 # Set the new default value
1688 new_params[param_name] = param.replace(default=arg_value)
1689
1690 if transform_to_kwonly:
1691 assert param.kind is not _POSITIONAL_ONLY
1692
1693 if param.kind is _POSITIONAL_OR_KEYWORD:
1694 new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1695 new_params[param_name] = new_param
1696 new_params.move_to_end(param_name)
1697 elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
1698 new_params.move_to_end(param_name)
1699 elif param.kind is _VAR_POSITIONAL:
1700 new_params.pop(param.name)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001701
1702 return wrapped_sig.replace(parameters=new_params.values())
1703
1704
Yury Selivanov62560fb2014-01-28 12:26:24 -05001705def _signature_bound_method(sig):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001706 """Private helper to transform signatures for unbound
1707 functions to bound methods.
1708 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001709
1710 params = tuple(sig.parameters.values())
1711
1712 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1713 raise ValueError('invalid method signature')
1714
1715 kind = params[0].kind
1716 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1717 # Drop first parameter:
1718 # '(p1, p2[, ...])' -> '(p2[, ...])'
1719 params = params[1:]
1720 else:
1721 if kind is not _VAR_POSITIONAL:
1722 # Unless we add a new parameter type we never
1723 # get here
1724 raise ValueError('invalid argument type')
1725 # It's a var-positional parameter.
1726 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1727
1728 return sig.replace(parameters=params)
1729
1730
Yury Selivanovb77511d2014-01-29 10:46:14 -05001731def _signature_is_builtin(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001732 """Private helper to test if `obj` is a callable that might
1733 support Argument Clinic's __text_signature__ protocol.
1734 """
Yury Selivanov1d241832014-02-02 12:51:20 -05001735 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001736 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001737 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001738 # Can't test 'isinstance(type)' here, as it would
1739 # also be True for regular python classes
1740 obj in (type, object))
1741
1742
Yury Selivanov63da7c72014-01-31 14:48:37 -05001743def _signature_is_functionlike(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001744 """Private helper to test if `obj` is a duck type of FunctionType.
1745 A good example of such objects are functions compiled with
1746 Cython, which have all attributes that a pure Python function
1747 would have, but have their code statically compiled.
1748 """
Yury Selivanov63da7c72014-01-31 14:48:37 -05001749
1750 if not callable(obj) or isclass(obj):
1751 # All function-like objects are obviously callables,
1752 # and not classes.
1753 return False
1754
1755 name = getattr(obj, '__name__', None)
1756 code = getattr(obj, '__code__', None)
1757 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
1758 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
1759 annotations = getattr(obj, '__annotations__', None)
1760
1761 return (isinstance(code, types.CodeType) and
1762 isinstance(name, str) and
1763 (defaults is None or isinstance(defaults, tuple)) and
1764 (kwdefaults is None or isinstance(kwdefaults, dict)) and
1765 isinstance(annotations, dict))
1766
1767
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001768def _signature_get_bound_param(spec):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001769 """ Private helper to get first parameter name from a
1770 __text_signature__ of a builtin method, which should
1771 be in the following format: '($param1, ...)'.
1772 Assumptions are that the first argument won't have
1773 a default value or an annotation.
1774 """
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001775
1776 assert spec.startswith('($')
1777
1778 pos = spec.find(',')
1779 if pos == -1:
1780 pos = spec.find(')')
1781
1782 cpos = spec.find(':')
1783 assert cpos == -1 or cpos > pos
1784
1785 cpos = spec.find('=')
1786 assert cpos == -1 or cpos > pos
1787
1788 return spec[2:pos]
1789
1790
Larry Hastings2623c8c2014-02-08 22:15:29 -08001791def _signature_strip_non_python_syntax(signature):
1792 """
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001793 Private helper function. Takes a signature in Argument Clinic's
1794 extended signature format.
1795
Larry Hastings2623c8c2014-02-08 22:15:29 -08001796 Returns a tuple of three things:
1797 * that signature re-rendered in standard Python syntax,
1798 * the index of the "self" parameter (generally 0), or None if
1799 the function does not have a "self" parameter, and
1800 * the index of the last "positional only" parameter,
1801 or None if the signature has no positional-only parameters.
1802 """
1803
1804 if not signature:
1805 return signature, None, None
1806
1807 self_parameter = None
1808 last_positional_only = None
1809
1810 lines = [l.encode('ascii') for l in signature.split('\n')]
1811 generator = iter(lines).__next__
1812 token_stream = tokenize.tokenize(generator)
1813
1814 delayed_comma = False
1815 skip_next_comma = False
1816 text = []
1817 add = text.append
1818
1819 current_parameter = 0
1820 OP = token.OP
1821 ERRORTOKEN = token.ERRORTOKEN
1822
1823 # token stream always starts with ENCODING token, skip it
1824 t = next(token_stream)
1825 assert t.type == tokenize.ENCODING
1826
1827 for t in token_stream:
1828 type, string = t.type, t.string
1829
1830 if type == OP:
1831 if string == ',':
1832 if skip_next_comma:
1833 skip_next_comma = False
1834 else:
1835 assert not delayed_comma
1836 delayed_comma = True
1837 current_parameter += 1
1838 continue
1839
1840 if string == '/':
1841 assert not skip_next_comma
1842 assert last_positional_only is None
1843 skip_next_comma = True
1844 last_positional_only = current_parameter - 1
1845 continue
1846
1847 if (type == ERRORTOKEN) and (string == '$'):
1848 assert self_parameter is None
1849 self_parameter = current_parameter
1850 continue
1851
1852 if delayed_comma:
1853 delayed_comma = False
1854 if not ((type == OP) and (string == ')')):
1855 add(', ')
1856 add(string)
1857 if (string == ','):
1858 add(' ')
1859 clean_signature = ''.join(text)
1860 return clean_signature, self_parameter, last_positional_only
1861
1862
Yury Selivanov57d240e2014-02-19 16:27:23 -05001863def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001864 """Private helper to parse content of '__text_signature__'
1865 and return a Signature based on it.
1866 """
1867
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001868 Parameter = cls._parameter_cls
1869
Larry Hastings2623c8c2014-02-08 22:15:29 -08001870 clean_signature, self_parameter, last_positional_only = \
1871 _signature_strip_non_python_syntax(s)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001872
Larry Hastings2623c8c2014-02-08 22:15:29 -08001873 program = "def foo" + clean_signature + ": pass"
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001874
1875 try:
Larry Hastings2623c8c2014-02-08 22:15:29 -08001876 module = ast.parse(program)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001877 except SyntaxError:
1878 module = None
1879
1880 if not isinstance(module, ast.Module):
1881 raise ValueError("{!r} builtin has invalid signature".format(obj))
1882
1883 f = module.body[0]
1884
1885 parameters = []
1886 empty = Parameter.empty
1887 invalid = object()
1888
1889 module = None
1890 module_dict = {}
1891 module_name = getattr(obj, '__module__', None)
1892 if module_name:
1893 module = sys.modules.get(module_name, None)
1894 if module:
1895 module_dict = module.__dict__
1896 sys_module_dict = sys.modules
1897
1898 def parse_name(node):
1899 assert isinstance(node, ast.arg)
1900 if node.annotation != None:
1901 raise ValueError("Annotations are not currently supported")
1902 return node.arg
1903
1904 def wrap_value(s):
1905 try:
1906 value = eval(s, module_dict)
1907 except NameError:
1908 try:
1909 value = eval(s, sys_module_dict)
1910 except NameError:
1911 raise RuntimeError()
1912
1913 if isinstance(value, str):
1914 return ast.Str(value)
1915 if isinstance(value, (int, float)):
1916 return ast.Num(value)
1917 if isinstance(value, bytes):
1918 return ast.Bytes(value)
1919 if value in (True, False, None):
1920 return ast.NameConstant(value)
1921 raise RuntimeError()
1922
1923 class RewriteSymbolics(ast.NodeTransformer):
1924 def visit_Attribute(self, node):
1925 a = []
1926 n = node
1927 while isinstance(n, ast.Attribute):
1928 a.append(n.attr)
1929 n = n.value
1930 if not isinstance(n, ast.Name):
1931 raise RuntimeError()
1932 a.append(n.id)
1933 value = ".".join(reversed(a))
1934 return wrap_value(value)
1935
1936 def visit_Name(self, node):
1937 if not isinstance(node.ctx, ast.Load):
1938 raise ValueError()
1939 return wrap_value(node.id)
1940
1941 def p(name_node, default_node, default=empty):
1942 name = parse_name(name_node)
1943 if name is invalid:
1944 return None
1945 if default_node and default_node is not _empty:
1946 try:
1947 default_node = RewriteSymbolics().visit(default_node)
1948 o = ast.literal_eval(default_node)
1949 except ValueError:
1950 o = invalid
1951 if o is invalid:
1952 return None
1953 default = o if o is not invalid else default
1954 parameters.append(Parameter(name, kind, default=default, annotation=empty))
1955
1956 # non-keyword-only parameters
1957 args = reversed(f.args.args)
1958 defaults = reversed(f.args.defaults)
1959 iter = itertools.zip_longest(args, defaults, fillvalue=None)
Larry Hastings2623c8c2014-02-08 22:15:29 -08001960 if last_positional_only is not None:
1961 kind = Parameter.POSITIONAL_ONLY
1962 else:
1963 kind = Parameter.POSITIONAL_OR_KEYWORD
1964 for i, (name, default) in enumerate(reversed(list(iter))):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001965 p(name, default)
Larry Hastings2623c8c2014-02-08 22:15:29 -08001966 if i == last_positional_only:
1967 kind = Parameter.POSITIONAL_OR_KEYWORD
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001968
1969 # *args
1970 if f.args.vararg:
1971 kind = Parameter.VAR_POSITIONAL
1972 p(f.args.vararg, empty)
1973
1974 # keyword-only arguments
1975 kind = Parameter.KEYWORD_ONLY
1976 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
1977 p(name, default)
1978
1979 # **kwargs
1980 if f.args.kwarg:
1981 kind = Parameter.VAR_KEYWORD
1982 p(f.args.kwarg, empty)
1983
Larry Hastings2623c8c2014-02-08 22:15:29 -08001984 if self_parameter is not None:
Yury Selivanov8c185ee2014-02-21 01:32:42 -05001985 # Possibly strip the bound argument:
1986 # - We *always* strip first bound argument if
1987 # it is a module.
1988 # - We don't strip first bound argument if
1989 # skip_bound_arg is False.
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001990 assert parameters
Yury Selivanov8c185ee2014-02-21 01:32:42 -05001991 _self = getattr(obj, '__self__', None)
1992 self_isbound = _self is not None
1993 self_ismodule = ismodule(_self)
1994 if self_isbound and (self_ismodule or skip_bound_arg):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001995 parameters.pop(0)
1996 else:
1997 # for builtins, self parameter is always positional-only!
1998 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
1999 parameters[0] = p
2000
2001 return cls(parameters, return_annotation=cls.empty)
2002
2003
Yury Selivanov57d240e2014-02-19 16:27:23 -05002004def _signature_from_builtin(cls, func, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002005 """Private helper function to get signature for
2006 builtin callables.
2007 """
2008
Yury Selivanov57d240e2014-02-19 16:27:23 -05002009 if not _signature_is_builtin(func):
2010 raise TypeError("{!r} is not a Python builtin "
2011 "function".format(func))
2012
2013 s = getattr(func, "__text_signature__", None)
2014 if not s:
2015 raise ValueError("no signature found for builtin {!r}".format(func))
2016
2017 return _signature_fromstr(cls, func, s, skip_bound_arg)
2018
2019
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002020def _signature_from_callable(obj, *,
2021 follow_wrapper_chains=True,
2022 skip_bound_arg=True,
2023 sigcls):
2024
2025 """Private helper function to get signature for arbitrary
2026 callable objects.
2027 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002028
2029 if not callable(obj):
2030 raise TypeError('{!r} is not a callable object'.format(obj))
2031
2032 if isinstance(obj, types.MethodType):
2033 # In this case we skip the first parameter of the underlying
2034 # function (usually `self` or `cls`).
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002035 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002036 obj.__func__,
2037 follow_wrapper_chains=follow_wrapper_chains,
2038 skip_bound_arg=skip_bound_arg,
2039 sigcls=sigcls)
2040
Yury Selivanov57d240e2014-02-19 16:27:23 -05002041 if skip_bound_arg:
2042 return _signature_bound_method(sig)
2043 else:
2044 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002045
Nick Coghlane8c45d62013-07-28 20:00:01 +10002046 # Was this function wrapped by a decorator?
Yury Selivanov57d240e2014-02-19 16:27:23 -05002047 if follow_wrapper_chains:
2048 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
Nick Coghlane8c45d62013-07-28 20:00:01 +10002049
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002050 try:
2051 sig = obj.__signature__
2052 except AttributeError:
2053 pass
2054 else:
2055 if sig is not None:
Yury Selivanov42407ab2014-06-23 10:23:50 -07002056 if not isinstance(sig, Signature):
2057 raise TypeError(
2058 'unexpected object {!r} in __signature__ '
2059 'attribute'.format(sig))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002060 return sig
2061
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002062 try:
2063 partialmethod = obj._partialmethod
2064 except AttributeError:
2065 pass
2066 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05002067 if isinstance(partialmethod, functools.partialmethod):
2068 # Unbound partialmethod (see functools.partialmethod)
2069 # This means, that we need to calculate the signature
2070 # as if it's a regular partial object, but taking into
2071 # account that the first positional argument
2072 # (usually `self`, or `cls`) will not be passed
2073 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002074
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002075 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002076 partialmethod.func,
2077 follow_wrapper_chains=follow_wrapper_chains,
2078 skip_bound_arg=skip_bound_arg,
2079 sigcls=sigcls)
2080
Yury Selivanov0486f812014-01-29 12:18:59 -05002081 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002082
Yury Selivanov0486f812014-01-29 12:18:59 -05002083 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
2084 new_params = (first_wrapped_param,) + tuple(sig.parameters.values())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002085
Yury Selivanov0486f812014-01-29 12:18:59 -05002086 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002087
Yury Selivanov63da7c72014-01-31 14:48:37 -05002088 if isfunction(obj) or _signature_is_functionlike(obj):
2089 # If it's a pure Python function, or an object that is duck type
2090 # of a Python function (Cython functions, for instance), then:
Yury Selivanovda396452014-03-27 12:09:24 -04002091 return sigcls.from_function(obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002092
Yury Selivanova773de02014-02-21 18:30:53 -05002093 if _signature_is_builtin(obj):
Yury Selivanovda396452014-03-27 12:09:24 -04002094 return _signature_from_builtin(sigcls, obj,
Yury Selivanova773de02014-02-21 18:30:53 -05002095 skip_bound_arg=skip_bound_arg)
2096
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002097 if isinstance(obj, functools.partial):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002098 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002099 obj.func,
2100 follow_wrapper_chains=follow_wrapper_chains,
2101 skip_bound_arg=skip_bound_arg,
2102 sigcls=sigcls)
Yury Selivanov62560fb2014-01-28 12:26:24 -05002103 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002104
2105 sig = None
2106 if isinstance(obj, type):
2107 # obj is a class or a metaclass
2108
2109 # First, let's see if it has an overloaded __call__ defined
2110 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05002111 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002112 if call is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002113 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002114 call,
2115 follow_wrapper_chains=follow_wrapper_chains,
2116 skip_bound_arg=skip_bound_arg,
2117 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002118 else:
2119 # Now we check if the 'obj' class has a '__new__' method
Yury Selivanov421f0c72014-01-29 12:05:40 -05002120 new = _signature_get_user_defined_method(obj, '__new__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002121 if new is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002122 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002123 new,
2124 follow_wrapper_chains=follow_wrapper_chains,
2125 skip_bound_arg=skip_bound_arg,
2126 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002127 else:
2128 # Finally, we should have at least __init__ implemented
Yury Selivanov421f0c72014-01-29 12:05:40 -05002129 init = _signature_get_user_defined_method(obj, '__init__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002130 if init is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002131 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002132 init,
2133 follow_wrapper_chains=follow_wrapper_chains,
2134 skip_bound_arg=skip_bound_arg,
2135 sigcls=sigcls)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002136
2137 if sig is None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002138 # At this point we know, that `obj` is a class, with no user-
2139 # defined '__init__', '__new__', or class-level '__call__'
2140
Larry Hastings2623c8c2014-02-08 22:15:29 -08002141 for base in obj.__mro__[:-1]:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002142 # Since '__text_signature__' is implemented as a
2143 # descriptor that extracts text signature from the
2144 # class docstring, if 'obj' is derived from a builtin
2145 # class, its own '__text_signature__' may be 'None'.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002146 # Therefore, we go through the MRO (except the last
2147 # class in there, which is 'object') to find the first
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002148 # class with non-empty text signature.
2149 try:
2150 text_sig = base.__text_signature__
2151 except AttributeError:
2152 pass
2153 else:
2154 if text_sig:
2155 # If 'obj' class has a __text_signature__ attribute:
2156 # return a signature based on it
Yury Selivanovda396452014-03-27 12:09:24 -04002157 return _signature_fromstr(sigcls, obj, text_sig)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002158
2159 # No '__text_signature__' was found for the 'obj' class.
2160 # Last option is to check if its '__init__' is
2161 # object.__init__ or type.__init__.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002162 if type not in obj.__mro__:
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002163 # We have a class (not metaclass), but no user-defined
2164 # __init__ or __new__ for it
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002165 if obj.__init__ is object.__init__:
2166 # Return a signature of 'object' builtin.
2167 return signature(object)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002168
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002169 elif not isinstance(obj, _NonUserDefinedCallables):
2170 # An object with __call__
2171 # We also check that the 'obj' is not an instance of
2172 # _WrapperDescriptor or _MethodWrapper to avoid
2173 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05002174 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002175 if call is not None:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002176 try:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002177 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002178 call,
2179 follow_wrapper_chains=follow_wrapper_chains,
2180 skip_bound_arg=skip_bound_arg,
2181 sigcls=sigcls)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002182 except ValueError as ex:
2183 msg = 'no signature found for {!r}'.format(obj)
2184 raise ValueError(msg) from ex
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002185
2186 if sig is not None:
2187 # For classes and objects we skip the first parameter of their
2188 # __call__, __new__, or __init__ methods
Yury Selivanov57d240e2014-02-19 16:27:23 -05002189 if skip_bound_arg:
2190 return _signature_bound_method(sig)
2191 else:
2192 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002193
2194 if isinstance(obj, types.BuiltinFunctionType):
2195 # Raise a nicer error message for builtins
2196 msg = 'no signature found for builtin function {!r}'.format(obj)
2197 raise ValueError(msg)
2198
2199 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2200
2201
2202class _void:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002203 """A private marker - used in Parameter & Signature."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002204
2205
2206class _empty:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002207 """Marker object for Signature.empty and Parameter.empty."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002208
2209
Yury Selivanov21e83a52014-03-27 11:23:13 -04002210class _ParameterKind(enum.IntEnum):
2211 POSITIONAL_ONLY = 0
2212 POSITIONAL_OR_KEYWORD = 1
2213 VAR_POSITIONAL = 2
2214 KEYWORD_ONLY = 3
2215 VAR_KEYWORD = 4
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002216
2217 def __str__(self):
Yury Selivanov21e83a52014-03-27 11:23:13 -04002218 return self._name_
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002219
2220
Yury Selivanov21e83a52014-03-27 11:23:13 -04002221_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
2222_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
2223_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
2224_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
2225_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002226
2227
2228class Parameter:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002229 """Represents a parameter in a function signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002230
2231 Has the following public attributes:
2232
2233 * name : str
2234 The name of the parameter as a string.
2235 * default : object
2236 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002237 parameter has no default value, this attribute is set to
2238 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002239 * annotation
2240 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002241 parameter has no annotation, this attribute is set to
2242 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002243 * kind : str
2244 Describes how argument values are bound to the parameter.
2245 Possible values: `Parameter.POSITIONAL_ONLY`,
2246 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
2247 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002248 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002249
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002250 __slots__ = ('_name', '_kind', '_default', '_annotation')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002251
2252 POSITIONAL_ONLY = _POSITIONAL_ONLY
2253 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
2254 VAR_POSITIONAL = _VAR_POSITIONAL
2255 KEYWORD_ONLY = _KEYWORD_ONLY
2256 VAR_KEYWORD = _VAR_KEYWORD
2257
2258 empty = _empty
2259
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002260 def __init__(self, name, kind, *, default=_empty, annotation=_empty):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002261
2262 if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
2263 _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
2264 raise ValueError("invalid value for 'Parameter.kind' attribute")
2265 self._kind = kind
2266
2267 if default is not _empty:
2268 if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2269 msg = '{} parameters cannot have default values'.format(kind)
2270 raise ValueError(msg)
2271 self._default = default
2272 self._annotation = annotation
2273
Yury Selivanov2393dca2014-01-27 15:07:58 -05002274 if name is _empty:
2275 raise ValueError('name is a required attribute for Parameter')
2276
2277 if not isinstance(name, str):
2278 raise TypeError("name must be a str, not a {!r}".format(name))
2279
2280 if not name.isidentifier():
2281 raise ValueError('{!r} is not a valid parameter name'.format(name))
2282
2283 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002284
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002285 def __reduce__(self):
2286 return (type(self),
2287 (self._name, self._kind),
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002288 {'_default': self._default,
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002289 '_annotation': self._annotation})
2290
2291 def __setstate__(self, state):
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002292 self._default = state['_default']
2293 self._annotation = state['_annotation']
2294
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002295 @property
2296 def name(self):
2297 return self._name
2298
2299 @property
2300 def default(self):
2301 return self._default
2302
2303 @property
2304 def annotation(self):
2305 return self._annotation
2306
2307 @property
2308 def kind(self):
2309 return self._kind
2310
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002311 def replace(self, *, name=_void, kind=_void,
2312 annotation=_void, default=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002313 """Creates a customized copy of the Parameter."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002314
2315 if name is _void:
2316 name = self._name
2317
2318 if kind is _void:
2319 kind = self._kind
2320
2321 if annotation is _void:
2322 annotation = self._annotation
2323
2324 if default is _void:
2325 default = self._default
2326
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002327 return type(self)(name, kind, default=default, annotation=annotation)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002328
2329 def __str__(self):
2330 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002331 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002332
2333 # Add annotation and default value
2334 if self._annotation is not _empty:
2335 formatted = '{}:{}'.format(formatted,
2336 formatannotation(self._annotation))
2337
2338 if self._default is not _empty:
2339 formatted = '{}={}'.format(formatted, repr(self._default))
2340
2341 if kind == _VAR_POSITIONAL:
2342 formatted = '*' + formatted
2343 elif kind == _VAR_KEYWORD:
2344 formatted = '**' + formatted
2345
2346 return formatted
2347
2348 def __repr__(self):
Yury Selivanov374375d2014-03-27 12:41:53 -04002349 return '<{} at {:#x} "{}">'.format(self.__class__.__name__,
2350 id(self), self)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002351
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002352 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002353 return hash((self.name, self.kind, self.annotation, self.default))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002354
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002355 def __eq__(self, other):
2356 return (issubclass(other.__class__, Parameter) and
2357 self._name == other._name and
2358 self._kind == other._kind and
2359 self._default == other._default and
2360 self._annotation == other._annotation)
2361
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002362
2363class BoundArguments:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002364 """Result of `Signature.bind` call. Holds the mapping of arguments
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002365 to the function's parameters.
2366
2367 Has the following public attributes:
2368
2369 * arguments : OrderedDict
2370 An ordered mutable mapping of parameters' names to arguments' values.
2371 Does not contain arguments' default values.
2372 * signature : Signature
2373 The Signature object that created this instance.
2374 * args : tuple
2375 Tuple of positional arguments values.
2376 * kwargs : dict
2377 Dict of keyword arguments values.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002378 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002379
2380 def __init__(self, signature, arguments):
2381 self.arguments = arguments
2382 self._signature = signature
2383
2384 @property
2385 def signature(self):
2386 return self._signature
2387
2388 @property
2389 def args(self):
2390 args = []
2391 for param_name, param in self._signature.parameters.items():
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002392 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002393 break
2394
2395 try:
2396 arg = self.arguments[param_name]
2397 except KeyError:
2398 # We're done here. Other arguments
2399 # will be mapped in 'BoundArguments.kwargs'
2400 break
2401 else:
2402 if param.kind == _VAR_POSITIONAL:
2403 # *args
2404 args.extend(arg)
2405 else:
2406 # plain argument
2407 args.append(arg)
2408
2409 return tuple(args)
2410
2411 @property
2412 def kwargs(self):
2413 kwargs = {}
2414 kwargs_started = False
2415 for param_name, param in self._signature.parameters.items():
2416 if not kwargs_started:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002417 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002418 kwargs_started = True
2419 else:
2420 if param_name not in self.arguments:
2421 kwargs_started = True
2422 continue
2423
2424 if not kwargs_started:
2425 continue
2426
2427 try:
2428 arg = self.arguments[param_name]
2429 except KeyError:
2430 pass
2431 else:
2432 if param.kind == _VAR_KEYWORD:
2433 # **kwargs
2434 kwargs.update(arg)
2435 else:
2436 # plain keyword argument
2437 kwargs[param_name] = arg
2438
2439 return kwargs
2440
2441 def __eq__(self, other):
2442 return (issubclass(other.__class__, BoundArguments) and
2443 self.signature == other.signature and
2444 self.arguments == other.arguments)
2445
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002446
2447class Signature:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002448 """A Signature object represents the overall signature of a function.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002449 It stores a Parameter object for each parameter accepted by the
2450 function, as well as information specific to the function itself.
2451
2452 A Signature object has the following public attributes and methods:
2453
2454 * parameters : OrderedDict
2455 An ordered mapping of parameters' names to the corresponding
2456 Parameter objects (keyword-only arguments are in the same order
2457 as listed in `code.co_varnames`).
2458 * return_annotation : object
2459 The annotation for the return type of the function if specified.
2460 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002461 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002462 * bind(*args, **kwargs) -> BoundArguments
2463 Creates a mapping from positional and keyword arguments to
2464 parameters.
2465 * bind_partial(*args, **kwargs) -> BoundArguments
2466 Creates a partial mapping from positional and keyword arguments
2467 to parameters (simulating 'functools.partial' behavior.)
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002468 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002469
2470 __slots__ = ('_return_annotation', '_parameters')
2471
2472 _parameter_cls = Parameter
2473 _bound_arguments_cls = BoundArguments
2474
2475 empty = _empty
2476
2477 def __init__(self, parameters=None, *, return_annotation=_empty,
2478 __validate_parameters__=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002479 """Constructs Signature from the given list of Parameter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002480 objects and 'return_annotation'. All arguments are optional.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002481 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002482
2483 if parameters is None:
2484 params = OrderedDict()
2485 else:
2486 if __validate_parameters__:
2487 params = OrderedDict()
2488 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002489 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002490
2491 for idx, param in enumerate(parameters):
2492 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002493 name = param.name
2494
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002495 if kind < top_kind:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002496 msg = 'wrong parameter order: {!r} before {!r}'
Yury Selivanov2393dca2014-01-27 15:07:58 -05002497 msg = msg.format(top_kind, kind)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002498 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002499 elif kind > top_kind:
2500 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002501 top_kind = kind
2502
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002503 if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
Yury Selivanov07a9e452014-01-29 10:58:16 -05002504 if param.default is _empty:
2505 if kind_defaults:
2506 # No default for this parameter, but the
2507 # previous parameter of the same kind had
2508 # a default
2509 msg = 'non-default argument follows default ' \
2510 'argument'
2511 raise ValueError(msg)
2512 else:
2513 # There is a default for this parameter.
2514 kind_defaults = True
2515
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002516 if name in params:
2517 msg = 'duplicate parameter name: {!r}'.format(name)
2518 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002519
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002520 params[name] = param
2521 else:
2522 params = OrderedDict(((param.name, param)
2523 for param in parameters))
2524
2525 self._parameters = types.MappingProxyType(params)
2526 self._return_annotation = return_annotation
2527
2528 @classmethod
2529 def from_function(cls, func):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002530 """Constructs Signature for the given python function."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002531
Yury Selivanov5334bcd2014-01-31 15:21:51 -05002532 is_duck_function = False
2533 if not isfunction(func):
2534 if _signature_is_functionlike(func):
2535 is_duck_function = True
2536 else:
2537 # If it's not a pure Python function, and not a duck type
2538 # of pure function:
2539 raise TypeError('{!r} is not a Python function'.format(func))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002540
2541 Parameter = cls._parameter_cls
2542
2543 # Parameter information.
2544 func_code = func.__code__
2545 pos_count = func_code.co_argcount
2546 arg_names = func_code.co_varnames
2547 positional = tuple(arg_names[:pos_count])
2548 keyword_only_count = func_code.co_kwonlyargcount
2549 keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)]
2550 annotations = func.__annotations__
2551 defaults = func.__defaults__
2552 kwdefaults = func.__kwdefaults__
2553
2554 if defaults:
2555 pos_default_count = len(defaults)
2556 else:
2557 pos_default_count = 0
2558
2559 parameters = []
2560
2561 # Non-keyword-only parameters w/o defaults.
2562 non_default_count = pos_count - pos_default_count
2563 for name in positional[:non_default_count]:
2564 annotation = annotations.get(name, _empty)
2565 parameters.append(Parameter(name, annotation=annotation,
2566 kind=_POSITIONAL_OR_KEYWORD))
2567
2568 # ... w/ defaults.
2569 for offset, name in enumerate(positional[non_default_count:]):
2570 annotation = annotations.get(name, _empty)
2571 parameters.append(Parameter(name, annotation=annotation,
2572 kind=_POSITIONAL_OR_KEYWORD,
2573 default=defaults[offset]))
2574
2575 # *args
Yury Selivanov89ca85c2014-01-29 16:50:40 -05002576 if func_code.co_flags & CO_VARARGS:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002577 name = arg_names[pos_count + keyword_only_count]
2578 annotation = annotations.get(name, _empty)
2579 parameters.append(Parameter(name, annotation=annotation,
2580 kind=_VAR_POSITIONAL))
2581
2582 # Keyword-only parameters.
2583 for name in keyword_only:
2584 default = _empty
2585 if kwdefaults is not None:
2586 default = kwdefaults.get(name, _empty)
2587
2588 annotation = annotations.get(name, _empty)
2589 parameters.append(Parameter(name, annotation=annotation,
2590 kind=_KEYWORD_ONLY,
2591 default=default))
2592 # **kwargs
Yury Selivanov89ca85c2014-01-29 16:50:40 -05002593 if func_code.co_flags & CO_VARKEYWORDS:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002594 index = pos_count + keyword_only_count
Yury Selivanov89ca85c2014-01-29 16:50:40 -05002595 if func_code.co_flags & CO_VARARGS:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002596 index += 1
2597
2598 name = arg_names[index]
2599 annotation = annotations.get(name, _empty)
2600 parameters.append(Parameter(name, annotation=annotation,
2601 kind=_VAR_KEYWORD))
2602
Yury Selivanov5334bcd2014-01-31 15:21:51 -05002603 # Is 'func' is a pure Python function - don't validate the
2604 # parameters list (for correct order and defaults), it should be OK.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002605 return cls(parameters,
2606 return_annotation=annotations.get('return', _empty),
Yury Selivanov5334bcd2014-01-31 15:21:51 -05002607 __validate_parameters__=is_duck_function)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002608
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002609 @classmethod
2610 def from_builtin(cls, func):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002611 """Constructs Signature for the given builtin function."""
Yury Selivanov57d240e2014-02-19 16:27:23 -05002612 return _signature_from_builtin(cls, func)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002613
Yury Selivanovda396452014-03-27 12:09:24 -04002614 @classmethod
2615 def from_callable(cls, obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002616 """Constructs Signature for the given callable object."""
2617 return _signature_from_callable(obj, sigcls=cls)
Yury Selivanovda396452014-03-27 12:09:24 -04002618
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002619 @property
2620 def parameters(self):
2621 return self._parameters
2622
2623 @property
2624 def return_annotation(self):
2625 return self._return_annotation
2626
2627 def replace(self, *, parameters=_void, return_annotation=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002628 """Creates a customized copy of the Signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002629 Pass 'parameters' and/or 'return_annotation' arguments
2630 to override them in the new copy.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002631 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002632
2633 if parameters is _void:
2634 parameters = self.parameters.values()
2635
2636 if return_annotation is _void:
2637 return_annotation = self._return_annotation
2638
2639 return type(self)(parameters,
2640 return_annotation=return_annotation)
2641
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002642 def _hash_basis(self):
2643 params = tuple(param for param in self.parameters.values()
2644 if param.kind != _KEYWORD_ONLY)
2645
2646 kwo_params = {param.name: param for param in self.parameters.values()
2647 if param.kind == _KEYWORD_ONLY}
2648
2649 return params, kwo_params, self.return_annotation
2650
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002651 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002652 params, kwo_params, return_annotation = self._hash_basis()
2653 kwo_params = frozenset(kwo_params.values())
2654 return hash((params, kwo_params, return_annotation))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002655
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002656 def __eq__(self, other):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002657 return (isinstance(other, Signature) and
2658 self._hash_basis() == other._hash_basis())
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002659
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002660 def _bind(self, args, kwargs, *, partial=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002661 """Private method. Don't use directly."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002662
2663 arguments = OrderedDict()
2664
2665 parameters = iter(self.parameters.values())
2666 parameters_ex = ()
2667 arg_vals = iter(args)
2668
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002669 while True:
2670 # Let's iterate through the positional arguments and corresponding
2671 # parameters
2672 try:
2673 arg_val = next(arg_vals)
2674 except StopIteration:
2675 # No more positional arguments
2676 try:
2677 param = next(parameters)
2678 except StopIteration:
2679 # No more parameters. That's it. Just need to check that
2680 # we have no `kwargs` after this while loop
2681 break
2682 else:
2683 if param.kind == _VAR_POSITIONAL:
2684 # That's OK, just empty *args. Let's start parsing
2685 # kwargs
2686 break
2687 elif param.name in kwargs:
2688 if param.kind == _POSITIONAL_ONLY:
2689 msg = '{arg!r} parameter is positional only, ' \
2690 'but was passed as a keyword'
2691 msg = msg.format(arg=param.name)
2692 raise TypeError(msg) from None
2693 parameters_ex = (param,)
2694 break
2695 elif (param.kind == _VAR_KEYWORD or
2696 param.default is not _empty):
2697 # That's fine too - we have a default value for this
2698 # parameter. So, lets start parsing `kwargs`, starting
2699 # with the current parameter
2700 parameters_ex = (param,)
2701 break
2702 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002703 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
2704 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002705 if partial:
2706 parameters_ex = (param,)
2707 break
2708 else:
2709 msg = '{arg!r} parameter lacking default value'
2710 msg = msg.format(arg=param.name)
2711 raise TypeError(msg) from None
2712 else:
2713 # We have a positional argument to process
2714 try:
2715 param = next(parameters)
2716 except StopIteration:
2717 raise TypeError('too many positional arguments') from None
2718 else:
2719 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
2720 # Looks like we have no parameter for this positional
2721 # argument
2722 raise TypeError('too many positional arguments')
2723
2724 if param.kind == _VAR_POSITIONAL:
2725 # We have an '*args'-like argument, let's fill it with
2726 # all positional arguments we have left and move on to
2727 # the next phase
2728 values = [arg_val]
2729 values.extend(arg_vals)
2730 arguments[param.name] = tuple(values)
2731 break
2732
2733 if param.name in kwargs:
2734 raise TypeError('multiple values for argument '
2735 '{arg!r}'.format(arg=param.name))
2736
2737 arguments[param.name] = arg_val
2738
2739 # Now, we iterate through the remaining parameters to process
2740 # keyword arguments
2741 kwargs_param = None
2742 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002743 if param.kind == _VAR_KEYWORD:
2744 # Memorize that we have a '**kwargs'-like parameter
2745 kwargs_param = param
2746 continue
2747
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002748 if param.kind == _VAR_POSITIONAL:
2749 # Named arguments don't refer to '*args'-like parameters.
2750 # We only arrive here if the positional arguments ended
2751 # before reaching the last parameter before *args.
2752 continue
2753
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002754 param_name = param.name
2755 try:
2756 arg_val = kwargs.pop(param_name)
2757 except KeyError:
2758 # We have no value for this parameter. It's fine though,
2759 # if it has a default value, or it is an '*args'-like
2760 # parameter, left alone by the processing of positional
2761 # arguments.
2762 if (not partial and param.kind != _VAR_POSITIONAL and
2763 param.default is _empty):
2764 raise TypeError('{arg!r} parameter lacking default value'. \
2765 format(arg=param_name)) from None
2766
2767 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05002768 if param.kind == _POSITIONAL_ONLY:
2769 # This should never happen in case of a properly built
2770 # Signature object (but let's have this check here
2771 # to ensure correct behaviour just in case)
2772 raise TypeError('{arg!r} parameter is positional only, '
2773 'but was passed as a keyword'. \
2774 format(arg=param.name))
2775
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002776 arguments[param_name] = arg_val
2777
2778 if kwargs:
2779 if kwargs_param is not None:
2780 # Process our '**kwargs'-like parameter
2781 arguments[kwargs_param.name] = kwargs
2782 else:
2783 raise TypeError('too many keyword arguments')
2784
2785 return self._bound_arguments_cls(self, arguments)
2786
Yury Selivanovc45873e2014-01-29 12:10:27 -05002787 def bind(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002788 """Get a BoundArguments object, that maps the passed `args`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002789 and `kwargs` to the function's signature. Raises `TypeError`
2790 if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002791 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05002792 return args[0]._bind(args[1:], kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002793
Yury Selivanovc45873e2014-01-29 12:10:27 -05002794 def bind_partial(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002795 """Get a BoundArguments object, that partially maps the
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002796 passed `args` and `kwargs` to the function's signature.
2797 Raises `TypeError` if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002798 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05002799 return args[0]._bind(args[1:], kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002800
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002801 def __reduce__(self):
2802 return (type(self),
2803 (tuple(self._parameters.values()),),
2804 {'_return_annotation': self._return_annotation})
2805
2806 def __setstate__(self, state):
2807 self._return_annotation = state['_return_annotation']
2808
Yury Selivanov374375d2014-03-27 12:41:53 -04002809 def __repr__(self):
2810 return '<{} at {:#x} "{}">'.format(self.__class__.__name__,
2811 id(self), self)
2812
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002813 def __str__(self):
2814 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05002815 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002816 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05002817 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002818 formatted = str(param)
2819
2820 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002821
2822 if kind == _POSITIONAL_ONLY:
2823 render_pos_only_separator = True
2824 elif render_pos_only_separator:
2825 # It's not a positional-only parameter, and the flag
2826 # is set to 'True' (there were pos-only params before.)
2827 result.append('/')
2828 render_pos_only_separator = False
2829
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002830 if kind == _VAR_POSITIONAL:
2831 # OK, we have an '*args'-like parameter, so we won't need
2832 # a '*' to separate keyword-only arguments
2833 render_kw_only_separator = False
2834 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
2835 # We have a keyword-only parameter to render and we haven't
2836 # rendered an '*args'-like parameter before, so add a '*'
2837 # separator to the parameters list ("foo(arg1, *, arg2)" case)
2838 result.append('*')
2839 # This condition should be only triggered once, so
2840 # reset the flag
2841 render_kw_only_separator = False
2842
2843 result.append(formatted)
2844
Yury Selivanov2393dca2014-01-27 15:07:58 -05002845 if render_pos_only_separator:
2846 # There were only positional-only parameters, hence the
2847 # flag was not reset to 'False'
2848 result.append('/')
2849
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002850 rendered = '({})'.format(', '.join(result))
2851
2852 if self.return_annotation is not _empty:
2853 anno = formatannotation(self.return_annotation)
2854 rendered += ' -> {}'.format(anno)
2855
2856 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10002857
Yury Selivanovda396452014-03-27 12:09:24 -04002858
2859def signature(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002860 """Get a signature object for the passed callable."""
Yury Selivanovda396452014-03-27 12:09:24 -04002861 return Signature.from_callable(obj)
2862
2863
Nick Coghlanf94a16b2013-09-22 22:46:49 +10002864def _main():
2865 """ Logic for inspecting an object given at command line """
2866 import argparse
2867 import importlib
2868
2869 parser = argparse.ArgumentParser()
2870 parser.add_argument(
2871 'object',
2872 help="The object to be analysed. "
2873 "It supports the 'module:qualname' syntax")
2874 parser.add_argument(
2875 '-d', '--details', action='store_true',
2876 help='Display info about the module rather than its source code')
2877
2878 args = parser.parse_args()
2879
2880 target = args.object
2881 mod_name, has_attrs, attrs = target.partition(":")
2882 try:
2883 obj = module = importlib.import_module(mod_name)
2884 except Exception as exc:
2885 msg = "Failed to import {} ({}: {})".format(mod_name,
2886 type(exc).__name__,
2887 exc)
2888 print(msg, file=sys.stderr)
2889 exit(2)
2890
2891 if has_attrs:
2892 parts = attrs.split(".")
2893 obj = module
2894 for part in parts:
2895 obj = getattr(obj, part)
2896
2897 if module.__name__ in sys.builtin_module_names:
2898 print("Can't get info for builtin modules.", file=sys.stderr)
2899 exit(1)
2900
2901 if args.details:
2902 print('Target: {}'.format(target))
2903 print('Origin: {}'.format(getsourcefile(module)))
2904 print('Cached: {}'.format(module.__cached__))
2905 if obj is module:
2906 print('Loader: {}'.format(repr(module.__loader__)))
2907 if hasattr(module, '__path__'):
2908 print('Submodule search path: {}'.format(module.__path__))
2909 else:
2910 try:
2911 __, lineno = findsource(obj)
2912 except Exception:
2913 pass
2914 else:
2915 print('Line: {}'.format(lineno))
2916
2917 print('\n')
2918 else:
2919 print(getsource(obj))
2920
2921
2922if __name__ == "__main__":
2923 _main()