blob: e799a83a74046fa59df71789611f5610e3e9fbe2 [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
Christian Heimes7131fd92008-02-19 14:21:46 +0000171def isgeneratorfunction(object):
172 """Return true if the object is a user-defined generator function.
173
Martin Panter0f0eac42016-09-07 11:04:41 +0000174 Generator function objects provide the same attributes as functions.
175 See help(isfunction) for a list of attributes."""
Georg Brandlb1441c72009-01-03 22:33:39 +0000176 return bool((isfunction(object) or ismethod(object)) and
Yury Selivanov5376ba92015-06-22 12:19:30 -0400177 object.__code__.co_flags & CO_GENERATOR)
Yury Selivanov75445082015-05-11 22:57:16 -0400178
179def iscoroutinefunction(object):
180 """Return true if the object is a coroutine function.
181
Yury Selivanov4778e132016-11-08 12:23:09 -0500182 Coroutine functions are defined with "async def" syntax.
Yury Selivanov75445082015-05-11 22:57:16 -0400183 """
184 return bool((isfunction(object) or ismethod(object)) and
Yury Selivanov5376ba92015-06-22 12:19:30 -0400185 object.__code__.co_flags & CO_COROUTINE)
Yury Selivanov75445082015-05-11 22:57:16 -0400186
Yury Selivanoveb636452016-09-08 22:01:51 -0700187def isasyncgenfunction(object):
Yury Selivanov4778e132016-11-08 12:23:09 -0500188 """Return true if the object is an asynchronous generator function.
189
190 Asynchronous generator functions are defined with "async def"
191 syntax and have "yield" expressions in their body.
192 """
Yury Selivanoveb636452016-09-08 22:01:51 -0700193 return bool((isfunction(object) or ismethod(object)) and
194 object.__code__.co_flags & CO_ASYNC_GENERATOR)
195
196def isasyncgen(object):
Yury Selivanov4778e132016-11-08 12:23:09 -0500197 """Return true if the object is an asynchronous generator."""
Yury Selivanoveb636452016-09-08 22:01:51 -0700198 return isinstance(object, types.AsyncGeneratorType)
199
Christian Heimes7131fd92008-02-19 14:21:46 +0000200def isgenerator(object):
201 """Return true if the object is a generator.
202
203 Generator objects provide these attributes:
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300204 __iter__ defined to support iteration over container
Christian Heimes7131fd92008-02-19 14:21:46 +0000205 close raises a new GeneratorExit exception inside the
206 generator to terminate the iteration
207 gi_code code object
208 gi_frame frame object or possibly None once the generator has
209 been exhausted
210 gi_running set to 1 when generator is executing, 0 otherwise
211 next return the next item from the container
212 send resumes the generator and "sends" a value that becomes
213 the result of the current yield-expression
214 throw used to raise an exception inside the generator"""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400215 return isinstance(object, types.GeneratorType)
Yury Selivanov75445082015-05-11 22:57:16 -0400216
217def iscoroutine(object):
218 """Return true if the object is a coroutine."""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400219 return isinstance(object, types.CoroutineType)
Christian Heimes7131fd92008-02-19 14:21:46 +0000220
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400221def isawaitable(object):
Yury Selivanovc0215df2016-11-08 19:57:44 -0500222 """Return true if object can be passed to an ``await`` expression."""
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400223 return (isinstance(object, types.CoroutineType) or
224 isinstance(object, types.GeneratorType) and
Yury Selivanovc0215df2016-11-08 19:57:44 -0500225 bool(object.gi_code.co_flags & CO_ITERABLE_COROUTINE) or
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400226 isinstance(object, collections.abc.Awaitable))
227
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000228def istraceback(object):
229 """Return true if the object is a traceback.
230
231 Traceback objects provide these attributes:
232 tb_frame frame object at this level
233 tb_lasti index of last attempted instruction in bytecode
234 tb_lineno current line number in Python source code
235 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000236 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000237
238def isframe(object):
239 """Return true if the object is a frame object.
240
241 Frame objects provide these attributes:
242 f_back next outer frame object (this frame's caller)
243 f_builtins built-in namespace seen by this frame
244 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000245 f_globals global namespace seen by this frame
246 f_lasti index of last attempted instruction in bytecode
247 f_lineno current line number in Python source code
248 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000249 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000250 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000251
252def iscode(object):
253 """Return true if the object is a code object.
254
255 Code objects provide these attributes:
Xiang Zhanga6902e62017-04-13 10:38:28 +0800256 co_argcount number of arguments (not including *, ** args
257 or keyword only arguments)
258 co_code string of raw compiled bytecode
259 co_cellvars tuple of names of cell variables
260 co_consts tuple of constants used in the bytecode
261 co_filename name of file in which this code object was created
262 co_firstlineno number of first line in Python source code
263 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
264 | 16=nested | 32=generator | 64=nofree | 128=coroutine
265 | 256=iterable_coroutine | 512=async_generator
266 co_freevars tuple of names of free variables
267 co_kwonlyargcount number of keyword only arguments (not including ** arg)
268 co_lnotab encoded mapping of line numbers to bytecode indices
269 co_name name with which this code object was defined
270 co_names tuple of names of local variables
271 co_nlocals number of local variables
272 co_stacksize virtual machine stack space required
273 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000274 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000275
276def isbuiltin(object):
277 """Return true if the object is a built-in function or method.
278
279 Built-in functions and methods provide these attributes:
280 __doc__ documentation string
281 __name__ original name of this function or method
282 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000283 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000284
285def isroutine(object):
286 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000287 return (isbuiltin(object)
288 or isfunction(object)
289 or ismethod(object)
290 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000291
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000292def isabstract(object):
293 """Return true if the object is an abstract base class (ABC)."""
Natefcfe80e2017-04-24 10:06:15 -0700294 if not isinstance(object, type):
295 return False
296 if object.__flags__ & TPFLAGS_IS_ABSTRACT:
297 return True
298 if not issubclass(type(object), abc.ABCMeta):
299 return False
300 if hasattr(object, '__abstractmethods__'):
301 # It looks like ABCMeta.__new__ has finished running;
302 # TPFLAGS_IS_ABSTRACT should have been accurate.
303 return False
304 # It looks like ABCMeta.__new__ has not finished running yet; we're
305 # probably in __init_subclass__. We'll look for abstractmethods manually.
306 for name, value in object.__dict__.items():
307 if getattr(value, "__isabstractmethod__", False):
308 return True
309 for base in object.__bases__:
310 for name in getattr(base, "__abstractmethods__", ()):
311 value = getattr(object, name, None)
312 if getattr(value, "__isabstractmethod__", False):
313 return True
314 return False
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000315
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000316def getmembers(object, predicate=None):
317 """Return all members of an object as (name, value) pairs sorted by name.
318 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100319 if isclass(object):
320 mro = (object,) + getmro(object)
321 else:
322 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000323 results = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700324 processed = set()
325 names = dir(object)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700326 # :dd any DynamicClassAttributes to the list of names if object is a class;
Ethan Furmane03ea372013-09-25 07:14:41 -0700327 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700328 # attribute with the same name as a DynamicClassAttribute exists
Ethan Furmane03ea372013-09-25 07:14:41 -0700329 try:
330 for base in object.__bases__:
331 for k, v in base.__dict__.items():
332 if isinstance(v, types.DynamicClassAttribute):
333 names.append(k)
334 except AttributeError:
335 pass
336 for key in names:
Ethan Furman63c141c2013-10-18 00:27:39 -0700337 # First try to get the value via getattr. Some descriptors don't
338 # like calling their __get__ (see bug #1785), so fall back to
339 # looking in the __dict__.
340 try:
341 value = getattr(object, key)
342 # handle the duplicate key
343 if key in processed:
344 raise AttributeError
345 except AttributeError:
346 for base in mro:
347 if key in base.__dict__:
348 value = base.__dict__[key]
349 break
350 else:
351 # could be a (currently) missing slot member, or a buggy
352 # __dir__; discard and move on
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100353 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000354 if not predicate or predicate(value):
355 results.append((key, value))
Ethan Furmane03ea372013-09-25 07:14:41 -0700356 processed.add(key)
357 results.sort(key=lambda pair: pair[0])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000358 return results
359
Christian Heimes25bb7832008-01-11 16:17:00 +0000360Attribute = namedtuple('Attribute', 'name kind defining_class object')
361
Tim Peters13b49d32001-09-23 02:00:29 +0000362def classify_class_attrs(cls):
363 """Return list of attribute-descriptor tuples.
364
365 For each name in dir(cls), the return list contains a 4-tuple
366 with these elements:
367
368 0. The name (a string).
369
370 1. The kind of attribute this is, one of these strings:
371 'class method' created via classmethod()
372 'static method' created via staticmethod()
373 'property' created via property()
Ethan Furmane03ea372013-09-25 07:14:41 -0700374 'method' any other flavor of method or descriptor
Tim Peters13b49d32001-09-23 02:00:29 +0000375 'data' not a method
376
377 2. The class which defined this attribute (a class).
378
Ethan Furmane03ea372013-09-25 07:14:41 -0700379 3. The object as obtained by calling getattr; if this fails, or if the
380 resulting object does not live anywhere in the class' mro (including
381 metaclasses) then the object is looked up in the defining class's
382 dict (found by walking the mro).
Ethan Furman668dede2013-09-14 18:53:26 -0700383
384 If one of the items in dir(cls) is stored in the metaclass it will now
385 be discovered and not have None be listed as the class in which it was
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700386 defined. Any items whose home class cannot be discovered are skipped.
Tim Peters13b49d32001-09-23 02:00:29 +0000387 """
388
389 mro = getmro(cls)
Ethan Furman668dede2013-09-14 18:53:26 -0700390 metamro = getmro(type(cls)) # for attributes stored in the metaclass
Jon Dufresne39726282017-05-18 07:35:54 -0700391 metamro = tuple(cls for cls in metamro if cls not in (type, object))
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700392 class_bases = (cls,) + mro
393 all_bases = class_bases + metamro
Tim Peters13b49d32001-09-23 02:00:29 +0000394 names = dir(cls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700395 # :dd any DynamicClassAttributes to the list of names;
Ethan Furmane03ea372013-09-25 07:14:41 -0700396 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700397 # attribute with the same name as a DynamicClassAttribute exists.
Ethan Furman63c141c2013-10-18 00:27:39 -0700398 for base in mro:
Ethan Furmane03ea372013-09-25 07:14:41 -0700399 for k, v in base.__dict__.items():
400 if isinstance(v, types.DynamicClassAttribute):
401 names.append(k)
Tim Peters13b49d32001-09-23 02:00:29 +0000402 result = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700403 processed = set()
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700404
Tim Peters13b49d32001-09-23 02:00:29 +0000405 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100406 # Get the object associated with the name, and where it was defined.
Ethan Furmane03ea372013-09-25 07:14:41 -0700407 # Normal objects will be looked up with both getattr and directly in
408 # its class' dict (in case getattr fails [bug #1785], and also to look
409 # for a docstring).
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700410 # For DynamicClassAttributes on the second pass we only look in the
Ethan Furmane03ea372013-09-25 07:14:41 -0700411 # class's dict.
412 #
Tim Peters13b49d32001-09-23 02:00:29 +0000413 # Getting an obj from the __dict__ sometimes reveals more than
414 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100415 homecls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700416 get_obj = None
417 dict_obj = None
Ethan Furmane03ea372013-09-25 07:14:41 -0700418 if name not in processed:
419 try:
Ethan Furmana8b07072013-10-18 01:22:08 -0700420 if name == '__dict__':
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700421 raise Exception("__dict__ is special, don't want the proxy")
Ethan Furmane03ea372013-09-25 07:14:41 -0700422 get_obj = getattr(cls, name)
423 except Exception as exc:
424 pass
425 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700426 homecls = getattr(get_obj, "__objclass__", homecls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700427 if homecls not in class_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700428 # if the resulting object does not live somewhere in the
Ethan Furman63c141c2013-10-18 00:27:39 -0700429 # mro, drop it and search the mro manually
Ethan Furmane03ea372013-09-25 07:14:41 -0700430 homecls = None
Ethan Furman63c141c2013-10-18 00:27:39 -0700431 last_cls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700432 # first look in the classes
433 for srch_cls in class_bases:
Ethan Furman63c141c2013-10-18 00:27:39 -0700434 srch_obj = getattr(srch_cls, name, None)
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400435 if srch_obj is get_obj:
Ethan Furman63c141c2013-10-18 00:27:39 -0700436 last_cls = srch_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700437 # then check the metaclasses
438 for srch_cls in metamro:
439 try:
440 srch_obj = srch_cls.__getattr__(cls, name)
441 except AttributeError:
442 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400443 if srch_obj is get_obj:
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700444 last_cls = srch_cls
Ethan Furman63c141c2013-10-18 00:27:39 -0700445 if last_cls is not None:
446 homecls = last_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700447 for base in all_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700448 if name in base.__dict__:
449 dict_obj = base.__dict__[name]
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700450 if homecls not in metamro:
451 homecls = base
Ethan Furmane03ea372013-09-25 07:14:41 -0700452 break
Ethan Furman63c141c2013-10-18 00:27:39 -0700453 if homecls is None:
454 # unable to locate the attribute anywhere, most likely due to
455 # buggy custom __dir__; discard and move on
456 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400457 obj = get_obj if get_obj is not None else dict_obj
Ethan Furmane03ea372013-09-25 07:14:41 -0700458 # Classify the object or its descriptor.
Serhiy Storchaka3327a2d2017-12-15 14:13:41 +0200459 if isinstance(dict_obj, (staticmethod, types.BuiltinMethodType)):
Tim Peters13b49d32001-09-23 02:00:29 +0000460 kind = "static method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700461 obj = dict_obj
Serhiy Storchaka3327a2d2017-12-15 14:13:41 +0200462 elif isinstance(dict_obj, (classmethod, types.ClassMethodDescriptorType)):
Tim Peters13b49d32001-09-23 02:00:29 +0000463 kind = "class method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700464 obj = dict_obj
465 elif isinstance(dict_obj, property):
Tim Peters13b49d32001-09-23 02:00:29 +0000466 kind = "property"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700467 obj = dict_obj
Yury Selivanov0860a0b2014-01-31 14:28:44 -0500468 elif isroutine(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000469 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100470 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700471 kind = "data"
Christian Heimes25bb7832008-01-11 16:17:00 +0000472 result.append(Attribute(name, kind, homecls, obj))
Ethan Furmane03ea372013-09-25 07:14:41 -0700473 processed.add(name)
Tim Peters13b49d32001-09-23 02:00:29 +0000474 return result
475
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000476# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000477
478def getmro(cls):
479 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000480 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000481
Nick Coghlane8c45d62013-07-28 20:00:01 +1000482# -------------------------------------------------------- function helpers
483
484def unwrap(func, *, stop=None):
485 """Get the object wrapped by *func*.
486
487 Follows the chain of :attr:`__wrapped__` attributes returning the last
488 object in the chain.
489
490 *stop* is an optional callback accepting an object in the wrapper chain
491 as its sole argument that allows the unwrapping to be terminated early if
492 the callback returns a true value. If the callback never returns a true
493 value, the last object in the chain is returned as usual. For example,
494 :func:`signature` uses this to stop unwrapping if any object in the
495 chain has a ``__signature__`` attribute defined.
496
497 :exc:`ValueError` is raised if a cycle is encountered.
498
499 """
500 if stop is None:
501 def _is_wrapper(f):
502 return hasattr(f, '__wrapped__')
503 else:
504 def _is_wrapper(f):
505 return hasattr(f, '__wrapped__') and not stop(f)
506 f = func # remember the original func for error reporting
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100507 # Memoise by id to tolerate non-hashable objects, but store objects to
508 # ensure they aren't destroyed, which would allow their IDs to be reused.
509 memo = {id(f): f}
510 recursion_limit = sys.getrecursionlimit()
Nick Coghlane8c45d62013-07-28 20:00:01 +1000511 while _is_wrapper(func):
512 func = func.__wrapped__
513 id_func = id(func)
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100514 if (id_func in memo) or (len(memo) >= recursion_limit):
Nick Coghlane8c45d62013-07-28 20:00:01 +1000515 raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100516 memo[id_func] = func
Nick Coghlane8c45d62013-07-28 20:00:01 +1000517 return func
518
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000519# -------------------------------------------------- source code extraction
520def indentsize(line):
521 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000522 expline = line.expandtabs()
523 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000524
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300525def _findclass(func):
526 cls = sys.modules.get(func.__module__)
527 if cls is None:
528 return None
529 for name in func.__qualname__.split('.')[:-1]:
530 cls = getattr(cls, name)
531 if not isclass(cls):
532 return None
533 return cls
534
535def _finddoc(obj):
536 if isclass(obj):
537 for base in obj.__mro__:
538 if base is not object:
539 try:
540 doc = base.__doc__
541 except AttributeError:
542 continue
543 if doc is not None:
544 return doc
545 return None
546
547 if ismethod(obj):
548 name = obj.__func__.__name__
549 self = obj.__self__
550 if (isclass(self) and
551 getattr(getattr(self, name, None), '__func__') is obj.__func__):
552 # classmethod
553 cls = self
554 else:
555 cls = self.__class__
556 elif isfunction(obj):
557 name = obj.__name__
558 cls = _findclass(obj)
559 if cls is None or getattr(cls, name) is not obj:
560 return None
561 elif isbuiltin(obj):
562 name = obj.__name__
563 self = obj.__self__
564 if (isclass(self) and
565 self.__qualname__ + '.' + name == obj.__qualname__):
566 # classmethod
567 cls = self
568 else:
569 cls = self.__class__
Serhiy Storchakaac4bdcc2015-10-29 08:15:50 +0200570 # Should be tested before isdatadescriptor().
571 elif isinstance(obj, property):
572 func = obj.fget
573 name = func.__name__
574 cls = _findclass(func)
575 if cls is None or getattr(cls, name) is not obj:
576 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300577 elif ismethoddescriptor(obj) or isdatadescriptor(obj):
578 name = obj.__name__
579 cls = obj.__objclass__
580 if getattr(cls, name) is not obj:
581 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300582 else:
583 return None
584
585 for base in cls.__mro__:
586 try:
587 doc = getattr(base, name).__doc__
588 except AttributeError:
589 continue
590 if doc is not None:
591 return doc
592 return None
593
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000594def getdoc(object):
595 """Get the documentation string for an object.
596
597 All tabs are expanded to spaces. To clean up docstrings that are
598 indented to line up with blocks of code, any whitespace than can be
599 uniformly removed from the second line onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000600 try:
601 doc = object.__doc__
602 except AttributeError:
603 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300604 if doc is None:
605 try:
606 doc = _finddoc(object)
607 except (AttributeError, TypeError):
608 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000609 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000610 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000611 return cleandoc(doc)
612
613def cleandoc(doc):
614 """Clean up indentation from docstrings.
615
616 Any whitespace that can be uniformly removed from the second line
617 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000618 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000619 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000620 except UnicodeError:
621 return None
622 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000623 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000624 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000625 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000626 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000627 if content:
628 indent = len(line) - content
629 margin = min(margin, indent)
630 # Remove indentation.
631 if lines:
632 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000633 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000634 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000635 # Remove any trailing or leading blank lines.
636 while lines and not lines[-1]:
637 lines.pop()
638 while lines and not lines[0]:
639 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000640 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000641
642def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000643 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000644 if ismodule(object):
Jason R. Coombsb9650a02018-03-05 18:29:08 -0500645 if getattr(object, '__file__', None):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000646 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000647 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000648 if isclass(object):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500649 if hasattr(object, '__module__'):
650 object = sys.modules.get(object.__module__)
Jason R. Coombsb9650a02018-03-05 18:29:08 -0500651 if getattr(object, '__file__', None):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500652 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000653 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000654 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000655 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000656 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000657 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000658 if istraceback(object):
659 object = object.tb_frame
660 if isframe(object):
661 object = object.f_code
662 if iscode(object):
663 return object.co_filename
Thomas Kluyvere968bc732017-10-24 13:42:36 +0100664 raise TypeError('module, class, method, function, traceback, frame, or '
665 'code object was expected, got {}'.format(
666 type(object).__name__))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000667
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000668def getmodulename(path):
669 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000670 fname = os.path.basename(path)
671 # Check for paths that look like an actual module file
672 suffixes = [(-len(suffix), suffix)
673 for suffix in importlib.machinery.all_suffixes()]
674 suffixes.sort() # try longest suffixes first, in case they overlap
675 for neglen, suffix in suffixes:
676 if fname.endswith(suffix):
677 return fname[:neglen]
678 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000679
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000680def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000681 """Return the filename that can be used to locate an object's source.
682 Return None if no way can be identified to get the source.
683 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000684 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400685 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
686 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
687 if any(filename.endswith(s) for s in all_bytecode_suffixes):
688 filename = (os.path.splitext(filename)[0] +
689 importlib.machinery.SOURCE_SUFFIXES[0])
690 elif any(filename.endswith(s) for s in
691 importlib.machinery.EXTENSION_SUFFIXES):
692 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000693 if os.path.exists(filename):
694 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000695 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400696 if getattr(getmodule(object, filename), '__loader__', None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000697 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000698 # or it is in the linecache
699 if filename in linecache.cache:
700 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000701
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000702def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000703 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000704
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000705 The idea is for each object to have a unique origin, so this routine
706 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000707 if _filename is None:
708 _filename = getsourcefile(object) or getfile(object)
709 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000710
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000711modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000712_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000713
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000714def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000715 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000716 if ismodule(object):
717 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000718 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000719 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000720 # Try the filename to modulename cache
721 if _filename is not None and _filename in modulesbyfile:
722 return sys.modules.get(modulesbyfile[_filename])
723 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000724 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000725 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000726 except TypeError:
727 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000728 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000729 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000730 # Update the filename to module name cache and check yet again
731 # Copy sys.modules in order to cope with changes while iterating
Éric Araujoa74f8ef2011-11-29 16:58:53 +0100732 for modname, module in list(sys.modules.items()):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000733 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000734 f = module.__file__
735 if f == _filesbymodname.get(modname, None):
736 # Have already mapped this module, so skip it
737 continue
738 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000739 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000740 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000741 modulesbyfile[f] = modulesbyfile[
742 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000743 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000744 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000745 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000746 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000747 if not hasattr(object, '__name__'):
748 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000749 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000750 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000751 if mainobject is object:
752 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000753 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000754 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000755 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000756 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000757 if builtinobject is object:
758 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000759
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000760def findsource(object):
761 """Return the entire source file and starting line number for an object.
762
763 The argument may be a module, class, method, function, traceback, frame,
764 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200765 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000766 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500767
Yury Selivanovef1e7502014-12-08 16:05:34 -0500768 file = getsourcefile(object)
769 if file:
770 # Invalidate cache if needed.
771 linecache.checkcache(file)
772 else:
773 file = getfile(object)
774 # Allow filenames in form of "<something>" to pass through.
775 # `doctest` monkeypatches `linecache` module to enable
776 # inspection, so let `linecache.getlines` to be called.
777 if not (file.startswith('<') and file.endswith('>')):
778 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500779
Thomas Wouters89f507f2006-12-13 04:49:30 +0000780 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000781 if module:
782 lines = linecache.getlines(file, module.__dict__)
783 else:
784 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000785 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200786 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000787
788 if ismodule(object):
789 return lines, 0
790
791 if isclass(object):
792 name = object.__name__
Thomas Wouters89f507f2006-12-13 04:49:30 +0000793 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
794 # make some effort to find the best matching class definition:
795 # use the one with the least indentation, which is the one
796 # that's most probably not inside a function definition.
797 candidates = []
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000798 for i in range(len(lines)):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000799 match = pat.match(lines[i])
800 if match:
801 # if it's at toplevel, it's already the best one
802 if lines[i][0] == 'c':
803 return lines, i
804 # else add whitespace to candidate list
805 candidates.append((match.group(1), i))
806 if candidates:
807 # this will sort by whitespace, and by line number,
808 # less whitespace first
809 candidates.sort()
810 return lines, candidates[0][1]
Jeremy Hyltonab919022003-06-27 18:41:20 +0000811 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200812 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000813
814 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000815 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000816 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000817 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000818 if istraceback(object):
819 object = object.tb_frame
820 if isframe(object):
821 object = object.f_code
822 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000823 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200824 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000825 lnum = object.co_firstlineno - 1
Yury Selivanove4e811d2015-07-21 19:01:52 +0300826 pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000827 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000828 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000829 lnum = lnum - 1
830 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200831 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000832
833def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000834 """Get lines of comments immediately preceding an object's source code.
835
836 Returns None when source can't be found.
837 """
838 try:
839 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200840 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000841 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000842
843 if ismodule(object):
844 # Look for a comment block at the top of the file.
845 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000846 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000847 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000848 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000849 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000850 comments = []
851 end = start
852 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000853 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000854 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000855 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000856
857 # Look for a preceding block of comments at the same indentation.
858 elif lnum > 0:
859 indent = indentsize(lines[lnum])
860 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000861 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000862 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000863 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000864 if end > 0:
865 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000866 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000867 while comment[:1] == '#' and indentsize(lines[end]) == indent:
868 comments[:0] = [comment]
869 end = end - 1
870 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000871 comment = lines[end].expandtabs().lstrip()
872 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000873 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000874 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000875 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000876 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000877
Tim Peters4efb6e92001-06-29 23:51:08 +0000878class EndOfBlock(Exception): pass
879
880class BlockFinder:
881 """Provide a tokeneater() method to detect the end of a code block."""
882 def __init__(self):
883 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000884 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000885 self.started = False
886 self.passline = False
Meador Inge5b718d72015-07-23 22:49:37 -0500887 self.indecorator = False
888 self.decoratorhasargs = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000889 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000890
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000891 def tokeneater(self, type, token, srowcol, erowcol, line):
Meador Inge5b718d72015-07-23 22:49:37 -0500892 if not self.started and not self.indecorator:
893 # skip any decorators
894 if token == "@":
895 self.indecorator = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000896 # look for the first "def", "class" or "lambda"
Meador Inge5b718d72015-07-23 22:49:37 -0500897 elif token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000898 if token == "lambda":
899 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000900 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000901 self.passline = True # skip to the end of the line
Meador Inge5b718d72015-07-23 22:49:37 -0500902 elif token == "(":
903 if self.indecorator:
904 self.decoratorhasargs = True
905 elif token == ")":
906 if self.indecorator:
907 self.indecorator = False
908 self.decoratorhasargs = False
Tim Peters4efb6e92001-06-29 23:51:08 +0000909 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000910 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000911 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000912 if self.islambda: # lambdas always end at the first NEWLINE
913 raise EndOfBlock
Meador Inge5b718d72015-07-23 22:49:37 -0500914 # hitting a NEWLINE when in a decorator without args
915 # ends the decorator
916 if self.indecorator and not self.decoratorhasargs:
917 self.indecorator = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000918 elif self.passline:
919 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000920 elif type == tokenize.INDENT:
921 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000922 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000923 elif type == tokenize.DEDENT:
924 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000925 # the end of matching indent/dedent pairs end a block
926 # (note that this only works for "def"/"class" blocks,
927 # not e.g. for "if: else:" or "try: finally:" blocks)
928 if self.indent <= 0:
929 raise EndOfBlock
930 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
931 # any other token on the same indentation level end the previous
932 # block as well, except the pseudo-tokens COMMENT and NL.
933 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000934
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000935def getblock(lines):
936 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000937 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000938 try:
Trent Nelson428de652008-03-18 22:41:35 +0000939 tokens = tokenize.generate_tokens(iter(lines).__next__)
940 for _token in tokens:
941 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000942 except (EndOfBlock, IndentationError):
943 pass
944 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000945
946def getsourcelines(object):
947 """Return a list of source lines and starting line number for an object.
948
949 The argument may be a module, class, method, function, traceback, frame,
950 or code object. The source code is returned as a list of the lines
951 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200952 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000953 raised if the source code cannot be retrieved."""
Yury Selivanov081bbf62014-09-26 17:34:54 -0400954 object = unwrap(object)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000955 lines, lnum = findsource(object)
956
Vladimir Matveev91cb2982018-08-24 07:18:00 -0700957 if istraceback(object):
958 object = object.tb_frame
959
960 # for module or frame that corresponds to module, return all source lines
961 if (ismodule(object) or
962 (isframe(object) and object.f_code.co_name == "<module>")):
Meador Inge5b718d72015-07-23 22:49:37 -0500963 return lines, 0
964 else:
965 return getblock(lines[lnum:]), lnum + 1
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000966
967def getsource(object):
968 """Return the text of the source code for an object.
969
970 The argument may be a module, class, method, function, traceback, frame,
971 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200972 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000973 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000974 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000975
976# --------------------------------------------------- class tree extraction
977def walktree(classes, children, parent):
978 """Recursive helper function for getclasstree()."""
979 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +0000980 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000981 for c in classes:
982 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +0000983 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000984 results.append(walktree(children[c], children, c))
985 return results
986
Georg Brandl5ce83a02009-06-01 17:23:51 +0000987def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000988 """Arrange the given list of classes into a hierarchy of nested lists.
989
990 Where a nested list appears, it contains classes derived from the class
991 whose entry immediately precedes the list. Each entry is a 2-tuple
992 containing a class and a tuple of its base classes. If the 'unique'
993 argument is true, exactly one entry appears in the returned structure
994 for each class in the given list. Otherwise, classes using multiple
995 inheritance and their descendants will appear multiple times."""
996 children = {}
997 roots = []
998 for c in classes:
999 if c.__bases__:
1000 for parent in c.__bases__:
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05301001 if parent not in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001002 children[parent] = []
Serhiy Storchaka362c1b52013-09-05 17:14:32 +03001003 if c not in children[parent]:
1004 children[parent].append(c)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001005 if unique and parent in classes: break
1006 elif c not in roots:
1007 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +00001008 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001009 if parent not in classes:
1010 roots.append(parent)
1011 return walktree(roots, children, None)
1012
1013# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001014Arguments = namedtuple('Arguments', 'args, varargs, varkw')
1015
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001016def getargs(co):
1017 """Get information about the arguments accepted by a code object.
1018
Guido van Rossum2e65f892007-02-28 22:03:49 +00001019 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001020 'args' is the list of argument names. Keyword-only arguments are
1021 appended. 'varargs' and 'varkw' are the names of the * and **
1022 arguments or None."""
Guido van Rossum2e65f892007-02-28 22:03:49 +00001023 args, varargs, kwonlyargs, varkw = _getfullargs(co)
Christian Heimes25bb7832008-01-11 16:17:00 +00001024 return Arguments(args + kwonlyargs, varargs, varkw)
Guido van Rossum2e65f892007-02-28 22:03:49 +00001025
1026def _getfullargs(co):
1027 """Get information about the arguments accepted by a code object.
1028
1029 Four things are returned: (args, varargs, kwonlyargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001030 'args' and 'kwonlyargs' are lists of argument names, and 'varargs'
1031 and 'varkw' are the names of the * and ** arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +00001032
1033 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001034 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001035
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001036 nargs = co.co_argcount
1037 names = co.co_varnames
Guido van Rossum2e65f892007-02-28 22:03:49 +00001038 nkwargs = co.co_kwonlyargcount
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001039 args = list(names[:nargs])
Guido van Rossum2e65f892007-02-28 22:03:49 +00001040 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001041 step = 0
1042
Guido van Rossum2e65f892007-02-28 22:03:49 +00001043 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001044 varargs = None
1045 if co.co_flags & CO_VARARGS:
1046 varargs = co.co_varnames[nargs]
1047 nargs = nargs + 1
1048 varkw = None
1049 if co.co_flags & CO_VARKEYWORDS:
1050 varkw = co.co_varnames[nargs]
Guido van Rossum2e65f892007-02-28 22:03:49 +00001051 return args, varargs, kwonlyargs, varkw
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001052
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001053
1054ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
1055
1056def getargspec(func):
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001057 """Get the names and default values of a function's parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001058
1059 A tuple of four things is returned: (args, varargs, keywords, defaults).
1060 'args' is a list of the argument names, including keyword-only argument names.
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001061 'varargs' and 'keywords' are the names of the * and ** parameters or None.
1062 'defaults' is an n-tuple of the default values of the last n parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001063
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001064 This function is deprecated, as it does not support annotations or
1065 keyword-only parameters and will raise ValueError if either is present
1066 on the supplied callable.
1067
1068 For a more structured introspection API, use inspect.signature() instead.
1069
1070 Alternatively, use getfullargspec() for an API with a similar namedtuple
1071 based interface, but full support for annotations and keyword-only
1072 parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001073 """
1074 warnings.warn("inspect.getargspec() is deprecated, "
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001075 "use inspect.signature() or inspect.getfullargspec()",
1076 DeprecationWarning, stacklevel=2)
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001077 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
1078 getfullargspec(func)
1079 if kwonlyargs or ann:
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001080 raise ValueError("Function has keyword-only parameters or annotations"
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001081 ", use getfullargspec() API which can support them")
1082 return ArgSpec(args, varargs, varkw, defaults)
1083
Christian Heimes25bb7832008-01-11 16:17:00 +00001084FullArgSpec = namedtuple('FullArgSpec',
Benjamin Peterson3d4ca742008-11-12 21:39:01 +00001085 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001086
1087def getfullargspec(func):
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001088 """Get the names and default values of a callable object's parameters.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001089
Brett Cannon504d8852007-09-07 02:12:14 +00001090 A tuple of seven things is returned:
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001091 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations).
1092 'args' is a list of the parameter names.
1093 'varargs' and 'varkw' are the names of the * and ** parameters or None.
1094 'defaults' is an n-tuple of the default values of the last n parameters.
1095 'kwonlyargs' is a list of keyword-only parameter names.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001096 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001097 'annotations' is a dictionary mapping parameter names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001098
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001099 Notable differences from inspect.signature():
1100 - the "self" parameter is always reported, even for bound methods
1101 - wrapper chains defined by __wrapped__ *not* unwrapped automatically
Jeremy Hylton64967882003-06-27 18:14:39 +00001102 """
1103
Yury Selivanov57d240e2014-02-19 16:27:23 -05001104 try:
1105 # Re: `skip_bound_arg=False`
1106 #
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001107 # There is a notable difference in behaviour between getfullargspec
1108 # and Signature: the former always returns 'self' parameter for bound
1109 # methods, whereas the Signature always shows the actual calling
1110 # signature of the passed object.
1111 #
1112 # To simulate this behaviour, we "unbind" bound methods, to trick
1113 # inspect.signature to always return their first parameter ("self",
1114 # usually)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001115
Yury Selivanov57d240e2014-02-19 16:27:23 -05001116 # Re: `follow_wrapper_chains=False`
1117 #
1118 # getfullargspec() historically ignored __wrapped__ attributes,
1119 # so we ensure that remains the case in 3.3+
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001120
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001121 sig = _signature_from_callable(func,
1122 follow_wrapper_chains=False,
1123 skip_bound_arg=False,
1124 sigcls=Signature)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001125 except Exception as ex:
1126 # Most of the times 'signature' will raise ValueError.
1127 # But, it can also raise AttributeError, and, maybe something
1128 # else. So to be fully backwards compatible, we catch all
1129 # possible exceptions here, and reraise a TypeError.
1130 raise TypeError('unsupported callable') from ex
1131
1132 args = []
1133 varargs = None
1134 varkw = None
1135 kwonlyargs = []
1136 defaults = ()
1137 annotations = {}
1138 defaults = ()
1139 kwdefaults = {}
1140
1141 if sig.return_annotation is not sig.empty:
1142 annotations['return'] = sig.return_annotation
1143
1144 for param in sig.parameters.values():
1145 kind = param.kind
1146 name = param.name
1147
1148 if kind is _POSITIONAL_ONLY:
1149 args.append(name)
1150 elif kind is _POSITIONAL_OR_KEYWORD:
1151 args.append(name)
1152 if param.default is not param.empty:
1153 defaults += (param.default,)
1154 elif kind is _VAR_POSITIONAL:
1155 varargs = name
1156 elif kind is _KEYWORD_ONLY:
1157 kwonlyargs.append(name)
1158 if param.default is not param.empty:
1159 kwdefaults[name] = param.default
1160 elif kind is _VAR_KEYWORD:
1161 varkw = name
1162
1163 if param.annotation is not param.empty:
1164 annotations[name] = param.annotation
1165
1166 if not kwdefaults:
1167 # compatibility with 'func.__kwdefaults__'
1168 kwdefaults = None
1169
1170 if not defaults:
1171 # compatibility with 'func.__defaults__'
1172 defaults = None
1173
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001174 return FullArgSpec(args, varargs, varkw, defaults,
1175 kwonlyargs, kwdefaults, annotations)
1176
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001177
Christian Heimes25bb7832008-01-11 16:17:00 +00001178ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
1179
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001180def getargvalues(frame):
1181 """Get information about arguments passed into a particular frame.
1182
1183 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001184 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001185 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1186 'locals' is the locals dictionary of the given frame."""
1187 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +00001188 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001189
Guido van Rossum2e65f892007-02-28 22:03:49 +00001190def formatannotation(annotation, base_module=None):
Guido van Rossum52e50042016-10-22 07:55:18 -07001191 if getattr(annotation, '__module__', None) == 'typing':
1192 return repr(annotation).replace('typing.', '')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001193 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001194 if annotation.__module__ in ('builtins', base_module):
Serhiy Storchaka521e5862014-07-22 15:00:37 +03001195 return annotation.__qualname__
1196 return annotation.__module__+'.'+annotation.__qualname__
Guido van Rossum2e65f892007-02-28 22:03:49 +00001197 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001198
Guido van Rossum2e65f892007-02-28 22:03:49 +00001199def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001200 module = getattr(object, '__module__', None)
1201 def _formatannotation(annotation):
1202 return formatannotation(annotation, module)
1203 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001204
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001205def formatargspec(args, varargs=None, varkw=None, defaults=None,
Guido van Rossum2e65f892007-02-28 22:03:49 +00001206 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001207 formatarg=str,
1208 formatvarargs=lambda name: '*' + name,
1209 formatvarkw=lambda name: '**' + name,
1210 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001211 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001212 formatannotation=formatannotation):
Berker Peksagfa3922c2015-07-31 04:11:29 +03001213 """Format an argument spec from the values returned by getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001214
Guido van Rossum2e65f892007-02-28 22:03:49 +00001215 The first seven arguments are (args, varargs, varkw, defaults,
1216 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1217 are the corresponding optional formatting functions that are called to
1218 turn names and values into strings. The last argument is an optional
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +02001219 function to format the sequence of arguments.
1220
1221 Deprecated since Python 3.5: use the `signature` function and `Signature`
1222 objects.
1223 """
1224
1225 from warnings import warn
1226
1227 warn("`formatargspec` is deprecated since Python 3.5. Use `signature` and "
Zackery Spytz41254eb2018-06-11 21:16:18 -06001228 "the `Signature` object directly",
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +02001229 DeprecationWarning,
1230 stacklevel=2)
1231
Guido van Rossum2e65f892007-02-28 22:03:49 +00001232 def formatargandannotation(arg):
1233 result = formatarg(arg)
1234 if arg in annotations:
1235 result += ': ' + formatannotation(annotations[arg])
1236 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001237 specs = []
1238 if defaults:
1239 firstdefault = len(args) - len(defaults)
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001240 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001241 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001242 if defaults and i >= firstdefault:
1243 spec = spec + formatvalue(defaults[i - firstdefault])
1244 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001245 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001246 specs.append(formatvarargs(formatargandannotation(varargs)))
1247 else:
1248 if kwonlyargs:
1249 specs.append('*')
1250 if kwonlyargs:
1251 for kwonlyarg in kwonlyargs:
1252 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001253 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001254 spec += formatvalue(kwonlydefaults[kwonlyarg])
1255 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001256 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001257 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001258 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001259 if 'return' in annotations:
1260 result += formatreturns(formatannotation(annotations['return']))
1261 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001262
1263def formatargvalues(args, varargs, varkw, locals,
1264 formatarg=str,
1265 formatvarargs=lambda name: '*' + name,
1266 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001267 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001268 """Format an argument spec from the 4 values returned by getargvalues.
1269
1270 The first four arguments are (args, varargs, varkw, locals). The
1271 next four arguments are the corresponding optional formatting functions
1272 that are called to turn names and values into strings. The ninth
1273 argument is an optional function to format the sequence of arguments."""
1274 def convert(name, locals=locals,
1275 formatarg=formatarg, formatvalue=formatvalue):
1276 return formatarg(name) + formatvalue(locals[name])
1277 specs = []
1278 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001279 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001280 if varargs:
1281 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1282 if varkw:
1283 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001284 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001285
Benjamin Petersone109c702011-06-24 09:37:26 -05001286def _missing_arguments(f_name, argnames, pos, values):
1287 names = [repr(name) for name in argnames if name not in values]
1288 missing = len(names)
1289 if missing == 1:
1290 s = names[0]
1291 elif missing == 2:
1292 s = "{} and {}".format(*names)
1293 else:
Yury Selivanovdccfa132014-03-27 18:42:52 -04001294 tail = ", {} and {}".format(*names[-2:])
Benjamin Petersone109c702011-06-24 09:37:26 -05001295 del names[-2:]
1296 s = ", ".join(names) + tail
1297 raise TypeError("%s() missing %i required %s argument%s: %s" %
1298 (f_name, missing,
1299 "positional" if pos else "keyword-only",
1300 "" if missing == 1 else "s", s))
1301
1302def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001303 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001304 kwonly_given = len([arg for arg in kwonly if arg in values])
1305 if varargs:
1306 plural = atleast != 1
1307 sig = "at least %d" % (atleast,)
1308 elif defcount:
1309 plural = True
1310 sig = "from %d to %d" % (atleast, len(args))
1311 else:
1312 plural = len(args) != 1
1313 sig = str(len(args))
1314 kwonly_sig = ""
1315 if kwonly_given:
1316 msg = " positional argument%s (and %d keyword-only argument%s)"
1317 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1318 "s" if kwonly_given != 1 else ""))
1319 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1320 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1321 "was" if given == 1 and not kwonly_given else "were"))
1322
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001323def getcallargs(*func_and_positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001324 """Get the mapping of arguments to values.
1325
1326 A dict is returned, with keys the function argument names (including the
1327 names of the * and ** arguments, if any), and values the respective bound
1328 values from 'positional' and 'named'."""
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001329 func = func_and_positional[0]
1330 positional = func_and_positional[1:]
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001331 spec = getfullargspec(func)
1332 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
1333 f_name = func.__name__
1334 arg2value = {}
1335
Benjamin Petersonb204a422011-06-05 22:04:07 -05001336
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001337 if ismethod(func) and func.__self__ is not None:
1338 # implicit 'self' (or 'cls' for classmethods) argument
1339 positional = (func.__self__,) + positional
1340 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001341 num_args = len(args)
1342 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001343
Benjamin Petersonb204a422011-06-05 22:04:07 -05001344 n = min(num_pos, num_args)
1345 for i in range(n):
1346 arg2value[args[i]] = positional[i]
1347 if varargs:
1348 arg2value[varargs] = tuple(positional[n:])
1349 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001350 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001351 arg2value[varkw] = {}
1352 for kw, value in named.items():
1353 if kw not in possible_kwargs:
1354 if not varkw:
1355 raise TypeError("%s() got an unexpected keyword argument %r" %
1356 (f_name, kw))
1357 arg2value[varkw][kw] = value
1358 continue
1359 if kw in arg2value:
1360 raise TypeError("%s() got multiple values for argument %r" %
1361 (f_name, kw))
1362 arg2value[kw] = value
1363 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001364 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1365 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001366 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001367 req = args[:num_args - num_defaults]
1368 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001369 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001370 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001371 for i, arg in enumerate(args[num_args - num_defaults:]):
1372 if arg not in arg2value:
1373 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001374 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001375 for kwarg in kwonlyargs:
1376 if kwarg not in arg2value:
Yury Selivanov875df202014-03-27 18:23:03 -04001377 if kwonlydefaults and kwarg in kwonlydefaults:
Benjamin Petersone109c702011-06-24 09:37:26 -05001378 arg2value[kwarg] = kwonlydefaults[kwarg]
1379 else:
1380 missing += 1
1381 if missing:
1382 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001383 return arg2value
1384
Nick Coghlan2f92e542012-06-23 19:39:55 +10001385ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1386
1387def getclosurevars(func):
1388 """
1389 Get the mapping of free variables to their current values.
1390
Meador Inge8fda3592012-07-19 21:33:21 -05001391 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001392 and builtin references as seen by the body of the function. A final
1393 set of unbound names that could not be resolved is also provided.
1394 """
1395
1396 if ismethod(func):
1397 func = func.__func__
1398
1399 if not isfunction(func):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001400 raise TypeError("{!r} is not a Python function".format(func))
Nick Coghlan2f92e542012-06-23 19:39:55 +10001401
1402 code = func.__code__
1403 # Nonlocal references are named in co_freevars and resolved
1404 # by looking them up in __closure__ by positional index
1405 if func.__closure__ is None:
1406 nonlocal_vars = {}
1407 else:
1408 nonlocal_vars = {
1409 var : cell.cell_contents
1410 for var, cell in zip(code.co_freevars, func.__closure__)
1411 }
1412
1413 # Global and builtin references are named in co_names and resolved
1414 # by looking them up in __globals__ or __builtins__
1415 global_ns = func.__globals__
1416 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1417 if ismodule(builtin_ns):
1418 builtin_ns = builtin_ns.__dict__
1419 global_vars = {}
1420 builtin_vars = {}
1421 unbound_names = set()
1422 for name in code.co_names:
1423 if name in ("None", "True", "False"):
1424 # Because these used to be builtins instead of keywords, they
1425 # may still show up as name references. We ignore them.
1426 continue
1427 try:
1428 global_vars[name] = global_ns[name]
1429 except KeyError:
1430 try:
1431 builtin_vars[name] = builtin_ns[name]
1432 except KeyError:
1433 unbound_names.add(name)
1434
1435 return ClosureVars(nonlocal_vars, global_vars,
1436 builtin_vars, unbound_names)
1437
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001438# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001439
1440Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1441
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001442def getframeinfo(frame, context=1):
1443 """Get information about a frame or traceback object.
1444
1445 A tuple of five things is returned: the filename, the line number of
1446 the current line, the function name, a list of lines of context from
1447 the source code, and the index of the current line within that list.
1448 The optional second argument specifies the number of lines of context
1449 to return, which are centered around the current line."""
1450 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001451 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001452 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001453 else:
1454 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001455 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001456 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001457
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001458 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001459 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001460 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001461 try:
1462 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001463 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001464 lines = index = None
1465 else:
Raymond Hettingera0501712004-06-15 11:22:53 +00001466 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001467 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001468 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001469 else:
1470 lines = index = None
1471
Christian Heimes25bb7832008-01-11 16:17:00 +00001472 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001473
1474def getlineno(frame):
1475 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001476 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1477 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001478
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001479FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
1480
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001481def getouterframes(frame, context=1):
1482 """Get a list of records for a frame and all higher (calling) frames.
1483
1484 Each record contains a frame object, filename, line number, function
1485 name, a list of lines of context, and index within the context."""
1486 framelist = []
1487 while frame:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001488 frameinfo = (frame,) + getframeinfo(frame, context)
1489 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001490 frame = frame.f_back
1491 return framelist
1492
1493def getinnerframes(tb, context=1):
1494 """Get a list of records for a traceback's frame and all lower frames.
1495
1496 Each record contains a frame object, filename, line number, function
1497 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001498 framelist = []
1499 while tb:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001500 frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
1501 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001502 tb = tb.tb_next
1503 return framelist
1504
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001505def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001506 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001507 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001508
1509def stack(context=1):
1510 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001511 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001512
1513def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001514 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001515 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001516
1517
1518# ------------------------------------------------ static version of getattr
1519
1520_sentinel = object()
1521
Michael Foorde5162652010-11-20 16:40:44 +00001522def _static_getmro(klass):
1523 return type.__dict__['__mro__'].__get__(klass)
1524
Michael Foord95fc51d2010-11-20 15:07:30 +00001525def _check_instance(obj, attr):
1526 instance_dict = {}
1527 try:
1528 instance_dict = object.__getattribute__(obj, "__dict__")
1529 except AttributeError:
1530 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001531 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001532
1533
1534def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001535 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001536 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001537 try:
1538 return entry.__dict__[attr]
1539 except KeyError:
1540 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001541 return _sentinel
1542
Michael Foord35184ed2010-11-20 16:58:30 +00001543def _is_type(obj):
1544 try:
1545 _static_getmro(obj)
1546 except TypeError:
1547 return False
1548 return True
1549
Michael Foorddcebe0f2011-03-15 19:20:44 -04001550def _shadowed_dict(klass):
1551 dict_attr = type.__dict__["__dict__"]
1552 for entry in _static_getmro(klass):
1553 try:
1554 class_dict = dict_attr.__get__(entry)["__dict__"]
1555 except KeyError:
1556 pass
1557 else:
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05301558 if not (isinstance(class_dict, types.GetSetDescriptorType) and
Michael Foorddcebe0f2011-03-15 19:20:44 -04001559 class_dict.__name__ == "__dict__" and
1560 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001561 return class_dict
1562 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001563
1564def getattr_static(obj, attr, default=_sentinel):
1565 """Retrieve attributes without triggering dynamic lookup via the
1566 descriptor protocol, __getattr__ or __getattribute__.
1567
1568 Note: this function may not be able to retrieve all attributes
1569 that getattr can fetch (like dynamically created attributes)
1570 and may find attributes that getattr can't (like descriptors
1571 that raise AttributeError). It can also return descriptor objects
1572 instead of instance members in some cases. See the
1573 documentation for details.
1574 """
1575 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001576 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001577 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001578 dict_attr = _shadowed_dict(klass)
1579 if (dict_attr is _sentinel or
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05301580 isinstance(dict_attr, types.MemberDescriptorType)):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001581 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001582 else:
1583 klass = obj
1584
1585 klass_result = _check_class(klass, attr)
1586
1587 if instance_result is not _sentinel and klass_result is not _sentinel:
1588 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1589 _check_class(type(klass_result), '__set__') is not _sentinel):
1590 return klass_result
1591
1592 if instance_result is not _sentinel:
1593 return instance_result
1594 if klass_result is not _sentinel:
1595 return klass_result
1596
1597 if obj is klass:
1598 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001599 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001600 if _shadowed_dict(type(entry)) is _sentinel:
1601 try:
1602 return entry.__dict__[attr]
1603 except KeyError:
1604 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001605 if default is not _sentinel:
1606 return default
1607 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001608
1609
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001610# ------------------------------------------------ generator introspection
1611
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001612GEN_CREATED = 'GEN_CREATED'
1613GEN_RUNNING = 'GEN_RUNNING'
1614GEN_SUSPENDED = 'GEN_SUSPENDED'
1615GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001616
1617def getgeneratorstate(generator):
1618 """Get current state of a generator-iterator.
1619
1620 Possible states are:
1621 GEN_CREATED: Waiting to start execution.
1622 GEN_RUNNING: Currently being executed by the interpreter.
1623 GEN_SUSPENDED: Currently suspended at a yield expression.
1624 GEN_CLOSED: Execution has completed.
1625 """
1626 if generator.gi_running:
1627 return GEN_RUNNING
1628 if generator.gi_frame is None:
1629 return GEN_CLOSED
1630 if generator.gi_frame.f_lasti == -1:
1631 return GEN_CREATED
1632 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001633
1634
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001635def getgeneratorlocals(generator):
1636 """
1637 Get the mapping of generator local variables to their current values.
1638
1639 A dict is returned, with the keys the local variable names and values the
1640 bound values."""
1641
1642 if not isgenerator(generator):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001643 raise TypeError("{!r} is not a Python generator".format(generator))
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001644
1645 frame = getattr(generator, "gi_frame", None)
1646 if frame is not None:
1647 return generator.gi_frame.f_locals
1648 else:
1649 return {}
1650
Yury Selivanov5376ba92015-06-22 12:19:30 -04001651
1652# ------------------------------------------------ coroutine introspection
1653
1654CORO_CREATED = 'CORO_CREATED'
1655CORO_RUNNING = 'CORO_RUNNING'
1656CORO_SUSPENDED = 'CORO_SUSPENDED'
1657CORO_CLOSED = 'CORO_CLOSED'
1658
1659def getcoroutinestate(coroutine):
1660 """Get current state of a coroutine object.
1661
1662 Possible states are:
1663 CORO_CREATED: Waiting to start execution.
1664 CORO_RUNNING: Currently being executed by the interpreter.
1665 CORO_SUSPENDED: Currently suspended at an await expression.
1666 CORO_CLOSED: Execution has completed.
1667 """
1668 if coroutine.cr_running:
1669 return CORO_RUNNING
1670 if coroutine.cr_frame is None:
1671 return CORO_CLOSED
1672 if coroutine.cr_frame.f_lasti == -1:
1673 return CORO_CREATED
1674 return CORO_SUSPENDED
1675
1676
1677def getcoroutinelocals(coroutine):
1678 """
1679 Get the mapping of coroutine local variables to their current values.
1680
1681 A dict is returned, with the keys the local variable names and values the
1682 bound values."""
1683 frame = getattr(coroutine, "cr_frame", None)
1684 if frame is not None:
1685 return frame.f_locals
1686 else:
1687 return {}
1688
1689
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001690###############################################################################
1691### Function Signature Object (PEP 362)
1692###############################################################################
1693
1694
1695_WrapperDescriptor = type(type.__call__)
1696_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001697_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001698
1699_NonUserDefinedCallables = (_WrapperDescriptor,
1700 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001701 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001702 types.BuiltinFunctionType)
1703
1704
Yury Selivanov421f0c72014-01-29 12:05:40 -05001705def _signature_get_user_defined_method(cls, method_name):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001706 """Private helper. Checks if ``cls`` has an attribute
1707 named ``method_name`` and returns it only if it is a
1708 pure python function.
1709 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001710 try:
1711 meth = getattr(cls, method_name)
1712 except AttributeError:
1713 return
1714 else:
1715 if not isinstance(meth, _NonUserDefinedCallables):
1716 # Once '__signature__' will be added to 'C'-level
1717 # callables, this check won't be necessary
1718 return meth
1719
1720
Yury Selivanov62560fb2014-01-28 12:26:24 -05001721def _signature_get_partial(wrapped_sig, partial, extra_args=()):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001722 """Private helper to calculate how 'wrapped_sig' signature will
1723 look like after applying a 'functools.partial' object (or alike)
1724 on it.
1725 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001726
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001727 old_params = wrapped_sig.parameters
1728 new_params = OrderedDict(old_params.items())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001729
1730 partial_args = partial.args or ()
1731 partial_keywords = partial.keywords or {}
1732
1733 if extra_args:
1734 partial_args = extra_args + partial_args
1735
1736 try:
1737 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1738 except TypeError as ex:
1739 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1740 raise ValueError(msg) from ex
1741
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001742
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001743 transform_to_kwonly = False
1744 for param_name, param in old_params.items():
1745 try:
1746 arg_value = ba.arguments[param_name]
1747 except KeyError:
1748 pass
1749 else:
1750 if param.kind is _POSITIONAL_ONLY:
1751 # If positional-only parameter is bound by partial,
1752 # it effectively disappears from the signature
1753 new_params.pop(param_name)
1754 continue
1755
1756 if param.kind is _POSITIONAL_OR_KEYWORD:
1757 if param_name in partial_keywords:
1758 # This means that this parameter, and all parameters
1759 # after it should be keyword-only (and var-positional
1760 # should be removed). Here's why. Consider the following
1761 # function:
1762 # foo(a, b, *args, c):
1763 # pass
1764 #
1765 # "partial(foo, a='spam')" will have the following
1766 # signature: "(*, a='spam', b, c)". Because attempting
1767 # to call that partial with "(10, 20)" arguments will
1768 # raise a TypeError, saying that "a" argument received
1769 # multiple values.
1770 transform_to_kwonly = True
1771 # Set the new default value
1772 new_params[param_name] = param.replace(default=arg_value)
1773 else:
1774 # was passed as a positional argument
1775 new_params.pop(param.name)
1776 continue
1777
1778 if param.kind is _KEYWORD_ONLY:
1779 # Set the new default value
1780 new_params[param_name] = param.replace(default=arg_value)
1781
1782 if transform_to_kwonly:
1783 assert param.kind is not _POSITIONAL_ONLY
1784
1785 if param.kind is _POSITIONAL_OR_KEYWORD:
1786 new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1787 new_params[param_name] = new_param
1788 new_params.move_to_end(param_name)
1789 elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
1790 new_params.move_to_end(param_name)
1791 elif param.kind is _VAR_POSITIONAL:
1792 new_params.pop(param.name)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001793
1794 return wrapped_sig.replace(parameters=new_params.values())
1795
1796
Yury Selivanov62560fb2014-01-28 12:26:24 -05001797def _signature_bound_method(sig):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001798 """Private helper to transform signatures for unbound
1799 functions to bound methods.
1800 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001801
1802 params = tuple(sig.parameters.values())
1803
1804 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1805 raise ValueError('invalid method signature')
1806
1807 kind = params[0].kind
1808 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1809 # Drop first parameter:
1810 # '(p1, p2[, ...])' -> '(p2[, ...])'
1811 params = params[1:]
1812 else:
1813 if kind is not _VAR_POSITIONAL:
1814 # Unless we add a new parameter type we never
1815 # get here
1816 raise ValueError('invalid argument type')
1817 # It's a var-positional parameter.
1818 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1819
1820 return sig.replace(parameters=params)
1821
1822
Yury Selivanovb77511d2014-01-29 10:46:14 -05001823def _signature_is_builtin(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001824 """Private helper to test if `obj` is a callable that might
1825 support Argument Clinic's __text_signature__ protocol.
1826 """
Yury Selivanov1d241832014-02-02 12:51:20 -05001827 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001828 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001829 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001830 # Can't test 'isinstance(type)' here, as it would
1831 # also be True for regular python classes
1832 obj in (type, object))
1833
1834
Yury Selivanov63da7c72014-01-31 14:48:37 -05001835def _signature_is_functionlike(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001836 """Private helper to test if `obj` is a duck type of FunctionType.
1837 A good example of such objects are functions compiled with
1838 Cython, which have all attributes that a pure Python function
1839 would have, but have their code statically compiled.
1840 """
Yury Selivanov63da7c72014-01-31 14:48:37 -05001841
1842 if not callable(obj) or isclass(obj):
1843 # All function-like objects are obviously callables,
1844 # and not classes.
1845 return False
1846
1847 name = getattr(obj, '__name__', None)
1848 code = getattr(obj, '__code__', None)
1849 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
1850 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
1851 annotations = getattr(obj, '__annotations__', None)
1852
1853 return (isinstance(code, types.CodeType) and
1854 isinstance(name, str) and
1855 (defaults is None or isinstance(defaults, tuple)) and
1856 (kwdefaults is None or isinstance(kwdefaults, dict)) and
1857 isinstance(annotations, dict))
1858
1859
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001860def _signature_get_bound_param(spec):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001861 """ Private helper to get first parameter name from a
1862 __text_signature__ of a builtin method, which should
1863 be in the following format: '($param1, ...)'.
1864 Assumptions are that the first argument won't have
1865 a default value or an annotation.
1866 """
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001867
1868 assert spec.startswith('($')
1869
1870 pos = spec.find(',')
1871 if pos == -1:
1872 pos = spec.find(')')
1873
1874 cpos = spec.find(':')
1875 assert cpos == -1 or cpos > pos
1876
1877 cpos = spec.find('=')
1878 assert cpos == -1 or cpos > pos
1879
1880 return spec[2:pos]
1881
1882
Larry Hastings2623c8c2014-02-08 22:15:29 -08001883def _signature_strip_non_python_syntax(signature):
1884 """
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001885 Private helper function. Takes a signature in Argument Clinic's
1886 extended signature format.
1887
Larry Hastings2623c8c2014-02-08 22:15:29 -08001888 Returns a tuple of three things:
1889 * that signature re-rendered in standard Python syntax,
1890 * the index of the "self" parameter (generally 0), or None if
1891 the function does not have a "self" parameter, and
1892 * the index of the last "positional only" parameter,
1893 or None if the signature has no positional-only parameters.
1894 """
1895
1896 if not signature:
1897 return signature, None, None
1898
1899 self_parameter = None
1900 last_positional_only = None
1901
1902 lines = [l.encode('ascii') for l in signature.split('\n')]
1903 generator = iter(lines).__next__
1904 token_stream = tokenize.tokenize(generator)
1905
1906 delayed_comma = False
1907 skip_next_comma = False
1908 text = []
1909 add = text.append
1910
1911 current_parameter = 0
1912 OP = token.OP
1913 ERRORTOKEN = token.ERRORTOKEN
1914
1915 # token stream always starts with ENCODING token, skip it
1916 t = next(token_stream)
1917 assert t.type == tokenize.ENCODING
1918
1919 for t in token_stream:
1920 type, string = t.type, t.string
1921
1922 if type == OP:
1923 if string == ',':
1924 if skip_next_comma:
1925 skip_next_comma = False
1926 else:
1927 assert not delayed_comma
1928 delayed_comma = True
1929 current_parameter += 1
1930 continue
1931
1932 if string == '/':
1933 assert not skip_next_comma
1934 assert last_positional_only is None
1935 skip_next_comma = True
1936 last_positional_only = current_parameter - 1
1937 continue
1938
1939 if (type == ERRORTOKEN) and (string == '$'):
1940 assert self_parameter is None
1941 self_parameter = current_parameter
1942 continue
1943
1944 if delayed_comma:
1945 delayed_comma = False
1946 if not ((type == OP) and (string == ')')):
1947 add(', ')
1948 add(string)
1949 if (string == ','):
1950 add(' ')
1951 clean_signature = ''.join(text)
1952 return clean_signature, self_parameter, last_positional_only
1953
1954
Yury Selivanov57d240e2014-02-19 16:27:23 -05001955def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001956 """Private helper to parse content of '__text_signature__'
1957 and return a Signature based on it.
1958 """
INADA Naoki37420de2018-01-27 10:10:06 +09001959 # Lazy import ast because it's relatively heavy and
1960 # it's not used for other than this function.
1961 import ast
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001962
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001963 Parameter = cls._parameter_cls
1964
Larry Hastings2623c8c2014-02-08 22:15:29 -08001965 clean_signature, self_parameter, last_positional_only = \
1966 _signature_strip_non_python_syntax(s)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001967
Larry Hastings2623c8c2014-02-08 22:15:29 -08001968 program = "def foo" + clean_signature + ": pass"
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001969
1970 try:
Larry Hastings2623c8c2014-02-08 22:15:29 -08001971 module = ast.parse(program)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001972 except SyntaxError:
1973 module = None
1974
1975 if not isinstance(module, ast.Module):
1976 raise ValueError("{!r} builtin has invalid signature".format(obj))
1977
1978 f = module.body[0]
1979
1980 parameters = []
1981 empty = Parameter.empty
1982 invalid = object()
1983
1984 module = None
1985 module_dict = {}
1986 module_name = getattr(obj, '__module__', None)
1987 if module_name:
1988 module = sys.modules.get(module_name, None)
1989 if module:
1990 module_dict = module.__dict__
1991 sys_module_dict = sys.modules
1992
1993 def parse_name(node):
1994 assert isinstance(node, ast.arg)
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05301995 if node.annotation is not None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001996 raise ValueError("Annotations are not currently supported")
1997 return node.arg
1998
1999 def wrap_value(s):
2000 try:
2001 value = eval(s, module_dict)
2002 except NameError:
2003 try:
2004 value = eval(s, sys_module_dict)
2005 except NameError:
2006 raise RuntimeError()
2007
2008 if isinstance(value, str):
2009 return ast.Str(value)
2010 if isinstance(value, (int, float)):
2011 return ast.Num(value)
2012 if isinstance(value, bytes):
2013 return ast.Bytes(value)
2014 if value in (True, False, None):
2015 return ast.NameConstant(value)
2016 raise RuntimeError()
2017
2018 class RewriteSymbolics(ast.NodeTransformer):
2019 def visit_Attribute(self, node):
2020 a = []
2021 n = node
2022 while isinstance(n, ast.Attribute):
2023 a.append(n.attr)
2024 n = n.value
2025 if not isinstance(n, ast.Name):
2026 raise RuntimeError()
2027 a.append(n.id)
2028 value = ".".join(reversed(a))
2029 return wrap_value(value)
2030
2031 def visit_Name(self, node):
2032 if not isinstance(node.ctx, ast.Load):
2033 raise ValueError()
2034 return wrap_value(node.id)
2035
2036 def p(name_node, default_node, default=empty):
2037 name = parse_name(name_node)
2038 if name is invalid:
2039 return None
2040 if default_node and default_node is not _empty:
2041 try:
2042 default_node = RewriteSymbolics().visit(default_node)
2043 o = ast.literal_eval(default_node)
2044 except ValueError:
2045 o = invalid
2046 if o is invalid:
2047 return None
2048 default = o if o is not invalid else default
2049 parameters.append(Parameter(name, kind, default=default, annotation=empty))
2050
2051 # non-keyword-only parameters
2052 args = reversed(f.args.args)
2053 defaults = reversed(f.args.defaults)
2054 iter = itertools.zip_longest(args, defaults, fillvalue=None)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002055 if last_positional_only is not None:
2056 kind = Parameter.POSITIONAL_ONLY
2057 else:
2058 kind = Parameter.POSITIONAL_OR_KEYWORD
2059 for i, (name, default) in enumerate(reversed(list(iter))):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002060 p(name, default)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002061 if i == last_positional_only:
2062 kind = Parameter.POSITIONAL_OR_KEYWORD
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002063
2064 # *args
2065 if f.args.vararg:
2066 kind = Parameter.VAR_POSITIONAL
2067 p(f.args.vararg, empty)
2068
2069 # keyword-only arguments
2070 kind = Parameter.KEYWORD_ONLY
2071 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
2072 p(name, default)
2073
2074 # **kwargs
2075 if f.args.kwarg:
2076 kind = Parameter.VAR_KEYWORD
2077 p(f.args.kwarg, empty)
2078
Larry Hastings2623c8c2014-02-08 22:15:29 -08002079 if self_parameter is not None:
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002080 # Possibly strip the bound argument:
2081 # - We *always* strip first bound argument if
2082 # it is a module.
2083 # - We don't strip first bound argument if
2084 # skip_bound_arg is False.
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002085 assert parameters
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002086 _self = getattr(obj, '__self__', None)
2087 self_isbound = _self is not None
2088 self_ismodule = ismodule(_self)
2089 if self_isbound and (self_ismodule or skip_bound_arg):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002090 parameters.pop(0)
2091 else:
2092 # for builtins, self parameter is always positional-only!
2093 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
2094 parameters[0] = p
2095
2096 return cls(parameters, return_annotation=cls.empty)
2097
2098
Yury Selivanov57d240e2014-02-19 16:27:23 -05002099def _signature_from_builtin(cls, func, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002100 """Private helper function to get signature for
2101 builtin callables.
2102 """
2103
Yury Selivanov57d240e2014-02-19 16:27:23 -05002104 if not _signature_is_builtin(func):
2105 raise TypeError("{!r} is not a Python builtin "
2106 "function".format(func))
2107
2108 s = getattr(func, "__text_signature__", None)
2109 if not s:
2110 raise ValueError("no signature found for builtin {!r}".format(func))
2111
2112 return _signature_fromstr(cls, func, s, skip_bound_arg)
2113
2114
Yury Selivanovcf45f022015-05-20 14:38:50 -04002115def _signature_from_function(cls, func):
2116 """Private helper: constructs Signature for the given python function."""
2117
2118 is_duck_function = False
2119 if not isfunction(func):
2120 if _signature_is_functionlike(func):
2121 is_duck_function = True
2122 else:
2123 # If it's not a pure Python function, and not a duck type
2124 # of pure function:
2125 raise TypeError('{!r} is not a Python function'.format(func))
2126
2127 Parameter = cls._parameter_cls
2128
2129 # Parameter information.
2130 func_code = func.__code__
2131 pos_count = func_code.co_argcount
2132 arg_names = func_code.co_varnames
2133 positional = tuple(arg_names[:pos_count])
2134 keyword_only_count = func_code.co_kwonlyargcount
2135 keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)]
2136 annotations = func.__annotations__
2137 defaults = func.__defaults__
2138 kwdefaults = func.__kwdefaults__
2139
2140 if defaults:
2141 pos_default_count = len(defaults)
2142 else:
2143 pos_default_count = 0
2144
2145 parameters = []
2146
2147 # Non-keyword-only parameters w/o defaults.
2148 non_default_count = pos_count - pos_default_count
2149 for name in positional[:non_default_count]:
2150 annotation = annotations.get(name, _empty)
2151 parameters.append(Parameter(name, annotation=annotation,
2152 kind=_POSITIONAL_OR_KEYWORD))
2153
2154 # ... w/ defaults.
2155 for offset, name in enumerate(positional[non_default_count:]):
2156 annotation = annotations.get(name, _empty)
2157 parameters.append(Parameter(name, annotation=annotation,
2158 kind=_POSITIONAL_OR_KEYWORD,
2159 default=defaults[offset]))
2160
2161 # *args
2162 if func_code.co_flags & CO_VARARGS:
2163 name = arg_names[pos_count + keyword_only_count]
2164 annotation = annotations.get(name, _empty)
2165 parameters.append(Parameter(name, annotation=annotation,
2166 kind=_VAR_POSITIONAL))
2167
2168 # Keyword-only parameters.
2169 for name in keyword_only:
2170 default = _empty
2171 if kwdefaults is not None:
2172 default = kwdefaults.get(name, _empty)
2173
2174 annotation = annotations.get(name, _empty)
2175 parameters.append(Parameter(name, annotation=annotation,
2176 kind=_KEYWORD_ONLY,
2177 default=default))
2178 # **kwargs
2179 if func_code.co_flags & CO_VARKEYWORDS:
2180 index = pos_count + keyword_only_count
2181 if func_code.co_flags & CO_VARARGS:
2182 index += 1
2183
2184 name = arg_names[index]
2185 annotation = annotations.get(name, _empty)
2186 parameters.append(Parameter(name, annotation=annotation,
2187 kind=_VAR_KEYWORD))
2188
2189 # Is 'func' is a pure Python function - don't validate the
2190 # parameters list (for correct order and defaults), it should be OK.
2191 return cls(parameters,
2192 return_annotation=annotations.get('return', _empty),
2193 __validate_parameters__=is_duck_function)
2194
2195
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002196def _signature_from_callable(obj, *,
2197 follow_wrapper_chains=True,
2198 skip_bound_arg=True,
2199 sigcls):
2200
2201 """Private helper function to get signature for arbitrary
2202 callable objects.
2203 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002204
2205 if not callable(obj):
2206 raise TypeError('{!r} is not a callable object'.format(obj))
2207
2208 if isinstance(obj, types.MethodType):
2209 # In this case we skip the first parameter of the underlying
2210 # function (usually `self` or `cls`).
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002211 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002212 obj.__func__,
2213 follow_wrapper_chains=follow_wrapper_chains,
2214 skip_bound_arg=skip_bound_arg,
2215 sigcls=sigcls)
2216
Yury Selivanov57d240e2014-02-19 16:27:23 -05002217 if skip_bound_arg:
2218 return _signature_bound_method(sig)
2219 else:
2220 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002221
Nick Coghlane8c45d62013-07-28 20:00:01 +10002222 # Was this function wrapped by a decorator?
Yury Selivanov57d240e2014-02-19 16:27:23 -05002223 if follow_wrapper_chains:
2224 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
Yury Selivanov46c759d2015-05-27 21:56:53 -04002225 if isinstance(obj, types.MethodType):
2226 # If the unwrapped object is a *method*, we might want to
2227 # skip its first parameter (self).
2228 # See test_signature_wrapped_bound_method for details.
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002229 return _signature_from_callable(
Yury Selivanov46c759d2015-05-27 21:56:53 -04002230 obj,
2231 follow_wrapper_chains=follow_wrapper_chains,
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002232 skip_bound_arg=skip_bound_arg,
2233 sigcls=sigcls)
Nick Coghlane8c45d62013-07-28 20:00:01 +10002234
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002235 try:
2236 sig = obj.__signature__
2237 except AttributeError:
2238 pass
2239 else:
2240 if sig is not None:
Yury Selivanov42407ab2014-06-23 10:23:50 -07002241 if not isinstance(sig, Signature):
2242 raise TypeError(
2243 'unexpected object {!r} in __signature__ '
2244 'attribute'.format(sig))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002245 return sig
2246
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002247 try:
2248 partialmethod = obj._partialmethod
2249 except AttributeError:
2250 pass
2251 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05002252 if isinstance(partialmethod, functools.partialmethod):
2253 # Unbound partialmethod (see functools.partialmethod)
2254 # This means, that we need to calculate the signature
2255 # as if it's a regular partial object, but taking into
2256 # account that the first positional argument
2257 # (usually `self`, or `cls`) will not be passed
2258 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002259
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002260 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002261 partialmethod.func,
2262 follow_wrapper_chains=follow_wrapper_chains,
2263 skip_bound_arg=skip_bound_arg,
2264 sigcls=sigcls)
2265
Yury Selivanov0486f812014-01-29 12:18:59 -05002266 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanov0486f812014-01-29 12:18:59 -05002267 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
Dong-hee Na378d7062017-05-18 04:00:51 +09002268 if first_wrapped_param.kind is Parameter.VAR_POSITIONAL:
2269 # First argument of the wrapped callable is `*args`, as in
2270 # `partialmethod(lambda *args)`.
2271 return sig
2272 else:
2273 sig_params = tuple(sig.parameters.values())
Yury Selivanov8a387212018-03-06 12:59:45 -05002274 assert (not sig_params or
2275 first_wrapped_param is not sig_params[0])
Dong-hee Na378d7062017-05-18 04:00:51 +09002276 new_params = (first_wrapped_param,) + sig_params
2277 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002278
Yury Selivanov63da7c72014-01-31 14:48:37 -05002279 if isfunction(obj) or _signature_is_functionlike(obj):
2280 # If it's a pure Python function, or an object that is duck type
2281 # of a Python function (Cython functions, for instance), then:
Yury Selivanovcf45f022015-05-20 14:38:50 -04002282 return _signature_from_function(sigcls, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002283
Yury Selivanova773de02014-02-21 18:30:53 -05002284 if _signature_is_builtin(obj):
Yury Selivanovda396452014-03-27 12:09:24 -04002285 return _signature_from_builtin(sigcls, obj,
Yury Selivanova773de02014-02-21 18:30:53 -05002286 skip_bound_arg=skip_bound_arg)
2287
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002288 if isinstance(obj, functools.partial):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002289 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002290 obj.func,
2291 follow_wrapper_chains=follow_wrapper_chains,
2292 skip_bound_arg=skip_bound_arg,
2293 sigcls=sigcls)
Yury Selivanov62560fb2014-01-28 12:26:24 -05002294 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002295
2296 sig = None
2297 if isinstance(obj, type):
2298 # obj is a class or a metaclass
2299
2300 # First, let's see if it has an overloaded __call__ defined
2301 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05002302 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002303 if call is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002304 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002305 call,
2306 follow_wrapper_chains=follow_wrapper_chains,
2307 skip_bound_arg=skip_bound_arg,
2308 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002309 else:
2310 # Now we check if the 'obj' class has a '__new__' method
Yury Selivanov421f0c72014-01-29 12:05:40 -05002311 new = _signature_get_user_defined_method(obj, '__new__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002312 if new is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002313 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002314 new,
2315 follow_wrapper_chains=follow_wrapper_chains,
2316 skip_bound_arg=skip_bound_arg,
2317 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002318 else:
2319 # Finally, we should have at least __init__ implemented
Yury Selivanov421f0c72014-01-29 12:05:40 -05002320 init = _signature_get_user_defined_method(obj, '__init__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002321 if init is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002322 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002323 init,
2324 follow_wrapper_chains=follow_wrapper_chains,
2325 skip_bound_arg=skip_bound_arg,
2326 sigcls=sigcls)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002327
2328 if sig is None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002329 # At this point we know, that `obj` is a class, with no user-
2330 # defined '__init__', '__new__', or class-level '__call__'
2331
Larry Hastings2623c8c2014-02-08 22:15:29 -08002332 for base in obj.__mro__[:-1]:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002333 # Since '__text_signature__' is implemented as a
2334 # descriptor that extracts text signature from the
2335 # class docstring, if 'obj' is derived from a builtin
2336 # class, its own '__text_signature__' may be 'None'.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002337 # Therefore, we go through the MRO (except the last
2338 # class in there, which is 'object') to find the first
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002339 # class with non-empty text signature.
2340 try:
2341 text_sig = base.__text_signature__
2342 except AttributeError:
2343 pass
2344 else:
2345 if text_sig:
2346 # If 'obj' class has a __text_signature__ attribute:
2347 # return a signature based on it
Yury Selivanovda396452014-03-27 12:09:24 -04002348 return _signature_fromstr(sigcls, obj, text_sig)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002349
2350 # No '__text_signature__' was found for the 'obj' class.
2351 # Last option is to check if its '__init__' is
2352 # object.__init__ or type.__init__.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002353 if type not in obj.__mro__:
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002354 # We have a class (not metaclass), but no user-defined
2355 # __init__ or __new__ for it
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002356 if (obj.__init__ is object.__init__ and
2357 obj.__new__ is object.__new__):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002358 # Return a signature of 'object' builtin.
2359 return signature(object)
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002360 else:
2361 raise ValueError(
2362 'no signature found for builtin type {!r}'.format(obj))
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002363
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002364 elif not isinstance(obj, _NonUserDefinedCallables):
2365 # An object with __call__
2366 # We also check that the 'obj' is not an instance of
2367 # _WrapperDescriptor or _MethodWrapper to avoid
2368 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05002369 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002370 if call is not None:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002371 try:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002372 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002373 call,
2374 follow_wrapper_chains=follow_wrapper_chains,
2375 skip_bound_arg=skip_bound_arg,
2376 sigcls=sigcls)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002377 except ValueError as ex:
2378 msg = 'no signature found for {!r}'.format(obj)
2379 raise ValueError(msg) from ex
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002380
2381 if sig is not None:
2382 # For classes and objects we skip the first parameter of their
2383 # __call__, __new__, or __init__ methods
Yury Selivanov57d240e2014-02-19 16:27:23 -05002384 if skip_bound_arg:
2385 return _signature_bound_method(sig)
2386 else:
2387 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002388
2389 if isinstance(obj, types.BuiltinFunctionType):
2390 # Raise a nicer error message for builtins
2391 msg = 'no signature found for builtin function {!r}'.format(obj)
2392 raise ValueError(msg)
2393
2394 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2395
2396
2397class _void:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002398 """A private marker - used in Parameter & Signature."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002399
2400
2401class _empty:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002402 """Marker object for Signature.empty and Parameter.empty."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002403
2404
Yury Selivanov21e83a52014-03-27 11:23:13 -04002405class _ParameterKind(enum.IntEnum):
2406 POSITIONAL_ONLY = 0
2407 POSITIONAL_OR_KEYWORD = 1
2408 VAR_POSITIONAL = 2
2409 KEYWORD_ONLY = 3
2410 VAR_KEYWORD = 4
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002411
2412 def __str__(self):
Yury Selivanov21e83a52014-03-27 11:23:13 -04002413 return self._name_
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002414
Dong-hee Na4aa30062018-06-08 12:46:31 +09002415 @property
2416 def description(self):
2417 return _PARAM_NAME_MAPPING[self]
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002418
Yury Selivanov21e83a52014-03-27 11:23:13 -04002419_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
2420_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
2421_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
2422_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
2423_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002424
Dong-hee Naa9cab432018-05-30 00:04:08 +09002425_PARAM_NAME_MAPPING = {
2426 _POSITIONAL_ONLY: 'positional-only',
2427 _POSITIONAL_OR_KEYWORD: 'positional or keyword',
2428 _VAR_POSITIONAL: 'variadic positional',
2429 _KEYWORD_ONLY: 'keyword-only',
2430 _VAR_KEYWORD: 'variadic keyword'
2431}
2432
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002433
2434class Parameter:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002435 """Represents a parameter in a function signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002436
2437 Has the following public attributes:
2438
2439 * name : str
2440 The name of the parameter as a string.
2441 * default : object
2442 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002443 parameter has no default value, this attribute is set to
2444 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002445 * annotation
2446 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002447 parameter has no annotation, this attribute is set to
2448 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002449 * kind : str
2450 Describes how argument values are bound to the parameter.
2451 Possible values: `Parameter.POSITIONAL_ONLY`,
2452 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
2453 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002454 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002455
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002456 __slots__ = ('_name', '_kind', '_default', '_annotation')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002457
2458 POSITIONAL_ONLY = _POSITIONAL_ONLY
2459 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
2460 VAR_POSITIONAL = _VAR_POSITIONAL
2461 KEYWORD_ONLY = _KEYWORD_ONLY
2462 VAR_KEYWORD = _VAR_KEYWORD
2463
2464 empty = _empty
2465
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002466 def __init__(self, name, kind, *, default=_empty, annotation=_empty):
Dong-hee Naa9cab432018-05-30 00:04:08 +09002467 try:
2468 self._kind = _ParameterKind(kind)
2469 except ValueError:
2470 raise ValueError(f'value {kind!r} is not a valid Parameter.kind')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002471 if default is not _empty:
Dong-hee Naa9cab432018-05-30 00:04:08 +09002472 if self._kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2473 msg = '{} parameters cannot have default values'
Dong-hee Na4aa30062018-06-08 12:46:31 +09002474 msg = msg.format(self._kind.description)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002475 raise ValueError(msg)
2476 self._default = default
2477 self._annotation = annotation
2478
Yury Selivanov2393dca2014-01-27 15:07:58 -05002479 if name is _empty:
2480 raise ValueError('name is a required attribute for Parameter')
2481
2482 if not isinstance(name, str):
Dong-hee Naa9cab432018-05-30 00:04:08 +09002483 msg = 'name must be a str, not a {}'.format(type(name).__name__)
2484 raise TypeError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002485
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002486 if name[0] == '.' and name[1:].isdigit():
2487 # These are implicit arguments generated by comprehensions. In
2488 # order to provide a friendlier interface to users, we recast
2489 # their name as "implicitN" and treat them as positional-only.
2490 # See issue 19611.
Dong-hee Naa9cab432018-05-30 00:04:08 +09002491 if self._kind != _POSITIONAL_OR_KEYWORD:
2492 msg = (
2493 'implicit arguments must be passed as '
2494 'positional or keyword arguments, not {}'
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002495 )
Dong-hee Na4aa30062018-06-08 12:46:31 +09002496 msg = msg.format(self._kind.description)
Dong-hee Naa9cab432018-05-30 00:04:08 +09002497 raise ValueError(msg)
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002498 self._kind = _POSITIONAL_ONLY
2499 name = 'implicit{}'.format(name[1:])
2500
Yury Selivanov2393dca2014-01-27 15:07:58 -05002501 if not name.isidentifier():
2502 raise ValueError('{!r} is not a valid parameter name'.format(name))
2503
2504 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002505
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002506 def __reduce__(self):
2507 return (type(self),
2508 (self._name, self._kind),
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002509 {'_default': self._default,
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002510 '_annotation': self._annotation})
2511
2512 def __setstate__(self, state):
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002513 self._default = state['_default']
2514 self._annotation = state['_annotation']
2515
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002516 @property
2517 def name(self):
2518 return self._name
2519
2520 @property
2521 def default(self):
2522 return self._default
2523
2524 @property
2525 def annotation(self):
2526 return self._annotation
2527
2528 @property
2529 def kind(self):
2530 return self._kind
2531
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002532 def replace(self, *, name=_void, kind=_void,
2533 annotation=_void, default=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002534 """Creates a customized copy of the Parameter."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002535
2536 if name is _void:
2537 name = self._name
2538
2539 if kind is _void:
2540 kind = self._kind
2541
2542 if annotation is _void:
2543 annotation = self._annotation
2544
2545 if default is _void:
2546 default = self._default
2547
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002548 return type(self)(name, kind, default=default, annotation=annotation)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002549
2550 def __str__(self):
2551 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002552 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002553
2554 # Add annotation and default value
2555 if self._annotation is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002556 formatted = '{}: {}'.format(formatted,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002557 formatannotation(self._annotation))
2558
2559 if self._default is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002560 if self._annotation is not _empty:
2561 formatted = '{} = {}'.format(formatted, repr(self._default))
2562 else:
2563 formatted = '{}={}'.format(formatted, repr(self._default))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002564
2565 if kind == _VAR_POSITIONAL:
2566 formatted = '*' + formatted
2567 elif kind == _VAR_KEYWORD:
2568 formatted = '**' + formatted
2569
2570 return formatted
2571
2572 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002573 return '<{} "{}">'.format(self.__class__.__name__, self)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002574
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002575 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002576 return hash((self.name, self.kind, self.annotation, self.default))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002577
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002578 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002579 if self is other:
2580 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002581 if not isinstance(other, Parameter):
2582 return NotImplemented
2583 return (self._name == other._name and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002584 self._kind == other._kind and
2585 self._default == other._default and
2586 self._annotation == other._annotation)
2587
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002588
2589class BoundArguments:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002590 """Result of `Signature.bind` call. Holds the mapping of arguments
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002591 to the function's parameters.
2592
2593 Has the following public attributes:
2594
2595 * arguments : OrderedDict
2596 An ordered mutable mapping of parameters' names to arguments' values.
2597 Does not contain arguments' default values.
2598 * signature : Signature
2599 The Signature object that created this instance.
2600 * args : tuple
2601 Tuple of positional arguments values.
2602 * kwargs : dict
2603 Dict of keyword arguments values.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002604 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002605
Yury Selivanov6abe0322015-05-13 17:18:41 -04002606 __slots__ = ('arguments', '_signature', '__weakref__')
2607
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002608 def __init__(self, signature, arguments):
2609 self.arguments = arguments
2610 self._signature = signature
2611
2612 @property
2613 def signature(self):
2614 return self._signature
2615
2616 @property
2617 def args(self):
2618 args = []
2619 for param_name, param in self._signature.parameters.items():
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002620 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002621 break
2622
2623 try:
2624 arg = self.arguments[param_name]
2625 except KeyError:
2626 # We're done here. Other arguments
2627 # will be mapped in 'BoundArguments.kwargs'
2628 break
2629 else:
2630 if param.kind == _VAR_POSITIONAL:
2631 # *args
2632 args.extend(arg)
2633 else:
2634 # plain argument
2635 args.append(arg)
2636
2637 return tuple(args)
2638
2639 @property
2640 def kwargs(self):
2641 kwargs = {}
2642 kwargs_started = False
2643 for param_name, param in self._signature.parameters.items():
2644 if not kwargs_started:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002645 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002646 kwargs_started = True
2647 else:
2648 if param_name not in self.arguments:
2649 kwargs_started = True
2650 continue
2651
2652 if not kwargs_started:
2653 continue
2654
2655 try:
2656 arg = self.arguments[param_name]
2657 except KeyError:
2658 pass
2659 else:
2660 if param.kind == _VAR_KEYWORD:
2661 # **kwargs
2662 kwargs.update(arg)
2663 else:
2664 # plain keyword argument
2665 kwargs[param_name] = arg
2666
2667 return kwargs
2668
Yury Selivanovb907a512015-05-16 13:45:09 -04002669 def apply_defaults(self):
2670 """Set default values for missing arguments.
2671
2672 For variable-positional arguments (*args) the default is an
2673 empty tuple.
2674
2675 For variable-keyword arguments (**kwargs) the default is an
2676 empty dict.
2677 """
2678 arguments = self.arguments
Yury Selivanovb907a512015-05-16 13:45:09 -04002679 new_arguments = []
2680 for name, param in self._signature.parameters.items():
2681 try:
2682 new_arguments.append((name, arguments[name]))
2683 except KeyError:
2684 if param.default is not _empty:
2685 val = param.default
2686 elif param.kind is _VAR_POSITIONAL:
2687 val = ()
2688 elif param.kind is _VAR_KEYWORD:
2689 val = {}
2690 else:
2691 # This BoundArguments was likely produced by
2692 # Signature.bind_partial().
2693 continue
2694 new_arguments.append((name, val))
2695 self.arguments = OrderedDict(new_arguments)
2696
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002697 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002698 if self is other:
2699 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002700 if not isinstance(other, BoundArguments):
2701 return NotImplemented
2702 return (self.signature == other.signature and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002703 self.arguments == other.arguments)
2704
Yury Selivanov6abe0322015-05-13 17:18:41 -04002705 def __setstate__(self, state):
2706 self._signature = state['_signature']
2707 self.arguments = state['arguments']
2708
2709 def __getstate__(self):
2710 return {'_signature': self._signature, 'arguments': self.arguments}
2711
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002712 def __repr__(self):
2713 args = []
2714 for arg, value in self.arguments.items():
2715 args.append('{}={!r}'.format(arg, value))
Yury Selivanovf229bc52015-05-15 12:53:56 -04002716 return '<{} ({})>'.format(self.__class__.__name__, ', '.join(args))
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002717
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002718
2719class Signature:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002720 """A Signature object represents the overall signature of a function.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002721 It stores a Parameter object for each parameter accepted by the
2722 function, as well as information specific to the function itself.
2723
2724 A Signature object has the following public attributes and methods:
2725
2726 * parameters : OrderedDict
2727 An ordered mapping of parameters' names to the corresponding
2728 Parameter objects (keyword-only arguments are in the same order
2729 as listed in `code.co_varnames`).
2730 * return_annotation : object
2731 The annotation for the return type of the function if specified.
2732 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002733 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002734 * bind(*args, **kwargs) -> BoundArguments
2735 Creates a mapping from positional and keyword arguments to
2736 parameters.
2737 * bind_partial(*args, **kwargs) -> BoundArguments
2738 Creates a partial mapping from positional and keyword arguments
2739 to parameters (simulating 'functools.partial' behavior.)
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002740 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002741
2742 __slots__ = ('_return_annotation', '_parameters')
2743
2744 _parameter_cls = Parameter
2745 _bound_arguments_cls = BoundArguments
2746
2747 empty = _empty
2748
2749 def __init__(self, parameters=None, *, return_annotation=_empty,
2750 __validate_parameters__=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002751 """Constructs Signature from the given list of Parameter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002752 objects and 'return_annotation'. All arguments are optional.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002753 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002754
2755 if parameters is None:
2756 params = OrderedDict()
2757 else:
2758 if __validate_parameters__:
2759 params = OrderedDict()
2760 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002761 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002762
2763 for idx, param in enumerate(parameters):
2764 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002765 name = param.name
2766
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002767 if kind < top_kind:
Dong-hee Naa9cab432018-05-30 00:04:08 +09002768 msg = (
2769 'wrong parameter order: {} parameter before {} '
2770 'parameter'
2771 )
Dong-hee Na4aa30062018-06-08 12:46:31 +09002772 msg = msg.format(top_kind.description,
2773 kind.description)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002774 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002775 elif kind > top_kind:
2776 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002777 top_kind = kind
2778
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002779 if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
Yury Selivanov07a9e452014-01-29 10:58:16 -05002780 if param.default is _empty:
2781 if kind_defaults:
2782 # No default for this parameter, but the
2783 # previous parameter of the same kind had
2784 # a default
2785 msg = 'non-default argument follows default ' \
2786 'argument'
2787 raise ValueError(msg)
2788 else:
2789 # There is a default for this parameter.
2790 kind_defaults = True
2791
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002792 if name in params:
2793 msg = 'duplicate parameter name: {!r}'.format(name)
2794 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002795
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002796 params[name] = param
2797 else:
2798 params = OrderedDict(((param.name, param)
2799 for param in parameters))
2800
2801 self._parameters = types.MappingProxyType(params)
2802 self._return_annotation = return_annotation
2803
2804 @classmethod
2805 def from_function(cls, func):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002806 """Constructs Signature for the given python function."""
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002807
2808 warnings.warn("inspect.Signature.from_function() is deprecated, "
Berker Peksagb5601582015-05-21 23:40:54 +03002809 "use Signature.from_callable()",
2810 DeprecationWarning, stacklevel=2)
Yury Selivanovcf45f022015-05-20 14:38:50 -04002811 return _signature_from_function(cls, func)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002812
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002813 @classmethod
2814 def from_builtin(cls, func):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002815 """Constructs Signature for the given builtin function."""
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002816
2817 warnings.warn("inspect.Signature.from_builtin() is deprecated, "
Berker Peksagb5601582015-05-21 23:40:54 +03002818 "use Signature.from_callable()",
2819 DeprecationWarning, stacklevel=2)
Yury Selivanov57d240e2014-02-19 16:27:23 -05002820 return _signature_from_builtin(cls, func)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002821
Yury Selivanovda396452014-03-27 12:09:24 -04002822 @classmethod
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002823 def from_callable(cls, obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002824 """Constructs Signature for the given callable object."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002825 return _signature_from_callable(obj, sigcls=cls,
2826 follow_wrapper_chains=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04002827
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002828 @property
2829 def parameters(self):
2830 return self._parameters
2831
2832 @property
2833 def return_annotation(self):
2834 return self._return_annotation
2835
2836 def replace(self, *, parameters=_void, return_annotation=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002837 """Creates a customized copy of the Signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002838 Pass 'parameters' and/or 'return_annotation' arguments
2839 to override them in the new copy.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002840 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002841
2842 if parameters is _void:
2843 parameters = self.parameters.values()
2844
2845 if return_annotation is _void:
2846 return_annotation = self._return_annotation
2847
2848 return type(self)(parameters,
2849 return_annotation=return_annotation)
2850
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002851 def _hash_basis(self):
2852 params = tuple(param for param in self.parameters.values()
2853 if param.kind != _KEYWORD_ONLY)
2854
2855 kwo_params = {param.name: param for param in self.parameters.values()
2856 if param.kind == _KEYWORD_ONLY}
2857
2858 return params, kwo_params, self.return_annotation
2859
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002860 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002861 params, kwo_params, return_annotation = self._hash_basis()
2862 kwo_params = frozenset(kwo_params.values())
2863 return hash((params, kwo_params, return_annotation))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002864
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002865 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002866 if self is other:
2867 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002868 if not isinstance(other, Signature):
2869 return NotImplemented
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002870 return self._hash_basis() == other._hash_basis()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002871
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002872 def _bind(self, args, kwargs, *, partial=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002873 """Private method. Don't use directly."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002874
2875 arguments = OrderedDict()
2876
2877 parameters = iter(self.parameters.values())
2878 parameters_ex = ()
2879 arg_vals = iter(args)
2880
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002881 while True:
2882 # Let's iterate through the positional arguments and corresponding
2883 # parameters
2884 try:
2885 arg_val = next(arg_vals)
2886 except StopIteration:
2887 # No more positional arguments
2888 try:
2889 param = next(parameters)
2890 except StopIteration:
2891 # No more parameters. That's it. Just need to check that
2892 # we have no `kwargs` after this while loop
2893 break
2894 else:
2895 if param.kind == _VAR_POSITIONAL:
2896 # That's OK, just empty *args. Let's start parsing
2897 # kwargs
2898 break
2899 elif param.name in kwargs:
2900 if param.kind == _POSITIONAL_ONLY:
2901 msg = '{arg!r} parameter is positional only, ' \
2902 'but was passed as a keyword'
2903 msg = msg.format(arg=param.name)
2904 raise TypeError(msg) from None
2905 parameters_ex = (param,)
2906 break
2907 elif (param.kind == _VAR_KEYWORD or
2908 param.default is not _empty):
2909 # That's fine too - we have a default value for this
2910 # parameter. So, lets start parsing `kwargs`, starting
2911 # with the current parameter
2912 parameters_ex = (param,)
2913 break
2914 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002915 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
2916 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002917 if partial:
2918 parameters_ex = (param,)
2919 break
2920 else:
Yury Selivanov86872752015-05-19 00:27:49 -04002921 msg = 'missing a required argument: {arg!r}'
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002922 msg = msg.format(arg=param.name)
2923 raise TypeError(msg) from None
2924 else:
2925 # We have a positional argument to process
2926 try:
2927 param = next(parameters)
2928 except StopIteration:
2929 raise TypeError('too many positional arguments') from None
2930 else:
2931 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
2932 # Looks like we have no parameter for this positional
2933 # argument
Yury Selivanov86872752015-05-19 00:27:49 -04002934 raise TypeError(
2935 'too many positional arguments') from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002936
2937 if param.kind == _VAR_POSITIONAL:
2938 # We have an '*args'-like argument, let's fill it with
2939 # all positional arguments we have left and move on to
2940 # the next phase
2941 values = [arg_val]
2942 values.extend(arg_vals)
2943 arguments[param.name] = tuple(values)
2944 break
2945
2946 if param.name in kwargs:
Yury Selivanov86872752015-05-19 00:27:49 -04002947 raise TypeError(
2948 'multiple values for argument {arg!r}'.format(
2949 arg=param.name)) from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002950
2951 arguments[param.name] = arg_val
2952
2953 # Now, we iterate through the remaining parameters to process
2954 # keyword arguments
2955 kwargs_param = None
2956 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002957 if param.kind == _VAR_KEYWORD:
2958 # Memorize that we have a '**kwargs'-like parameter
2959 kwargs_param = param
2960 continue
2961
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002962 if param.kind == _VAR_POSITIONAL:
2963 # Named arguments don't refer to '*args'-like parameters.
2964 # We only arrive here if the positional arguments ended
2965 # before reaching the last parameter before *args.
2966 continue
2967
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002968 param_name = param.name
2969 try:
2970 arg_val = kwargs.pop(param_name)
2971 except KeyError:
2972 # We have no value for this parameter. It's fine though,
2973 # if it has a default value, or it is an '*args'-like
2974 # parameter, left alone by the processing of positional
2975 # arguments.
2976 if (not partial and param.kind != _VAR_POSITIONAL and
2977 param.default is _empty):
Yury Selivanov86872752015-05-19 00:27:49 -04002978 raise TypeError('missing a required argument: {arg!r}'. \
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002979 format(arg=param_name)) from None
2980
2981 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05002982 if param.kind == _POSITIONAL_ONLY:
2983 # This should never happen in case of a properly built
2984 # Signature object (but let's have this check here
2985 # to ensure correct behaviour just in case)
2986 raise TypeError('{arg!r} parameter is positional only, '
2987 'but was passed as a keyword'. \
2988 format(arg=param.name))
2989
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002990 arguments[param_name] = arg_val
2991
2992 if kwargs:
2993 if kwargs_param is not None:
2994 # Process our '**kwargs'-like parameter
2995 arguments[kwargs_param.name] = kwargs
2996 else:
Yury Selivanov86872752015-05-19 00:27:49 -04002997 raise TypeError(
2998 'got an unexpected keyword argument {arg!r}'.format(
2999 arg=next(iter(kwargs))))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003000
3001 return self._bound_arguments_cls(self, arguments)
3002
Yury Selivanovc45873e2014-01-29 12:10:27 -05003003 def bind(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003004 """Get a BoundArguments object, that maps the passed `args`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003005 and `kwargs` to the function's signature. Raises `TypeError`
3006 if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003007 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05003008 return args[0]._bind(args[1:], kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003009
Yury Selivanovc45873e2014-01-29 12:10:27 -05003010 def bind_partial(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003011 """Get a BoundArguments object, that partially maps the
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003012 passed `args` and `kwargs` to the function's signature.
3013 Raises `TypeError` if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003014 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05003015 return args[0]._bind(args[1:], kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003016
Yury Selivanova5d63dd2014-03-27 11:31:43 -04003017 def __reduce__(self):
3018 return (type(self),
3019 (tuple(self._parameters.values()),),
3020 {'_return_annotation': self._return_annotation})
3021
3022 def __setstate__(self, state):
3023 self._return_annotation = state['_return_annotation']
3024
Yury Selivanov374375d2014-03-27 12:41:53 -04003025 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04003026 return '<{} {}>'.format(self.__class__.__name__, self)
Yury Selivanov374375d2014-03-27 12:41:53 -04003027
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003028 def __str__(self):
3029 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05003030 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003031 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05003032 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003033 formatted = str(param)
3034
3035 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05003036
3037 if kind == _POSITIONAL_ONLY:
3038 render_pos_only_separator = True
3039 elif render_pos_only_separator:
3040 # It's not a positional-only parameter, and the flag
3041 # is set to 'True' (there were pos-only params before.)
3042 result.append('/')
3043 render_pos_only_separator = False
3044
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003045 if kind == _VAR_POSITIONAL:
3046 # OK, we have an '*args'-like parameter, so we won't need
3047 # a '*' to separate keyword-only arguments
3048 render_kw_only_separator = False
3049 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
3050 # We have a keyword-only parameter to render and we haven't
3051 # rendered an '*args'-like parameter before, so add a '*'
3052 # separator to the parameters list ("foo(arg1, *, arg2)" case)
3053 result.append('*')
3054 # This condition should be only triggered once, so
3055 # reset the flag
3056 render_kw_only_separator = False
3057
3058 result.append(formatted)
3059
Yury Selivanov2393dca2014-01-27 15:07:58 -05003060 if render_pos_only_separator:
3061 # There were only positional-only parameters, hence the
3062 # flag was not reset to 'False'
3063 result.append('/')
3064
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003065 rendered = '({})'.format(', '.join(result))
3066
3067 if self.return_annotation is not _empty:
3068 anno = formatannotation(self.return_annotation)
3069 rendered += ' -> {}'.format(anno)
3070
3071 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003072
Yury Selivanovda396452014-03-27 12:09:24 -04003073
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04003074def signature(obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003075 """Get a signature object for the passed callable."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04003076 return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04003077
3078
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003079def _main():
3080 """ Logic for inspecting an object given at command line """
3081 import argparse
3082 import importlib
3083
3084 parser = argparse.ArgumentParser()
3085 parser.add_argument(
3086 'object',
3087 help="The object to be analysed. "
3088 "It supports the 'module:qualname' syntax")
3089 parser.add_argument(
3090 '-d', '--details', action='store_true',
3091 help='Display info about the module rather than its source code')
3092
3093 args = parser.parse_args()
3094
3095 target = args.object
3096 mod_name, has_attrs, attrs = target.partition(":")
3097 try:
3098 obj = module = importlib.import_module(mod_name)
3099 except Exception as exc:
3100 msg = "Failed to import {} ({}: {})".format(mod_name,
3101 type(exc).__name__,
3102 exc)
3103 print(msg, file=sys.stderr)
3104 exit(2)
3105
3106 if has_attrs:
3107 parts = attrs.split(".")
3108 obj = module
3109 for part in parts:
3110 obj = getattr(obj, part)
3111
3112 if module.__name__ in sys.builtin_module_names:
3113 print("Can't get info for builtin modules.", file=sys.stderr)
3114 exit(1)
3115
3116 if args.details:
3117 print('Target: {}'.format(target))
3118 print('Origin: {}'.format(getsourcefile(module)))
3119 print('Cached: {}'.format(module.__cached__))
3120 if obj is module:
3121 print('Loader: {}'.format(repr(module.__loader__)))
3122 if hasattr(module, '__path__'):
3123 print('Submodule search path: {}'.format(module.__path__))
3124 else:
3125 try:
3126 __, lineno = findsource(obj)
3127 except Exception:
3128 pass
3129 else:
3130 print('Line: {}'.format(lineno))
3131
3132 print('\n')
3133 else:
3134 print(getsource(obj))
3135
3136
3137if __name__ == "__main__":
3138 _main()