blob: 2380095e766f2cc771e2ae9581a4bec9233fe2e1 [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
Berker Peksagfa3922c2015-07-31 04:11:29 +030019 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
Martin Panter0f0eac42016-09-07 11:04:41 +0000174 Generator function objects provide the same attributes as functions.
175 See help(isfunction) for a list of attributes."""
Georg Brandlb1441c72009-01-03 22:33:39 +0000176 return bool((isfunction(object) or ismethod(object)) and
Yury Selivanov5376ba92015-06-22 12:19:30 -0400177 object.__code__.co_flags & CO_GENERATOR)
Yury Selivanov75445082015-05-11 22:57:16 -0400178
179def iscoroutinefunction(object):
180 """Return true if the object is a coroutine function.
181
182 Coroutine functions are defined with "async def" syntax,
183 or generators decorated with "types.coroutine".
184 """
185 return bool((isfunction(object) or ismethod(object)) and
Yury Selivanov5376ba92015-06-22 12:19:30 -0400186 object.__code__.co_flags & CO_COROUTINE)
Yury Selivanov75445082015-05-11 22:57:16 -0400187
Yury Selivanoveb636452016-09-08 22:01:51 -0700188def isasyncgenfunction(object):
189 return bool((isfunction(object) or ismethod(object)) and
190 object.__code__.co_flags & CO_ASYNC_GENERATOR)
191
192def isasyncgen(object):
193 return isinstance(object, types.AsyncGeneratorType)
194
Christian Heimes7131fd92008-02-19 14:21:46 +0000195def isgenerator(object):
196 """Return true if the object is a generator.
197
198 Generator objects provide these attributes:
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300199 __iter__ defined to support iteration over container
Christian Heimes7131fd92008-02-19 14:21:46 +0000200 close raises a new GeneratorExit exception inside the
201 generator to terminate the iteration
202 gi_code code object
203 gi_frame frame object or possibly None once the generator has
204 been exhausted
205 gi_running set to 1 when generator is executing, 0 otherwise
206 next return the next item from the container
207 send resumes the generator and "sends" a value that becomes
208 the result of the current yield-expression
209 throw used to raise an exception inside the generator"""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400210 return isinstance(object, types.GeneratorType)
Yury Selivanov75445082015-05-11 22:57:16 -0400211
212def iscoroutine(object):
213 """Return true if the object is a coroutine."""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400214 return isinstance(object, types.CoroutineType)
Christian Heimes7131fd92008-02-19 14:21:46 +0000215
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400216def isawaitable(object):
217 """Return true is object can be passed to an ``await`` expression."""
218 return (isinstance(object, types.CoroutineType) or
219 isinstance(object, types.GeneratorType) and
220 object.gi_code.co_flags & CO_ITERABLE_COROUTINE or
221 isinstance(object, collections.abc.Awaitable))
222
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000223def istraceback(object):
224 """Return true if the object is a traceback.
225
226 Traceback objects provide these attributes:
227 tb_frame frame object at this level
228 tb_lasti index of last attempted instruction in bytecode
229 tb_lineno current line number in Python source code
230 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000231 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000232
233def isframe(object):
234 """Return true if the object is a frame object.
235
236 Frame objects provide these attributes:
237 f_back next outer frame object (this frame's caller)
238 f_builtins built-in namespace seen by this frame
239 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000240 f_globals global namespace seen by this frame
241 f_lasti index of last attempted instruction in bytecode
242 f_lineno current line number in Python source code
243 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000244 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000245 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000246
247def iscode(object):
248 """Return true if the object is a code object.
249
250 Code objects provide these attributes:
251 co_argcount number of arguments (not including * or ** args)
252 co_code string of raw compiled bytecode
253 co_consts tuple of constants used in the bytecode
254 co_filename name of file in which this code object was created
255 co_firstlineno number of first line in Python source code
256 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
257 co_lnotab encoded mapping of line numbers to bytecode indices
258 co_name name with which this code object was defined
259 co_names tuple of names of local variables
260 co_nlocals number of local variables
261 co_stacksize virtual machine stack space required
262 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000263 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000264
265def isbuiltin(object):
266 """Return true if the object is a built-in function or method.
267
268 Built-in functions and methods provide these attributes:
269 __doc__ documentation string
270 __name__ original name of this function or method
271 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000272 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000273
274def isroutine(object):
275 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000276 return (isbuiltin(object)
277 or isfunction(object)
278 or ismethod(object)
279 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000280
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000281def isabstract(object):
282 """Return true if the object is an abstract base class (ABC)."""
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000283 return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT)
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000284
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000285def getmembers(object, predicate=None):
286 """Return all members of an object as (name, value) pairs sorted by name.
287 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100288 if isclass(object):
289 mro = (object,) + getmro(object)
290 else:
291 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000292 results = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700293 processed = set()
294 names = dir(object)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700295 # :dd any DynamicClassAttributes to the list of names if object is a class;
Ethan Furmane03ea372013-09-25 07:14:41 -0700296 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700297 # attribute with the same name as a DynamicClassAttribute exists
Ethan Furmane03ea372013-09-25 07:14:41 -0700298 try:
299 for base in object.__bases__:
300 for k, v in base.__dict__.items():
301 if isinstance(v, types.DynamicClassAttribute):
302 names.append(k)
303 except AttributeError:
304 pass
305 for key in names:
Ethan Furman63c141c2013-10-18 00:27:39 -0700306 # First try to get the value via getattr. Some descriptors don't
307 # like calling their __get__ (see bug #1785), so fall back to
308 # looking in the __dict__.
309 try:
310 value = getattr(object, key)
311 # handle the duplicate key
312 if key in processed:
313 raise AttributeError
314 except AttributeError:
315 for base in mro:
316 if key in base.__dict__:
317 value = base.__dict__[key]
318 break
319 else:
320 # could be a (currently) missing slot member, or a buggy
321 # __dir__; discard and move on
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100322 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000323 if not predicate or predicate(value):
324 results.append((key, value))
Ethan Furmane03ea372013-09-25 07:14:41 -0700325 processed.add(key)
326 results.sort(key=lambda pair: pair[0])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000327 return results
328
Christian Heimes25bb7832008-01-11 16:17:00 +0000329Attribute = namedtuple('Attribute', 'name kind defining_class object')
330
Tim Peters13b49d32001-09-23 02:00:29 +0000331def classify_class_attrs(cls):
332 """Return list of attribute-descriptor tuples.
333
334 For each name in dir(cls), the return list contains a 4-tuple
335 with these elements:
336
337 0. The name (a string).
338
339 1. The kind of attribute this is, one of these strings:
340 'class method' created via classmethod()
341 'static method' created via staticmethod()
342 'property' created via property()
Ethan Furmane03ea372013-09-25 07:14:41 -0700343 'method' any other flavor of method or descriptor
Tim Peters13b49d32001-09-23 02:00:29 +0000344 'data' not a method
345
346 2. The class which defined this attribute (a class).
347
Ethan Furmane03ea372013-09-25 07:14:41 -0700348 3. The object as obtained by calling getattr; if this fails, or if the
349 resulting object does not live anywhere in the class' mro (including
350 metaclasses) then the object is looked up in the defining class's
351 dict (found by walking the mro).
Ethan Furman668dede2013-09-14 18:53:26 -0700352
353 If one of the items in dir(cls) is stored in the metaclass it will now
354 be discovered and not have None be listed as the class in which it was
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700355 defined. Any items whose home class cannot be discovered are skipped.
Tim Peters13b49d32001-09-23 02:00:29 +0000356 """
357
358 mro = getmro(cls)
Ethan Furman668dede2013-09-14 18:53:26 -0700359 metamro = getmro(type(cls)) # for attributes stored in the metaclass
Ethan Furmane03ea372013-09-25 07:14:41 -0700360 metamro = tuple([cls for cls in metamro if cls not in (type, object)])
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700361 class_bases = (cls,) + mro
362 all_bases = class_bases + metamro
Tim Peters13b49d32001-09-23 02:00:29 +0000363 names = dir(cls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700364 # :dd any DynamicClassAttributes to the list of names;
Ethan Furmane03ea372013-09-25 07:14:41 -0700365 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700366 # attribute with the same name as a DynamicClassAttribute exists.
Ethan Furman63c141c2013-10-18 00:27:39 -0700367 for base in mro:
Ethan Furmane03ea372013-09-25 07:14:41 -0700368 for k, v in base.__dict__.items():
369 if isinstance(v, types.DynamicClassAttribute):
370 names.append(k)
Tim Peters13b49d32001-09-23 02:00:29 +0000371 result = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700372 processed = set()
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700373
Tim Peters13b49d32001-09-23 02:00:29 +0000374 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100375 # Get the object associated with the name, and where it was defined.
Ethan Furmane03ea372013-09-25 07:14:41 -0700376 # Normal objects will be looked up with both getattr and directly in
377 # its class' dict (in case getattr fails [bug #1785], and also to look
378 # for a docstring).
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700379 # For DynamicClassAttributes on the second pass we only look in the
Ethan Furmane03ea372013-09-25 07:14:41 -0700380 # class's dict.
381 #
Tim Peters13b49d32001-09-23 02:00:29 +0000382 # Getting an obj from the __dict__ sometimes reveals more than
383 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100384 homecls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700385 get_obj = None
386 dict_obj = None
Ethan Furmane03ea372013-09-25 07:14:41 -0700387 if name not in processed:
388 try:
Ethan Furmana8b07072013-10-18 01:22:08 -0700389 if name == '__dict__':
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700390 raise Exception("__dict__ is special, don't want the proxy")
Ethan Furmane03ea372013-09-25 07:14:41 -0700391 get_obj = getattr(cls, name)
392 except Exception as exc:
393 pass
394 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700395 homecls = getattr(get_obj, "__objclass__", homecls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700396 if homecls not in class_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700397 # if the resulting object does not live somewhere in the
Ethan Furman63c141c2013-10-18 00:27:39 -0700398 # mro, drop it and search the mro manually
Ethan Furmane03ea372013-09-25 07:14:41 -0700399 homecls = None
Ethan Furman63c141c2013-10-18 00:27:39 -0700400 last_cls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700401 # first look in the classes
402 for srch_cls in class_bases:
Ethan Furman63c141c2013-10-18 00:27:39 -0700403 srch_obj = getattr(srch_cls, name, None)
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400404 if srch_obj is get_obj:
Ethan Furman63c141c2013-10-18 00:27:39 -0700405 last_cls = srch_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700406 # then check the metaclasses
407 for srch_cls in metamro:
408 try:
409 srch_obj = srch_cls.__getattr__(cls, name)
410 except AttributeError:
411 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400412 if srch_obj is get_obj:
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700413 last_cls = srch_cls
Ethan Furman63c141c2013-10-18 00:27:39 -0700414 if last_cls is not None:
415 homecls = last_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700416 for base in all_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700417 if name in base.__dict__:
418 dict_obj = base.__dict__[name]
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700419 if homecls not in metamro:
420 homecls = base
Ethan Furmane03ea372013-09-25 07:14:41 -0700421 break
Ethan Furman63c141c2013-10-18 00:27:39 -0700422 if homecls is None:
423 # unable to locate the attribute anywhere, most likely due to
424 # buggy custom __dir__; discard and move on
425 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400426 obj = get_obj if get_obj is not None else dict_obj
Ethan Furmane03ea372013-09-25 07:14:41 -0700427 # Classify the object or its descriptor.
Ethan Furman63c141c2013-10-18 00:27:39 -0700428 if isinstance(dict_obj, staticmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000429 kind = "static method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700430 obj = dict_obj
Ethan Furman63c141c2013-10-18 00:27:39 -0700431 elif isinstance(dict_obj, classmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000432 kind = "class method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700433 obj = dict_obj
434 elif isinstance(dict_obj, property):
Tim Peters13b49d32001-09-23 02:00:29 +0000435 kind = "property"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700436 obj = dict_obj
Yury Selivanov0860a0b2014-01-31 14:28:44 -0500437 elif isroutine(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000438 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100439 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700440 kind = "data"
Christian Heimes25bb7832008-01-11 16:17:00 +0000441 result.append(Attribute(name, kind, homecls, obj))
Ethan Furmane03ea372013-09-25 07:14:41 -0700442 processed.add(name)
Tim Peters13b49d32001-09-23 02:00:29 +0000443 return result
444
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000445# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000446
447def getmro(cls):
448 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000449 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000450
Nick Coghlane8c45d62013-07-28 20:00:01 +1000451# -------------------------------------------------------- function helpers
452
453def unwrap(func, *, stop=None):
454 """Get the object wrapped by *func*.
455
456 Follows the chain of :attr:`__wrapped__` attributes returning the last
457 object in the chain.
458
459 *stop* is an optional callback accepting an object in the wrapper chain
460 as its sole argument that allows the unwrapping to be terminated early if
461 the callback returns a true value. If the callback never returns a true
462 value, the last object in the chain is returned as usual. For example,
463 :func:`signature` uses this to stop unwrapping if any object in the
464 chain has a ``__signature__`` attribute defined.
465
466 :exc:`ValueError` is raised if a cycle is encountered.
467
468 """
469 if stop is None:
470 def _is_wrapper(f):
471 return hasattr(f, '__wrapped__')
472 else:
473 def _is_wrapper(f):
474 return hasattr(f, '__wrapped__') and not stop(f)
475 f = func # remember the original func for error reporting
476 memo = {id(f)} # Memoise by id to tolerate non-hashable objects
477 while _is_wrapper(func):
478 func = func.__wrapped__
479 id_func = id(func)
480 if id_func in memo:
481 raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
482 memo.add(id_func)
483 return func
484
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000485# -------------------------------------------------- source code extraction
486def indentsize(line):
487 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000488 expline = line.expandtabs()
489 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000490
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300491def _findclass(func):
492 cls = sys.modules.get(func.__module__)
493 if cls is None:
494 return None
495 for name in func.__qualname__.split('.')[:-1]:
496 cls = getattr(cls, name)
497 if not isclass(cls):
498 return None
499 return cls
500
501def _finddoc(obj):
502 if isclass(obj):
503 for base in obj.__mro__:
504 if base is not object:
505 try:
506 doc = base.__doc__
507 except AttributeError:
508 continue
509 if doc is not None:
510 return doc
511 return None
512
513 if ismethod(obj):
514 name = obj.__func__.__name__
515 self = obj.__self__
516 if (isclass(self) and
517 getattr(getattr(self, name, None), '__func__') is obj.__func__):
518 # classmethod
519 cls = self
520 else:
521 cls = self.__class__
522 elif isfunction(obj):
523 name = obj.__name__
524 cls = _findclass(obj)
525 if cls is None or getattr(cls, name) is not obj:
526 return None
527 elif isbuiltin(obj):
528 name = obj.__name__
529 self = obj.__self__
530 if (isclass(self) and
531 self.__qualname__ + '.' + name == obj.__qualname__):
532 # classmethod
533 cls = self
534 else:
535 cls = self.__class__
Serhiy Storchakaac4bdcc2015-10-29 08:15:50 +0200536 # Should be tested before isdatadescriptor().
537 elif isinstance(obj, property):
538 func = obj.fget
539 name = func.__name__
540 cls = _findclass(func)
541 if cls is None or getattr(cls, name) is not obj:
542 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300543 elif ismethoddescriptor(obj) or isdatadescriptor(obj):
544 name = obj.__name__
545 cls = obj.__objclass__
546 if getattr(cls, name) is not obj:
547 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300548 else:
549 return None
550
551 for base in cls.__mro__:
552 try:
553 doc = getattr(base, name).__doc__
554 except AttributeError:
555 continue
556 if doc is not None:
557 return doc
558 return None
559
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000560def getdoc(object):
561 """Get the documentation string for an object.
562
563 All tabs are expanded to spaces. To clean up docstrings that are
564 indented to line up with blocks of code, any whitespace than can be
565 uniformly removed from the second line onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000566 try:
567 doc = object.__doc__
568 except AttributeError:
569 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300570 if doc is None:
571 try:
572 doc = _finddoc(object)
573 except (AttributeError, TypeError):
574 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000575 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000576 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000577 return cleandoc(doc)
578
579def cleandoc(doc):
580 """Clean up indentation from docstrings.
581
582 Any whitespace that can be uniformly removed from the second line
583 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000584 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000585 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000586 except UnicodeError:
587 return None
588 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000589 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000590 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000591 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000592 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000593 if content:
594 indent = len(line) - content
595 margin = min(margin, indent)
596 # Remove indentation.
597 if lines:
598 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000599 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000600 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000601 # Remove any trailing or leading blank lines.
602 while lines and not lines[-1]:
603 lines.pop()
604 while lines and not lines[0]:
605 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000606 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000607
608def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000609 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000610 if ismodule(object):
611 if hasattr(object, '__file__'):
612 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000613 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000614 if isclass(object):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500615 if hasattr(object, '__module__'):
616 object = sys.modules.get(object.__module__)
617 if hasattr(object, '__file__'):
618 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000619 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000620 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000621 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000622 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000623 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000624 if istraceback(object):
625 object = object.tb_frame
626 if isframe(object):
627 object = object.f_code
628 if iscode(object):
629 return object.co_filename
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000630 raise TypeError('{!r} is not a module, class, method, '
631 'function, traceback, frame, or code object'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000632
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000633def getmodulename(path):
634 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000635 fname = os.path.basename(path)
636 # Check for paths that look like an actual module file
637 suffixes = [(-len(suffix), suffix)
638 for suffix in importlib.machinery.all_suffixes()]
639 suffixes.sort() # try longest suffixes first, in case they overlap
640 for neglen, suffix in suffixes:
641 if fname.endswith(suffix):
642 return fname[:neglen]
643 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000644
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000645def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000646 """Return the filename that can be used to locate an object's source.
647 Return None if no way can be identified to get the source.
648 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000649 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400650 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
651 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
652 if any(filename.endswith(s) for s in all_bytecode_suffixes):
653 filename = (os.path.splitext(filename)[0] +
654 importlib.machinery.SOURCE_SUFFIXES[0])
655 elif any(filename.endswith(s) for s in
656 importlib.machinery.EXTENSION_SUFFIXES):
657 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000658 if os.path.exists(filename):
659 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000660 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400661 if getattr(getmodule(object, filename), '__loader__', None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000662 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000663 # or it is in the linecache
664 if filename in linecache.cache:
665 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000666
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000667def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000668 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000669
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000670 The idea is for each object to have a unique origin, so this routine
671 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000672 if _filename is None:
673 _filename = getsourcefile(object) or getfile(object)
674 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000675
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000676modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000677_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000678
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000679def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000680 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000681 if ismodule(object):
682 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000683 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000684 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000685 # Try the filename to modulename cache
686 if _filename is not None and _filename in modulesbyfile:
687 return sys.modules.get(modulesbyfile[_filename])
688 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000689 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000690 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000691 except TypeError:
692 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000693 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000694 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000695 # Update the filename to module name cache and check yet again
696 # Copy sys.modules in order to cope with changes while iterating
Éric Araujoa74f8ef2011-11-29 16:58:53 +0100697 for modname, module in list(sys.modules.items()):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000698 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000699 f = module.__file__
700 if f == _filesbymodname.get(modname, None):
701 # Have already mapped this module, so skip it
702 continue
703 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000704 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000705 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000706 modulesbyfile[f] = modulesbyfile[
707 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000708 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000709 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000710 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000711 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000712 if not hasattr(object, '__name__'):
713 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000714 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000715 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000716 if mainobject is object:
717 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000718 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000719 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000720 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000721 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000722 if builtinobject is object:
723 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000724
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000725def findsource(object):
726 """Return the entire source file and starting line number for an object.
727
728 The argument may be a module, class, method, function, traceback, frame,
729 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200730 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000731 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500732
Yury Selivanovef1e7502014-12-08 16:05:34 -0500733 file = getsourcefile(object)
734 if file:
735 # Invalidate cache if needed.
736 linecache.checkcache(file)
737 else:
738 file = getfile(object)
739 # Allow filenames in form of "<something>" to pass through.
740 # `doctest` monkeypatches `linecache` module to enable
741 # inspection, so let `linecache.getlines` to be called.
742 if not (file.startswith('<') and file.endswith('>')):
743 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500744
Thomas Wouters89f507f2006-12-13 04:49:30 +0000745 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000746 if module:
747 lines = linecache.getlines(file, module.__dict__)
748 else:
749 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000750 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200751 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000752
753 if ismodule(object):
754 return lines, 0
755
756 if isclass(object):
757 name = object.__name__
Thomas Wouters89f507f2006-12-13 04:49:30 +0000758 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
759 # make some effort to find the best matching class definition:
760 # use the one with the least indentation, which is the one
761 # that's most probably not inside a function definition.
762 candidates = []
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000763 for i in range(len(lines)):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000764 match = pat.match(lines[i])
765 if match:
766 # if it's at toplevel, it's already the best one
767 if lines[i][0] == 'c':
768 return lines, i
769 # else add whitespace to candidate list
770 candidates.append((match.group(1), i))
771 if candidates:
772 # this will sort by whitespace, and by line number,
773 # less whitespace first
774 candidates.sort()
775 return lines, candidates[0][1]
Jeremy Hyltonab919022003-06-27 18:41:20 +0000776 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200777 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000778
779 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000780 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000781 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000782 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000783 if istraceback(object):
784 object = object.tb_frame
785 if isframe(object):
786 object = object.f_code
787 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000788 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200789 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000790 lnum = object.co_firstlineno - 1
Yury Selivanove4e811d2015-07-21 19:01:52 +0300791 pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000792 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000793 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000794 lnum = lnum - 1
795 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200796 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000797
798def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000799 """Get lines of comments immediately preceding an object's source code.
800
801 Returns None when source can't be found.
802 """
803 try:
804 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200805 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000806 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000807
808 if ismodule(object):
809 # Look for a comment block at the top of the file.
810 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000811 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000812 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000813 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000814 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000815 comments = []
816 end = start
817 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000818 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000819 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000820 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000821
822 # Look for a preceding block of comments at the same indentation.
823 elif lnum > 0:
824 indent = indentsize(lines[lnum])
825 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000826 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000827 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000828 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000829 if end > 0:
830 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000831 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000832 while comment[:1] == '#' and indentsize(lines[end]) == indent:
833 comments[:0] = [comment]
834 end = end - 1
835 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000836 comment = lines[end].expandtabs().lstrip()
837 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000838 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000839 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000840 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000841 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000842
Tim Peters4efb6e92001-06-29 23:51:08 +0000843class EndOfBlock(Exception): pass
844
845class BlockFinder:
846 """Provide a tokeneater() method to detect the end of a code block."""
847 def __init__(self):
848 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000849 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000850 self.started = False
851 self.passline = False
Meador Inge5b718d72015-07-23 22:49:37 -0500852 self.indecorator = False
853 self.decoratorhasargs = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000854 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000855
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000856 def tokeneater(self, type, token, srowcol, erowcol, line):
Meador Inge5b718d72015-07-23 22:49:37 -0500857 if not self.started and not self.indecorator:
858 # skip any decorators
859 if token == "@":
860 self.indecorator = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000861 # look for the first "def", "class" or "lambda"
Meador Inge5b718d72015-07-23 22:49:37 -0500862 elif token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000863 if token == "lambda":
864 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000865 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000866 self.passline = True # skip to the end of the line
Meador Inge5b718d72015-07-23 22:49:37 -0500867 elif token == "(":
868 if self.indecorator:
869 self.decoratorhasargs = True
870 elif token == ")":
871 if self.indecorator:
872 self.indecorator = False
873 self.decoratorhasargs = False
Tim Peters4efb6e92001-06-29 23:51:08 +0000874 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000875 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000876 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000877 if self.islambda: # lambdas always end at the first NEWLINE
878 raise EndOfBlock
Meador Inge5b718d72015-07-23 22:49:37 -0500879 # hitting a NEWLINE when in a decorator without args
880 # ends the decorator
881 if self.indecorator and not self.decoratorhasargs:
882 self.indecorator = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000883 elif self.passline:
884 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000885 elif type == tokenize.INDENT:
886 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000887 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000888 elif type == tokenize.DEDENT:
889 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000890 # the end of matching indent/dedent pairs end a block
891 # (note that this only works for "def"/"class" blocks,
892 # not e.g. for "if: else:" or "try: finally:" blocks)
893 if self.indent <= 0:
894 raise EndOfBlock
895 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
896 # any other token on the same indentation level end the previous
897 # block as well, except the pseudo-tokens COMMENT and NL.
898 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000899
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000900def getblock(lines):
901 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000902 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000903 try:
Trent Nelson428de652008-03-18 22:41:35 +0000904 tokens = tokenize.generate_tokens(iter(lines).__next__)
905 for _token in tokens:
906 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000907 except (EndOfBlock, IndentationError):
908 pass
909 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000910
911def getsourcelines(object):
912 """Return a list of source lines and starting line number for an object.
913
914 The argument may be a module, class, method, function, traceback, frame,
915 or code object. The source code is returned as a list of the lines
916 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200917 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000918 raised if the source code cannot be retrieved."""
Yury Selivanov081bbf62014-09-26 17:34:54 -0400919 object = unwrap(object)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000920 lines, lnum = findsource(object)
921
Meador Inge5b718d72015-07-23 22:49:37 -0500922 if ismodule(object):
923 return lines, 0
924 else:
925 return getblock(lines[lnum:]), lnum + 1
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000926
927def getsource(object):
928 """Return the text of the source code for an object.
929
930 The argument may be a module, class, method, function, traceback, frame,
931 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200932 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000933 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000934 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000935
936# --------------------------------------------------- class tree extraction
937def walktree(classes, children, parent):
938 """Recursive helper function for getclasstree()."""
939 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +0000940 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000941 for c in classes:
942 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +0000943 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000944 results.append(walktree(children[c], children, c))
945 return results
946
Georg Brandl5ce83a02009-06-01 17:23:51 +0000947def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000948 """Arrange the given list of classes into a hierarchy of nested lists.
949
950 Where a nested list appears, it contains classes derived from the class
951 whose entry immediately precedes the list. Each entry is a 2-tuple
952 containing a class and a tuple of its base classes. If the 'unique'
953 argument is true, exactly one entry appears in the returned structure
954 for each class in the given list. Otherwise, classes using multiple
955 inheritance and their descendants will appear multiple times."""
956 children = {}
957 roots = []
958 for c in classes:
959 if c.__bases__:
960 for parent in c.__bases__:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000961 if not parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000962 children[parent] = []
Serhiy Storchaka362c1b52013-09-05 17:14:32 +0300963 if c not in children[parent]:
964 children[parent].append(c)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000965 if unique and parent in classes: break
966 elif c not in roots:
967 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +0000968 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000969 if parent not in classes:
970 roots.append(parent)
971 return walktree(roots, children, None)
972
973# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +0000974Arguments = namedtuple('Arguments', 'args, varargs, varkw')
975
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000976def getargs(co):
977 """Get information about the arguments accepted by a code object.
978
Guido van Rossum2e65f892007-02-28 22:03:49 +0000979 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000980 'args' is the list of argument names. Keyword-only arguments are
981 appended. 'varargs' and 'varkw' are the names of the * and **
982 arguments or None."""
Guido van Rossum2e65f892007-02-28 22:03:49 +0000983 args, varargs, kwonlyargs, varkw = _getfullargs(co)
Christian Heimes25bb7832008-01-11 16:17:00 +0000984 return Arguments(args + kwonlyargs, varargs, varkw)
Guido van Rossum2e65f892007-02-28 22:03:49 +0000985
986def _getfullargs(co):
987 """Get information about the arguments accepted by a code object.
988
989 Four things are returned: (args, varargs, kwonlyargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000990 'args' and 'kwonlyargs' are lists of argument names, and 'varargs'
991 and 'varkw' are the names of the * and ** arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +0000992
993 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000994 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000995
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000996 nargs = co.co_argcount
997 names = co.co_varnames
Guido van Rossum2e65f892007-02-28 22:03:49 +0000998 nkwargs = co.co_kwonlyargcount
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000999 args = list(names[:nargs])
Guido van Rossum2e65f892007-02-28 22:03:49 +00001000 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001001 step = 0
1002
Guido van Rossum2e65f892007-02-28 22:03:49 +00001003 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001004 varargs = None
1005 if co.co_flags & CO_VARARGS:
1006 varargs = co.co_varnames[nargs]
1007 nargs = nargs + 1
1008 varkw = None
1009 if co.co_flags & CO_VARKEYWORDS:
1010 varkw = co.co_varnames[nargs]
Guido van Rossum2e65f892007-02-28 22:03:49 +00001011 return args, varargs, kwonlyargs, varkw
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001012
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001013
1014ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
1015
1016def getargspec(func):
1017 """Get the names and default values of a function's arguments.
1018
1019 A tuple of four things is returned: (args, varargs, keywords, defaults).
1020 'args' is a list of the argument names, including keyword-only argument names.
1021 'varargs' and 'keywords' are the names of the * and ** arguments or None.
1022 'defaults' is an n-tuple of the default values of the last n arguments.
1023
1024 Use the getfullargspec() API for Python 3 code, as annotations
1025 and keyword arguments are supported. getargspec() will raise ValueError
1026 if the func has either annotations or keyword arguments.
1027 """
1028 warnings.warn("inspect.getargspec() is deprecated, "
1029 "use inspect.signature() instead", DeprecationWarning,
1030 stacklevel=2)
1031 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
1032 getfullargspec(func)
1033 if kwonlyargs or ann:
1034 raise ValueError("Function has keyword-only arguments or annotations"
1035 ", use getfullargspec() API which can support them")
1036 return ArgSpec(args, varargs, varkw, defaults)
1037
Christian Heimes25bb7832008-01-11 16:17:00 +00001038FullArgSpec = namedtuple('FullArgSpec',
Benjamin Peterson3d4ca742008-11-12 21:39:01 +00001039 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001040
1041def getfullargspec(func):
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001042 """Get the names and default values of a callable object's arguments.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001043
Brett Cannon504d8852007-09-07 02:12:14 +00001044 A tuple of seven things is returned:
1045 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults annotations).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001046 'args' is a list of the argument names.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001047 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1048 'defaults' is an n-tuple of the default values of the last n arguments.
1049 'kwonlyargs' is a list of keyword-only argument names.
1050 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
1051 'annotations' is a dictionary mapping argument names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001052
Yury Selivanov3cfec2e2015-05-22 11:38:38 -04001053 This function is deprecated, use inspect.signature() instead.
Jeremy Hylton64967882003-06-27 18:14:39 +00001054 """
1055
Yury Selivanov57d240e2014-02-19 16:27:23 -05001056 try:
1057 # Re: `skip_bound_arg=False`
1058 #
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001059 # There is a notable difference in behaviour between getfullargspec
1060 # and Signature: the former always returns 'self' parameter for bound
1061 # methods, whereas the Signature always shows the actual calling
1062 # signature of the passed object.
1063 #
1064 # To simulate this behaviour, we "unbind" bound methods, to trick
1065 # inspect.signature to always return their first parameter ("self",
1066 # usually)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001067
Yury Selivanov57d240e2014-02-19 16:27:23 -05001068 # Re: `follow_wrapper_chains=False`
1069 #
1070 # getfullargspec() historically ignored __wrapped__ attributes,
1071 # so we ensure that remains the case in 3.3+
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001072
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001073 sig = _signature_from_callable(func,
1074 follow_wrapper_chains=False,
1075 skip_bound_arg=False,
1076 sigcls=Signature)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001077 except Exception as ex:
1078 # Most of the times 'signature' will raise ValueError.
1079 # But, it can also raise AttributeError, and, maybe something
1080 # else. So to be fully backwards compatible, we catch all
1081 # possible exceptions here, and reraise a TypeError.
1082 raise TypeError('unsupported callable') from ex
1083
1084 args = []
1085 varargs = None
1086 varkw = None
1087 kwonlyargs = []
1088 defaults = ()
1089 annotations = {}
1090 defaults = ()
1091 kwdefaults = {}
1092
1093 if sig.return_annotation is not sig.empty:
1094 annotations['return'] = sig.return_annotation
1095
1096 for param in sig.parameters.values():
1097 kind = param.kind
1098 name = param.name
1099
1100 if kind is _POSITIONAL_ONLY:
1101 args.append(name)
1102 elif kind is _POSITIONAL_OR_KEYWORD:
1103 args.append(name)
1104 if param.default is not param.empty:
1105 defaults += (param.default,)
1106 elif kind is _VAR_POSITIONAL:
1107 varargs = name
1108 elif kind is _KEYWORD_ONLY:
1109 kwonlyargs.append(name)
1110 if param.default is not param.empty:
1111 kwdefaults[name] = param.default
1112 elif kind is _VAR_KEYWORD:
1113 varkw = name
1114
1115 if param.annotation is not param.empty:
1116 annotations[name] = param.annotation
1117
1118 if not kwdefaults:
1119 # compatibility with 'func.__kwdefaults__'
1120 kwdefaults = None
1121
1122 if not defaults:
1123 # compatibility with 'func.__defaults__'
1124 defaults = None
1125
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001126 return FullArgSpec(args, varargs, varkw, defaults,
1127 kwonlyargs, kwdefaults, annotations)
1128
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001129
Christian Heimes25bb7832008-01-11 16:17:00 +00001130ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
1131
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001132def getargvalues(frame):
1133 """Get information about arguments passed into a particular frame.
1134
1135 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001136 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001137 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1138 'locals' is the locals dictionary of the given frame."""
1139 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +00001140 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001141
Guido van Rossum2e65f892007-02-28 22:03:49 +00001142def formatannotation(annotation, base_module=None):
1143 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001144 if annotation.__module__ in ('builtins', base_module):
Serhiy Storchaka521e5862014-07-22 15:00:37 +03001145 return annotation.__qualname__
1146 return annotation.__module__+'.'+annotation.__qualname__
Guido van Rossum2e65f892007-02-28 22:03:49 +00001147 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001148
Guido van Rossum2e65f892007-02-28 22:03:49 +00001149def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001150 module = getattr(object, '__module__', None)
1151 def _formatannotation(annotation):
1152 return formatannotation(annotation, module)
1153 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001154
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001155def formatargspec(args, varargs=None, varkw=None, defaults=None,
Guido van Rossum2e65f892007-02-28 22:03:49 +00001156 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001157 formatarg=str,
1158 formatvarargs=lambda name: '*' + name,
1159 formatvarkw=lambda name: '**' + name,
1160 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001161 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001162 formatannotation=formatannotation):
Berker Peksagfa3922c2015-07-31 04:11:29 +03001163 """Format an argument spec from the values returned by getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001164
Guido van Rossum2e65f892007-02-28 22:03:49 +00001165 The first seven arguments are (args, varargs, varkw, defaults,
1166 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1167 are the corresponding optional formatting functions that are called to
1168 turn names and values into strings. The last argument is an optional
1169 function to format the sequence of arguments."""
1170 def formatargandannotation(arg):
1171 result = formatarg(arg)
1172 if arg in annotations:
1173 result += ': ' + formatannotation(annotations[arg])
1174 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001175 specs = []
1176 if defaults:
1177 firstdefault = len(args) - len(defaults)
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001178 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001179 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001180 if defaults and i >= firstdefault:
1181 spec = spec + formatvalue(defaults[i - firstdefault])
1182 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001183 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001184 specs.append(formatvarargs(formatargandannotation(varargs)))
1185 else:
1186 if kwonlyargs:
1187 specs.append('*')
1188 if kwonlyargs:
1189 for kwonlyarg in kwonlyargs:
1190 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001191 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001192 spec += formatvalue(kwonlydefaults[kwonlyarg])
1193 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001194 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001195 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001196 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001197 if 'return' in annotations:
1198 result += formatreturns(formatannotation(annotations['return']))
1199 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001200
1201def formatargvalues(args, varargs, varkw, locals,
1202 formatarg=str,
1203 formatvarargs=lambda name: '*' + name,
1204 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001205 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001206 """Format an argument spec from the 4 values returned by getargvalues.
1207
1208 The first four arguments are (args, varargs, varkw, locals). The
1209 next four arguments are the corresponding optional formatting functions
1210 that are called to turn names and values into strings. The ninth
1211 argument is an optional function to format the sequence of arguments."""
1212 def convert(name, locals=locals,
1213 formatarg=formatarg, formatvalue=formatvalue):
1214 return formatarg(name) + formatvalue(locals[name])
1215 specs = []
1216 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001217 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001218 if varargs:
1219 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1220 if varkw:
1221 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001222 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001223
Benjamin Petersone109c702011-06-24 09:37:26 -05001224def _missing_arguments(f_name, argnames, pos, values):
1225 names = [repr(name) for name in argnames if name not in values]
1226 missing = len(names)
1227 if missing == 1:
1228 s = names[0]
1229 elif missing == 2:
1230 s = "{} and {}".format(*names)
1231 else:
Yury Selivanovdccfa132014-03-27 18:42:52 -04001232 tail = ", {} and {}".format(*names[-2:])
Benjamin Petersone109c702011-06-24 09:37:26 -05001233 del names[-2:]
1234 s = ", ".join(names) + tail
1235 raise TypeError("%s() missing %i required %s argument%s: %s" %
1236 (f_name, missing,
1237 "positional" if pos else "keyword-only",
1238 "" if missing == 1 else "s", s))
1239
1240def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001241 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001242 kwonly_given = len([arg for arg in kwonly if arg in values])
1243 if varargs:
1244 plural = atleast != 1
1245 sig = "at least %d" % (atleast,)
1246 elif defcount:
1247 plural = True
1248 sig = "from %d to %d" % (atleast, len(args))
1249 else:
1250 plural = len(args) != 1
1251 sig = str(len(args))
1252 kwonly_sig = ""
1253 if kwonly_given:
1254 msg = " positional argument%s (and %d keyword-only argument%s)"
1255 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1256 "s" if kwonly_given != 1 else ""))
1257 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1258 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1259 "was" if given == 1 and not kwonly_given else "were"))
1260
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001261def getcallargs(*func_and_positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001262 """Get the mapping of arguments to values.
1263
1264 A dict is returned, with keys the function argument names (including the
1265 names of the * and ** arguments, if any), and values the respective bound
1266 values from 'positional' and 'named'."""
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001267 func = func_and_positional[0]
1268 positional = func_and_positional[1:]
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001269 spec = getfullargspec(func)
1270 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
1271 f_name = func.__name__
1272 arg2value = {}
1273
Benjamin Petersonb204a422011-06-05 22:04:07 -05001274
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001275 if ismethod(func) and func.__self__ is not None:
1276 # implicit 'self' (or 'cls' for classmethods) argument
1277 positional = (func.__self__,) + positional
1278 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001279 num_args = len(args)
1280 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001281
Benjamin Petersonb204a422011-06-05 22:04:07 -05001282 n = min(num_pos, num_args)
1283 for i in range(n):
1284 arg2value[args[i]] = positional[i]
1285 if varargs:
1286 arg2value[varargs] = tuple(positional[n:])
1287 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001288 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001289 arg2value[varkw] = {}
1290 for kw, value in named.items():
1291 if kw not in possible_kwargs:
1292 if not varkw:
1293 raise TypeError("%s() got an unexpected keyword argument %r" %
1294 (f_name, kw))
1295 arg2value[varkw][kw] = value
1296 continue
1297 if kw in arg2value:
1298 raise TypeError("%s() got multiple values for argument %r" %
1299 (f_name, kw))
1300 arg2value[kw] = value
1301 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001302 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1303 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001304 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001305 req = args[:num_args - num_defaults]
1306 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001307 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001308 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001309 for i, arg in enumerate(args[num_args - num_defaults:]):
1310 if arg not in arg2value:
1311 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001312 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001313 for kwarg in kwonlyargs:
1314 if kwarg not in arg2value:
Yury Selivanov875df202014-03-27 18:23:03 -04001315 if kwonlydefaults and kwarg in kwonlydefaults:
Benjamin Petersone109c702011-06-24 09:37:26 -05001316 arg2value[kwarg] = kwonlydefaults[kwarg]
1317 else:
1318 missing += 1
1319 if missing:
1320 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001321 return arg2value
1322
Nick Coghlan2f92e542012-06-23 19:39:55 +10001323ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1324
1325def getclosurevars(func):
1326 """
1327 Get the mapping of free variables to their current values.
1328
Meador Inge8fda3592012-07-19 21:33:21 -05001329 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001330 and builtin references as seen by the body of the function. A final
1331 set of unbound names that could not be resolved is also provided.
1332 """
1333
1334 if ismethod(func):
1335 func = func.__func__
1336
1337 if not isfunction(func):
1338 raise TypeError("'{!r}' is not a Python function".format(func))
1339
1340 code = func.__code__
1341 # Nonlocal references are named in co_freevars and resolved
1342 # by looking them up in __closure__ by positional index
1343 if func.__closure__ is None:
1344 nonlocal_vars = {}
1345 else:
1346 nonlocal_vars = {
1347 var : cell.cell_contents
1348 for var, cell in zip(code.co_freevars, func.__closure__)
1349 }
1350
1351 # Global and builtin references are named in co_names and resolved
1352 # by looking them up in __globals__ or __builtins__
1353 global_ns = func.__globals__
1354 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1355 if ismodule(builtin_ns):
1356 builtin_ns = builtin_ns.__dict__
1357 global_vars = {}
1358 builtin_vars = {}
1359 unbound_names = set()
1360 for name in code.co_names:
1361 if name in ("None", "True", "False"):
1362 # Because these used to be builtins instead of keywords, they
1363 # may still show up as name references. We ignore them.
1364 continue
1365 try:
1366 global_vars[name] = global_ns[name]
1367 except KeyError:
1368 try:
1369 builtin_vars[name] = builtin_ns[name]
1370 except KeyError:
1371 unbound_names.add(name)
1372
1373 return ClosureVars(nonlocal_vars, global_vars,
1374 builtin_vars, unbound_names)
1375
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001376# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001377
1378Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1379
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001380def getframeinfo(frame, context=1):
1381 """Get information about a frame or traceback object.
1382
1383 A tuple of five things is returned: the filename, the line number of
1384 the current line, the function name, a list of lines of context from
1385 the source code, and the index of the current line within that list.
1386 The optional second argument specifies the number of lines of context
1387 to return, which are centered around the current line."""
1388 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001389 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001390 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001391 else:
1392 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001393 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001394 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001395
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001396 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001397 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001398 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001399 try:
1400 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001401 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001402 lines = index = None
1403 else:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001404 start = max(start, 1)
Raymond Hettingera0501712004-06-15 11:22:53 +00001405 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001406 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001407 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001408 else:
1409 lines = index = None
1410
Christian Heimes25bb7832008-01-11 16:17:00 +00001411 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001412
1413def getlineno(frame):
1414 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001415 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1416 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001417
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001418FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
1419
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001420def getouterframes(frame, context=1):
1421 """Get a list of records for a frame and all higher (calling) frames.
1422
1423 Each record contains a frame object, filename, line number, function
1424 name, a list of lines of context, and index within the context."""
1425 framelist = []
1426 while frame:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001427 frameinfo = (frame,) + getframeinfo(frame, context)
1428 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001429 frame = frame.f_back
1430 return framelist
1431
1432def getinnerframes(tb, context=1):
1433 """Get a list of records for a traceback's frame and all lower frames.
1434
1435 Each record contains a frame object, filename, line number, function
1436 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001437 framelist = []
1438 while tb:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001439 frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
1440 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001441 tb = tb.tb_next
1442 return framelist
1443
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001444def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001445 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001446 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001447
1448def stack(context=1):
1449 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001450 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001451
1452def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001453 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001454 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001455
1456
1457# ------------------------------------------------ static version of getattr
1458
1459_sentinel = object()
1460
Michael Foorde5162652010-11-20 16:40:44 +00001461def _static_getmro(klass):
1462 return type.__dict__['__mro__'].__get__(klass)
1463
Michael Foord95fc51d2010-11-20 15:07:30 +00001464def _check_instance(obj, attr):
1465 instance_dict = {}
1466 try:
1467 instance_dict = object.__getattribute__(obj, "__dict__")
1468 except AttributeError:
1469 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001470 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001471
1472
1473def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001474 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001475 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001476 try:
1477 return entry.__dict__[attr]
1478 except KeyError:
1479 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001480 return _sentinel
1481
Michael Foord35184ed2010-11-20 16:58:30 +00001482def _is_type(obj):
1483 try:
1484 _static_getmro(obj)
1485 except TypeError:
1486 return False
1487 return True
1488
Michael Foorddcebe0f2011-03-15 19:20:44 -04001489def _shadowed_dict(klass):
1490 dict_attr = type.__dict__["__dict__"]
1491 for entry in _static_getmro(klass):
1492 try:
1493 class_dict = dict_attr.__get__(entry)["__dict__"]
1494 except KeyError:
1495 pass
1496 else:
1497 if not (type(class_dict) is types.GetSetDescriptorType and
1498 class_dict.__name__ == "__dict__" and
1499 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001500 return class_dict
1501 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001502
1503def getattr_static(obj, attr, default=_sentinel):
1504 """Retrieve attributes without triggering dynamic lookup via the
1505 descriptor protocol, __getattr__ or __getattribute__.
1506
1507 Note: this function may not be able to retrieve all attributes
1508 that getattr can fetch (like dynamically created attributes)
1509 and may find attributes that getattr can't (like descriptors
1510 that raise AttributeError). It can also return descriptor objects
1511 instead of instance members in some cases. See the
1512 documentation for details.
1513 """
1514 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001515 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001516 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001517 dict_attr = _shadowed_dict(klass)
1518 if (dict_attr is _sentinel or
1519 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001520 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001521 else:
1522 klass = obj
1523
1524 klass_result = _check_class(klass, attr)
1525
1526 if instance_result is not _sentinel and klass_result is not _sentinel:
1527 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1528 _check_class(type(klass_result), '__set__') is not _sentinel):
1529 return klass_result
1530
1531 if instance_result is not _sentinel:
1532 return instance_result
1533 if klass_result is not _sentinel:
1534 return klass_result
1535
1536 if obj is klass:
1537 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001538 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001539 if _shadowed_dict(type(entry)) is _sentinel:
1540 try:
1541 return entry.__dict__[attr]
1542 except KeyError:
1543 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001544 if default is not _sentinel:
1545 return default
1546 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001547
1548
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001549# ------------------------------------------------ generator introspection
1550
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001551GEN_CREATED = 'GEN_CREATED'
1552GEN_RUNNING = 'GEN_RUNNING'
1553GEN_SUSPENDED = 'GEN_SUSPENDED'
1554GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001555
1556def getgeneratorstate(generator):
1557 """Get current state of a generator-iterator.
1558
1559 Possible states are:
1560 GEN_CREATED: Waiting to start execution.
1561 GEN_RUNNING: Currently being executed by the interpreter.
1562 GEN_SUSPENDED: Currently suspended at a yield expression.
1563 GEN_CLOSED: Execution has completed.
1564 """
1565 if generator.gi_running:
1566 return GEN_RUNNING
1567 if generator.gi_frame is None:
1568 return GEN_CLOSED
1569 if generator.gi_frame.f_lasti == -1:
1570 return GEN_CREATED
1571 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001572
1573
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001574def getgeneratorlocals(generator):
1575 """
1576 Get the mapping of generator local variables to their current values.
1577
1578 A dict is returned, with the keys the local variable names and values the
1579 bound values."""
1580
1581 if not isgenerator(generator):
1582 raise TypeError("'{!r}' is not a Python generator".format(generator))
1583
1584 frame = getattr(generator, "gi_frame", None)
1585 if frame is not None:
1586 return generator.gi_frame.f_locals
1587 else:
1588 return {}
1589
Yury Selivanov5376ba92015-06-22 12:19:30 -04001590
1591# ------------------------------------------------ coroutine introspection
1592
1593CORO_CREATED = 'CORO_CREATED'
1594CORO_RUNNING = 'CORO_RUNNING'
1595CORO_SUSPENDED = 'CORO_SUSPENDED'
1596CORO_CLOSED = 'CORO_CLOSED'
1597
1598def getcoroutinestate(coroutine):
1599 """Get current state of a coroutine object.
1600
1601 Possible states are:
1602 CORO_CREATED: Waiting to start execution.
1603 CORO_RUNNING: Currently being executed by the interpreter.
1604 CORO_SUSPENDED: Currently suspended at an await expression.
1605 CORO_CLOSED: Execution has completed.
1606 """
1607 if coroutine.cr_running:
1608 return CORO_RUNNING
1609 if coroutine.cr_frame is None:
1610 return CORO_CLOSED
1611 if coroutine.cr_frame.f_lasti == -1:
1612 return CORO_CREATED
1613 return CORO_SUSPENDED
1614
1615
1616def getcoroutinelocals(coroutine):
1617 """
1618 Get the mapping of coroutine local variables to their current values.
1619
1620 A dict is returned, with the keys the local variable names and values the
1621 bound values."""
1622 frame = getattr(coroutine, "cr_frame", None)
1623 if frame is not None:
1624 return frame.f_locals
1625 else:
1626 return {}
1627
1628
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001629###############################################################################
1630### Function Signature Object (PEP 362)
1631###############################################################################
1632
1633
1634_WrapperDescriptor = type(type.__call__)
1635_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001636_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001637
1638_NonUserDefinedCallables = (_WrapperDescriptor,
1639 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001640 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001641 types.BuiltinFunctionType)
1642
1643
Yury Selivanov421f0c72014-01-29 12:05:40 -05001644def _signature_get_user_defined_method(cls, method_name):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001645 """Private helper. Checks if ``cls`` has an attribute
1646 named ``method_name`` and returns it only if it is a
1647 pure python function.
1648 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001649 try:
1650 meth = getattr(cls, method_name)
1651 except AttributeError:
1652 return
1653 else:
1654 if not isinstance(meth, _NonUserDefinedCallables):
1655 # Once '__signature__' will be added to 'C'-level
1656 # callables, this check won't be necessary
1657 return meth
1658
1659
Yury Selivanov62560fb2014-01-28 12:26:24 -05001660def _signature_get_partial(wrapped_sig, partial, extra_args=()):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001661 """Private helper to calculate how 'wrapped_sig' signature will
1662 look like after applying a 'functools.partial' object (or alike)
1663 on it.
1664 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001665
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001666 old_params = wrapped_sig.parameters
1667 new_params = OrderedDict(old_params.items())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001668
1669 partial_args = partial.args or ()
1670 partial_keywords = partial.keywords or {}
1671
1672 if extra_args:
1673 partial_args = extra_args + partial_args
1674
1675 try:
1676 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1677 except TypeError as ex:
1678 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1679 raise ValueError(msg) from ex
1680
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001681
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001682 transform_to_kwonly = False
1683 for param_name, param in old_params.items():
1684 try:
1685 arg_value = ba.arguments[param_name]
1686 except KeyError:
1687 pass
1688 else:
1689 if param.kind is _POSITIONAL_ONLY:
1690 # If positional-only parameter is bound by partial,
1691 # it effectively disappears from the signature
1692 new_params.pop(param_name)
1693 continue
1694
1695 if param.kind is _POSITIONAL_OR_KEYWORD:
1696 if param_name in partial_keywords:
1697 # This means that this parameter, and all parameters
1698 # after it should be keyword-only (and var-positional
1699 # should be removed). Here's why. Consider the following
1700 # function:
1701 # foo(a, b, *args, c):
1702 # pass
1703 #
1704 # "partial(foo, a='spam')" will have the following
1705 # signature: "(*, a='spam', b, c)". Because attempting
1706 # to call that partial with "(10, 20)" arguments will
1707 # raise a TypeError, saying that "a" argument received
1708 # multiple values.
1709 transform_to_kwonly = True
1710 # Set the new default value
1711 new_params[param_name] = param.replace(default=arg_value)
1712 else:
1713 # was passed as a positional argument
1714 new_params.pop(param.name)
1715 continue
1716
1717 if param.kind is _KEYWORD_ONLY:
1718 # Set the new default value
1719 new_params[param_name] = param.replace(default=arg_value)
1720
1721 if transform_to_kwonly:
1722 assert param.kind is not _POSITIONAL_ONLY
1723
1724 if param.kind is _POSITIONAL_OR_KEYWORD:
1725 new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1726 new_params[param_name] = new_param
1727 new_params.move_to_end(param_name)
1728 elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
1729 new_params.move_to_end(param_name)
1730 elif param.kind is _VAR_POSITIONAL:
1731 new_params.pop(param.name)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001732
1733 return wrapped_sig.replace(parameters=new_params.values())
1734
1735
Yury Selivanov62560fb2014-01-28 12:26:24 -05001736def _signature_bound_method(sig):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001737 """Private helper to transform signatures for unbound
1738 functions to bound methods.
1739 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001740
1741 params = tuple(sig.parameters.values())
1742
1743 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1744 raise ValueError('invalid method signature')
1745
1746 kind = params[0].kind
1747 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1748 # Drop first parameter:
1749 # '(p1, p2[, ...])' -> '(p2[, ...])'
1750 params = params[1:]
1751 else:
1752 if kind is not _VAR_POSITIONAL:
1753 # Unless we add a new parameter type we never
1754 # get here
1755 raise ValueError('invalid argument type')
1756 # It's a var-positional parameter.
1757 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1758
1759 return sig.replace(parameters=params)
1760
1761
Yury Selivanovb77511d2014-01-29 10:46:14 -05001762def _signature_is_builtin(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001763 """Private helper to test if `obj` is a callable that might
1764 support Argument Clinic's __text_signature__ protocol.
1765 """
Yury Selivanov1d241832014-02-02 12:51:20 -05001766 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001767 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001768 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001769 # Can't test 'isinstance(type)' here, as it would
1770 # also be True for regular python classes
1771 obj in (type, object))
1772
1773
Yury Selivanov63da7c72014-01-31 14:48:37 -05001774def _signature_is_functionlike(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001775 """Private helper to test if `obj` is a duck type of FunctionType.
1776 A good example of such objects are functions compiled with
1777 Cython, which have all attributes that a pure Python function
1778 would have, but have their code statically compiled.
1779 """
Yury Selivanov63da7c72014-01-31 14:48:37 -05001780
1781 if not callable(obj) or isclass(obj):
1782 # All function-like objects are obviously callables,
1783 # and not classes.
1784 return False
1785
1786 name = getattr(obj, '__name__', None)
1787 code = getattr(obj, '__code__', None)
1788 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
1789 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
1790 annotations = getattr(obj, '__annotations__', None)
1791
1792 return (isinstance(code, types.CodeType) and
1793 isinstance(name, str) and
1794 (defaults is None or isinstance(defaults, tuple)) and
1795 (kwdefaults is None or isinstance(kwdefaults, dict)) and
1796 isinstance(annotations, dict))
1797
1798
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001799def _signature_get_bound_param(spec):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001800 """ Private helper to get first parameter name from a
1801 __text_signature__ of a builtin method, which should
1802 be in the following format: '($param1, ...)'.
1803 Assumptions are that the first argument won't have
1804 a default value or an annotation.
1805 """
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001806
1807 assert spec.startswith('($')
1808
1809 pos = spec.find(',')
1810 if pos == -1:
1811 pos = spec.find(')')
1812
1813 cpos = spec.find(':')
1814 assert cpos == -1 or cpos > pos
1815
1816 cpos = spec.find('=')
1817 assert cpos == -1 or cpos > pos
1818
1819 return spec[2:pos]
1820
1821
Larry Hastings2623c8c2014-02-08 22:15:29 -08001822def _signature_strip_non_python_syntax(signature):
1823 """
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001824 Private helper function. Takes a signature in Argument Clinic's
1825 extended signature format.
1826
Larry Hastings2623c8c2014-02-08 22:15:29 -08001827 Returns a tuple of three things:
1828 * that signature re-rendered in standard Python syntax,
1829 * the index of the "self" parameter (generally 0), or None if
1830 the function does not have a "self" parameter, and
1831 * the index of the last "positional only" parameter,
1832 or None if the signature has no positional-only parameters.
1833 """
1834
1835 if not signature:
1836 return signature, None, None
1837
1838 self_parameter = None
1839 last_positional_only = None
1840
1841 lines = [l.encode('ascii') for l in signature.split('\n')]
1842 generator = iter(lines).__next__
1843 token_stream = tokenize.tokenize(generator)
1844
1845 delayed_comma = False
1846 skip_next_comma = False
1847 text = []
1848 add = text.append
1849
1850 current_parameter = 0
1851 OP = token.OP
1852 ERRORTOKEN = token.ERRORTOKEN
1853
1854 # token stream always starts with ENCODING token, skip it
1855 t = next(token_stream)
1856 assert t.type == tokenize.ENCODING
1857
1858 for t in token_stream:
1859 type, string = t.type, t.string
1860
1861 if type == OP:
1862 if string == ',':
1863 if skip_next_comma:
1864 skip_next_comma = False
1865 else:
1866 assert not delayed_comma
1867 delayed_comma = True
1868 current_parameter += 1
1869 continue
1870
1871 if string == '/':
1872 assert not skip_next_comma
1873 assert last_positional_only is None
1874 skip_next_comma = True
1875 last_positional_only = current_parameter - 1
1876 continue
1877
1878 if (type == ERRORTOKEN) and (string == '$'):
1879 assert self_parameter is None
1880 self_parameter = current_parameter
1881 continue
1882
1883 if delayed_comma:
1884 delayed_comma = False
1885 if not ((type == OP) and (string == ')')):
1886 add(', ')
1887 add(string)
1888 if (string == ','):
1889 add(' ')
1890 clean_signature = ''.join(text)
1891 return clean_signature, self_parameter, last_positional_only
1892
1893
Yury Selivanov57d240e2014-02-19 16:27:23 -05001894def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001895 """Private helper to parse content of '__text_signature__'
1896 and return a Signature based on it.
1897 """
1898
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001899 Parameter = cls._parameter_cls
1900
Larry Hastings2623c8c2014-02-08 22:15:29 -08001901 clean_signature, self_parameter, last_positional_only = \
1902 _signature_strip_non_python_syntax(s)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001903
Larry Hastings2623c8c2014-02-08 22:15:29 -08001904 program = "def foo" + clean_signature + ": pass"
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001905
1906 try:
Larry Hastings2623c8c2014-02-08 22:15:29 -08001907 module = ast.parse(program)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001908 except SyntaxError:
1909 module = None
1910
1911 if not isinstance(module, ast.Module):
1912 raise ValueError("{!r} builtin has invalid signature".format(obj))
1913
1914 f = module.body[0]
1915
1916 parameters = []
1917 empty = Parameter.empty
1918 invalid = object()
1919
1920 module = None
1921 module_dict = {}
1922 module_name = getattr(obj, '__module__', None)
1923 if module_name:
1924 module = sys.modules.get(module_name, None)
1925 if module:
1926 module_dict = module.__dict__
1927 sys_module_dict = sys.modules
1928
1929 def parse_name(node):
1930 assert isinstance(node, ast.arg)
1931 if node.annotation != None:
1932 raise ValueError("Annotations are not currently supported")
1933 return node.arg
1934
1935 def wrap_value(s):
1936 try:
1937 value = eval(s, module_dict)
1938 except NameError:
1939 try:
1940 value = eval(s, sys_module_dict)
1941 except NameError:
1942 raise RuntimeError()
1943
1944 if isinstance(value, str):
1945 return ast.Str(value)
1946 if isinstance(value, (int, float)):
1947 return ast.Num(value)
1948 if isinstance(value, bytes):
1949 return ast.Bytes(value)
1950 if value in (True, False, None):
1951 return ast.NameConstant(value)
1952 raise RuntimeError()
1953
1954 class RewriteSymbolics(ast.NodeTransformer):
1955 def visit_Attribute(self, node):
1956 a = []
1957 n = node
1958 while isinstance(n, ast.Attribute):
1959 a.append(n.attr)
1960 n = n.value
1961 if not isinstance(n, ast.Name):
1962 raise RuntimeError()
1963 a.append(n.id)
1964 value = ".".join(reversed(a))
1965 return wrap_value(value)
1966
1967 def visit_Name(self, node):
1968 if not isinstance(node.ctx, ast.Load):
1969 raise ValueError()
1970 return wrap_value(node.id)
1971
1972 def p(name_node, default_node, default=empty):
1973 name = parse_name(name_node)
1974 if name is invalid:
1975 return None
1976 if default_node and default_node is not _empty:
1977 try:
1978 default_node = RewriteSymbolics().visit(default_node)
1979 o = ast.literal_eval(default_node)
1980 except ValueError:
1981 o = invalid
1982 if o is invalid:
1983 return None
1984 default = o if o is not invalid else default
1985 parameters.append(Parameter(name, kind, default=default, annotation=empty))
1986
1987 # non-keyword-only parameters
1988 args = reversed(f.args.args)
1989 defaults = reversed(f.args.defaults)
1990 iter = itertools.zip_longest(args, defaults, fillvalue=None)
Larry Hastings2623c8c2014-02-08 22:15:29 -08001991 if last_positional_only is not None:
1992 kind = Parameter.POSITIONAL_ONLY
1993 else:
1994 kind = Parameter.POSITIONAL_OR_KEYWORD
1995 for i, (name, default) in enumerate(reversed(list(iter))):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001996 p(name, default)
Larry Hastings2623c8c2014-02-08 22:15:29 -08001997 if i == last_positional_only:
1998 kind = Parameter.POSITIONAL_OR_KEYWORD
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001999
2000 # *args
2001 if f.args.vararg:
2002 kind = Parameter.VAR_POSITIONAL
2003 p(f.args.vararg, empty)
2004
2005 # keyword-only arguments
2006 kind = Parameter.KEYWORD_ONLY
2007 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
2008 p(name, default)
2009
2010 # **kwargs
2011 if f.args.kwarg:
2012 kind = Parameter.VAR_KEYWORD
2013 p(f.args.kwarg, empty)
2014
Larry Hastings2623c8c2014-02-08 22:15:29 -08002015 if self_parameter is not None:
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002016 # Possibly strip the bound argument:
2017 # - We *always* strip first bound argument if
2018 # it is a module.
2019 # - We don't strip first bound argument if
2020 # skip_bound_arg is False.
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002021 assert parameters
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002022 _self = getattr(obj, '__self__', None)
2023 self_isbound = _self is not None
2024 self_ismodule = ismodule(_self)
2025 if self_isbound and (self_ismodule or skip_bound_arg):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002026 parameters.pop(0)
2027 else:
2028 # for builtins, self parameter is always positional-only!
2029 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
2030 parameters[0] = p
2031
2032 return cls(parameters, return_annotation=cls.empty)
2033
2034
Yury Selivanov57d240e2014-02-19 16:27:23 -05002035def _signature_from_builtin(cls, func, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002036 """Private helper function to get signature for
2037 builtin callables.
2038 """
2039
Yury Selivanov57d240e2014-02-19 16:27:23 -05002040 if not _signature_is_builtin(func):
2041 raise TypeError("{!r} is not a Python builtin "
2042 "function".format(func))
2043
2044 s = getattr(func, "__text_signature__", None)
2045 if not s:
2046 raise ValueError("no signature found for builtin {!r}".format(func))
2047
2048 return _signature_fromstr(cls, func, s, skip_bound_arg)
2049
2050
Yury Selivanovcf45f022015-05-20 14:38:50 -04002051def _signature_from_function(cls, func):
2052 """Private helper: constructs Signature for the given python function."""
2053
2054 is_duck_function = False
2055 if not isfunction(func):
2056 if _signature_is_functionlike(func):
2057 is_duck_function = True
2058 else:
2059 # If it's not a pure Python function, and not a duck type
2060 # of pure function:
2061 raise TypeError('{!r} is not a Python function'.format(func))
2062
2063 Parameter = cls._parameter_cls
2064
2065 # Parameter information.
2066 func_code = func.__code__
2067 pos_count = func_code.co_argcount
2068 arg_names = func_code.co_varnames
2069 positional = tuple(arg_names[:pos_count])
2070 keyword_only_count = func_code.co_kwonlyargcount
2071 keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)]
2072 annotations = func.__annotations__
2073 defaults = func.__defaults__
2074 kwdefaults = func.__kwdefaults__
2075
2076 if defaults:
2077 pos_default_count = len(defaults)
2078 else:
2079 pos_default_count = 0
2080
2081 parameters = []
2082
2083 # Non-keyword-only parameters w/o defaults.
2084 non_default_count = pos_count - pos_default_count
2085 for name in positional[:non_default_count]:
2086 annotation = annotations.get(name, _empty)
2087 parameters.append(Parameter(name, annotation=annotation,
2088 kind=_POSITIONAL_OR_KEYWORD))
2089
2090 # ... w/ defaults.
2091 for offset, name in enumerate(positional[non_default_count:]):
2092 annotation = annotations.get(name, _empty)
2093 parameters.append(Parameter(name, annotation=annotation,
2094 kind=_POSITIONAL_OR_KEYWORD,
2095 default=defaults[offset]))
2096
2097 # *args
2098 if func_code.co_flags & CO_VARARGS:
2099 name = arg_names[pos_count + keyword_only_count]
2100 annotation = annotations.get(name, _empty)
2101 parameters.append(Parameter(name, annotation=annotation,
2102 kind=_VAR_POSITIONAL))
2103
2104 # Keyword-only parameters.
2105 for name in keyword_only:
2106 default = _empty
2107 if kwdefaults is not None:
2108 default = kwdefaults.get(name, _empty)
2109
2110 annotation = annotations.get(name, _empty)
2111 parameters.append(Parameter(name, annotation=annotation,
2112 kind=_KEYWORD_ONLY,
2113 default=default))
2114 # **kwargs
2115 if func_code.co_flags & CO_VARKEYWORDS:
2116 index = pos_count + keyword_only_count
2117 if func_code.co_flags & CO_VARARGS:
2118 index += 1
2119
2120 name = arg_names[index]
2121 annotation = annotations.get(name, _empty)
2122 parameters.append(Parameter(name, annotation=annotation,
2123 kind=_VAR_KEYWORD))
2124
2125 # Is 'func' is a pure Python function - don't validate the
2126 # parameters list (for correct order and defaults), it should be OK.
2127 return cls(parameters,
2128 return_annotation=annotations.get('return', _empty),
2129 __validate_parameters__=is_duck_function)
2130
2131
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002132def _signature_from_callable(obj, *,
2133 follow_wrapper_chains=True,
2134 skip_bound_arg=True,
2135 sigcls):
2136
2137 """Private helper function to get signature for arbitrary
2138 callable objects.
2139 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002140
2141 if not callable(obj):
2142 raise TypeError('{!r} is not a callable object'.format(obj))
2143
2144 if isinstance(obj, types.MethodType):
2145 # In this case we skip the first parameter of the underlying
2146 # function (usually `self` or `cls`).
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002147 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002148 obj.__func__,
2149 follow_wrapper_chains=follow_wrapper_chains,
2150 skip_bound_arg=skip_bound_arg,
2151 sigcls=sigcls)
2152
Yury Selivanov57d240e2014-02-19 16:27:23 -05002153 if skip_bound_arg:
2154 return _signature_bound_method(sig)
2155 else:
2156 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002157
Nick Coghlane8c45d62013-07-28 20:00:01 +10002158 # Was this function wrapped by a decorator?
Yury Selivanov57d240e2014-02-19 16:27:23 -05002159 if follow_wrapper_chains:
2160 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
Yury Selivanov46c759d2015-05-27 21:56:53 -04002161 if isinstance(obj, types.MethodType):
2162 # If the unwrapped object is a *method*, we might want to
2163 # skip its first parameter (self).
2164 # See test_signature_wrapped_bound_method for details.
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002165 return _signature_from_callable(
Yury Selivanov46c759d2015-05-27 21:56:53 -04002166 obj,
2167 follow_wrapper_chains=follow_wrapper_chains,
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002168 skip_bound_arg=skip_bound_arg,
2169 sigcls=sigcls)
Nick Coghlane8c45d62013-07-28 20:00:01 +10002170
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002171 try:
2172 sig = obj.__signature__
2173 except AttributeError:
2174 pass
2175 else:
2176 if sig is not None:
Yury Selivanov42407ab2014-06-23 10:23:50 -07002177 if not isinstance(sig, Signature):
2178 raise TypeError(
2179 'unexpected object {!r} in __signature__ '
2180 'attribute'.format(sig))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002181 return sig
2182
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002183 try:
2184 partialmethod = obj._partialmethod
2185 except AttributeError:
2186 pass
2187 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05002188 if isinstance(partialmethod, functools.partialmethod):
2189 # Unbound partialmethod (see functools.partialmethod)
2190 # This means, that we need to calculate the signature
2191 # as if it's a regular partial object, but taking into
2192 # account that the first positional argument
2193 # (usually `self`, or `cls`) will not be passed
2194 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002195
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002196 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002197 partialmethod.func,
2198 follow_wrapper_chains=follow_wrapper_chains,
2199 skip_bound_arg=skip_bound_arg,
2200 sigcls=sigcls)
2201
Yury Selivanov0486f812014-01-29 12:18:59 -05002202 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002203
Yury Selivanov0486f812014-01-29 12:18:59 -05002204 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
2205 new_params = (first_wrapped_param,) + tuple(sig.parameters.values())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002206
Yury Selivanov0486f812014-01-29 12:18:59 -05002207 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002208
Yury Selivanov63da7c72014-01-31 14:48:37 -05002209 if isfunction(obj) or _signature_is_functionlike(obj):
2210 # If it's a pure Python function, or an object that is duck type
2211 # of a Python function (Cython functions, for instance), then:
Yury Selivanovcf45f022015-05-20 14:38:50 -04002212 return _signature_from_function(sigcls, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002213
Yury Selivanova773de02014-02-21 18:30:53 -05002214 if _signature_is_builtin(obj):
Yury Selivanovda396452014-03-27 12:09:24 -04002215 return _signature_from_builtin(sigcls, obj,
Yury Selivanova773de02014-02-21 18:30:53 -05002216 skip_bound_arg=skip_bound_arg)
2217
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002218 if isinstance(obj, functools.partial):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002219 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002220 obj.func,
2221 follow_wrapper_chains=follow_wrapper_chains,
2222 skip_bound_arg=skip_bound_arg,
2223 sigcls=sigcls)
Yury Selivanov62560fb2014-01-28 12:26:24 -05002224 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002225
2226 sig = None
2227 if isinstance(obj, type):
2228 # obj is a class or a metaclass
2229
2230 # First, let's see if it has an overloaded __call__ defined
2231 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05002232 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002233 if call is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002234 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002235 call,
2236 follow_wrapper_chains=follow_wrapper_chains,
2237 skip_bound_arg=skip_bound_arg,
2238 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002239 else:
2240 # Now we check if the 'obj' class has a '__new__' method
Yury Selivanov421f0c72014-01-29 12:05:40 -05002241 new = _signature_get_user_defined_method(obj, '__new__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002242 if new is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002243 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002244 new,
2245 follow_wrapper_chains=follow_wrapper_chains,
2246 skip_bound_arg=skip_bound_arg,
2247 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002248 else:
2249 # Finally, we should have at least __init__ implemented
Yury Selivanov421f0c72014-01-29 12:05:40 -05002250 init = _signature_get_user_defined_method(obj, '__init__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002251 if init is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002252 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002253 init,
2254 follow_wrapper_chains=follow_wrapper_chains,
2255 skip_bound_arg=skip_bound_arg,
2256 sigcls=sigcls)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002257
2258 if sig is None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002259 # At this point we know, that `obj` is a class, with no user-
2260 # defined '__init__', '__new__', or class-level '__call__'
2261
Larry Hastings2623c8c2014-02-08 22:15:29 -08002262 for base in obj.__mro__[:-1]:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002263 # Since '__text_signature__' is implemented as a
2264 # descriptor that extracts text signature from the
2265 # class docstring, if 'obj' is derived from a builtin
2266 # class, its own '__text_signature__' may be 'None'.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002267 # Therefore, we go through the MRO (except the last
2268 # class in there, which is 'object') to find the first
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002269 # class with non-empty text signature.
2270 try:
2271 text_sig = base.__text_signature__
2272 except AttributeError:
2273 pass
2274 else:
2275 if text_sig:
2276 # If 'obj' class has a __text_signature__ attribute:
2277 # return a signature based on it
Yury Selivanovda396452014-03-27 12:09:24 -04002278 return _signature_fromstr(sigcls, obj, text_sig)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002279
2280 # No '__text_signature__' was found for the 'obj' class.
2281 # Last option is to check if its '__init__' is
2282 # object.__init__ or type.__init__.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002283 if type not in obj.__mro__:
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002284 # We have a class (not metaclass), but no user-defined
2285 # __init__ or __new__ for it
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002286 if (obj.__init__ is object.__init__ and
2287 obj.__new__ is object.__new__):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002288 # Return a signature of 'object' builtin.
2289 return signature(object)
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002290 else:
2291 raise ValueError(
2292 'no signature found for builtin type {!r}'.format(obj))
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002293
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002294 elif not isinstance(obj, _NonUserDefinedCallables):
2295 # An object with __call__
2296 # We also check that the 'obj' is not an instance of
2297 # _WrapperDescriptor or _MethodWrapper to avoid
2298 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05002299 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002300 if call is not None:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002301 try:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002302 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002303 call,
2304 follow_wrapper_chains=follow_wrapper_chains,
2305 skip_bound_arg=skip_bound_arg,
2306 sigcls=sigcls)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002307 except ValueError as ex:
2308 msg = 'no signature found for {!r}'.format(obj)
2309 raise ValueError(msg) from ex
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002310
2311 if sig is not None:
2312 # For classes and objects we skip the first parameter of their
2313 # __call__, __new__, or __init__ methods
Yury Selivanov57d240e2014-02-19 16:27:23 -05002314 if skip_bound_arg:
2315 return _signature_bound_method(sig)
2316 else:
2317 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002318
2319 if isinstance(obj, types.BuiltinFunctionType):
2320 # Raise a nicer error message for builtins
2321 msg = 'no signature found for builtin function {!r}'.format(obj)
2322 raise ValueError(msg)
2323
2324 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2325
2326
2327class _void:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002328 """A private marker - used in Parameter & Signature."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002329
2330
2331class _empty:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002332 """Marker object for Signature.empty and Parameter.empty."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002333
2334
Yury Selivanov21e83a52014-03-27 11:23:13 -04002335class _ParameterKind(enum.IntEnum):
2336 POSITIONAL_ONLY = 0
2337 POSITIONAL_OR_KEYWORD = 1
2338 VAR_POSITIONAL = 2
2339 KEYWORD_ONLY = 3
2340 VAR_KEYWORD = 4
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002341
2342 def __str__(self):
Yury Selivanov21e83a52014-03-27 11:23:13 -04002343 return self._name_
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002344
2345
Yury Selivanov21e83a52014-03-27 11:23:13 -04002346_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
2347_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
2348_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
2349_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
2350_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002351
2352
2353class Parameter:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002354 """Represents a parameter in a function signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002355
2356 Has the following public attributes:
2357
2358 * name : str
2359 The name of the parameter as a string.
2360 * default : object
2361 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002362 parameter has no default value, this attribute is set to
2363 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002364 * annotation
2365 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002366 parameter has no annotation, this attribute is set to
2367 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002368 * kind : str
2369 Describes how argument values are bound to the parameter.
2370 Possible values: `Parameter.POSITIONAL_ONLY`,
2371 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
2372 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002373 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002374
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002375 __slots__ = ('_name', '_kind', '_default', '_annotation')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002376
2377 POSITIONAL_ONLY = _POSITIONAL_ONLY
2378 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
2379 VAR_POSITIONAL = _VAR_POSITIONAL
2380 KEYWORD_ONLY = _KEYWORD_ONLY
2381 VAR_KEYWORD = _VAR_KEYWORD
2382
2383 empty = _empty
2384
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002385 def __init__(self, name, kind, *, default=_empty, annotation=_empty):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002386
2387 if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
2388 _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
2389 raise ValueError("invalid value for 'Parameter.kind' attribute")
2390 self._kind = kind
2391
2392 if default is not _empty:
2393 if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2394 msg = '{} parameters cannot have default values'.format(kind)
2395 raise ValueError(msg)
2396 self._default = default
2397 self._annotation = annotation
2398
Yury Selivanov2393dca2014-01-27 15:07:58 -05002399 if name is _empty:
2400 raise ValueError('name is a required attribute for Parameter')
2401
2402 if not isinstance(name, str):
2403 raise TypeError("name must be a str, not a {!r}".format(name))
2404
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002405 if name[0] == '.' and name[1:].isdigit():
2406 # These are implicit arguments generated by comprehensions. In
2407 # order to provide a friendlier interface to users, we recast
2408 # their name as "implicitN" and treat them as positional-only.
2409 # See issue 19611.
2410 if kind != _POSITIONAL_OR_KEYWORD:
2411 raise ValueError(
2412 'implicit arguments must be passed in as {}'.format(
2413 _POSITIONAL_OR_KEYWORD
2414 )
2415 )
2416 self._kind = _POSITIONAL_ONLY
2417 name = 'implicit{}'.format(name[1:])
2418
Yury Selivanov2393dca2014-01-27 15:07:58 -05002419 if not name.isidentifier():
2420 raise ValueError('{!r} is not a valid parameter name'.format(name))
2421
2422 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002423
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002424 def __reduce__(self):
2425 return (type(self),
2426 (self._name, self._kind),
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002427 {'_default': self._default,
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002428 '_annotation': self._annotation})
2429
2430 def __setstate__(self, state):
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002431 self._default = state['_default']
2432 self._annotation = state['_annotation']
2433
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002434 @property
2435 def name(self):
2436 return self._name
2437
2438 @property
2439 def default(self):
2440 return self._default
2441
2442 @property
2443 def annotation(self):
2444 return self._annotation
2445
2446 @property
2447 def kind(self):
2448 return self._kind
2449
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002450 def replace(self, *, name=_void, kind=_void,
2451 annotation=_void, default=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002452 """Creates a customized copy of the Parameter."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002453
2454 if name is _void:
2455 name = self._name
2456
2457 if kind is _void:
2458 kind = self._kind
2459
2460 if annotation is _void:
2461 annotation = self._annotation
2462
2463 if default is _void:
2464 default = self._default
2465
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002466 return type(self)(name, kind, default=default, annotation=annotation)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002467
2468 def __str__(self):
2469 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002470 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002471
2472 # Add annotation and default value
2473 if self._annotation is not _empty:
2474 formatted = '{}:{}'.format(formatted,
2475 formatannotation(self._annotation))
2476
2477 if self._default is not _empty:
2478 formatted = '{}={}'.format(formatted, repr(self._default))
2479
2480 if kind == _VAR_POSITIONAL:
2481 formatted = '*' + formatted
2482 elif kind == _VAR_KEYWORD:
2483 formatted = '**' + formatted
2484
2485 return formatted
2486
2487 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002488 return '<{} "{}">'.format(self.__class__.__name__, self)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002489
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002490 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002491 return hash((self.name, self.kind, self.annotation, self.default))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002492
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002493 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002494 if self is other:
2495 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002496 if not isinstance(other, Parameter):
2497 return NotImplemented
2498 return (self._name == other._name and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002499 self._kind == other._kind and
2500 self._default == other._default and
2501 self._annotation == other._annotation)
2502
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002503
2504class BoundArguments:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002505 """Result of `Signature.bind` call. Holds the mapping of arguments
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002506 to the function's parameters.
2507
2508 Has the following public attributes:
2509
2510 * arguments : OrderedDict
2511 An ordered mutable mapping of parameters' names to arguments' values.
2512 Does not contain arguments' default values.
2513 * signature : Signature
2514 The Signature object that created this instance.
2515 * args : tuple
2516 Tuple of positional arguments values.
2517 * kwargs : dict
2518 Dict of keyword arguments values.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002519 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002520
Yury Selivanov6abe0322015-05-13 17:18:41 -04002521 __slots__ = ('arguments', '_signature', '__weakref__')
2522
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002523 def __init__(self, signature, arguments):
2524 self.arguments = arguments
2525 self._signature = signature
2526
2527 @property
2528 def signature(self):
2529 return self._signature
2530
2531 @property
2532 def args(self):
2533 args = []
2534 for param_name, param in self._signature.parameters.items():
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002535 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002536 break
2537
2538 try:
2539 arg = self.arguments[param_name]
2540 except KeyError:
2541 # We're done here. Other arguments
2542 # will be mapped in 'BoundArguments.kwargs'
2543 break
2544 else:
2545 if param.kind == _VAR_POSITIONAL:
2546 # *args
2547 args.extend(arg)
2548 else:
2549 # plain argument
2550 args.append(arg)
2551
2552 return tuple(args)
2553
2554 @property
2555 def kwargs(self):
2556 kwargs = {}
2557 kwargs_started = False
2558 for param_name, param in self._signature.parameters.items():
2559 if not kwargs_started:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002560 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002561 kwargs_started = True
2562 else:
2563 if param_name not in self.arguments:
2564 kwargs_started = True
2565 continue
2566
2567 if not kwargs_started:
2568 continue
2569
2570 try:
2571 arg = self.arguments[param_name]
2572 except KeyError:
2573 pass
2574 else:
2575 if param.kind == _VAR_KEYWORD:
2576 # **kwargs
2577 kwargs.update(arg)
2578 else:
2579 # plain keyword argument
2580 kwargs[param_name] = arg
2581
2582 return kwargs
2583
Yury Selivanovb907a512015-05-16 13:45:09 -04002584 def apply_defaults(self):
2585 """Set default values for missing arguments.
2586
2587 For variable-positional arguments (*args) the default is an
2588 empty tuple.
2589
2590 For variable-keyword arguments (**kwargs) the default is an
2591 empty dict.
2592 """
2593 arguments = self.arguments
Yury Selivanovb907a512015-05-16 13:45:09 -04002594 new_arguments = []
2595 for name, param in self._signature.parameters.items():
2596 try:
2597 new_arguments.append((name, arguments[name]))
2598 except KeyError:
2599 if param.default is not _empty:
2600 val = param.default
2601 elif param.kind is _VAR_POSITIONAL:
2602 val = ()
2603 elif param.kind is _VAR_KEYWORD:
2604 val = {}
2605 else:
2606 # This BoundArguments was likely produced by
2607 # Signature.bind_partial().
2608 continue
2609 new_arguments.append((name, val))
2610 self.arguments = OrderedDict(new_arguments)
2611
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002612 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002613 if self is other:
2614 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002615 if not isinstance(other, BoundArguments):
2616 return NotImplemented
2617 return (self.signature == other.signature and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002618 self.arguments == other.arguments)
2619
Yury Selivanov6abe0322015-05-13 17:18:41 -04002620 def __setstate__(self, state):
2621 self._signature = state['_signature']
2622 self.arguments = state['arguments']
2623
2624 def __getstate__(self):
2625 return {'_signature': self._signature, 'arguments': self.arguments}
2626
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002627 def __repr__(self):
2628 args = []
2629 for arg, value in self.arguments.items():
2630 args.append('{}={!r}'.format(arg, value))
Yury Selivanovf229bc52015-05-15 12:53:56 -04002631 return '<{} ({})>'.format(self.__class__.__name__, ', '.join(args))
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002632
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002633
2634class Signature:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002635 """A Signature object represents the overall signature of a function.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002636 It stores a Parameter object for each parameter accepted by the
2637 function, as well as information specific to the function itself.
2638
2639 A Signature object has the following public attributes and methods:
2640
2641 * parameters : OrderedDict
2642 An ordered mapping of parameters' names to the corresponding
2643 Parameter objects (keyword-only arguments are in the same order
2644 as listed in `code.co_varnames`).
2645 * return_annotation : object
2646 The annotation for the return type of the function if specified.
2647 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002648 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002649 * bind(*args, **kwargs) -> BoundArguments
2650 Creates a mapping from positional and keyword arguments to
2651 parameters.
2652 * bind_partial(*args, **kwargs) -> BoundArguments
2653 Creates a partial mapping from positional and keyword arguments
2654 to parameters (simulating 'functools.partial' behavior.)
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002655 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002656
2657 __slots__ = ('_return_annotation', '_parameters')
2658
2659 _parameter_cls = Parameter
2660 _bound_arguments_cls = BoundArguments
2661
2662 empty = _empty
2663
2664 def __init__(self, parameters=None, *, return_annotation=_empty,
2665 __validate_parameters__=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002666 """Constructs Signature from the given list of Parameter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002667 objects and 'return_annotation'. All arguments are optional.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002668 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002669
2670 if parameters is None:
2671 params = OrderedDict()
2672 else:
2673 if __validate_parameters__:
2674 params = OrderedDict()
2675 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002676 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002677
2678 for idx, param in enumerate(parameters):
2679 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002680 name = param.name
2681
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002682 if kind < top_kind:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002683 msg = 'wrong parameter order: {!r} before {!r}'
Yury Selivanov2393dca2014-01-27 15:07:58 -05002684 msg = msg.format(top_kind, kind)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002685 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002686 elif kind > top_kind:
2687 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002688 top_kind = kind
2689
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002690 if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
Yury Selivanov07a9e452014-01-29 10:58:16 -05002691 if param.default is _empty:
2692 if kind_defaults:
2693 # No default for this parameter, but the
2694 # previous parameter of the same kind had
2695 # a default
2696 msg = 'non-default argument follows default ' \
2697 'argument'
2698 raise ValueError(msg)
2699 else:
2700 # There is a default for this parameter.
2701 kind_defaults = True
2702
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002703 if name in params:
2704 msg = 'duplicate parameter name: {!r}'.format(name)
2705 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002706
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002707 params[name] = param
2708 else:
2709 params = OrderedDict(((param.name, param)
2710 for param in parameters))
2711
2712 self._parameters = types.MappingProxyType(params)
2713 self._return_annotation = return_annotation
2714
2715 @classmethod
2716 def from_function(cls, func):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002717 """Constructs Signature for the given python function."""
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002718
2719 warnings.warn("inspect.Signature.from_function() is deprecated, "
Berker Peksagb5601582015-05-21 23:40:54 +03002720 "use Signature.from_callable()",
2721 DeprecationWarning, stacklevel=2)
Yury Selivanovcf45f022015-05-20 14:38:50 -04002722 return _signature_from_function(cls, func)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002723
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002724 @classmethod
2725 def from_builtin(cls, func):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002726 """Constructs Signature for the given builtin function."""
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002727
2728 warnings.warn("inspect.Signature.from_builtin() is deprecated, "
Berker Peksagb5601582015-05-21 23:40:54 +03002729 "use Signature.from_callable()",
2730 DeprecationWarning, stacklevel=2)
Yury Selivanov57d240e2014-02-19 16:27:23 -05002731 return _signature_from_builtin(cls, func)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002732
Yury Selivanovda396452014-03-27 12:09:24 -04002733 @classmethod
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002734 def from_callable(cls, obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002735 """Constructs Signature for the given callable object."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002736 return _signature_from_callable(obj, sigcls=cls,
2737 follow_wrapper_chains=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04002738
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002739 @property
2740 def parameters(self):
2741 return self._parameters
2742
2743 @property
2744 def return_annotation(self):
2745 return self._return_annotation
2746
2747 def replace(self, *, parameters=_void, return_annotation=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002748 """Creates a customized copy of the Signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002749 Pass 'parameters' and/or 'return_annotation' arguments
2750 to override them in the new copy.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002751 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002752
2753 if parameters is _void:
2754 parameters = self.parameters.values()
2755
2756 if return_annotation is _void:
2757 return_annotation = self._return_annotation
2758
2759 return type(self)(parameters,
2760 return_annotation=return_annotation)
2761
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002762 def _hash_basis(self):
2763 params = tuple(param for param in self.parameters.values()
2764 if param.kind != _KEYWORD_ONLY)
2765
2766 kwo_params = {param.name: param for param in self.parameters.values()
2767 if param.kind == _KEYWORD_ONLY}
2768
2769 return params, kwo_params, self.return_annotation
2770
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002771 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002772 params, kwo_params, return_annotation = self._hash_basis()
2773 kwo_params = frozenset(kwo_params.values())
2774 return hash((params, kwo_params, return_annotation))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002775
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002776 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002777 if self is other:
2778 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002779 if not isinstance(other, Signature):
2780 return NotImplemented
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002781 return self._hash_basis() == other._hash_basis()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002782
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002783 def _bind(self, args, kwargs, *, partial=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002784 """Private method. Don't use directly."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002785
2786 arguments = OrderedDict()
2787
2788 parameters = iter(self.parameters.values())
2789 parameters_ex = ()
2790 arg_vals = iter(args)
2791
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002792 while True:
2793 # Let's iterate through the positional arguments and corresponding
2794 # parameters
2795 try:
2796 arg_val = next(arg_vals)
2797 except StopIteration:
2798 # No more positional arguments
2799 try:
2800 param = next(parameters)
2801 except StopIteration:
2802 # No more parameters. That's it. Just need to check that
2803 # we have no `kwargs` after this while loop
2804 break
2805 else:
2806 if param.kind == _VAR_POSITIONAL:
2807 # That's OK, just empty *args. Let's start parsing
2808 # kwargs
2809 break
2810 elif param.name in kwargs:
2811 if param.kind == _POSITIONAL_ONLY:
2812 msg = '{arg!r} parameter is positional only, ' \
2813 'but was passed as a keyword'
2814 msg = msg.format(arg=param.name)
2815 raise TypeError(msg) from None
2816 parameters_ex = (param,)
2817 break
2818 elif (param.kind == _VAR_KEYWORD or
2819 param.default is not _empty):
2820 # That's fine too - we have a default value for this
2821 # parameter. So, lets start parsing `kwargs`, starting
2822 # with the current parameter
2823 parameters_ex = (param,)
2824 break
2825 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002826 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
2827 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002828 if partial:
2829 parameters_ex = (param,)
2830 break
2831 else:
Yury Selivanov86872752015-05-19 00:27:49 -04002832 msg = 'missing a required argument: {arg!r}'
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002833 msg = msg.format(arg=param.name)
2834 raise TypeError(msg) from None
2835 else:
2836 # We have a positional argument to process
2837 try:
2838 param = next(parameters)
2839 except StopIteration:
2840 raise TypeError('too many positional arguments') from None
2841 else:
2842 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
2843 # Looks like we have no parameter for this positional
2844 # argument
Yury Selivanov86872752015-05-19 00:27:49 -04002845 raise TypeError(
2846 'too many positional arguments') from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002847
2848 if param.kind == _VAR_POSITIONAL:
2849 # We have an '*args'-like argument, let's fill it with
2850 # all positional arguments we have left and move on to
2851 # the next phase
2852 values = [arg_val]
2853 values.extend(arg_vals)
2854 arguments[param.name] = tuple(values)
2855 break
2856
2857 if param.name in kwargs:
Yury Selivanov86872752015-05-19 00:27:49 -04002858 raise TypeError(
2859 'multiple values for argument {arg!r}'.format(
2860 arg=param.name)) from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002861
2862 arguments[param.name] = arg_val
2863
2864 # Now, we iterate through the remaining parameters to process
2865 # keyword arguments
2866 kwargs_param = None
2867 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002868 if param.kind == _VAR_KEYWORD:
2869 # Memorize that we have a '**kwargs'-like parameter
2870 kwargs_param = param
2871 continue
2872
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002873 if param.kind == _VAR_POSITIONAL:
2874 # Named arguments don't refer to '*args'-like parameters.
2875 # We only arrive here if the positional arguments ended
2876 # before reaching the last parameter before *args.
2877 continue
2878
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002879 param_name = param.name
2880 try:
2881 arg_val = kwargs.pop(param_name)
2882 except KeyError:
2883 # We have no value for this parameter. It's fine though,
2884 # if it has a default value, or it is an '*args'-like
2885 # parameter, left alone by the processing of positional
2886 # arguments.
2887 if (not partial and param.kind != _VAR_POSITIONAL and
2888 param.default is _empty):
Yury Selivanov86872752015-05-19 00:27:49 -04002889 raise TypeError('missing a required argument: {arg!r}'. \
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002890 format(arg=param_name)) from None
2891
2892 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05002893 if param.kind == _POSITIONAL_ONLY:
2894 # This should never happen in case of a properly built
2895 # Signature object (but let's have this check here
2896 # to ensure correct behaviour just in case)
2897 raise TypeError('{arg!r} parameter is positional only, '
2898 'but was passed as a keyword'. \
2899 format(arg=param.name))
2900
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002901 arguments[param_name] = arg_val
2902
2903 if kwargs:
2904 if kwargs_param is not None:
2905 # Process our '**kwargs'-like parameter
2906 arguments[kwargs_param.name] = kwargs
2907 else:
Yury Selivanov86872752015-05-19 00:27:49 -04002908 raise TypeError(
2909 'got an unexpected keyword argument {arg!r}'.format(
2910 arg=next(iter(kwargs))))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002911
2912 return self._bound_arguments_cls(self, arguments)
2913
Yury Selivanovc45873e2014-01-29 12:10:27 -05002914 def bind(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002915 """Get a BoundArguments object, that maps the passed `args`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002916 and `kwargs` to the function's signature. Raises `TypeError`
2917 if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002918 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05002919 return args[0]._bind(args[1:], kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002920
Yury Selivanovc45873e2014-01-29 12:10:27 -05002921 def bind_partial(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002922 """Get a BoundArguments object, that partially maps the
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002923 passed `args` and `kwargs` to the function's signature.
2924 Raises `TypeError` if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002925 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05002926 return args[0]._bind(args[1:], kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002927
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002928 def __reduce__(self):
2929 return (type(self),
2930 (tuple(self._parameters.values()),),
2931 {'_return_annotation': self._return_annotation})
2932
2933 def __setstate__(self, state):
2934 self._return_annotation = state['_return_annotation']
2935
Yury Selivanov374375d2014-03-27 12:41:53 -04002936 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002937 return '<{} {}>'.format(self.__class__.__name__, self)
Yury Selivanov374375d2014-03-27 12:41:53 -04002938
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002939 def __str__(self):
2940 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05002941 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002942 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05002943 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002944 formatted = str(param)
2945
2946 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002947
2948 if kind == _POSITIONAL_ONLY:
2949 render_pos_only_separator = True
2950 elif render_pos_only_separator:
2951 # It's not a positional-only parameter, and the flag
2952 # is set to 'True' (there were pos-only params before.)
2953 result.append('/')
2954 render_pos_only_separator = False
2955
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002956 if kind == _VAR_POSITIONAL:
2957 # OK, we have an '*args'-like parameter, so we won't need
2958 # a '*' to separate keyword-only arguments
2959 render_kw_only_separator = False
2960 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
2961 # We have a keyword-only parameter to render and we haven't
2962 # rendered an '*args'-like parameter before, so add a '*'
2963 # separator to the parameters list ("foo(arg1, *, arg2)" case)
2964 result.append('*')
2965 # This condition should be only triggered once, so
2966 # reset the flag
2967 render_kw_only_separator = False
2968
2969 result.append(formatted)
2970
Yury Selivanov2393dca2014-01-27 15:07:58 -05002971 if render_pos_only_separator:
2972 # There were only positional-only parameters, hence the
2973 # flag was not reset to 'False'
2974 result.append('/')
2975
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002976 rendered = '({})'.format(', '.join(result))
2977
2978 if self.return_annotation is not _empty:
2979 anno = formatannotation(self.return_annotation)
2980 rendered += ' -> {}'.format(anno)
2981
2982 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10002983
Yury Selivanovda396452014-03-27 12:09:24 -04002984
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002985def signature(obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002986 """Get a signature object for the passed callable."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002987 return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04002988
2989
Nick Coghlanf94a16b2013-09-22 22:46:49 +10002990def _main():
2991 """ Logic for inspecting an object given at command line """
2992 import argparse
2993 import importlib
2994
2995 parser = argparse.ArgumentParser()
2996 parser.add_argument(
2997 'object',
2998 help="The object to be analysed. "
2999 "It supports the 'module:qualname' syntax")
3000 parser.add_argument(
3001 '-d', '--details', action='store_true',
3002 help='Display info about the module rather than its source code')
3003
3004 args = parser.parse_args()
3005
3006 target = args.object
3007 mod_name, has_attrs, attrs = target.partition(":")
3008 try:
3009 obj = module = importlib.import_module(mod_name)
3010 except Exception as exc:
3011 msg = "Failed to import {} ({}: {})".format(mod_name,
3012 type(exc).__name__,
3013 exc)
3014 print(msg, file=sys.stderr)
3015 exit(2)
3016
3017 if has_attrs:
3018 parts = attrs.split(".")
3019 obj = module
3020 for part in parts:
3021 obj = getattr(obj, part)
3022
3023 if module.__name__ in sys.builtin_module_names:
3024 print("Can't get info for builtin modules.", file=sys.stderr)
3025 exit(1)
3026
3027 if args.details:
3028 print('Target: {}'.format(target))
3029 print('Origin: {}'.format(getsourcefile(module)))
3030 print('Cached: {}'.format(module.__cached__))
3031 if obj is module:
3032 print('Loader: {}'.format(repr(module.__loader__)))
3033 if hasattr(module, '__path__'):
3034 print('Submodule search path: {}'.format(module.__path__))
3035 else:
3036 try:
3037 __, lineno = findsource(obj)
3038 except Exception:
3039 pass
3040 else:
3041 print('Line: {}'.format(lineno))
3042
3043 print('\n')
3044 else:
3045 print(getsource(obj))
3046
3047
3048if __name__ == "__main__":
3049 _main()