blob: 6640375263f8bcac2caaf85e0146223875aa050e [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
Yury Selivanov4778e132016-11-08 12:23:09 -0500182 Coroutine functions are defined with "async def" syntax.
Yury Selivanov75445082015-05-11 22:57:16 -0400183 """
184 return bool((isfunction(object) or ismethod(object)) and
Yury Selivanov5376ba92015-06-22 12:19:30 -0400185 object.__code__.co_flags & CO_COROUTINE)
Yury Selivanov75445082015-05-11 22:57:16 -0400186
Yury Selivanoveb636452016-09-08 22:01:51 -0700187def isasyncgenfunction(object):
Yury Selivanov4778e132016-11-08 12:23:09 -0500188 """Return true if the object is an asynchronous generator function.
189
190 Asynchronous generator functions are defined with "async def"
191 syntax and have "yield" expressions in their body.
192 """
Yury Selivanoveb636452016-09-08 22:01:51 -0700193 return bool((isfunction(object) or ismethod(object)) and
194 object.__code__.co_flags & CO_ASYNC_GENERATOR)
195
196def isasyncgen(object):
Yury Selivanov4778e132016-11-08 12:23:09 -0500197 """Return true if the object is an asynchronous generator."""
Yury Selivanoveb636452016-09-08 22:01:51 -0700198 return isinstance(object, types.AsyncGeneratorType)
199
Christian Heimes7131fd92008-02-19 14:21:46 +0000200def isgenerator(object):
201 """Return true if the object is a generator.
202
203 Generator objects provide these attributes:
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300204 __iter__ defined to support iteration over container
Christian Heimes7131fd92008-02-19 14:21:46 +0000205 close raises a new GeneratorExit exception inside the
206 generator to terminate the iteration
207 gi_code code object
208 gi_frame frame object or possibly None once the generator has
209 been exhausted
210 gi_running set to 1 when generator is executing, 0 otherwise
211 next return the next item from the container
212 send resumes the generator and "sends" a value that becomes
213 the result of the current yield-expression
214 throw used to raise an exception inside the generator"""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400215 return isinstance(object, types.GeneratorType)
Yury Selivanov75445082015-05-11 22:57:16 -0400216
217def iscoroutine(object):
218 """Return true if the object is a coroutine."""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400219 return isinstance(object, types.CoroutineType)
Christian Heimes7131fd92008-02-19 14:21:46 +0000220
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400221def isawaitable(object):
Yury Selivanovc0215df2016-11-08 19:57:44 -0500222 """Return true if object can be passed to an ``await`` expression."""
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400223 return (isinstance(object, types.CoroutineType) or
224 isinstance(object, types.GeneratorType) and
Yury Selivanovc0215df2016-11-08 19:57:44 -0500225 bool(object.gi_code.co_flags & CO_ITERABLE_COROUTINE) or
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400226 isinstance(object, collections.abc.Awaitable))
227
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000228def istraceback(object):
229 """Return true if the object is a traceback.
230
231 Traceback objects provide these attributes:
232 tb_frame frame object at this level
233 tb_lasti index of last attempted instruction in bytecode
234 tb_lineno current line number in Python source code
235 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000236 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000237
238def isframe(object):
239 """Return true if the object is a frame object.
240
241 Frame objects provide these attributes:
242 f_back next outer frame object (this frame's caller)
243 f_builtins built-in namespace seen by this frame
244 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000245 f_globals global namespace seen by this frame
246 f_lasti index of last attempted instruction in bytecode
247 f_lineno current line number in Python source code
248 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000249 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000250 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000251
252def iscode(object):
253 """Return true if the object is a code object.
254
255 Code objects provide these attributes:
256 co_argcount number of arguments (not including * or ** args)
257 co_code string of raw compiled bytecode
258 co_consts tuple of constants used in the bytecode
259 co_filename name of file in which this code object was created
260 co_firstlineno number of first line in Python source code
261 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
262 co_lnotab encoded mapping of line numbers to bytecode indices
263 co_name name with which this code object was defined
264 co_names tuple of names of local variables
265 co_nlocals number of local variables
266 co_stacksize virtual machine stack space required
267 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000268 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000269
270def isbuiltin(object):
271 """Return true if the object is a built-in function or method.
272
273 Built-in functions and methods provide these attributes:
274 __doc__ documentation string
275 __name__ original name of this function or method
276 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000277 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000278
279def isroutine(object):
280 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000281 return (isbuiltin(object)
282 or isfunction(object)
283 or ismethod(object)
284 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000285
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000286def isabstract(object):
287 """Return true if the object is an abstract base class (ABC)."""
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000288 return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT)
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000289
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000290def getmembers(object, predicate=None):
291 """Return all members of an object as (name, value) pairs sorted by name.
292 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100293 if isclass(object):
294 mro = (object,) + getmro(object)
295 else:
296 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000297 results = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700298 processed = set()
299 names = dir(object)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700300 # :dd any DynamicClassAttributes to the list of names if object is a class;
Ethan Furmane03ea372013-09-25 07:14:41 -0700301 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700302 # attribute with the same name as a DynamicClassAttribute exists
Ethan Furmane03ea372013-09-25 07:14:41 -0700303 try:
304 for base in object.__bases__:
305 for k, v in base.__dict__.items():
306 if isinstance(v, types.DynamicClassAttribute):
307 names.append(k)
308 except AttributeError:
309 pass
310 for key in names:
Ethan Furman63c141c2013-10-18 00:27:39 -0700311 # First try to get the value via getattr. Some descriptors don't
312 # like calling their __get__ (see bug #1785), so fall back to
313 # looking in the __dict__.
314 try:
315 value = getattr(object, key)
316 # handle the duplicate key
317 if key in processed:
318 raise AttributeError
319 except AttributeError:
320 for base in mro:
321 if key in base.__dict__:
322 value = base.__dict__[key]
323 break
324 else:
325 # could be a (currently) missing slot member, or a buggy
326 # __dir__; discard and move on
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100327 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000328 if not predicate or predicate(value):
329 results.append((key, value))
Ethan Furmane03ea372013-09-25 07:14:41 -0700330 processed.add(key)
331 results.sort(key=lambda pair: pair[0])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000332 return results
333
Christian Heimes25bb7832008-01-11 16:17:00 +0000334Attribute = namedtuple('Attribute', 'name kind defining_class object')
335
Tim Peters13b49d32001-09-23 02:00:29 +0000336def classify_class_attrs(cls):
337 """Return list of attribute-descriptor tuples.
338
339 For each name in dir(cls), the return list contains a 4-tuple
340 with these elements:
341
342 0. The name (a string).
343
344 1. The kind of attribute this is, one of these strings:
345 'class method' created via classmethod()
346 'static method' created via staticmethod()
347 'property' created via property()
Ethan Furmane03ea372013-09-25 07:14:41 -0700348 'method' any other flavor of method or descriptor
Tim Peters13b49d32001-09-23 02:00:29 +0000349 'data' not a method
350
351 2. The class which defined this attribute (a class).
352
Ethan Furmane03ea372013-09-25 07:14:41 -0700353 3. The object as obtained by calling getattr; if this fails, or if the
354 resulting object does not live anywhere in the class' mro (including
355 metaclasses) then the object is looked up in the defining class's
356 dict (found by walking the mro).
Ethan Furman668dede2013-09-14 18:53:26 -0700357
358 If one of the items in dir(cls) is stored in the metaclass it will now
359 be discovered and not have None be listed as the class in which it was
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700360 defined. Any items whose home class cannot be discovered are skipped.
Tim Peters13b49d32001-09-23 02:00:29 +0000361 """
362
363 mro = getmro(cls)
Ethan Furman668dede2013-09-14 18:53:26 -0700364 metamro = getmro(type(cls)) # for attributes stored in the metaclass
Ethan Furmane03ea372013-09-25 07:14:41 -0700365 metamro = tuple([cls for cls in metamro if cls not in (type, object)])
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700366 class_bases = (cls,) + mro
367 all_bases = class_bases + metamro
Tim Peters13b49d32001-09-23 02:00:29 +0000368 names = dir(cls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700369 # :dd any DynamicClassAttributes to the list of names;
Ethan Furmane03ea372013-09-25 07:14:41 -0700370 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700371 # attribute with the same name as a DynamicClassAttribute exists.
Ethan Furman63c141c2013-10-18 00:27:39 -0700372 for base in mro:
Ethan Furmane03ea372013-09-25 07:14:41 -0700373 for k, v in base.__dict__.items():
374 if isinstance(v, types.DynamicClassAttribute):
375 names.append(k)
Tim Peters13b49d32001-09-23 02:00:29 +0000376 result = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700377 processed = set()
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700378
Tim Peters13b49d32001-09-23 02:00:29 +0000379 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100380 # Get the object associated with the name, and where it was defined.
Ethan Furmane03ea372013-09-25 07:14:41 -0700381 # Normal objects will be looked up with both getattr and directly in
382 # its class' dict (in case getattr fails [bug #1785], and also to look
383 # for a docstring).
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700384 # For DynamicClassAttributes on the second pass we only look in the
Ethan Furmane03ea372013-09-25 07:14:41 -0700385 # class's dict.
386 #
Tim Peters13b49d32001-09-23 02:00:29 +0000387 # Getting an obj from the __dict__ sometimes reveals more than
388 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100389 homecls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700390 get_obj = None
391 dict_obj = None
Ethan Furmane03ea372013-09-25 07:14:41 -0700392 if name not in processed:
393 try:
Ethan Furmana8b07072013-10-18 01:22:08 -0700394 if name == '__dict__':
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700395 raise Exception("__dict__ is special, don't want the proxy")
Ethan Furmane03ea372013-09-25 07:14:41 -0700396 get_obj = getattr(cls, name)
397 except Exception as exc:
398 pass
399 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700400 homecls = getattr(get_obj, "__objclass__", homecls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700401 if homecls not in class_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700402 # if the resulting object does not live somewhere in the
Ethan Furman63c141c2013-10-18 00:27:39 -0700403 # mro, drop it and search the mro manually
Ethan Furmane03ea372013-09-25 07:14:41 -0700404 homecls = None
Ethan Furman63c141c2013-10-18 00:27:39 -0700405 last_cls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700406 # first look in the classes
407 for srch_cls in class_bases:
Ethan Furman63c141c2013-10-18 00:27:39 -0700408 srch_obj = getattr(srch_cls, name, None)
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400409 if srch_obj is get_obj:
Ethan Furman63c141c2013-10-18 00:27:39 -0700410 last_cls = srch_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700411 # then check the metaclasses
412 for srch_cls in metamro:
413 try:
414 srch_obj = srch_cls.__getattr__(cls, name)
415 except AttributeError:
416 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400417 if srch_obj is get_obj:
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700418 last_cls = srch_cls
Ethan Furman63c141c2013-10-18 00:27:39 -0700419 if last_cls is not None:
420 homecls = last_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700421 for base in all_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700422 if name in base.__dict__:
423 dict_obj = base.__dict__[name]
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700424 if homecls not in metamro:
425 homecls = base
Ethan Furmane03ea372013-09-25 07:14:41 -0700426 break
Ethan Furman63c141c2013-10-18 00:27:39 -0700427 if homecls is None:
428 # unable to locate the attribute anywhere, most likely due to
429 # buggy custom __dir__; discard and move on
430 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400431 obj = get_obj if get_obj is not None else dict_obj
Ethan Furmane03ea372013-09-25 07:14:41 -0700432 # Classify the object or its descriptor.
Ethan Furman63c141c2013-10-18 00:27:39 -0700433 if isinstance(dict_obj, staticmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000434 kind = "static method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700435 obj = dict_obj
Ethan Furman63c141c2013-10-18 00:27:39 -0700436 elif isinstance(dict_obj, classmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000437 kind = "class method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700438 obj = dict_obj
439 elif isinstance(dict_obj, property):
Tim Peters13b49d32001-09-23 02:00:29 +0000440 kind = "property"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700441 obj = dict_obj
Yury Selivanov0860a0b2014-01-31 14:28:44 -0500442 elif isroutine(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000443 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100444 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700445 kind = "data"
Christian Heimes25bb7832008-01-11 16:17:00 +0000446 result.append(Attribute(name, kind, homecls, obj))
Ethan Furmane03ea372013-09-25 07:14:41 -0700447 processed.add(name)
Tim Peters13b49d32001-09-23 02:00:29 +0000448 return result
449
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000450# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000451
452def getmro(cls):
453 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000454 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000455
Nick Coghlane8c45d62013-07-28 20:00:01 +1000456# -------------------------------------------------------- function helpers
457
458def unwrap(func, *, stop=None):
459 """Get the object wrapped by *func*.
460
461 Follows the chain of :attr:`__wrapped__` attributes returning the last
462 object in the chain.
463
464 *stop* is an optional callback accepting an object in the wrapper chain
465 as its sole argument that allows the unwrapping to be terminated early if
466 the callback returns a true value. If the callback never returns a true
467 value, the last object in the chain is returned as usual. For example,
468 :func:`signature` uses this to stop unwrapping if any object in the
469 chain has a ``__signature__`` attribute defined.
470
471 :exc:`ValueError` is raised if a cycle is encountered.
472
473 """
474 if stop is None:
475 def _is_wrapper(f):
476 return hasattr(f, '__wrapped__')
477 else:
478 def _is_wrapper(f):
479 return hasattr(f, '__wrapped__') and not stop(f)
480 f = func # remember the original func for error reporting
481 memo = {id(f)} # Memoise by id to tolerate non-hashable objects
482 while _is_wrapper(func):
483 func = func.__wrapped__
484 id_func = id(func)
485 if id_func in memo:
486 raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
487 memo.add(id_func)
488 return func
489
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000490# -------------------------------------------------- source code extraction
491def indentsize(line):
492 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000493 expline = line.expandtabs()
494 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000495
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300496def _findclass(func):
497 cls = sys.modules.get(func.__module__)
498 if cls is None:
499 return None
500 for name in func.__qualname__.split('.')[:-1]:
501 cls = getattr(cls, name)
502 if not isclass(cls):
503 return None
504 return cls
505
506def _finddoc(obj):
507 if isclass(obj):
508 for base in obj.__mro__:
509 if base is not object:
510 try:
511 doc = base.__doc__
512 except AttributeError:
513 continue
514 if doc is not None:
515 return doc
516 return None
517
518 if ismethod(obj):
519 name = obj.__func__.__name__
520 self = obj.__self__
521 if (isclass(self) and
522 getattr(getattr(self, name, None), '__func__') is obj.__func__):
523 # classmethod
524 cls = self
525 else:
526 cls = self.__class__
527 elif isfunction(obj):
528 name = obj.__name__
529 cls = _findclass(obj)
530 if cls is None or getattr(cls, name) is not obj:
531 return None
532 elif isbuiltin(obj):
533 name = obj.__name__
534 self = obj.__self__
535 if (isclass(self) and
536 self.__qualname__ + '.' + name == obj.__qualname__):
537 # classmethod
538 cls = self
539 else:
540 cls = self.__class__
Serhiy Storchakaac4bdcc2015-10-29 08:15:50 +0200541 # Should be tested before isdatadescriptor().
542 elif isinstance(obj, property):
543 func = obj.fget
544 name = func.__name__
545 cls = _findclass(func)
546 if cls is None or getattr(cls, name) is not obj:
547 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300548 elif ismethoddescriptor(obj) or isdatadescriptor(obj):
549 name = obj.__name__
550 cls = obj.__objclass__
551 if getattr(cls, name) is not obj:
552 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300553 else:
554 return None
555
556 for base in cls.__mro__:
557 try:
558 doc = getattr(base, name).__doc__
559 except AttributeError:
560 continue
561 if doc is not None:
562 return doc
563 return None
564
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000565def getdoc(object):
566 """Get the documentation string for an object.
567
568 All tabs are expanded to spaces. To clean up docstrings that are
569 indented to line up with blocks of code, any whitespace than can be
570 uniformly removed from the second line onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000571 try:
572 doc = object.__doc__
573 except AttributeError:
574 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300575 if doc is None:
576 try:
577 doc = _finddoc(object)
578 except (AttributeError, TypeError):
579 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000580 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000581 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000582 return cleandoc(doc)
583
584def cleandoc(doc):
585 """Clean up indentation from docstrings.
586
587 Any whitespace that can be uniformly removed from the second line
588 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000589 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000590 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000591 except UnicodeError:
592 return None
593 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000594 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000595 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000596 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000597 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000598 if content:
599 indent = len(line) - content
600 margin = min(margin, indent)
601 # Remove indentation.
602 if lines:
603 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000604 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000605 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000606 # Remove any trailing or leading blank lines.
607 while lines and not lines[-1]:
608 lines.pop()
609 while lines and not lines[0]:
610 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000611 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000612
613def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000614 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000615 if ismodule(object):
616 if hasattr(object, '__file__'):
617 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000618 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000619 if isclass(object):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500620 if hasattr(object, '__module__'):
621 object = sys.modules.get(object.__module__)
622 if hasattr(object, '__file__'):
623 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000624 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000625 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000626 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000627 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000628 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000629 if istraceback(object):
630 object = object.tb_frame
631 if isframe(object):
632 object = object.f_code
633 if iscode(object):
634 return object.co_filename
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000635 raise TypeError('{!r} is not a module, class, method, '
636 'function, traceback, frame, or code object'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000637
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000638def getmodulename(path):
639 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000640 fname = os.path.basename(path)
641 # Check for paths that look like an actual module file
642 suffixes = [(-len(suffix), suffix)
643 for suffix in importlib.machinery.all_suffixes()]
644 suffixes.sort() # try longest suffixes first, in case they overlap
645 for neglen, suffix in suffixes:
646 if fname.endswith(suffix):
647 return fname[:neglen]
648 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000649
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000650def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000651 """Return the filename that can be used to locate an object's source.
652 Return None if no way can be identified to get the source.
653 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000654 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400655 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
656 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
657 if any(filename.endswith(s) for s in all_bytecode_suffixes):
658 filename = (os.path.splitext(filename)[0] +
659 importlib.machinery.SOURCE_SUFFIXES[0])
660 elif any(filename.endswith(s) for s in
661 importlib.machinery.EXTENSION_SUFFIXES):
662 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000663 if os.path.exists(filename):
664 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000665 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400666 if getattr(getmodule(object, filename), '__loader__', None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000667 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000668 # or it is in the linecache
669 if filename in linecache.cache:
670 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000671
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000672def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000673 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000674
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000675 The idea is for each object to have a unique origin, so this routine
676 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000677 if _filename is None:
678 _filename = getsourcefile(object) or getfile(object)
679 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000680
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000681modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000682_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000683
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000684def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000685 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000686 if ismodule(object):
687 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000688 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000689 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000690 # Try the filename to modulename cache
691 if _filename is not None and _filename in modulesbyfile:
692 return sys.modules.get(modulesbyfile[_filename])
693 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000694 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000695 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000696 except TypeError:
697 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000698 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000699 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000700 # Update the filename to module name cache and check yet again
701 # Copy sys.modules in order to cope with changes while iterating
Éric Araujoa74f8ef2011-11-29 16:58:53 +0100702 for modname, module in list(sys.modules.items()):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000703 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000704 f = module.__file__
705 if f == _filesbymodname.get(modname, None):
706 # Have already mapped this module, so skip it
707 continue
708 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000709 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000710 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000711 modulesbyfile[f] = modulesbyfile[
712 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000713 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000714 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000715 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000716 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000717 if not hasattr(object, '__name__'):
718 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000719 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000720 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000721 if mainobject is object:
722 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000723 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000724 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000725 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000726 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000727 if builtinobject is object:
728 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000729
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000730def findsource(object):
731 """Return the entire source file and starting line number for an object.
732
733 The argument may be a module, class, method, function, traceback, frame,
734 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200735 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000736 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500737
Yury Selivanovef1e7502014-12-08 16:05:34 -0500738 file = getsourcefile(object)
739 if file:
740 # Invalidate cache if needed.
741 linecache.checkcache(file)
742 else:
743 file = getfile(object)
744 # Allow filenames in form of "<something>" to pass through.
745 # `doctest` monkeypatches `linecache` module to enable
746 # inspection, so let `linecache.getlines` to be called.
747 if not (file.startswith('<') and file.endswith('>')):
748 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500749
Thomas Wouters89f507f2006-12-13 04:49:30 +0000750 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000751 if module:
752 lines = linecache.getlines(file, module.__dict__)
753 else:
754 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000755 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200756 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000757
758 if ismodule(object):
759 return lines, 0
760
761 if isclass(object):
762 name = object.__name__
Thomas Wouters89f507f2006-12-13 04:49:30 +0000763 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
764 # make some effort to find the best matching class definition:
765 # use the one with the least indentation, which is the one
766 # that's most probably not inside a function definition.
767 candidates = []
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000768 for i in range(len(lines)):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000769 match = pat.match(lines[i])
770 if match:
771 # if it's at toplevel, it's already the best one
772 if lines[i][0] == 'c':
773 return lines, i
774 # else add whitespace to candidate list
775 candidates.append((match.group(1), i))
776 if candidates:
777 # this will sort by whitespace, and by line number,
778 # less whitespace first
779 candidates.sort()
780 return lines, candidates[0][1]
Jeremy Hyltonab919022003-06-27 18:41:20 +0000781 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200782 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000783
784 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000785 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000786 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000787 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000788 if istraceback(object):
789 object = object.tb_frame
790 if isframe(object):
791 object = object.f_code
792 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000793 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200794 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000795 lnum = object.co_firstlineno - 1
Yury Selivanove4e811d2015-07-21 19:01:52 +0300796 pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000797 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000798 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000799 lnum = lnum - 1
800 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200801 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000802
803def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000804 """Get lines of comments immediately preceding an object's source code.
805
806 Returns None when source can't be found.
807 """
808 try:
809 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200810 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000811 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000812
813 if ismodule(object):
814 # Look for a comment block at the top of the file.
815 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000816 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000817 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000818 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000819 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000820 comments = []
821 end = start
822 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000823 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000824 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000825 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000826
827 # Look for a preceding block of comments at the same indentation.
828 elif lnum > 0:
829 indent = indentsize(lines[lnum])
830 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000831 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000832 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000833 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000834 if end > 0:
835 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000836 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000837 while comment[:1] == '#' and indentsize(lines[end]) == indent:
838 comments[:0] = [comment]
839 end = end - 1
840 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000841 comment = lines[end].expandtabs().lstrip()
842 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000843 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000844 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000845 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000846 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000847
Tim Peters4efb6e92001-06-29 23:51:08 +0000848class EndOfBlock(Exception): pass
849
850class BlockFinder:
851 """Provide a tokeneater() method to detect the end of a code block."""
852 def __init__(self):
853 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000854 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000855 self.started = False
856 self.passline = False
Meador Inge5b718d72015-07-23 22:49:37 -0500857 self.indecorator = False
858 self.decoratorhasargs = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000859 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000860
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000861 def tokeneater(self, type, token, srowcol, erowcol, line):
Meador Inge5b718d72015-07-23 22:49:37 -0500862 if not self.started and not self.indecorator:
863 # skip any decorators
864 if token == "@":
865 self.indecorator = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000866 # look for the first "def", "class" or "lambda"
Meador Inge5b718d72015-07-23 22:49:37 -0500867 elif token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000868 if token == "lambda":
869 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000870 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000871 self.passline = True # skip to the end of the line
Meador Inge5b718d72015-07-23 22:49:37 -0500872 elif token == "(":
873 if self.indecorator:
874 self.decoratorhasargs = True
875 elif token == ")":
876 if self.indecorator:
877 self.indecorator = False
878 self.decoratorhasargs = False
Tim Peters4efb6e92001-06-29 23:51:08 +0000879 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000880 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000881 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000882 if self.islambda: # lambdas always end at the first NEWLINE
883 raise EndOfBlock
Meador Inge5b718d72015-07-23 22:49:37 -0500884 # hitting a NEWLINE when in a decorator without args
885 # ends the decorator
886 if self.indecorator and not self.decoratorhasargs:
887 self.indecorator = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000888 elif self.passline:
889 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000890 elif type == tokenize.INDENT:
891 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000892 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000893 elif type == tokenize.DEDENT:
894 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000895 # the end of matching indent/dedent pairs end a block
896 # (note that this only works for "def"/"class" blocks,
897 # not e.g. for "if: else:" or "try: finally:" blocks)
898 if self.indent <= 0:
899 raise EndOfBlock
900 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
901 # any other token on the same indentation level end the previous
902 # block as well, except the pseudo-tokens COMMENT and NL.
903 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000904
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000905def getblock(lines):
906 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000907 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000908 try:
Trent Nelson428de652008-03-18 22:41:35 +0000909 tokens = tokenize.generate_tokens(iter(lines).__next__)
910 for _token in tokens:
911 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000912 except (EndOfBlock, IndentationError):
913 pass
914 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000915
916def getsourcelines(object):
917 """Return a list of source lines and starting line number for an object.
918
919 The argument may be a module, class, method, function, traceback, frame,
920 or code object. The source code is returned as a list of the lines
921 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200922 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000923 raised if the source code cannot be retrieved."""
Yury Selivanov081bbf62014-09-26 17:34:54 -0400924 object = unwrap(object)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000925 lines, lnum = findsource(object)
926
Meador Inge5b718d72015-07-23 22:49:37 -0500927 if ismodule(object):
928 return lines, 0
929 else:
930 return getblock(lines[lnum:]), lnum + 1
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000931
932def getsource(object):
933 """Return the text of the source code for an object.
934
935 The argument may be a module, class, method, function, traceback, frame,
936 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200937 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000938 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000939 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000940
941# --------------------------------------------------- class tree extraction
942def walktree(classes, children, parent):
943 """Recursive helper function for getclasstree()."""
944 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +0000945 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000946 for c in classes:
947 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +0000948 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000949 results.append(walktree(children[c], children, c))
950 return results
951
Georg Brandl5ce83a02009-06-01 17:23:51 +0000952def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000953 """Arrange the given list of classes into a hierarchy of nested lists.
954
955 Where a nested list appears, it contains classes derived from the class
956 whose entry immediately precedes the list. Each entry is a 2-tuple
957 containing a class and a tuple of its base classes. If the 'unique'
958 argument is true, exactly one entry appears in the returned structure
959 for each class in the given list. Otherwise, classes using multiple
960 inheritance and their descendants will appear multiple times."""
961 children = {}
962 roots = []
963 for c in classes:
964 if c.__bases__:
965 for parent in c.__bases__:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000966 if not parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000967 children[parent] = []
Serhiy Storchaka362c1b52013-09-05 17:14:32 +0300968 if c not in children[parent]:
969 children[parent].append(c)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000970 if unique and parent in classes: break
971 elif c not in roots:
972 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +0000973 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000974 if parent not in classes:
975 roots.append(parent)
976 return walktree(roots, children, None)
977
978# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +0000979Arguments = namedtuple('Arguments', 'args, varargs, varkw')
980
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000981def getargs(co):
982 """Get information about the arguments accepted by a code object.
983
Guido van Rossum2e65f892007-02-28 22:03:49 +0000984 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000985 'args' is the list of argument names. Keyword-only arguments are
986 appended. 'varargs' and 'varkw' are the names of the * and **
987 arguments or None."""
Guido van Rossum2e65f892007-02-28 22:03:49 +0000988 args, varargs, kwonlyargs, varkw = _getfullargs(co)
Christian Heimes25bb7832008-01-11 16:17:00 +0000989 return Arguments(args + kwonlyargs, varargs, varkw)
Guido van Rossum2e65f892007-02-28 22:03:49 +0000990
991def _getfullargs(co):
992 """Get information about the arguments accepted by a code object.
993
994 Four things are returned: (args, varargs, kwonlyargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000995 'args' and 'kwonlyargs' are lists of argument names, and 'varargs'
996 and 'varkw' are the names of the * and ** arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +0000997
998 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000999 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001000
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001001 nargs = co.co_argcount
1002 names = co.co_varnames
Guido van Rossum2e65f892007-02-28 22:03:49 +00001003 nkwargs = co.co_kwonlyargcount
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001004 args = list(names[:nargs])
Guido van Rossum2e65f892007-02-28 22:03:49 +00001005 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001006 step = 0
1007
Guido van Rossum2e65f892007-02-28 22:03:49 +00001008 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001009 varargs = None
1010 if co.co_flags & CO_VARARGS:
1011 varargs = co.co_varnames[nargs]
1012 nargs = nargs + 1
1013 varkw = None
1014 if co.co_flags & CO_VARKEYWORDS:
1015 varkw = co.co_varnames[nargs]
Guido van Rossum2e65f892007-02-28 22:03:49 +00001016 return args, varargs, kwonlyargs, varkw
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001017
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001018
1019ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
1020
1021def getargspec(func):
1022 """Get the names and default values of a function's arguments.
1023
1024 A tuple of four things is returned: (args, varargs, keywords, defaults).
1025 'args' is a list of the argument names, including keyword-only argument names.
1026 'varargs' and 'keywords' are the names of the * and ** arguments or None.
1027 'defaults' is an n-tuple of the default values of the last n arguments.
1028
1029 Use the getfullargspec() API for Python 3 code, as annotations
1030 and keyword arguments are supported. getargspec() will raise ValueError
1031 if the func has either annotations or keyword arguments.
1032 """
1033 warnings.warn("inspect.getargspec() is deprecated, "
1034 "use inspect.signature() instead", DeprecationWarning,
1035 stacklevel=2)
1036 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
1037 getfullargspec(func)
1038 if kwonlyargs or ann:
1039 raise ValueError("Function has keyword-only arguments or annotations"
1040 ", use getfullargspec() API which can support them")
1041 return ArgSpec(args, varargs, varkw, defaults)
1042
Christian Heimes25bb7832008-01-11 16:17:00 +00001043FullArgSpec = namedtuple('FullArgSpec',
Benjamin Peterson3d4ca742008-11-12 21:39:01 +00001044 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001045
1046def getfullargspec(func):
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001047 """Get the names and default values of a callable object's arguments.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001048
Brett Cannon504d8852007-09-07 02:12:14 +00001049 A tuple of seven things is returned:
1050 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults annotations).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001051 'args' is a list of the argument names.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001052 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1053 'defaults' is an n-tuple of the default values of the last n arguments.
1054 'kwonlyargs' is a list of keyword-only argument names.
1055 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
1056 'annotations' is a dictionary mapping argument names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001057
Yury Selivanov3cfec2e2015-05-22 11:38:38 -04001058 This function is deprecated, use inspect.signature() instead.
Jeremy Hylton64967882003-06-27 18:14:39 +00001059 """
1060
Yury Selivanov57d240e2014-02-19 16:27:23 -05001061 try:
1062 # Re: `skip_bound_arg=False`
1063 #
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001064 # There is a notable difference in behaviour between getfullargspec
1065 # and Signature: the former always returns 'self' parameter for bound
1066 # methods, whereas the Signature always shows the actual calling
1067 # signature of the passed object.
1068 #
1069 # To simulate this behaviour, we "unbind" bound methods, to trick
1070 # inspect.signature to always return their first parameter ("self",
1071 # usually)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001072
Yury Selivanov57d240e2014-02-19 16:27:23 -05001073 # Re: `follow_wrapper_chains=False`
1074 #
1075 # getfullargspec() historically ignored __wrapped__ attributes,
1076 # so we ensure that remains the case in 3.3+
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001077
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001078 sig = _signature_from_callable(func,
1079 follow_wrapper_chains=False,
1080 skip_bound_arg=False,
1081 sigcls=Signature)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001082 except Exception as ex:
1083 # Most of the times 'signature' will raise ValueError.
1084 # But, it can also raise AttributeError, and, maybe something
1085 # else. So to be fully backwards compatible, we catch all
1086 # possible exceptions here, and reraise a TypeError.
1087 raise TypeError('unsupported callable') from ex
1088
1089 args = []
1090 varargs = None
1091 varkw = None
1092 kwonlyargs = []
1093 defaults = ()
1094 annotations = {}
1095 defaults = ()
1096 kwdefaults = {}
1097
1098 if sig.return_annotation is not sig.empty:
1099 annotations['return'] = sig.return_annotation
1100
1101 for param in sig.parameters.values():
1102 kind = param.kind
1103 name = param.name
1104
1105 if kind is _POSITIONAL_ONLY:
1106 args.append(name)
1107 elif kind is _POSITIONAL_OR_KEYWORD:
1108 args.append(name)
1109 if param.default is not param.empty:
1110 defaults += (param.default,)
1111 elif kind is _VAR_POSITIONAL:
1112 varargs = name
1113 elif kind is _KEYWORD_ONLY:
1114 kwonlyargs.append(name)
1115 if param.default is not param.empty:
1116 kwdefaults[name] = param.default
1117 elif kind is _VAR_KEYWORD:
1118 varkw = name
1119
1120 if param.annotation is not param.empty:
1121 annotations[name] = param.annotation
1122
1123 if not kwdefaults:
1124 # compatibility with 'func.__kwdefaults__'
1125 kwdefaults = None
1126
1127 if not defaults:
1128 # compatibility with 'func.__defaults__'
1129 defaults = None
1130
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001131 return FullArgSpec(args, varargs, varkw, defaults,
1132 kwonlyargs, kwdefaults, annotations)
1133
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001134
Christian Heimes25bb7832008-01-11 16:17:00 +00001135ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
1136
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001137def getargvalues(frame):
1138 """Get information about arguments passed into a particular frame.
1139
1140 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001141 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001142 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1143 'locals' is the locals dictionary of the given frame."""
1144 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +00001145 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001146
Guido van Rossum2e65f892007-02-28 22:03:49 +00001147def formatannotation(annotation, base_module=None):
Guido van Rossum52e50042016-10-22 07:55:18 -07001148 if getattr(annotation, '__module__', None) == 'typing':
1149 return repr(annotation).replace('typing.', '')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001150 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001151 if annotation.__module__ in ('builtins', base_module):
Serhiy Storchaka521e5862014-07-22 15:00:37 +03001152 return annotation.__qualname__
1153 return annotation.__module__+'.'+annotation.__qualname__
Guido van Rossum2e65f892007-02-28 22:03:49 +00001154 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001155
Guido van Rossum2e65f892007-02-28 22:03:49 +00001156def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001157 module = getattr(object, '__module__', None)
1158 def _formatannotation(annotation):
1159 return formatannotation(annotation, module)
1160 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001161
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001162def formatargspec(args, varargs=None, varkw=None, defaults=None,
Guido van Rossum2e65f892007-02-28 22:03:49 +00001163 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001164 formatarg=str,
1165 formatvarargs=lambda name: '*' + name,
1166 formatvarkw=lambda name: '**' + name,
1167 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001168 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001169 formatannotation=formatannotation):
Berker Peksagfa3922c2015-07-31 04:11:29 +03001170 """Format an argument spec from the values returned by getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001171
Guido van Rossum2e65f892007-02-28 22:03:49 +00001172 The first seven arguments are (args, varargs, varkw, defaults,
1173 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1174 are the corresponding optional formatting functions that are called to
1175 turn names and values into strings. The last argument is an optional
1176 function to format the sequence of arguments."""
1177 def formatargandannotation(arg):
1178 result = formatarg(arg)
1179 if arg in annotations:
1180 result += ': ' + formatannotation(annotations[arg])
1181 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001182 specs = []
1183 if defaults:
1184 firstdefault = len(args) - len(defaults)
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001185 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001186 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001187 if defaults and i >= firstdefault:
1188 spec = spec + formatvalue(defaults[i - firstdefault])
1189 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001190 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001191 specs.append(formatvarargs(formatargandannotation(varargs)))
1192 else:
1193 if kwonlyargs:
1194 specs.append('*')
1195 if kwonlyargs:
1196 for kwonlyarg in kwonlyargs:
1197 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001198 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001199 spec += formatvalue(kwonlydefaults[kwonlyarg])
1200 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001201 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001202 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001203 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001204 if 'return' in annotations:
1205 result += formatreturns(formatannotation(annotations['return']))
1206 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001207
1208def formatargvalues(args, varargs, varkw, locals,
1209 formatarg=str,
1210 formatvarargs=lambda name: '*' + name,
1211 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001212 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001213 """Format an argument spec from the 4 values returned by getargvalues.
1214
1215 The first four arguments are (args, varargs, varkw, locals). The
1216 next four arguments are the corresponding optional formatting functions
1217 that are called to turn names and values into strings. The ninth
1218 argument is an optional function to format the sequence of arguments."""
1219 def convert(name, locals=locals,
1220 formatarg=formatarg, formatvalue=formatvalue):
1221 return formatarg(name) + formatvalue(locals[name])
1222 specs = []
1223 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001224 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001225 if varargs:
1226 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1227 if varkw:
1228 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001229 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001230
Benjamin Petersone109c702011-06-24 09:37:26 -05001231def _missing_arguments(f_name, argnames, pos, values):
1232 names = [repr(name) for name in argnames if name not in values]
1233 missing = len(names)
1234 if missing == 1:
1235 s = names[0]
1236 elif missing == 2:
1237 s = "{} and {}".format(*names)
1238 else:
Yury Selivanovdccfa132014-03-27 18:42:52 -04001239 tail = ", {} and {}".format(*names[-2:])
Benjamin Petersone109c702011-06-24 09:37:26 -05001240 del names[-2:]
1241 s = ", ".join(names) + tail
1242 raise TypeError("%s() missing %i required %s argument%s: %s" %
1243 (f_name, missing,
1244 "positional" if pos else "keyword-only",
1245 "" if missing == 1 else "s", s))
1246
1247def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001248 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001249 kwonly_given = len([arg for arg in kwonly if arg in values])
1250 if varargs:
1251 plural = atleast != 1
1252 sig = "at least %d" % (atleast,)
1253 elif defcount:
1254 plural = True
1255 sig = "from %d to %d" % (atleast, len(args))
1256 else:
1257 plural = len(args) != 1
1258 sig = str(len(args))
1259 kwonly_sig = ""
1260 if kwonly_given:
1261 msg = " positional argument%s (and %d keyword-only argument%s)"
1262 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1263 "s" if kwonly_given != 1 else ""))
1264 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1265 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1266 "was" if given == 1 and not kwonly_given else "were"))
1267
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001268def getcallargs(*func_and_positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001269 """Get the mapping of arguments to values.
1270
1271 A dict is returned, with keys the function argument names (including the
1272 names of the * and ** arguments, if any), and values the respective bound
1273 values from 'positional' and 'named'."""
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001274 func = func_and_positional[0]
1275 positional = func_and_positional[1:]
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001276 spec = getfullargspec(func)
1277 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
1278 f_name = func.__name__
1279 arg2value = {}
1280
Benjamin Petersonb204a422011-06-05 22:04:07 -05001281
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001282 if ismethod(func) and func.__self__ is not None:
1283 # implicit 'self' (or 'cls' for classmethods) argument
1284 positional = (func.__self__,) + positional
1285 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001286 num_args = len(args)
1287 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001288
Benjamin Petersonb204a422011-06-05 22:04:07 -05001289 n = min(num_pos, num_args)
1290 for i in range(n):
1291 arg2value[args[i]] = positional[i]
1292 if varargs:
1293 arg2value[varargs] = tuple(positional[n:])
1294 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001295 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001296 arg2value[varkw] = {}
1297 for kw, value in named.items():
1298 if kw not in possible_kwargs:
1299 if not varkw:
1300 raise TypeError("%s() got an unexpected keyword argument %r" %
1301 (f_name, kw))
1302 arg2value[varkw][kw] = value
1303 continue
1304 if kw in arg2value:
1305 raise TypeError("%s() got multiple values for argument %r" %
1306 (f_name, kw))
1307 arg2value[kw] = value
1308 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001309 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1310 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001311 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001312 req = args[:num_args - num_defaults]
1313 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001314 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001315 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001316 for i, arg in enumerate(args[num_args - num_defaults:]):
1317 if arg not in arg2value:
1318 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001319 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001320 for kwarg in kwonlyargs:
1321 if kwarg not in arg2value:
Yury Selivanov875df202014-03-27 18:23:03 -04001322 if kwonlydefaults and kwarg in kwonlydefaults:
Benjamin Petersone109c702011-06-24 09:37:26 -05001323 arg2value[kwarg] = kwonlydefaults[kwarg]
1324 else:
1325 missing += 1
1326 if missing:
1327 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001328 return arg2value
1329
Nick Coghlan2f92e542012-06-23 19:39:55 +10001330ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1331
1332def getclosurevars(func):
1333 """
1334 Get the mapping of free variables to their current values.
1335
Meador Inge8fda3592012-07-19 21:33:21 -05001336 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001337 and builtin references as seen by the body of the function. A final
1338 set of unbound names that could not be resolved is also provided.
1339 """
1340
1341 if ismethod(func):
1342 func = func.__func__
1343
1344 if not isfunction(func):
1345 raise TypeError("'{!r}' is not a Python function".format(func))
1346
1347 code = func.__code__
1348 # Nonlocal references are named in co_freevars and resolved
1349 # by looking them up in __closure__ by positional index
1350 if func.__closure__ is None:
1351 nonlocal_vars = {}
1352 else:
1353 nonlocal_vars = {
1354 var : cell.cell_contents
1355 for var, cell in zip(code.co_freevars, func.__closure__)
1356 }
1357
1358 # Global and builtin references are named in co_names and resolved
1359 # by looking them up in __globals__ or __builtins__
1360 global_ns = func.__globals__
1361 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1362 if ismodule(builtin_ns):
1363 builtin_ns = builtin_ns.__dict__
1364 global_vars = {}
1365 builtin_vars = {}
1366 unbound_names = set()
1367 for name in code.co_names:
1368 if name in ("None", "True", "False"):
1369 # Because these used to be builtins instead of keywords, they
1370 # may still show up as name references. We ignore them.
1371 continue
1372 try:
1373 global_vars[name] = global_ns[name]
1374 except KeyError:
1375 try:
1376 builtin_vars[name] = builtin_ns[name]
1377 except KeyError:
1378 unbound_names.add(name)
1379
1380 return ClosureVars(nonlocal_vars, global_vars,
1381 builtin_vars, unbound_names)
1382
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001383# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001384
1385Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1386
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001387def getframeinfo(frame, context=1):
1388 """Get information about a frame or traceback object.
1389
1390 A tuple of five things is returned: the filename, the line number of
1391 the current line, the function name, a list of lines of context from
1392 the source code, and the index of the current line within that list.
1393 The optional second argument specifies the number of lines of context
1394 to return, which are centered around the current line."""
1395 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001396 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001397 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001398 else:
1399 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001400 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001401 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001402
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001403 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001404 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001405 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001406 try:
1407 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001408 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001409 lines = index = None
1410 else:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001411 start = max(start, 1)
Raymond Hettingera0501712004-06-15 11:22:53 +00001412 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001413 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001414 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001415 else:
1416 lines = index = None
1417
Christian Heimes25bb7832008-01-11 16:17:00 +00001418 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001419
1420def getlineno(frame):
1421 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001422 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1423 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001424
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001425FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
1426
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001427def getouterframes(frame, context=1):
1428 """Get a list of records for a frame and all higher (calling) frames.
1429
1430 Each record contains a frame object, filename, line number, function
1431 name, a list of lines of context, and index within the context."""
1432 framelist = []
1433 while frame:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001434 frameinfo = (frame,) + getframeinfo(frame, context)
1435 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001436 frame = frame.f_back
1437 return framelist
1438
1439def getinnerframes(tb, context=1):
1440 """Get a list of records for a traceback's frame and all lower frames.
1441
1442 Each record contains a frame object, filename, line number, function
1443 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001444 framelist = []
1445 while tb:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001446 frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
1447 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001448 tb = tb.tb_next
1449 return framelist
1450
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001451def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001452 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001453 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001454
1455def stack(context=1):
1456 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001457 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001458
1459def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001460 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001461 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001462
1463
1464# ------------------------------------------------ static version of getattr
1465
1466_sentinel = object()
1467
Michael Foorde5162652010-11-20 16:40:44 +00001468def _static_getmro(klass):
1469 return type.__dict__['__mro__'].__get__(klass)
1470
Michael Foord95fc51d2010-11-20 15:07:30 +00001471def _check_instance(obj, attr):
1472 instance_dict = {}
1473 try:
1474 instance_dict = object.__getattribute__(obj, "__dict__")
1475 except AttributeError:
1476 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001477 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001478
1479
1480def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001481 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001482 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001483 try:
1484 return entry.__dict__[attr]
1485 except KeyError:
1486 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001487 return _sentinel
1488
Michael Foord35184ed2010-11-20 16:58:30 +00001489def _is_type(obj):
1490 try:
1491 _static_getmro(obj)
1492 except TypeError:
1493 return False
1494 return True
1495
Michael Foorddcebe0f2011-03-15 19:20:44 -04001496def _shadowed_dict(klass):
1497 dict_attr = type.__dict__["__dict__"]
1498 for entry in _static_getmro(klass):
1499 try:
1500 class_dict = dict_attr.__get__(entry)["__dict__"]
1501 except KeyError:
1502 pass
1503 else:
1504 if not (type(class_dict) is types.GetSetDescriptorType and
1505 class_dict.__name__ == "__dict__" and
1506 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001507 return class_dict
1508 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001509
1510def getattr_static(obj, attr, default=_sentinel):
1511 """Retrieve attributes without triggering dynamic lookup via the
1512 descriptor protocol, __getattr__ or __getattribute__.
1513
1514 Note: this function may not be able to retrieve all attributes
1515 that getattr can fetch (like dynamically created attributes)
1516 and may find attributes that getattr can't (like descriptors
1517 that raise AttributeError). It can also return descriptor objects
1518 instead of instance members in some cases. See the
1519 documentation for details.
1520 """
1521 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001522 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001523 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001524 dict_attr = _shadowed_dict(klass)
1525 if (dict_attr is _sentinel or
1526 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001527 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001528 else:
1529 klass = obj
1530
1531 klass_result = _check_class(klass, attr)
1532
1533 if instance_result is not _sentinel and klass_result is not _sentinel:
1534 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1535 _check_class(type(klass_result), '__set__') is not _sentinel):
1536 return klass_result
1537
1538 if instance_result is not _sentinel:
1539 return instance_result
1540 if klass_result is not _sentinel:
1541 return klass_result
1542
1543 if obj is klass:
1544 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001545 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001546 if _shadowed_dict(type(entry)) is _sentinel:
1547 try:
1548 return entry.__dict__[attr]
1549 except KeyError:
1550 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001551 if default is not _sentinel:
1552 return default
1553 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001554
1555
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001556# ------------------------------------------------ generator introspection
1557
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001558GEN_CREATED = 'GEN_CREATED'
1559GEN_RUNNING = 'GEN_RUNNING'
1560GEN_SUSPENDED = 'GEN_SUSPENDED'
1561GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001562
1563def getgeneratorstate(generator):
1564 """Get current state of a generator-iterator.
1565
1566 Possible states are:
1567 GEN_CREATED: Waiting to start execution.
1568 GEN_RUNNING: Currently being executed by the interpreter.
1569 GEN_SUSPENDED: Currently suspended at a yield expression.
1570 GEN_CLOSED: Execution has completed.
1571 """
1572 if generator.gi_running:
1573 return GEN_RUNNING
1574 if generator.gi_frame is None:
1575 return GEN_CLOSED
1576 if generator.gi_frame.f_lasti == -1:
1577 return GEN_CREATED
1578 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001579
1580
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001581def getgeneratorlocals(generator):
1582 """
1583 Get the mapping of generator local variables to their current values.
1584
1585 A dict is returned, with the keys the local variable names and values the
1586 bound values."""
1587
1588 if not isgenerator(generator):
1589 raise TypeError("'{!r}' is not a Python generator".format(generator))
1590
1591 frame = getattr(generator, "gi_frame", None)
1592 if frame is not None:
1593 return generator.gi_frame.f_locals
1594 else:
1595 return {}
1596
Yury Selivanov5376ba92015-06-22 12:19:30 -04001597
1598# ------------------------------------------------ coroutine introspection
1599
1600CORO_CREATED = 'CORO_CREATED'
1601CORO_RUNNING = 'CORO_RUNNING'
1602CORO_SUSPENDED = 'CORO_SUSPENDED'
1603CORO_CLOSED = 'CORO_CLOSED'
1604
1605def getcoroutinestate(coroutine):
1606 """Get current state of a coroutine object.
1607
1608 Possible states are:
1609 CORO_CREATED: Waiting to start execution.
1610 CORO_RUNNING: Currently being executed by the interpreter.
1611 CORO_SUSPENDED: Currently suspended at an await expression.
1612 CORO_CLOSED: Execution has completed.
1613 """
1614 if coroutine.cr_running:
1615 return CORO_RUNNING
1616 if coroutine.cr_frame is None:
1617 return CORO_CLOSED
1618 if coroutine.cr_frame.f_lasti == -1:
1619 return CORO_CREATED
1620 return CORO_SUSPENDED
1621
1622
1623def getcoroutinelocals(coroutine):
1624 """
1625 Get the mapping of coroutine local variables to their current values.
1626
1627 A dict is returned, with the keys the local variable names and values the
1628 bound values."""
1629 frame = getattr(coroutine, "cr_frame", None)
1630 if frame is not None:
1631 return frame.f_locals
1632 else:
1633 return {}
1634
1635
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001636###############################################################################
1637### Function Signature Object (PEP 362)
1638###############################################################################
1639
1640
1641_WrapperDescriptor = type(type.__call__)
1642_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001643_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001644
1645_NonUserDefinedCallables = (_WrapperDescriptor,
1646 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001647 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001648 types.BuiltinFunctionType)
1649
1650
Yury Selivanov421f0c72014-01-29 12:05:40 -05001651def _signature_get_user_defined_method(cls, method_name):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001652 """Private helper. Checks if ``cls`` has an attribute
1653 named ``method_name`` and returns it only if it is a
1654 pure python function.
1655 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001656 try:
1657 meth = getattr(cls, method_name)
1658 except AttributeError:
1659 return
1660 else:
1661 if not isinstance(meth, _NonUserDefinedCallables):
1662 # Once '__signature__' will be added to 'C'-level
1663 # callables, this check won't be necessary
1664 return meth
1665
1666
Yury Selivanov62560fb2014-01-28 12:26:24 -05001667def _signature_get_partial(wrapped_sig, partial, extra_args=()):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001668 """Private helper to calculate how 'wrapped_sig' signature will
1669 look like after applying a 'functools.partial' object (or alike)
1670 on it.
1671 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001672
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001673 old_params = wrapped_sig.parameters
1674 new_params = OrderedDict(old_params.items())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001675
1676 partial_args = partial.args or ()
1677 partial_keywords = partial.keywords or {}
1678
1679 if extra_args:
1680 partial_args = extra_args + partial_args
1681
1682 try:
1683 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1684 except TypeError as ex:
1685 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1686 raise ValueError(msg) from ex
1687
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001688
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001689 transform_to_kwonly = False
1690 for param_name, param in old_params.items():
1691 try:
1692 arg_value = ba.arguments[param_name]
1693 except KeyError:
1694 pass
1695 else:
1696 if param.kind is _POSITIONAL_ONLY:
1697 # If positional-only parameter is bound by partial,
1698 # it effectively disappears from the signature
1699 new_params.pop(param_name)
1700 continue
1701
1702 if param.kind is _POSITIONAL_OR_KEYWORD:
1703 if param_name in partial_keywords:
1704 # This means that this parameter, and all parameters
1705 # after it should be keyword-only (and var-positional
1706 # should be removed). Here's why. Consider the following
1707 # function:
1708 # foo(a, b, *args, c):
1709 # pass
1710 #
1711 # "partial(foo, a='spam')" will have the following
1712 # signature: "(*, a='spam', b, c)". Because attempting
1713 # to call that partial with "(10, 20)" arguments will
1714 # raise a TypeError, saying that "a" argument received
1715 # multiple values.
1716 transform_to_kwonly = True
1717 # Set the new default value
1718 new_params[param_name] = param.replace(default=arg_value)
1719 else:
1720 # was passed as a positional argument
1721 new_params.pop(param.name)
1722 continue
1723
1724 if param.kind is _KEYWORD_ONLY:
1725 # Set the new default value
1726 new_params[param_name] = param.replace(default=arg_value)
1727
1728 if transform_to_kwonly:
1729 assert param.kind is not _POSITIONAL_ONLY
1730
1731 if param.kind is _POSITIONAL_OR_KEYWORD:
1732 new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1733 new_params[param_name] = new_param
1734 new_params.move_to_end(param_name)
1735 elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
1736 new_params.move_to_end(param_name)
1737 elif param.kind is _VAR_POSITIONAL:
1738 new_params.pop(param.name)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001739
1740 return wrapped_sig.replace(parameters=new_params.values())
1741
1742
Yury Selivanov62560fb2014-01-28 12:26:24 -05001743def _signature_bound_method(sig):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001744 """Private helper to transform signatures for unbound
1745 functions to bound methods.
1746 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001747
1748 params = tuple(sig.parameters.values())
1749
1750 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1751 raise ValueError('invalid method signature')
1752
1753 kind = params[0].kind
1754 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1755 # Drop first parameter:
1756 # '(p1, p2[, ...])' -> '(p2[, ...])'
1757 params = params[1:]
1758 else:
1759 if kind is not _VAR_POSITIONAL:
1760 # Unless we add a new parameter type we never
1761 # get here
1762 raise ValueError('invalid argument type')
1763 # It's a var-positional parameter.
1764 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1765
1766 return sig.replace(parameters=params)
1767
1768
Yury Selivanovb77511d2014-01-29 10:46:14 -05001769def _signature_is_builtin(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001770 """Private helper to test if `obj` is a callable that might
1771 support Argument Clinic's __text_signature__ protocol.
1772 """
Yury Selivanov1d241832014-02-02 12:51:20 -05001773 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001774 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001775 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001776 # Can't test 'isinstance(type)' here, as it would
1777 # also be True for regular python classes
1778 obj in (type, object))
1779
1780
Yury Selivanov63da7c72014-01-31 14:48:37 -05001781def _signature_is_functionlike(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001782 """Private helper to test if `obj` is a duck type of FunctionType.
1783 A good example of such objects are functions compiled with
1784 Cython, which have all attributes that a pure Python function
1785 would have, but have their code statically compiled.
1786 """
Yury Selivanov63da7c72014-01-31 14:48:37 -05001787
1788 if not callable(obj) or isclass(obj):
1789 # All function-like objects are obviously callables,
1790 # and not classes.
1791 return False
1792
1793 name = getattr(obj, '__name__', None)
1794 code = getattr(obj, '__code__', None)
1795 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
1796 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
1797 annotations = getattr(obj, '__annotations__', None)
1798
1799 return (isinstance(code, types.CodeType) and
1800 isinstance(name, str) and
1801 (defaults is None or isinstance(defaults, tuple)) and
1802 (kwdefaults is None or isinstance(kwdefaults, dict)) and
1803 isinstance(annotations, dict))
1804
1805
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001806def _signature_get_bound_param(spec):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001807 """ Private helper to get first parameter name from a
1808 __text_signature__ of a builtin method, which should
1809 be in the following format: '($param1, ...)'.
1810 Assumptions are that the first argument won't have
1811 a default value or an annotation.
1812 """
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001813
1814 assert spec.startswith('($')
1815
1816 pos = spec.find(',')
1817 if pos == -1:
1818 pos = spec.find(')')
1819
1820 cpos = spec.find(':')
1821 assert cpos == -1 or cpos > pos
1822
1823 cpos = spec.find('=')
1824 assert cpos == -1 or cpos > pos
1825
1826 return spec[2:pos]
1827
1828
Larry Hastings2623c8c2014-02-08 22:15:29 -08001829def _signature_strip_non_python_syntax(signature):
1830 """
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001831 Private helper function. Takes a signature in Argument Clinic's
1832 extended signature format.
1833
Larry Hastings2623c8c2014-02-08 22:15:29 -08001834 Returns a tuple of three things:
1835 * that signature re-rendered in standard Python syntax,
1836 * the index of the "self" parameter (generally 0), or None if
1837 the function does not have a "self" parameter, and
1838 * the index of the last "positional only" parameter,
1839 or None if the signature has no positional-only parameters.
1840 """
1841
1842 if not signature:
1843 return signature, None, None
1844
1845 self_parameter = None
1846 last_positional_only = None
1847
1848 lines = [l.encode('ascii') for l in signature.split('\n')]
1849 generator = iter(lines).__next__
1850 token_stream = tokenize.tokenize(generator)
1851
1852 delayed_comma = False
1853 skip_next_comma = False
1854 text = []
1855 add = text.append
1856
1857 current_parameter = 0
1858 OP = token.OP
1859 ERRORTOKEN = token.ERRORTOKEN
1860
1861 # token stream always starts with ENCODING token, skip it
1862 t = next(token_stream)
1863 assert t.type == tokenize.ENCODING
1864
1865 for t in token_stream:
1866 type, string = t.type, t.string
1867
1868 if type == OP:
1869 if string == ',':
1870 if skip_next_comma:
1871 skip_next_comma = False
1872 else:
1873 assert not delayed_comma
1874 delayed_comma = True
1875 current_parameter += 1
1876 continue
1877
1878 if string == '/':
1879 assert not skip_next_comma
1880 assert last_positional_only is None
1881 skip_next_comma = True
1882 last_positional_only = current_parameter - 1
1883 continue
1884
1885 if (type == ERRORTOKEN) and (string == '$'):
1886 assert self_parameter is None
1887 self_parameter = current_parameter
1888 continue
1889
1890 if delayed_comma:
1891 delayed_comma = False
1892 if not ((type == OP) and (string == ')')):
1893 add(', ')
1894 add(string)
1895 if (string == ','):
1896 add(' ')
1897 clean_signature = ''.join(text)
1898 return clean_signature, self_parameter, last_positional_only
1899
1900
Yury Selivanov57d240e2014-02-19 16:27:23 -05001901def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001902 """Private helper to parse content of '__text_signature__'
1903 and return a Signature based on it.
1904 """
1905
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001906 Parameter = cls._parameter_cls
1907
Larry Hastings2623c8c2014-02-08 22:15:29 -08001908 clean_signature, self_parameter, last_positional_only = \
1909 _signature_strip_non_python_syntax(s)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001910
Larry Hastings2623c8c2014-02-08 22:15:29 -08001911 program = "def foo" + clean_signature + ": pass"
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001912
1913 try:
Larry Hastings2623c8c2014-02-08 22:15:29 -08001914 module = ast.parse(program)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001915 except SyntaxError:
1916 module = None
1917
1918 if not isinstance(module, ast.Module):
1919 raise ValueError("{!r} builtin has invalid signature".format(obj))
1920
1921 f = module.body[0]
1922
1923 parameters = []
1924 empty = Parameter.empty
1925 invalid = object()
1926
1927 module = None
1928 module_dict = {}
1929 module_name = getattr(obj, '__module__', None)
1930 if module_name:
1931 module = sys.modules.get(module_name, None)
1932 if module:
1933 module_dict = module.__dict__
1934 sys_module_dict = sys.modules
1935
1936 def parse_name(node):
1937 assert isinstance(node, ast.arg)
1938 if node.annotation != None:
1939 raise ValueError("Annotations are not currently supported")
1940 return node.arg
1941
1942 def wrap_value(s):
1943 try:
1944 value = eval(s, module_dict)
1945 except NameError:
1946 try:
1947 value = eval(s, sys_module_dict)
1948 except NameError:
1949 raise RuntimeError()
1950
1951 if isinstance(value, str):
1952 return ast.Str(value)
1953 if isinstance(value, (int, float)):
1954 return ast.Num(value)
1955 if isinstance(value, bytes):
1956 return ast.Bytes(value)
1957 if value in (True, False, None):
1958 return ast.NameConstant(value)
1959 raise RuntimeError()
1960
1961 class RewriteSymbolics(ast.NodeTransformer):
1962 def visit_Attribute(self, node):
1963 a = []
1964 n = node
1965 while isinstance(n, ast.Attribute):
1966 a.append(n.attr)
1967 n = n.value
1968 if not isinstance(n, ast.Name):
1969 raise RuntimeError()
1970 a.append(n.id)
1971 value = ".".join(reversed(a))
1972 return wrap_value(value)
1973
1974 def visit_Name(self, node):
1975 if not isinstance(node.ctx, ast.Load):
1976 raise ValueError()
1977 return wrap_value(node.id)
1978
1979 def p(name_node, default_node, default=empty):
1980 name = parse_name(name_node)
1981 if name is invalid:
1982 return None
1983 if default_node and default_node is not _empty:
1984 try:
1985 default_node = RewriteSymbolics().visit(default_node)
1986 o = ast.literal_eval(default_node)
1987 except ValueError:
1988 o = invalid
1989 if o is invalid:
1990 return None
1991 default = o if o is not invalid else default
1992 parameters.append(Parameter(name, kind, default=default, annotation=empty))
1993
1994 # non-keyword-only parameters
1995 args = reversed(f.args.args)
1996 defaults = reversed(f.args.defaults)
1997 iter = itertools.zip_longest(args, defaults, fillvalue=None)
Larry Hastings2623c8c2014-02-08 22:15:29 -08001998 if last_positional_only is not None:
1999 kind = Parameter.POSITIONAL_ONLY
2000 else:
2001 kind = Parameter.POSITIONAL_OR_KEYWORD
2002 for i, (name, default) in enumerate(reversed(list(iter))):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002003 p(name, default)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002004 if i == last_positional_only:
2005 kind = Parameter.POSITIONAL_OR_KEYWORD
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002006
2007 # *args
2008 if f.args.vararg:
2009 kind = Parameter.VAR_POSITIONAL
2010 p(f.args.vararg, empty)
2011
2012 # keyword-only arguments
2013 kind = Parameter.KEYWORD_ONLY
2014 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
2015 p(name, default)
2016
2017 # **kwargs
2018 if f.args.kwarg:
2019 kind = Parameter.VAR_KEYWORD
2020 p(f.args.kwarg, empty)
2021
Larry Hastings2623c8c2014-02-08 22:15:29 -08002022 if self_parameter is not None:
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002023 # Possibly strip the bound argument:
2024 # - We *always* strip first bound argument if
2025 # it is a module.
2026 # - We don't strip first bound argument if
2027 # skip_bound_arg is False.
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002028 assert parameters
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002029 _self = getattr(obj, '__self__', None)
2030 self_isbound = _self is not None
2031 self_ismodule = ismodule(_self)
2032 if self_isbound and (self_ismodule or skip_bound_arg):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002033 parameters.pop(0)
2034 else:
2035 # for builtins, self parameter is always positional-only!
2036 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
2037 parameters[0] = p
2038
2039 return cls(parameters, return_annotation=cls.empty)
2040
2041
Yury Selivanov57d240e2014-02-19 16:27:23 -05002042def _signature_from_builtin(cls, func, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002043 """Private helper function to get signature for
2044 builtin callables.
2045 """
2046
Yury Selivanov57d240e2014-02-19 16:27:23 -05002047 if not _signature_is_builtin(func):
2048 raise TypeError("{!r} is not a Python builtin "
2049 "function".format(func))
2050
2051 s = getattr(func, "__text_signature__", None)
2052 if not s:
2053 raise ValueError("no signature found for builtin {!r}".format(func))
2054
2055 return _signature_fromstr(cls, func, s, skip_bound_arg)
2056
2057
Yury Selivanovcf45f022015-05-20 14:38:50 -04002058def _signature_from_function(cls, func):
2059 """Private helper: constructs Signature for the given python function."""
2060
2061 is_duck_function = False
2062 if not isfunction(func):
2063 if _signature_is_functionlike(func):
2064 is_duck_function = True
2065 else:
2066 # If it's not a pure Python function, and not a duck type
2067 # of pure function:
2068 raise TypeError('{!r} is not a Python function'.format(func))
2069
2070 Parameter = cls._parameter_cls
2071
2072 # Parameter information.
2073 func_code = func.__code__
2074 pos_count = func_code.co_argcount
2075 arg_names = func_code.co_varnames
2076 positional = tuple(arg_names[:pos_count])
2077 keyword_only_count = func_code.co_kwonlyargcount
2078 keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)]
2079 annotations = func.__annotations__
2080 defaults = func.__defaults__
2081 kwdefaults = func.__kwdefaults__
2082
2083 if defaults:
2084 pos_default_count = len(defaults)
2085 else:
2086 pos_default_count = 0
2087
2088 parameters = []
2089
2090 # Non-keyword-only parameters w/o defaults.
2091 non_default_count = pos_count - pos_default_count
2092 for name in positional[:non_default_count]:
2093 annotation = annotations.get(name, _empty)
2094 parameters.append(Parameter(name, annotation=annotation,
2095 kind=_POSITIONAL_OR_KEYWORD))
2096
2097 # ... w/ defaults.
2098 for offset, name in enumerate(positional[non_default_count:]):
2099 annotation = annotations.get(name, _empty)
2100 parameters.append(Parameter(name, annotation=annotation,
2101 kind=_POSITIONAL_OR_KEYWORD,
2102 default=defaults[offset]))
2103
2104 # *args
2105 if func_code.co_flags & CO_VARARGS:
2106 name = arg_names[pos_count + keyword_only_count]
2107 annotation = annotations.get(name, _empty)
2108 parameters.append(Parameter(name, annotation=annotation,
2109 kind=_VAR_POSITIONAL))
2110
2111 # Keyword-only parameters.
2112 for name in keyword_only:
2113 default = _empty
2114 if kwdefaults is not None:
2115 default = kwdefaults.get(name, _empty)
2116
2117 annotation = annotations.get(name, _empty)
2118 parameters.append(Parameter(name, annotation=annotation,
2119 kind=_KEYWORD_ONLY,
2120 default=default))
2121 # **kwargs
2122 if func_code.co_flags & CO_VARKEYWORDS:
2123 index = pos_count + keyword_only_count
2124 if func_code.co_flags & CO_VARARGS:
2125 index += 1
2126
2127 name = arg_names[index]
2128 annotation = annotations.get(name, _empty)
2129 parameters.append(Parameter(name, annotation=annotation,
2130 kind=_VAR_KEYWORD))
2131
2132 # Is 'func' is a pure Python function - don't validate the
2133 # parameters list (for correct order and defaults), it should be OK.
2134 return cls(parameters,
2135 return_annotation=annotations.get('return', _empty),
2136 __validate_parameters__=is_duck_function)
2137
2138
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002139def _signature_from_callable(obj, *,
2140 follow_wrapper_chains=True,
2141 skip_bound_arg=True,
2142 sigcls):
2143
2144 """Private helper function to get signature for arbitrary
2145 callable objects.
2146 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002147
2148 if not callable(obj):
2149 raise TypeError('{!r} is not a callable object'.format(obj))
2150
2151 if isinstance(obj, types.MethodType):
2152 # In this case we skip the first parameter of the underlying
2153 # function (usually `self` or `cls`).
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002154 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002155 obj.__func__,
2156 follow_wrapper_chains=follow_wrapper_chains,
2157 skip_bound_arg=skip_bound_arg,
2158 sigcls=sigcls)
2159
Yury Selivanov57d240e2014-02-19 16:27:23 -05002160 if skip_bound_arg:
2161 return _signature_bound_method(sig)
2162 else:
2163 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002164
Nick Coghlane8c45d62013-07-28 20:00:01 +10002165 # Was this function wrapped by a decorator?
Yury Selivanov57d240e2014-02-19 16:27:23 -05002166 if follow_wrapper_chains:
2167 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
Yury Selivanov46c759d2015-05-27 21:56:53 -04002168 if isinstance(obj, types.MethodType):
2169 # If the unwrapped object is a *method*, we might want to
2170 # skip its first parameter (self).
2171 # See test_signature_wrapped_bound_method for details.
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002172 return _signature_from_callable(
Yury Selivanov46c759d2015-05-27 21:56:53 -04002173 obj,
2174 follow_wrapper_chains=follow_wrapper_chains,
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002175 skip_bound_arg=skip_bound_arg,
2176 sigcls=sigcls)
Nick Coghlane8c45d62013-07-28 20:00:01 +10002177
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002178 try:
2179 sig = obj.__signature__
2180 except AttributeError:
2181 pass
2182 else:
2183 if sig is not None:
Yury Selivanov42407ab2014-06-23 10:23:50 -07002184 if not isinstance(sig, Signature):
2185 raise TypeError(
2186 'unexpected object {!r} in __signature__ '
2187 'attribute'.format(sig))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002188 return sig
2189
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002190 try:
2191 partialmethod = obj._partialmethod
2192 except AttributeError:
2193 pass
2194 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05002195 if isinstance(partialmethod, functools.partialmethod):
2196 # Unbound partialmethod (see functools.partialmethod)
2197 # This means, that we need to calculate the signature
2198 # as if it's a regular partial object, but taking into
2199 # account that the first positional argument
2200 # (usually `self`, or `cls`) will not be passed
2201 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002202
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002203 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002204 partialmethod.func,
2205 follow_wrapper_chains=follow_wrapper_chains,
2206 skip_bound_arg=skip_bound_arg,
2207 sigcls=sigcls)
2208
Yury Selivanov0486f812014-01-29 12:18:59 -05002209 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002210
Yury Selivanov0486f812014-01-29 12:18:59 -05002211 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
2212 new_params = (first_wrapped_param,) + tuple(sig.parameters.values())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002213
Yury Selivanov0486f812014-01-29 12:18:59 -05002214 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002215
Yury Selivanov63da7c72014-01-31 14:48:37 -05002216 if isfunction(obj) or _signature_is_functionlike(obj):
2217 # If it's a pure Python function, or an object that is duck type
2218 # of a Python function (Cython functions, for instance), then:
Yury Selivanovcf45f022015-05-20 14:38:50 -04002219 return _signature_from_function(sigcls, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002220
Yury Selivanova773de02014-02-21 18:30:53 -05002221 if _signature_is_builtin(obj):
Yury Selivanovda396452014-03-27 12:09:24 -04002222 return _signature_from_builtin(sigcls, obj,
Yury Selivanova773de02014-02-21 18:30:53 -05002223 skip_bound_arg=skip_bound_arg)
2224
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002225 if isinstance(obj, functools.partial):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002226 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002227 obj.func,
2228 follow_wrapper_chains=follow_wrapper_chains,
2229 skip_bound_arg=skip_bound_arg,
2230 sigcls=sigcls)
Yury Selivanov62560fb2014-01-28 12:26:24 -05002231 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002232
2233 sig = None
2234 if isinstance(obj, type):
2235 # obj is a class or a metaclass
2236
2237 # First, let's see if it has an overloaded __call__ defined
2238 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05002239 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002240 if call is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002241 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002242 call,
2243 follow_wrapper_chains=follow_wrapper_chains,
2244 skip_bound_arg=skip_bound_arg,
2245 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002246 else:
2247 # Now we check if the 'obj' class has a '__new__' method
Yury Selivanov421f0c72014-01-29 12:05:40 -05002248 new = _signature_get_user_defined_method(obj, '__new__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002249 if new is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002250 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002251 new,
2252 follow_wrapper_chains=follow_wrapper_chains,
2253 skip_bound_arg=skip_bound_arg,
2254 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002255 else:
2256 # Finally, we should have at least __init__ implemented
Yury Selivanov421f0c72014-01-29 12:05:40 -05002257 init = _signature_get_user_defined_method(obj, '__init__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002258 if init is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002259 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002260 init,
2261 follow_wrapper_chains=follow_wrapper_chains,
2262 skip_bound_arg=skip_bound_arg,
2263 sigcls=sigcls)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002264
2265 if sig is None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002266 # At this point we know, that `obj` is a class, with no user-
2267 # defined '__init__', '__new__', or class-level '__call__'
2268
Larry Hastings2623c8c2014-02-08 22:15:29 -08002269 for base in obj.__mro__[:-1]:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002270 # Since '__text_signature__' is implemented as a
2271 # descriptor that extracts text signature from the
2272 # class docstring, if 'obj' is derived from a builtin
2273 # class, its own '__text_signature__' may be 'None'.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002274 # Therefore, we go through the MRO (except the last
2275 # class in there, which is 'object') to find the first
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002276 # class with non-empty text signature.
2277 try:
2278 text_sig = base.__text_signature__
2279 except AttributeError:
2280 pass
2281 else:
2282 if text_sig:
2283 # If 'obj' class has a __text_signature__ attribute:
2284 # return a signature based on it
Yury Selivanovda396452014-03-27 12:09:24 -04002285 return _signature_fromstr(sigcls, obj, text_sig)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002286
2287 # No '__text_signature__' was found for the 'obj' class.
2288 # Last option is to check if its '__init__' is
2289 # object.__init__ or type.__init__.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002290 if type not in obj.__mro__:
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002291 # We have a class (not metaclass), but no user-defined
2292 # __init__ or __new__ for it
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002293 if (obj.__init__ is object.__init__ and
2294 obj.__new__ is object.__new__):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002295 # Return a signature of 'object' builtin.
2296 return signature(object)
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002297 else:
2298 raise ValueError(
2299 'no signature found for builtin type {!r}'.format(obj))
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002300
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002301 elif not isinstance(obj, _NonUserDefinedCallables):
2302 # An object with __call__
2303 # We also check that the 'obj' is not an instance of
2304 # _WrapperDescriptor or _MethodWrapper to avoid
2305 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05002306 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002307 if call is not None:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002308 try:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002309 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002310 call,
2311 follow_wrapper_chains=follow_wrapper_chains,
2312 skip_bound_arg=skip_bound_arg,
2313 sigcls=sigcls)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002314 except ValueError as ex:
2315 msg = 'no signature found for {!r}'.format(obj)
2316 raise ValueError(msg) from ex
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002317
2318 if sig is not None:
2319 # For classes and objects we skip the first parameter of their
2320 # __call__, __new__, or __init__ methods
Yury Selivanov57d240e2014-02-19 16:27:23 -05002321 if skip_bound_arg:
2322 return _signature_bound_method(sig)
2323 else:
2324 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002325
2326 if isinstance(obj, types.BuiltinFunctionType):
2327 # Raise a nicer error message for builtins
2328 msg = 'no signature found for builtin function {!r}'.format(obj)
2329 raise ValueError(msg)
2330
2331 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2332
2333
2334class _void:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002335 """A private marker - used in Parameter & Signature."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002336
2337
2338class _empty:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002339 """Marker object for Signature.empty and Parameter.empty."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002340
2341
Yury Selivanov21e83a52014-03-27 11:23:13 -04002342class _ParameterKind(enum.IntEnum):
2343 POSITIONAL_ONLY = 0
2344 POSITIONAL_OR_KEYWORD = 1
2345 VAR_POSITIONAL = 2
2346 KEYWORD_ONLY = 3
2347 VAR_KEYWORD = 4
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002348
2349 def __str__(self):
Yury Selivanov21e83a52014-03-27 11:23:13 -04002350 return self._name_
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002351
2352
Yury Selivanov21e83a52014-03-27 11:23:13 -04002353_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
2354_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
2355_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
2356_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
2357_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002358
2359
2360class Parameter:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002361 """Represents a parameter in a function signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002362
2363 Has the following public attributes:
2364
2365 * name : str
2366 The name of the parameter as a string.
2367 * default : object
2368 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002369 parameter has no default value, this attribute is set to
2370 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002371 * annotation
2372 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002373 parameter has no annotation, this attribute is set to
2374 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002375 * kind : str
2376 Describes how argument values are bound to the parameter.
2377 Possible values: `Parameter.POSITIONAL_ONLY`,
2378 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
2379 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002380 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002381
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002382 __slots__ = ('_name', '_kind', '_default', '_annotation')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002383
2384 POSITIONAL_ONLY = _POSITIONAL_ONLY
2385 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
2386 VAR_POSITIONAL = _VAR_POSITIONAL
2387 KEYWORD_ONLY = _KEYWORD_ONLY
2388 VAR_KEYWORD = _VAR_KEYWORD
2389
2390 empty = _empty
2391
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002392 def __init__(self, name, kind, *, default=_empty, annotation=_empty):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002393
2394 if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
2395 _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
2396 raise ValueError("invalid value for 'Parameter.kind' attribute")
2397 self._kind = kind
2398
2399 if default is not _empty:
2400 if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2401 msg = '{} parameters cannot have default values'.format(kind)
2402 raise ValueError(msg)
2403 self._default = default
2404 self._annotation = annotation
2405
Yury Selivanov2393dca2014-01-27 15:07:58 -05002406 if name is _empty:
2407 raise ValueError('name is a required attribute for Parameter')
2408
2409 if not isinstance(name, str):
2410 raise TypeError("name must be a str, not a {!r}".format(name))
2411
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002412 if name[0] == '.' and name[1:].isdigit():
2413 # These are implicit arguments generated by comprehensions. In
2414 # order to provide a friendlier interface to users, we recast
2415 # their name as "implicitN" and treat them as positional-only.
2416 # See issue 19611.
2417 if kind != _POSITIONAL_OR_KEYWORD:
2418 raise ValueError(
2419 'implicit arguments must be passed in as {}'.format(
2420 _POSITIONAL_OR_KEYWORD
2421 )
2422 )
2423 self._kind = _POSITIONAL_ONLY
2424 name = 'implicit{}'.format(name[1:])
2425
Yury Selivanov2393dca2014-01-27 15:07:58 -05002426 if not name.isidentifier():
2427 raise ValueError('{!r} is not a valid parameter name'.format(name))
2428
2429 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002430
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002431 def __reduce__(self):
2432 return (type(self),
2433 (self._name, self._kind),
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002434 {'_default': self._default,
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002435 '_annotation': self._annotation})
2436
2437 def __setstate__(self, state):
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002438 self._default = state['_default']
2439 self._annotation = state['_annotation']
2440
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002441 @property
2442 def name(self):
2443 return self._name
2444
2445 @property
2446 def default(self):
2447 return self._default
2448
2449 @property
2450 def annotation(self):
2451 return self._annotation
2452
2453 @property
2454 def kind(self):
2455 return self._kind
2456
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002457 def replace(self, *, name=_void, kind=_void,
2458 annotation=_void, default=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002459 """Creates a customized copy of the Parameter."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002460
2461 if name is _void:
2462 name = self._name
2463
2464 if kind is _void:
2465 kind = self._kind
2466
2467 if annotation is _void:
2468 annotation = self._annotation
2469
2470 if default is _void:
2471 default = self._default
2472
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002473 return type(self)(name, kind, default=default, annotation=annotation)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002474
2475 def __str__(self):
2476 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002477 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002478
2479 # Add annotation and default value
2480 if self._annotation is not _empty:
2481 formatted = '{}:{}'.format(formatted,
2482 formatannotation(self._annotation))
2483
2484 if self._default is not _empty:
2485 formatted = '{}={}'.format(formatted, repr(self._default))
2486
2487 if kind == _VAR_POSITIONAL:
2488 formatted = '*' + formatted
2489 elif kind == _VAR_KEYWORD:
2490 formatted = '**' + formatted
2491
2492 return formatted
2493
2494 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002495 return '<{} "{}">'.format(self.__class__.__name__, self)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002496
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002497 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002498 return hash((self.name, self.kind, self.annotation, self.default))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002499
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002500 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002501 if self is other:
2502 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002503 if not isinstance(other, Parameter):
2504 return NotImplemented
2505 return (self._name == other._name and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002506 self._kind == other._kind and
2507 self._default == other._default and
2508 self._annotation == other._annotation)
2509
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002510
2511class BoundArguments:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002512 """Result of `Signature.bind` call. Holds the mapping of arguments
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002513 to the function's parameters.
2514
2515 Has the following public attributes:
2516
2517 * arguments : OrderedDict
2518 An ordered mutable mapping of parameters' names to arguments' values.
2519 Does not contain arguments' default values.
2520 * signature : Signature
2521 The Signature object that created this instance.
2522 * args : tuple
2523 Tuple of positional arguments values.
2524 * kwargs : dict
2525 Dict of keyword arguments values.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002526 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002527
Yury Selivanov6abe0322015-05-13 17:18:41 -04002528 __slots__ = ('arguments', '_signature', '__weakref__')
2529
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002530 def __init__(self, signature, arguments):
2531 self.arguments = arguments
2532 self._signature = signature
2533
2534 @property
2535 def signature(self):
2536 return self._signature
2537
2538 @property
2539 def args(self):
2540 args = []
2541 for param_name, param in self._signature.parameters.items():
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002542 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002543 break
2544
2545 try:
2546 arg = self.arguments[param_name]
2547 except KeyError:
2548 # We're done here. Other arguments
2549 # will be mapped in 'BoundArguments.kwargs'
2550 break
2551 else:
2552 if param.kind == _VAR_POSITIONAL:
2553 # *args
2554 args.extend(arg)
2555 else:
2556 # plain argument
2557 args.append(arg)
2558
2559 return tuple(args)
2560
2561 @property
2562 def kwargs(self):
2563 kwargs = {}
2564 kwargs_started = False
2565 for param_name, param in self._signature.parameters.items():
2566 if not kwargs_started:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002567 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002568 kwargs_started = True
2569 else:
2570 if param_name not in self.arguments:
2571 kwargs_started = True
2572 continue
2573
2574 if not kwargs_started:
2575 continue
2576
2577 try:
2578 arg = self.arguments[param_name]
2579 except KeyError:
2580 pass
2581 else:
2582 if param.kind == _VAR_KEYWORD:
2583 # **kwargs
2584 kwargs.update(arg)
2585 else:
2586 # plain keyword argument
2587 kwargs[param_name] = arg
2588
2589 return kwargs
2590
Yury Selivanovb907a512015-05-16 13:45:09 -04002591 def apply_defaults(self):
2592 """Set default values for missing arguments.
2593
2594 For variable-positional arguments (*args) the default is an
2595 empty tuple.
2596
2597 For variable-keyword arguments (**kwargs) the default is an
2598 empty dict.
2599 """
2600 arguments = self.arguments
Yury Selivanovb907a512015-05-16 13:45:09 -04002601 new_arguments = []
2602 for name, param in self._signature.parameters.items():
2603 try:
2604 new_arguments.append((name, arguments[name]))
2605 except KeyError:
2606 if param.default is not _empty:
2607 val = param.default
2608 elif param.kind is _VAR_POSITIONAL:
2609 val = ()
2610 elif param.kind is _VAR_KEYWORD:
2611 val = {}
2612 else:
2613 # This BoundArguments was likely produced by
2614 # Signature.bind_partial().
2615 continue
2616 new_arguments.append((name, val))
2617 self.arguments = OrderedDict(new_arguments)
2618
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002619 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002620 if self is other:
2621 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002622 if not isinstance(other, BoundArguments):
2623 return NotImplemented
2624 return (self.signature == other.signature and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002625 self.arguments == other.arguments)
2626
Yury Selivanov6abe0322015-05-13 17:18:41 -04002627 def __setstate__(self, state):
2628 self._signature = state['_signature']
2629 self.arguments = state['arguments']
2630
2631 def __getstate__(self):
2632 return {'_signature': self._signature, 'arguments': self.arguments}
2633
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002634 def __repr__(self):
2635 args = []
2636 for arg, value in self.arguments.items():
2637 args.append('{}={!r}'.format(arg, value))
Yury Selivanovf229bc52015-05-15 12:53:56 -04002638 return '<{} ({})>'.format(self.__class__.__name__, ', '.join(args))
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002639
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002640
2641class Signature:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002642 """A Signature object represents the overall signature of a function.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002643 It stores a Parameter object for each parameter accepted by the
2644 function, as well as information specific to the function itself.
2645
2646 A Signature object has the following public attributes and methods:
2647
2648 * parameters : OrderedDict
2649 An ordered mapping of parameters' names to the corresponding
2650 Parameter objects (keyword-only arguments are in the same order
2651 as listed in `code.co_varnames`).
2652 * return_annotation : object
2653 The annotation for the return type of the function if specified.
2654 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002655 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002656 * bind(*args, **kwargs) -> BoundArguments
2657 Creates a mapping from positional and keyword arguments to
2658 parameters.
2659 * bind_partial(*args, **kwargs) -> BoundArguments
2660 Creates a partial mapping from positional and keyword arguments
2661 to parameters (simulating 'functools.partial' behavior.)
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002662 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002663
2664 __slots__ = ('_return_annotation', '_parameters')
2665
2666 _parameter_cls = Parameter
2667 _bound_arguments_cls = BoundArguments
2668
2669 empty = _empty
2670
2671 def __init__(self, parameters=None, *, return_annotation=_empty,
2672 __validate_parameters__=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002673 """Constructs Signature from the given list of Parameter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002674 objects and 'return_annotation'. All arguments are optional.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002675 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002676
2677 if parameters is None:
2678 params = OrderedDict()
2679 else:
2680 if __validate_parameters__:
2681 params = OrderedDict()
2682 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002683 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002684
2685 for idx, param in enumerate(parameters):
2686 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002687 name = param.name
2688
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002689 if kind < top_kind:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002690 msg = 'wrong parameter order: {!r} before {!r}'
Yury Selivanov2393dca2014-01-27 15:07:58 -05002691 msg = msg.format(top_kind, kind)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002692 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002693 elif kind > top_kind:
2694 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002695 top_kind = kind
2696
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002697 if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
Yury Selivanov07a9e452014-01-29 10:58:16 -05002698 if param.default is _empty:
2699 if kind_defaults:
2700 # No default for this parameter, but the
2701 # previous parameter of the same kind had
2702 # a default
2703 msg = 'non-default argument follows default ' \
2704 'argument'
2705 raise ValueError(msg)
2706 else:
2707 # There is a default for this parameter.
2708 kind_defaults = True
2709
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002710 if name in params:
2711 msg = 'duplicate parameter name: {!r}'.format(name)
2712 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002713
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002714 params[name] = param
2715 else:
2716 params = OrderedDict(((param.name, param)
2717 for param in parameters))
2718
2719 self._parameters = types.MappingProxyType(params)
2720 self._return_annotation = return_annotation
2721
2722 @classmethod
2723 def from_function(cls, func):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002724 """Constructs Signature for the given python function."""
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002725
2726 warnings.warn("inspect.Signature.from_function() is deprecated, "
Berker Peksagb5601582015-05-21 23:40:54 +03002727 "use Signature.from_callable()",
2728 DeprecationWarning, stacklevel=2)
Yury Selivanovcf45f022015-05-20 14:38:50 -04002729 return _signature_from_function(cls, func)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002730
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002731 @classmethod
2732 def from_builtin(cls, func):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002733 """Constructs Signature for the given builtin function."""
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002734
2735 warnings.warn("inspect.Signature.from_builtin() is deprecated, "
Berker Peksagb5601582015-05-21 23:40:54 +03002736 "use Signature.from_callable()",
2737 DeprecationWarning, stacklevel=2)
Yury Selivanov57d240e2014-02-19 16:27:23 -05002738 return _signature_from_builtin(cls, func)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002739
Yury Selivanovda396452014-03-27 12:09:24 -04002740 @classmethod
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002741 def from_callable(cls, obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002742 """Constructs Signature for the given callable object."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002743 return _signature_from_callable(obj, sigcls=cls,
2744 follow_wrapper_chains=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04002745
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002746 @property
2747 def parameters(self):
2748 return self._parameters
2749
2750 @property
2751 def return_annotation(self):
2752 return self._return_annotation
2753
2754 def replace(self, *, parameters=_void, return_annotation=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002755 """Creates a customized copy of the Signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002756 Pass 'parameters' and/or 'return_annotation' arguments
2757 to override them in the new copy.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002758 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002759
2760 if parameters is _void:
2761 parameters = self.parameters.values()
2762
2763 if return_annotation is _void:
2764 return_annotation = self._return_annotation
2765
2766 return type(self)(parameters,
2767 return_annotation=return_annotation)
2768
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002769 def _hash_basis(self):
2770 params = tuple(param for param in self.parameters.values()
2771 if param.kind != _KEYWORD_ONLY)
2772
2773 kwo_params = {param.name: param for param in self.parameters.values()
2774 if param.kind == _KEYWORD_ONLY}
2775
2776 return params, kwo_params, self.return_annotation
2777
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002778 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002779 params, kwo_params, return_annotation = self._hash_basis()
2780 kwo_params = frozenset(kwo_params.values())
2781 return hash((params, kwo_params, return_annotation))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002782
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002783 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002784 if self is other:
2785 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002786 if not isinstance(other, Signature):
2787 return NotImplemented
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002788 return self._hash_basis() == other._hash_basis()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002789
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002790 def _bind(self, args, kwargs, *, partial=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002791 """Private method. Don't use directly."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002792
2793 arguments = OrderedDict()
2794
2795 parameters = iter(self.parameters.values())
2796 parameters_ex = ()
2797 arg_vals = iter(args)
2798
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002799 while True:
2800 # Let's iterate through the positional arguments and corresponding
2801 # parameters
2802 try:
2803 arg_val = next(arg_vals)
2804 except StopIteration:
2805 # No more positional arguments
2806 try:
2807 param = next(parameters)
2808 except StopIteration:
2809 # No more parameters. That's it. Just need to check that
2810 # we have no `kwargs` after this while loop
2811 break
2812 else:
2813 if param.kind == _VAR_POSITIONAL:
2814 # That's OK, just empty *args. Let's start parsing
2815 # kwargs
2816 break
2817 elif param.name in kwargs:
2818 if param.kind == _POSITIONAL_ONLY:
2819 msg = '{arg!r} parameter is positional only, ' \
2820 'but was passed as a keyword'
2821 msg = msg.format(arg=param.name)
2822 raise TypeError(msg) from None
2823 parameters_ex = (param,)
2824 break
2825 elif (param.kind == _VAR_KEYWORD or
2826 param.default is not _empty):
2827 # That's fine too - we have a default value for this
2828 # parameter. So, lets start parsing `kwargs`, starting
2829 # with the current parameter
2830 parameters_ex = (param,)
2831 break
2832 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002833 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
2834 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002835 if partial:
2836 parameters_ex = (param,)
2837 break
2838 else:
Yury Selivanov86872752015-05-19 00:27:49 -04002839 msg = 'missing a required argument: {arg!r}'
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002840 msg = msg.format(arg=param.name)
2841 raise TypeError(msg) from None
2842 else:
2843 # We have a positional argument to process
2844 try:
2845 param = next(parameters)
2846 except StopIteration:
2847 raise TypeError('too many positional arguments') from None
2848 else:
2849 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
2850 # Looks like we have no parameter for this positional
2851 # argument
Yury Selivanov86872752015-05-19 00:27:49 -04002852 raise TypeError(
2853 'too many positional arguments') from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002854
2855 if param.kind == _VAR_POSITIONAL:
2856 # We have an '*args'-like argument, let's fill it with
2857 # all positional arguments we have left and move on to
2858 # the next phase
2859 values = [arg_val]
2860 values.extend(arg_vals)
2861 arguments[param.name] = tuple(values)
2862 break
2863
2864 if param.name in kwargs:
Yury Selivanov86872752015-05-19 00:27:49 -04002865 raise TypeError(
2866 'multiple values for argument {arg!r}'.format(
2867 arg=param.name)) from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002868
2869 arguments[param.name] = arg_val
2870
2871 # Now, we iterate through the remaining parameters to process
2872 # keyword arguments
2873 kwargs_param = None
2874 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002875 if param.kind == _VAR_KEYWORD:
2876 # Memorize that we have a '**kwargs'-like parameter
2877 kwargs_param = param
2878 continue
2879
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002880 if param.kind == _VAR_POSITIONAL:
2881 # Named arguments don't refer to '*args'-like parameters.
2882 # We only arrive here if the positional arguments ended
2883 # before reaching the last parameter before *args.
2884 continue
2885
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002886 param_name = param.name
2887 try:
2888 arg_val = kwargs.pop(param_name)
2889 except KeyError:
2890 # We have no value for this parameter. It's fine though,
2891 # if it has a default value, or it is an '*args'-like
2892 # parameter, left alone by the processing of positional
2893 # arguments.
2894 if (not partial and param.kind != _VAR_POSITIONAL and
2895 param.default is _empty):
Yury Selivanov86872752015-05-19 00:27:49 -04002896 raise TypeError('missing a required argument: {arg!r}'. \
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002897 format(arg=param_name)) from None
2898
2899 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05002900 if param.kind == _POSITIONAL_ONLY:
2901 # This should never happen in case of a properly built
2902 # Signature object (but let's have this check here
2903 # to ensure correct behaviour just in case)
2904 raise TypeError('{arg!r} parameter is positional only, '
2905 'but was passed as a keyword'. \
2906 format(arg=param.name))
2907
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002908 arguments[param_name] = arg_val
2909
2910 if kwargs:
2911 if kwargs_param is not None:
2912 # Process our '**kwargs'-like parameter
2913 arguments[kwargs_param.name] = kwargs
2914 else:
Yury Selivanov86872752015-05-19 00:27:49 -04002915 raise TypeError(
2916 'got an unexpected keyword argument {arg!r}'.format(
2917 arg=next(iter(kwargs))))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002918
2919 return self._bound_arguments_cls(self, arguments)
2920
Yury Selivanovc45873e2014-01-29 12:10:27 -05002921 def bind(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002922 """Get a BoundArguments object, that maps the passed `args`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002923 and `kwargs` to the function's signature. Raises `TypeError`
2924 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)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002927
Yury Selivanovc45873e2014-01-29 12:10:27 -05002928 def bind_partial(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002929 """Get a BoundArguments object, that partially maps the
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002930 passed `args` and `kwargs` to the function's signature.
2931 Raises `TypeError` if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002932 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05002933 return args[0]._bind(args[1:], kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002934
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002935 def __reduce__(self):
2936 return (type(self),
2937 (tuple(self._parameters.values()),),
2938 {'_return_annotation': self._return_annotation})
2939
2940 def __setstate__(self, state):
2941 self._return_annotation = state['_return_annotation']
2942
Yury Selivanov374375d2014-03-27 12:41:53 -04002943 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002944 return '<{} {}>'.format(self.__class__.__name__, self)
Yury Selivanov374375d2014-03-27 12:41:53 -04002945
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002946 def __str__(self):
2947 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05002948 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002949 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05002950 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002951 formatted = str(param)
2952
2953 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002954
2955 if kind == _POSITIONAL_ONLY:
2956 render_pos_only_separator = True
2957 elif render_pos_only_separator:
2958 # It's not a positional-only parameter, and the flag
2959 # is set to 'True' (there were pos-only params before.)
2960 result.append('/')
2961 render_pos_only_separator = False
2962
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002963 if kind == _VAR_POSITIONAL:
2964 # OK, we have an '*args'-like parameter, so we won't need
2965 # a '*' to separate keyword-only arguments
2966 render_kw_only_separator = False
2967 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
2968 # We have a keyword-only parameter to render and we haven't
2969 # rendered an '*args'-like parameter before, so add a '*'
2970 # separator to the parameters list ("foo(arg1, *, arg2)" case)
2971 result.append('*')
2972 # This condition should be only triggered once, so
2973 # reset the flag
2974 render_kw_only_separator = False
2975
2976 result.append(formatted)
2977
Yury Selivanov2393dca2014-01-27 15:07:58 -05002978 if render_pos_only_separator:
2979 # There were only positional-only parameters, hence the
2980 # flag was not reset to 'False'
2981 result.append('/')
2982
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002983 rendered = '({})'.format(', '.join(result))
2984
2985 if self.return_annotation is not _empty:
2986 anno = formatannotation(self.return_annotation)
2987 rendered += ' -> {}'.format(anno)
2988
2989 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10002990
Yury Selivanovda396452014-03-27 12:09:24 -04002991
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002992def signature(obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002993 """Get a signature object for the passed callable."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002994 return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04002995
2996
Nick Coghlanf94a16b2013-09-22 22:46:49 +10002997def _main():
2998 """ Logic for inspecting an object given at command line """
2999 import argparse
3000 import importlib
3001
3002 parser = argparse.ArgumentParser()
3003 parser.add_argument(
3004 'object',
3005 help="The object to be analysed. "
3006 "It supports the 'module:qualname' syntax")
3007 parser.add_argument(
3008 '-d', '--details', action='store_true',
3009 help='Display info about the module rather than its source code')
3010
3011 args = parser.parse_args()
3012
3013 target = args.object
3014 mod_name, has_attrs, attrs = target.partition(":")
3015 try:
3016 obj = module = importlib.import_module(mod_name)
3017 except Exception as exc:
3018 msg = "Failed to import {} ({}: {})".format(mod_name,
3019 type(exc).__name__,
3020 exc)
3021 print(msg, file=sys.stderr)
3022 exit(2)
3023
3024 if has_attrs:
3025 parts = attrs.split(".")
3026 obj = module
3027 for part in parts:
3028 obj = getattr(obj, part)
3029
3030 if module.__name__ in sys.builtin_module_names:
3031 print("Can't get info for builtin modules.", file=sys.stderr)
3032 exit(1)
3033
3034 if args.details:
3035 print('Target: {}'.format(target))
3036 print('Origin: {}'.format(getsourcefile(module)))
3037 print('Cached: {}'.format(module.__cached__))
3038 if obj is module:
3039 print('Loader: {}'.format(repr(module.__loader__)))
3040 if hasattr(module, '__path__'):
3041 print('Submodule search path: {}'.format(module.__path__))
3042 else:
3043 try:
3044 __, lineno = findsource(obj)
3045 except Exception:
3046 pass
3047 else:
3048 print('Line: {}'.format(lineno))
3049
3050 print('\n')
3051 else:
3052 print(getsource(obj))
3053
3054
3055if __name__ == "__main__":
3056 _main()