blob: f48769e514473a5ccdc0ba1cbf1231f453fc4bb9 [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 Selivanov5376ba92015-06-22 12:19:30 -0400178 object.__code__.co_flags & CO_GENERATOR)
Yury Selivanov75445082015-05-11 22:57:16 -0400179
180def iscoroutinefunction(object):
181 """Return true if the object is a coroutine function.
182
183 Coroutine functions are defined with "async def" syntax,
184 or generators decorated with "types.coroutine".
185 """
186 return bool((isfunction(object) or ismethod(object)) and
Yury Selivanov5376ba92015-06-22 12:19:30 -0400187 object.__code__.co_flags & CO_COROUTINE)
Yury Selivanov75445082015-05-11 22:57:16 -0400188
Christian Heimes7131fd92008-02-19 14:21:46 +0000189def isgenerator(object):
190 """Return true if the object is a generator.
191
192 Generator objects provide these attributes:
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300193 __iter__ defined to support iteration over container
Christian Heimes7131fd92008-02-19 14:21:46 +0000194 close raises a new GeneratorExit exception inside the
195 generator to terminate the iteration
196 gi_code code object
197 gi_frame frame object or possibly None once the generator has
198 been exhausted
199 gi_running set to 1 when generator is executing, 0 otherwise
200 next return the next item from the container
201 send resumes the generator and "sends" a value that becomes
202 the result of the current yield-expression
203 throw used to raise an exception inside the generator"""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400204 return isinstance(object, types.GeneratorType)
Yury Selivanov75445082015-05-11 22:57:16 -0400205
206def iscoroutine(object):
207 """Return true if the object is a coroutine."""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400208 return isinstance(object, types.CoroutineType)
Christian Heimes7131fd92008-02-19 14:21:46 +0000209
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000210def istraceback(object):
211 """Return true if the object is a traceback.
212
213 Traceback objects provide these attributes:
214 tb_frame frame object at this level
215 tb_lasti index of last attempted instruction in bytecode
216 tb_lineno current line number in Python source code
217 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000218 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000219
220def isframe(object):
221 """Return true if the object is a frame object.
222
223 Frame objects provide these attributes:
224 f_back next outer frame object (this frame's caller)
225 f_builtins built-in namespace seen by this frame
226 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000227 f_globals global namespace seen by this frame
228 f_lasti index of last attempted instruction in bytecode
229 f_lineno current line number in Python source code
230 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000231 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000232 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000233
234def iscode(object):
235 """Return true if the object is a code object.
236
237 Code objects provide these attributes:
238 co_argcount number of arguments (not including * or ** args)
239 co_code string of raw compiled bytecode
240 co_consts tuple of constants used in the bytecode
241 co_filename name of file in which this code object was created
242 co_firstlineno number of first line in Python source code
243 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
244 co_lnotab encoded mapping of line numbers to bytecode indices
245 co_name name with which this code object was defined
246 co_names tuple of names of local variables
247 co_nlocals number of local variables
248 co_stacksize virtual machine stack space required
249 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000250 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000251
252def isbuiltin(object):
253 """Return true if the object is a built-in function or method.
254
255 Built-in functions and methods provide these attributes:
256 __doc__ documentation string
257 __name__ original name of this function or method
258 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000259 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000260
261def isroutine(object):
262 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000263 return (isbuiltin(object)
264 or isfunction(object)
265 or ismethod(object)
266 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000267
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000268def isabstract(object):
269 """Return true if the object is an abstract base class (ABC)."""
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000270 return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT)
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000271
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000272def getmembers(object, predicate=None):
273 """Return all members of an object as (name, value) pairs sorted by name.
274 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100275 if isclass(object):
276 mro = (object,) + getmro(object)
277 else:
278 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000279 results = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700280 processed = set()
281 names = dir(object)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700282 # :dd any DynamicClassAttributes to the list of names if object is a class;
Ethan Furmane03ea372013-09-25 07:14:41 -0700283 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700284 # attribute with the same name as a DynamicClassAttribute exists
Ethan Furmane03ea372013-09-25 07:14:41 -0700285 try:
286 for base in object.__bases__:
287 for k, v in base.__dict__.items():
288 if isinstance(v, types.DynamicClassAttribute):
289 names.append(k)
290 except AttributeError:
291 pass
292 for key in names:
Ethan Furman63c141c2013-10-18 00:27:39 -0700293 # First try to get the value via getattr. Some descriptors don't
294 # like calling their __get__ (see bug #1785), so fall back to
295 # looking in the __dict__.
296 try:
297 value = getattr(object, key)
298 # handle the duplicate key
299 if key in processed:
300 raise AttributeError
301 except AttributeError:
302 for base in mro:
303 if key in base.__dict__:
304 value = base.__dict__[key]
305 break
306 else:
307 # could be a (currently) missing slot member, or a buggy
308 # __dir__; discard and move on
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100309 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000310 if not predicate or predicate(value):
311 results.append((key, value))
Ethan Furmane03ea372013-09-25 07:14:41 -0700312 processed.add(key)
313 results.sort(key=lambda pair: pair[0])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000314 return results
315
Christian Heimes25bb7832008-01-11 16:17:00 +0000316Attribute = namedtuple('Attribute', 'name kind defining_class object')
317
Tim Peters13b49d32001-09-23 02:00:29 +0000318def classify_class_attrs(cls):
319 """Return list of attribute-descriptor tuples.
320
321 For each name in dir(cls), the return list contains a 4-tuple
322 with these elements:
323
324 0. The name (a string).
325
326 1. The kind of attribute this is, one of these strings:
327 'class method' created via classmethod()
328 'static method' created via staticmethod()
329 'property' created via property()
Ethan Furmane03ea372013-09-25 07:14:41 -0700330 'method' any other flavor of method or descriptor
Tim Peters13b49d32001-09-23 02:00:29 +0000331 'data' not a method
332
333 2. The class which defined this attribute (a class).
334
Ethan Furmane03ea372013-09-25 07:14:41 -0700335 3. The object as obtained by calling getattr; if this fails, or if the
336 resulting object does not live anywhere in the class' mro (including
337 metaclasses) then the object is looked up in the defining class's
338 dict (found by walking the mro).
Ethan Furman668dede2013-09-14 18:53:26 -0700339
340 If one of the items in dir(cls) is stored in the metaclass it will now
341 be discovered and not have None be listed as the class in which it was
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700342 defined. Any items whose home class cannot be discovered are skipped.
Tim Peters13b49d32001-09-23 02:00:29 +0000343 """
344
345 mro = getmro(cls)
Ethan Furman668dede2013-09-14 18:53:26 -0700346 metamro = getmro(type(cls)) # for attributes stored in the metaclass
Ethan Furmane03ea372013-09-25 07:14:41 -0700347 metamro = tuple([cls for cls in metamro if cls not in (type, object)])
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700348 class_bases = (cls,) + mro
349 all_bases = class_bases + metamro
Tim Peters13b49d32001-09-23 02:00:29 +0000350 names = dir(cls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700351 # :dd any DynamicClassAttributes to the list of names;
Ethan Furmane03ea372013-09-25 07:14:41 -0700352 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700353 # attribute with the same name as a DynamicClassAttribute exists.
Ethan Furman63c141c2013-10-18 00:27:39 -0700354 for base in mro:
Ethan Furmane03ea372013-09-25 07:14:41 -0700355 for k, v in base.__dict__.items():
356 if isinstance(v, types.DynamicClassAttribute):
357 names.append(k)
Tim Peters13b49d32001-09-23 02:00:29 +0000358 result = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700359 processed = set()
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700360
Tim Peters13b49d32001-09-23 02:00:29 +0000361 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100362 # Get the object associated with the name, and where it was defined.
Ethan Furmane03ea372013-09-25 07:14:41 -0700363 # Normal objects will be looked up with both getattr and directly in
364 # its class' dict (in case getattr fails [bug #1785], and also to look
365 # for a docstring).
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700366 # For DynamicClassAttributes on the second pass we only look in the
Ethan Furmane03ea372013-09-25 07:14:41 -0700367 # class's dict.
368 #
Tim Peters13b49d32001-09-23 02:00:29 +0000369 # Getting an obj from the __dict__ sometimes reveals more than
370 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100371 homecls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700372 get_obj = None
373 dict_obj = None
Ethan Furmane03ea372013-09-25 07:14:41 -0700374 if name not in processed:
375 try:
Ethan Furmana8b07072013-10-18 01:22:08 -0700376 if name == '__dict__':
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700377 raise Exception("__dict__ is special, don't want the proxy")
Ethan Furmane03ea372013-09-25 07:14:41 -0700378 get_obj = getattr(cls, name)
379 except Exception as exc:
380 pass
381 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700382 homecls = getattr(get_obj, "__objclass__", homecls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700383 if homecls not in class_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700384 # if the resulting object does not live somewhere in the
Ethan Furman63c141c2013-10-18 00:27:39 -0700385 # mro, drop it and search the mro manually
Ethan Furmane03ea372013-09-25 07:14:41 -0700386 homecls = None
Ethan Furman63c141c2013-10-18 00:27:39 -0700387 last_cls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700388 # first look in the classes
389 for srch_cls in class_bases:
Ethan Furman63c141c2013-10-18 00:27:39 -0700390 srch_obj = getattr(srch_cls, name, None)
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400391 if srch_obj is get_obj:
Ethan Furman63c141c2013-10-18 00:27:39 -0700392 last_cls = srch_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700393 # then check the metaclasses
394 for srch_cls in metamro:
395 try:
396 srch_obj = srch_cls.__getattr__(cls, name)
397 except AttributeError:
398 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400399 if srch_obj is get_obj:
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700400 last_cls = srch_cls
Ethan Furman63c141c2013-10-18 00:27:39 -0700401 if last_cls is not None:
402 homecls = last_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700403 for base in all_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700404 if name in base.__dict__:
405 dict_obj = base.__dict__[name]
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700406 if homecls not in metamro:
407 homecls = base
Ethan Furmane03ea372013-09-25 07:14:41 -0700408 break
Ethan Furman63c141c2013-10-18 00:27:39 -0700409 if homecls is None:
410 # unable to locate the attribute anywhere, most likely due to
411 # buggy custom __dir__; discard and move on
412 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400413 obj = get_obj if get_obj is not None else dict_obj
Ethan Furmane03ea372013-09-25 07:14:41 -0700414 # Classify the object or its descriptor.
Ethan Furman63c141c2013-10-18 00:27:39 -0700415 if isinstance(dict_obj, staticmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000416 kind = "static method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700417 obj = dict_obj
Ethan Furman63c141c2013-10-18 00:27:39 -0700418 elif isinstance(dict_obj, classmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000419 kind = "class method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700420 obj = dict_obj
421 elif isinstance(dict_obj, property):
Tim Peters13b49d32001-09-23 02:00:29 +0000422 kind = "property"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700423 obj = dict_obj
Yury Selivanov0860a0b2014-01-31 14:28:44 -0500424 elif isroutine(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000425 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100426 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700427 kind = "data"
Christian Heimes25bb7832008-01-11 16:17:00 +0000428 result.append(Attribute(name, kind, homecls, obj))
Ethan Furmane03ea372013-09-25 07:14:41 -0700429 processed.add(name)
Tim Peters13b49d32001-09-23 02:00:29 +0000430 return result
431
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000432# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000433
434def getmro(cls):
435 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000436 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000437
Nick Coghlane8c45d62013-07-28 20:00:01 +1000438# -------------------------------------------------------- function helpers
439
440def unwrap(func, *, stop=None):
441 """Get the object wrapped by *func*.
442
443 Follows the chain of :attr:`__wrapped__` attributes returning the last
444 object in the chain.
445
446 *stop* is an optional callback accepting an object in the wrapper chain
447 as its sole argument that allows the unwrapping to be terminated early if
448 the callback returns a true value. If the callback never returns a true
449 value, the last object in the chain is returned as usual. For example,
450 :func:`signature` uses this to stop unwrapping if any object in the
451 chain has a ``__signature__`` attribute defined.
452
453 :exc:`ValueError` is raised if a cycle is encountered.
454
455 """
456 if stop is None:
457 def _is_wrapper(f):
458 return hasattr(f, '__wrapped__')
459 else:
460 def _is_wrapper(f):
461 return hasattr(f, '__wrapped__') and not stop(f)
462 f = func # remember the original func for error reporting
463 memo = {id(f)} # Memoise by id to tolerate non-hashable objects
464 while _is_wrapper(func):
465 func = func.__wrapped__
466 id_func = id(func)
467 if id_func in memo:
468 raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
469 memo.add(id_func)
470 return func
471
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000472# -------------------------------------------------- source code extraction
473def indentsize(line):
474 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000475 expline = line.expandtabs()
476 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000477
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300478def _findclass(func):
479 cls = sys.modules.get(func.__module__)
480 if cls is None:
481 return None
482 for name in func.__qualname__.split('.')[:-1]:
483 cls = getattr(cls, name)
484 if not isclass(cls):
485 return None
486 return cls
487
488def _finddoc(obj):
489 if isclass(obj):
490 for base in obj.__mro__:
491 if base is not object:
492 try:
493 doc = base.__doc__
494 except AttributeError:
495 continue
496 if doc is not None:
497 return doc
498 return None
499
500 if ismethod(obj):
501 name = obj.__func__.__name__
502 self = obj.__self__
503 if (isclass(self) and
504 getattr(getattr(self, name, None), '__func__') is obj.__func__):
505 # classmethod
506 cls = self
507 else:
508 cls = self.__class__
509 elif isfunction(obj):
510 name = obj.__name__
511 cls = _findclass(obj)
512 if cls is None or getattr(cls, name) is not obj:
513 return None
514 elif isbuiltin(obj):
515 name = obj.__name__
516 self = obj.__self__
517 if (isclass(self) and
518 self.__qualname__ + '.' + name == obj.__qualname__):
519 # classmethod
520 cls = self
521 else:
522 cls = self.__class__
523 elif ismethoddescriptor(obj) or isdatadescriptor(obj):
524 name = obj.__name__
525 cls = obj.__objclass__
526 if getattr(cls, name) is not obj:
527 return None
528 elif isinstance(obj, property):
529 func = f.fget
530 name = func.__name__
531 cls = _findclass(func)
532 if cls is None or getattr(cls, name) is not obj:
533 return None
534 else:
535 return None
536
537 for base in cls.__mro__:
538 try:
539 doc = getattr(base, name).__doc__
540 except AttributeError:
541 continue
542 if doc is not None:
543 return doc
544 return None
545
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000546def getdoc(object):
547 """Get the documentation string for an object.
548
549 All tabs are expanded to spaces. To clean up docstrings that are
550 indented to line up with blocks of code, any whitespace than can be
551 uniformly removed from the second line onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000552 try:
553 doc = object.__doc__
554 except AttributeError:
555 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300556 if doc is None:
557 try:
558 doc = _finddoc(object)
559 except (AttributeError, TypeError):
560 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000561 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000562 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000563 return cleandoc(doc)
564
565def cleandoc(doc):
566 """Clean up indentation from docstrings.
567
568 Any whitespace that can be uniformly removed from the second line
569 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000570 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000571 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000572 except UnicodeError:
573 return None
574 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000575 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000576 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000577 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000578 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000579 if content:
580 indent = len(line) - content
581 margin = min(margin, indent)
582 # Remove indentation.
583 if lines:
584 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000585 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000586 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000587 # Remove any trailing or leading blank lines.
588 while lines and not lines[-1]:
589 lines.pop()
590 while lines and not lines[0]:
591 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000592 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000593
594def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000595 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000596 if ismodule(object):
597 if hasattr(object, '__file__'):
598 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000599 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000600 if isclass(object):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500601 if hasattr(object, '__module__'):
602 object = sys.modules.get(object.__module__)
603 if hasattr(object, '__file__'):
604 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000605 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000606 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000607 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000608 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000609 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000610 if istraceback(object):
611 object = object.tb_frame
612 if isframe(object):
613 object = object.f_code
614 if iscode(object):
615 return object.co_filename
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000616 raise TypeError('{!r} is not a module, class, method, '
617 'function, traceback, frame, or code object'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000618
Christian Heimes25bb7832008-01-11 16:17:00 +0000619ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')
620
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000621def getmoduleinfo(path):
622 """Get the module name, suffix, mode, and module type for a given file."""
Brett Cannoncb66eb02012-05-11 12:58:42 -0400623 warnings.warn('inspect.getmoduleinfo() is deprecated', DeprecationWarning,
624 2)
Brett Cannone4f41de2013-06-16 13:13:40 -0400625 with warnings.catch_warnings():
626 warnings.simplefilter('ignore', PendingDeprecationWarning)
627 import imp
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000628 filename = os.path.basename(path)
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000629 suffixes = [(-len(suffix), suffix, mode, mtype)
630 for suffix, mode, mtype in imp.get_suffixes()]
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000631 suffixes.sort() # try longest suffixes first, in case they overlap
632 for neglen, suffix, mode, mtype in suffixes:
633 if filename[neglen:] == suffix:
Christian Heimes25bb7832008-01-11 16:17:00 +0000634 return ModuleInfo(filename[:neglen], suffix, mode, mtype)
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000635
636def getmodulename(path):
637 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000638 fname = os.path.basename(path)
639 # Check for paths that look like an actual module file
640 suffixes = [(-len(suffix), suffix)
641 for suffix in importlib.machinery.all_suffixes()]
642 suffixes.sort() # try longest suffixes first, in case they overlap
643 for neglen, suffix in suffixes:
644 if fname.endswith(suffix):
645 return fname[:neglen]
646 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000647
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000648def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000649 """Return the filename that can be used to locate an object's source.
650 Return None if no way can be identified to get the source.
651 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000652 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400653 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
654 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
655 if any(filename.endswith(s) for s in all_bytecode_suffixes):
656 filename = (os.path.splitext(filename)[0] +
657 importlib.machinery.SOURCE_SUFFIXES[0])
658 elif any(filename.endswith(s) for s in
659 importlib.machinery.EXTENSION_SUFFIXES):
660 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000661 if os.path.exists(filename):
662 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000663 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400664 if getattr(getmodule(object, filename), '__loader__', None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000665 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000666 # or it is in the linecache
667 if filename in linecache.cache:
668 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000669
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000670def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000671 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000672
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000673 The idea is for each object to have a unique origin, so this routine
674 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000675 if _filename is None:
676 _filename = getsourcefile(object) or getfile(object)
677 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000678
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000679modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000680_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000681
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000682def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000683 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000684 if ismodule(object):
685 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000686 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000687 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000688 # Try the filename to modulename cache
689 if _filename is not None and _filename in modulesbyfile:
690 return sys.modules.get(modulesbyfile[_filename])
691 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000692 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000693 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000694 except TypeError:
695 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000696 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000697 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000698 # Update the filename to module name cache and check yet again
699 # Copy sys.modules in order to cope with changes while iterating
Éric Araujoa74f8ef2011-11-29 16:58:53 +0100700 for modname, module in list(sys.modules.items()):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000701 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000702 f = module.__file__
703 if f == _filesbymodname.get(modname, None):
704 # Have already mapped this module, so skip it
705 continue
706 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000707 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000708 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000709 modulesbyfile[f] = modulesbyfile[
710 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000711 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000712 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000713 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000714 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000715 if not hasattr(object, '__name__'):
716 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000717 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000718 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000719 if mainobject is object:
720 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000721 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000722 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000723 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000724 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000725 if builtinobject is object:
726 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000727
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000728def findsource(object):
729 """Return the entire source file and starting line number for an object.
730
731 The argument may be a module, class, method, function, traceback, frame,
732 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200733 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000734 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500735
Yury Selivanovef1e7502014-12-08 16:05:34 -0500736 file = getsourcefile(object)
737 if file:
738 # Invalidate cache if needed.
739 linecache.checkcache(file)
740 else:
741 file = getfile(object)
742 # Allow filenames in form of "<something>" to pass through.
743 # `doctest` monkeypatches `linecache` module to enable
744 # inspection, so let `linecache.getlines` to be called.
745 if not (file.startswith('<') and file.endswith('>')):
746 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500747
Thomas Wouters89f507f2006-12-13 04:49:30 +0000748 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000749 if module:
750 lines = linecache.getlines(file, module.__dict__)
751 else:
752 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000753 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200754 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000755
756 if ismodule(object):
757 return lines, 0
758
759 if isclass(object):
760 name = object.__name__
Thomas Wouters89f507f2006-12-13 04:49:30 +0000761 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
762 # make some effort to find the best matching class definition:
763 # use the one with the least indentation, which is the one
764 # that's most probably not inside a function definition.
765 candidates = []
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000766 for i in range(len(lines)):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000767 match = pat.match(lines[i])
768 if match:
769 # if it's at toplevel, it's already the best one
770 if lines[i][0] == 'c':
771 return lines, i
772 # else add whitespace to candidate list
773 candidates.append((match.group(1), i))
774 if candidates:
775 # this will sort by whitespace, and by line number,
776 # less whitespace first
777 candidates.sort()
778 return lines, candidates[0][1]
Jeremy Hyltonab919022003-06-27 18:41:20 +0000779 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200780 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000781
782 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000783 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000784 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000785 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000786 if istraceback(object):
787 object = object.tb_frame
788 if isframe(object):
789 object = object.f_code
790 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000791 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200792 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000793 lnum = object.co_firstlineno - 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000794 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000795 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000796 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000797 lnum = lnum - 1
798 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200799 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000800
801def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000802 """Get lines of comments immediately preceding an object's source code.
803
804 Returns None when source can't be found.
805 """
806 try:
807 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200808 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000809 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000810
811 if ismodule(object):
812 # Look for a comment block at the top of the file.
813 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000814 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000815 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000816 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000817 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000818 comments = []
819 end = start
820 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000821 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000822 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000823 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000824
825 # Look for a preceding block of comments at the same indentation.
826 elif lnum > 0:
827 indent = indentsize(lines[lnum])
828 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000829 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000830 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000831 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000832 if end > 0:
833 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000834 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000835 while comment[:1] == '#' and indentsize(lines[end]) == indent:
836 comments[:0] = [comment]
837 end = end - 1
838 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000839 comment = lines[end].expandtabs().lstrip()
840 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000841 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000842 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000843 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000844 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000845
Tim Peters4efb6e92001-06-29 23:51:08 +0000846class EndOfBlock(Exception): pass
847
848class BlockFinder:
849 """Provide a tokeneater() method to detect the end of a code block."""
850 def __init__(self):
851 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000852 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000853 self.started = False
854 self.passline = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000855 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000856
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000857 def tokeneater(self, type, token, srowcol, erowcol, line):
Tim Peters4efb6e92001-06-29 23:51:08 +0000858 if not self.started:
Armin Rigodd5c0232005-09-25 11:45:45 +0000859 # look for the first "def", "class" or "lambda"
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000860 if token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000861 if token == "lambda":
862 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000863 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000864 self.passline = True # skip to the end of the line
Tim Peters4efb6e92001-06-29 23:51:08 +0000865 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000866 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000867 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000868 if self.islambda: # lambdas always end at the first NEWLINE
869 raise EndOfBlock
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000870 elif self.passline:
871 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000872 elif type == tokenize.INDENT:
873 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000874 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000875 elif type == tokenize.DEDENT:
876 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000877 # the end of matching indent/dedent pairs end a block
878 # (note that this only works for "def"/"class" blocks,
879 # not e.g. for "if: else:" or "try: finally:" blocks)
880 if self.indent <= 0:
881 raise EndOfBlock
882 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
883 # any other token on the same indentation level end the previous
884 # block as well, except the pseudo-tokens COMMENT and NL.
885 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000886
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000887def getblock(lines):
888 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000889 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000890 try:
Trent Nelson428de652008-03-18 22:41:35 +0000891 tokens = tokenize.generate_tokens(iter(lines).__next__)
892 for _token in tokens:
893 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000894 except (EndOfBlock, IndentationError):
895 pass
896 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000897
Antoine Pitroua8723a02015-04-15 00:41:29 +0200898def _line_number_helper(code_obj, lines, lnum):
899 """Return a list of source lines and starting line number for a code object.
900
901 The arguments must be a code object with lines and lnum from findsource.
902 """
903 _, end_line = list(dis.findlinestarts(code_obj))[-1]
904 return lines[lnum:end_line], lnum + 1
905
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000906def getsourcelines(object):
907 """Return a list of source lines and starting line number for an object.
908
909 The argument may be a module, class, method, function, traceback, frame,
910 or code object. The source code is returned as a list of the lines
911 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200912 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000913 raised if the source code cannot be retrieved."""
Yury Selivanov081bbf62014-09-26 17:34:54 -0400914 object = unwrap(object)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000915 lines, lnum = findsource(object)
916
Antoine Pitroua8723a02015-04-15 00:41:29 +0200917 if ismodule(object):
918 return lines, 0
919 elif iscode(object):
920 return _line_number_helper(object, lines, lnum)
921 elif isfunction(object):
922 return _line_number_helper(object.__code__, lines, lnum)
923 elif ismethod(object):
924 return _line_number_helper(object.__func__.__code__, lines, lnum)
925 else:
926 return getblock(lines[lnum:]), lnum + 1
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000927
928def getsource(object):
929 """Return the text of the source code for an object.
930
931 The argument may be a module, class, method, function, traceback, frame,
932 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200933 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000934 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000935 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000936
937# --------------------------------------------------- class tree extraction
938def walktree(classes, children, parent):
939 """Recursive helper function for getclasstree()."""
940 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +0000941 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000942 for c in classes:
943 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +0000944 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000945 results.append(walktree(children[c], children, c))
946 return results
947
Georg Brandl5ce83a02009-06-01 17:23:51 +0000948def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000949 """Arrange the given list of classes into a hierarchy of nested lists.
950
951 Where a nested list appears, it contains classes derived from the class
952 whose entry immediately precedes the list. Each entry is a 2-tuple
953 containing a class and a tuple of its base classes. If the 'unique'
954 argument is true, exactly one entry appears in the returned structure
955 for each class in the given list. Otherwise, classes using multiple
956 inheritance and their descendants will appear multiple times."""
957 children = {}
958 roots = []
959 for c in classes:
960 if c.__bases__:
961 for parent in c.__bases__:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000962 if not parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000963 children[parent] = []
Serhiy Storchaka362c1b52013-09-05 17:14:32 +0300964 if c not in children[parent]:
965 children[parent].append(c)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000966 if unique and parent in classes: break
967 elif c not in roots:
968 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +0000969 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000970 if parent not in classes:
971 roots.append(parent)
972 return walktree(roots, children, None)
973
974# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +0000975Arguments = namedtuple('Arguments', 'args, varargs, varkw')
976
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000977def getargs(co):
978 """Get information about the arguments accepted by a code object.
979
Guido van Rossum2e65f892007-02-28 22:03:49 +0000980 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000981 'args' is the list of argument names. Keyword-only arguments are
982 appended. 'varargs' and 'varkw' are the names of the * and **
983 arguments or None."""
Guido van Rossum2e65f892007-02-28 22:03:49 +0000984 args, varargs, kwonlyargs, varkw = _getfullargs(co)
Christian Heimes25bb7832008-01-11 16:17:00 +0000985 return Arguments(args + kwonlyargs, varargs, varkw)
Guido van Rossum2e65f892007-02-28 22:03:49 +0000986
987def _getfullargs(co):
988 """Get information about the arguments accepted by a code object.
989
990 Four things are returned: (args, varargs, kwonlyargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000991 'args' and 'kwonlyargs' are lists of argument names, and 'varargs'
992 and 'varkw' are the names of the * and ** arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +0000993
994 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000995 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000996
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000997 nargs = co.co_argcount
998 names = co.co_varnames
Guido van Rossum2e65f892007-02-28 22:03:49 +0000999 nkwargs = co.co_kwonlyargcount
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001000 args = list(names[:nargs])
Guido van Rossum2e65f892007-02-28 22:03:49 +00001001 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001002 step = 0
1003
Guido van Rossum2e65f892007-02-28 22:03:49 +00001004 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001005 varargs = None
1006 if co.co_flags & CO_VARARGS:
1007 varargs = co.co_varnames[nargs]
1008 nargs = nargs + 1
1009 varkw = None
1010 if co.co_flags & CO_VARKEYWORDS:
1011 varkw = co.co_varnames[nargs]
Guido van Rossum2e65f892007-02-28 22:03:49 +00001012 return args, varargs, kwonlyargs, varkw
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001013
Christian Heimes25bb7832008-01-11 16:17:00 +00001014
1015ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
1016
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001017def getargspec(func):
1018 """Get the names and default values of a function's arguments.
1019
Guido van Rossume82881c2014-07-15 12:29:11 -07001020 A tuple of four things is returned: (args, varargs, keywords, defaults).
1021 'args' is a list of the argument names, including keyword-only argument names.
1022 'varargs' and 'keywords' are the names of the * and ** arguments or None.
Jeremy Hylton64967882003-06-27 18:14:39 +00001023 'defaults' is an n-tuple of the default values of the last n arguments.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001024
Yury Selivanov0cf3ed62014-04-01 10:17:08 -04001025 Use the getfullargspec() API for Python 3 code, as annotations
Guido van Rossum2e65f892007-02-28 22:03:49 +00001026 and keyword arguments are supported. getargspec() will raise ValueError
1027 if the func has either annotations or keyword arguments.
1028 """
Yury Selivanov3cfec2e2015-05-22 11:38:38 -04001029 warnings.warn("inspect.getargspec() is deprecated, "
Yury Selivanovc8386f72015-05-22 16:09:44 -04001030 "use inspect.signature() instead", DeprecationWarning,
1031 stacklevel=2)
Guido van Rossum2e65f892007-02-28 22:03:49 +00001032 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
1033 getfullargspec(func)
1034 if kwonlyargs or ann:
Collin Winterce36ad82007-08-30 01:19:48 +00001035 raise ValueError("Function has keyword-only arguments or annotations"
1036 ", use getfullargspec() API which can support them")
Christian Heimes25bb7832008-01-11 16:17:00 +00001037 return ArgSpec(args, varargs, varkw, defaults)
1038
1039FullArgSpec = namedtuple('FullArgSpec',
Benjamin Peterson3d4ca742008-11-12 21:39:01 +00001040 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001041
1042def getfullargspec(func):
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001043 """Get the names and default values of a callable object's arguments.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001044
Brett Cannon504d8852007-09-07 02:12:14 +00001045 A tuple of seven things is returned:
1046 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults annotations).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001047 'args' is a list of the argument names.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001048 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1049 'defaults' is an n-tuple of the default values of the last n arguments.
1050 'kwonlyargs' is a list of keyword-only argument names.
1051 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
1052 'annotations' is a dictionary mapping argument names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001053
Guido van Rossum2e65f892007-02-28 22:03:49 +00001054 The first four items in the tuple correspond to getargspec().
Yury Selivanov3cfec2e2015-05-22 11:38:38 -04001055
1056 This function is deprecated, use inspect.signature() instead.
Jeremy Hylton64967882003-06-27 18:14:39 +00001057 """
1058
Yury Selivanov57d240e2014-02-19 16:27:23 -05001059 try:
1060 # Re: `skip_bound_arg=False`
1061 #
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001062 # There is a notable difference in behaviour between getfullargspec
1063 # and Signature: the former always returns 'self' parameter for bound
1064 # methods, whereas the Signature always shows the actual calling
1065 # signature of the passed object.
1066 #
1067 # To simulate this behaviour, we "unbind" bound methods, to trick
1068 # inspect.signature to always return their first parameter ("self",
1069 # usually)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001070
Yury Selivanov57d240e2014-02-19 16:27:23 -05001071 # Re: `follow_wrapper_chains=False`
1072 #
1073 # getfullargspec() historically ignored __wrapped__ attributes,
1074 # so we ensure that remains the case in 3.3+
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001075
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001076 sig = _signature_from_callable(func,
1077 follow_wrapper_chains=False,
1078 skip_bound_arg=False,
1079 sigcls=Signature)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001080 except Exception as ex:
1081 # Most of the times 'signature' will raise ValueError.
1082 # But, it can also raise AttributeError, and, maybe something
1083 # else. So to be fully backwards compatible, we catch all
1084 # possible exceptions here, and reraise a TypeError.
1085 raise TypeError('unsupported callable') from ex
1086
1087 args = []
1088 varargs = None
1089 varkw = None
1090 kwonlyargs = []
1091 defaults = ()
1092 annotations = {}
1093 defaults = ()
1094 kwdefaults = {}
1095
1096 if sig.return_annotation is not sig.empty:
1097 annotations['return'] = sig.return_annotation
1098
1099 for param in sig.parameters.values():
1100 kind = param.kind
1101 name = param.name
1102
1103 if kind is _POSITIONAL_ONLY:
1104 args.append(name)
1105 elif kind is _POSITIONAL_OR_KEYWORD:
1106 args.append(name)
1107 if param.default is not param.empty:
1108 defaults += (param.default,)
1109 elif kind is _VAR_POSITIONAL:
1110 varargs = name
1111 elif kind is _KEYWORD_ONLY:
1112 kwonlyargs.append(name)
1113 if param.default is not param.empty:
1114 kwdefaults[name] = param.default
1115 elif kind is _VAR_KEYWORD:
1116 varkw = name
1117
1118 if param.annotation is not param.empty:
1119 annotations[name] = param.annotation
1120
1121 if not kwdefaults:
1122 # compatibility with 'func.__kwdefaults__'
1123 kwdefaults = None
1124
1125 if not defaults:
1126 # compatibility with 'func.__defaults__'
1127 defaults = None
1128
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001129 return FullArgSpec(args, varargs, varkw, defaults,
1130 kwonlyargs, kwdefaults, annotations)
1131
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001132
Christian Heimes25bb7832008-01-11 16:17:00 +00001133ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
1134
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001135def getargvalues(frame):
1136 """Get information about arguments passed into a particular frame.
1137
1138 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001139 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001140 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1141 'locals' is the locals dictionary of the given frame."""
1142 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +00001143 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001144
Guido van Rossum2e65f892007-02-28 22:03:49 +00001145def formatannotation(annotation, base_module=None):
1146 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001147 if annotation.__module__ in ('builtins', base_module):
Serhiy Storchaka521e5862014-07-22 15:00:37 +03001148 return annotation.__qualname__
1149 return annotation.__module__+'.'+annotation.__qualname__
Guido van Rossum2e65f892007-02-28 22:03:49 +00001150 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001151
Guido van Rossum2e65f892007-02-28 22:03:49 +00001152def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001153 module = getattr(object, '__module__', None)
1154 def _formatannotation(annotation):
1155 return formatannotation(annotation, module)
1156 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001157
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001158def formatargspec(args, varargs=None, varkw=None, defaults=None,
Guido van Rossum2e65f892007-02-28 22:03:49 +00001159 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001160 formatarg=str,
1161 formatvarargs=lambda name: '*' + name,
1162 formatvarkw=lambda name: '**' + name,
1163 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001164 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001165 formatannotation=formatannotation):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001166 """Format an argument spec from the values returned by getargspec
Guido van Rossum2e65f892007-02-28 22:03:49 +00001167 or getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001168
Guido van Rossum2e65f892007-02-28 22:03:49 +00001169 The first seven arguments are (args, varargs, varkw, defaults,
1170 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1171 are the corresponding optional formatting functions that are called to
1172 turn names and values into strings. The last argument is an optional
1173 function to format the sequence of arguments."""
1174 def formatargandannotation(arg):
1175 result = formatarg(arg)
1176 if arg in annotations:
1177 result += ': ' + formatannotation(annotations[arg])
1178 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001179 specs = []
1180 if defaults:
1181 firstdefault = len(args) - len(defaults)
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001182 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001183 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001184 if defaults and i >= firstdefault:
1185 spec = spec + formatvalue(defaults[i - firstdefault])
1186 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001187 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001188 specs.append(formatvarargs(formatargandannotation(varargs)))
1189 else:
1190 if kwonlyargs:
1191 specs.append('*')
1192 if kwonlyargs:
1193 for kwonlyarg in kwonlyargs:
1194 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001195 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001196 spec += formatvalue(kwonlydefaults[kwonlyarg])
1197 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001198 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001199 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001200 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001201 if 'return' in annotations:
1202 result += formatreturns(formatannotation(annotations['return']))
1203 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001204
1205def formatargvalues(args, varargs, varkw, locals,
1206 formatarg=str,
1207 formatvarargs=lambda name: '*' + name,
1208 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001209 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001210 """Format an argument spec from the 4 values returned by getargvalues.
1211
1212 The first four arguments are (args, varargs, varkw, locals). The
1213 next four arguments are the corresponding optional formatting functions
1214 that are called to turn names and values into strings. The ninth
1215 argument is an optional function to format the sequence of arguments."""
1216 def convert(name, locals=locals,
1217 formatarg=formatarg, formatvalue=formatvalue):
1218 return formatarg(name) + formatvalue(locals[name])
1219 specs = []
1220 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001221 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001222 if varargs:
1223 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1224 if varkw:
1225 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001226 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001227
Benjamin Petersone109c702011-06-24 09:37:26 -05001228def _missing_arguments(f_name, argnames, pos, values):
1229 names = [repr(name) for name in argnames if name not in values]
1230 missing = len(names)
1231 if missing == 1:
1232 s = names[0]
1233 elif missing == 2:
1234 s = "{} and {}".format(*names)
1235 else:
Yury Selivanovdccfa132014-03-27 18:42:52 -04001236 tail = ", {} and {}".format(*names[-2:])
Benjamin Petersone109c702011-06-24 09:37:26 -05001237 del names[-2:]
1238 s = ", ".join(names) + tail
1239 raise TypeError("%s() missing %i required %s argument%s: %s" %
1240 (f_name, missing,
1241 "positional" if pos else "keyword-only",
1242 "" if missing == 1 else "s", s))
1243
1244def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001245 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001246 kwonly_given = len([arg for arg in kwonly if arg in values])
1247 if varargs:
1248 plural = atleast != 1
1249 sig = "at least %d" % (atleast,)
1250 elif defcount:
1251 plural = True
1252 sig = "from %d to %d" % (atleast, len(args))
1253 else:
1254 plural = len(args) != 1
1255 sig = str(len(args))
1256 kwonly_sig = ""
1257 if kwonly_given:
1258 msg = " positional argument%s (and %d keyword-only argument%s)"
1259 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1260 "s" if kwonly_given != 1 else ""))
1261 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1262 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1263 "was" if given == 1 and not kwonly_given else "were"))
1264
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001265def getcallargs(*func_and_positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001266 """Get the mapping of arguments to values.
1267
1268 A dict is returned, with keys the function argument names (including the
1269 names of the * and ** arguments, if any), and values the respective bound
1270 values from 'positional' and 'named'."""
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001271 func = func_and_positional[0]
1272 positional = func_and_positional[1:]
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001273 spec = getfullargspec(func)
1274 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
1275 f_name = func.__name__
1276 arg2value = {}
1277
Benjamin Petersonb204a422011-06-05 22:04:07 -05001278
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001279 if ismethod(func) and func.__self__ is not None:
1280 # implicit 'self' (or 'cls' for classmethods) argument
1281 positional = (func.__self__,) + positional
1282 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001283 num_args = len(args)
1284 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001285
Benjamin Petersonb204a422011-06-05 22:04:07 -05001286 n = min(num_pos, num_args)
1287 for i in range(n):
1288 arg2value[args[i]] = positional[i]
1289 if varargs:
1290 arg2value[varargs] = tuple(positional[n:])
1291 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001292 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001293 arg2value[varkw] = {}
1294 for kw, value in named.items():
1295 if kw not in possible_kwargs:
1296 if not varkw:
1297 raise TypeError("%s() got an unexpected keyword argument %r" %
1298 (f_name, kw))
1299 arg2value[varkw][kw] = value
1300 continue
1301 if kw in arg2value:
1302 raise TypeError("%s() got multiple values for argument %r" %
1303 (f_name, kw))
1304 arg2value[kw] = value
1305 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001306 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1307 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001308 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001309 req = args[:num_args - num_defaults]
1310 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001311 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001312 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001313 for i, arg in enumerate(args[num_args - num_defaults:]):
1314 if arg not in arg2value:
1315 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001316 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001317 for kwarg in kwonlyargs:
1318 if kwarg not in arg2value:
Yury Selivanov875df202014-03-27 18:23:03 -04001319 if kwonlydefaults and kwarg in kwonlydefaults:
Benjamin Petersone109c702011-06-24 09:37:26 -05001320 arg2value[kwarg] = kwonlydefaults[kwarg]
1321 else:
1322 missing += 1
1323 if missing:
1324 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001325 return arg2value
1326
Nick Coghlan2f92e542012-06-23 19:39:55 +10001327ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1328
1329def getclosurevars(func):
1330 """
1331 Get the mapping of free variables to their current values.
1332
Meador Inge8fda3592012-07-19 21:33:21 -05001333 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001334 and builtin references as seen by the body of the function. A final
1335 set of unbound names that could not be resolved is also provided.
1336 """
1337
1338 if ismethod(func):
1339 func = func.__func__
1340
1341 if not isfunction(func):
1342 raise TypeError("'{!r}' is not a Python function".format(func))
1343
1344 code = func.__code__
1345 # Nonlocal references are named in co_freevars and resolved
1346 # by looking them up in __closure__ by positional index
1347 if func.__closure__ is None:
1348 nonlocal_vars = {}
1349 else:
1350 nonlocal_vars = {
1351 var : cell.cell_contents
1352 for var, cell in zip(code.co_freevars, func.__closure__)
1353 }
1354
1355 # Global and builtin references are named in co_names and resolved
1356 # by looking them up in __globals__ or __builtins__
1357 global_ns = func.__globals__
1358 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1359 if ismodule(builtin_ns):
1360 builtin_ns = builtin_ns.__dict__
1361 global_vars = {}
1362 builtin_vars = {}
1363 unbound_names = set()
1364 for name in code.co_names:
1365 if name in ("None", "True", "False"):
1366 # Because these used to be builtins instead of keywords, they
1367 # may still show up as name references. We ignore them.
1368 continue
1369 try:
1370 global_vars[name] = global_ns[name]
1371 except KeyError:
1372 try:
1373 builtin_vars[name] = builtin_ns[name]
1374 except KeyError:
1375 unbound_names.add(name)
1376
1377 return ClosureVars(nonlocal_vars, global_vars,
1378 builtin_vars, unbound_names)
1379
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001380# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001381
1382Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1383
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001384def getframeinfo(frame, context=1):
1385 """Get information about a frame or traceback object.
1386
1387 A tuple of five things is returned: the filename, the line number of
1388 the current line, the function name, a list of lines of context from
1389 the source code, and the index of the current line within that list.
1390 The optional second argument specifies the number of lines of context
1391 to return, which are centered around the current line."""
1392 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001393 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001394 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001395 else:
1396 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001397 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001398 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001399
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001400 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001401 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001402 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001403 try:
1404 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001405 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001406 lines = index = None
1407 else:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001408 start = max(start, 1)
Raymond Hettingera0501712004-06-15 11:22:53 +00001409 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001410 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001411 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001412 else:
1413 lines = index = None
1414
Christian Heimes25bb7832008-01-11 16:17:00 +00001415 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001416
1417def getlineno(frame):
1418 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001419 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1420 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001421
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001422FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
1423
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001424def getouterframes(frame, context=1):
1425 """Get a list of records for a frame and all higher (calling) frames.
1426
1427 Each record contains a frame object, filename, line number, function
1428 name, a list of lines of context, and index within the context."""
1429 framelist = []
1430 while frame:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001431 frameinfo = (frame,) + getframeinfo(frame, context)
1432 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001433 frame = frame.f_back
1434 return framelist
1435
1436def getinnerframes(tb, context=1):
1437 """Get a list of records for a traceback's frame and all lower frames.
1438
1439 Each record contains a frame object, filename, line number, function
1440 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001441 framelist = []
1442 while tb:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001443 frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
1444 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001445 tb = tb.tb_next
1446 return framelist
1447
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001448def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001449 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001450 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001451
1452def stack(context=1):
1453 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001454 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001455
1456def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001457 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001458 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001459
1460
1461# ------------------------------------------------ static version of getattr
1462
1463_sentinel = object()
1464
Michael Foorde5162652010-11-20 16:40:44 +00001465def _static_getmro(klass):
1466 return type.__dict__['__mro__'].__get__(klass)
1467
Michael Foord95fc51d2010-11-20 15:07:30 +00001468def _check_instance(obj, attr):
1469 instance_dict = {}
1470 try:
1471 instance_dict = object.__getattribute__(obj, "__dict__")
1472 except AttributeError:
1473 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001474 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001475
1476
1477def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001478 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001479 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001480 try:
1481 return entry.__dict__[attr]
1482 except KeyError:
1483 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001484 return _sentinel
1485
Michael Foord35184ed2010-11-20 16:58:30 +00001486def _is_type(obj):
1487 try:
1488 _static_getmro(obj)
1489 except TypeError:
1490 return False
1491 return True
1492
Michael Foorddcebe0f2011-03-15 19:20:44 -04001493def _shadowed_dict(klass):
1494 dict_attr = type.__dict__["__dict__"]
1495 for entry in _static_getmro(klass):
1496 try:
1497 class_dict = dict_attr.__get__(entry)["__dict__"]
1498 except KeyError:
1499 pass
1500 else:
1501 if not (type(class_dict) is types.GetSetDescriptorType and
1502 class_dict.__name__ == "__dict__" and
1503 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001504 return class_dict
1505 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001506
1507def getattr_static(obj, attr, default=_sentinel):
1508 """Retrieve attributes without triggering dynamic lookup via the
1509 descriptor protocol, __getattr__ or __getattribute__.
1510
1511 Note: this function may not be able to retrieve all attributes
1512 that getattr can fetch (like dynamically created attributes)
1513 and may find attributes that getattr can't (like descriptors
1514 that raise AttributeError). It can also return descriptor objects
1515 instead of instance members in some cases. See the
1516 documentation for details.
1517 """
1518 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001519 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001520 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001521 dict_attr = _shadowed_dict(klass)
1522 if (dict_attr is _sentinel or
1523 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001524 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001525 else:
1526 klass = obj
1527
1528 klass_result = _check_class(klass, attr)
1529
1530 if instance_result is not _sentinel and klass_result is not _sentinel:
1531 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1532 _check_class(type(klass_result), '__set__') is not _sentinel):
1533 return klass_result
1534
1535 if instance_result is not _sentinel:
1536 return instance_result
1537 if klass_result is not _sentinel:
1538 return klass_result
1539
1540 if obj is klass:
1541 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001542 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001543 if _shadowed_dict(type(entry)) is _sentinel:
1544 try:
1545 return entry.__dict__[attr]
1546 except KeyError:
1547 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001548 if default is not _sentinel:
1549 return default
1550 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001551
1552
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001553# ------------------------------------------------ generator introspection
1554
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001555GEN_CREATED = 'GEN_CREATED'
1556GEN_RUNNING = 'GEN_RUNNING'
1557GEN_SUSPENDED = 'GEN_SUSPENDED'
1558GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001559
1560def getgeneratorstate(generator):
1561 """Get current state of a generator-iterator.
1562
1563 Possible states are:
1564 GEN_CREATED: Waiting to start execution.
1565 GEN_RUNNING: Currently being executed by the interpreter.
1566 GEN_SUSPENDED: Currently suspended at a yield expression.
1567 GEN_CLOSED: Execution has completed.
1568 """
1569 if generator.gi_running:
1570 return GEN_RUNNING
1571 if generator.gi_frame is None:
1572 return GEN_CLOSED
1573 if generator.gi_frame.f_lasti == -1:
1574 return GEN_CREATED
1575 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001576
1577
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001578def getgeneratorlocals(generator):
1579 """
1580 Get the mapping of generator local variables to their current values.
1581
1582 A dict is returned, with the keys the local variable names and values the
1583 bound values."""
1584
1585 if not isgenerator(generator):
1586 raise TypeError("'{!r}' is not a Python generator".format(generator))
1587
1588 frame = getattr(generator, "gi_frame", None)
1589 if frame is not None:
1590 return generator.gi_frame.f_locals
1591 else:
1592 return {}
1593
Yury Selivanov5376ba92015-06-22 12:19:30 -04001594
1595# ------------------------------------------------ coroutine introspection
1596
1597CORO_CREATED = 'CORO_CREATED'
1598CORO_RUNNING = 'CORO_RUNNING'
1599CORO_SUSPENDED = 'CORO_SUSPENDED'
1600CORO_CLOSED = 'CORO_CLOSED'
1601
1602def getcoroutinestate(coroutine):
1603 """Get current state of a coroutine object.
1604
1605 Possible states are:
1606 CORO_CREATED: Waiting to start execution.
1607 CORO_RUNNING: Currently being executed by the interpreter.
1608 CORO_SUSPENDED: Currently suspended at an await expression.
1609 CORO_CLOSED: Execution has completed.
1610 """
1611 if coroutine.cr_running:
1612 return CORO_RUNNING
1613 if coroutine.cr_frame is None:
1614 return CORO_CLOSED
1615 if coroutine.cr_frame.f_lasti == -1:
1616 return CORO_CREATED
1617 return CORO_SUSPENDED
1618
1619
1620def getcoroutinelocals(coroutine):
1621 """
1622 Get the mapping of coroutine local variables to their current values.
1623
1624 A dict is returned, with the keys the local variable names and values the
1625 bound values."""
1626 frame = getattr(coroutine, "cr_frame", None)
1627 if frame is not None:
1628 return frame.f_locals
1629 else:
1630 return {}
1631
1632
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001633###############################################################################
1634### Function Signature Object (PEP 362)
1635###############################################################################
1636
1637
1638_WrapperDescriptor = type(type.__call__)
1639_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001640_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001641
1642_NonUserDefinedCallables = (_WrapperDescriptor,
1643 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001644 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001645 types.BuiltinFunctionType)
1646
1647
Yury Selivanov421f0c72014-01-29 12:05:40 -05001648def _signature_get_user_defined_method(cls, method_name):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001649 """Private helper. Checks if ``cls`` has an attribute
1650 named ``method_name`` and returns it only if it is a
1651 pure python function.
1652 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001653 try:
1654 meth = getattr(cls, method_name)
1655 except AttributeError:
1656 return
1657 else:
1658 if not isinstance(meth, _NonUserDefinedCallables):
1659 # Once '__signature__' will be added to 'C'-level
1660 # callables, this check won't be necessary
1661 return meth
1662
1663
Yury Selivanov62560fb2014-01-28 12:26:24 -05001664def _signature_get_partial(wrapped_sig, partial, extra_args=()):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001665 """Private helper to calculate how 'wrapped_sig' signature will
1666 look like after applying a 'functools.partial' object (or alike)
1667 on it.
1668 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001669
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001670 old_params = wrapped_sig.parameters
1671 new_params = OrderedDict(old_params.items())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001672
1673 partial_args = partial.args or ()
1674 partial_keywords = partial.keywords or {}
1675
1676 if extra_args:
1677 partial_args = extra_args + partial_args
1678
1679 try:
1680 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1681 except TypeError as ex:
1682 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1683 raise ValueError(msg) from ex
1684
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001685
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001686 transform_to_kwonly = False
1687 for param_name, param in old_params.items():
1688 try:
1689 arg_value = ba.arguments[param_name]
1690 except KeyError:
1691 pass
1692 else:
1693 if param.kind is _POSITIONAL_ONLY:
1694 # If positional-only parameter is bound by partial,
1695 # it effectively disappears from the signature
1696 new_params.pop(param_name)
1697 continue
1698
1699 if param.kind is _POSITIONAL_OR_KEYWORD:
1700 if param_name in partial_keywords:
1701 # This means that this parameter, and all parameters
1702 # after it should be keyword-only (and var-positional
1703 # should be removed). Here's why. Consider the following
1704 # function:
1705 # foo(a, b, *args, c):
1706 # pass
1707 #
1708 # "partial(foo, a='spam')" will have the following
1709 # signature: "(*, a='spam', b, c)". Because attempting
1710 # to call that partial with "(10, 20)" arguments will
1711 # raise a TypeError, saying that "a" argument received
1712 # multiple values.
1713 transform_to_kwonly = True
1714 # Set the new default value
1715 new_params[param_name] = param.replace(default=arg_value)
1716 else:
1717 # was passed as a positional argument
1718 new_params.pop(param.name)
1719 continue
1720
1721 if param.kind is _KEYWORD_ONLY:
1722 # Set the new default value
1723 new_params[param_name] = param.replace(default=arg_value)
1724
1725 if transform_to_kwonly:
1726 assert param.kind is not _POSITIONAL_ONLY
1727
1728 if param.kind is _POSITIONAL_OR_KEYWORD:
1729 new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1730 new_params[param_name] = new_param
1731 new_params.move_to_end(param_name)
1732 elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
1733 new_params.move_to_end(param_name)
1734 elif param.kind is _VAR_POSITIONAL:
1735 new_params.pop(param.name)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001736
1737 return wrapped_sig.replace(parameters=new_params.values())
1738
1739
Yury Selivanov62560fb2014-01-28 12:26:24 -05001740def _signature_bound_method(sig):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001741 """Private helper to transform signatures for unbound
1742 functions to bound methods.
1743 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001744
1745 params = tuple(sig.parameters.values())
1746
1747 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1748 raise ValueError('invalid method signature')
1749
1750 kind = params[0].kind
1751 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1752 # Drop first parameter:
1753 # '(p1, p2[, ...])' -> '(p2[, ...])'
1754 params = params[1:]
1755 else:
1756 if kind is not _VAR_POSITIONAL:
1757 # Unless we add a new parameter type we never
1758 # get here
1759 raise ValueError('invalid argument type')
1760 # It's a var-positional parameter.
1761 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1762
1763 return sig.replace(parameters=params)
1764
1765
Yury Selivanovb77511d2014-01-29 10:46:14 -05001766def _signature_is_builtin(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001767 """Private helper to test if `obj` is a callable that might
1768 support Argument Clinic's __text_signature__ protocol.
1769 """
Yury Selivanov1d241832014-02-02 12:51:20 -05001770 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001771 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001772 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001773 # Can't test 'isinstance(type)' here, as it would
1774 # also be True for regular python classes
1775 obj in (type, object))
1776
1777
Yury Selivanov63da7c72014-01-31 14:48:37 -05001778def _signature_is_functionlike(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001779 """Private helper to test if `obj` is a duck type of FunctionType.
1780 A good example of such objects are functions compiled with
1781 Cython, which have all attributes that a pure Python function
1782 would have, but have their code statically compiled.
1783 """
Yury Selivanov63da7c72014-01-31 14:48:37 -05001784
1785 if not callable(obj) or isclass(obj):
1786 # All function-like objects are obviously callables,
1787 # and not classes.
1788 return False
1789
1790 name = getattr(obj, '__name__', None)
1791 code = getattr(obj, '__code__', None)
1792 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
1793 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
1794 annotations = getattr(obj, '__annotations__', None)
1795
1796 return (isinstance(code, types.CodeType) and
1797 isinstance(name, str) and
1798 (defaults is None or isinstance(defaults, tuple)) and
1799 (kwdefaults is None or isinstance(kwdefaults, dict)) and
1800 isinstance(annotations, dict))
1801
1802
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001803def _signature_get_bound_param(spec):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001804 """ Private helper to get first parameter name from a
1805 __text_signature__ of a builtin method, which should
1806 be in the following format: '($param1, ...)'.
1807 Assumptions are that the first argument won't have
1808 a default value or an annotation.
1809 """
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001810
1811 assert spec.startswith('($')
1812
1813 pos = spec.find(',')
1814 if pos == -1:
1815 pos = spec.find(')')
1816
1817 cpos = spec.find(':')
1818 assert cpos == -1 or cpos > pos
1819
1820 cpos = spec.find('=')
1821 assert cpos == -1 or cpos > pos
1822
1823 return spec[2:pos]
1824
1825
Larry Hastings2623c8c2014-02-08 22:15:29 -08001826def _signature_strip_non_python_syntax(signature):
1827 """
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001828 Private helper function. Takes a signature in Argument Clinic's
1829 extended signature format.
1830
Larry Hastings2623c8c2014-02-08 22:15:29 -08001831 Returns a tuple of three things:
1832 * that signature re-rendered in standard Python syntax,
1833 * the index of the "self" parameter (generally 0), or None if
1834 the function does not have a "self" parameter, and
1835 * the index of the last "positional only" parameter,
1836 or None if the signature has no positional-only parameters.
1837 """
1838
1839 if not signature:
1840 return signature, None, None
1841
1842 self_parameter = None
1843 last_positional_only = None
1844
1845 lines = [l.encode('ascii') for l in signature.split('\n')]
1846 generator = iter(lines).__next__
1847 token_stream = tokenize.tokenize(generator)
1848
1849 delayed_comma = False
1850 skip_next_comma = False
1851 text = []
1852 add = text.append
1853
1854 current_parameter = 0
1855 OP = token.OP
1856 ERRORTOKEN = token.ERRORTOKEN
1857
1858 # token stream always starts with ENCODING token, skip it
1859 t = next(token_stream)
1860 assert t.type == tokenize.ENCODING
1861
1862 for t in token_stream:
1863 type, string = t.type, t.string
1864
1865 if type == OP:
1866 if string == ',':
1867 if skip_next_comma:
1868 skip_next_comma = False
1869 else:
1870 assert not delayed_comma
1871 delayed_comma = True
1872 current_parameter += 1
1873 continue
1874
1875 if string == '/':
1876 assert not skip_next_comma
1877 assert last_positional_only is None
1878 skip_next_comma = True
1879 last_positional_only = current_parameter - 1
1880 continue
1881
1882 if (type == ERRORTOKEN) and (string == '$'):
1883 assert self_parameter is None
1884 self_parameter = current_parameter
1885 continue
1886
1887 if delayed_comma:
1888 delayed_comma = False
1889 if not ((type == OP) and (string == ')')):
1890 add(', ')
1891 add(string)
1892 if (string == ','):
1893 add(' ')
1894 clean_signature = ''.join(text)
1895 return clean_signature, self_parameter, last_positional_only
1896
1897
Yury Selivanov57d240e2014-02-19 16:27:23 -05001898def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001899 """Private helper to parse content of '__text_signature__'
1900 and return a Signature based on it.
1901 """
1902
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001903 Parameter = cls._parameter_cls
1904
Larry Hastings2623c8c2014-02-08 22:15:29 -08001905 clean_signature, self_parameter, last_positional_only = \
1906 _signature_strip_non_python_syntax(s)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001907
Larry Hastings2623c8c2014-02-08 22:15:29 -08001908 program = "def foo" + clean_signature + ": pass"
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001909
1910 try:
Larry Hastings2623c8c2014-02-08 22:15:29 -08001911 module = ast.parse(program)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001912 except SyntaxError:
1913 module = None
1914
1915 if not isinstance(module, ast.Module):
1916 raise ValueError("{!r} builtin has invalid signature".format(obj))
1917
1918 f = module.body[0]
1919
1920 parameters = []
1921 empty = Parameter.empty
1922 invalid = object()
1923
1924 module = None
1925 module_dict = {}
1926 module_name = getattr(obj, '__module__', None)
1927 if module_name:
1928 module = sys.modules.get(module_name, None)
1929 if module:
1930 module_dict = module.__dict__
1931 sys_module_dict = sys.modules
1932
1933 def parse_name(node):
1934 assert isinstance(node, ast.arg)
1935 if node.annotation != None:
1936 raise ValueError("Annotations are not currently supported")
1937 return node.arg
1938
1939 def wrap_value(s):
1940 try:
1941 value = eval(s, module_dict)
1942 except NameError:
1943 try:
1944 value = eval(s, sys_module_dict)
1945 except NameError:
1946 raise RuntimeError()
1947
1948 if isinstance(value, str):
1949 return ast.Str(value)
1950 if isinstance(value, (int, float)):
1951 return ast.Num(value)
1952 if isinstance(value, bytes):
1953 return ast.Bytes(value)
1954 if value in (True, False, None):
1955 return ast.NameConstant(value)
1956 raise RuntimeError()
1957
1958 class RewriteSymbolics(ast.NodeTransformer):
1959 def visit_Attribute(self, node):
1960 a = []
1961 n = node
1962 while isinstance(n, ast.Attribute):
1963 a.append(n.attr)
1964 n = n.value
1965 if not isinstance(n, ast.Name):
1966 raise RuntimeError()
1967 a.append(n.id)
1968 value = ".".join(reversed(a))
1969 return wrap_value(value)
1970
1971 def visit_Name(self, node):
1972 if not isinstance(node.ctx, ast.Load):
1973 raise ValueError()
1974 return wrap_value(node.id)
1975
1976 def p(name_node, default_node, default=empty):
1977 name = parse_name(name_node)
1978 if name is invalid:
1979 return None
1980 if default_node and default_node is not _empty:
1981 try:
1982 default_node = RewriteSymbolics().visit(default_node)
1983 o = ast.literal_eval(default_node)
1984 except ValueError:
1985 o = invalid
1986 if o is invalid:
1987 return None
1988 default = o if o is not invalid else default
1989 parameters.append(Parameter(name, kind, default=default, annotation=empty))
1990
1991 # non-keyword-only parameters
1992 args = reversed(f.args.args)
1993 defaults = reversed(f.args.defaults)
1994 iter = itertools.zip_longest(args, defaults, fillvalue=None)
Larry Hastings2623c8c2014-02-08 22:15:29 -08001995 if last_positional_only is not None:
1996 kind = Parameter.POSITIONAL_ONLY
1997 else:
1998 kind = Parameter.POSITIONAL_OR_KEYWORD
1999 for i, (name, default) in enumerate(reversed(list(iter))):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002000 p(name, default)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002001 if i == last_positional_only:
2002 kind = Parameter.POSITIONAL_OR_KEYWORD
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002003
2004 # *args
2005 if f.args.vararg:
2006 kind = Parameter.VAR_POSITIONAL
2007 p(f.args.vararg, empty)
2008
2009 # keyword-only arguments
2010 kind = Parameter.KEYWORD_ONLY
2011 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
2012 p(name, default)
2013
2014 # **kwargs
2015 if f.args.kwarg:
2016 kind = Parameter.VAR_KEYWORD
2017 p(f.args.kwarg, empty)
2018
Larry Hastings2623c8c2014-02-08 22:15:29 -08002019 if self_parameter is not None:
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002020 # Possibly strip the bound argument:
2021 # - We *always* strip first bound argument if
2022 # it is a module.
2023 # - We don't strip first bound argument if
2024 # skip_bound_arg is False.
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002025 assert parameters
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002026 _self = getattr(obj, '__self__', None)
2027 self_isbound = _self is not None
2028 self_ismodule = ismodule(_self)
2029 if self_isbound and (self_ismodule or skip_bound_arg):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002030 parameters.pop(0)
2031 else:
2032 # for builtins, self parameter is always positional-only!
2033 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
2034 parameters[0] = p
2035
2036 return cls(parameters, return_annotation=cls.empty)
2037
2038
Yury Selivanov57d240e2014-02-19 16:27:23 -05002039def _signature_from_builtin(cls, func, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002040 """Private helper function to get signature for
2041 builtin callables.
2042 """
2043
Yury Selivanov57d240e2014-02-19 16:27:23 -05002044 if not _signature_is_builtin(func):
2045 raise TypeError("{!r} is not a Python builtin "
2046 "function".format(func))
2047
2048 s = getattr(func, "__text_signature__", None)
2049 if not s:
2050 raise ValueError("no signature found for builtin {!r}".format(func))
2051
2052 return _signature_fromstr(cls, func, s, skip_bound_arg)
2053
2054
Yury Selivanovcf45f022015-05-20 14:38:50 -04002055def _signature_from_function(cls, func):
2056 """Private helper: constructs Signature for the given python function."""
2057
2058 is_duck_function = False
2059 if not isfunction(func):
2060 if _signature_is_functionlike(func):
2061 is_duck_function = True
2062 else:
2063 # If it's not a pure Python function, and not a duck type
2064 # of pure function:
2065 raise TypeError('{!r} is not a Python function'.format(func))
2066
2067 Parameter = cls._parameter_cls
2068
2069 # Parameter information.
2070 func_code = func.__code__
2071 pos_count = func_code.co_argcount
2072 arg_names = func_code.co_varnames
2073 positional = tuple(arg_names[:pos_count])
2074 keyword_only_count = func_code.co_kwonlyargcount
2075 keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)]
2076 annotations = func.__annotations__
2077 defaults = func.__defaults__
2078 kwdefaults = func.__kwdefaults__
2079
2080 if defaults:
2081 pos_default_count = len(defaults)
2082 else:
2083 pos_default_count = 0
2084
2085 parameters = []
2086
2087 # Non-keyword-only parameters w/o defaults.
2088 non_default_count = pos_count - pos_default_count
2089 for name in positional[:non_default_count]:
2090 annotation = annotations.get(name, _empty)
2091 parameters.append(Parameter(name, annotation=annotation,
2092 kind=_POSITIONAL_OR_KEYWORD))
2093
2094 # ... w/ defaults.
2095 for offset, name in enumerate(positional[non_default_count:]):
2096 annotation = annotations.get(name, _empty)
2097 parameters.append(Parameter(name, annotation=annotation,
2098 kind=_POSITIONAL_OR_KEYWORD,
2099 default=defaults[offset]))
2100
2101 # *args
2102 if func_code.co_flags & CO_VARARGS:
2103 name = arg_names[pos_count + keyword_only_count]
2104 annotation = annotations.get(name, _empty)
2105 parameters.append(Parameter(name, annotation=annotation,
2106 kind=_VAR_POSITIONAL))
2107
2108 # Keyword-only parameters.
2109 for name in keyword_only:
2110 default = _empty
2111 if kwdefaults is not None:
2112 default = kwdefaults.get(name, _empty)
2113
2114 annotation = annotations.get(name, _empty)
2115 parameters.append(Parameter(name, annotation=annotation,
2116 kind=_KEYWORD_ONLY,
2117 default=default))
2118 # **kwargs
2119 if func_code.co_flags & CO_VARKEYWORDS:
2120 index = pos_count + keyword_only_count
2121 if func_code.co_flags & CO_VARARGS:
2122 index += 1
2123
2124 name = arg_names[index]
2125 annotation = annotations.get(name, _empty)
2126 parameters.append(Parameter(name, annotation=annotation,
2127 kind=_VAR_KEYWORD))
2128
2129 # Is 'func' is a pure Python function - don't validate the
2130 # parameters list (for correct order and defaults), it should be OK.
2131 return cls(parameters,
2132 return_annotation=annotations.get('return', _empty),
2133 __validate_parameters__=is_duck_function)
2134
2135
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002136def _signature_from_callable(obj, *,
2137 follow_wrapper_chains=True,
2138 skip_bound_arg=True,
2139 sigcls):
2140
2141 """Private helper function to get signature for arbitrary
2142 callable objects.
2143 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002144
2145 if not callable(obj):
2146 raise TypeError('{!r} is not a callable object'.format(obj))
2147
2148 if isinstance(obj, types.MethodType):
2149 # In this case we skip the first parameter of the underlying
2150 # function (usually `self` or `cls`).
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002151 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002152 obj.__func__,
2153 follow_wrapper_chains=follow_wrapper_chains,
2154 skip_bound_arg=skip_bound_arg,
2155 sigcls=sigcls)
2156
Yury Selivanov57d240e2014-02-19 16:27:23 -05002157 if skip_bound_arg:
2158 return _signature_bound_method(sig)
2159 else:
2160 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002161
Nick Coghlane8c45d62013-07-28 20:00:01 +10002162 # Was this function wrapped by a decorator?
Yury Selivanov57d240e2014-02-19 16:27:23 -05002163 if follow_wrapper_chains:
2164 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
Yury Selivanov46c759d2015-05-27 21:56:53 -04002165 if isinstance(obj, types.MethodType):
2166 # If the unwrapped object is a *method*, we might want to
2167 # skip its first parameter (self).
2168 # See test_signature_wrapped_bound_method for details.
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002169 return _signature_from_callable(
Yury Selivanov46c759d2015-05-27 21:56:53 -04002170 obj,
2171 follow_wrapper_chains=follow_wrapper_chains,
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002172 skip_bound_arg=skip_bound_arg,
2173 sigcls=sigcls)
Nick Coghlane8c45d62013-07-28 20:00:01 +10002174
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002175 try:
2176 sig = obj.__signature__
2177 except AttributeError:
2178 pass
2179 else:
2180 if sig is not None:
Yury Selivanov42407ab2014-06-23 10:23:50 -07002181 if not isinstance(sig, Signature):
2182 raise TypeError(
2183 'unexpected object {!r} in __signature__ '
2184 'attribute'.format(sig))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002185 return sig
2186
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002187 try:
2188 partialmethod = obj._partialmethod
2189 except AttributeError:
2190 pass
2191 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05002192 if isinstance(partialmethod, functools.partialmethod):
2193 # Unbound partialmethod (see functools.partialmethod)
2194 # This means, that we need to calculate the signature
2195 # as if it's a regular partial object, but taking into
2196 # account that the first positional argument
2197 # (usually `self`, or `cls`) will not be passed
2198 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002199
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002200 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002201 partialmethod.func,
2202 follow_wrapper_chains=follow_wrapper_chains,
2203 skip_bound_arg=skip_bound_arg,
2204 sigcls=sigcls)
2205
Yury Selivanov0486f812014-01-29 12:18:59 -05002206 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002207
Yury Selivanov0486f812014-01-29 12:18:59 -05002208 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
2209 new_params = (first_wrapped_param,) + tuple(sig.parameters.values())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002210
Yury Selivanov0486f812014-01-29 12:18:59 -05002211 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002212
Yury Selivanov63da7c72014-01-31 14:48:37 -05002213 if isfunction(obj) or _signature_is_functionlike(obj):
2214 # If it's a pure Python function, or an object that is duck type
2215 # of a Python function (Cython functions, for instance), then:
Yury Selivanovcf45f022015-05-20 14:38:50 -04002216 return _signature_from_function(sigcls, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002217
Yury Selivanova773de02014-02-21 18:30:53 -05002218 if _signature_is_builtin(obj):
Yury Selivanovda396452014-03-27 12:09:24 -04002219 return _signature_from_builtin(sigcls, obj,
Yury Selivanova773de02014-02-21 18:30:53 -05002220 skip_bound_arg=skip_bound_arg)
2221
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002222 if isinstance(obj, functools.partial):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002223 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002224 obj.func,
2225 follow_wrapper_chains=follow_wrapper_chains,
2226 skip_bound_arg=skip_bound_arg,
2227 sigcls=sigcls)
Yury Selivanov62560fb2014-01-28 12:26:24 -05002228 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002229
2230 sig = None
2231 if isinstance(obj, type):
2232 # obj is a class or a metaclass
2233
2234 # First, let's see if it has an overloaded __call__ defined
2235 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05002236 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002237 if call is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002238 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002239 call,
2240 follow_wrapper_chains=follow_wrapper_chains,
2241 skip_bound_arg=skip_bound_arg,
2242 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002243 else:
2244 # Now we check if the 'obj' class has a '__new__' method
Yury Selivanov421f0c72014-01-29 12:05:40 -05002245 new = _signature_get_user_defined_method(obj, '__new__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002246 if new is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002247 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002248 new,
2249 follow_wrapper_chains=follow_wrapper_chains,
2250 skip_bound_arg=skip_bound_arg,
2251 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002252 else:
2253 # Finally, we should have at least __init__ implemented
Yury Selivanov421f0c72014-01-29 12:05:40 -05002254 init = _signature_get_user_defined_method(obj, '__init__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002255 if init is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002256 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002257 init,
2258 follow_wrapper_chains=follow_wrapper_chains,
2259 skip_bound_arg=skip_bound_arg,
2260 sigcls=sigcls)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002261
2262 if sig is None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002263 # At this point we know, that `obj` is a class, with no user-
2264 # defined '__init__', '__new__', or class-level '__call__'
2265
Larry Hastings2623c8c2014-02-08 22:15:29 -08002266 for base in obj.__mro__[:-1]:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002267 # Since '__text_signature__' is implemented as a
2268 # descriptor that extracts text signature from the
2269 # class docstring, if 'obj' is derived from a builtin
2270 # class, its own '__text_signature__' may be 'None'.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002271 # Therefore, we go through the MRO (except the last
2272 # class in there, which is 'object') to find the first
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002273 # class with non-empty text signature.
2274 try:
2275 text_sig = base.__text_signature__
2276 except AttributeError:
2277 pass
2278 else:
2279 if text_sig:
2280 # If 'obj' class has a __text_signature__ attribute:
2281 # return a signature based on it
Yury Selivanovda396452014-03-27 12:09:24 -04002282 return _signature_fromstr(sigcls, obj, text_sig)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002283
2284 # No '__text_signature__' was found for the 'obj' class.
2285 # Last option is to check if its '__init__' is
2286 # object.__init__ or type.__init__.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002287 if type not in obj.__mro__:
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002288 # We have a class (not metaclass), but no user-defined
2289 # __init__ or __new__ for it
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002290 if (obj.__init__ is object.__init__ and
2291 obj.__new__ is object.__new__):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002292 # Return a signature of 'object' builtin.
2293 return signature(object)
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002294 else:
2295 raise ValueError(
2296 'no signature found for builtin type {!r}'.format(obj))
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002297
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002298 elif not isinstance(obj, _NonUserDefinedCallables):
2299 # An object with __call__
2300 # We also check that the 'obj' is not an instance of
2301 # _WrapperDescriptor or _MethodWrapper to avoid
2302 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05002303 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002304 if call is not None:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002305 try:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002306 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002307 call,
2308 follow_wrapper_chains=follow_wrapper_chains,
2309 skip_bound_arg=skip_bound_arg,
2310 sigcls=sigcls)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002311 except ValueError as ex:
2312 msg = 'no signature found for {!r}'.format(obj)
2313 raise ValueError(msg) from ex
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002314
2315 if sig is not None:
2316 # For classes and objects we skip the first parameter of their
2317 # __call__, __new__, or __init__ methods
Yury Selivanov57d240e2014-02-19 16:27:23 -05002318 if skip_bound_arg:
2319 return _signature_bound_method(sig)
2320 else:
2321 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002322
2323 if isinstance(obj, types.BuiltinFunctionType):
2324 # Raise a nicer error message for builtins
2325 msg = 'no signature found for builtin function {!r}'.format(obj)
2326 raise ValueError(msg)
2327
2328 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2329
2330
2331class _void:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002332 """A private marker - used in Parameter & Signature."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002333
2334
2335class _empty:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002336 """Marker object for Signature.empty and Parameter.empty."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002337
2338
Yury Selivanov21e83a52014-03-27 11:23:13 -04002339class _ParameterKind(enum.IntEnum):
2340 POSITIONAL_ONLY = 0
2341 POSITIONAL_OR_KEYWORD = 1
2342 VAR_POSITIONAL = 2
2343 KEYWORD_ONLY = 3
2344 VAR_KEYWORD = 4
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002345
2346 def __str__(self):
Yury Selivanov21e83a52014-03-27 11:23:13 -04002347 return self._name_
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002348
2349
Yury Selivanov21e83a52014-03-27 11:23:13 -04002350_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
2351_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
2352_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
2353_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
2354_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002355
2356
2357class Parameter:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002358 """Represents a parameter in a function signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002359
2360 Has the following public attributes:
2361
2362 * name : str
2363 The name of the parameter as a string.
2364 * default : object
2365 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002366 parameter has no default value, this attribute is set to
2367 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002368 * annotation
2369 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002370 parameter has no annotation, this attribute is set to
2371 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002372 * kind : str
2373 Describes how argument values are bound to the parameter.
2374 Possible values: `Parameter.POSITIONAL_ONLY`,
2375 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
2376 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002377 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002378
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002379 __slots__ = ('_name', '_kind', '_default', '_annotation')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002380
2381 POSITIONAL_ONLY = _POSITIONAL_ONLY
2382 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
2383 VAR_POSITIONAL = _VAR_POSITIONAL
2384 KEYWORD_ONLY = _KEYWORD_ONLY
2385 VAR_KEYWORD = _VAR_KEYWORD
2386
2387 empty = _empty
2388
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002389 def __init__(self, name, kind, *, default=_empty, annotation=_empty):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002390
2391 if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
2392 _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
2393 raise ValueError("invalid value for 'Parameter.kind' attribute")
2394 self._kind = kind
2395
2396 if default is not _empty:
2397 if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2398 msg = '{} parameters cannot have default values'.format(kind)
2399 raise ValueError(msg)
2400 self._default = default
2401 self._annotation = annotation
2402
Yury Selivanov2393dca2014-01-27 15:07:58 -05002403 if name is _empty:
2404 raise ValueError('name is a required attribute for Parameter')
2405
2406 if not isinstance(name, str):
2407 raise TypeError("name must be a str, not a {!r}".format(name))
2408
2409 if not name.isidentifier():
2410 raise ValueError('{!r} is not a valid parameter name'.format(name))
2411
2412 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002413
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002414 def __reduce__(self):
2415 return (type(self),
2416 (self._name, self._kind),
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002417 {'_default': self._default,
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002418 '_annotation': self._annotation})
2419
2420 def __setstate__(self, state):
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002421 self._default = state['_default']
2422 self._annotation = state['_annotation']
2423
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002424 @property
2425 def name(self):
2426 return self._name
2427
2428 @property
2429 def default(self):
2430 return self._default
2431
2432 @property
2433 def annotation(self):
2434 return self._annotation
2435
2436 @property
2437 def kind(self):
2438 return self._kind
2439
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002440 def replace(self, *, name=_void, kind=_void,
2441 annotation=_void, default=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002442 """Creates a customized copy of the Parameter."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002443
2444 if name is _void:
2445 name = self._name
2446
2447 if kind is _void:
2448 kind = self._kind
2449
2450 if annotation is _void:
2451 annotation = self._annotation
2452
2453 if default is _void:
2454 default = self._default
2455
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002456 return type(self)(name, kind, default=default, annotation=annotation)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002457
2458 def __str__(self):
2459 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002460 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002461
2462 # Add annotation and default value
2463 if self._annotation is not _empty:
2464 formatted = '{}:{}'.format(formatted,
2465 formatannotation(self._annotation))
2466
2467 if self._default is not _empty:
2468 formatted = '{}={}'.format(formatted, repr(self._default))
2469
2470 if kind == _VAR_POSITIONAL:
2471 formatted = '*' + formatted
2472 elif kind == _VAR_KEYWORD:
2473 formatted = '**' + formatted
2474
2475 return formatted
2476
2477 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002478 return '<{} "{}">'.format(self.__class__.__name__, self)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002479
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002480 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002481 return hash((self.name, self.kind, self.annotation, self.default))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002482
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002483 def __eq__(self, other):
Yury Selivanov692b3402015-05-14 18:20:01 -04002484 return (self is other or
2485 (issubclass(other.__class__, Parameter) and
2486 self._name == other._name and
2487 self._kind == other._kind and
2488 self._default == other._default and
2489 self._annotation == other._annotation))
2490
2491 def __ne__(self, other):
2492 return not self.__eq__(other)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002493
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002494
2495class BoundArguments:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002496 """Result of `Signature.bind` call. Holds the mapping of arguments
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002497 to the function's parameters.
2498
2499 Has the following public attributes:
2500
2501 * arguments : OrderedDict
2502 An ordered mutable mapping of parameters' names to arguments' values.
2503 Does not contain arguments' default values.
2504 * signature : Signature
2505 The Signature object that created this instance.
2506 * args : tuple
2507 Tuple of positional arguments values.
2508 * kwargs : dict
2509 Dict of keyword arguments values.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002510 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002511
Yury Selivanov6abe0322015-05-13 17:18:41 -04002512 __slots__ = ('arguments', '_signature', '__weakref__')
2513
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002514 def __init__(self, signature, arguments):
2515 self.arguments = arguments
2516 self._signature = signature
2517
2518 @property
2519 def signature(self):
2520 return self._signature
2521
2522 @property
2523 def args(self):
2524 args = []
2525 for param_name, param in self._signature.parameters.items():
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002526 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002527 break
2528
2529 try:
2530 arg = self.arguments[param_name]
2531 except KeyError:
2532 # We're done here. Other arguments
2533 # will be mapped in 'BoundArguments.kwargs'
2534 break
2535 else:
2536 if param.kind == _VAR_POSITIONAL:
2537 # *args
2538 args.extend(arg)
2539 else:
2540 # plain argument
2541 args.append(arg)
2542
2543 return tuple(args)
2544
2545 @property
2546 def kwargs(self):
2547 kwargs = {}
2548 kwargs_started = False
2549 for param_name, param in self._signature.parameters.items():
2550 if not kwargs_started:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002551 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002552 kwargs_started = True
2553 else:
2554 if param_name not in self.arguments:
2555 kwargs_started = True
2556 continue
2557
2558 if not kwargs_started:
2559 continue
2560
2561 try:
2562 arg = self.arguments[param_name]
2563 except KeyError:
2564 pass
2565 else:
2566 if param.kind == _VAR_KEYWORD:
2567 # **kwargs
2568 kwargs.update(arg)
2569 else:
2570 # plain keyword argument
2571 kwargs[param_name] = arg
2572
2573 return kwargs
2574
Yury Selivanovb907a512015-05-16 13:45:09 -04002575 def apply_defaults(self):
2576 """Set default values for missing arguments.
2577
2578 For variable-positional arguments (*args) the default is an
2579 empty tuple.
2580
2581 For variable-keyword arguments (**kwargs) the default is an
2582 empty dict.
2583 """
2584 arguments = self.arguments
2585 if not arguments:
2586 return
2587 new_arguments = []
2588 for name, param in self._signature.parameters.items():
2589 try:
2590 new_arguments.append((name, arguments[name]))
2591 except KeyError:
2592 if param.default is not _empty:
2593 val = param.default
2594 elif param.kind is _VAR_POSITIONAL:
2595 val = ()
2596 elif param.kind is _VAR_KEYWORD:
2597 val = {}
2598 else:
2599 # This BoundArguments was likely produced by
2600 # Signature.bind_partial().
2601 continue
2602 new_arguments.append((name, val))
2603 self.arguments = OrderedDict(new_arguments)
2604
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002605 def __eq__(self, other):
Yury Selivanov692b3402015-05-14 18:20:01 -04002606 return (self is other or
2607 (issubclass(other.__class__, BoundArguments) and
2608 self.signature == other.signature and
2609 self.arguments == other.arguments))
2610
2611 def __ne__(self, other):
2612 return not self.__eq__(other)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002613
Yury Selivanov6abe0322015-05-13 17:18:41 -04002614 def __setstate__(self, state):
2615 self._signature = state['_signature']
2616 self.arguments = state['arguments']
2617
2618 def __getstate__(self):
2619 return {'_signature': self._signature, 'arguments': self.arguments}
2620
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002621 def __repr__(self):
2622 args = []
2623 for arg, value in self.arguments.items():
2624 args.append('{}={!r}'.format(arg, value))
Yury Selivanovf229bc52015-05-15 12:53:56 -04002625 return '<{} ({})>'.format(self.__class__.__name__, ', '.join(args))
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002626
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002627
2628class Signature:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002629 """A Signature object represents the overall signature of a function.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002630 It stores a Parameter object for each parameter accepted by the
2631 function, as well as information specific to the function itself.
2632
2633 A Signature object has the following public attributes and methods:
2634
2635 * parameters : OrderedDict
2636 An ordered mapping of parameters' names to the corresponding
2637 Parameter objects (keyword-only arguments are in the same order
2638 as listed in `code.co_varnames`).
2639 * return_annotation : object
2640 The annotation for the return type of the function if specified.
2641 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002642 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002643 * bind(*args, **kwargs) -> BoundArguments
2644 Creates a mapping from positional and keyword arguments to
2645 parameters.
2646 * bind_partial(*args, **kwargs) -> BoundArguments
2647 Creates a partial mapping from positional and keyword arguments
2648 to parameters (simulating 'functools.partial' behavior.)
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002649 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002650
2651 __slots__ = ('_return_annotation', '_parameters')
2652
2653 _parameter_cls = Parameter
2654 _bound_arguments_cls = BoundArguments
2655
2656 empty = _empty
2657
2658 def __init__(self, parameters=None, *, return_annotation=_empty,
2659 __validate_parameters__=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002660 """Constructs Signature from the given list of Parameter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002661 objects and 'return_annotation'. All arguments are optional.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002662 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002663
2664 if parameters is None:
2665 params = OrderedDict()
2666 else:
2667 if __validate_parameters__:
2668 params = OrderedDict()
2669 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002670 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002671
2672 for idx, param in enumerate(parameters):
2673 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002674 name = param.name
2675
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002676 if kind < top_kind:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002677 msg = 'wrong parameter order: {!r} before {!r}'
Yury Selivanov2393dca2014-01-27 15:07:58 -05002678 msg = msg.format(top_kind, kind)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002679 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002680 elif kind > top_kind:
2681 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002682 top_kind = kind
2683
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002684 if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
Yury Selivanov07a9e452014-01-29 10:58:16 -05002685 if param.default is _empty:
2686 if kind_defaults:
2687 # No default for this parameter, but the
2688 # previous parameter of the same kind had
2689 # a default
2690 msg = 'non-default argument follows default ' \
2691 'argument'
2692 raise ValueError(msg)
2693 else:
2694 # There is a default for this parameter.
2695 kind_defaults = True
2696
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002697 if name in params:
2698 msg = 'duplicate parameter name: {!r}'.format(name)
2699 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002700
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002701 params[name] = param
2702 else:
2703 params = OrderedDict(((param.name, param)
2704 for param in parameters))
2705
2706 self._parameters = types.MappingProxyType(params)
2707 self._return_annotation = return_annotation
2708
2709 @classmethod
2710 def from_function(cls, func):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002711 """Constructs Signature for the given python function."""
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002712
2713 warnings.warn("inspect.Signature.from_function() is deprecated, "
Berker Peksagb5601582015-05-21 23:40:54 +03002714 "use Signature.from_callable()",
2715 DeprecationWarning, stacklevel=2)
Yury Selivanovcf45f022015-05-20 14:38:50 -04002716 return _signature_from_function(cls, func)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002717
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002718 @classmethod
2719 def from_builtin(cls, func):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002720 """Constructs Signature for the given builtin function."""
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002721
2722 warnings.warn("inspect.Signature.from_builtin() is deprecated, "
Berker Peksagb5601582015-05-21 23:40:54 +03002723 "use Signature.from_callable()",
2724 DeprecationWarning, stacklevel=2)
Yury Selivanov57d240e2014-02-19 16:27:23 -05002725 return _signature_from_builtin(cls, func)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002726
Yury Selivanovda396452014-03-27 12:09:24 -04002727 @classmethod
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002728 def from_callable(cls, obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002729 """Constructs Signature for the given callable object."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002730 return _signature_from_callable(obj, sigcls=cls,
2731 follow_wrapper_chains=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04002732
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002733 @property
2734 def parameters(self):
2735 return self._parameters
2736
2737 @property
2738 def return_annotation(self):
2739 return self._return_annotation
2740
2741 def replace(self, *, parameters=_void, return_annotation=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002742 """Creates a customized copy of the Signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002743 Pass 'parameters' and/or 'return_annotation' arguments
2744 to override them in the new copy.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002745 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002746
2747 if parameters is _void:
2748 parameters = self.parameters.values()
2749
2750 if return_annotation is _void:
2751 return_annotation = self._return_annotation
2752
2753 return type(self)(parameters,
2754 return_annotation=return_annotation)
2755
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002756 def _hash_basis(self):
2757 params = tuple(param for param in self.parameters.values()
2758 if param.kind != _KEYWORD_ONLY)
2759
2760 kwo_params = {param.name: param for param in self.parameters.values()
2761 if param.kind == _KEYWORD_ONLY}
2762
2763 return params, kwo_params, self.return_annotation
2764
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002765 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002766 params, kwo_params, return_annotation = self._hash_basis()
2767 kwo_params = frozenset(kwo_params.values())
2768 return hash((params, kwo_params, return_annotation))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002769
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002770 def __eq__(self, other):
Yury Selivanov692b3402015-05-14 18:20:01 -04002771 return (self is other or
2772 (isinstance(other, Signature) and
2773 self._hash_basis() == other._hash_basis()))
2774
2775 def __ne__(self, other):
2776 return not self.__eq__(other)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002777
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002778 def _bind(self, args, kwargs, *, partial=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002779 """Private method. Don't use directly."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002780
2781 arguments = OrderedDict()
2782
2783 parameters = iter(self.parameters.values())
2784 parameters_ex = ()
2785 arg_vals = iter(args)
2786
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002787 while True:
2788 # Let's iterate through the positional arguments and corresponding
2789 # parameters
2790 try:
2791 arg_val = next(arg_vals)
2792 except StopIteration:
2793 # No more positional arguments
2794 try:
2795 param = next(parameters)
2796 except StopIteration:
2797 # No more parameters. That's it. Just need to check that
2798 # we have no `kwargs` after this while loop
2799 break
2800 else:
2801 if param.kind == _VAR_POSITIONAL:
2802 # That's OK, just empty *args. Let's start parsing
2803 # kwargs
2804 break
2805 elif param.name in kwargs:
2806 if param.kind == _POSITIONAL_ONLY:
2807 msg = '{arg!r} parameter is positional only, ' \
2808 'but was passed as a keyword'
2809 msg = msg.format(arg=param.name)
2810 raise TypeError(msg) from None
2811 parameters_ex = (param,)
2812 break
2813 elif (param.kind == _VAR_KEYWORD or
2814 param.default is not _empty):
2815 # That's fine too - we have a default value for this
2816 # parameter. So, lets start parsing `kwargs`, starting
2817 # with the current parameter
2818 parameters_ex = (param,)
2819 break
2820 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002821 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
2822 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002823 if partial:
2824 parameters_ex = (param,)
2825 break
2826 else:
Yury Selivanov86872752015-05-19 00:27:49 -04002827 msg = 'missing a required argument: {arg!r}'
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002828 msg = msg.format(arg=param.name)
2829 raise TypeError(msg) from None
2830 else:
2831 # We have a positional argument to process
2832 try:
2833 param = next(parameters)
2834 except StopIteration:
2835 raise TypeError('too many positional arguments') from None
2836 else:
2837 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
2838 # Looks like we have no parameter for this positional
2839 # argument
Yury Selivanov86872752015-05-19 00:27:49 -04002840 raise TypeError(
2841 'too many positional arguments') from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002842
2843 if param.kind == _VAR_POSITIONAL:
2844 # We have an '*args'-like argument, let's fill it with
2845 # all positional arguments we have left and move on to
2846 # the next phase
2847 values = [arg_val]
2848 values.extend(arg_vals)
2849 arguments[param.name] = tuple(values)
2850 break
2851
2852 if param.name in kwargs:
Yury Selivanov86872752015-05-19 00:27:49 -04002853 raise TypeError(
2854 'multiple values for argument {arg!r}'.format(
2855 arg=param.name)) from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002856
2857 arguments[param.name] = arg_val
2858
2859 # Now, we iterate through the remaining parameters to process
2860 # keyword arguments
2861 kwargs_param = None
2862 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002863 if param.kind == _VAR_KEYWORD:
2864 # Memorize that we have a '**kwargs'-like parameter
2865 kwargs_param = param
2866 continue
2867
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002868 if param.kind == _VAR_POSITIONAL:
2869 # Named arguments don't refer to '*args'-like parameters.
2870 # We only arrive here if the positional arguments ended
2871 # before reaching the last parameter before *args.
2872 continue
2873
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002874 param_name = param.name
2875 try:
2876 arg_val = kwargs.pop(param_name)
2877 except KeyError:
2878 # We have no value for this parameter. It's fine though,
2879 # if it has a default value, or it is an '*args'-like
2880 # parameter, left alone by the processing of positional
2881 # arguments.
2882 if (not partial and param.kind != _VAR_POSITIONAL and
2883 param.default is _empty):
Yury Selivanov86872752015-05-19 00:27:49 -04002884 raise TypeError('missing a required argument: {arg!r}'. \
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002885 format(arg=param_name)) from None
2886
2887 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05002888 if param.kind == _POSITIONAL_ONLY:
2889 # This should never happen in case of a properly built
2890 # Signature object (but let's have this check here
2891 # to ensure correct behaviour just in case)
2892 raise TypeError('{arg!r} parameter is positional only, '
2893 'but was passed as a keyword'. \
2894 format(arg=param.name))
2895
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002896 arguments[param_name] = arg_val
2897
2898 if kwargs:
2899 if kwargs_param is not None:
2900 # Process our '**kwargs'-like parameter
2901 arguments[kwargs_param.name] = kwargs
2902 else:
Yury Selivanov86872752015-05-19 00:27:49 -04002903 raise TypeError(
2904 'got an unexpected keyword argument {arg!r}'.format(
2905 arg=next(iter(kwargs))))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002906
2907 return self._bound_arguments_cls(self, arguments)
2908
Yury Selivanovc45873e2014-01-29 12:10:27 -05002909 def bind(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002910 """Get a BoundArguments object, that maps the passed `args`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002911 and `kwargs` to the function's signature. Raises `TypeError`
2912 if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002913 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05002914 return args[0]._bind(args[1:], kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002915
Yury Selivanovc45873e2014-01-29 12:10:27 -05002916 def bind_partial(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002917 """Get a BoundArguments object, that partially maps the
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002918 passed `args` and `kwargs` to the function's signature.
2919 Raises `TypeError` if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002920 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05002921 return args[0]._bind(args[1:], kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002922
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002923 def __reduce__(self):
2924 return (type(self),
2925 (tuple(self._parameters.values()),),
2926 {'_return_annotation': self._return_annotation})
2927
2928 def __setstate__(self, state):
2929 self._return_annotation = state['_return_annotation']
2930
Yury Selivanov374375d2014-03-27 12:41:53 -04002931 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002932 return '<{} {}>'.format(self.__class__.__name__, self)
Yury Selivanov374375d2014-03-27 12:41:53 -04002933
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002934 def __str__(self):
2935 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05002936 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002937 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05002938 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002939 formatted = str(param)
2940
2941 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002942
2943 if kind == _POSITIONAL_ONLY:
2944 render_pos_only_separator = True
2945 elif render_pos_only_separator:
2946 # It's not a positional-only parameter, and the flag
2947 # is set to 'True' (there were pos-only params before.)
2948 result.append('/')
2949 render_pos_only_separator = False
2950
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002951 if kind == _VAR_POSITIONAL:
2952 # OK, we have an '*args'-like parameter, so we won't need
2953 # a '*' to separate keyword-only arguments
2954 render_kw_only_separator = False
2955 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
2956 # We have a keyword-only parameter to render and we haven't
2957 # rendered an '*args'-like parameter before, so add a '*'
2958 # separator to the parameters list ("foo(arg1, *, arg2)" case)
2959 result.append('*')
2960 # This condition should be only triggered once, so
2961 # reset the flag
2962 render_kw_only_separator = False
2963
2964 result.append(formatted)
2965
Yury Selivanov2393dca2014-01-27 15:07:58 -05002966 if render_pos_only_separator:
2967 # There were only positional-only parameters, hence the
2968 # flag was not reset to 'False'
2969 result.append('/')
2970
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002971 rendered = '({})'.format(', '.join(result))
2972
2973 if self.return_annotation is not _empty:
2974 anno = formatannotation(self.return_annotation)
2975 rendered += ' -> {}'.format(anno)
2976
2977 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10002978
Yury Selivanovda396452014-03-27 12:09:24 -04002979
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002980def signature(obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002981 """Get a signature object for the passed callable."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002982 return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04002983
2984
Nick Coghlanf94a16b2013-09-22 22:46:49 +10002985def _main():
2986 """ Logic for inspecting an object given at command line """
2987 import argparse
2988 import importlib
2989
2990 parser = argparse.ArgumentParser()
2991 parser.add_argument(
2992 'object',
2993 help="The object to be analysed. "
2994 "It supports the 'module:qualname' syntax")
2995 parser.add_argument(
2996 '-d', '--details', action='store_true',
2997 help='Display info about the module rather than its source code')
2998
2999 args = parser.parse_args()
3000
3001 target = args.object
3002 mod_name, has_attrs, attrs = target.partition(":")
3003 try:
3004 obj = module = importlib.import_module(mod_name)
3005 except Exception as exc:
3006 msg = "Failed to import {} ({}: {})".format(mod_name,
3007 type(exc).__name__,
3008 exc)
3009 print(msg, file=sys.stderr)
3010 exit(2)
3011
3012 if has_attrs:
3013 parts = attrs.split(".")
3014 obj = module
3015 for part in parts:
3016 obj = getattr(obj, part)
3017
3018 if module.__name__ in sys.builtin_module_names:
3019 print("Can't get info for builtin modules.", file=sys.stderr)
3020 exit(1)
3021
3022 if args.details:
3023 print('Target: {}'.format(target))
3024 print('Origin: {}'.format(getsourcefile(module)))
3025 print('Cached: {}'.format(module.__cached__))
3026 if obj is module:
3027 print('Loader: {}'.format(repr(module.__loader__)))
3028 if hasattr(module, '__path__'):
3029 print('Submodule search path: {}'.format(module.__path__))
3030 else:
3031 try:
3032 __, lineno = findsource(obj)
3033 except Exception:
3034 pass
3035 else:
3036 print('Line: {}'.format(lineno))
3037
3038 print('\n')
3039 else:
3040 print(getsource(obj))
3041
3042
3043if __name__ == "__main__":
3044 _main()