blob: 8c121ce96c415871122169ac6c0363604a7811e1 [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
Natefcfe80e2017-04-24 10:06:15 -070034import abc
Larry Hastings44e2eaa2013-11-23 15:37:55 -080035import ast
Antoine Pitroua8723a02015-04-15 00:41:29 +020036import dis
Yury Selivanov75445082015-05-11 22:57:16 -040037import collections.abc
Yury Selivanov21e83a52014-03-27 11:23:13 -040038import enum
Brett Cannoncb66eb02012-05-11 12:58:42 -040039import importlib.machinery
40import itertools
Christian Heimes7131fd92008-02-19 14:21:46 +000041import linecache
Brett Cannoncb66eb02012-05-11 12:58:42 -040042import os
43import re
44import sys
45import tokenize
Larry Hastings2623c8c2014-02-08 22:15:29 -080046import token
Brett Cannoncb66eb02012-05-11 12:58:42 -040047import types
Brett Cannon2b88fcf2012-06-02 22:28:42 -040048import warnings
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070049import functools
Nick Coghlan2f92e542012-06-23 19:39:55 +100050import builtins
Raymond Hettingera1a992c2005-03-11 06:46:45 +000051from operator import attrgetter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070052from collections import namedtuple, OrderedDict
Nick Coghlan09c81232010-08-17 10:18:16 +000053
54# Create constants for the compiler flags in Include/code.h
Antoine Pitroua8723a02015-04-15 00:41:29 +020055# We try to get them from dis to avoid duplication
56mod_dict = globals()
57for k, v in dis.COMPILER_FLAG_NAMES.items():
58 mod_dict["CO_" + v] = k
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000059
Christian Heimesbe5b30b2008-03-03 19:18:51 +000060# See Include/object.h
61TPFLAGS_IS_ABSTRACT = 1 << 20
62
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000063# ----------------------------------------------------------- type-checking
64def ismodule(object):
65 """Return true if the object is a module.
66
67 Module objects provide these attributes:
Barry Warsaw28a691b2010-04-17 00:19:56 +000068 __cached__ pathname to byte compiled file
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000069 __doc__ documentation string
70 __file__ filename (missing for built-in modules)"""
Tim Peters28bc59f2001-09-16 08:40:16 +000071 return isinstance(object, types.ModuleType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000072
73def isclass(object):
74 """Return true if the object is a class.
75
76 Class objects provide these attributes:
77 __doc__ documentation string
78 __module__ name of module in which this class was defined"""
Benjamin Petersonc4656002009-01-17 22:41:18 +000079 return isinstance(object, type)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000080
81def ismethod(object):
82 """Return true if the object is an instance method.
83
84 Instance method objects provide these attributes:
85 __doc__ documentation string
86 __name__ name with which this method was defined
Christian Heimesff737952007-11-27 10:40:20 +000087 __func__ function object containing implementation of method
88 __self__ instance to which this method is bound"""
Tim Peters28bc59f2001-09-16 08:40:16 +000089 return isinstance(object, types.MethodType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000090
Tim Peters536d2262001-09-20 05:13:38 +000091def ismethoddescriptor(object):
Tim Petersf1d90b92001-09-20 05:47:55 +000092 """Return true if the object is a method descriptor.
93
94 But not if ismethod() or isclass() or isfunction() are true.
Tim Peters536d2262001-09-20 05:13:38 +000095
96 This is new in Python 2.2, and, for example, is true of int.__add__.
97 An object passing this test has a __get__ attribute but not a __set__
98 attribute, but beyond that the set of attributes varies. __name__ is
99 usually sensible, and __doc__ often is.
100
Tim Petersf1d90b92001-09-20 05:47:55 +0000101 Methods implemented via descriptors that also pass one of the other
102 tests return false from the ismethoddescriptor() test, simply because
103 the other tests promise more -- you can, e.g., count on having the
Christian Heimesff737952007-11-27 10:40:20 +0000104 __func__ attribute (etc) when an object passes ismethod()."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100105 if isclass(object) or ismethod(object) or isfunction(object):
106 # mutual exclusion
107 return False
108 tp = type(object)
109 return hasattr(tp, "__get__") and not hasattr(tp, "__set__")
Tim Peters536d2262001-09-20 05:13:38 +0000110
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000111def isdatadescriptor(object):
112 """Return true if the object is a data descriptor.
113
114 Data descriptors have both a __get__ and a __set__ attribute. Examples are
115 properties (defined in Python) and getsets and members (defined in C).
116 Typically, data descriptors will also have __name__ and __doc__ attributes
117 (properties, getsets, and members have both of these attributes), but this
118 is not guaranteed."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100119 if isclass(object) or ismethod(object) or isfunction(object):
120 # mutual exclusion
121 return False
122 tp = type(object)
123 return hasattr(tp, "__set__") and hasattr(tp, "__get__")
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000124
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000125if hasattr(types, 'MemberDescriptorType'):
126 # CPython and equivalent
127 def ismemberdescriptor(object):
128 """Return true if the object is a member descriptor.
129
130 Member descriptors are specialized descriptors defined in extension
131 modules."""
132 return isinstance(object, types.MemberDescriptorType)
133else:
134 # Other implementations
135 def ismemberdescriptor(object):
136 """Return true if the object is a member descriptor.
137
138 Member descriptors are specialized descriptors defined in extension
139 modules."""
140 return False
141
142if hasattr(types, 'GetSetDescriptorType'):
143 # CPython and equivalent
144 def isgetsetdescriptor(object):
145 """Return true if the object is a getset descriptor.
146
147 getset descriptors are specialized descriptors defined in extension
148 modules."""
149 return isinstance(object, types.GetSetDescriptorType)
150else:
151 # Other implementations
152 def isgetsetdescriptor(object):
153 """Return true if the object is a getset descriptor.
154
155 getset descriptors are specialized descriptors defined in extension
156 modules."""
157 return False
158
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000159def isfunction(object):
160 """Return true if the object is a user-defined function.
161
162 Function objects provide these attributes:
163 __doc__ documentation string
164 __name__ name with which this function was defined
Neal Norwitz221085d2007-02-25 20:55:47 +0000165 __code__ code object containing compiled function bytecode
166 __defaults__ tuple of any default values for arguments
167 __globals__ global namespace in which this function was defined
168 __annotations__ dict of parameter annotations
169 __kwdefaults__ dict of keyword only parameters with defaults"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000170 return isinstance(object, types.FunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000171
Christian Heimes7131fd92008-02-19 14:21:46 +0000172def isgeneratorfunction(object):
173 """Return true if the object is a user-defined generator function.
174
Martin Panter0f0eac42016-09-07 11:04:41 +0000175 Generator function objects provide the same attributes as functions.
176 See help(isfunction) for a list of attributes."""
Georg Brandlb1441c72009-01-03 22:33:39 +0000177 return bool((isfunction(object) or ismethod(object)) and
Yury Selivanov5376ba92015-06-22 12:19:30 -0400178 object.__code__.co_flags & CO_GENERATOR)
Yury Selivanov75445082015-05-11 22:57:16 -0400179
180def iscoroutinefunction(object):
181 """Return true if the object is a coroutine function.
182
Yury Selivanov4778e132016-11-08 12:23:09 -0500183 Coroutine functions are defined with "async def" syntax.
Yury Selivanov75445082015-05-11 22:57:16 -0400184 """
185 return bool((isfunction(object) or ismethod(object)) and
Yury Selivanov5376ba92015-06-22 12:19:30 -0400186 object.__code__.co_flags & CO_COROUTINE)
Yury Selivanov75445082015-05-11 22:57:16 -0400187
Yury Selivanoveb636452016-09-08 22:01:51 -0700188def isasyncgenfunction(object):
Yury Selivanov4778e132016-11-08 12:23:09 -0500189 """Return true if the object is an asynchronous generator function.
190
191 Asynchronous generator functions are defined with "async def"
192 syntax and have "yield" expressions in their body.
193 """
Yury Selivanoveb636452016-09-08 22:01:51 -0700194 return bool((isfunction(object) or ismethod(object)) and
195 object.__code__.co_flags & CO_ASYNC_GENERATOR)
196
197def isasyncgen(object):
Yury Selivanov4778e132016-11-08 12:23:09 -0500198 """Return true if the object is an asynchronous generator."""
Yury Selivanoveb636452016-09-08 22:01:51 -0700199 return isinstance(object, types.AsyncGeneratorType)
200
Christian Heimes7131fd92008-02-19 14:21:46 +0000201def isgenerator(object):
202 """Return true if the object is a generator.
203
204 Generator objects provide these attributes:
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300205 __iter__ defined to support iteration over container
Christian Heimes7131fd92008-02-19 14:21:46 +0000206 close raises a new GeneratorExit exception inside the
207 generator to terminate the iteration
208 gi_code code object
209 gi_frame frame object or possibly None once the generator has
210 been exhausted
211 gi_running set to 1 when generator is executing, 0 otherwise
212 next return the next item from the container
213 send resumes the generator and "sends" a value that becomes
214 the result of the current yield-expression
215 throw used to raise an exception inside the generator"""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400216 return isinstance(object, types.GeneratorType)
Yury Selivanov75445082015-05-11 22:57:16 -0400217
218def iscoroutine(object):
219 """Return true if the object is a coroutine."""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400220 return isinstance(object, types.CoroutineType)
Christian Heimes7131fd92008-02-19 14:21:46 +0000221
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400222def isawaitable(object):
Yury Selivanovc0215df2016-11-08 19:57:44 -0500223 """Return true if object can be passed to an ``await`` expression."""
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400224 return (isinstance(object, types.CoroutineType) or
225 isinstance(object, types.GeneratorType) and
Yury Selivanovc0215df2016-11-08 19:57:44 -0500226 bool(object.gi_code.co_flags & CO_ITERABLE_COROUTINE) or
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400227 isinstance(object, collections.abc.Awaitable))
228
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000229def istraceback(object):
230 """Return true if the object is a traceback.
231
232 Traceback objects provide these attributes:
233 tb_frame frame object at this level
234 tb_lasti index of last attempted instruction in bytecode
235 tb_lineno current line number in Python source code
236 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000237 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000238
239def isframe(object):
240 """Return true if the object is a frame object.
241
242 Frame objects provide these attributes:
243 f_back next outer frame object (this frame's caller)
244 f_builtins built-in namespace seen by this frame
245 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000246 f_globals global namespace seen by this frame
247 f_lasti index of last attempted instruction in bytecode
248 f_lineno current line number in Python source code
249 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000250 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000251 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000252
253def iscode(object):
254 """Return true if the object is a code object.
255
256 Code objects provide these attributes:
Xiang Zhanga6902e62017-04-13 10:38:28 +0800257 co_argcount number of arguments (not including *, ** args
258 or keyword only arguments)
259 co_code string of raw compiled bytecode
260 co_cellvars tuple of names of cell variables
261 co_consts tuple of constants used in the bytecode
262 co_filename name of file in which this code object was created
263 co_firstlineno number of first line in Python source code
264 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
265 | 16=nested | 32=generator | 64=nofree | 128=coroutine
266 | 256=iterable_coroutine | 512=async_generator
267 co_freevars tuple of names of free variables
268 co_kwonlyargcount number of keyword only arguments (not including ** arg)
269 co_lnotab encoded mapping of line numbers to bytecode indices
270 co_name name with which this code object was defined
271 co_names tuple of names of local variables
272 co_nlocals number of local variables
273 co_stacksize virtual machine stack space required
274 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000275 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000276
277def isbuiltin(object):
278 """Return true if the object is a built-in function or method.
279
280 Built-in functions and methods provide these attributes:
281 __doc__ documentation string
282 __name__ original name of this function or method
283 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000284 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000285
286def isroutine(object):
287 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000288 return (isbuiltin(object)
289 or isfunction(object)
290 or ismethod(object)
291 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000292
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000293def isabstract(object):
294 """Return true if the object is an abstract base class (ABC)."""
Natefcfe80e2017-04-24 10:06:15 -0700295 if not isinstance(object, type):
296 return False
297 if object.__flags__ & TPFLAGS_IS_ABSTRACT:
298 return True
299 if not issubclass(type(object), abc.ABCMeta):
300 return False
301 if hasattr(object, '__abstractmethods__'):
302 # It looks like ABCMeta.__new__ has finished running;
303 # TPFLAGS_IS_ABSTRACT should have been accurate.
304 return False
305 # It looks like ABCMeta.__new__ has not finished running yet; we're
306 # probably in __init_subclass__. We'll look for abstractmethods manually.
307 for name, value in object.__dict__.items():
308 if getattr(value, "__isabstractmethod__", False):
309 return True
310 for base in object.__bases__:
311 for name in getattr(base, "__abstractmethods__", ()):
312 value = getattr(object, name, None)
313 if getattr(value, "__isabstractmethod__", False):
314 return True
315 return False
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000316
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000317def getmembers(object, predicate=None):
318 """Return all members of an object as (name, value) pairs sorted by name.
319 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100320 if isclass(object):
321 mro = (object,) + getmro(object)
322 else:
323 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000324 results = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700325 processed = set()
326 names = dir(object)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700327 # :dd any DynamicClassAttributes to the list of names if object is a class;
Ethan Furmane03ea372013-09-25 07:14:41 -0700328 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700329 # attribute with the same name as a DynamicClassAttribute exists
Ethan Furmane03ea372013-09-25 07:14:41 -0700330 try:
331 for base in object.__bases__:
332 for k, v in base.__dict__.items():
333 if isinstance(v, types.DynamicClassAttribute):
334 names.append(k)
335 except AttributeError:
336 pass
337 for key in names:
Ethan Furman63c141c2013-10-18 00:27:39 -0700338 # First try to get the value via getattr. Some descriptors don't
339 # like calling their __get__ (see bug #1785), so fall back to
340 # looking in the __dict__.
341 try:
342 value = getattr(object, key)
343 # handle the duplicate key
344 if key in processed:
345 raise AttributeError
346 except AttributeError:
347 for base in mro:
348 if key in base.__dict__:
349 value = base.__dict__[key]
350 break
351 else:
352 # could be a (currently) missing slot member, or a buggy
353 # __dir__; discard and move on
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100354 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000355 if not predicate or predicate(value):
356 results.append((key, value))
Ethan Furmane03ea372013-09-25 07:14:41 -0700357 processed.add(key)
358 results.sort(key=lambda pair: pair[0])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000359 return results
360
Christian Heimes25bb7832008-01-11 16:17:00 +0000361Attribute = namedtuple('Attribute', 'name kind defining_class object')
362
Tim Peters13b49d32001-09-23 02:00:29 +0000363def classify_class_attrs(cls):
364 """Return list of attribute-descriptor tuples.
365
366 For each name in dir(cls), the return list contains a 4-tuple
367 with these elements:
368
369 0. The name (a string).
370
371 1. The kind of attribute this is, one of these strings:
372 'class method' created via classmethod()
373 'static method' created via staticmethod()
374 'property' created via property()
Ethan Furmane03ea372013-09-25 07:14:41 -0700375 'method' any other flavor of method or descriptor
Tim Peters13b49d32001-09-23 02:00:29 +0000376 'data' not a method
377
378 2. The class which defined this attribute (a class).
379
Ethan Furmane03ea372013-09-25 07:14:41 -0700380 3. The object as obtained by calling getattr; if this fails, or if the
381 resulting object does not live anywhere in the class' mro (including
382 metaclasses) then the object is looked up in the defining class's
383 dict (found by walking the mro).
Ethan Furman668dede2013-09-14 18:53:26 -0700384
385 If one of the items in dir(cls) is stored in the metaclass it will now
386 be discovered and not have None be listed as the class in which it was
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700387 defined. Any items whose home class cannot be discovered are skipped.
Tim Peters13b49d32001-09-23 02:00:29 +0000388 """
389
390 mro = getmro(cls)
Ethan Furman668dede2013-09-14 18:53:26 -0700391 metamro = getmro(type(cls)) # for attributes stored in the metaclass
Jon Dufresne39726282017-05-18 07:35:54 -0700392 metamro = tuple(cls for cls in metamro if cls not in (type, object))
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700393 class_bases = (cls,) + mro
394 all_bases = class_bases + metamro
Tim Peters13b49d32001-09-23 02:00:29 +0000395 names = dir(cls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700396 # :dd any DynamicClassAttributes to the list of names;
Ethan Furmane03ea372013-09-25 07:14:41 -0700397 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700398 # attribute with the same name as a DynamicClassAttribute exists.
Ethan Furman63c141c2013-10-18 00:27:39 -0700399 for base in mro:
Ethan Furmane03ea372013-09-25 07:14:41 -0700400 for k, v in base.__dict__.items():
401 if isinstance(v, types.DynamicClassAttribute):
402 names.append(k)
Tim Peters13b49d32001-09-23 02:00:29 +0000403 result = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700404 processed = set()
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700405
Tim Peters13b49d32001-09-23 02:00:29 +0000406 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100407 # Get the object associated with the name, and where it was defined.
Ethan Furmane03ea372013-09-25 07:14:41 -0700408 # Normal objects will be looked up with both getattr and directly in
409 # its class' dict (in case getattr fails [bug #1785], and also to look
410 # for a docstring).
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700411 # For DynamicClassAttributes on the second pass we only look in the
Ethan Furmane03ea372013-09-25 07:14:41 -0700412 # class's dict.
413 #
Tim Peters13b49d32001-09-23 02:00:29 +0000414 # Getting an obj from the __dict__ sometimes reveals more than
415 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100416 homecls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700417 get_obj = None
418 dict_obj = None
Ethan Furmane03ea372013-09-25 07:14:41 -0700419 if name not in processed:
420 try:
Ethan Furmana8b07072013-10-18 01:22:08 -0700421 if name == '__dict__':
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700422 raise Exception("__dict__ is special, don't want the proxy")
Ethan Furmane03ea372013-09-25 07:14:41 -0700423 get_obj = getattr(cls, name)
424 except Exception as exc:
425 pass
426 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700427 homecls = getattr(get_obj, "__objclass__", homecls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700428 if homecls not in class_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700429 # if the resulting object does not live somewhere in the
Ethan Furman63c141c2013-10-18 00:27:39 -0700430 # mro, drop it and search the mro manually
Ethan Furmane03ea372013-09-25 07:14:41 -0700431 homecls = None
Ethan Furman63c141c2013-10-18 00:27:39 -0700432 last_cls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700433 # first look in the classes
434 for srch_cls in class_bases:
Ethan Furman63c141c2013-10-18 00:27:39 -0700435 srch_obj = getattr(srch_cls, name, None)
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400436 if srch_obj is get_obj:
Ethan Furman63c141c2013-10-18 00:27:39 -0700437 last_cls = srch_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700438 # then check the metaclasses
439 for srch_cls in metamro:
440 try:
441 srch_obj = srch_cls.__getattr__(cls, name)
442 except AttributeError:
443 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400444 if srch_obj is get_obj:
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700445 last_cls = srch_cls
Ethan Furman63c141c2013-10-18 00:27:39 -0700446 if last_cls is not None:
447 homecls = last_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700448 for base in all_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700449 if name in base.__dict__:
450 dict_obj = base.__dict__[name]
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700451 if homecls not in metamro:
452 homecls = base
Ethan Furmane03ea372013-09-25 07:14:41 -0700453 break
Ethan Furman63c141c2013-10-18 00:27:39 -0700454 if homecls is None:
455 # unable to locate the attribute anywhere, most likely due to
456 # buggy custom __dir__; discard and move on
457 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400458 obj = get_obj if get_obj is not None else dict_obj
Ethan Furmane03ea372013-09-25 07:14:41 -0700459 # Classify the object or its descriptor.
Ethan Furman63c141c2013-10-18 00:27:39 -0700460 if isinstance(dict_obj, staticmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000461 kind = "static method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700462 obj = dict_obj
Ethan Furman63c141c2013-10-18 00:27:39 -0700463 elif isinstance(dict_obj, classmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000464 kind = "class method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700465 obj = dict_obj
466 elif isinstance(dict_obj, property):
Tim Peters13b49d32001-09-23 02:00:29 +0000467 kind = "property"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700468 obj = dict_obj
Yury Selivanov0860a0b2014-01-31 14:28:44 -0500469 elif isroutine(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000470 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100471 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700472 kind = "data"
Christian Heimes25bb7832008-01-11 16:17:00 +0000473 result.append(Attribute(name, kind, homecls, obj))
Ethan Furmane03ea372013-09-25 07:14:41 -0700474 processed.add(name)
Tim Peters13b49d32001-09-23 02:00:29 +0000475 return result
476
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000477# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000478
479def getmro(cls):
480 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000481 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000482
Nick Coghlane8c45d62013-07-28 20:00:01 +1000483# -------------------------------------------------------- function helpers
484
485def unwrap(func, *, stop=None):
486 """Get the object wrapped by *func*.
487
488 Follows the chain of :attr:`__wrapped__` attributes returning the last
489 object in the chain.
490
491 *stop* is an optional callback accepting an object in the wrapper chain
492 as its sole argument that allows the unwrapping to be terminated early if
493 the callback returns a true value. If the callback never returns a true
494 value, the last object in the chain is returned as usual. For example,
495 :func:`signature` uses this to stop unwrapping if any object in the
496 chain has a ``__signature__`` attribute defined.
497
498 :exc:`ValueError` is raised if a cycle is encountered.
499
500 """
501 if stop is None:
502 def _is_wrapper(f):
503 return hasattr(f, '__wrapped__')
504 else:
505 def _is_wrapper(f):
506 return hasattr(f, '__wrapped__') and not stop(f)
507 f = func # remember the original func for error reporting
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100508 # Memoise by id to tolerate non-hashable objects, but store objects to
509 # ensure they aren't destroyed, which would allow their IDs to be reused.
510 memo = {id(f): f}
511 recursion_limit = sys.getrecursionlimit()
Nick Coghlane8c45d62013-07-28 20:00:01 +1000512 while _is_wrapper(func):
513 func = func.__wrapped__
514 id_func = id(func)
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100515 if (id_func in memo) or (len(memo) >= recursion_limit):
Nick Coghlane8c45d62013-07-28 20:00:01 +1000516 raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100517 memo[id_func] = func
Nick Coghlane8c45d62013-07-28 20:00:01 +1000518 return func
519
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000520# -------------------------------------------------- source code extraction
521def indentsize(line):
522 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000523 expline = line.expandtabs()
524 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000525
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300526def _findclass(func):
527 cls = sys.modules.get(func.__module__)
528 if cls is None:
529 return None
530 for name in func.__qualname__.split('.')[:-1]:
531 cls = getattr(cls, name)
532 if not isclass(cls):
533 return None
534 return cls
535
536def _finddoc(obj):
537 if isclass(obj):
538 for base in obj.__mro__:
539 if base is not object:
540 try:
541 doc = base.__doc__
542 except AttributeError:
543 continue
544 if doc is not None:
545 return doc
546 return None
547
548 if ismethod(obj):
549 name = obj.__func__.__name__
550 self = obj.__self__
551 if (isclass(self) and
552 getattr(getattr(self, name, None), '__func__') is obj.__func__):
553 # classmethod
554 cls = self
555 else:
556 cls = self.__class__
557 elif isfunction(obj):
558 name = obj.__name__
559 cls = _findclass(obj)
560 if cls is None or getattr(cls, name) is not obj:
561 return None
562 elif isbuiltin(obj):
563 name = obj.__name__
564 self = obj.__self__
565 if (isclass(self) and
566 self.__qualname__ + '.' + name == obj.__qualname__):
567 # classmethod
568 cls = self
569 else:
570 cls = self.__class__
Serhiy Storchakaac4bdcc2015-10-29 08:15:50 +0200571 # Should be tested before isdatadescriptor().
572 elif isinstance(obj, property):
573 func = obj.fget
574 name = func.__name__
575 cls = _findclass(func)
576 if cls is None or getattr(cls, name) is not obj:
577 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300578 elif ismethoddescriptor(obj) or isdatadescriptor(obj):
579 name = obj.__name__
580 cls = obj.__objclass__
581 if getattr(cls, name) is not obj:
582 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300583 else:
584 return None
585
586 for base in cls.__mro__:
587 try:
588 doc = getattr(base, name).__doc__
589 except AttributeError:
590 continue
591 if doc is not None:
592 return doc
593 return None
594
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000595def getdoc(object):
596 """Get the documentation string for an object.
597
598 All tabs are expanded to spaces. To clean up docstrings that are
599 indented to line up with blocks of code, any whitespace than can be
600 uniformly removed from the second line onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000601 try:
602 doc = object.__doc__
603 except AttributeError:
604 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300605 if doc is None:
606 try:
607 doc = _finddoc(object)
608 except (AttributeError, TypeError):
609 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000610 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000611 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000612 return cleandoc(doc)
613
614def cleandoc(doc):
615 """Clean up indentation from docstrings.
616
617 Any whitespace that can be uniformly removed from the second line
618 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000619 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000620 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000621 except UnicodeError:
622 return None
623 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000624 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000625 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000626 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000627 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000628 if content:
629 indent = len(line) - content
630 margin = min(margin, indent)
631 # Remove indentation.
632 if lines:
633 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000634 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000635 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000636 # Remove any trailing or leading blank lines.
637 while lines and not lines[-1]:
638 lines.pop()
639 while lines and not lines[0]:
640 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000641 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000642
643def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000644 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000645 if ismodule(object):
646 if hasattr(object, '__file__'):
647 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000648 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000649 if isclass(object):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500650 if hasattr(object, '__module__'):
651 object = sys.modules.get(object.__module__)
652 if hasattr(object, '__file__'):
653 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000654 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000655 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000656 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000657 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000658 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000659 if istraceback(object):
660 object = object.tb_frame
661 if isframe(object):
662 object = object.f_code
663 if iscode(object):
664 return object.co_filename
Thomas Kluyvere968bc732017-10-24 13:42:36 +0100665 raise TypeError('module, class, method, function, traceback, frame, or '
666 'code object was expected, got {}'.format(
667 type(object).__name__))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000668
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000669def getmodulename(path):
670 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000671 fname = os.path.basename(path)
672 # Check for paths that look like an actual module file
673 suffixes = [(-len(suffix), suffix)
674 for suffix in importlib.machinery.all_suffixes()]
675 suffixes.sort() # try longest suffixes first, in case they overlap
676 for neglen, suffix in suffixes:
677 if fname.endswith(suffix):
678 return fname[:neglen]
679 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000680
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000681def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000682 """Return the filename that can be used to locate an object's source.
683 Return None if no way can be identified to get the source.
684 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000685 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400686 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
687 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
688 if any(filename.endswith(s) for s in all_bytecode_suffixes):
689 filename = (os.path.splitext(filename)[0] +
690 importlib.machinery.SOURCE_SUFFIXES[0])
691 elif any(filename.endswith(s) for s in
692 importlib.machinery.EXTENSION_SUFFIXES):
693 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000694 if os.path.exists(filename):
695 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000696 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400697 if getattr(getmodule(object, filename), '__loader__', None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000698 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000699 # or it is in the linecache
700 if filename in linecache.cache:
701 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000702
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000703def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000704 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000705
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000706 The idea is for each object to have a unique origin, so this routine
707 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000708 if _filename is None:
709 _filename = getsourcefile(object) or getfile(object)
710 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000711
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000712modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000713_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000714
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000715def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000716 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000717 if ismodule(object):
718 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000719 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000720 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000721 # Try the filename to modulename cache
722 if _filename is not None and _filename in modulesbyfile:
723 return sys.modules.get(modulesbyfile[_filename])
724 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000725 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000726 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000727 except TypeError:
728 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000729 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000730 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000731 # Update the filename to module name cache and check yet again
732 # Copy sys.modules in order to cope with changes while iterating
Éric Araujoa74f8ef2011-11-29 16:58:53 +0100733 for modname, module in list(sys.modules.items()):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000734 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000735 f = module.__file__
736 if f == _filesbymodname.get(modname, None):
737 # Have already mapped this module, so skip it
738 continue
739 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000740 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000741 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000742 modulesbyfile[f] = modulesbyfile[
743 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000744 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000745 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000746 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000747 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000748 if not hasattr(object, '__name__'):
749 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000750 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000751 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000752 if mainobject is object:
753 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000754 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000755 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000756 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000757 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000758 if builtinobject is object:
759 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000760
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000761def findsource(object):
762 """Return the entire source file and starting line number for an object.
763
764 The argument may be a module, class, method, function, traceback, frame,
765 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200766 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000767 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500768
Yury Selivanovef1e7502014-12-08 16:05:34 -0500769 file = getsourcefile(object)
770 if file:
771 # Invalidate cache if needed.
772 linecache.checkcache(file)
773 else:
774 file = getfile(object)
775 # Allow filenames in form of "<something>" to pass through.
776 # `doctest` monkeypatches `linecache` module to enable
777 # inspection, so let `linecache.getlines` to be called.
778 if not (file.startswith('<') and file.endswith('>')):
779 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500780
Thomas Wouters89f507f2006-12-13 04:49:30 +0000781 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000782 if module:
783 lines = linecache.getlines(file, module.__dict__)
784 else:
785 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000786 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200787 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000788
789 if ismodule(object):
790 return lines, 0
791
792 if isclass(object):
793 name = object.__name__
Thomas Wouters89f507f2006-12-13 04:49:30 +0000794 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
795 # make some effort to find the best matching class definition:
796 # use the one with the least indentation, which is the one
797 # that's most probably not inside a function definition.
798 candidates = []
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000799 for i in range(len(lines)):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000800 match = pat.match(lines[i])
801 if match:
802 # if it's at toplevel, it's already the best one
803 if lines[i][0] == 'c':
804 return lines, i
805 # else add whitespace to candidate list
806 candidates.append((match.group(1), i))
807 if candidates:
808 # this will sort by whitespace, and by line number,
809 # less whitespace first
810 candidates.sort()
811 return lines, candidates[0][1]
Jeremy Hyltonab919022003-06-27 18:41:20 +0000812 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200813 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000814
815 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000816 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000817 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000818 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000819 if istraceback(object):
820 object = object.tb_frame
821 if isframe(object):
822 object = object.f_code
823 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000824 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200825 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000826 lnum = object.co_firstlineno - 1
Yury Selivanove4e811d2015-07-21 19:01:52 +0300827 pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000828 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000829 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000830 lnum = lnum - 1
831 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200832 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000833
834def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000835 """Get lines of comments immediately preceding an object's source code.
836
837 Returns None when source can't be found.
838 """
839 try:
840 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200841 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000842 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000843
844 if ismodule(object):
845 # Look for a comment block at the top of the file.
846 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000847 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000848 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000849 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000850 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000851 comments = []
852 end = start
853 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000854 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000855 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000856 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000857
858 # Look for a preceding block of comments at the same indentation.
859 elif lnum > 0:
860 indent = indentsize(lines[lnum])
861 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000862 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000863 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000864 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000865 if end > 0:
866 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000867 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000868 while comment[:1] == '#' and indentsize(lines[end]) == indent:
869 comments[:0] = [comment]
870 end = end - 1
871 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000872 comment = lines[end].expandtabs().lstrip()
873 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000874 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000875 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000876 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000877 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000878
Tim Peters4efb6e92001-06-29 23:51:08 +0000879class EndOfBlock(Exception): pass
880
881class BlockFinder:
882 """Provide a tokeneater() method to detect the end of a code block."""
883 def __init__(self):
884 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000885 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000886 self.started = False
887 self.passline = False
Meador Inge5b718d72015-07-23 22:49:37 -0500888 self.indecorator = False
889 self.decoratorhasargs = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000890 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000891
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000892 def tokeneater(self, type, token, srowcol, erowcol, line):
Meador Inge5b718d72015-07-23 22:49:37 -0500893 if not self.started and not self.indecorator:
894 # skip any decorators
895 if token == "@":
896 self.indecorator = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000897 # look for the first "def", "class" or "lambda"
Meador Inge5b718d72015-07-23 22:49:37 -0500898 elif token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000899 if token == "lambda":
900 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000901 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000902 self.passline = True # skip to the end of the line
Meador Inge5b718d72015-07-23 22:49:37 -0500903 elif token == "(":
904 if self.indecorator:
905 self.decoratorhasargs = True
906 elif token == ")":
907 if self.indecorator:
908 self.indecorator = False
909 self.decoratorhasargs = False
Tim Peters4efb6e92001-06-29 23:51:08 +0000910 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000911 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000912 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000913 if self.islambda: # lambdas always end at the first NEWLINE
914 raise EndOfBlock
Meador Inge5b718d72015-07-23 22:49:37 -0500915 # hitting a NEWLINE when in a decorator without args
916 # ends the decorator
917 if self.indecorator and not self.decoratorhasargs:
918 self.indecorator = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000919 elif self.passline:
920 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000921 elif type == tokenize.INDENT:
922 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000923 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000924 elif type == tokenize.DEDENT:
925 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000926 # the end of matching indent/dedent pairs end a block
927 # (note that this only works for "def"/"class" blocks,
928 # not e.g. for "if: else:" or "try: finally:" blocks)
929 if self.indent <= 0:
930 raise EndOfBlock
931 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
932 # any other token on the same indentation level end the previous
933 # block as well, except the pseudo-tokens COMMENT and NL.
934 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000935
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000936def getblock(lines):
937 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000938 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000939 try:
Trent Nelson428de652008-03-18 22:41:35 +0000940 tokens = tokenize.generate_tokens(iter(lines).__next__)
941 for _token in tokens:
942 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000943 except (EndOfBlock, IndentationError):
944 pass
945 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000946
947def getsourcelines(object):
948 """Return a list of source lines and starting line number for an object.
949
950 The argument may be a module, class, method, function, traceback, frame,
951 or code object. The source code is returned as a list of the lines
952 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200953 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000954 raised if the source code cannot be retrieved."""
Yury Selivanov081bbf62014-09-26 17:34:54 -0400955 object = unwrap(object)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000956 lines, lnum = findsource(object)
957
Meador Inge5b718d72015-07-23 22:49:37 -0500958 if ismodule(object):
959 return lines, 0
960 else:
961 return getblock(lines[lnum:]), lnum + 1
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000962
963def getsource(object):
964 """Return the text of the source code for an object.
965
966 The argument may be a module, class, method, function, traceback, frame,
967 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200968 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000969 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000970 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000971
972# --------------------------------------------------- class tree extraction
973def walktree(classes, children, parent):
974 """Recursive helper function for getclasstree()."""
975 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +0000976 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000977 for c in classes:
978 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +0000979 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000980 results.append(walktree(children[c], children, c))
981 return results
982
Georg Brandl5ce83a02009-06-01 17:23:51 +0000983def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000984 """Arrange the given list of classes into a hierarchy of nested lists.
985
986 Where a nested list appears, it contains classes derived from the class
987 whose entry immediately precedes the list. Each entry is a 2-tuple
988 containing a class and a tuple of its base classes. If the 'unique'
989 argument is true, exactly one entry appears in the returned structure
990 for each class in the given list. Otherwise, classes using multiple
991 inheritance and their descendants will appear multiple times."""
992 children = {}
993 roots = []
994 for c in classes:
995 if c.__bases__:
996 for parent in c.__bases__:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000997 if not parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000998 children[parent] = []
Serhiy Storchaka362c1b52013-09-05 17:14:32 +0300999 if c not in children[parent]:
1000 children[parent].append(c)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001001 if unique and parent in classes: break
1002 elif c not in roots:
1003 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +00001004 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001005 if parent not in classes:
1006 roots.append(parent)
1007 return walktree(roots, children, None)
1008
1009# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001010Arguments = namedtuple('Arguments', 'args, varargs, varkw')
1011
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001012def getargs(co):
1013 """Get information about the arguments accepted by a code object.
1014
Guido van Rossum2e65f892007-02-28 22:03:49 +00001015 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001016 'args' is the list of argument names. Keyword-only arguments are
1017 appended. 'varargs' and 'varkw' are the names of the * and **
1018 arguments or None."""
Guido van Rossum2e65f892007-02-28 22:03:49 +00001019 args, varargs, kwonlyargs, varkw = _getfullargs(co)
Christian Heimes25bb7832008-01-11 16:17:00 +00001020 return Arguments(args + kwonlyargs, varargs, varkw)
Guido van Rossum2e65f892007-02-28 22:03:49 +00001021
1022def _getfullargs(co):
1023 """Get information about the arguments accepted by a code object.
1024
1025 Four things are returned: (args, varargs, kwonlyargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001026 'args' and 'kwonlyargs' are lists of argument names, and 'varargs'
1027 and 'varkw' are the names of the * and ** arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +00001028
1029 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001030 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001031
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001032 nargs = co.co_argcount
1033 names = co.co_varnames
Guido van Rossum2e65f892007-02-28 22:03:49 +00001034 nkwargs = co.co_kwonlyargcount
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001035 args = list(names[:nargs])
Guido van Rossum2e65f892007-02-28 22:03:49 +00001036 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001037 step = 0
1038
Guido van Rossum2e65f892007-02-28 22:03:49 +00001039 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001040 varargs = None
1041 if co.co_flags & CO_VARARGS:
1042 varargs = co.co_varnames[nargs]
1043 nargs = nargs + 1
1044 varkw = None
1045 if co.co_flags & CO_VARKEYWORDS:
1046 varkw = co.co_varnames[nargs]
Guido van Rossum2e65f892007-02-28 22:03:49 +00001047 return args, varargs, kwonlyargs, varkw
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001048
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001049
1050ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
1051
1052def getargspec(func):
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001053 """Get the names and default values of a function's parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001054
1055 A tuple of four things is returned: (args, varargs, keywords, defaults).
1056 'args' is a list of the argument names, including keyword-only argument names.
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001057 'varargs' and 'keywords' are the names of the * and ** parameters or None.
1058 'defaults' is an n-tuple of the default values of the last n parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001059
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001060 This function is deprecated, as it does not support annotations or
1061 keyword-only parameters and will raise ValueError if either is present
1062 on the supplied callable.
1063
1064 For a more structured introspection API, use inspect.signature() instead.
1065
1066 Alternatively, use getfullargspec() for an API with a similar namedtuple
1067 based interface, but full support for annotations and keyword-only
1068 parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001069 """
1070 warnings.warn("inspect.getargspec() is deprecated, "
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001071 "use inspect.signature() or inspect.getfullargspec()",
1072 DeprecationWarning, stacklevel=2)
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001073 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
1074 getfullargspec(func)
1075 if kwonlyargs or ann:
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001076 raise ValueError("Function has keyword-only parameters or annotations"
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001077 ", use getfullargspec() API which can support them")
1078 return ArgSpec(args, varargs, varkw, defaults)
1079
Christian Heimes25bb7832008-01-11 16:17:00 +00001080FullArgSpec = namedtuple('FullArgSpec',
Benjamin Peterson3d4ca742008-11-12 21:39:01 +00001081 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001082
1083def getfullargspec(func):
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001084 """Get the names and default values of a callable object's parameters.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001085
Brett Cannon504d8852007-09-07 02:12:14 +00001086 A tuple of seven things is returned:
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001087 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations).
1088 'args' is a list of the parameter names.
1089 'varargs' and 'varkw' are the names of the * and ** parameters or None.
1090 'defaults' is an n-tuple of the default values of the last n parameters.
1091 'kwonlyargs' is a list of keyword-only parameter names.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001092 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001093 'annotations' is a dictionary mapping parameter names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001094
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001095 Notable differences from inspect.signature():
1096 - the "self" parameter is always reported, even for bound methods
1097 - wrapper chains defined by __wrapped__ *not* unwrapped automatically
Jeremy Hylton64967882003-06-27 18:14:39 +00001098 """
1099
Yury Selivanov57d240e2014-02-19 16:27:23 -05001100 try:
1101 # Re: `skip_bound_arg=False`
1102 #
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001103 # There is a notable difference in behaviour between getfullargspec
1104 # and Signature: the former always returns 'self' parameter for bound
1105 # methods, whereas the Signature always shows the actual calling
1106 # signature of the passed object.
1107 #
1108 # To simulate this behaviour, we "unbind" bound methods, to trick
1109 # inspect.signature to always return their first parameter ("self",
1110 # usually)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001111
Yury Selivanov57d240e2014-02-19 16:27:23 -05001112 # Re: `follow_wrapper_chains=False`
1113 #
1114 # getfullargspec() historically ignored __wrapped__ attributes,
1115 # so we ensure that remains the case in 3.3+
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001116
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001117 sig = _signature_from_callable(func,
1118 follow_wrapper_chains=False,
1119 skip_bound_arg=False,
1120 sigcls=Signature)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001121 except Exception as ex:
1122 # Most of the times 'signature' will raise ValueError.
1123 # But, it can also raise AttributeError, and, maybe something
1124 # else. So to be fully backwards compatible, we catch all
1125 # possible exceptions here, and reraise a TypeError.
1126 raise TypeError('unsupported callable') from ex
1127
1128 args = []
1129 varargs = None
1130 varkw = None
1131 kwonlyargs = []
1132 defaults = ()
1133 annotations = {}
1134 defaults = ()
1135 kwdefaults = {}
1136
1137 if sig.return_annotation is not sig.empty:
1138 annotations['return'] = sig.return_annotation
1139
1140 for param in sig.parameters.values():
1141 kind = param.kind
1142 name = param.name
1143
1144 if kind is _POSITIONAL_ONLY:
1145 args.append(name)
1146 elif kind is _POSITIONAL_OR_KEYWORD:
1147 args.append(name)
1148 if param.default is not param.empty:
1149 defaults += (param.default,)
1150 elif kind is _VAR_POSITIONAL:
1151 varargs = name
1152 elif kind is _KEYWORD_ONLY:
1153 kwonlyargs.append(name)
1154 if param.default is not param.empty:
1155 kwdefaults[name] = param.default
1156 elif kind is _VAR_KEYWORD:
1157 varkw = name
1158
1159 if param.annotation is not param.empty:
1160 annotations[name] = param.annotation
1161
1162 if not kwdefaults:
1163 # compatibility with 'func.__kwdefaults__'
1164 kwdefaults = None
1165
1166 if not defaults:
1167 # compatibility with 'func.__defaults__'
1168 defaults = None
1169
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001170 return FullArgSpec(args, varargs, varkw, defaults,
1171 kwonlyargs, kwdefaults, annotations)
1172
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001173
Christian Heimes25bb7832008-01-11 16:17:00 +00001174ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
1175
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001176def getargvalues(frame):
1177 """Get information about arguments passed into a particular frame.
1178
1179 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001180 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001181 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1182 'locals' is the locals dictionary of the given frame."""
1183 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +00001184 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001185
Guido van Rossum2e65f892007-02-28 22:03:49 +00001186def formatannotation(annotation, base_module=None):
Guido van Rossum52e50042016-10-22 07:55:18 -07001187 if getattr(annotation, '__module__', None) == 'typing':
1188 return repr(annotation).replace('typing.', '')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001189 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001190 if annotation.__module__ in ('builtins', base_module):
Serhiy Storchaka521e5862014-07-22 15:00:37 +03001191 return annotation.__qualname__
1192 return annotation.__module__+'.'+annotation.__qualname__
Guido van Rossum2e65f892007-02-28 22:03:49 +00001193 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001194
Guido van Rossum2e65f892007-02-28 22:03:49 +00001195def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001196 module = getattr(object, '__module__', None)
1197 def _formatannotation(annotation):
1198 return formatannotation(annotation, module)
1199 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001200
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001201def formatargspec(args, varargs=None, varkw=None, defaults=None,
Guido van Rossum2e65f892007-02-28 22:03:49 +00001202 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001203 formatarg=str,
1204 formatvarargs=lambda name: '*' + name,
1205 formatvarkw=lambda name: '**' + name,
1206 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001207 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001208 formatannotation=formatannotation):
Berker Peksagfa3922c2015-07-31 04:11:29 +03001209 """Format an argument spec from the values returned by getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001210
Guido van Rossum2e65f892007-02-28 22:03:49 +00001211 The first seven arguments are (args, varargs, varkw, defaults,
1212 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1213 are the corresponding optional formatting functions that are called to
1214 turn names and values into strings. The last argument is an optional
1215 function to format the sequence of arguments."""
1216 def formatargandannotation(arg):
1217 result = formatarg(arg)
1218 if arg in annotations:
1219 result += ': ' + formatannotation(annotations[arg])
1220 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001221 specs = []
1222 if defaults:
1223 firstdefault = len(args) - len(defaults)
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001224 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001225 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001226 if defaults and i >= firstdefault:
1227 spec = spec + formatvalue(defaults[i - firstdefault])
1228 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001229 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001230 specs.append(formatvarargs(formatargandannotation(varargs)))
1231 else:
1232 if kwonlyargs:
1233 specs.append('*')
1234 if kwonlyargs:
1235 for kwonlyarg in kwonlyargs:
1236 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001237 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001238 spec += formatvalue(kwonlydefaults[kwonlyarg])
1239 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001240 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001241 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001242 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001243 if 'return' in annotations:
1244 result += formatreturns(formatannotation(annotations['return']))
1245 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001246
1247def formatargvalues(args, varargs, varkw, locals,
1248 formatarg=str,
1249 formatvarargs=lambda name: '*' + name,
1250 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001251 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001252 """Format an argument spec from the 4 values returned by getargvalues.
1253
1254 The first four arguments are (args, varargs, varkw, locals). The
1255 next four arguments are the corresponding optional formatting functions
1256 that are called to turn names and values into strings. The ninth
1257 argument is an optional function to format the sequence of arguments."""
1258 def convert(name, locals=locals,
1259 formatarg=formatarg, formatvalue=formatvalue):
1260 return formatarg(name) + formatvalue(locals[name])
1261 specs = []
1262 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001263 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001264 if varargs:
1265 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1266 if varkw:
1267 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001268 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001269
Benjamin Petersone109c702011-06-24 09:37:26 -05001270def _missing_arguments(f_name, argnames, pos, values):
1271 names = [repr(name) for name in argnames if name not in values]
1272 missing = len(names)
1273 if missing == 1:
1274 s = names[0]
1275 elif missing == 2:
1276 s = "{} and {}".format(*names)
1277 else:
Yury Selivanovdccfa132014-03-27 18:42:52 -04001278 tail = ", {} and {}".format(*names[-2:])
Benjamin Petersone109c702011-06-24 09:37:26 -05001279 del names[-2:]
1280 s = ", ".join(names) + tail
1281 raise TypeError("%s() missing %i required %s argument%s: %s" %
1282 (f_name, missing,
1283 "positional" if pos else "keyword-only",
1284 "" if missing == 1 else "s", s))
1285
1286def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001287 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001288 kwonly_given = len([arg for arg in kwonly if arg in values])
1289 if varargs:
1290 plural = atleast != 1
1291 sig = "at least %d" % (atleast,)
1292 elif defcount:
1293 plural = True
1294 sig = "from %d to %d" % (atleast, len(args))
1295 else:
1296 plural = len(args) != 1
1297 sig = str(len(args))
1298 kwonly_sig = ""
1299 if kwonly_given:
1300 msg = " positional argument%s (and %d keyword-only argument%s)"
1301 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1302 "s" if kwonly_given != 1 else ""))
1303 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1304 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1305 "was" if given == 1 and not kwonly_given else "were"))
1306
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001307def getcallargs(*func_and_positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001308 """Get the mapping of arguments to values.
1309
1310 A dict is returned, with keys the function argument names (including the
1311 names of the * and ** arguments, if any), and values the respective bound
1312 values from 'positional' and 'named'."""
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001313 func = func_and_positional[0]
1314 positional = func_and_positional[1:]
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001315 spec = getfullargspec(func)
1316 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
1317 f_name = func.__name__
1318 arg2value = {}
1319
Benjamin Petersonb204a422011-06-05 22:04:07 -05001320
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001321 if ismethod(func) and func.__self__ is not None:
1322 # implicit 'self' (or 'cls' for classmethods) argument
1323 positional = (func.__self__,) + positional
1324 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001325 num_args = len(args)
1326 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001327
Benjamin Petersonb204a422011-06-05 22:04:07 -05001328 n = min(num_pos, num_args)
1329 for i in range(n):
1330 arg2value[args[i]] = positional[i]
1331 if varargs:
1332 arg2value[varargs] = tuple(positional[n:])
1333 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001334 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001335 arg2value[varkw] = {}
1336 for kw, value in named.items():
1337 if kw not in possible_kwargs:
1338 if not varkw:
1339 raise TypeError("%s() got an unexpected keyword argument %r" %
1340 (f_name, kw))
1341 arg2value[varkw][kw] = value
1342 continue
1343 if kw in arg2value:
1344 raise TypeError("%s() got multiple values for argument %r" %
1345 (f_name, kw))
1346 arg2value[kw] = value
1347 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001348 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1349 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001350 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001351 req = args[:num_args - num_defaults]
1352 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001353 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001354 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001355 for i, arg in enumerate(args[num_args - num_defaults:]):
1356 if arg not in arg2value:
1357 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001358 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001359 for kwarg in kwonlyargs:
1360 if kwarg not in arg2value:
Yury Selivanov875df202014-03-27 18:23:03 -04001361 if kwonlydefaults and kwarg in kwonlydefaults:
Benjamin Petersone109c702011-06-24 09:37:26 -05001362 arg2value[kwarg] = kwonlydefaults[kwarg]
1363 else:
1364 missing += 1
1365 if missing:
1366 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001367 return arg2value
1368
Nick Coghlan2f92e542012-06-23 19:39:55 +10001369ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1370
1371def getclosurevars(func):
1372 """
1373 Get the mapping of free variables to their current values.
1374
Meador Inge8fda3592012-07-19 21:33:21 -05001375 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001376 and builtin references as seen by the body of the function. A final
1377 set of unbound names that could not be resolved is also provided.
1378 """
1379
1380 if ismethod(func):
1381 func = func.__func__
1382
1383 if not isfunction(func):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001384 raise TypeError("{!r} is not a Python function".format(func))
Nick Coghlan2f92e542012-06-23 19:39:55 +10001385
1386 code = func.__code__
1387 # Nonlocal references are named in co_freevars and resolved
1388 # by looking them up in __closure__ by positional index
1389 if func.__closure__ is None:
1390 nonlocal_vars = {}
1391 else:
1392 nonlocal_vars = {
1393 var : cell.cell_contents
1394 for var, cell in zip(code.co_freevars, func.__closure__)
1395 }
1396
1397 # Global and builtin references are named in co_names and resolved
1398 # by looking them up in __globals__ or __builtins__
1399 global_ns = func.__globals__
1400 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1401 if ismodule(builtin_ns):
1402 builtin_ns = builtin_ns.__dict__
1403 global_vars = {}
1404 builtin_vars = {}
1405 unbound_names = set()
1406 for name in code.co_names:
1407 if name in ("None", "True", "False"):
1408 # Because these used to be builtins instead of keywords, they
1409 # may still show up as name references. We ignore them.
1410 continue
1411 try:
1412 global_vars[name] = global_ns[name]
1413 except KeyError:
1414 try:
1415 builtin_vars[name] = builtin_ns[name]
1416 except KeyError:
1417 unbound_names.add(name)
1418
1419 return ClosureVars(nonlocal_vars, global_vars,
1420 builtin_vars, unbound_names)
1421
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001422# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001423
1424Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1425
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001426def getframeinfo(frame, context=1):
1427 """Get information about a frame or traceback object.
1428
1429 A tuple of five things is returned: the filename, the line number of
1430 the current line, the function name, a list of lines of context from
1431 the source code, and the index of the current line within that list.
1432 The optional second argument specifies the number of lines of context
1433 to return, which are centered around the current line."""
1434 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001435 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001436 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001437 else:
1438 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001439 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001440 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001441
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001442 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001443 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001444 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001445 try:
1446 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001447 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001448 lines = index = None
1449 else:
Raymond Hettingera0501712004-06-15 11:22:53 +00001450 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001451 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001452 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001453 else:
1454 lines = index = None
1455
Christian Heimes25bb7832008-01-11 16:17:00 +00001456 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001457
1458def getlineno(frame):
1459 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001460 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1461 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001462
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001463FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
1464
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001465def getouterframes(frame, context=1):
1466 """Get a list of records for a frame and all higher (calling) frames.
1467
1468 Each record contains a frame object, filename, line number, function
1469 name, a list of lines of context, and index within the context."""
1470 framelist = []
1471 while frame:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001472 frameinfo = (frame,) + getframeinfo(frame, context)
1473 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001474 frame = frame.f_back
1475 return framelist
1476
1477def getinnerframes(tb, context=1):
1478 """Get a list of records for a traceback's frame and all lower frames.
1479
1480 Each record contains a frame object, filename, line number, function
1481 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001482 framelist = []
1483 while tb:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001484 frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
1485 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001486 tb = tb.tb_next
1487 return framelist
1488
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001489def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001490 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001491 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001492
1493def stack(context=1):
1494 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001495 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001496
1497def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001498 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001499 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001500
1501
1502# ------------------------------------------------ static version of getattr
1503
1504_sentinel = object()
1505
Michael Foorde5162652010-11-20 16:40:44 +00001506def _static_getmro(klass):
1507 return type.__dict__['__mro__'].__get__(klass)
1508
Michael Foord95fc51d2010-11-20 15:07:30 +00001509def _check_instance(obj, attr):
1510 instance_dict = {}
1511 try:
1512 instance_dict = object.__getattribute__(obj, "__dict__")
1513 except AttributeError:
1514 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001515 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001516
1517
1518def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001519 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001520 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001521 try:
1522 return entry.__dict__[attr]
1523 except KeyError:
1524 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001525 return _sentinel
1526
Michael Foord35184ed2010-11-20 16:58:30 +00001527def _is_type(obj):
1528 try:
1529 _static_getmro(obj)
1530 except TypeError:
1531 return False
1532 return True
1533
Michael Foorddcebe0f2011-03-15 19:20:44 -04001534def _shadowed_dict(klass):
1535 dict_attr = type.__dict__["__dict__"]
1536 for entry in _static_getmro(klass):
1537 try:
1538 class_dict = dict_attr.__get__(entry)["__dict__"]
1539 except KeyError:
1540 pass
1541 else:
1542 if not (type(class_dict) is types.GetSetDescriptorType and
1543 class_dict.__name__ == "__dict__" and
1544 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001545 return class_dict
1546 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001547
1548def getattr_static(obj, attr, default=_sentinel):
1549 """Retrieve attributes without triggering dynamic lookup via the
1550 descriptor protocol, __getattr__ or __getattribute__.
1551
1552 Note: this function may not be able to retrieve all attributes
1553 that getattr can fetch (like dynamically created attributes)
1554 and may find attributes that getattr can't (like descriptors
1555 that raise AttributeError). It can also return descriptor objects
1556 instead of instance members in some cases. See the
1557 documentation for details.
1558 """
1559 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001560 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001561 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001562 dict_attr = _shadowed_dict(klass)
1563 if (dict_attr is _sentinel or
1564 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001565 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001566 else:
1567 klass = obj
1568
1569 klass_result = _check_class(klass, attr)
1570
1571 if instance_result is not _sentinel and klass_result is not _sentinel:
1572 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1573 _check_class(type(klass_result), '__set__') is not _sentinel):
1574 return klass_result
1575
1576 if instance_result is not _sentinel:
1577 return instance_result
1578 if klass_result is not _sentinel:
1579 return klass_result
1580
1581 if obj is klass:
1582 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001583 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001584 if _shadowed_dict(type(entry)) is _sentinel:
1585 try:
1586 return entry.__dict__[attr]
1587 except KeyError:
1588 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001589 if default is not _sentinel:
1590 return default
1591 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001592
1593
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001594# ------------------------------------------------ generator introspection
1595
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001596GEN_CREATED = 'GEN_CREATED'
1597GEN_RUNNING = 'GEN_RUNNING'
1598GEN_SUSPENDED = 'GEN_SUSPENDED'
1599GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001600
1601def getgeneratorstate(generator):
1602 """Get current state of a generator-iterator.
1603
1604 Possible states are:
1605 GEN_CREATED: Waiting to start execution.
1606 GEN_RUNNING: Currently being executed by the interpreter.
1607 GEN_SUSPENDED: Currently suspended at a yield expression.
1608 GEN_CLOSED: Execution has completed.
1609 """
1610 if generator.gi_running:
1611 return GEN_RUNNING
1612 if generator.gi_frame is None:
1613 return GEN_CLOSED
1614 if generator.gi_frame.f_lasti == -1:
1615 return GEN_CREATED
1616 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001617
1618
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001619def getgeneratorlocals(generator):
1620 """
1621 Get the mapping of generator local variables to their current values.
1622
1623 A dict is returned, with the keys the local variable names and values the
1624 bound values."""
1625
1626 if not isgenerator(generator):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001627 raise TypeError("{!r} is not a Python generator".format(generator))
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001628
1629 frame = getattr(generator, "gi_frame", None)
1630 if frame is not None:
1631 return generator.gi_frame.f_locals
1632 else:
1633 return {}
1634
Yury Selivanov5376ba92015-06-22 12:19:30 -04001635
1636# ------------------------------------------------ coroutine introspection
1637
1638CORO_CREATED = 'CORO_CREATED'
1639CORO_RUNNING = 'CORO_RUNNING'
1640CORO_SUSPENDED = 'CORO_SUSPENDED'
1641CORO_CLOSED = 'CORO_CLOSED'
1642
1643def getcoroutinestate(coroutine):
1644 """Get current state of a coroutine object.
1645
1646 Possible states are:
1647 CORO_CREATED: Waiting to start execution.
1648 CORO_RUNNING: Currently being executed by the interpreter.
1649 CORO_SUSPENDED: Currently suspended at an await expression.
1650 CORO_CLOSED: Execution has completed.
1651 """
1652 if coroutine.cr_running:
1653 return CORO_RUNNING
1654 if coroutine.cr_frame is None:
1655 return CORO_CLOSED
1656 if coroutine.cr_frame.f_lasti == -1:
1657 return CORO_CREATED
1658 return CORO_SUSPENDED
1659
1660
1661def getcoroutinelocals(coroutine):
1662 """
1663 Get the mapping of coroutine local variables to their current values.
1664
1665 A dict is returned, with the keys the local variable names and values the
1666 bound values."""
1667 frame = getattr(coroutine, "cr_frame", None)
1668 if frame is not None:
1669 return frame.f_locals
1670 else:
1671 return {}
1672
1673
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001674###############################################################################
1675### Function Signature Object (PEP 362)
1676###############################################################################
1677
1678
1679_WrapperDescriptor = type(type.__call__)
1680_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001681_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001682
1683_NonUserDefinedCallables = (_WrapperDescriptor,
1684 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001685 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001686 types.BuiltinFunctionType)
1687
1688
Yury Selivanov421f0c72014-01-29 12:05:40 -05001689def _signature_get_user_defined_method(cls, method_name):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001690 """Private helper. Checks if ``cls`` has an attribute
1691 named ``method_name`` and returns it only if it is a
1692 pure python function.
1693 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001694 try:
1695 meth = getattr(cls, method_name)
1696 except AttributeError:
1697 return
1698 else:
1699 if not isinstance(meth, _NonUserDefinedCallables):
1700 # Once '__signature__' will be added to 'C'-level
1701 # callables, this check won't be necessary
1702 return meth
1703
1704
Yury Selivanov62560fb2014-01-28 12:26:24 -05001705def _signature_get_partial(wrapped_sig, partial, extra_args=()):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001706 """Private helper to calculate how 'wrapped_sig' signature will
1707 look like after applying a 'functools.partial' object (or alike)
1708 on it.
1709 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001710
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001711 old_params = wrapped_sig.parameters
1712 new_params = OrderedDict(old_params.items())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001713
1714 partial_args = partial.args or ()
1715 partial_keywords = partial.keywords or {}
1716
1717 if extra_args:
1718 partial_args = extra_args + partial_args
1719
1720 try:
1721 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1722 except TypeError as ex:
1723 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1724 raise ValueError(msg) from ex
1725
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001726
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001727 transform_to_kwonly = False
1728 for param_name, param in old_params.items():
1729 try:
1730 arg_value = ba.arguments[param_name]
1731 except KeyError:
1732 pass
1733 else:
1734 if param.kind is _POSITIONAL_ONLY:
1735 # If positional-only parameter is bound by partial,
1736 # it effectively disappears from the signature
1737 new_params.pop(param_name)
1738 continue
1739
1740 if param.kind is _POSITIONAL_OR_KEYWORD:
1741 if param_name in partial_keywords:
1742 # This means that this parameter, and all parameters
1743 # after it should be keyword-only (and var-positional
1744 # should be removed). Here's why. Consider the following
1745 # function:
1746 # foo(a, b, *args, c):
1747 # pass
1748 #
1749 # "partial(foo, a='spam')" will have the following
1750 # signature: "(*, a='spam', b, c)". Because attempting
1751 # to call that partial with "(10, 20)" arguments will
1752 # raise a TypeError, saying that "a" argument received
1753 # multiple values.
1754 transform_to_kwonly = True
1755 # Set the new default value
1756 new_params[param_name] = param.replace(default=arg_value)
1757 else:
1758 # was passed as a positional argument
1759 new_params.pop(param.name)
1760 continue
1761
1762 if param.kind is _KEYWORD_ONLY:
1763 # Set the new default value
1764 new_params[param_name] = param.replace(default=arg_value)
1765
1766 if transform_to_kwonly:
1767 assert param.kind is not _POSITIONAL_ONLY
1768
1769 if param.kind is _POSITIONAL_OR_KEYWORD:
1770 new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1771 new_params[param_name] = new_param
1772 new_params.move_to_end(param_name)
1773 elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
1774 new_params.move_to_end(param_name)
1775 elif param.kind is _VAR_POSITIONAL:
1776 new_params.pop(param.name)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001777
1778 return wrapped_sig.replace(parameters=new_params.values())
1779
1780
Yury Selivanov62560fb2014-01-28 12:26:24 -05001781def _signature_bound_method(sig):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001782 """Private helper to transform signatures for unbound
1783 functions to bound methods.
1784 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001785
1786 params = tuple(sig.parameters.values())
1787
1788 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1789 raise ValueError('invalid method signature')
1790
1791 kind = params[0].kind
1792 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1793 # Drop first parameter:
1794 # '(p1, p2[, ...])' -> '(p2[, ...])'
1795 params = params[1:]
1796 else:
1797 if kind is not _VAR_POSITIONAL:
1798 # Unless we add a new parameter type we never
1799 # get here
1800 raise ValueError('invalid argument type')
1801 # It's a var-positional parameter.
1802 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1803
1804 return sig.replace(parameters=params)
1805
1806
Yury Selivanovb77511d2014-01-29 10:46:14 -05001807def _signature_is_builtin(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001808 """Private helper to test if `obj` is a callable that might
1809 support Argument Clinic's __text_signature__ protocol.
1810 """
Yury Selivanov1d241832014-02-02 12:51:20 -05001811 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001812 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001813 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001814 # Can't test 'isinstance(type)' here, as it would
1815 # also be True for regular python classes
1816 obj in (type, object))
1817
1818
Yury Selivanov63da7c72014-01-31 14:48:37 -05001819def _signature_is_functionlike(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001820 """Private helper to test if `obj` is a duck type of FunctionType.
1821 A good example of such objects are functions compiled with
1822 Cython, which have all attributes that a pure Python function
1823 would have, but have their code statically compiled.
1824 """
Yury Selivanov63da7c72014-01-31 14:48:37 -05001825
1826 if not callable(obj) or isclass(obj):
1827 # All function-like objects are obviously callables,
1828 # and not classes.
1829 return False
1830
1831 name = getattr(obj, '__name__', None)
1832 code = getattr(obj, '__code__', None)
1833 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
1834 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
1835 annotations = getattr(obj, '__annotations__', None)
1836
1837 return (isinstance(code, types.CodeType) and
1838 isinstance(name, str) and
1839 (defaults is None or isinstance(defaults, tuple)) and
1840 (kwdefaults is None or isinstance(kwdefaults, dict)) and
1841 isinstance(annotations, dict))
1842
1843
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001844def _signature_get_bound_param(spec):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001845 """ Private helper to get first parameter name from a
1846 __text_signature__ of a builtin method, which should
1847 be in the following format: '($param1, ...)'.
1848 Assumptions are that the first argument won't have
1849 a default value or an annotation.
1850 """
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001851
1852 assert spec.startswith('($')
1853
1854 pos = spec.find(',')
1855 if pos == -1:
1856 pos = spec.find(')')
1857
1858 cpos = spec.find(':')
1859 assert cpos == -1 or cpos > pos
1860
1861 cpos = spec.find('=')
1862 assert cpos == -1 or cpos > pos
1863
1864 return spec[2:pos]
1865
1866
Larry Hastings2623c8c2014-02-08 22:15:29 -08001867def _signature_strip_non_python_syntax(signature):
1868 """
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001869 Private helper function. Takes a signature in Argument Clinic's
1870 extended signature format.
1871
Larry Hastings2623c8c2014-02-08 22:15:29 -08001872 Returns a tuple of three things:
1873 * that signature re-rendered in standard Python syntax,
1874 * the index of the "self" parameter (generally 0), or None if
1875 the function does not have a "self" parameter, and
1876 * the index of the last "positional only" parameter,
1877 or None if the signature has no positional-only parameters.
1878 """
1879
1880 if not signature:
1881 return signature, None, None
1882
1883 self_parameter = None
1884 last_positional_only = None
1885
1886 lines = [l.encode('ascii') for l in signature.split('\n')]
1887 generator = iter(lines).__next__
1888 token_stream = tokenize.tokenize(generator)
1889
1890 delayed_comma = False
1891 skip_next_comma = False
1892 text = []
1893 add = text.append
1894
1895 current_parameter = 0
1896 OP = token.OP
1897 ERRORTOKEN = token.ERRORTOKEN
1898
1899 # token stream always starts with ENCODING token, skip it
1900 t = next(token_stream)
1901 assert t.type == tokenize.ENCODING
1902
1903 for t in token_stream:
1904 type, string = t.type, t.string
1905
1906 if type == OP:
1907 if string == ',':
1908 if skip_next_comma:
1909 skip_next_comma = False
1910 else:
1911 assert not delayed_comma
1912 delayed_comma = True
1913 current_parameter += 1
1914 continue
1915
1916 if string == '/':
1917 assert not skip_next_comma
1918 assert last_positional_only is None
1919 skip_next_comma = True
1920 last_positional_only = current_parameter - 1
1921 continue
1922
1923 if (type == ERRORTOKEN) and (string == '$'):
1924 assert self_parameter is None
1925 self_parameter = current_parameter
1926 continue
1927
1928 if delayed_comma:
1929 delayed_comma = False
1930 if not ((type == OP) and (string == ')')):
1931 add(', ')
1932 add(string)
1933 if (string == ','):
1934 add(' ')
1935 clean_signature = ''.join(text)
1936 return clean_signature, self_parameter, last_positional_only
1937
1938
Yury Selivanov57d240e2014-02-19 16:27:23 -05001939def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001940 """Private helper to parse content of '__text_signature__'
1941 and return a Signature based on it.
1942 """
1943
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001944 Parameter = cls._parameter_cls
1945
Larry Hastings2623c8c2014-02-08 22:15:29 -08001946 clean_signature, self_parameter, last_positional_only = \
1947 _signature_strip_non_python_syntax(s)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001948
Larry Hastings2623c8c2014-02-08 22:15:29 -08001949 program = "def foo" + clean_signature + ": pass"
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001950
1951 try:
Larry Hastings2623c8c2014-02-08 22:15:29 -08001952 module = ast.parse(program)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001953 except SyntaxError:
1954 module = None
1955
1956 if not isinstance(module, ast.Module):
1957 raise ValueError("{!r} builtin has invalid signature".format(obj))
1958
1959 f = module.body[0]
1960
1961 parameters = []
1962 empty = Parameter.empty
1963 invalid = object()
1964
1965 module = None
1966 module_dict = {}
1967 module_name = getattr(obj, '__module__', None)
1968 if module_name:
1969 module = sys.modules.get(module_name, None)
1970 if module:
1971 module_dict = module.__dict__
1972 sys_module_dict = sys.modules
1973
1974 def parse_name(node):
1975 assert isinstance(node, ast.arg)
1976 if node.annotation != None:
1977 raise ValueError("Annotations are not currently supported")
1978 return node.arg
1979
1980 def wrap_value(s):
1981 try:
1982 value = eval(s, module_dict)
1983 except NameError:
1984 try:
1985 value = eval(s, sys_module_dict)
1986 except NameError:
1987 raise RuntimeError()
1988
1989 if isinstance(value, str):
1990 return ast.Str(value)
1991 if isinstance(value, (int, float)):
1992 return ast.Num(value)
1993 if isinstance(value, bytes):
1994 return ast.Bytes(value)
1995 if value in (True, False, None):
1996 return ast.NameConstant(value)
1997 raise RuntimeError()
1998
1999 class RewriteSymbolics(ast.NodeTransformer):
2000 def visit_Attribute(self, node):
2001 a = []
2002 n = node
2003 while isinstance(n, ast.Attribute):
2004 a.append(n.attr)
2005 n = n.value
2006 if not isinstance(n, ast.Name):
2007 raise RuntimeError()
2008 a.append(n.id)
2009 value = ".".join(reversed(a))
2010 return wrap_value(value)
2011
2012 def visit_Name(self, node):
2013 if not isinstance(node.ctx, ast.Load):
2014 raise ValueError()
2015 return wrap_value(node.id)
2016
2017 def p(name_node, default_node, default=empty):
2018 name = parse_name(name_node)
2019 if name is invalid:
2020 return None
2021 if default_node and default_node is not _empty:
2022 try:
2023 default_node = RewriteSymbolics().visit(default_node)
2024 o = ast.literal_eval(default_node)
2025 except ValueError:
2026 o = invalid
2027 if o is invalid:
2028 return None
2029 default = o if o is not invalid else default
2030 parameters.append(Parameter(name, kind, default=default, annotation=empty))
2031
2032 # non-keyword-only parameters
2033 args = reversed(f.args.args)
2034 defaults = reversed(f.args.defaults)
2035 iter = itertools.zip_longest(args, defaults, fillvalue=None)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002036 if last_positional_only is not None:
2037 kind = Parameter.POSITIONAL_ONLY
2038 else:
2039 kind = Parameter.POSITIONAL_OR_KEYWORD
2040 for i, (name, default) in enumerate(reversed(list(iter))):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002041 p(name, default)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002042 if i == last_positional_only:
2043 kind = Parameter.POSITIONAL_OR_KEYWORD
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002044
2045 # *args
2046 if f.args.vararg:
2047 kind = Parameter.VAR_POSITIONAL
2048 p(f.args.vararg, empty)
2049
2050 # keyword-only arguments
2051 kind = Parameter.KEYWORD_ONLY
2052 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
2053 p(name, default)
2054
2055 # **kwargs
2056 if f.args.kwarg:
2057 kind = Parameter.VAR_KEYWORD
2058 p(f.args.kwarg, empty)
2059
Larry Hastings2623c8c2014-02-08 22:15:29 -08002060 if self_parameter is not None:
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002061 # Possibly strip the bound argument:
2062 # - We *always* strip first bound argument if
2063 # it is a module.
2064 # - We don't strip first bound argument if
2065 # skip_bound_arg is False.
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002066 assert parameters
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002067 _self = getattr(obj, '__self__', None)
2068 self_isbound = _self is not None
2069 self_ismodule = ismodule(_self)
2070 if self_isbound and (self_ismodule or skip_bound_arg):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002071 parameters.pop(0)
2072 else:
2073 # for builtins, self parameter is always positional-only!
2074 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
2075 parameters[0] = p
2076
2077 return cls(parameters, return_annotation=cls.empty)
2078
2079
Yury Selivanov57d240e2014-02-19 16:27:23 -05002080def _signature_from_builtin(cls, func, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002081 """Private helper function to get signature for
2082 builtin callables.
2083 """
2084
Yury Selivanov57d240e2014-02-19 16:27:23 -05002085 if not _signature_is_builtin(func):
2086 raise TypeError("{!r} is not a Python builtin "
2087 "function".format(func))
2088
2089 s = getattr(func, "__text_signature__", None)
2090 if not s:
2091 raise ValueError("no signature found for builtin {!r}".format(func))
2092
2093 return _signature_fromstr(cls, func, s, skip_bound_arg)
2094
2095
Yury Selivanovcf45f022015-05-20 14:38:50 -04002096def _signature_from_function(cls, func):
2097 """Private helper: constructs Signature for the given python function."""
2098
2099 is_duck_function = False
2100 if not isfunction(func):
2101 if _signature_is_functionlike(func):
2102 is_duck_function = True
2103 else:
2104 # If it's not a pure Python function, and not a duck type
2105 # of pure function:
2106 raise TypeError('{!r} is not a Python function'.format(func))
2107
2108 Parameter = cls._parameter_cls
2109
2110 # Parameter information.
2111 func_code = func.__code__
2112 pos_count = func_code.co_argcount
2113 arg_names = func_code.co_varnames
2114 positional = tuple(arg_names[:pos_count])
2115 keyword_only_count = func_code.co_kwonlyargcount
2116 keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)]
2117 annotations = func.__annotations__
2118 defaults = func.__defaults__
2119 kwdefaults = func.__kwdefaults__
2120
2121 if defaults:
2122 pos_default_count = len(defaults)
2123 else:
2124 pos_default_count = 0
2125
2126 parameters = []
2127
2128 # Non-keyword-only parameters w/o defaults.
2129 non_default_count = pos_count - pos_default_count
2130 for name in positional[:non_default_count]:
2131 annotation = annotations.get(name, _empty)
2132 parameters.append(Parameter(name, annotation=annotation,
2133 kind=_POSITIONAL_OR_KEYWORD))
2134
2135 # ... w/ defaults.
2136 for offset, name in enumerate(positional[non_default_count:]):
2137 annotation = annotations.get(name, _empty)
2138 parameters.append(Parameter(name, annotation=annotation,
2139 kind=_POSITIONAL_OR_KEYWORD,
2140 default=defaults[offset]))
2141
2142 # *args
2143 if func_code.co_flags & CO_VARARGS:
2144 name = arg_names[pos_count + keyword_only_count]
2145 annotation = annotations.get(name, _empty)
2146 parameters.append(Parameter(name, annotation=annotation,
2147 kind=_VAR_POSITIONAL))
2148
2149 # Keyword-only parameters.
2150 for name in keyword_only:
2151 default = _empty
2152 if kwdefaults is not None:
2153 default = kwdefaults.get(name, _empty)
2154
2155 annotation = annotations.get(name, _empty)
2156 parameters.append(Parameter(name, annotation=annotation,
2157 kind=_KEYWORD_ONLY,
2158 default=default))
2159 # **kwargs
2160 if func_code.co_flags & CO_VARKEYWORDS:
2161 index = pos_count + keyword_only_count
2162 if func_code.co_flags & CO_VARARGS:
2163 index += 1
2164
2165 name = arg_names[index]
2166 annotation = annotations.get(name, _empty)
2167 parameters.append(Parameter(name, annotation=annotation,
2168 kind=_VAR_KEYWORD))
2169
2170 # Is 'func' is a pure Python function - don't validate the
2171 # parameters list (for correct order and defaults), it should be OK.
2172 return cls(parameters,
2173 return_annotation=annotations.get('return', _empty),
2174 __validate_parameters__=is_duck_function)
2175
2176
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002177def _signature_from_callable(obj, *,
2178 follow_wrapper_chains=True,
2179 skip_bound_arg=True,
2180 sigcls):
2181
2182 """Private helper function to get signature for arbitrary
2183 callable objects.
2184 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002185
2186 if not callable(obj):
2187 raise TypeError('{!r} is not a callable object'.format(obj))
2188
2189 if isinstance(obj, types.MethodType):
2190 # In this case we skip the first parameter of the underlying
2191 # function (usually `self` or `cls`).
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002192 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002193 obj.__func__,
2194 follow_wrapper_chains=follow_wrapper_chains,
2195 skip_bound_arg=skip_bound_arg,
2196 sigcls=sigcls)
2197
Yury Selivanov57d240e2014-02-19 16:27:23 -05002198 if skip_bound_arg:
2199 return _signature_bound_method(sig)
2200 else:
2201 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002202
Nick Coghlane8c45d62013-07-28 20:00:01 +10002203 # Was this function wrapped by a decorator?
Yury Selivanov57d240e2014-02-19 16:27:23 -05002204 if follow_wrapper_chains:
2205 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
Yury Selivanov46c759d2015-05-27 21:56:53 -04002206 if isinstance(obj, types.MethodType):
2207 # If the unwrapped object is a *method*, we might want to
2208 # skip its first parameter (self).
2209 # See test_signature_wrapped_bound_method for details.
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002210 return _signature_from_callable(
Yury Selivanov46c759d2015-05-27 21:56:53 -04002211 obj,
2212 follow_wrapper_chains=follow_wrapper_chains,
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002213 skip_bound_arg=skip_bound_arg,
2214 sigcls=sigcls)
Nick Coghlane8c45d62013-07-28 20:00:01 +10002215
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002216 try:
2217 sig = obj.__signature__
2218 except AttributeError:
2219 pass
2220 else:
2221 if sig is not None:
Yury Selivanov42407ab2014-06-23 10:23:50 -07002222 if not isinstance(sig, Signature):
2223 raise TypeError(
2224 'unexpected object {!r} in __signature__ '
2225 'attribute'.format(sig))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002226 return sig
2227
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002228 try:
2229 partialmethod = obj._partialmethod
2230 except AttributeError:
2231 pass
2232 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05002233 if isinstance(partialmethod, functools.partialmethod):
2234 # Unbound partialmethod (see functools.partialmethod)
2235 # This means, that we need to calculate the signature
2236 # as if it's a regular partial object, but taking into
2237 # account that the first positional argument
2238 # (usually `self`, or `cls`) will not be passed
2239 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002240
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002241 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002242 partialmethod.func,
2243 follow_wrapper_chains=follow_wrapper_chains,
2244 skip_bound_arg=skip_bound_arg,
2245 sigcls=sigcls)
2246
Yury Selivanov0486f812014-01-29 12:18:59 -05002247 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanov0486f812014-01-29 12:18:59 -05002248 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
Dong-hee Na378d7062017-05-18 04:00:51 +09002249 if first_wrapped_param.kind is Parameter.VAR_POSITIONAL:
2250 # First argument of the wrapped callable is `*args`, as in
2251 # `partialmethod(lambda *args)`.
2252 return sig
2253 else:
2254 sig_params = tuple(sig.parameters.values())
2255 assert first_wrapped_param is not sig_params[0]
2256 new_params = (first_wrapped_param,) + sig_params
2257 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002258
Yury Selivanov63da7c72014-01-31 14:48:37 -05002259 if isfunction(obj) or _signature_is_functionlike(obj):
2260 # If it's a pure Python function, or an object that is duck type
2261 # of a Python function (Cython functions, for instance), then:
Yury Selivanovcf45f022015-05-20 14:38:50 -04002262 return _signature_from_function(sigcls, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002263
Yury Selivanova773de02014-02-21 18:30:53 -05002264 if _signature_is_builtin(obj):
Yury Selivanovda396452014-03-27 12:09:24 -04002265 return _signature_from_builtin(sigcls, obj,
Yury Selivanova773de02014-02-21 18:30:53 -05002266 skip_bound_arg=skip_bound_arg)
2267
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002268 if isinstance(obj, functools.partial):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002269 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002270 obj.func,
2271 follow_wrapper_chains=follow_wrapper_chains,
2272 skip_bound_arg=skip_bound_arg,
2273 sigcls=sigcls)
Yury Selivanov62560fb2014-01-28 12:26:24 -05002274 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002275
2276 sig = None
2277 if isinstance(obj, type):
2278 # obj is a class or a metaclass
2279
2280 # First, let's see if it has an overloaded __call__ defined
2281 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05002282 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002283 if call is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002284 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002285 call,
2286 follow_wrapper_chains=follow_wrapper_chains,
2287 skip_bound_arg=skip_bound_arg,
2288 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002289 else:
2290 # Now we check if the 'obj' class has a '__new__' method
Yury Selivanov421f0c72014-01-29 12:05:40 -05002291 new = _signature_get_user_defined_method(obj, '__new__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002292 if new is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002293 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002294 new,
2295 follow_wrapper_chains=follow_wrapper_chains,
2296 skip_bound_arg=skip_bound_arg,
2297 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002298 else:
2299 # Finally, we should have at least __init__ implemented
Yury Selivanov421f0c72014-01-29 12:05:40 -05002300 init = _signature_get_user_defined_method(obj, '__init__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002301 if init is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002302 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002303 init,
2304 follow_wrapper_chains=follow_wrapper_chains,
2305 skip_bound_arg=skip_bound_arg,
2306 sigcls=sigcls)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002307
2308 if sig is None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002309 # At this point we know, that `obj` is a class, with no user-
2310 # defined '__init__', '__new__', or class-level '__call__'
2311
Larry Hastings2623c8c2014-02-08 22:15:29 -08002312 for base in obj.__mro__[:-1]:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002313 # Since '__text_signature__' is implemented as a
2314 # descriptor that extracts text signature from the
2315 # class docstring, if 'obj' is derived from a builtin
2316 # class, its own '__text_signature__' may be 'None'.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002317 # Therefore, we go through the MRO (except the last
2318 # class in there, which is 'object') to find the first
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002319 # class with non-empty text signature.
2320 try:
2321 text_sig = base.__text_signature__
2322 except AttributeError:
2323 pass
2324 else:
2325 if text_sig:
2326 # If 'obj' class has a __text_signature__ attribute:
2327 # return a signature based on it
Yury Selivanovda396452014-03-27 12:09:24 -04002328 return _signature_fromstr(sigcls, obj, text_sig)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002329
2330 # No '__text_signature__' was found for the 'obj' class.
2331 # Last option is to check if its '__init__' is
2332 # object.__init__ or type.__init__.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002333 if type not in obj.__mro__:
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002334 # We have a class (not metaclass), but no user-defined
2335 # __init__ or __new__ for it
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002336 if (obj.__init__ is object.__init__ and
2337 obj.__new__ is object.__new__):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002338 # Return a signature of 'object' builtin.
2339 return signature(object)
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002340 else:
2341 raise ValueError(
2342 'no signature found for builtin type {!r}'.format(obj))
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002343
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002344 elif not isinstance(obj, _NonUserDefinedCallables):
2345 # An object with __call__
2346 # We also check that the 'obj' is not an instance of
2347 # _WrapperDescriptor or _MethodWrapper to avoid
2348 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05002349 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002350 if call is not None:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002351 try:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002352 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002353 call,
2354 follow_wrapper_chains=follow_wrapper_chains,
2355 skip_bound_arg=skip_bound_arg,
2356 sigcls=sigcls)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002357 except ValueError as ex:
2358 msg = 'no signature found for {!r}'.format(obj)
2359 raise ValueError(msg) from ex
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002360
2361 if sig is not None:
2362 # For classes and objects we skip the first parameter of their
2363 # __call__, __new__, or __init__ methods
Yury Selivanov57d240e2014-02-19 16:27:23 -05002364 if skip_bound_arg:
2365 return _signature_bound_method(sig)
2366 else:
2367 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002368
2369 if isinstance(obj, types.BuiltinFunctionType):
2370 # Raise a nicer error message for builtins
2371 msg = 'no signature found for builtin function {!r}'.format(obj)
2372 raise ValueError(msg)
2373
2374 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2375
2376
2377class _void:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002378 """A private marker - used in Parameter & Signature."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002379
2380
2381class _empty:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002382 """Marker object for Signature.empty and Parameter.empty."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002383
2384
Yury Selivanov21e83a52014-03-27 11:23:13 -04002385class _ParameterKind(enum.IntEnum):
2386 POSITIONAL_ONLY = 0
2387 POSITIONAL_OR_KEYWORD = 1
2388 VAR_POSITIONAL = 2
2389 KEYWORD_ONLY = 3
2390 VAR_KEYWORD = 4
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002391
2392 def __str__(self):
Yury Selivanov21e83a52014-03-27 11:23:13 -04002393 return self._name_
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002394
2395
Yury Selivanov21e83a52014-03-27 11:23:13 -04002396_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
2397_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
2398_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
2399_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
2400_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002401
2402
2403class Parameter:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002404 """Represents a parameter in a function signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002405
2406 Has the following public attributes:
2407
2408 * name : str
2409 The name of the parameter as a string.
2410 * default : object
2411 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002412 parameter has no default value, this attribute is set to
2413 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002414 * annotation
2415 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002416 parameter has no annotation, this attribute is set to
2417 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002418 * kind : str
2419 Describes how argument values are bound to the parameter.
2420 Possible values: `Parameter.POSITIONAL_ONLY`,
2421 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
2422 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002423 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002424
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002425 __slots__ = ('_name', '_kind', '_default', '_annotation')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002426
2427 POSITIONAL_ONLY = _POSITIONAL_ONLY
2428 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
2429 VAR_POSITIONAL = _VAR_POSITIONAL
2430 KEYWORD_ONLY = _KEYWORD_ONLY
2431 VAR_KEYWORD = _VAR_KEYWORD
2432
2433 empty = _empty
2434
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002435 def __init__(self, name, kind, *, default=_empty, annotation=_empty):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002436
2437 if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
2438 _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
2439 raise ValueError("invalid value for 'Parameter.kind' attribute")
2440 self._kind = kind
2441
2442 if default is not _empty:
2443 if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2444 msg = '{} parameters cannot have default values'.format(kind)
2445 raise ValueError(msg)
2446 self._default = default
2447 self._annotation = annotation
2448
Yury Selivanov2393dca2014-01-27 15:07:58 -05002449 if name is _empty:
2450 raise ValueError('name is a required attribute for Parameter')
2451
2452 if not isinstance(name, str):
2453 raise TypeError("name must be a str, not a {!r}".format(name))
2454
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002455 if name[0] == '.' and name[1:].isdigit():
2456 # These are implicit arguments generated by comprehensions. In
2457 # order to provide a friendlier interface to users, we recast
2458 # their name as "implicitN" and treat them as positional-only.
2459 # See issue 19611.
2460 if kind != _POSITIONAL_OR_KEYWORD:
2461 raise ValueError(
2462 'implicit arguments must be passed in as {}'.format(
2463 _POSITIONAL_OR_KEYWORD
2464 )
2465 )
2466 self._kind = _POSITIONAL_ONLY
2467 name = 'implicit{}'.format(name[1:])
2468
Yury Selivanov2393dca2014-01-27 15:07:58 -05002469 if not name.isidentifier():
2470 raise ValueError('{!r} is not a valid parameter name'.format(name))
2471
2472 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002473
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002474 def __reduce__(self):
2475 return (type(self),
2476 (self._name, self._kind),
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002477 {'_default': self._default,
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002478 '_annotation': self._annotation})
2479
2480 def __setstate__(self, state):
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002481 self._default = state['_default']
2482 self._annotation = state['_annotation']
2483
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002484 @property
2485 def name(self):
2486 return self._name
2487
2488 @property
2489 def default(self):
2490 return self._default
2491
2492 @property
2493 def annotation(self):
2494 return self._annotation
2495
2496 @property
2497 def kind(self):
2498 return self._kind
2499
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002500 def replace(self, *, name=_void, kind=_void,
2501 annotation=_void, default=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002502 """Creates a customized copy of the Parameter."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002503
2504 if name is _void:
2505 name = self._name
2506
2507 if kind is _void:
2508 kind = self._kind
2509
2510 if annotation is _void:
2511 annotation = self._annotation
2512
2513 if default is _void:
2514 default = self._default
2515
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002516 return type(self)(name, kind, default=default, annotation=annotation)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002517
2518 def __str__(self):
2519 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002520 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002521
2522 # Add annotation and default value
2523 if self._annotation is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002524 formatted = '{}: {}'.format(formatted,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002525 formatannotation(self._annotation))
2526
2527 if self._default is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002528 if self._annotation is not _empty:
2529 formatted = '{} = {}'.format(formatted, repr(self._default))
2530 else:
2531 formatted = '{}={}'.format(formatted, repr(self._default))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002532
2533 if kind == _VAR_POSITIONAL:
2534 formatted = '*' + formatted
2535 elif kind == _VAR_KEYWORD:
2536 formatted = '**' + formatted
2537
2538 return formatted
2539
2540 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002541 return '<{} "{}">'.format(self.__class__.__name__, self)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002542
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002543 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002544 return hash((self.name, self.kind, self.annotation, self.default))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002545
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002546 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002547 if self is other:
2548 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002549 if not isinstance(other, Parameter):
2550 return NotImplemented
2551 return (self._name == other._name and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002552 self._kind == other._kind and
2553 self._default == other._default and
2554 self._annotation == other._annotation)
2555
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002556
2557class BoundArguments:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002558 """Result of `Signature.bind` call. Holds the mapping of arguments
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002559 to the function's parameters.
2560
2561 Has the following public attributes:
2562
2563 * arguments : OrderedDict
2564 An ordered mutable mapping of parameters' names to arguments' values.
2565 Does not contain arguments' default values.
2566 * signature : Signature
2567 The Signature object that created this instance.
2568 * args : tuple
2569 Tuple of positional arguments values.
2570 * kwargs : dict
2571 Dict of keyword arguments values.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002572 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002573
Yury Selivanov6abe0322015-05-13 17:18:41 -04002574 __slots__ = ('arguments', '_signature', '__weakref__')
2575
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002576 def __init__(self, signature, arguments):
2577 self.arguments = arguments
2578 self._signature = signature
2579
2580 @property
2581 def signature(self):
2582 return self._signature
2583
2584 @property
2585 def args(self):
2586 args = []
2587 for param_name, param in self._signature.parameters.items():
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002588 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002589 break
2590
2591 try:
2592 arg = self.arguments[param_name]
2593 except KeyError:
2594 # We're done here. Other arguments
2595 # will be mapped in 'BoundArguments.kwargs'
2596 break
2597 else:
2598 if param.kind == _VAR_POSITIONAL:
2599 # *args
2600 args.extend(arg)
2601 else:
2602 # plain argument
2603 args.append(arg)
2604
2605 return tuple(args)
2606
2607 @property
2608 def kwargs(self):
2609 kwargs = {}
2610 kwargs_started = False
2611 for param_name, param in self._signature.parameters.items():
2612 if not kwargs_started:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002613 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002614 kwargs_started = True
2615 else:
2616 if param_name not in self.arguments:
2617 kwargs_started = True
2618 continue
2619
2620 if not kwargs_started:
2621 continue
2622
2623 try:
2624 arg = self.arguments[param_name]
2625 except KeyError:
2626 pass
2627 else:
2628 if param.kind == _VAR_KEYWORD:
2629 # **kwargs
2630 kwargs.update(arg)
2631 else:
2632 # plain keyword argument
2633 kwargs[param_name] = arg
2634
2635 return kwargs
2636
Yury Selivanovb907a512015-05-16 13:45:09 -04002637 def apply_defaults(self):
2638 """Set default values for missing arguments.
2639
2640 For variable-positional arguments (*args) the default is an
2641 empty tuple.
2642
2643 For variable-keyword arguments (**kwargs) the default is an
2644 empty dict.
2645 """
2646 arguments = self.arguments
Yury Selivanovb907a512015-05-16 13:45:09 -04002647 new_arguments = []
2648 for name, param in self._signature.parameters.items():
2649 try:
2650 new_arguments.append((name, arguments[name]))
2651 except KeyError:
2652 if param.default is not _empty:
2653 val = param.default
2654 elif param.kind is _VAR_POSITIONAL:
2655 val = ()
2656 elif param.kind is _VAR_KEYWORD:
2657 val = {}
2658 else:
2659 # This BoundArguments was likely produced by
2660 # Signature.bind_partial().
2661 continue
2662 new_arguments.append((name, val))
2663 self.arguments = OrderedDict(new_arguments)
2664
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002665 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002666 if self is other:
2667 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002668 if not isinstance(other, BoundArguments):
2669 return NotImplemented
2670 return (self.signature == other.signature and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002671 self.arguments == other.arguments)
2672
Yury Selivanov6abe0322015-05-13 17:18:41 -04002673 def __setstate__(self, state):
2674 self._signature = state['_signature']
2675 self.arguments = state['arguments']
2676
2677 def __getstate__(self):
2678 return {'_signature': self._signature, 'arguments': self.arguments}
2679
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002680 def __repr__(self):
2681 args = []
2682 for arg, value in self.arguments.items():
2683 args.append('{}={!r}'.format(arg, value))
Yury Selivanovf229bc52015-05-15 12:53:56 -04002684 return '<{} ({})>'.format(self.__class__.__name__, ', '.join(args))
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002685
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002686
2687class Signature:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002688 """A Signature object represents the overall signature of a function.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002689 It stores a Parameter object for each parameter accepted by the
2690 function, as well as information specific to the function itself.
2691
2692 A Signature object has the following public attributes and methods:
2693
2694 * parameters : OrderedDict
2695 An ordered mapping of parameters' names to the corresponding
2696 Parameter objects (keyword-only arguments are in the same order
2697 as listed in `code.co_varnames`).
2698 * return_annotation : object
2699 The annotation for the return type of the function if specified.
2700 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002701 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002702 * bind(*args, **kwargs) -> BoundArguments
2703 Creates a mapping from positional and keyword arguments to
2704 parameters.
2705 * bind_partial(*args, **kwargs) -> BoundArguments
2706 Creates a partial mapping from positional and keyword arguments
2707 to parameters (simulating 'functools.partial' behavior.)
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002708 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002709
2710 __slots__ = ('_return_annotation', '_parameters')
2711
2712 _parameter_cls = Parameter
2713 _bound_arguments_cls = BoundArguments
2714
2715 empty = _empty
2716
2717 def __init__(self, parameters=None, *, return_annotation=_empty,
2718 __validate_parameters__=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002719 """Constructs Signature from the given list of Parameter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002720 objects and 'return_annotation'. All arguments are optional.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002721 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002722
2723 if parameters is None:
2724 params = OrderedDict()
2725 else:
2726 if __validate_parameters__:
2727 params = OrderedDict()
2728 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002729 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002730
2731 for idx, param in enumerate(parameters):
2732 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002733 name = param.name
2734
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002735 if kind < top_kind:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002736 msg = 'wrong parameter order: {!r} before {!r}'
Yury Selivanov2393dca2014-01-27 15:07:58 -05002737 msg = msg.format(top_kind, kind)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002738 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002739 elif kind > top_kind:
2740 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002741 top_kind = kind
2742
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002743 if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
Yury Selivanov07a9e452014-01-29 10:58:16 -05002744 if param.default is _empty:
2745 if kind_defaults:
2746 # No default for this parameter, but the
2747 # previous parameter of the same kind had
2748 # a default
2749 msg = 'non-default argument follows default ' \
2750 'argument'
2751 raise ValueError(msg)
2752 else:
2753 # There is a default for this parameter.
2754 kind_defaults = True
2755
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002756 if name in params:
2757 msg = 'duplicate parameter name: {!r}'.format(name)
2758 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002759
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002760 params[name] = param
2761 else:
2762 params = OrderedDict(((param.name, param)
2763 for param in parameters))
2764
2765 self._parameters = types.MappingProxyType(params)
2766 self._return_annotation = return_annotation
2767
2768 @classmethod
2769 def from_function(cls, func):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002770 """Constructs Signature for the given python function."""
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002771
2772 warnings.warn("inspect.Signature.from_function() is deprecated, "
Berker Peksagb5601582015-05-21 23:40:54 +03002773 "use Signature.from_callable()",
2774 DeprecationWarning, stacklevel=2)
Yury Selivanovcf45f022015-05-20 14:38:50 -04002775 return _signature_from_function(cls, func)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002776
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002777 @classmethod
2778 def from_builtin(cls, func):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002779 """Constructs Signature for the given builtin function."""
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002780
2781 warnings.warn("inspect.Signature.from_builtin() is deprecated, "
Berker Peksagb5601582015-05-21 23:40:54 +03002782 "use Signature.from_callable()",
2783 DeprecationWarning, stacklevel=2)
Yury Selivanov57d240e2014-02-19 16:27:23 -05002784 return _signature_from_builtin(cls, func)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002785
Yury Selivanovda396452014-03-27 12:09:24 -04002786 @classmethod
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002787 def from_callable(cls, obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002788 """Constructs Signature for the given callable object."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002789 return _signature_from_callable(obj, sigcls=cls,
2790 follow_wrapper_chains=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04002791
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002792 @property
2793 def parameters(self):
2794 return self._parameters
2795
2796 @property
2797 def return_annotation(self):
2798 return self._return_annotation
2799
2800 def replace(self, *, parameters=_void, return_annotation=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002801 """Creates a customized copy of the Signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002802 Pass 'parameters' and/or 'return_annotation' arguments
2803 to override them in the new copy.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002804 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002805
2806 if parameters is _void:
2807 parameters = self.parameters.values()
2808
2809 if return_annotation is _void:
2810 return_annotation = self._return_annotation
2811
2812 return type(self)(parameters,
2813 return_annotation=return_annotation)
2814
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002815 def _hash_basis(self):
2816 params = tuple(param for param in self.parameters.values()
2817 if param.kind != _KEYWORD_ONLY)
2818
2819 kwo_params = {param.name: param for param in self.parameters.values()
2820 if param.kind == _KEYWORD_ONLY}
2821
2822 return params, kwo_params, self.return_annotation
2823
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002824 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002825 params, kwo_params, return_annotation = self._hash_basis()
2826 kwo_params = frozenset(kwo_params.values())
2827 return hash((params, kwo_params, return_annotation))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002828
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002829 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002830 if self is other:
2831 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002832 if not isinstance(other, Signature):
2833 return NotImplemented
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002834 return self._hash_basis() == other._hash_basis()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002835
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002836 def _bind(self, args, kwargs, *, partial=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002837 """Private method. Don't use directly."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002838
2839 arguments = OrderedDict()
2840
2841 parameters = iter(self.parameters.values())
2842 parameters_ex = ()
2843 arg_vals = iter(args)
2844
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002845 while True:
2846 # Let's iterate through the positional arguments and corresponding
2847 # parameters
2848 try:
2849 arg_val = next(arg_vals)
2850 except StopIteration:
2851 # No more positional arguments
2852 try:
2853 param = next(parameters)
2854 except StopIteration:
2855 # No more parameters. That's it. Just need to check that
2856 # we have no `kwargs` after this while loop
2857 break
2858 else:
2859 if param.kind == _VAR_POSITIONAL:
2860 # That's OK, just empty *args. Let's start parsing
2861 # kwargs
2862 break
2863 elif param.name in kwargs:
2864 if param.kind == _POSITIONAL_ONLY:
2865 msg = '{arg!r} parameter is positional only, ' \
2866 'but was passed as a keyword'
2867 msg = msg.format(arg=param.name)
2868 raise TypeError(msg) from None
2869 parameters_ex = (param,)
2870 break
2871 elif (param.kind == _VAR_KEYWORD or
2872 param.default is not _empty):
2873 # That's fine too - we have a default value for this
2874 # parameter. So, lets start parsing `kwargs`, starting
2875 # with the current parameter
2876 parameters_ex = (param,)
2877 break
2878 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002879 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
2880 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002881 if partial:
2882 parameters_ex = (param,)
2883 break
2884 else:
Yury Selivanov86872752015-05-19 00:27:49 -04002885 msg = 'missing a required argument: {arg!r}'
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002886 msg = msg.format(arg=param.name)
2887 raise TypeError(msg) from None
2888 else:
2889 # We have a positional argument to process
2890 try:
2891 param = next(parameters)
2892 except StopIteration:
2893 raise TypeError('too many positional arguments') from None
2894 else:
2895 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
2896 # Looks like we have no parameter for this positional
2897 # argument
Yury Selivanov86872752015-05-19 00:27:49 -04002898 raise TypeError(
2899 'too many positional arguments') from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002900
2901 if param.kind == _VAR_POSITIONAL:
2902 # We have an '*args'-like argument, let's fill it with
2903 # all positional arguments we have left and move on to
2904 # the next phase
2905 values = [arg_val]
2906 values.extend(arg_vals)
2907 arguments[param.name] = tuple(values)
2908 break
2909
2910 if param.name in kwargs:
Yury Selivanov86872752015-05-19 00:27:49 -04002911 raise TypeError(
2912 'multiple values for argument {arg!r}'.format(
2913 arg=param.name)) from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002914
2915 arguments[param.name] = arg_val
2916
2917 # Now, we iterate through the remaining parameters to process
2918 # keyword arguments
2919 kwargs_param = None
2920 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002921 if param.kind == _VAR_KEYWORD:
2922 # Memorize that we have a '**kwargs'-like parameter
2923 kwargs_param = param
2924 continue
2925
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002926 if param.kind == _VAR_POSITIONAL:
2927 # Named arguments don't refer to '*args'-like parameters.
2928 # We only arrive here if the positional arguments ended
2929 # before reaching the last parameter before *args.
2930 continue
2931
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002932 param_name = param.name
2933 try:
2934 arg_val = kwargs.pop(param_name)
2935 except KeyError:
2936 # We have no value for this parameter. It's fine though,
2937 # if it has a default value, or it is an '*args'-like
2938 # parameter, left alone by the processing of positional
2939 # arguments.
2940 if (not partial and param.kind != _VAR_POSITIONAL and
2941 param.default is _empty):
Yury Selivanov86872752015-05-19 00:27:49 -04002942 raise TypeError('missing a required argument: {arg!r}'. \
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002943 format(arg=param_name)) from None
2944
2945 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05002946 if param.kind == _POSITIONAL_ONLY:
2947 # This should never happen in case of a properly built
2948 # Signature object (but let's have this check here
2949 # to ensure correct behaviour just in case)
2950 raise TypeError('{arg!r} parameter is positional only, '
2951 'but was passed as a keyword'. \
2952 format(arg=param.name))
2953
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002954 arguments[param_name] = arg_val
2955
2956 if kwargs:
2957 if kwargs_param is not None:
2958 # Process our '**kwargs'-like parameter
2959 arguments[kwargs_param.name] = kwargs
2960 else:
Yury Selivanov86872752015-05-19 00:27:49 -04002961 raise TypeError(
2962 'got an unexpected keyword argument {arg!r}'.format(
2963 arg=next(iter(kwargs))))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002964
2965 return self._bound_arguments_cls(self, arguments)
2966
Yury Selivanovc45873e2014-01-29 12:10:27 -05002967 def bind(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002968 """Get a BoundArguments object, that maps the passed `args`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002969 and `kwargs` to the function's signature. Raises `TypeError`
2970 if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002971 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05002972 return args[0]._bind(args[1:], kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002973
Yury Selivanovc45873e2014-01-29 12:10:27 -05002974 def bind_partial(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002975 """Get a BoundArguments object, that partially maps the
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002976 passed `args` and `kwargs` to the function's signature.
2977 Raises `TypeError` if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002978 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05002979 return args[0]._bind(args[1:], kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002980
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002981 def __reduce__(self):
2982 return (type(self),
2983 (tuple(self._parameters.values()),),
2984 {'_return_annotation': self._return_annotation})
2985
2986 def __setstate__(self, state):
2987 self._return_annotation = state['_return_annotation']
2988
Yury Selivanov374375d2014-03-27 12:41:53 -04002989 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002990 return '<{} {}>'.format(self.__class__.__name__, self)
Yury Selivanov374375d2014-03-27 12:41:53 -04002991
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002992 def __str__(self):
2993 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05002994 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002995 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05002996 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002997 formatted = str(param)
2998
2999 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05003000
3001 if kind == _POSITIONAL_ONLY:
3002 render_pos_only_separator = True
3003 elif render_pos_only_separator:
3004 # It's not a positional-only parameter, and the flag
3005 # is set to 'True' (there were pos-only params before.)
3006 result.append('/')
3007 render_pos_only_separator = False
3008
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003009 if kind == _VAR_POSITIONAL:
3010 # OK, we have an '*args'-like parameter, so we won't need
3011 # a '*' to separate keyword-only arguments
3012 render_kw_only_separator = False
3013 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
3014 # We have a keyword-only parameter to render and we haven't
3015 # rendered an '*args'-like parameter before, so add a '*'
3016 # separator to the parameters list ("foo(arg1, *, arg2)" case)
3017 result.append('*')
3018 # This condition should be only triggered once, so
3019 # reset the flag
3020 render_kw_only_separator = False
3021
3022 result.append(formatted)
3023
Yury Selivanov2393dca2014-01-27 15:07:58 -05003024 if render_pos_only_separator:
3025 # There were only positional-only parameters, hence the
3026 # flag was not reset to 'False'
3027 result.append('/')
3028
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003029 rendered = '({})'.format(', '.join(result))
3030
3031 if self.return_annotation is not _empty:
3032 anno = formatannotation(self.return_annotation)
3033 rendered += ' -> {}'.format(anno)
3034
3035 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003036
Yury Selivanovda396452014-03-27 12:09:24 -04003037
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04003038def signature(obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003039 """Get a signature object for the passed callable."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04003040 return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04003041
3042
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003043def _main():
3044 """ Logic for inspecting an object given at command line """
3045 import argparse
3046 import importlib
3047
3048 parser = argparse.ArgumentParser()
3049 parser.add_argument(
3050 'object',
3051 help="The object to be analysed. "
3052 "It supports the 'module:qualname' syntax")
3053 parser.add_argument(
3054 '-d', '--details', action='store_true',
3055 help='Display info about the module rather than its source code')
3056
3057 args = parser.parse_args()
3058
3059 target = args.object
3060 mod_name, has_attrs, attrs = target.partition(":")
3061 try:
3062 obj = module = importlib.import_module(mod_name)
3063 except Exception as exc:
3064 msg = "Failed to import {} ({}: {})".format(mod_name,
3065 type(exc).__name__,
3066 exc)
3067 print(msg, file=sys.stderr)
3068 exit(2)
3069
3070 if has_attrs:
3071 parts = attrs.split(".")
3072 obj = module
3073 for part in parts:
3074 obj = getattr(obj, part)
3075
3076 if module.__name__ in sys.builtin_module_names:
3077 print("Can't get info for builtin modules.", file=sys.stderr)
3078 exit(1)
3079
3080 if args.details:
3081 print('Target: {}'.format(target))
3082 print('Origin: {}'.format(getsourcefile(module)))
3083 print('Cached: {}'.format(module.__cached__))
3084 if obj is module:
3085 print('Loader: {}'.format(repr(module.__loader__)))
3086 if hasattr(module, '__path__'):
3087 print('Submodule search path: {}'.format(module.__path__))
3088 else:
3089 try:
3090 __, lineno = findsource(obj)
3091 except Exception:
3092 pass
3093 else:
3094 print('Line: {}'.format(lineno))
3095
3096 print('\n')
3097 else:
3098 print(getsource(obj))
3099
3100
3101if __name__ == "__main__":
3102 _main()