blob: 8c398bd3534cb6a7fb949ad1c66d890a4bcd305c [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
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +020021 formatargvalues() - format an argument spec
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000022 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
Antoine Pitroua8723a02015-04-15 00:41:29 +020035import dis
Yury Selivanov75445082015-05-11 22:57:16 -040036import collections.abc
Yury Selivanov21e83a52014-03-27 11:23:13 -040037import enum
Brett Cannoncb66eb02012-05-11 12:58:42 -040038import importlib.machinery
39import itertools
Christian Heimes7131fd92008-02-19 14:21:46 +000040import linecache
Brett Cannoncb66eb02012-05-11 12:58:42 -040041import os
42import re
43import sys
44import tokenize
Larry Hastings2623c8c2014-02-08 22:15:29 -080045import token
Brett Cannoncb66eb02012-05-11 12:58:42 -040046import types
Brett Cannon2b88fcf2012-06-02 22:28:42 -040047import warnings
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070048import functools
Nick Coghlan2f92e542012-06-23 19:39:55 +100049import builtins
Raymond Hettingera1a992c2005-03-11 06:46:45 +000050from operator import attrgetter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070051from collections import namedtuple, OrderedDict
Nick Coghlan09c81232010-08-17 10:18:16 +000052
53# Create constants for the compiler flags in Include/code.h
Antoine Pitroua8723a02015-04-15 00:41:29 +020054# We try to get them from dis to avoid duplication
55mod_dict = globals()
56for k, v in dis.COMPILER_FLAG_NAMES.items():
57 mod_dict["CO_" + v] = k
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000058
Christian Heimesbe5b30b2008-03-03 19:18:51 +000059# See Include/object.h
60TPFLAGS_IS_ABSTRACT = 1 << 20
61
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000062# ----------------------------------------------------------- type-checking
63def ismodule(object):
64 """Return true if the object is a module.
65
66 Module objects provide these attributes:
Barry Warsaw28a691b2010-04-17 00:19:56 +000067 __cached__ pathname to byte compiled file
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000068 __doc__ documentation string
69 __file__ filename (missing for built-in modules)"""
Tim Peters28bc59f2001-09-16 08:40:16 +000070 return isinstance(object, types.ModuleType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000071
72def isclass(object):
73 """Return true if the object is a class.
74
75 Class objects provide these attributes:
76 __doc__ documentation string
77 __module__ name of module in which this class was defined"""
Benjamin Petersonc4656002009-01-17 22:41:18 +000078 return isinstance(object, type)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000079
80def ismethod(object):
81 """Return true if the object is an instance method.
82
83 Instance method objects provide these attributes:
84 __doc__ documentation string
85 __name__ name with which this method was defined
Christian Heimesff737952007-11-27 10:40:20 +000086 __func__ function object containing implementation of method
87 __self__ instance to which this method is bound"""
Tim Peters28bc59f2001-09-16 08:40:16 +000088 return isinstance(object, types.MethodType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000089
Tim Peters536d2262001-09-20 05:13:38 +000090def ismethoddescriptor(object):
Tim Petersf1d90b92001-09-20 05:47:55 +000091 """Return true if the object is a method descriptor.
92
93 But not if ismethod() or isclass() or isfunction() are true.
Tim Peters536d2262001-09-20 05:13:38 +000094
95 This is new in Python 2.2, and, for example, is true of int.__add__.
96 An object passing this test has a __get__ attribute but not a __set__
97 attribute, but beyond that the set of attributes varies. __name__ is
98 usually sensible, and __doc__ often is.
99
Tim Petersf1d90b92001-09-20 05:47:55 +0000100 Methods implemented via descriptors that also pass one of the other
101 tests return false from the ismethoddescriptor() test, simply because
102 the other tests promise more -- you can, e.g., count on having the
Christian Heimesff737952007-11-27 10:40:20 +0000103 __func__ attribute (etc) when an object passes ismethod()."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100104 if isclass(object) or ismethod(object) or isfunction(object):
105 # mutual exclusion
106 return False
107 tp = type(object)
108 return hasattr(tp, "__get__") and not hasattr(tp, "__set__")
Tim Peters536d2262001-09-20 05:13:38 +0000109
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000110def isdatadescriptor(object):
111 """Return true if the object is a data descriptor.
112
Aaron Hall, MBA4054b172018-05-20 19:46:42 -0400113 Data descriptors have a __set__ or a __delete__ attribute. Examples are
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000114 properties (defined in Python) and getsets and members (defined in C).
115 Typically, data descriptors will also have __name__ and __doc__ attributes
116 (properties, getsets, and members have both of these attributes), but this
117 is not guaranteed."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100118 if isclass(object) or ismethod(object) or isfunction(object):
119 # mutual exclusion
120 return False
121 tp = type(object)
Aaron Hall, MBA4054b172018-05-20 19:46:42 -0400122 return hasattr(tp, "__set__") or hasattr(tp, "__delete__")
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000123
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000124if hasattr(types, 'MemberDescriptorType'):
125 # CPython and equivalent
126 def ismemberdescriptor(object):
127 """Return true if the object is a member descriptor.
128
129 Member descriptors are specialized descriptors defined in extension
130 modules."""
131 return isinstance(object, types.MemberDescriptorType)
132else:
133 # Other implementations
134 def ismemberdescriptor(object):
135 """Return true if the object is a member descriptor.
136
137 Member descriptors are specialized descriptors defined in extension
138 modules."""
139 return False
140
141if hasattr(types, 'GetSetDescriptorType'):
142 # CPython and equivalent
143 def isgetsetdescriptor(object):
144 """Return true if the object is a getset descriptor.
145
146 getset descriptors are specialized descriptors defined in extension
147 modules."""
148 return isinstance(object, types.GetSetDescriptorType)
149else:
150 # Other implementations
151 def isgetsetdescriptor(object):
152 """Return true if the object is a getset descriptor.
153
154 getset descriptors are specialized descriptors defined in extension
155 modules."""
156 return False
157
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000158def isfunction(object):
159 """Return true if the object is a user-defined function.
160
161 Function objects provide these attributes:
162 __doc__ documentation string
163 __name__ name with which this function was defined
Neal Norwitz221085d2007-02-25 20:55:47 +0000164 __code__ code object containing compiled function bytecode
165 __defaults__ tuple of any default values for arguments
166 __globals__ global namespace in which this function was defined
167 __annotations__ dict of parameter annotations
168 __kwdefaults__ dict of keyword only parameters with defaults"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000169 return isinstance(object, types.FunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000170
Pablo Galindo7cd25432018-10-26 12:19:14 +0100171def isgeneratorfunction(obj):
Christian Heimes7131fd92008-02-19 14:21:46 +0000172 """Return true if the object is a user-defined generator function.
173
Martin Panter0f0eac42016-09-07 11:04:41 +0000174 Generator function objects provide the same attributes as functions.
175 See help(isfunction) for a list of attributes."""
Pablo Galindo7cd25432018-10-26 12:19:14 +0100176 obj = functools._unwrap_partial(obj)
177 return bool((isfunction(obj) or ismethod(obj)) and
178 obj.__code__.co_flags & CO_GENERATOR)
Yury Selivanov75445082015-05-11 22:57:16 -0400179
Pablo Galindo7cd25432018-10-26 12:19:14 +0100180def iscoroutinefunction(obj):
Yury Selivanov75445082015-05-11 22:57:16 -0400181 """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 """
Pablo Galindo7cd25432018-10-26 12:19:14 +0100185 obj = functools._unwrap_partial(obj)
186 return bool(((isfunction(obj) or ismethod(obj)) and
187 obj.__code__.co_flags & CO_COROUTINE))
Yury Selivanov75445082015-05-11 22:57:16 -0400188
Pablo Galindo7cd25432018-10-26 12:19:14 +0100189def isasyncgenfunction(obj):
Yury Selivanov4778e132016-11-08 12:23:09 -0500190 """Return true if the object is an asynchronous generator function.
191
192 Asynchronous generator functions are defined with "async def"
193 syntax and have "yield" expressions in their body.
194 """
Pablo Galindo7cd25432018-10-26 12:19:14 +0100195 obj = functools._unwrap_partial(obj)
196 return bool((isfunction(obj) or ismethod(obj)) and
197 obj.__code__.co_flags & CO_ASYNC_GENERATOR)
Yury Selivanoveb636452016-09-08 22:01:51 -0700198
199def isasyncgen(object):
Yury Selivanov4778e132016-11-08 12:23:09 -0500200 """Return true if the object is an asynchronous generator."""
Yury Selivanoveb636452016-09-08 22:01:51 -0700201 return isinstance(object, types.AsyncGeneratorType)
202
Christian Heimes7131fd92008-02-19 14:21:46 +0000203def isgenerator(object):
204 """Return true if the object is a generator.
205
206 Generator objects provide these attributes:
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300207 __iter__ defined to support iteration over container
Christian Heimes7131fd92008-02-19 14:21:46 +0000208 close raises a new GeneratorExit exception inside the
209 generator to terminate the iteration
210 gi_code code object
211 gi_frame frame object or possibly None once the generator has
212 been exhausted
213 gi_running set to 1 when generator is executing, 0 otherwise
214 next return the next item from the container
215 send resumes the generator and "sends" a value that becomes
216 the result of the current yield-expression
217 throw used to raise an exception inside the generator"""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400218 return isinstance(object, types.GeneratorType)
Yury Selivanov75445082015-05-11 22:57:16 -0400219
220def iscoroutine(object):
221 """Return true if the object is a coroutine."""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400222 return isinstance(object, types.CoroutineType)
Christian Heimes7131fd92008-02-19 14:21:46 +0000223
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400224def isawaitable(object):
Yury Selivanovc0215df2016-11-08 19:57:44 -0500225 """Return true if object can be passed to an ``await`` expression."""
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400226 return (isinstance(object, types.CoroutineType) or
227 isinstance(object, types.GeneratorType) and
Yury Selivanovc0215df2016-11-08 19:57:44 -0500228 bool(object.gi_code.co_flags & CO_ITERABLE_COROUTINE) or
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400229 isinstance(object, collections.abc.Awaitable))
230
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000231def istraceback(object):
232 """Return true if the object is a traceback.
233
234 Traceback objects provide these attributes:
235 tb_frame frame object at this level
236 tb_lasti index of last attempted instruction in bytecode
237 tb_lineno current line number in Python source code
238 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000239 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000240
241def isframe(object):
242 """Return true if the object is a frame object.
243
244 Frame objects provide these attributes:
245 f_back next outer frame object (this frame's caller)
246 f_builtins built-in namespace seen by this frame
247 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000248 f_globals global namespace seen by this frame
249 f_lasti index of last attempted instruction in bytecode
250 f_lineno current line number in Python source code
251 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000252 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000253 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000254
255def iscode(object):
256 """Return true if the object is a code object.
257
258 Code objects provide these attributes:
Xiang Zhanga6902e62017-04-13 10:38:28 +0800259 co_argcount number of arguments (not including *, ** args
260 or keyword only arguments)
261 co_code string of raw compiled bytecode
262 co_cellvars tuple of names of cell variables
263 co_consts tuple of constants used in the bytecode
264 co_filename name of file in which this code object was created
265 co_firstlineno number of first line in Python source code
266 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
267 | 16=nested | 32=generator | 64=nofree | 128=coroutine
268 | 256=iterable_coroutine | 512=async_generator
269 co_freevars tuple of names of free variables
270 co_kwonlyargcount number of keyword only arguments (not including ** arg)
271 co_lnotab encoded mapping of line numbers to bytecode indices
272 co_name name with which this code object was defined
273 co_names tuple of names of local variables
274 co_nlocals number of local variables
275 co_stacksize virtual machine stack space required
276 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000277 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000278
279def isbuiltin(object):
280 """Return true if the object is a built-in function or method.
281
282 Built-in functions and methods provide these attributes:
283 __doc__ documentation string
284 __name__ original name of this function or method
285 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000286 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000287
288def isroutine(object):
289 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000290 return (isbuiltin(object)
291 or isfunction(object)
292 or ismethod(object)
293 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000294
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000295def isabstract(object):
296 """Return true if the object is an abstract base class (ABC)."""
Natefcfe80e2017-04-24 10:06:15 -0700297 if not isinstance(object, type):
298 return False
299 if object.__flags__ & TPFLAGS_IS_ABSTRACT:
300 return True
301 if not issubclass(type(object), abc.ABCMeta):
302 return False
303 if hasattr(object, '__abstractmethods__'):
304 # It looks like ABCMeta.__new__ has finished running;
305 # TPFLAGS_IS_ABSTRACT should have been accurate.
306 return False
307 # It looks like ABCMeta.__new__ has not finished running yet; we're
308 # probably in __init_subclass__. We'll look for abstractmethods manually.
309 for name, value in object.__dict__.items():
310 if getattr(value, "__isabstractmethod__", False):
311 return True
312 for base in object.__bases__:
313 for name in getattr(base, "__abstractmethods__", ()):
314 value = getattr(object, name, None)
315 if getattr(value, "__isabstractmethod__", False):
316 return True
317 return False
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000318
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000319def getmembers(object, predicate=None):
320 """Return all members of an object as (name, value) pairs sorted by name.
321 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100322 if isclass(object):
323 mro = (object,) + getmro(object)
324 else:
325 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000326 results = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700327 processed = set()
328 names = dir(object)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700329 # :dd any DynamicClassAttributes to the list of names if object is a class;
Ethan Furmane03ea372013-09-25 07:14:41 -0700330 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700331 # attribute with the same name as a DynamicClassAttribute exists
Ethan Furmane03ea372013-09-25 07:14:41 -0700332 try:
333 for base in object.__bases__:
334 for k, v in base.__dict__.items():
335 if isinstance(v, types.DynamicClassAttribute):
336 names.append(k)
337 except AttributeError:
338 pass
339 for key in names:
Ethan Furman63c141c2013-10-18 00:27:39 -0700340 # First try to get the value via getattr. Some descriptors don't
341 # like calling their __get__ (see bug #1785), so fall back to
342 # looking in the __dict__.
343 try:
344 value = getattr(object, key)
345 # handle the duplicate key
346 if key in processed:
347 raise AttributeError
348 except AttributeError:
349 for base in mro:
350 if key in base.__dict__:
351 value = base.__dict__[key]
352 break
353 else:
354 # could be a (currently) missing slot member, or a buggy
355 # __dir__; discard and move on
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100356 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000357 if not predicate or predicate(value):
358 results.append((key, value))
Ethan Furmane03ea372013-09-25 07:14:41 -0700359 processed.add(key)
360 results.sort(key=lambda pair: pair[0])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000361 return results
362
Christian Heimes25bb7832008-01-11 16:17:00 +0000363Attribute = namedtuple('Attribute', 'name kind defining_class object')
364
Tim Peters13b49d32001-09-23 02:00:29 +0000365def classify_class_attrs(cls):
366 """Return list of attribute-descriptor tuples.
367
368 For each name in dir(cls), the return list contains a 4-tuple
369 with these elements:
370
371 0. The name (a string).
372
373 1. The kind of attribute this is, one of these strings:
374 'class method' created via classmethod()
375 'static method' created via staticmethod()
376 'property' created via property()
Ethan Furmane03ea372013-09-25 07:14:41 -0700377 'method' any other flavor of method or descriptor
Tim Peters13b49d32001-09-23 02:00:29 +0000378 'data' not a method
379
380 2. The class which defined this attribute (a class).
381
Ethan Furmane03ea372013-09-25 07:14:41 -0700382 3. The object as obtained by calling getattr; if this fails, or if the
383 resulting object does not live anywhere in the class' mro (including
384 metaclasses) then the object is looked up in the defining class's
385 dict (found by walking the mro).
Ethan Furman668dede2013-09-14 18:53:26 -0700386
387 If one of the items in dir(cls) is stored in the metaclass it will now
388 be discovered and not have None be listed as the class in which it was
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700389 defined. Any items whose home class cannot be discovered are skipped.
Tim Peters13b49d32001-09-23 02:00:29 +0000390 """
391
392 mro = getmro(cls)
Ethan Furman668dede2013-09-14 18:53:26 -0700393 metamro = getmro(type(cls)) # for attributes stored in the metaclass
Jon Dufresne39726282017-05-18 07:35:54 -0700394 metamro = tuple(cls for cls in metamro if cls not in (type, object))
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700395 class_bases = (cls,) + mro
396 all_bases = class_bases + metamro
Tim Peters13b49d32001-09-23 02:00:29 +0000397 names = dir(cls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700398 # :dd any DynamicClassAttributes to the list of names;
Ethan Furmane03ea372013-09-25 07:14:41 -0700399 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700400 # attribute with the same name as a DynamicClassAttribute exists.
Ethan Furman63c141c2013-10-18 00:27:39 -0700401 for base in mro:
Ethan Furmane03ea372013-09-25 07:14:41 -0700402 for k, v in base.__dict__.items():
403 if isinstance(v, types.DynamicClassAttribute):
404 names.append(k)
Tim Peters13b49d32001-09-23 02:00:29 +0000405 result = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700406 processed = set()
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700407
Tim Peters13b49d32001-09-23 02:00:29 +0000408 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100409 # Get the object associated with the name, and where it was defined.
Ethan Furmane03ea372013-09-25 07:14:41 -0700410 # Normal objects will be looked up with both getattr and directly in
411 # its class' dict (in case getattr fails [bug #1785], and also to look
412 # for a docstring).
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700413 # For DynamicClassAttributes on the second pass we only look in the
Ethan Furmane03ea372013-09-25 07:14:41 -0700414 # class's dict.
415 #
Tim Peters13b49d32001-09-23 02:00:29 +0000416 # Getting an obj from the __dict__ sometimes reveals more than
417 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100418 homecls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700419 get_obj = None
420 dict_obj = None
Ethan Furmane03ea372013-09-25 07:14:41 -0700421 if name not in processed:
422 try:
Ethan Furmana8b07072013-10-18 01:22:08 -0700423 if name == '__dict__':
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700424 raise Exception("__dict__ is special, don't want the proxy")
Ethan Furmane03ea372013-09-25 07:14:41 -0700425 get_obj = getattr(cls, name)
426 except Exception as exc:
427 pass
428 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700429 homecls = getattr(get_obj, "__objclass__", homecls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700430 if homecls not in class_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700431 # if the resulting object does not live somewhere in the
Ethan Furman63c141c2013-10-18 00:27:39 -0700432 # mro, drop it and search the mro manually
Ethan Furmane03ea372013-09-25 07:14:41 -0700433 homecls = None
Ethan Furman63c141c2013-10-18 00:27:39 -0700434 last_cls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700435 # first look in the classes
436 for srch_cls in class_bases:
Ethan Furman63c141c2013-10-18 00:27:39 -0700437 srch_obj = getattr(srch_cls, name, None)
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400438 if srch_obj is get_obj:
Ethan Furman63c141c2013-10-18 00:27:39 -0700439 last_cls = srch_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700440 # then check the metaclasses
441 for srch_cls in metamro:
442 try:
443 srch_obj = srch_cls.__getattr__(cls, name)
444 except AttributeError:
445 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400446 if srch_obj is get_obj:
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700447 last_cls = srch_cls
Ethan Furman63c141c2013-10-18 00:27:39 -0700448 if last_cls is not None:
449 homecls = last_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700450 for base in all_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700451 if name in base.__dict__:
452 dict_obj = base.__dict__[name]
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700453 if homecls not in metamro:
454 homecls = base
Ethan Furmane03ea372013-09-25 07:14:41 -0700455 break
Ethan Furman63c141c2013-10-18 00:27:39 -0700456 if homecls is None:
457 # unable to locate the attribute anywhere, most likely due to
458 # buggy custom __dir__; discard and move on
459 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400460 obj = get_obj if get_obj is not None else dict_obj
Ethan Furmane03ea372013-09-25 07:14:41 -0700461 # Classify the object or its descriptor.
Serhiy Storchaka3327a2d2017-12-15 14:13:41 +0200462 if isinstance(dict_obj, (staticmethod, types.BuiltinMethodType)):
Tim Peters13b49d32001-09-23 02:00:29 +0000463 kind = "static method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700464 obj = dict_obj
Serhiy Storchaka3327a2d2017-12-15 14:13:41 +0200465 elif isinstance(dict_obj, (classmethod, types.ClassMethodDescriptorType)):
Tim Peters13b49d32001-09-23 02:00:29 +0000466 kind = "class method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700467 obj = dict_obj
468 elif isinstance(dict_obj, property):
Tim Peters13b49d32001-09-23 02:00:29 +0000469 kind = "property"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700470 obj = dict_obj
Yury Selivanov0860a0b2014-01-31 14:28:44 -0500471 elif isroutine(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000472 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100473 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700474 kind = "data"
Christian Heimes25bb7832008-01-11 16:17:00 +0000475 result.append(Attribute(name, kind, homecls, obj))
Ethan Furmane03ea372013-09-25 07:14:41 -0700476 processed.add(name)
Tim Peters13b49d32001-09-23 02:00:29 +0000477 return result
478
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000479# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000480
481def getmro(cls):
482 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000483 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000484
Nick Coghlane8c45d62013-07-28 20:00:01 +1000485# -------------------------------------------------------- function helpers
486
487def unwrap(func, *, stop=None):
488 """Get the object wrapped by *func*.
489
490 Follows the chain of :attr:`__wrapped__` attributes returning the last
491 object in the chain.
492
493 *stop* is an optional callback accepting an object in the wrapper chain
494 as its sole argument that allows the unwrapping to be terminated early if
495 the callback returns a true value. If the callback never returns a true
496 value, the last object in the chain is returned as usual. For example,
497 :func:`signature` uses this to stop unwrapping if any object in the
498 chain has a ``__signature__`` attribute defined.
499
500 :exc:`ValueError` is raised if a cycle is encountered.
501
502 """
503 if stop is None:
504 def _is_wrapper(f):
505 return hasattr(f, '__wrapped__')
506 else:
507 def _is_wrapper(f):
508 return hasattr(f, '__wrapped__') and not stop(f)
509 f = func # remember the original func for error reporting
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100510 # Memoise by id to tolerate non-hashable objects, but store objects to
511 # ensure they aren't destroyed, which would allow their IDs to be reused.
512 memo = {id(f): f}
513 recursion_limit = sys.getrecursionlimit()
Nick Coghlane8c45d62013-07-28 20:00:01 +1000514 while _is_wrapper(func):
515 func = func.__wrapped__
516 id_func = id(func)
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100517 if (id_func in memo) or (len(memo) >= recursion_limit):
Nick Coghlane8c45d62013-07-28 20:00:01 +1000518 raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100519 memo[id_func] = func
Nick Coghlane8c45d62013-07-28 20:00:01 +1000520 return func
521
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000522# -------------------------------------------------- source code extraction
523def indentsize(line):
524 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000525 expline = line.expandtabs()
526 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000527
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300528def _findclass(func):
529 cls = sys.modules.get(func.__module__)
530 if cls is None:
531 return None
532 for name in func.__qualname__.split('.')[:-1]:
533 cls = getattr(cls, name)
534 if not isclass(cls):
535 return None
536 return cls
537
538def _finddoc(obj):
539 if isclass(obj):
540 for base in obj.__mro__:
541 if base is not object:
542 try:
543 doc = base.__doc__
544 except AttributeError:
545 continue
546 if doc is not None:
547 return doc
548 return None
549
550 if ismethod(obj):
551 name = obj.__func__.__name__
552 self = obj.__self__
553 if (isclass(self) and
554 getattr(getattr(self, name, None), '__func__') is obj.__func__):
555 # classmethod
556 cls = self
557 else:
558 cls = self.__class__
559 elif isfunction(obj):
560 name = obj.__name__
561 cls = _findclass(obj)
562 if cls is None or getattr(cls, name) is not obj:
563 return None
564 elif isbuiltin(obj):
565 name = obj.__name__
566 self = obj.__self__
567 if (isclass(self) and
568 self.__qualname__ + '.' + name == obj.__qualname__):
569 # classmethod
570 cls = self
571 else:
572 cls = self.__class__
Serhiy Storchakaac4bdcc2015-10-29 08:15:50 +0200573 # Should be tested before isdatadescriptor().
574 elif isinstance(obj, property):
575 func = obj.fget
576 name = func.__name__
577 cls = _findclass(func)
578 if cls is None or getattr(cls, name) is not obj:
579 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300580 elif ismethoddescriptor(obj) or isdatadescriptor(obj):
581 name = obj.__name__
582 cls = obj.__objclass__
583 if getattr(cls, name) is not obj:
584 return None
Raymond Hettingerd1e768a2019-03-25 13:01:13 -0700585 if ismemberdescriptor(obj):
586 slots = getattr(cls, '__slots__', None)
587 if isinstance(slots, dict) and name in slots:
588 return slots[name]
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300589 else:
590 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300591 for base in cls.__mro__:
592 try:
593 doc = getattr(base, name).__doc__
594 except AttributeError:
595 continue
596 if doc is not None:
597 return doc
598 return None
599
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000600def getdoc(object):
601 """Get the documentation string for an object.
602
603 All tabs are expanded to spaces. To clean up docstrings that are
604 indented to line up with blocks of code, any whitespace than can be
605 uniformly removed from the second line onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000606 try:
607 doc = object.__doc__
608 except AttributeError:
609 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300610 if doc is None:
611 try:
612 doc = _finddoc(object)
613 except (AttributeError, TypeError):
614 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000615 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000616 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000617 return cleandoc(doc)
618
619def cleandoc(doc):
620 """Clean up indentation from docstrings.
621
622 Any whitespace that can be uniformly removed from the second line
623 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000624 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000625 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000626 except UnicodeError:
627 return None
628 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000629 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000630 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000631 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000632 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000633 if content:
634 indent = len(line) - content
635 margin = min(margin, indent)
636 # Remove indentation.
637 if lines:
638 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000639 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000640 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000641 # Remove any trailing or leading blank lines.
642 while lines and not lines[-1]:
643 lines.pop()
644 while lines and not lines[0]:
645 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000646 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000647
648def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000649 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000650 if ismodule(object):
Jason R. Coombsb9650a02018-03-05 18:29:08 -0500651 if getattr(object, '__file__', None):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000652 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000653 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000654 if isclass(object):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500655 if hasattr(object, '__module__'):
656 object = sys.modules.get(object.__module__)
Jason R. Coombsb9650a02018-03-05 18:29:08 -0500657 if getattr(object, '__file__', None):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500658 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000659 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000660 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000661 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000662 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000663 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000664 if istraceback(object):
665 object = object.tb_frame
666 if isframe(object):
667 object = object.f_code
668 if iscode(object):
669 return object.co_filename
Thomas Kluyvere968bc732017-10-24 13:42:36 +0100670 raise TypeError('module, class, method, function, traceback, frame, or '
671 'code object was expected, got {}'.format(
672 type(object).__name__))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000673
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000674def getmodulename(path):
675 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000676 fname = os.path.basename(path)
677 # Check for paths that look like an actual module file
678 suffixes = [(-len(suffix), suffix)
679 for suffix in importlib.machinery.all_suffixes()]
680 suffixes.sort() # try longest suffixes first, in case they overlap
681 for neglen, suffix in suffixes:
682 if fname.endswith(suffix):
683 return fname[:neglen]
684 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000685
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000686def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000687 """Return the filename that can be used to locate an object's source.
688 Return None if no way can be identified to get the source.
689 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000690 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400691 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
692 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
693 if any(filename.endswith(s) for s in all_bytecode_suffixes):
694 filename = (os.path.splitext(filename)[0] +
695 importlib.machinery.SOURCE_SUFFIXES[0])
696 elif any(filename.endswith(s) for s in
697 importlib.machinery.EXTENSION_SUFFIXES):
698 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000699 if os.path.exists(filename):
700 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000701 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400702 if getattr(getmodule(object, filename), '__loader__', None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000703 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000704 # or it is in the linecache
705 if filename in linecache.cache:
706 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000707
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000708def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000709 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000710
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000711 The idea is for each object to have a unique origin, so this routine
712 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000713 if _filename is None:
714 _filename = getsourcefile(object) or getfile(object)
715 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000716
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000717modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000718_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000719
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000720def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000721 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000722 if ismodule(object):
723 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000724 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000725 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000726 # Try the filename to modulename cache
727 if _filename is not None and _filename in modulesbyfile:
728 return sys.modules.get(modulesbyfile[_filename])
729 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000730 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000731 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000732 except TypeError:
733 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000734 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000735 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000736 # Update the filename to module name cache and check yet again
737 # Copy sys.modules in order to cope with changes while iterating
Éric Araujoa74f8ef2011-11-29 16:58:53 +0100738 for modname, module in list(sys.modules.items()):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000739 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000740 f = module.__file__
741 if f == _filesbymodname.get(modname, None):
742 # Have already mapped this module, so skip it
743 continue
744 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000745 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000746 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000747 modulesbyfile[f] = modulesbyfile[
748 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000749 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000750 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000751 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000752 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000753 if not hasattr(object, '__name__'):
754 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000755 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000756 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000757 if mainobject is object:
758 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000759 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000760 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000761 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000762 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000763 if builtinobject is object:
764 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000765
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000766def findsource(object):
767 """Return the entire source file and starting line number for an object.
768
769 The argument may be a module, class, method, function, traceback, frame,
770 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200771 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000772 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500773
Yury Selivanovef1e7502014-12-08 16:05:34 -0500774 file = getsourcefile(object)
775 if file:
776 # Invalidate cache if needed.
777 linecache.checkcache(file)
778 else:
779 file = getfile(object)
780 # Allow filenames in form of "<something>" to pass through.
781 # `doctest` monkeypatches `linecache` module to enable
782 # inspection, so let `linecache.getlines` to be called.
783 if not (file.startswith('<') and file.endswith('>')):
784 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500785
Thomas Wouters89f507f2006-12-13 04:49:30 +0000786 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000787 if module:
788 lines = linecache.getlines(file, module.__dict__)
789 else:
790 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000791 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200792 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000793
794 if ismodule(object):
795 return lines, 0
796
797 if isclass(object):
798 name = object.__name__
Thomas Wouters89f507f2006-12-13 04:49:30 +0000799 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
800 # make some effort to find the best matching class definition:
801 # use the one with the least indentation, which is the one
802 # that's most probably not inside a function definition.
803 candidates = []
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000804 for i in range(len(lines)):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000805 match = pat.match(lines[i])
806 if match:
807 # if it's at toplevel, it's already the best one
808 if lines[i][0] == 'c':
809 return lines, i
810 # else add whitespace to candidate list
811 candidates.append((match.group(1), i))
812 if candidates:
813 # this will sort by whitespace, and by line number,
814 # less whitespace first
815 candidates.sort()
816 return lines, candidates[0][1]
Jeremy Hyltonab919022003-06-27 18:41:20 +0000817 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200818 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000819
820 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000821 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000822 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000823 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000824 if istraceback(object):
825 object = object.tb_frame
826 if isframe(object):
827 object = object.f_code
828 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000829 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200830 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000831 lnum = object.co_firstlineno - 1
Yury Selivanove4e811d2015-07-21 19:01:52 +0300832 pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000833 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000834 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000835 lnum = lnum - 1
836 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200837 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000838
839def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000840 """Get lines of comments immediately preceding an object's source code.
841
842 Returns None when source can't be found.
843 """
844 try:
845 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200846 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000847 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000848
849 if ismodule(object):
850 # Look for a comment block at the top of the file.
851 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000852 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000853 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000854 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000855 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000856 comments = []
857 end = start
858 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000859 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000860 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000861 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000862
863 # Look for a preceding block of comments at the same indentation.
864 elif lnum > 0:
865 indent = indentsize(lines[lnum])
866 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000867 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000868 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000869 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000870 if end > 0:
871 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000872 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000873 while comment[:1] == '#' and indentsize(lines[end]) == indent:
874 comments[:0] = [comment]
875 end = end - 1
876 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000877 comment = lines[end].expandtabs().lstrip()
878 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000879 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000880 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000881 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000882 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000883
Tim Peters4efb6e92001-06-29 23:51:08 +0000884class EndOfBlock(Exception): pass
885
886class BlockFinder:
887 """Provide a tokeneater() method to detect the end of a code block."""
888 def __init__(self):
889 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000890 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000891 self.started = False
892 self.passline = False
Meador Inge5b718d72015-07-23 22:49:37 -0500893 self.indecorator = False
894 self.decoratorhasargs = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000895 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000896
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000897 def tokeneater(self, type, token, srowcol, erowcol, line):
Meador Inge5b718d72015-07-23 22:49:37 -0500898 if not self.started and not self.indecorator:
899 # skip any decorators
900 if token == "@":
901 self.indecorator = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000902 # look for the first "def", "class" or "lambda"
Meador Inge5b718d72015-07-23 22:49:37 -0500903 elif token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000904 if token == "lambda":
905 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000906 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000907 self.passline = True # skip to the end of the line
Meador Inge5b718d72015-07-23 22:49:37 -0500908 elif token == "(":
909 if self.indecorator:
910 self.decoratorhasargs = True
911 elif token == ")":
912 if self.indecorator:
913 self.indecorator = False
914 self.decoratorhasargs = False
Tim Peters4efb6e92001-06-29 23:51:08 +0000915 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000916 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000917 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000918 if self.islambda: # lambdas always end at the first NEWLINE
919 raise EndOfBlock
Meador Inge5b718d72015-07-23 22:49:37 -0500920 # hitting a NEWLINE when in a decorator without args
921 # ends the decorator
922 if self.indecorator and not self.decoratorhasargs:
923 self.indecorator = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000924 elif self.passline:
925 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000926 elif type == tokenize.INDENT:
927 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000928 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000929 elif type == tokenize.DEDENT:
930 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000931 # the end of matching indent/dedent pairs end a block
932 # (note that this only works for "def"/"class" blocks,
933 # not e.g. for "if: else:" or "try: finally:" blocks)
934 if self.indent <= 0:
935 raise EndOfBlock
936 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
937 # any other token on the same indentation level end the previous
938 # block as well, except the pseudo-tokens COMMENT and NL.
939 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000940
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000941def getblock(lines):
942 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000943 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000944 try:
Trent Nelson428de652008-03-18 22:41:35 +0000945 tokens = tokenize.generate_tokens(iter(lines).__next__)
946 for _token in tokens:
947 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000948 except (EndOfBlock, IndentationError):
949 pass
950 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000951
952def getsourcelines(object):
953 """Return a list of source lines and starting line number for an object.
954
955 The argument may be a module, class, method, function, traceback, frame,
956 or code object. The source code is returned as a list of the lines
957 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200958 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000959 raised if the source code cannot be retrieved."""
Yury Selivanov081bbf62014-09-26 17:34:54 -0400960 object = unwrap(object)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000961 lines, lnum = findsource(object)
962
Vladimir Matveev91cb2982018-08-24 07:18:00 -0700963 if istraceback(object):
964 object = object.tb_frame
965
966 # for module or frame that corresponds to module, return all source lines
967 if (ismodule(object) or
968 (isframe(object) and object.f_code.co_name == "<module>")):
Meador Inge5b718d72015-07-23 22:49:37 -0500969 return lines, 0
970 else:
971 return getblock(lines[lnum:]), lnum + 1
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000972
973def getsource(object):
974 """Return the text of the source code for an object.
975
976 The argument may be a module, class, method, function, traceback, frame,
977 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200978 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000979 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000980 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000981
982# --------------------------------------------------- class tree extraction
983def walktree(classes, children, parent):
984 """Recursive helper function for getclasstree()."""
985 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +0000986 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000987 for c in classes:
988 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +0000989 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000990 results.append(walktree(children[c], children, c))
991 return results
992
Georg Brandl5ce83a02009-06-01 17:23:51 +0000993def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000994 """Arrange the given list of classes into a hierarchy of nested lists.
995
996 Where a nested list appears, it contains classes derived from the class
997 whose entry immediately precedes the list. Each entry is a 2-tuple
998 containing a class and a tuple of its base classes. If the 'unique'
999 argument is true, exactly one entry appears in the returned structure
1000 for each class in the given list. Otherwise, classes using multiple
1001 inheritance and their descendants will appear multiple times."""
1002 children = {}
1003 roots = []
1004 for c in classes:
1005 if c.__bases__:
1006 for parent in c.__bases__:
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05301007 if parent not in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001008 children[parent] = []
Serhiy Storchaka362c1b52013-09-05 17:14:32 +03001009 if c not in children[parent]:
1010 children[parent].append(c)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001011 if unique and parent in classes: break
1012 elif c not in roots:
1013 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +00001014 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001015 if parent not in classes:
1016 roots.append(parent)
1017 return walktree(roots, children, None)
1018
1019# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001020Arguments = namedtuple('Arguments', 'args, varargs, varkw')
1021
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001022def getargs(co):
1023 """Get information about the arguments accepted by a code object.
1024
Guido van Rossum2e65f892007-02-28 22:03:49 +00001025 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001026 'args' is the list of argument names. Keyword-only arguments are
1027 appended. 'varargs' and 'varkw' are the names of the * and **
1028 arguments or None."""
Guido van Rossum2e65f892007-02-28 22:03:49 +00001029 args, varargs, kwonlyargs, varkw = _getfullargs(co)
Christian Heimes25bb7832008-01-11 16:17:00 +00001030 return Arguments(args + kwonlyargs, varargs, varkw)
Guido van Rossum2e65f892007-02-28 22:03:49 +00001031
1032def _getfullargs(co):
1033 """Get information about the arguments accepted by a code object.
1034
1035 Four things are returned: (args, varargs, kwonlyargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001036 'args' and 'kwonlyargs' are lists of argument names, and 'varargs'
1037 and 'varkw' are the names of the * and ** arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +00001038
1039 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001040 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001041
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001042 nargs = co.co_argcount
1043 names = co.co_varnames
Guido van Rossum2e65f892007-02-28 22:03:49 +00001044 nkwargs = co.co_kwonlyargcount
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001045 args = list(names[:nargs])
Guido van Rossum2e65f892007-02-28 22:03:49 +00001046 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001047 step = 0
1048
Guido van Rossum2e65f892007-02-28 22:03:49 +00001049 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001050 varargs = None
1051 if co.co_flags & CO_VARARGS:
1052 varargs = co.co_varnames[nargs]
1053 nargs = nargs + 1
1054 varkw = None
1055 if co.co_flags & CO_VARKEYWORDS:
1056 varkw = co.co_varnames[nargs]
Guido van Rossum2e65f892007-02-28 22:03:49 +00001057 return args, varargs, kwonlyargs, varkw
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001058
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001059
1060ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
1061
1062def getargspec(func):
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001063 """Get the names and default values of a function's parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001064
1065 A tuple of four things is returned: (args, varargs, keywords, defaults).
1066 'args' is a list of the argument names, including keyword-only argument names.
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001067 'varargs' and 'keywords' are the names of the * and ** parameters or None.
1068 'defaults' is an n-tuple of the default values of the last n parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001069
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001070 This function is deprecated, as it does not support annotations or
1071 keyword-only parameters and will raise ValueError if either is present
1072 on the supplied callable.
1073
1074 For a more structured introspection API, use inspect.signature() instead.
1075
1076 Alternatively, use getfullargspec() for an API with a similar namedtuple
1077 based interface, but full support for annotations and keyword-only
1078 parameters.
Matthias Bussonnierded87d82018-10-19 16:40:45 -07001079
1080 Deprecated since Python 3.5, use `inspect.getfullargspec()`.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001081 """
Matthias Bussonnierded87d82018-10-19 16:40:45 -07001082 warnings.warn("inspect.getargspec() is deprecated since Python 3.0, "
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001083 "use inspect.signature() or inspect.getfullargspec()",
1084 DeprecationWarning, stacklevel=2)
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001085 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
1086 getfullargspec(func)
1087 if kwonlyargs or ann:
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001088 raise ValueError("Function has keyword-only parameters or annotations"
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001089 ", use getfullargspec() API which can support them")
1090 return ArgSpec(args, varargs, varkw, defaults)
1091
Christian Heimes25bb7832008-01-11 16:17:00 +00001092FullArgSpec = namedtuple('FullArgSpec',
Benjamin Peterson3d4ca742008-11-12 21:39:01 +00001093 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001094
1095def getfullargspec(func):
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001096 """Get the names and default values of a callable object's parameters.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001097
Brett Cannon504d8852007-09-07 02:12:14 +00001098 A tuple of seven things is returned:
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001099 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations).
1100 'args' is a list of the parameter names.
1101 'varargs' and 'varkw' are the names of the * and ** parameters or None.
1102 'defaults' is an n-tuple of the default values of the last n parameters.
1103 'kwonlyargs' is a list of keyword-only parameter names.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001104 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001105 'annotations' is a dictionary mapping parameter names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001106
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001107 Notable differences from inspect.signature():
1108 - the "self" parameter is always reported, even for bound methods
1109 - wrapper chains defined by __wrapped__ *not* unwrapped automatically
Jeremy Hylton64967882003-06-27 18:14:39 +00001110 """
1111
Yury Selivanov57d240e2014-02-19 16:27:23 -05001112 try:
1113 # Re: `skip_bound_arg=False`
1114 #
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001115 # There is a notable difference in behaviour between getfullargspec
1116 # and Signature: the former always returns 'self' parameter for bound
1117 # methods, whereas the Signature always shows the actual calling
1118 # signature of the passed object.
1119 #
1120 # To simulate this behaviour, we "unbind" bound methods, to trick
1121 # inspect.signature to always return their first parameter ("self",
1122 # usually)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001123
Yury Selivanov57d240e2014-02-19 16:27:23 -05001124 # Re: `follow_wrapper_chains=False`
1125 #
1126 # getfullargspec() historically ignored __wrapped__ attributes,
1127 # so we ensure that remains the case in 3.3+
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001128
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001129 sig = _signature_from_callable(func,
1130 follow_wrapper_chains=False,
1131 skip_bound_arg=False,
1132 sigcls=Signature)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001133 except Exception as ex:
1134 # Most of the times 'signature' will raise ValueError.
1135 # But, it can also raise AttributeError, and, maybe something
1136 # else. So to be fully backwards compatible, we catch all
1137 # possible exceptions here, and reraise a TypeError.
1138 raise TypeError('unsupported callable') from ex
1139
1140 args = []
1141 varargs = None
1142 varkw = None
1143 kwonlyargs = []
1144 defaults = ()
1145 annotations = {}
1146 defaults = ()
1147 kwdefaults = {}
1148
1149 if sig.return_annotation is not sig.empty:
1150 annotations['return'] = sig.return_annotation
1151
1152 for param in sig.parameters.values():
1153 kind = param.kind
1154 name = param.name
1155
1156 if kind is _POSITIONAL_ONLY:
1157 args.append(name)
1158 elif kind is _POSITIONAL_OR_KEYWORD:
1159 args.append(name)
1160 if param.default is not param.empty:
1161 defaults += (param.default,)
1162 elif kind is _VAR_POSITIONAL:
1163 varargs = name
1164 elif kind is _KEYWORD_ONLY:
1165 kwonlyargs.append(name)
1166 if param.default is not param.empty:
1167 kwdefaults[name] = param.default
1168 elif kind is _VAR_KEYWORD:
1169 varkw = name
1170
1171 if param.annotation is not param.empty:
1172 annotations[name] = param.annotation
1173
1174 if not kwdefaults:
1175 # compatibility with 'func.__kwdefaults__'
1176 kwdefaults = None
1177
1178 if not defaults:
1179 # compatibility with 'func.__defaults__'
1180 defaults = None
1181
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001182 return FullArgSpec(args, varargs, varkw, defaults,
1183 kwonlyargs, kwdefaults, annotations)
1184
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001185
Christian Heimes25bb7832008-01-11 16:17:00 +00001186ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
1187
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001188def getargvalues(frame):
1189 """Get information about arguments passed into a particular frame.
1190
1191 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001192 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001193 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1194 'locals' is the locals dictionary of the given frame."""
1195 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +00001196 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001197
Guido van Rossum2e65f892007-02-28 22:03:49 +00001198def formatannotation(annotation, base_module=None):
Guido van Rossum52e50042016-10-22 07:55:18 -07001199 if getattr(annotation, '__module__', None) == 'typing':
1200 return repr(annotation).replace('typing.', '')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001201 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001202 if annotation.__module__ in ('builtins', base_module):
Serhiy Storchaka521e5862014-07-22 15:00:37 +03001203 return annotation.__qualname__
1204 return annotation.__module__+'.'+annotation.__qualname__
Guido van Rossum2e65f892007-02-28 22:03:49 +00001205 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001206
Guido van Rossum2e65f892007-02-28 22:03:49 +00001207def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001208 module = getattr(object, '__module__', None)
1209 def _formatannotation(annotation):
1210 return formatannotation(annotation, module)
1211 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001212
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001213def formatargspec(args, varargs=None, varkw=None, defaults=None,
Guido van Rossum2e65f892007-02-28 22:03:49 +00001214 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001215 formatarg=str,
1216 formatvarargs=lambda name: '*' + name,
1217 formatvarkw=lambda name: '**' + name,
1218 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001219 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001220 formatannotation=formatannotation):
Berker Peksagfa3922c2015-07-31 04:11:29 +03001221 """Format an argument spec from the values returned by getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001222
Guido van Rossum2e65f892007-02-28 22:03:49 +00001223 The first seven arguments are (args, varargs, varkw, defaults,
1224 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1225 are the corresponding optional formatting functions that are called to
1226 turn names and values into strings. The last argument is an optional
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +02001227 function to format the sequence of arguments.
1228
1229 Deprecated since Python 3.5: use the `signature` function and `Signature`
1230 objects.
1231 """
1232
1233 from warnings import warn
1234
1235 warn("`formatargspec` is deprecated since Python 3.5. Use `signature` and "
Zackery Spytz41254eb2018-06-11 21:16:18 -06001236 "the `Signature` object directly",
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +02001237 DeprecationWarning,
1238 stacklevel=2)
1239
Guido van Rossum2e65f892007-02-28 22:03:49 +00001240 def formatargandannotation(arg):
1241 result = formatarg(arg)
1242 if arg in annotations:
1243 result += ': ' + formatannotation(annotations[arg])
1244 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001245 specs = []
1246 if defaults:
1247 firstdefault = len(args) - len(defaults)
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001248 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001249 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001250 if defaults and i >= firstdefault:
1251 spec = spec + formatvalue(defaults[i - firstdefault])
1252 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001253 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001254 specs.append(formatvarargs(formatargandannotation(varargs)))
1255 else:
1256 if kwonlyargs:
1257 specs.append('*')
1258 if kwonlyargs:
1259 for kwonlyarg in kwonlyargs:
1260 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001261 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001262 spec += formatvalue(kwonlydefaults[kwonlyarg])
1263 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001264 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001265 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001266 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001267 if 'return' in annotations:
1268 result += formatreturns(formatannotation(annotations['return']))
1269 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001270
1271def formatargvalues(args, varargs, varkw, locals,
1272 formatarg=str,
1273 formatvarargs=lambda name: '*' + name,
1274 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001275 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001276 """Format an argument spec from the 4 values returned by getargvalues.
1277
1278 The first four arguments are (args, varargs, varkw, locals). The
1279 next four arguments are the corresponding optional formatting functions
1280 that are called to turn names and values into strings. The ninth
1281 argument is an optional function to format the sequence of arguments."""
1282 def convert(name, locals=locals,
1283 formatarg=formatarg, formatvalue=formatvalue):
1284 return formatarg(name) + formatvalue(locals[name])
1285 specs = []
1286 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001287 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001288 if varargs:
1289 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1290 if varkw:
1291 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001292 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001293
Benjamin Petersone109c702011-06-24 09:37:26 -05001294def _missing_arguments(f_name, argnames, pos, values):
1295 names = [repr(name) for name in argnames if name not in values]
1296 missing = len(names)
1297 if missing == 1:
1298 s = names[0]
1299 elif missing == 2:
1300 s = "{} and {}".format(*names)
1301 else:
Yury Selivanovdccfa132014-03-27 18:42:52 -04001302 tail = ", {} and {}".format(*names[-2:])
Benjamin Petersone109c702011-06-24 09:37:26 -05001303 del names[-2:]
1304 s = ", ".join(names) + tail
1305 raise TypeError("%s() missing %i required %s argument%s: %s" %
1306 (f_name, missing,
1307 "positional" if pos else "keyword-only",
1308 "" if missing == 1 else "s", s))
1309
1310def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001311 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001312 kwonly_given = len([arg for arg in kwonly if arg in values])
1313 if varargs:
1314 plural = atleast != 1
1315 sig = "at least %d" % (atleast,)
1316 elif defcount:
1317 plural = True
1318 sig = "from %d to %d" % (atleast, len(args))
1319 else:
1320 plural = len(args) != 1
1321 sig = str(len(args))
1322 kwonly_sig = ""
1323 if kwonly_given:
1324 msg = " positional argument%s (and %d keyword-only argument%s)"
1325 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1326 "s" if kwonly_given != 1 else ""))
1327 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1328 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1329 "was" if given == 1 and not kwonly_given else "were"))
1330
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001331def getcallargs(*func_and_positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001332 """Get the mapping of arguments to values.
1333
1334 A dict is returned, with keys the function argument names (including the
1335 names of the * and ** arguments, if any), and values the respective bound
1336 values from 'positional' and 'named'."""
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001337 func = func_and_positional[0]
1338 positional = func_and_positional[1:]
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001339 spec = getfullargspec(func)
1340 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
1341 f_name = func.__name__
1342 arg2value = {}
1343
Benjamin Petersonb204a422011-06-05 22:04:07 -05001344
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001345 if ismethod(func) and func.__self__ is not None:
1346 # implicit 'self' (or 'cls' for classmethods) argument
1347 positional = (func.__self__,) + positional
1348 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001349 num_args = len(args)
1350 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001351
Benjamin Petersonb204a422011-06-05 22:04:07 -05001352 n = min(num_pos, num_args)
1353 for i in range(n):
1354 arg2value[args[i]] = positional[i]
1355 if varargs:
1356 arg2value[varargs] = tuple(positional[n:])
1357 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001358 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001359 arg2value[varkw] = {}
1360 for kw, value in named.items():
1361 if kw not in possible_kwargs:
1362 if not varkw:
1363 raise TypeError("%s() got an unexpected keyword argument %r" %
1364 (f_name, kw))
1365 arg2value[varkw][kw] = value
1366 continue
1367 if kw in arg2value:
1368 raise TypeError("%s() got multiple values for argument %r" %
1369 (f_name, kw))
1370 arg2value[kw] = value
1371 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001372 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1373 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001374 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001375 req = args[:num_args - num_defaults]
1376 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001377 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001378 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001379 for i, arg in enumerate(args[num_args - num_defaults:]):
1380 if arg not in arg2value:
1381 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001382 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001383 for kwarg in kwonlyargs:
1384 if kwarg not in arg2value:
Yury Selivanov875df202014-03-27 18:23:03 -04001385 if kwonlydefaults and kwarg in kwonlydefaults:
Benjamin Petersone109c702011-06-24 09:37:26 -05001386 arg2value[kwarg] = kwonlydefaults[kwarg]
1387 else:
1388 missing += 1
1389 if missing:
1390 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001391 return arg2value
1392
Nick Coghlan2f92e542012-06-23 19:39:55 +10001393ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1394
1395def getclosurevars(func):
1396 """
1397 Get the mapping of free variables to their current values.
1398
Meador Inge8fda3592012-07-19 21:33:21 -05001399 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001400 and builtin references as seen by the body of the function. A final
1401 set of unbound names that could not be resolved is also provided.
1402 """
1403
1404 if ismethod(func):
1405 func = func.__func__
1406
1407 if not isfunction(func):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001408 raise TypeError("{!r} is not a Python function".format(func))
Nick Coghlan2f92e542012-06-23 19:39:55 +10001409
1410 code = func.__code__
1411 # Nonlocal references are named in co_freevars and resolved
1412 # by looking them up in __closure__ by positional index
1413 if func.__closure__ is None:
1414 nonlocal_vars = {}
1415 else:
1416 nonlocal_vars = {
1417 var : cell.cell_contents
1418 for var, cell in zip(code.co_freevars, func.__closure__)
1419 }
1420
1421 # Global and builtin references are named in co_names and resolved
1422 # by looking them up in __globals__ or __builtins__
1423 global_ns = func.__globals__
1424 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1425 if ismodule(builtin_ns):
1426 builtin_ns = builtin_ns.__dict__
1427 global_vars = {}
1428 builtin_vars = {}
1429 unbound_names = set()
1430 for name in code.co_names:
1431 if name in ("None", "True", "False"):
1432 # Because these used to be builtins instead of keywords, they
1433 # may still show up as name references. We ignore them.
1434 continue
1435 try:
1436 global_vars[name] = global_ns[name]
1437 except KeyError:
1438 try:
1439 builtin_vars[name] = builtin_ns[name]
1440 except KeyError:
1441 unbound_names.add(name)
1442
1443 return ClosureVars(nonlocal_vars, global_vars,
1444 builtin_vars, unbound_names)
1445
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001446# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001447
1448Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1449
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001450def getframeinfo(frame, context=1):
1451 """Get information about a frame or traceback object.
1452
1453 A tuple of five things is returned: the filename, the line number of
1454 the current line, the function name, a list of lines of context from
1455 the source code, and the index of the current line within that list.
1456 The optional second argument specifies the number of lines of context
1457 to return, which are centered around the current line."""
1458 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001459 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001460 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001461 else:
1462 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001463 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001464 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001465
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001466 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001467 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001468 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001469 try:
1470 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001471 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001472 lines = index = None
1473 else:
Raymond Hettingera0501712004-06-15 11:22:53 +00001474 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001475 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001476 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001477 else:
1478 lines = index = None
1479
Christian Heimes25bb7832008-01-11 16:17:00 +00001480 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001481
1482def getlineno(frame):
1483 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001484 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1485 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001486
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001487FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
1488
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001489def getouterframes(frame, context=1):
1490 """Get a list of records for a frame and all higher (calling) frames.
1491
1492 Each record contains a frame object, filename, line number, function
1493 name, a list of lines of context, and index within the context."""
1494 framelist = []
1495 while frame:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001496 frameinfo = (frame,) + getframeinfo(frame, context)
1497 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001498 frame = frame.f_back
1499 return framelist
1500
1501def getinnerframes(tb, context=1):
1502 """Get a list of records for a traceback's frame and all lower frames.
1503
1504 Each record contains a frame object, filename, line number, function
1505 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001506 framelist = []
1507 while tb:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001508 frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
1509 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001510 tb = tb.tb_next
1511 return framelist
1512
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001513def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001514 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001515 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001516
1517def stack(context=1):
1518 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001519 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001520
1521def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001522 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001523 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001524
1525
1526# ------------------------------------------------ static version of getattr
1527
1528_sentinel = object()
1529
Michael Foorde5162652010-11-20 16:40:44 +00001530def _static_getmro(klass):
1531 return type.__dict__['__mro__'].__get__(klass)
1532
Michael Foord95fc51d2010-11-20 15:07:30 +00001533def _check_instance(obj, attr):
1534 instance_dict = {}
1535 try:
1536 instance_dict = object.__getattribute__(obj, "__dict__")
1537 except AttributeError:
1538 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001539 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001540
1541
1542def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001543 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001544 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001545 try:
1546 return entry.__dict__[attr]
1547 except KeyError:
1548 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001549 return _sentinel
1550
Michael Foord35184ed2010-11-20 16:58:30 +00001551def _is_type(obj):
1552 try:
1553 _static_getmro(obj)
1554 except TypeError:
1555 return False
1556 return True
1557
Michael Foorddcebe0f2011-03-15 19:20:44 -04001558def _shadowed_dict(klass):
1559 dict_attr = type.__dict__["__dict__"]
1560 for entry in _static_getmro(klass):
1561 try:
1562 class_dict = dict_attr.__get__(entry)["__dict__"]
1563 except KeyError:
1564 pass
1565 else:
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05301566 if not (isinstance(class_dict, types.GetSetDescriptorType) and
Michael Foorddcebe0f2011-03-15 19:20:44 -04001567 class_dict.__name__ == "__dict__" and
1568 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001569 return class_dict
1570 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001571
1572def getattr_static(obj, attr, default=_sentinel):
1573 """Retrieve attributes without triggering dynamic lookup via the
1574 descriptor protocol, __getattr__ or __getattribute__.
1575
1576 Note: this function may not be able to retrieve all attributes
1577 that getattr can fetch (like dynamically created attributes)
1578 and may find attributes that getattr can't (like descriptors
1579 that raise AttributeError). It can also return descriptor objects
1580 instead of instance members in some cases. See the
1581 documentation for details.
1582 """
1583 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001584 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001585 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001586 dict_attr = _shadowed_dict(klass)
1587 if (dict_attr is _sentinel or
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05301588 isinstance(dict_attr, types.MemberDescriptorType)):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001589 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001590 else:
1591 klass = obj
1592
1593 klass_result = _check_class(klass, attr)
1594
1595 if instance_result is not _sentinel and klass_result is not _sentinel:
1596 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1597 _check_class(type(klass_result), '__set__') is not _sentinel):
1598 return klass_result
1599
1600 if instance_result is not _sentinel:
1601 return instance_result
1602 if klass_result is not _sentinel:
1603 return klass_result
1604
1605 if obj is klass:
1606 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001607 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001608 if _shadowed_dict(type(entry)) is _sentinel:
1609 try:
1610 return entry.__dict__[attr]
1611 except KeyError:
1612 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001613 if default is not _sentinel:
1614 return default
1615 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001616
1617
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001618# ------------------------------------------------ generator introspection
1619
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001620GEN_CREATED = 'GEN_CREATED'
1621GEN_RUNNING = 'GEN_RUNNING'
1622GEN_SUSPENDED = 'GEN_SUSPENDED'
1623GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001624
1625def getgeneratorstate(generator):
1626 """Get current state of a generator-iterator.
1627
1628 Possible states are:
1629 GEN_CREATED: Waiting to start execution.
1630 GEN_RUNNING: Currently being executed by the interpreter.
1631 GEN_SUSPENDED: Currently suspended at a yield expression.
1632 GEN_CLOSED: Execution has completed.
1633 """
1634 if generator.gi_running:
1635 return GEN_RUNNING
1636 if generator.gi_frame is None:
1637 return GEN_CLOSED
1638 if generator.gi_frame.f_lasti == -1:
1639 return GEN_CREATED
1640 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001641
1642
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001643def getgeneratorlocals(generator):
1644 """
1645 Get the mapping of generator local variables to their current values.
1646
1647 A dict is returned, with the keys the local variable names and values the
1648 bound values."""
1649
1650 if not isgenerator(generator):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001651 raise TypeError("{!r} is not a Python generator".format(generator))
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001652
1653 frame = getattr(generator, "gi_frame", None)
1654 if frame is not None:
1655 return generator.gi_frame.f_locals
1656 else:
1657 return {}
1658
Yury Selivanov5376ba92015-06-22 12:19:30 -04001659
1660# ------------------------------------------------ coroutine introspection
1661
1662CORO_CREATED = 'CORO_CREATED'
1663CORO_RUNNING = 'CORO_RUNNING'
1664CORO_SUSPENDED = 'CORO_SUSPENDED'
1665CORO_CLOSED = 'CORO_CLOSED'
1666
1667def getcoroutinestate(coroutine):
1668 """Get current state of a coroutine object.
1669
1670 Possible states are:
1671 CORO_CREATED: Waiting to start execution.
1672 CORO_RUNNING: Currently being executed by the interpreter.
1673 CORO_SUSPENDED: Currently suspended at an await expression.
1674 CORO_CLOSED: Execution has completed.
1675 """
1676 if coroutine.cr_running:
1677 return CORO_RUNNING
1678 if coroutine.cr_frame is None:
1679 return CORO_CLOSED
1680 if coroutine.cr_frame.f_lasti == -1:
1681 return CORO_CREATED
1682 return CORO_SUSPENDED
1683
1684
1685def getcoroutinelocals(coroutine):
1686 """
1687 Get the mapping of coroutine local variables to their current values.
1688
1689 A dict is returned, with the keys the local variable names and values the
1690 bound values."""
1691 frame = getattr(coroutine, "cr_frame", None)
1692 if frame is not None:
1693 return frame.f_locals
1694 else:
1695 return {}
1696
1697
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001698###############################################################################
1699### Function Signature Object (PEP 362)
1700###############################################################################
1701
1702
1703_WrapperDescriptor = type(type.__call__)
1704_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001705_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001706
1707_NonUserDefinedCallables = (_WrapperDescriptor,
1708 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001709 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001710 types.BuiltinFunctionType)
1711
1712
Yury Selivanov421f0c72014-01-29 12:05:40 -05001713def _signature_get_user_defined_method(cls, method_name):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001714 """Private helper. Checks if ``cls`` has an attribute
1715 named ``method_name`` and returns it only if it is a
1716 pure python function.
1717 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001718 try:
1719 meth = getattr(cls, method_name)
1720 except AttributeError:
1721 return
1722 else:
1723 if not isinstance(meth, _NonUserDefinedCallables):
1724 # Once '__signature__' will be added to 'C'-level
1725 # callables, this check won't be necessary
1726 return meth
1727
1728
Yury Selivanov62560fb2014-01-28 12:26:24 -05001729def _signature_get_partial(wrapped_sig, partial, extra_args=()):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001730 """Private helper to calculate how 'wrapped_sig' signature will
1731 look like after applying a 'functools.partial' object (or alike)
1732 on it.
1733 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001734
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001735 old_params = wrapped_sig.parameters
1736 new_params = OrderedDict(old_params.items())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001737
1738 partial_args = partial.args or ()
1739 partial_keywords = partial.keywords or {}
1740
1741 if extra_args:
1742 partial_args = extra_args + partial_args
1743
1744 try:
1745 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1746 except TypeError as ex:
1747 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1748 raise ValueError(msg) from ex
1749
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001750
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001751 transform_to_kwonly = False
1752 for param_name, param in old_params.items():
1753 try:
1754 arg_value = ba.arguments[param_name]
1755 except KeyError:
1756 pass
1757 else:
1758 if param.kind is _POSITIONAL_ONLY:
1759 # If positional-only parameter is bound by partial,
1760 # it effectively disappears from the signature
1761 new_params.pop(param_name)
1762 continue
1763
1764 if param.kind is _POSITIONAL_OR_KEYWORD:
1765 if param_name in partial_keywords:
1766 # This means that this parameter, and all parameters
1767 # after it should be keyword-only (and var-positional
1768 # should be removed). Here's why. Consider the following
1769 # function:
1770 # foo(a, b, *args, c):
1771 # pass
1772 #
1773 # "partial(foo, a='spam')" will have the following
1774 # signature: "(*, a='spam', b, c)". Because attempting
1775 # to call that partial with "(10, 20)" arguments will
1776 # raise a TypeError, saying that "a" argument received
1777 # multiple values.
1778 transform_to_kwonly = True
1779 # Set the new default value
1780 new_params[param_name] = param.replace(default=arg_value)
1781 else:
1782 # was passed as a positional argument
1783 new_params.pop(param.name)
1784 continue
1785
1786 if param.kind is _KEYWORD_ONLY:
1787 # Set the new default value
1788 new_params[param_name] = param.replace(default=arg_value)
1789
1790 if transform_to_kwonly:
1791 assert param.kind is not _POSITIONAL_ONLY
1792
1793 if param.kind is _POSITIONAL_OR_KEYWORD:
1794 new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1795 new_params[param_name] = new_param
1796 new_params.move_to_end(param_name)
1797 elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
1798 new_params.move_to_end(param_name)
1799 elif param.kind is _VAR_POSITIONAL:
1800 new_params.pop(param.name)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001801
1802 return wrapped_sig.replace(parameters=new_params.values())
1803
1804
Yury Selivanov62560fb2014-01-28 12:26:24 -05001805def _signature_bound_method(sig):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001806 """Private helper to transform signatures for unbound
1807 functions to bound methods.
1808 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001809
1810 params = tuple(sig.parameters.values())
1811
1812 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1813 raise ValueError('invalid method signature')
1814
1815 kind = params[0].kind
1816 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1817 # Drop first parameter:
1818 # '(p1, p2[, ...])' -> '(p2[, ...])'
1819 params = params[1:]
1820 else:
1821 if kind is not _VAR_POSITIONAL:
1822 # Unless we add a new parameter type we never
1823 # get here
1824 raise ValueError('invalid argument type')
1825 # It's a var-positional parameter.
1826 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1827
1828 return sig.replace(parameters=params)
1829
1830
Yury Selivanovb77511d2014-01-29 10:46:14 -05001831def _signature_is_builtin(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001832 """Private helper to test if `obj` is a callable that might
1833 support Argument Clinic's __text_signature__ protocol.
1834 """
Yury Selivanov1d241832014-02-02 12:51:20 -05001835 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001836 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001837 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001838 # Can't test 'isinstance(type)' here, as it would
1839 # also be True for regular python classes
1840 obj in (type, object))
1841
1842
Yury Selivanov63da7c72014-01-31 14:48:37 -05001843def _signature_is_functionlike(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001844 """Private helper to test if `obj` is a duck type of FunctionType.
1845 A good example of such objects are functions compiled with
1846 Cython, which have all attributes that a pure Python function
1847 would have, but have their code statically compiled.
1848 """
Yury Selivanov63da7c72014-01-31 14:48:37 -05001849
1850 if not callable(obj) or isclass(obj):
1851 # All function-like objects are obviously callables,
1852 # and not classes.
1853 return False
1854
1855 name = getattr(obj, '__name__', None)
1856 code = getattr(obj, '__code__', None)
1857 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
1858 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
1859 annotations = getattr(obj, '__annotations__', None)
1860
1861 return (isinstance(code, types.CodeType) and
1862 isinstance(name, str) and
1863 (defaults is None or isinstance(defaults, tuple)) and
1864 (kwdefaults is None or isinstance(kwdefaults, dict)) and
1865 isinstance(annotations, dict))
1866
1867
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001868def _signature_get_bound_param(spec):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001869 """ Private helper to get first parameter name from a
1870 __text_signature__ of a builtin method, which should
1871 be in the following format: '($param1, ...)'.
1872 Assumptions are that the first argument won't have
1873 a default value or an annotation.
1874 """
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001875
1876 assert spec.startswith('($')
1877
1878 pos = spec.find(',')
1879 if pos == -1:
1880 pos = spec.find(')')
1881
1882 cpos = spec.find(':')
1883 assert cpos == -1 or cpos > pos
1884
1885 cpos = spec.find('=')
1886 assert cpos == -1 or cpos > pos
1887
1888 return spec[2:pos]
1889
1890
Larry Hastings2623c8c2014-02-08 22:15:29 -08001891def _signature_strip_non_python_syntax(signature):
1892 """
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001893 Private helper function. Takes a signature in Argument Clinic's
1894 extended signature format.
1895
Larry Hastings2623c8c2014-02-08 22:15:29 -08001896 Returns a tuple of three things:
1897 * that signature re-rendered in standard Python syntax,
1898 * the index of the "self" parameter (generally 0), or None if
1899 the function does not have a "self" parameter, and
1900 * the index of the last "positional only" parameter,
1901 or None if the signature has no positional-only parameters.
1902 """
1903
1904 if not signature:
1905 return signature, None, None
1906
1907 self_parameter = None
1908 last_positional_only = None
1909
1910 lines = [l.encode('ascii') for l in signature.split('\n')]
1911 generator = iter(lines).__next__
1912 token_stream = tokenize.tokenize(generator)
1913
1914 delayed_comma = False
1915 skip_next_comma = False
1916 text = []
1917 add = text.append
1918
1919 current_parameter = 0
1920 OP = token.OP
1921 ERRORTOKEN = token.ERRORTOKEN
1922
1923 # token stream always starts with ENCODING token, skip it
1924 t = next(token_stream)
1925 assert t.type == tokenize.ENCODING
1926
1927 for t in token_stream:
1928 type, string = t.type, t.string
1929
1930 if type == OP:
1931 if string == ',':
1932 if skip_next_comma:
1933 skip_next_comma = False
1934 else:
1935 assert not delayed_comma
1936 delayed_comma = True
1937 current_parameter += 1
1938 continue
1939
1940 if string == '/':
1941 assert not skip_next_comma
1942 assert last_positional_only is None
1943 skip_next_comma = True
1944 last_positional_only = current_parameter - 1
1945 continue
1946
1947 if (type == ERRORTOKEN) and (string == '$'):
1948 assert self_parameter is None
1949 self_parameter = current_parameter
1950 continue
1951
1952 if delayed_comma:
1953 delayed_comma = False
1954 if not ((type == OP) and (string == ')')):
1955 add(', ')
1956 add(string)
1957 if (string == ','):
1958 add(' ')
1959 clean_signature = ''.join(text)
1960 return clean_signature, self_parameter, last_positional_only
1961
1962
Yury Selivanov57d240e2014-02-19 16:27:23 -05001963def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001964 """Private helper to parse content of '__text_signature__'
1965 and return a Signature based on it.
1966 """
INADA Naoki37420de2018-01-27 10:10:06 +09001967 # Lazy import ast because it's relatively heavy and
1968 # it's not used for other than this function.
1969 import ast
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001970
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001971 Parameter = cls._parameter_cls
1972
Larry Hastings2623c8c2014-02-08 22:15:29 -08001973 clean_signature, self_parameter, last_positional_only = \
1974 _signature_strip_non_python_syntax(s)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001975
Larry Hastings2623c8c2014-02-08 22:15:29 -08001976 program = "def foo" + clean_signature + ": pass"
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001977
1978 try:
Larry Hastings2623c8c2014-02-08 22:15:29 -08001979 module = ast.parse(program)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001980 except SyntaxError:
1981 module = None
1982
1983 if not isinstance(module, ast.Module):
1984 raise ValueError("{!r} builtin has invalid signature".format(obj))
1985
1986 f = module.body[0]
1987
1988 parameters = []
1989 empty = Parameter.empty
1990 invalid = object()
1991
1992 module = None
1993 module_dict = {}
1994 module_name = getattr(obj, '__module__', None)
1995 if module_name:
1996 module = sys.modules.get(module_name, None)
1997 if module:
1998 module_dict = module.__dict__
INADA Naoki6f85b822018-10-05 01:47:09 +09001999 sys_module_dict = sys.modules.copy()
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002000
2001 def parse_name(node):
2002 assert isinstance(node, ast.arg)
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05302003 if node.annotation is not None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002004 raise ValueError("Annotations are not currently supported")
2005 return node.arg
2006
2007 def wrap_value(s):
2008 try:
2009 value = eval(s, module_dict)
2010 except NameError:
2011 try:
2012 value = eval(s, sys_module_dict)
2013 except NameError:
2014 raise RuntimeError()
2015
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002016 if isinstance(value, (str, int, float, bytes, bool, type(None))):
2017 return ast.Constant(value)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002018 raise RuntimeError()
2019
2020 class RewriteSymbolics(ast.NodeTransformer):
2021 def visit_Attribute(self, node):
2022 a = []
2023 n = node
2024 while isinstance(n, ast.Attribute):
2025 a.append(n.attr)
2026 n = n.value
2027 if not isinstance(n, ast.Name):
2028 raise RuntimeError()
2029 a.append(n.id)
2030 value = ".".join(reversed(a))
2031 return wrap_value(value)
2032
2033 def visit_Name(self, node):
2034 if not isinstance(node.ctx, ast.Load):
2035 raise ValueError()
2036 return wrap_value(node.id)
2037
2038 def p(name_node, default_node, default=empty):
2039 name = parse_name(name_node)
2040 if name is invalid:
2041 return None
2042 if default_node and default_node is not _empty:
2043 try:
2044 default_node = RewriteSymbolics().visit(default_node)
2045 o = ast.literal_eval(default_node)
2046 except ValueError:
2047 o = invalid
2048 if o is invalid:
2049 return None
2050 default = o if o is not invalid else default
2051 parameters.append(Parameter(name, kind, default=default, annotation=empty))
2052
2053 # non-keyword-only parameters
2054 args = reversed(f.args.args)
2055 defaults = reversed(f.args.defaults)
2056 iter = itertools.zip_longest(args, defaults, fillvalue=None)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002057 if last_positional_only is not None:
2058 kind = Parameter.POSITIONAL_ONLY
2059 else:
2060 kind = Parameter.POSITIONAL_OR_KEYWORD
2061 for i, (name, default) in enumerate(reversed(list(iter))):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002062 p(name, default)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002063 if i == last_positional_only:
2064 kind = Parameter.POSITIONAL_OR_KEYWORD
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002065
2066 # *args
2067 if f.args.vararg:
2068 kind = Parameter.VAR_POSITIONAL
2069 p(f.args.vararg, empty)
2070
2071 # keyword-only arguments
2072 kind = Parameter.KEYWORD_ONLY
2073 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
2074 p(name, default)
2075
2076 # **kwargs
2077 if f.args.kwarg:
2078 kind = Parameter.VAR_KEYWORD
2079 p(f.args.kwarg, empty)
2080
Larry Hastings2623c8c2014-02-08 22:15:29 -08002081 if self_parameter is not None:
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002082 # Possibly strip the bound argument:
2083 # - We *always* strip first bound argument if
2084 # it is a module.
2085 # - We don't strip first bound argument if
2086 # skip_bound_arg is False.
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002087 assert parameters
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002088 _self = getattr(obj, '__self__', None)
2089 self_isbound = _self is not None
2090 self_ismodule = ismodule(_self)
2091 if self_isbound and (self_ismodule or skip_bound_arg):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002092 parameters.pop(0)
2093 else:
2094 # for builtins, self parameter is always positional-only!
2095 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
2096 parameters[0] = p
2097
2098 return cls(parameters, return_annotation=cls.empty)
2099
2100
Yury Selivanov57d240e2014-02-19 16:27:23 -05002101def _signature_from_builtin(cls, func, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002102 """Private helper function to get signature for
2103 builtin callables.
2104 """
2105
Yury Selivanov57d240e2014-02-19 16:27:23 -05002106 if not _signature_is_builtin(func):
2107 raise TypeError("{!r} is not a Python builtin "
2108 "function".format(func))
2109
2110 s = getattr(func, "__text_signature__", None)
2111 if not s:
2112 raise ValueError("no signature found for builtin {!r}".format(func))
2113
2114 return _signature_fromstr(cls, func, s, skip_bound_arg)
2115
2116
Yury Selivanovcf45f022015-05-20 14:38:50 -04002117def _signature_from_function(cls, func):
2118 """Private helper: constructs Signature for the given python function."""
2119
2120 is_duck_function = False
2121 if not isfunction(func):
2122 if _signature_is_functionlike(func):
2123 is_duck_function = True
2124 else:
2125 # If it's not a pure Python function, and not a duck type
2126 # of pure function:
2127 raise TypeError('{!r} is not a Python function'.format(func))
2128
2129 Parameter = cls._parameter_cls
2130
2131 # Parameter information.
2132 func_code = func.__code__
2133 pos_count = func_code.co_argcount
2134 arg_names = func_code.co_varnames
2135 positional = tuple(arg_names[:pos_count])
2136 keyword_only_count = func_code.co_kwonlyargcount
2137 keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)]
2138 annotations = func.__annotations__
2139 defaults = func.__defaults__
2140 kwdefaults = func.__kwdefaults__
2141
2142 if defaults:
2143 pos_default_count = len(defaults)
2144 else:
2145 pos_default_count = 0
2146
2147 parameters = []
2148
2149 # Non-keyword-only parameters w/o defaults.
2150 non_default_count = pos_count - pos_default_count
2151 for name in positional[:non_default_count]:
2152 annotation = annotations.get(name, _empty)
2153 parameters.append(Parameter(name, annotation=annotation,
2154 kind=_POSITIONAL_OR_KEYWORD))
2155
2156 # ... w/ defaults.
2157 for offset, name in enumerate(positional[non_default_count:]):
2158 annotation = annotations.get(name, _empty)
2159 parameters.append(Parameter(name, annotation=annotation,
2160 kind=_POSITIONAL_OR_KEYWORD,
2161 default=defaults[offset]))
2162
2163 # *args
2164 if func_code.co_flags & CO_VARARGS:
2165 name = arg_names[pos_count + keyword_only_count]
2166 annotation = annotations.get(name, _empty)
2167 parameters.append(Parameter(name, annotation=annotation,
2168 kind=_VAR_POSITIONAL))
2169
2170 # Keyword-only parameters.
2171 for name in keyword_only:
2172 default = _empty
2173 if kwdefaults is not None:
2174 default = kwdefaults.get(name, _empty)
2175
2176 annotation = annotations.get(name, _empty)
2177 parameters.append(Parameter(name, annotation=annotation,
2178 kind=_KEYWORD_ONLY,
2179 default=default))
2180 # **kwargs
2181 if func_code.co_flags & CO_VARKEYWORDS:
2182 index = pos_count + keyword_only_count
2183 if func_code.co_flags & CO_VARARGS:
2184 index += 1
2185
2186 name = arg_names[index]
2187 annotation = annotations.get(name, _empty)
2188 parameters.append(Parameter(name, annotation=annotation,
2189 kind=_VAR_KEYWORD))
2190
2191 # Is 'func' is a pure Python function - don't validate the
2192 # parameters list (for correct order and defaults), it should be OK.
2193 return cls(parameters,
2194 return_annotation=annotations.get('return', _empty),
2195 __validate_parameters__=is_duck_function)
2196
2197
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002198def _signature_from_callable(obj, *,
2199 follow_wrapper_chains=True,
2200 skip_bound_arg=True,
2201 sigcls):
2202
2203 """Private helper function to get signature for arbitrary
2204 callable objects.
2205 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002206
2207 if not callable(obj):
2208 raise TypeError('{!r} is not a callable object'.format(obj))
2209
2210 if isinstance(obj, types.MethodType):
2211 # In this case we skip the first parameter of the underlying
2212 # function (usually `self` or `cls`).
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002213 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002214 obj.__func__,
2215 follow_wrapper_chains=follow_wrapper_chains,
2216 skip_bound_arg=skip_bound_arg,
2217 sigcls=sigcls)
2218
Yury Selivanov57d240e2014-02-19 16:27:23 -05002219 if skip_bound_arg:
2220 return _signature_bound_method(sig)
2221 else:
2222 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002223
Nick Coghlane8c45d62013-07-28 20:00:01 +10002224 # Was this function wrapped by a decorator?
Yury Selivanov57d240e2014-02-19 16:27:23 -05002225 if follow_wrapper_chains:
2226 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
Yury Selivanov46c759d2015-05-27 21:56:53 -04002227 if isinstance(obj, types.MethodType):
2228 # If the unwrapped object is a *method*, we might want to
2229 # skip its first parameter (self).
2230 # See test_signature_wrapped_bound_method for details.
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002231 return _signature_from_callable(
Yury Selivanov46c759d2015-05-27 21:56:53 -04002232 obj,
2233 follow_wrapper_chains=follow_wrapper_chains,
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002234 skip_bound_arg=skip_bound_arg,
2235 sigcls=sigcls)
Nick Coghlane8c45d62013-07-28 20:00:01 +10002236
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002237 try:
2238 sig = obj.__signature__
2239 except AttributeError:
2240 pass
2241 else:
2242 if sig is not None:
Yury Selivanov42407ab2014-06-23 10:23:50 -07002243 if not isinstance(sig, Signature):
2244 raise TypeError(
2245 'unexpected object {!r} in __signature__ '
2246 'attribute'.format(sig))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002247 return sig
2248
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002249 try:
2250 partialmethod = obj._partialmethod
2251 except AttributeError:
2252 pass
2253 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05002254 if isinstance(partialmethod, functools.partialmethod):
2255 # Unbound partialmethod (see functools.partialmethod)
2256 # This means, that we need to calculate the signature
2257 # as if it's a regular partial object, but taking into
2258 # account that the first positional argument
2259 # (usually `self`, or `cls`) will not be passed
2260 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002261
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002262 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002263 partialmethod.func,
2264 follow_wrapper_chains=follow_wrapper_chains,
2265 skip_bound_arg=skip_bound_arg,
2266 sigcls=sigcls)
2267
Yury Selivanov0486f812014-01-29 12:18:59 -05002268 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanov0486f812014-01-29 12:18:59 -05002269 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
Dong-hee Na378d7062017-05-18 04:00:51 +09002270 if first_wrapped_param.kind is Parameter.VAR_POSITIONAL:
2271 # First argument of the wrapped callable is `*args`, as in
2272 # `partialmethod(lambda *args)`.
2273 return sig
2274 else:
2275 sig_params = tuple(sig.parameters.values())
Yury Selivanov8a387212018-03-06 12:59:45 -05002276 assert (not sig_params or
2277 first_wrapped_param is not sig_params[0])
Dong-hee Na378d7062017-05-18 04:00:51 +09002278 new_params = (first_wrapped_param,) + sig_params
2279 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002280
Yury Selivanov63da7c72014-01-31 14:48:37 -05002281 if isfunction(obj) or _signature_is_functionlike(obj):
2282 # If it's a pure Python function, or an object that is duck type
2283 # of a Python function (Cython functions, for instance), then:
Yury Selivanovcf45f022015-05-20 14:38:50 -04002284 return _signature_from_function(sigcls, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002285
Yury Selivanova773de02014-02-21 18:30:53 -05002286 if _signature_is_builtin(obj):
Yury Selivanovda396452014-03-27 12:09:24 -04002287 return _signature_from_builtin(sigcls, obj,
Yury Selivanova773de02014-02-21 18:30:53 -05002288 skip_bound_arg=skip_bound_arg)
2289
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002290 if isinstance(obj, functools.partial):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002291 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002292 obj.func,
2293 follow_wrapper_chains=follow_wrapper_chains,
2294 skip_bound_arg=skip_bound_arg,
2295 sigcls=sigcls)
Yury Selivanov62560fb2014-01-28 12:26:24 -05002296 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002297
2298 sig = None
2299 if isinstance(obj, type):
2300 # obj is a class or a metaclass
2301
2302 # First, let's see if it has an overloaded __call__ defined
2303 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05002304 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002305 if call is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002306 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002307 call,
2308 follow_wrapper_chains=follow_wrapper_chains,
2309 skip_bound_arg=skip_bound_arg,
2310 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002311 else:
2312 # Now we check if the 'obj' class has a '__new__' method
Yury Selivanov421f0c72014-01-29 12:05:40 -05002313 new = _signature_get_user_defined_method(obj, '__new__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002314 if new is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002315 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002316 new,
2317 follow_wrapper_chains=follow_wrapper_chains,
2318 skip_bound_arg=skip_bound_arg,
2319 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002320 else:
2321 # Finally, we should have at least __init__ implemented
Yury Selivanov421f0c72014-01-29 12:05:40 -05002322 init = _signature_get_user_defined_method(obj, '__init__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002323 if init is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002324 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002325 init,
2326 follow_wrapper_chains=follow_wrapper_chains,
2327 skip_bound_arg=skip_bound_arg,
2328 sigcls=sigcls)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002329
2330 if sig is None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002331 # At this point we know, that `obj` is a class, with no user-
2332 # defined '__init__', '__new__', or class-level '__call__'
2333
Larry Hastings2623c8c2014-02-08 22:15:29 -08002334 for base in obj.__mro__[:-1]:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002335 # Since '__text_signature__' is implemented as a
2336 # descriptor that extracts text signature from the
2337 # class docstring, if 'obj' is derived from a builtin
2338 # class, its own '__text_signature__' may be 'None'.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002339 # Therefore, we go through the MRO (except the last
2340 # class in there, which is 'object') to find the first
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002341 # class with non-empty text signature.
2342 try:
2343 text_sig = base.__text_signature__
2344 except AttributeError:
2345 pass
2346 else:
2347 if text_sig:
2348 # If 'obj' class has a __text_signature__ attribute:
2349 # return a signature based on it
Yury Selivanovda396452014-03-27 12:09:24 -04002350 return _signature_fromstr(sigcls, obj, text_sig)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002351
2352 # No '__text_signature__' was found for the 'obj' class.
2353 # Last option is to check if its '__init__' is
2354 # object.__init__ or type.__init__.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002355 if type not in obj.__mro__:
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002356 # We have a class (not metaclass), but no user-defined
2357 # __init__ or __new__ for it
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002358 if (obj.__init__ is object.__init__ and
2359 obj.__new__ is object.__new__):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002360 # Return a signature of 'object' builtin.
2361 return signature(object)
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002362 else:
2363 raise ValueError(
2364 'no signature found for builtin type {!r}'.format(obj))
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002365
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002366 elif not isinstance(obj, _NonUserDefinedCallables):
2367 # An object with __call__
2368 # We also check that the 'obj' is not an instance of
2369 # _WrapperDescriptor or _MethodWrapper to avoid
2370 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05002371 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002372 if call is not None:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002373 try:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002374 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002375 call,
2376 follow_wrapper_chains=follow_wrapper_chains,
2377 skip_bound_arg=skip_bound_arg,
2378 sigcls=sigcls)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002379 except ValueError as ex:
2380 msg = 'no signature found for {!r}'.format(obj)
2381 raise ValueError(msg) from ex
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002382
2383 if sig is not None:
2384 # For classes and objects we skip the first parameter of their
2385 # __call__, __new__, or __init__ methods
Yury Selivanov57d240e2014-02-19 16:27:23 -05002386 if skip_bound_arg:
2387 return _signature_bound_method(sig)
2388 else:
2389 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002390
2391 if isinstance(obj, types.BuiltinFunctionType):
2392 # Raise a nicer error message for builtins
2393 msg = 'no signature found for builtin function {!r}'.format(obj)
2394 raise ValueError(msg)
2395
2396 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2397
2398
2399class _void:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002400 """A private marker - used in Parameter & Signature."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002401
2402
2403class _empty:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002404 """Marker object for Signature.empty and Parameter.empty."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002405
2406
Yury Selivanov21e83a52014-03-27 11:23:13 -04002407class _ParameterKind(enum.IntEnum):
2408 POSITIONAL_ONLY = 0
2409 POSITIONAL_OR_KEYWORD = 1
2410 VAR_POSITIONAL = 2
2411 KEYWORD_ONLY = 3
2412 VAR_KEYWORD = 4
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002413
2414 def __str__(self):
Yury Selivanov21e83a52014-03-27 11:23:13 -04002415 return self._name_
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002416
Dong-hee Na4aa30062018-06-08 12:46:31 +09002417 @property
2418 def description(self):
2419 return _PARAM_NAME_MAPPING[self]
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002420
Yury Selivanov21e83a52014-03-27 11:23:13 -04002421_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
2422_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
2423_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
2424_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
2425_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002426
Dong-hee Naa9cab432018-05-30 00:04:08 +09002427_PARAM_NAME_MAPPING = {
2428 _POSITIONAL_ONLY: 'positional-only',
2429 _POSITIONAL_OR_KEYWORD: 'positional or keyword',
2430 _VAR_POSITIONAL: 'variadic positional',
2431 _KEYWORD_ONLY: 'keyword-only',
2432 _VAR_KEYWORD: 'variadic keyword'
2433}
2434
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002435
2436class Parameter:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002437 """Represents a parameter in a function signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002438
2439 Has the following public attributes:
2440
2441 * name : str
2442 The name of the parameter as a string.
2443 * default : object
2444 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002445 parameter has no default value, this attribute is set to
2446 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002447 * annotation
2448 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002449 parameter has no annotation, this attribute is set to
2450 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002451 * kind : str
2452 Describes how argument values are bound to the parameter.
2453 Possible values: `Parameter.POSITIONAL_ONLY`,
2454 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
2455 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002456 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002457
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002458 __slots__ = ('_name', '_kind', '_default', '_annotation')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002459
2460 POSITIONAL_ONLY = _POSITIONAL_ONLY
2461 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
2462 VAR_POSITIONAL = _VAR_POSITIONAL
2463 KEYWORD_ONLY = _KEYWORD_ONLY
2464 VAR_KEYWORD = _VAR_KEYWORD
2465
2466 empty = _empty
2467
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002468 def __init__(self, name, kind, *, default=_empty, annotation=_empty):
Dong-hee Naa9cab432018-05-30 00:04:08 +09002469 try:
2470 self._kind = _ParameterKind(kind)
2471 except ValueError:
2472 raise ValueError(f'value {kind!r} is not a valid Parameter.kind')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002473 if default is not _empty:
Dong-hee Naa9cab432018-05-30 00:04:08 +09002474 if self._kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2475 msg = '{} parameters cannot have default values'
Dong-hee Na4aa30062018-06-08 12:46:31 +09002476 msg = msg.format(self._kind.description)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002477 raise ValueError(msg)
2478 self._default = default
2479 self._annotation = annotation
2480
Yury Selivanov2393dca2014-01-27 15:07:58 -05002481 if name is _empty:
2482 raise ValueError('name is a required attribute for Parameter')
2483
2484 if not isinstance(name, str):
Dong-hee Naa9cab432018-05-30 00:04:08 +09002485 msg = 'name must be a str, not a {}'.format(type(name).__name__)
2486 raise TypeError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002487
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002488 if name[0] == '.' and name[1:].isdigit():
2489 # These are implicit arguments generated by comprehensions. In
2490 # order to provide a friendlier interface to users, we recast
2491 # their name as "implicitN" and treat them as positional-only.
2492 # See issue 19611.
Dong-hee Naa9cab432018-05-30 00:04:08 +09002493 if self._kind != _POSITIONAL_OR_KEYWORD:
2494 msg = (
2495 'implicit arguments must be passed as '
2496 'positional or keyword arguments, not {}'
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002497 )
Dong-hee Na4aa30062018-06-08 12:46:31 +09002498 msg = msg.format(self._kind.description)
Dong-hee Naa9cab432018-05-30 00:04:08 +09002499 raise ValueError(msg)
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002500 self._kind = _POSITIONAL_ONLY
2501 name = 'implicit{}'.format(name[1:])
2502
Yury Selivanov2393dca2014-01-27 15:07:58 -05002503 if not name.isidentifier():
2504 raise ValueError('{!r} is not a valid parameter name'.format(name))
2505
2506 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002507
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002508 def __reduce__(self):
2509 return (type(self),
2510 (self._name, self._kind),
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002511 {'_default': self._default,
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002512 '_annotation': self._annotation})
2513
2514 def __setstate__(self, state):
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002515 self._default = state['_default']
2516 self._annotation = state['_annotation']
2517
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002518 @property
2519 def name(self):
2520 return self._name
2521
2522 @property
2523 def default(self):
2524 return self._default
2525
2526 @property
2527 def annotation(self):
2528 return self._annotation
2529
2530 @property
2531 def kind(self):
2532 return self._kind
2533
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002534 def replace(self, *, name=_void, kind=_void,
2535 annotation=_void, default=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002536 """Creates a customized copy of the Parameter."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002537
2538 if name is _void:
2539 name = self._name
2540
2541 if kind is _void:
2542 kind = self._kind
2543
2544 if annotation is _void:
2545 annotation = self._annotation
2546
2547 if default is _void:
2548 default = self._default
2549
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002550 return type(self)(name, kind, default=default, annotation=annotation)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002551
2552 def __str__(self):
2553 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002554 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002555
2556 # Add annotation and default value
2557 if self._annotation is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002558 formatted = '{}: {}'.format(formatted,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002559 formatannotation(self._annotation))
2560
2561 if self._default is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002562 if self._annotation is not _empty:
2563 formatted = '{} = {}'.format(formatted, repr(self._default))
2564 else:
2565 formatted = '{}={}'.format(formatted, repr(self._default))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002566
2567 if kind == _VAR_POSITIONAL:
2568 formatted = '*' + formatted
2569 elif kind == _VAR_KEYWORD:
2570 formatted = '**' + formatted
2571
2572 return formatted
2573
2574 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002575 return '<{} "{}">'.format(self.__class__.__name__, self)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002576
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002577 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002578 return hash((self.name, self.kind, self.annotation, self.default))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002579
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002580 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002581 if self is other:
2582 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002583 if not isinstance(other, Parameter):
2584 return NotImplemented
2585 return (self._name == other._name and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002586 self._kind == other._kind and
2587 self._default == other._default and
2588 self._annotation == other._annotation)
2589
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002590
2591class BoundArguments:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002592 """Result of `Signature.bind` call. Holds the mapping of arguments
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002593 to the function's parameters.
2594
2595 Has the following public attributes:
2596
2597 * arguments : OrderedDict
2598 An ordered mutable mapping of parameters' names to arguments' values.
2599 Does not contain arguments' default values.
2600 * signature : Signature
2601 The Signature object that created this instance.
2602 * args : tuple
2603 Tuple of positional arguments values.
2604 * kwargs : dict
2605 Dict of keyword arguments values.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002606 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002607
Yury Selivanov6abe0322015-05-13 17:18:41 -04002608 __slots__ = ('arguments', '_signature', '__weakref__')
2609
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002610 def __init__(self, signature, arguments):
2611 self.arguments = arguments
2612 self._signature = signature
2613
2614 @property
2615 def signature(self):
2616 return self._signature
2617
2618 @property
2619 def args(self):
2620 args = []
2621 for param_name, param in self._signature.parameters.items():
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002622 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002623 break
2624
2625 try:
2626 arg = self.arguments[param_name]
2627 except KeyError:
2628 # We're done here. Other arguments
2629 # will be mapped in 'BoundArguments.kwargs'
2630 break
2631 else:
2632 if param.kind == _VAR_POSITIONAL:
2633 # *args
2634 args.extend(arg)
2635 else:
2636 # plain argument
2637 args.append(arg)
2638
2639 return tuple(args)
2640
2641 @property
2642 def kwargs(self):
2643 kwargs = {}
2644 kwargs_started = False
2645 for param_name, param in self._signature.parameters.items():
2646 if not kwargs_started:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002647 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002648 kwargs_started = True
2649 else:
2650 if param_name not in self.arguments:
2651 kwargs_started = True
2652 continue
2653
2654 if not kwargs_started:
2655 continue
2656
2657 try:
2658 arg = self.arguments[param_name]
2659 except KeyError:
2660 pass
2661 else:
2662 if param.kind == _VAR_KEYWORD:
2663 # **kwargs
2664 kwargs.update(arg)
2665 else:
2666 # plain keyword argument
2667 kwargs[param_name] = arg
2668
2669 return kwargs
2670
Yury Selivanovb907a512015-05-16 13:45:09 -04002671 def apply_defaults(self):
2672 """Set default values for missing arguments.
2673
2674 For variable-positional arguments (*args) the default is an
2675 empty tuple.
2676
2677 For variable-keyword arguments (**kwargs) the default is an
2678 empty dict.
2679 """
2680 arguments = self.arguments
Yury Selivanovb907a512015-05-16 13:45:09 -04002681 new_arguments = []
2682 for name, param in self._signature.parameters.items():
2683 try:
2684 new_arguments.append((name, arguments[name]))
2685 except KeyError:
2686 if param.default is not _empty:
2687 val = param.default
2688 elif param.kind is _VAR_POSITIONAL:
2689 val = ()
2690 elif param.kind is _VAR_KEYWORD:
2691 val = {}
2692 else:
2693 # This BoundArguments was likely produced by
2694 # Signature.bind_partial().
2695 continue
2696 new_arguments.append((name, val))
2697 self.arguments = OrderedDict(new_arguments)
2698
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002699 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002700 if self is other:
2701 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002702 if not isinstance(other, BoundArguments):
2703 return NotImplemented
2704 return (self.signature == other.signature and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002705 self.arguments == other.arguments)
2706
Yury Selivanov6abe0322015-05-13 17:18:41 -04002707 def __setstate__(self, state):
2708 self._signature = state['_signature']
2709 self.arguments = state['arguments']
2710
2711 def __getstate__(self):
2712 return {'_signature': self._signature, 'arguments': self.arguments}
2713
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002714 def __repr__(self):
2715 args = []
2716 for arg, value in self.arguments.items():
2717 args.append('{}={!r}'.format(arg, value))
Yury Selivanovf229bc52015-05-15 12:53:56 -04002718 return '<{} ({})>'.format(self.__class__.__name__, ', '.join(args))
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002719
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002720
2721class Signature:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002722 """A Signature object represents the overall signature of a function.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002723 It stores a Parameter object for each parameter accepted by the
2724 function, as well as information specific to the function itself.
2725
2726 A Signature object has the following public attributes and methods:
2727
2728 * parameters : OrderedDict
2729 An ordered mapping of parameters' names to the corresponding
2730 Parameter objects (keyword-only arguments are in the same order
2731 as listed in `code.co_varnames`).
2732 * return_annotation : object
2733 The annotation for the return type of the function if specified.
2734 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002735 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002736 * bind(*args, **kwargs) -> BoundArguments
2737 Creates a mapping from positional and keyword arguments to
2738 parameters.
2739 * bind_partial(*args, **kwargs) -> BoundArguments
2740 Creates a partial mapping from positional and keyword arguments
2741 to parameters (simulating 'functools.partial' behavior.)
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002742 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002743
2744 __slots__ = ('_return_annotation', '_parameters')
2745
2746 _parameter_cls = Parameter
2747 _bound_arguments_cls = BoundArguments
2748
2749 empty = _empty
2750
2751 def __init__(self, parameters=None, *, return_annotation=_empty,
2752 __validate_parameters__=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002753 """Constructs Signature from the given list of Parameter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002754 objects and 'return_annotation'. All arguments are optional.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002755 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002756
2757 if parameters is None:
2758 params = OrderedDict()
2759 else:
2760 if __validate_parameters__:
2761 params = OrderedDict()
2762 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002763 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002764
2765 for idx, param in enumerate(parameters):
2766 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002767 name = param.name
2768
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002769 if kind < top_kind:
Dong-hee Naa9cab432018-05-30 00:04:08 +09002770 msg = (
2771 'wrong parameter order: {} parameter before {} '
2772 'parameter'
2773 )
Dong-hee Na4aa30062018-06-08 12:46:31 +09002774 msg = msg.format(top_kind.description,
2775 kind.description)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002776 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002777 elif kind > top_kind:
2778 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002779 top_kind = kind
2780
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002781 if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
Yury Selivanov07a9e452014-01-29 10:58:16 -05002782 if param.default is _empty:
2783 if kind_defaults:
2784 # No default for this parameter, but the
2785 # previous parameter of the same kind had
2786 # a default
2787 msg = 'non-default argument follows default ' \
2788 'argument'
2789 raise ValueError(msg)
2790 else:
2791 # There is a default for this parameter.
2792 kind_defaults = True
2793
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002794 if name in params:
2795 msg = 'duplicate parameter name: {!r}'.format(name)
2796 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002797
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002798 params[name] = param
2799 else:
2800 params = OrderedDict(((param.name, param)
2801 for param in parameters))
2802
2803 self._parameters = types.MappingProxyType(params)
2804 self._return_annotation = return_annotation
2805
2806 @classmethod
2807 def from_function(cls, func):
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002808 """Constructs Signature for the given python function.
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002809
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002810 Deprecated since Python 3.5, use `Signature.from_callable()`.
2811 """
2812
2813 warnings.warn("inspect.Signature.from_function() is deprecated since "
2814 "Python 3.5, use Signature.from_callable()",
Berker Peksagb5601582015-05-21 23:40:54 +03002815 DeprecationWarning, stacklevel=2)
Yury Selivanovcf45f022015-05-20 14:38:50 -04002816 return _signature_from_function(cls, func)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002817
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002818 @classmethod
2819 def from_builtin(cls, func):
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002820 """Constructs Signature for the given builtin function.
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002821
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002822 Deprecated since Python 3.5, use `Signature.from_callable()`.
2823 """
2824
2825 warnings.warn("inspect.Signature.from_builtin() is deprecated since "
2826 "Python 3.5, use Signature.from_callable()",
Berker Peksagb5601582015-05-21 23:40:54 +03002827 DeprecationWarning, stacklevel=2)
Yury Selivanov57d240e2014-02-19 16:27:23 -05002828 return _signature_from_builtin(cls, func)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002829
Yury Selivanovda396452014-03-27 12:09:24 -04002830 @classmethod
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002831 def from_callable(cls, obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002832 """Constructs Signature for the given callable object."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002833 return _signature_from_callable(obj, sigcls=cls,
2834 follow_wrapper_chains=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04002835
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002836 @property
2837 def parameters(self):
2838 return self._parameters
2839
2840 @property
2841 def return_annotation(self):
2842 return self._return_annotation
2843
2844 def replace(self, *, parameters=_void, return_annotation=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002845 """Creates a customized copy of the Signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002846 Pass 'parameters' and/or 'return_annotation' arguments
2847 to override them in the new copy.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002848 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002849
2850 if parameters is _void:
2851 parameters = self.parameters.values()
2852
2853 if return_annotation is _void:
2854 return_annotation = self._return_annotation
2855
2856 return type(self)(parameters,
2857 return_annotation=return_annotation)
2858
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002859 def _hash_basis(self):
2860 params = tuple(param for param in self.parameters.values()
2861 if param.kind != _KEYWORD_ONLY)
2862
2863 kwo_params = {param.name: param for param in self.parameters.values()
2864 if param.kind == _KEYWORD_ONLY}
2865
2866 return params, kwo_params, self.return_annotation
2867
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002868 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002869 params, kwo_params, return_annotation = self._hash_basis()
2870 kwo_params = frozenset(kwo_params.values())
2871 return hash((params, kwo_params, return_annotation))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002872
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002873 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002874 if self is other:
2875 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002876 if not isinstance(other, Signature):
2877 return NotImplemented
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002878 return self._hash_basis() == other._hash_basis()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002879
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002880 def _bind(self, args, kwargs, *, partial=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002881 """Private method. Don't use directly."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002882
2883 arguments = OrderedDict()
2884
2885 parameters = iter(self.parameters.values())
2886 parameters_ex = ()
2887 arg_vals = iter(args)
2888
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002889 while True:
2890 # Let's iterate through the positional arguments and corresponding
2891 # parameters
2892 try:
2893 arg_val = next(arg_vals)
2894 except StopIteration:
2895 # No more positional arguments
2896 try:
2897 param = next(parameters)
2898 except StopIteration:
2899 # No more parameters. That's it. Just need to check that
2900 # we have no `kwargs` after this while loop
2901 break
2902 else:
2903 if param.kind == _VAR_POSITIONAL:
2904 # That's OK, just empty *args. Let's start parsing
2905 # kwargs
2906 break
2907 elif param.name in kwargs:
2908 if param.kind == _POSITIONAL_ONLY:
2909 msg = '{arg!r} parameter is positional only, ' \
2910 'but was passed as a keyword'
2911 msg = msg.format(arg=param.name)
2912 raise TypeError(msg) from None
2913 parameters_ex = (param,)
2914 break
2915 elif (param.kind == _VAR_KEYWORD or
2916 param.default is not _empty):
2917 # That's fine too - we have a default value for this
2918 # parameter. So, lets start parsing `kwargs`, starting
2919 # with the current parameter
2920 parameters_ex = (param,)
2921 break
2922 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002923 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
2924 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002925 if partial:
2926 parameters_ex = (param,)
2927 break
2928 else:
Yury Selivanov86872752015-05-19 00:27:49 -04002929 msg = 'missing a required argument: {arg!r}'
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002930 msg = msg.format(arg=param.name)
2931 raise TypeError(msg) from None
2932 else:
2933 # We have a positional argument to process
2934 try:
2935 param = next(parameters)
2936 except StopIteration:
2937 raise TypeError('too many positional arguments') from None
2938 else:
2939 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
2940 # Looks like we have no parameter for this positional
2941 # argument
Yury Selivanov86872752015-05-19 00:27:49 -04002942 raise TypeError(
2943 'too many positional arguments') from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002944
2945 if param.kind == _VAR_POSITIONAL:
2946 # We have an '*args'-like argument, let's fill it with
2947 # all positional arguments we have left and move on to
2948 # the next phase
2949 values = [arg_val]
2950 values.extend(arg_vals)
2951 arguments[param.name] = tuple(values)
2952 break
2953
2954 if param.name in kwargs:
Yury Selivanov86872752015-05-19 00:27:49 -04002955 raise TypeError(
2956 'multiple values for argument {arg!r}'.format(
2957 arg=param.name)) from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002958
2959 arguments[param.name] = arg_val
2960
2961 # Now, we iterate through the remaining parameters to process
2962 # keyword arguments
2963 kwargs_param = None
2964 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002965 if param.kind == _VAR_KEYWORD:
2966 # Memorize that we have a '**kwargs'-like parameter
2967 kwargs_param = param
2968 continue
2969
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002970 if param.kind == _VAR_POSITIONAL:
2971 # Named arguments don't refer to '*args'-like parameters.
2972 # We only arrive here if the positional arguments ended
2973 # before reaching the last parameter before *args.
2974 continue
2975
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002976 param_name = param.name
2977 try:
2978 arg_val = kwargs.pop(param_name)
2979 except KeyError:
2980 # We have no value for this parameter. It's fine though,
2981 # if it has a default value, or it is an '*args'-like
2982 # parameter, left alone by the processing of positional
2983 # arguments.
2984 if (not partial and param.kind != _VAR_POSITIONAL and
2985 param.default is _empty):
Yury Selivanov86872752015-05-19 00:27:49 -04002986 raise TypeError('missing a required argument: {arg!r}'. \
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002987 format(arg=param_name)) from None
2988
2989 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05002990 if param.kind == _POSITIONAL_ONLY:
2991 # This should never happen in case of a properly built
2992 # Signature object (but let's have this check here
2993 # to ensure correct behaviour just in case)
2994 raise TypeError('{arg!r} parameter is positional only, '
2995 'but was passed as a keyword'. \
2996 format(arg=param.name))
2997
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002998 arguments[param_name] = arg_val
2999
3000 if kwargs:
3001 if kwargs_param is not None:
3002 # Process our '**kwargs'-like parameter
3003 arguments[kwargs_param.name] = kwargs
3004 else:
Yury Selivanov86872752015-05-19 00:27:49 -04003005 raise TypeError(
3006 'got an unexpected keyword argument {arg!r}'.format(
3007 arg=next(iter(kwargs))))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003008
3009 return self._bound_arguments_cls(self, arguments)
3010
Yury Selivanovc45873e2014-01-29 12:10:27 -05003011 def bind(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003012 """Get a BoundArguments object, that maps the passed `args`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003013 and `kwargs` to the function's signature. Raises `TypeError`
3014 if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003015 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05003016 return args[0]._bind(args[1:], kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003017
Yury Selivanovc45873e2014-01-29 12:10:27 -05003018 def bind_partial(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003019 """Get a BoundArguments object, that partially maps the
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003020 passed `args` and `kwargs` to the function's signature.
3021 Raises `TypeError` if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003022 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05003023 return args[0]._bind(args[1:], kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003024
Yury Selivanova5d63dd2014-03-27 11:31:43 -04003025 def __reduce__(self):
3026 return (type(self),
3027 (tuple(self._parameters.values()),),
3028 {'_return_annotation': self._return_annotation})
3029
3030 def __setstate__(self, state):
3031 self._return_annotation = state['_return_annotation']
3032
Yury Selivanov374375d2014-03-27 12:41:53 -04003033 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04003034 return '<{} {}>'.format(self.__class__.__name__, self)
Yury Selivanov374375d2014-03-27 12:41:53 -04003035
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003036 def __str__(self):
3037 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05003038 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003039 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05003040 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003041 formatted = str(param)
3042
3043 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05003044
3045 if kind == _POSITIONAL_ONLY:
3046 render_pos_only_separator = True
3047 elif render_pos_only_separator:
3048 # It's not a positional-only parameter, and the flag
3049 # is set to 'True' (there were pos-only params before.)
3050 result.append('/')
3051 render_pos_only_separator = False
3052
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003053 if kind == _VAR_POSITIONAL:
3054 # OK, we have an '*args'-like parameter, so we won't need
3055 # a '*' to separate keyword-only arguments
3056 render_kw_only_separator = False
3057 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
3058 # We have a keyword-only parameter to render and we haven't
3059 # rendered an '*args'-like parameter before, so add a '*'
3060 # separator to the parameters list ("foo(arg1, *, arg2)" case)
3061 result.append('*')
3062 # This condition should be only triggered once, so
3063 # reset the flag
3064 render_kw_only_separator = False
3065
3066 result.append(formatted)
3067
Yury Selivanov2393dca2014-01-27 15:07:58 -05003068 if render_pos_only_separator:
3069 # There were only positional-only parameters, hence the
3070 # flag was not reset to 'False'
3071 result.append('/')
3072
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003073 rendered = '({})'.format(', '.join(result))
3074
3075 if self.return_annotation is not _empty:
3076 anno = formatannotation(self.return_annotation)
3077 rendered += ' -> {}'.format(anno)
3078
3079 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003080
Yury Selivanovda396452014-03-27 12:09:24 -04003081
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04003082def signature(obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003083 """Get a signature object for the passed callable."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04003084 return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04003085
3086
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003087def _main():
3088 """ Logic for inspecting an object given at command line """
3089 import argparse
3090 import importlib
3091
3092 parser = argparse.ArgumentParser()
3093 parser.add_argument(
3094 'object',
3095 help="The object to be analysed. "
3096 "It supports the 'module:qualname' syntax")
3097 parser.add_argument(
3098 '-d', '--details', action='store_true',
3099 help='Display info about the module rather than its source code')
3100
3101 args = parser.parse_args()
3102
3103 target = args.object
3104 mod_name, has_attrs, attrs = target.partition(":")
3105 try:
3106 obj = module = importlib.import_module(mod_name)
3107 except Exception as exc:
3108 msg = "Failed to import {} ({}: {})".format(mod_name,
3109 type(exc).__name__,
3110 exc)
3111 print(msg, file=sys.stderr)
3112 exit(2)
3113
3114 if has_attrs:
3115 parts = attrs.split(".")
3116 obj = module
3117 for part in parts:
3118 obj = getattr(obj, part)
3119
3120 if module.__name__ in sys.builtin_module_names:
3121 print("Can't get info for builtin modules.", file=sys.stderr)
3122 exit(1)
3123
3124 if args.details:
3125 print('Target: {}'.format(target))
3126 print('Origin: {}'.format(getsourcefile(module)))
3127 print('Cached: {}'.format(module.__cached__))
3128 if obj is module:
3129 print('Loader: {}'.format(repr(module.__loader__)))
3130 if hasattr(module, '__path__'):
3131 print('Submodule search path: {}'.format(module.__path__))
3132 else:
3133 try:
3134 __, lineno = findsource(obj)
3135 except Exception:
3136 pass
3137 else:
3138 print('Line: {}'.format(lineno))
3139
3140 print('\n')
3141 else:
3142 print(getsource(obj))
3143
3144
3145if __name__ == "__main__":
3146 _main()