blob: 18bed9028ed8ab974aa1eb520192be491f4fde45 [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
Karthikeyan Singaravelan696136b2020-04-18 21:49:32 +053035import ast
Antoine Pitroua8723a02015-04-15 00:41:29 +020036import dis
Yury Selivanov75445082015-05-11 22:57:16 -040037import collections.abc
Yury Selivanov21e83a52014-03-27 11:23:13 -040038import enum
Brett Cannoncb66eb02012-05-11 12:58:42 -040039import importlib.machinery
40import itertools
Christian Heimes7131fd92008-02-19 14:21:46 +000041import linecache
Brett Cannoncb66eb02012-05-11 12:58:42 -040042import os
43import re
44import sys
45import tokenize
Larry Hastings2623c8c2014-02-08 22:15:29 -080046import token
Brett Cannoncb66eb02012-05-11 12:58:42 -040047import types
Brett Cannon2b88fcf2012-06-02 22:28:42 -040048import warnings
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070049import functools
Nick Coghlan2f92e542012-06-23 19:39:55 +100050import builtins
Raymond Hettingera1a992c2005-03-11 06:46:45 +000051from operator import attrgetter
Inada Naoki21105512020-03-02 18:54:49 +090052from collections import namedtuple, OrderedDict
Nick Coghlan09c81232010-08-17 10:18:16 +000053
54# Create constants for the compiler flags in Include/code.h
Antoine Pitroua8723a02015-04-15 00:41:29 +020055# We try to get them from dis to avoid duplication
56mod_dict = globals()
57for k, v in dis.COMPILER_FLAG_NAMES.items():
58 mod_dict["CO_" + v] = k
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000059
Christian Heimesbe5b30b2008-03-03 19:18:51 +000060# See Include/object.h
61TPFLAGS_IS_ABSTRACT = 1 << 20
62
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000063# ----------------------------------------------------------- type-checking
64def ismodule(object):
65 """Return true if the object is a module.
66
67 Module objects provide these attributes:
Barry Warsaw28a691b2010-04-17 00:19:56 +000068 __cached__ pathname to byte compiled file
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000069 __doc__ documentation string
70 __file__ filename (missing for built-in modules)"""
Tim Peters28bc59f2001-09-16 08:40:16 +000071 return isinstance(object, types.ModuleType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000072
73def isclass(object):
74 """Return true if the object is a class.
75
76 Class objects provide these attributes:
77 __doc__ documentation string
78 __module__ name of module in which this class was defined"""
Benjamin Petersonc4656002009-01-17 22:41:18 +000079 return isinstance(object, type)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000080
81def ismethod(object):
82 """Return true if the object is an instance method.
83
84 Instance method objects provide these attributes:
85 __doc__ documentation string
86 __name__ name with which this method was defined
Christian Heimesff737952007-11-27 10:40:20 +000087 __func__ function object containing implementation of method
88 __self__ instance to which this method is bound"""
Tim Peters28bc59f2001-09-16 08:40:16 +000089 return isinstance(object, types.MethodType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000090
Tim Peters536d2262001-09-20 05:13:38 +000091def ismethoddescriptor(object):
Tim Petersf1d90b92001-09-20 05:47:55 +000092 """Return true if the object is a method descriptor.
93
94 But not if ismethod() or isclass() or isfunction() are true.
Tim Peters536d2262001-09-20 05:13:38 +000095
96 This is new in Python 2.2, and, for example, is true of int.__add__.
97 An object passing this test has a __get__ attribute but not a __set__
98 attribute, but beyond that the set of attributes varies. __name__ is
99 usually sensible, and __doc__ often is.
100
Tim Petersf1d90b92001-09-20 05:47:55 +0000101 Methods implemented via descriptors that also pass one of the other
102 tests return false from the ismethoddescriptor() test, simply because
103 the other tests promise more -- you can, e.g., count on having the
Christian Heimesff737952007-11-27 10:40:20 +0000104 __func__ attribute (etc) when an object passes ismethod()."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100105 if isclass(object) or ismethod(object) or isfunction(object):
106 # mutual exclusion
107 return False
108 tp = type(object)
109 return hasattr(tp, "__get__") and not hasattr(tp, "__set__")
Tim Peters536d2262001-09-20 05:13:38 +0000110
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000111def isdatadescriptor(object):
112 """Return true if the object is a data descriptor.
113
Aaron Hall, MBA4054b172018-05-20 19:46:42 -0400114 Data descriptors have a __set__ or a __delete__ attribute. Examples are
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000115 properties (defined in Python) and getsets and members (defined in C).
116 Typically, data descriptors will also have __name__ and __doc__ attributes
117 (properties, getsets, and members have both of these attributes), but this
118 is not guaranteed."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100119 if isclass(object) or ismethod(object) or isfunction(object):
120 # mutual exclusion
121 return False
122 tp = type(object)
Aaron Hall, MBA4054b172018-05-20 19:46:42 -0400123 return hasattr(tp, "__set__") or hasattr(tp, "__delete__")
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000124
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000125if hasattr(types, 'MemberDescriptorType'):
126 # CPython and equivalent
127 def ismemberdescriptor(object):
128 """Return true if the object is a member descriptor.
129
130 Member descriptors are specialized descriptors defined in extension
131 modules."""
132 return isinstance(object, types.MemberDescriptorType)
133else:
134 # Other implementations
135 def ismemberdescriptor(object):
136 """Return true if the object is a member descriptor.
137
138 Member descriptors are specialized descriptors defined in extension
139 modules."""
140 return False
141
142if hasattr(types, 'GetSetDescriptorType'):
143 # CPython and equivalent
144 def isgetsetdescriptor(object):
145 """Return true if the object is a getset descriptor.
146
147 getset descriptors are specialized descriptors defined in extension
148 modules."""
149 return isinstance(object, types.GetSetDescriptorType)
150else:
151 # Other implementations
152 def isgetsetdescriptor(object):
153 """Return true if the object is a getset descriptor.
154
155 getset descriptors are specialized descriptors defined in extension
156 modules."""
157 return False
158
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000159def isfunction(object):
160 """Return true if the object is a user-defined function.
161
162 Function objects provide these attributes:
163 __doc__ documentation string
164 __name__ name with which this function was defined
Neal Norwitz221085d2007-02-25 20:55:47 +0000165 __code__ code object containing compiled function bytecode
166 __defaults__ tuple of any default values for arguments
167 __globals__ global namespace in which this function was defined
168 __annotations__ dict of parameter annotations
169 __kwdefaults__ dict of keyword only parameters with defaults"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000170 return isinstance(object, types.FunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000171
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200172def _has_code_flag(f, flag):
173 """Return true if ``f`` is a function (or a method or functools.partial
174 wrapper wrapping a function) whose code object has the given ``flag``
175 set in its flags."""
176 while ismethod(f):
177 f = f.__func__
178 f = functools._unwrap_partial(f)
179 if not isfunction(f):
180 return False
181 return bool(f.__code__.co_flags & flag)
182
Pablo Galindo7cd25432018-10-26 12:19:14 +0100183def isgeneratorfunction(obj):
Christian Heimes7131fd92008-02-19 14:21:46 +0000184 """Return true if the object is a user-defined generator function.
185
Martin Panter0f0eac42016-09-07 11:04:41 +0000186 Generator function objects provide the same attributes as functions.
187 See help(isfunction) for a list of attributes."""
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200188 return _has_code_flag(obj, CO_GENERATOR)
Yury Selivanov75445082015-05-11 22:57:16 -0400189
Pablo Galindo7cd25432018-10-26 12:19:14 +0100190def iscoroutinefunction(obj):
Yury Selivanov75445082015-05-11 22:57:16 -0400191 """Return true if the object is a coroutine function.
192
Yury Selivanov4778e132016-11-08 12:23:09 -0500193 Coroutine functions are defined with "async def" syntax.
Yury Selivanov75445082015-05-11 22:57:16 -0400194 """
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200195 return _has_code_flag(obj, CO_COROUTINE)
Yury Selivanov75445082015-05-11 22:57:16 -0400196
Pablo Galindo7cd25432018-10-26 12:19:14 +0100197def isasyncgenfunction(obj):
Yury Selivanov4778e132016-11-08 12:23:09 -0500198 """Return true if the object is an asynchronous generator function.
199
200 Asynchronous generator functions are defined with "async def"
201 syntax and have "yield" expressions in their body.
202 """
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200203 return _has_code_flag(obj, CO_ASYNC_GENERATOR)
Yury Selivanoveb636452016-09-08 22:01:51 -0700204
205def isasyncgen(object):
Yury Selivanov4778e132016-11-08 12:23:09 -0500206 """Return true if the object is an asynchronous generator."""
Yury Selivanoveb636452016-09-08 22:01:51 -0700207 return isinstance(object, types.AsyncGeneratorType)
208
Christian Heimes7131fd92008-02-19 14:21:46 +0000209def isgenerator(object):
210 """Return true if the object is a generator.
211
212 Generator objects provide these attributes:
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300213 __iter__ defined to support iteration over container
Christian Heimes7131fd92008-02-19 14:21:46 +0000214 close raises a new GeneratorExit exception inside the
215 generator to terminate the iteration
216 gi_code code object
217 gi_frame frame object or possibly None once the generator has
218 been exhausted
219 gi_running set to 1 when generator is executing, 0 otherwise
220 next return the next item from the container
221 send resumes the generator and "sends" a value that becomes
222 the result of the current yield-expression
223 throw used to raise an exception inside the generator"""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400224 return isinstance(object, types.GeneratorType)
Yury Selivanov75445082015-05-11 22:57:16 -0400225
226def iscoroutine(object):
227 """Return true if the object is a coroutine."""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400228 return isinstance(object, types.CoroutineType)
Christian Heimes7131fd92008-02-19 14:21:46 +0000229
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400230def isawaitable(object):
Yury Selivanovc0215df2016-11-08 19:57:44 -0500231 """Return true if object can be passed to an ``await`` expression."""
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400232 return (isinstance(object, types.CoroutineType) or
233 isinstance(object, types.GeneratorType) and
Yury Selivanovc0215df2016-11-08 19:57:44 -0500234 bool(object.gi_code.co_flags & CO_ITERABLE_COROUTINE) or
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400235 isinstance(object, collections.abc.Awaitable))
236
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000237def istraceback(object):
238 """Return true if the object is a traceback.
239
240 Traceback objects provide these attributes:
241 tb_frame frame object at this level
242 tb_lasti index of last attempted instruction in bytecode
243 tb_lineno current line number in Python source code
244 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000245 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000246
247def isframe(object):
248 """Return true if the object is a frame object.
249
250 Frame objects provide these attributes:
251 f_back next outer frame object (this frame's caller)
252 f_builtins built-in namespace seen by this frame
253 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000254 f_globals global namespace seen by this frame
255 f_lasti index of last attempted instruction in bytecode
256 f_lineno current line number in Python source code
257 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000258 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000259 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000260
261def iscode(object):
262 """Return true if the object is a code object.
263
264 Code objects provide these attributes:
Xiang Zhanga6902e62017-04-13 10:38:28 +0800265 co_argcount number of arguments (not including *, ** args
266 or keyword only arguments)
267 co_code string of raw compiled bytecode
268 co_cellvars tuple of names of cell variables
269 co_consts tuple of constants used in the bytecode
270 co_filename name of file in which this code object was created
271 co_firstlineno number of first line in Python source code
272 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
273 | 16=nested | 32=generator | 64=nofree | 128=coroutine
274 | 256=iterable_coroutine | 512=async_generator
275 co_freevars tuple of names of free variables
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100276 co_posonlyargcount number of positional only arguments
Xiang Zhanga6902e62017-04-13 10:38:28 +0800277 co_kwonlyargcount number of keyword only arguments (not including ** arg)
278 co_lnotab encoded mapping of line numbers to bytecode indices
279 co_name name with which this code object was defined
280 co_names tuple of names of local variables
281 co_nlocals number of local variables
282 co_stacksize virtual machine stack space required
283 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000284 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000285
286def isbuiltin(object):
287 """Return true if the object is a built-in function or method.
288
289 Built-in functions and methods provide these attributes:
290 __doc__ documentation string
291 __name__ original name of this function or method
292 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000293 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000294
295def isroutine(object):
296 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000297 return (isbuiltin(object)
298 or isfunction(object)
299 or ismethod(object)
300 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000301
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000302def isabstract(object):
303 """Return true if the object is an abstract base class (ABC)."""
Natefcfe80e2017-04-24 10:06:15 -0700304 if not isinstance(object, type):
305 return False
306 if object.__flags__ & TPFLAGS_IS_ABSTRACT:
307 return True
308 if not issubclass(type(object), abc.ABCMeta):
309 return False
310 if hasattr(object, '__abstractmethods__'):
311 # It looks like ABCMeta.__new__ has finished running;
312 # TPFLAGS_IS_ABSTRACT should have been accurate.
313 return False
314 # It looks like ABCMeta.__new__ has not finished running yet; we're
315 # probably in __init_subclass__. We'll look for abstractmethods manually.
316 for name, value in object.__dict__.items():
317 if getattr(value, "__isabstractmethod__", False):
318 return True
319 for base in object.__bases__:
320 for name in getattr(base, "__abstractmethods__", ()):
321 value = getattr(object, name, None)
322 if getattr(value, "__isabstractmethod__", False):
323 return True
324 return False
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000325
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000326def getmembers(object, predicate=None):
327 """Return all members of an object as (name, value) pairs sorted by name.
328 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100329 if isclass(object):
330 mro = (object,) + getmro(object)
331 else:
332 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000333 results = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700334 processed = set()
335 names = dir(object)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700336 # :dd any DynamicClassAttributes to the list of names if object is a class;
Ethan Furmane03ea372013-09-25 07:14:41 -0700337 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700338 # attribute with the same name as a DynamicClassAttribute exists
Ethan Furmane03ea372013-09-25 07:14:41 -0700339 try:
340 for base in object.__bases__:
341 for k, v in base.__dict__.items():
342 if isinstance(v, types.DynamicClassAttribute):
343 names.append(k)
344 except AttributeError:
345 pass
346 for key in names:
Ethan Furman63c141c2013-10-18 00:27:39 -0700347 # First try to get the value via getattr. Some descriptors don't
348 # like calling their __get__ (see bug #1785), so fall back to
349 # looking in the __dict__.
350 try:
351 value = getattr(object, key)
352 # handle the duplicate key
353 if key in processed:
354 raise AttributeError
355 except AttributeError:
356 for base in mro:
357 if key in base.__dict__:
358 value = base.__dict__[key]
359 break
360 else:
361 # could be a (currently) missing slot member, or a buggy
362 # __dir__; discard and move on
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100363 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000364 if not predicate or predicate(value):
365 results.append((key, value))
Ethan Furmane03ea372013-09-25 07:14:41 -0700366 processed.add(key)
367 results.sort(key=lambda pair: pair[0])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000368 return results
369
Christian Heimes25bb7832008-01-11 16:17:00 +0000370Attribute = namedtuple('Attribute', 'name kind defining_class object')
371
Tim Peters13b49d32001-09-23 02:00:29 +0000372def classify_class_attrs(cls):
373 """Return list of attribute-descriptor tuples.
374
375 For each name in dir(cls), the return list contains a 4-tuple
376 with these elements:
377
378 0. The name (a string).
379
380 1. The kind of attribute this is, one of these strings:
381 'class method' created via classmethod()
382 'static method' created via staticmethod()
383 'property' created via property()
Ethan Furmane03ea372013-09-25 07:14:41 -0700384 'method' any other flavor of method or descriptor
Tim Peters13b49d32001-09-23 02:00:29 +0000385 'data' not a method
386
387 2. The class which defined this attribute (a class).
388
Ethan Furmane03ea372013-09-25 07:14:41 -0700389 3. The object as obtained by calling getattr; if this fails, or if the
390 resulting object does not live anywhere in the class' mro (including
391 metaclasses) then the object is looked up in the defining class's
392 dict (found by walking the mro).
Ethan Furman668dede2013-09-14 18:53:26 -0700393
394 If one of the items in dir(cls) is stored in the metaclass it will now
395 be discovered and not have None be listed as the class in which it was
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700396 defined. Any items whose home class cannot be discovered are skipped.
Tim Peters13b49d32001-09-23 02:00:29 +0000397 """
398
399 mro = getmro(cls)
Ethan Furman668dede2013-09-14 18:53:26 -0700400 metamro = getmro(type(cls)) # for attributes stored in the metaclass
Jon Dufresne39726282017-05-18 07:35:54 -0700401 metamro = tuple(cls for cls in metamro if cls not in (type, object))
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700402 class_bases = (cls,) + mro
403 all_bases = class_bases + metamro
Tim Peters13b49d32001-09-23 02:00:29 +0000404 names = dir(cls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700405 # :dd any DynamicClassAttributes to the list of names;
Ethan Furmane03ea372013-09-25 07:14:41 -0700406 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700407 # attribute with the same name as a DynamicClassAttribute exists.
Ethan Furman63c141c2013-10-18 00:27:39 -0700408 for base in mro:
Ethan Furmane03ea372013-09-25 07:14:41 -0700409 for k, v in base.__dict__.items():
410 if isinstance(v, types.DynamicClassAttribute):
411 names.append(k)
Tim Peters13b49d32001-09-23 02:00:29 +0000412 result = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700413 processed = set()
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700414
Tim Peters13b49d32001-09-23 02:00:29 +0000415 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100416 # Get the object associated with the name, and where it was defined.
Ethan Furmane03ea372013-09-25 07:14:41 -0700417 # Normal objects will be looked up with both getattr and directly in
418 # its class' dict (in case getattr fails [bug #1785], and also to look
419 # for a docstring).
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700420 # For DynamicClassAttributes on the second pass we only look in the
Ethan Furmane03ea372013-09-25 07:14:41 -0700421 # class's dict.
422 #
Tim Peters13b49d32001-09-23 02:00:29 +0000423 # Getting an obj from the __dict__ sometimes reveals more than
424 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100425 homecls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700426 get_obj = None
427 dict_obj = None
Ethan Furmane03ea372013-09-25 07:14:41 -0700428 if name not in processed:
429 try:
Ethan Furmana8b07072013-10-18 01:22:08 -0700430 if name == '__dict__':
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700431 raise Exception("__dict__ is special, don't want the proxy")
Ethan Furmane03ea372013-09-25 07:14:41 -0700432 get_obj = getattr(cls, name)
433 except Exception as exc:
434 pass
435 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700436 homecls = getattr(get_obj, "__objclass__", homecls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700437 if homecls not in class_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700438 # if the resulting object does not live somewhere in the
Ethan Furman63c141c2013-10-18 00:27:39 -0700439 # mro, drop it and search the mro manually
Ethan Furmane03ea372013-09-25 07:14:41 -0700440 homecls = None
Ethan Furman63c141c2013-10-18 00:27:39 -0700441 last_cls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700442 # first look in the classes
443 for srch_cls in class_bases:
Ethan Furman63c141c2013-10-18 00:27:39 -0700444 srch_obj = getattr(srch_cls, name, None)
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400445 if srch_obj is get_obj:
Ethan Furman63c141c2013-10-18 00:27:39 -0700446 last_cls = srch_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700447 # then check the metaclasses
448 for srch_cls in metamro:
449 try:
450 srch_obj = srch_cls.__getattr__(cls, name)
451 except AttributeError:
452 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400453 if srch_obj is get_obj:
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700454 last_cls = srch_cls
Ethan Furman63c141c2013-10-18 00:27:39 -0700455 if last_cls is not None:
456 homecls = last_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700457 for base in all_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700458 if name in base.__dict__:
459 dict_obj = base.__dict__[name]
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700460 if homecls not in metamro:
461 homecls = base
Ethan Furmane03ea372013-09-25 07:14:41 -0700462 break
Ethan Furman63c141c2013-10-18 00:27:39 -0700463 if homecls is None:
464 # unable to locate the attribute anywhere, most likely due to
465 # buggy custom __dir__; discard and move on
466 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400467 obj = get_obj if get_obj is not None else dict_obj
Ethan Furmane03ea372013-09-25 07:14:41 -0700468 # Classify the object or its descriptor.
Serhiy Storchaka3327a2d2017-12-15 14:13:41 +0200469 if isinstance(dict_obj, (staticmethod, types.BuiltinMethodType)):
Tim Peters13b49d32001-09-23 02:00:29 +0000470 kind = "static method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700471 obj = dict_obj
Serhiy Storchaka3327a2d2017-12-15 14:13:41 +0200472 elif isinstance(dict_obj, (classmethod, types.ClassMethodDescriptorType)):
Tim Peters13b49d32001-09-23 02:00:29 +0000473 kind = "class method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700474 obj = dict_obj
475 elif isinstance(dict_obj, property):
Tim Peters13b49d32001-09-23 02:00:29 +0000476 kind = "property"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700477 obj = dict_obj
Yury Selivanov0860a0b2014-01-31 14:28:44 -0500478 elif isroutine(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000479 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100480 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700481 kind = "data"
Christian Heimes25bb7832008-01-11 16:17:00 +0000482 result.append(Attribute(name, kind, homecls, obj))
Ethan Furmane03ea372013-09-25 07:14:41 -0700483 processed.add(name)
Tim Peters13b49d32001-09-23 02:00:29 +0000484 return result
485
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000486# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000487
488def getmro(cls):
489 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000490 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000491
Nick Coghlane8c45d62013-07-28 20:00:01 +1000492# -------------------------------------------------------- function helpers
493
494def unwrap(func, *, stop=None):
495 """Get the object wrapped by *func*.
496
497 Follows the chain of :attr:`__wrapped__` attributes returning the last
498 object in the chain.
499
500 *stop* is an optional callback accepting an object in the wrapper chain
501 as its sole argument that allows the unwrapping to be terminated early if
502 the callback returns a true value. If the callback never returns a true
503 value, the last object in the chain is returned as usual. For example,
504 :func:`signature` uses this to stop unwrapping if any object in the
505 chain has a ``__signature__`` attribute defined.
506
507 :exc:`ValueError` is raised if a cycle is encountered.
508
509 """
510 if stop is None:
511 def _is_wrapper(f):
512 return hasattr(f, '__wrapped__')
513 else:
514 def _is_wrapper(f):
515 return hasattr(f, '__wrapped__') and not stop(f)
516 f = func # remember the original func for error reporting
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100517 # Memoise by id to tolerate non-hashable objects, but store objects to
518 # ensure they aren't destroyed, which would allow their IDs to be reused.
519 memo = {id(f): f}
520 recursion_limit = sys.getrecursionlimit()
Nick Coghlane8c45d62013-07-28 20:00:01 +1000521 while _is_wrapper(func):
522 func = func.__wrapped__
523 id_func = id(func)
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100524 if (id_func in memo) or (len(memo) >= recursion_limit):
Nick Coghlane8c45d62013-07-28 20:00:01 +1000525 raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100526 memo[id_func] = func
Nick Coghlane8c45d62013-07-28 20:00:01 +1000527 return func
528
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000529# -------------------------------------------------- source code extraction
530def indentsize(line):
531 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000532 expline = line.expandtabs()
533 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000534
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300535def _findclass(func):
536 cls = sys.modules.get(func.__module__)
537 if cls is None:
538 return None
539 for name in func.__qualname__.split('.')[:-1]:
540 cls = getattr(cls, name)
541 if not isclass(cls):
542 return None
543 return cls
544
545def _finddoc(obj):
Serhiy Storchaka08b47c32020-05-18 20:25:07 +0300546 if isclass(obj):
547 for base in obj.__mro__:
548 if base is not object:
549 try:
550 doc = base.__doc__
551 except AttributeError:
552 continue
553 if doc is not None:
554 return doc
555 return None
556
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300557 if ismethod(obj):
558 name = obj.__func__.__name__
559 self = obj.__self__
560 if (isclass(self) and
561 getattr(getattr(self, name, None), '__func__') is obj.__func__):
562 # classmethod
563 cls = self
564 else:
565 cls = self.__class__
566 elif isfunction(obj):
567 name = obj.__name__
568 cls = _findclass(obj)
569 if cls is None or getattr(cls, name) is not obj:
570 return None
571 elif isbuiltin(obj):
572 name = obj.__name__
573 self = obj.__self__
574 if (isclass(self) and
575 self.__qualname__ + '.' + name == obj.__qualname__):
576 # classmethod
577 cls = self
578 else:
579 cls = self.__class__
Serhiy Storchakaac4bdcc2015-10-29 08:15:50 +0200580 # Should be tested before isdatadescriptor().
581 elif isinstance(obj, property):
582 func = obj.fget
583 name = func.__name__
584 cls = _findclass(func)
585 if cls is None or getattr(cls, name) is not obj:
586 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300587 elif ismethoddescriptor(obj) or isdatadescriptor(obj):
588 name = obj.__name__
589 cls = obj.__objclass__
590 if getattr(cls, name) is not obj:
591 return None
Raymond Hettingerd1e768a2019-03-25 13:01:13 -0700592 if ismemberdescriptor(obj):
593 slots = getattr(cls, '__slots__', None)
594 if isinstance(slots, dict) and name in slots:
595 return slots[name]
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300596 else:
597 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300598 for base in cls.__mro__:
599 try:
Serhiy Storchaka08b47c32020-05-18 20:25:07 +0300600 doc = getattr(base, name).__doc__
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300601 except AttributeError:
602 continue
603 if doc is not None:
604 return doc
605 return None
606
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000607def getdoc(object):
608 """Get the documentation string for an object.
609
610 All tabs are expanded to spaces. To clean up docstrings that are
611 indented to line up with blocks of code, any whitespace than can be
612 uniformly removed from the second line onwards is removed."""
Serhiy Storchaka08b47c32020-05-18 20:25:07 +0300613 try:
614 doc = object.__doc__
615 except AttributeError:
616 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300617 if doc is None:
618 try:
619 doc = _finddoc(object)
620 except (AttributeError, TypeError):
621 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000622 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000623 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000624 return cleandoc(doc)
625
626def cleandoc(doc):
627 """Clean up indentation from docstrings.
628
629 Any whitespace that can be uniformly removed from the second line
630 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000631 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000632 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000633 except UnicodeError:
634 return None
635 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000636 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000637 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000638 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000639 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000640 if content:
641 indent = len(line) - content
642 margin = min(margin, indent)
643 # Remove indentation.
644 if lines:
645 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000646 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000647 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000648 # Remove any trailing or leading blank lines.
649 while lines and not lines[-1]:
650 lines.pop()
651 while lines and not lines[0]:
652 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000653 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000654
655def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000656 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000657 if ismodule(object):
Jason R. Coombsb9650a02018-03-05 18:29:08 -0500658 if getattr(object, '__file__', None):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000659 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000660 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000661 if isclass(object):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500662 if hasattr(object, '__module__'):
Philipp Ad407d2a2019-06-08 14:05:46 +0200663 module = sys.modules.get(object.__module__)
664 if getattr(module, '__file__', None):
665 return module.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000666 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000667 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000668 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000669 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000670 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000671 if istraceback(object):
672 object = object.tb_frame
673 if isframe(object):
674 object = object.f_code
675 if iscode(object):
676 return object.co_filename
Thomas Kluyvere968bc732017-10-24 13:42:36 +0100677 raise TypeError('module, class, method, function, traceback, frame, or '
678 'code object was expected, got {}'.format(
679 type(object).__name__))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000680
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000681def getmodulename(path):
682 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000683 fname = os.path.basename(path)
684 # Check for paths that look like an actual module file
685 suffixes = [(-len(suffix), suffix)
686 for suffix in importlib.machinery.all_suffixes()]
687 suffixes.sort() # try longest suffixes first, in case they overlap
688 for neglen, suffix in suffixes:
689 if fname.endswith(suffix):
690 return fname[:neglen]
691 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000692
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000693def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000694 """Return the filename that can be used to locate an object's source.
695 Return None if no way can be identified to get the source.
696 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000697 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400698 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
699 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
700 if any(filename.endswith(s) for s in all_bytecode_suffixes):
701 filename = (os.path.splitext(filename)[0] +
702 importlib.machinery.SOURCE_SUFFIXES[0])
703 elif any(filename.endswith(s) for s in
704 importlib.machinery.EXTENSION_SUFFIXES):
705 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000706 if os.path.exists(filename):
707 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000708 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400709 if getattr(getmodule(object, filename), '__loader__', None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000710 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000711 # or it is in the linecache
712 if filename in linecache.cache:
713 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000714
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000715def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000716 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000717
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000718 The idea is for each object to have a unique origin, so this routine
719 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000720 if _filename is None:
721 _filename = getsourcefile(object) or getfile(object)
722 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000723
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000724modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000725_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000726
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000727def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000728 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000729 if ismodule(object):
730 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000731 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000732 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000733 # Try the filename to modulename cache
734 if _filename is not None and _filename in modulesbyfile:
735 return sys.modules.get(modulesbyfile[_filename])
736 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000737 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000738 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000739 except TypeError:
740 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000741 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000742 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000743 # Update the filename to module name cache and check yet again
744 # Copy sys.modules in order to cope with changes while iterating
Gregory P. Smith85cf1d52020-03-04 16:45:22 -0800745 for modname, module in sys.modules.copy().items():
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000746 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000747 f = module.__file__
748 if f == _filesbymodname.get(modname, None):
749 # Have already mapped this module, so skip it
750 continue
751 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000752 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000753 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000754 modulesbyfile[f] = modulesbyfile[
755 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000756 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000757 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000758 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000759 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000760 if not hasattr(object, '__name__'):
761 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000762 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000763 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000764 if mainobject is object:
765 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000766 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000767 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000768 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000769 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000770 if builtinobject is object:
771 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000772
Karthikeyan Singaravelan696136b2020-04-18 21:49:32 +0530773
774class ClassFoundException(Exception):
775 pass
776
777
778class _ClassFinder(ast.NodeVisitor):
779
780 def __init__(self, qualname):
781 self.stack = []
782 self.qualname = qualname
783
784 def visit_FunctionDef(self, node):
785 self.stack.append(node.name)
786 self.stack.append('<locals>')
787 self.generic_visit(node)
788 self.stack.pop()
789 self.stack.pop()
790
791 visit_AsyncFunctionDef = visit_FunctionDef
792
793 def visit_ClassDef(self, node):
794 self.stack.append(node.name)
795 if self.qualname == '.'.join(self.stack):
796 # Return the decorator for the class if present
797 if node.decorator_list:
798 line_number = node.decorator_list[0].lineno
799 else:
800 line_number = node.lineno
801
802 # decrement by one since lines starts with indexing by zero
803 line_number -= 1
804 raise ClassFoundException(line_number)
805 self.generic_visit(node)
806 self.stack.pop()
807
808
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000809def findsource(object):
810 """Return the entire source file and starting line number for an object.
811
812 The argument may be a module, class, method, function, traceback, frame,
813 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200814 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000815 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500816
Yury Selivanovef1e7502014-12-08 16:05:34 -0500817 file = getsourcefile(object)
818 if file:
819 # Invalidate cache if needed.
820 linecache.checkcache(file)
821 else:
822 file = getfile(object)
823 # Allow filenames in form of "<something>" to pass through.
824 # `doctest` monkeypatches `linecache` module to enable
825 # inspection, so let `linecache.getlines` to be called.
826 if not (file.startswith('<') and file.endswith('>')):
827 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500828
Thomas Wouters89f507f2006-12-13 04:49:30 +0000829 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000830 if module:
831 lines = linecache.getlines(file, module.__dict__)
832 else:
833 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000834 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200835 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000836
837 if ismodule(object):
838 return lines, 0
839
840 if isclass(object):
Karthikeyan Singaravelan696136b2020-04-18 21:49:32 +0530841 qualname = object.__qualname__
842 source = ''.join(lines)
843 tree = ast.parse(source)
844 class_finder = _ClassFinder(qualname)
845 try:
846 class_finder.visit(tree)
847 except ClassFoundException as e:
848 line_number = e.args[0]
849 return lines, line_number
Jeremy Hyltonab919022003-06-27 18:41:20 +0000850 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200851 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000852
853 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000854 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000855 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000856 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000857 if istraceback(object):
858 object = object.tb_frame
859 if isframe(object):
860 object = object.f_code
861 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000862 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200863 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000864 lnum = object.co_firstlineno - 1
Yury Selivanove4e811d2015-07-21 19:01:52 +0300865 pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000866 while lnum > 0:
Miss Islington (bot)d1f07412020-12-04 14:41:58 -0800867 try:
868 line = lines[lnum]
869 except IndexError:
870 raise OSError('lineno is out of bounds')
871 if pat.match(line):
872 break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000873 lnum = lnum - 1
874 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200875 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000876
877def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000878 """Get lines of comments immediately preceding an object's source code.
879
880 Returns None when source can't be found.
881 """
882 try:
883 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200884 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000885 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000886
887 if ismodule(object):
888 # Look for a comment block at the top of the file.
889 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000890 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000891 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000892 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000893 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000894 comments = []
895 end = start
896 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000897 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000898 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000899 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000900
901 # Look for a preceding block of comments at the same indentation.
902 elif lnum > 0:
903 indent = indentsize(lines[lnum])
904 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000905 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000906 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000907 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000908 if end > 0:
909 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000910 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000911 while comment[:1] == '#' and indentsize(lines[end]) == indent:
912 comments[:0] = [comment]
913 end = end - 1
914 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000915 comment = lines[end].expandtabs().lstrip()
916 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000917 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000918 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000919 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000920 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000921
Tim Peters4efb6e92001-06-29 23:51:08 +0000922class EndOfBlock(Exception): pass
923
924class BlockFinder:
925 """Provide a tokeneater() method to detect the end of a code block."""
926 def __init__(self):
927 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000928 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000929 self.started = False
930 self.passline = False
Meador Inge5b718d72015-07-23 22:49:37 -0500931 self.indecorator = False
932 self.decoratorhasargs = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000933 self.last = 1
Miss Islington (bot)81ac0302020-12-04 12:20:05 -0800934 self.body_col0 = None
Tim Peters4efb6e92001-06-29 23:51:08 +0000935
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000936 def tokeneater(self, type, token, srowcol, erowcol, line):
Meador Inge5b718d72015-07-23 22:49:37 -0500937 if not self.started and not self.indecorator:
938 # skip any decorators
939 if token == "@":
940 self.indecorator = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000941 # look for the first "def", "class" or "lambda"
Meador Inge5b718d72015-07-23 22:49:37 -0500942 elif token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000943 if token == "lambda":
944 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000945 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000946 self.passline = True # skip to the end of the line
Meador Inge5b718d72015-07-23 22:49:37 -0500947 elif token == "(":
948 if self.indecorator:
949 self.decoratorhasargs = True
950 elif token == ")":
951 if self.indecorator:
952 self.indecorator = False
953 self.decoratorhasargs = False
Tim Peters4efb6e92001-06-29 23:51:08 +0000954 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000955 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000956 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000957 if self.islambda: # lambdas always end at the first NEWLINE
958 raise EndOfBlock
Meador Inge5b718d72015-07-23 22:49:37 -0500959 # hitting a NEWLINE when in a decorator without args
960 # ends the decorator
961 if self.indecorator and not self.decoratorhasargs:
962 self.indecorator = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000963 elif self.passline:
964 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000965 elif type == tokenize.INDENT:
Miss Islington (bot)81ac0302020-12-04 12:20:05 -0800966 if self.body_col0 is None and self.started:
967 self.body_col0 = erowcol[1]
Tim Peters4efb6e92001-06-29 23:51:08 +0000968 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000969 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000970 elif type == tokenize.DEDENT:
971 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000972 # the end of matching indent/dedent pairs end a block
973 # (note that this only works for "def"/"class" blocks,
974 # not e.g. for "if: else:" or "try: finally:" blocks)
975 if self.indent <= 0:
976 raise EndOfBlock
Miss Islington (bot)81ac0302020-12-04 12:20:05 -0800977 elif type == tokenize.COMMENT:
978 if self.body_col0 is not None and srowcol[1] >= self.body_col0:
979 # Include comments if indented at least as much as the block
980 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000981 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
982 # any other token on the same indentation level end the previous
983 # block as well, except the pseudo-tokens COMMENT and NL.
984 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000985
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000986def getblock(lines):
987 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000988 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000989 try:
Trent Nelson428de652008-03-18 22:41:35 +0000990 tokens = tokenize.generate_tokens(iter(lines).__next__)
991 for _token in tokens:
992 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000993 except (EndOfBlock, IndentationError):
994 pass
995 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000996
997def getsourcelines(object):
998 """Return a list of source lines and starting line number for an object.
999
1000 The argument may be a module, class, method, function, traceback, frame,
1001 or code object. The source code is returned as a list of the lines
1002 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001003 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001004 raised if the source code cannot be retrieved."""
Yury Selivanov081bbf62014-09-26 17:34:54 -04001005 object = unwrap(object)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001006 lines, lnum = findsource(object)
1007
Vladimir Matveev91cb2982018-08-24 07:18:00 -07001008 if istraceback(object):
1009 object = object.tb_frame
1010
1011 # for module or frame that corresponds to module, return all source lines
1012 if (ismodule(object) or
1013 (isframe(object) and object.f_code.co_name == "<module>")):
Meador Inge5b718d72015-07-23 22:49:37 -05001014 return lines, 0
1015 else:
1016 return getblock(lines[lnum:]), lnum + 1
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001017
1018def getsource(object):
1019 """Return the text of the source code for an object.
1020
1021 The argument may be a module, class, method, function, traceback, frame,
1022 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001023 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001024 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001025 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001026
1027# --------------------------------------------------- class tree extraction
1028def walktree(classes, children, parent):
1029 """Recursive helper function for getclasstree()."""
1030 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +00001031 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001032 for c in classes:
1033 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +00001034 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001035 results.append(walktree(children[c], children, c))
1036 return results
1037
Georg Brandl5ce83a02009-06-01 17:23:51 +00001038def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001039 """Arrange the given list of classes into a hierarchy of nested lists.
1040
1041 Where a nested list appears, it contains classes derived from the class
1042 whose entry immediately precedes the list. Each entry is a 2-tuple
1043 containing a class and a tuple of its base classes. If the 'unique'
1044 argument is true, exactly one entry appears in the returned structure
1045 for each class in the given list. Otherwise, classes using multiple
1046 inheritance and their descendants will appear multiple times."""
1047 children = {}
1048 roots = []
1049 for c in classes:
1050 if c.__bases__:
1051 for parent in c.__bases__:
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05301052 if parent not in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001053 children[parent] = []
Serhiy Storchaka362c1b52013-09-05 17:14:32 +03001054 if c not in children[parent]:
1055 children[parent].append(c)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001056 if unique and parent in classes: break
1057 elif c not in roots:
1058 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +00001059 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001060 if parent not in classes:
1061 roots.append(parent)
1062 return walktree(roots, children, None)
1063
1064# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001065Arguments = namedtuple('Arguments', 'args, varargs, varkw')
1066
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001067def getargs(co):
1068 """Get information about the arguments accepted by a code object.
1069
Guido van Rossum2e65f892007-02-28 22:03:49 +00001070 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001071 'args' is the list of argument names. Keyword-only arguments are
1072 appended. 'varargs' and 'varkw' are the names of the * and **
1073 arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +00001074 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001075 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001076
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001077 names = co.co_varnames
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001078 nargs = co.co_argcount
Guido van Rossum2e65f892007-02-28 22:03:49 +00001079 nkwargs = co.co_kwonlyargcount
Pablo Galindocd74e662019-06-01 18:08:04 +01001080 args = list(names[:nargs])
1081 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001082 step = 0
1083
Guido van Rossum2e65f892007-02-28 22:03:49 +00001084 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001085 varargs = None
1086 if co.co_flags & CO_VARARGS:
1087 varargs = co.co_varnames[nargs]
1088 nargs = nargs + 1
1089 varkw = None
1090 if co.co_flags & CO_VARKEYWORDS:
1091 varkw = co.co_varnames[nargs]
Pablo Galindocd74e662019-06-01 18:08:04 +01001092 return Arguments(args + kwonlyargs, varargs, varkw)
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001093
1094ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
1095
1096def getargspec(func):
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001097 """Get the names and default values of a function's parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001098
1099 A tuple of four things is returned: (args, varargs, keywords, defaults).
1100 'args' is a list of the argument names, including keyword-only argument names.
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001101 'varargs' and 'keywords' are the names of the * and ** parameters or None.
1102 'defaults' is an n-tuple of the default values of the last n parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001103
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001104 This function is deprecated, as it does not support annotations or
1105 keyword-only parameters and will raise ValueError if either is present
1106 on the supplied callable.
1107
1108 For a more structured introspection API, use inspect.signature() instead.
1109
1110 Alternatively, use getfullargspec() for an API with a similar namedtuple
1111 based interface, but full support for annotations and keyword-only
1112 parameters.
Matthias Bussonnierded87d82018-10-19 16:40:45 -07001113
1114 Deprecated since Python 3.5, use `inspect.getfullargspec()`.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001115 """
Matthias Bussonnierded87d82018-10-19 16:40:45 -07001116 warnings.warn("inspect.getargspec() is deprecated since Python 3.0, "
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001117 "use inspect.signature() or inspect.getfullargspec()",
1118 DeprecationWarning, stacklevel=2)
Pablo Galindod5d2b452019-04-30 02:01:14 +01001119 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
1120 getfullargspec(func)
1121 if kwonlyargs or ann:
1122 raise ValueError("Function has keyword-only parameters or annotations"
1123 ", use inspect.signature() API which can support them")
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001124 return ArgSpec(args, varargs, varkw, defaults)
1125
Christian Heimes25bb7832008-01-11 16:17:00 +00001126FullArgSpec = namedtuple('FullArgSpec',
Pablo Galindod5d2b452019-04-30 02:01:14 +01001127 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001128
1129def getfullargspec(func):
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001130 """Get the names and default values of a callable object's parameters.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001131
Brett Cannon504d8852007-09-07 02:12:14 +00001132 A tuple of seven things is returned:
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001133 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations).
1134 'args' is a list of the parameter names.
1135 'varargs' and 'varkw' are the names of the * and ** parameters or None.
1136 'defaults' is an n-tuple of the default values of the last n parameters.
1137 'kwonlyargs' is a list of keyword-only parameter names.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001138 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001139 'annotations' is a dictionary mapping parameter names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001140
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001141 Notable differences from inspect.signature():
1142 - the "self" parameter is always reported, even for bound methods
1143 - wrapper chains defined by __wrapped__ *not* unwrapped automatically
Jeremy Hylton64967882003-06-27 18:14:39 +00001144 """
Yury Selivanov57d240e2014-02-19 16:27:23 -05001145 try:
1146 # Re: `skip_bound_arg=False`
1147 #
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001148 # There is a notable difference in behaviour between getfullargspec
1149 # and Signature: the former always returns 'self' parameter for bound
1150 # methods, whereas the Signature always shows the actual calling
1151 # signature of the passed object.
1152 #
1153 # To simulate this behaviour, we "unbind" bound methods, to trick
1154 # inspect.signature to always return their first parameter ("self",
1155 # usually)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001156
Yury Selivanov57d240e2014-02-19 16:27:23 -05001157 # Re: `follow_wrapper_chains=False`
1158 #
1159 # getfullargspec() historically ignored __wrapped__ attributes,
1160 # so we ensure that remains the case in 3.3+
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001161
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001162 sig = _signature_from_callable(func,
1163 follow_wrapper_chains=False,
1164 skip_bound_arg=False,
1165 sigcls=Signature)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001166 except Exception as ex:
1167 # Most of the times 'signature' will raise ValueError.
1168 # But, it can also raise AttributeError, and, maybe something
1169 # else. So to be fully backwards compatible, we catch all
1170 # possible exceptions here, and reraise a TypeError.
1171 raise TypeError('unsupported callable') from ex
1172
1173 args = []
1174 varargs = None
1175 varkw = None
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001176 posonlyargs = []
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001177 kwonlyargs = []
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001178 annotations = {}
1179 defaults = ()
1180 kwdefaults = {}
1181
1182 if sig.return_annotation is not sig.empty:
1183 annotations['return'] = sig.return_annotation
1184
1185 for param in sig.parameters.values():
1186 kind = param.kind
1187 name = param.name
1188
1189 if kind is _POSITIONAL_ONLY:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001190 posonlyargs.append(name)
1191 if param.default is not param.empty:
1192 defaults += (param.default,)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001193 elif kind is _POSITIONAL_OR_KEYWORD:
1194 args.append(name)
1195 if param.default is not param.empty:
1196 defaults += (param.default,)
1197 elif kind is _VAR_POSITIONAL:
1198 varargs = name
1199 elif kind is _KEYWORD_ONLY:
1200 kwonlyargs.append(name)
1201 if param.default is not param.empty:
1202 kwdefaults[name] = param.default
1203 elif kind is _VAR_KEYWORD:
1204 varkw = name
1205
1206 if param.annotation is not param.empty:
1207 annotations[name] = param.annotation
1208
1209 if not kwdefaults:
1210 # compatibility with 'func.__kwdefaults__'
1211 kwdefaults = None
1212
1213 if not defaults:
1214 # compatibility with 'func.__defaults__'
1215 defaults = None
1216
Pablo Galindod5d2b452019-04-30 02:01:14 +01001217 return FullArgSpec(posonlyargs + args, varargs, varkw, defaults,
1218 kwonlyargs, kwdefaults, annotations)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001219
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001220
Christian Heimes25bb7832008-01-11 16:17:00 +00001221ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
1222
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001223def getargvalues(frame):
1224 """Get information about arguments passed into a particular frame.
1225
1226 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001227 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001228 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1229 'locals' is the locals dictionary of the given frame."""
1230 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +00001231 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001232
Guido van Rossum2e65f892007-02-28 22:03:49 +00001233def formatannotation(annotation, base_module=None):
Guido van Rossum52e50042016-10-22 07:55:18 -07001234 if getattr(annotation, '__module__', None) == 'typing':
1235 return repr(annotation).replace('typing.', '')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001236 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001237 if annotation.__module__ in ('builtins', base_module):
Serhiy Storchaka521e5862014-07-22 15:00:37 +03001238 return annotation.__qualname__
1239 return annotation.__module__+'.'+annotation.__qualname__
Guido van Rossum2e65f892007-02-28 22:03:49 +00001240 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001241
Guido van Rossum2e65f892007-02-28 22:03:49 +00001242def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001243 module = getattr(object, '__module__', None)
1244 def _formatannotation(annotation):
1245 return formatannotation(annotation, module)
1246 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001247
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001248def formatargspec(args, varargs=None, varkw=None, defaults=None,
Pablo Galindod5d2b452019-04-30 02:01:14 +01001249 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001250 formatarg=str,
1251 formatvarargs=lambda name: '*' + name,
1252 formatvarkw=lambda name: '**' + name,
1253 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001254 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001255 formatannotation=formatannotation):
Berker Peksagfa3922c2015-07-31 04:11:29 +03001256 """Format an argument spec from the values returned by getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001257
Guido van Rossum2e65f892007-02-28 22:03:49 +00001258 The first seven arguments are (args, varargs, varkw, defaults,
1259 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1260 are the corresponding optional formatting functions that are called to
1261 turn names and values into strings. The last argument is an optional
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +02001262 function to format the sequence of arguments.
1263
1264 Deprecated since Python 3.5: use the `signature` function and `Signature`
1265 objects.
1266 """
1267
1268 from warnings import warn
1269
1270 warn("`formatargspec` is deprecated since Python 3.5. Use `signature` and "
Zackery Spytz41254eb2018-06-11 21:16:18 -06001271 "the `Signature` object directly",
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +02001272 DeprecationWarning,
1273 stacklevel=2)
1274
Guido van Rossum2e65f892007-02-28 22:03:49 +00001275 def formatargandannotation(arg):
1276 result = formatarg(arg)
1277 if arg in annotations:
1278 result += ': ' + formatannotation(annotations[arg])
1279 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001280 specs = []
1281 if defaults:
Pablo Galindod5d2b452019-04-30 02:01:14 +01001282 firstdefault = len(args) - len(defaults)
1283 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001284 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001285 if defaults and i >= firstdefault:
1286 spec = spec + formatvalue(defaults[i - firstdefault])
1287 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001288 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001289 specs.append(formatvarargs(formatargandannotation(varargs)))
1290 else:
1291 if kwonlyargs:
1292 specs.append('*')
1293 if kwonlyargs:
1294 for kwonlyarg in kwonlyargs:
1295 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001296 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001297 spec += formatvalue(kwonlydefaults[kwonlyarg])
1298 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001299 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001300 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001301 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001302 if 'return' in annotations:
1303 result += formatreturns(formatannotation(annotations['return']))
1304 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001305
1306def formatargvalues(args, varargs, varkw, locals,
1307 formatarg=str,
1308 formatvarargs=lambda name: '*' + name,
1309 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001310 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001311 """Format an argument spec from the 4 values returned by getargvalues.
1312
1313 The first four arguments are (args, varargs, varkw, locals). The
1314 next four arguments are the corresponding optional formatting functions
1315 that are called to turn names and values into strings. The ninth
1316 argument is an optional function to format the sequence of arguments."""
1317 def convert(name, locals=locals,
1318 formatarg=formatarg, formatvalue=formatvalue):
1319 return formatarg(name) + formatvalue(locals[name])
1320 specs = []
1321 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001322 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001323 if varargs:
1324 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1325 if varkw:
1326 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001327 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001328
Benjamin Petersone109c702011-06-24 09:37:26 -05001329def _missing_arguments(f_name, argnames, pos, values):
1330 names = [repr(name) for name in argnames if name not in values]
1331 missing = len(names)
1332 if missing == 1:
1333 s = names[0]
1334 elif missing == 2:
1335 s = "{} and {}".format(*names)
1336 else:
Yury Selivanovdccfa132014-03-27 18:42:52 -04001337 tail = ", {} and {}".format(*names[-2:])
Benjamin Petersone109c702011-06-24 09:37:26 -05001338 del names[-2:]
1339 s = ", ".join(names) + tail
1340 raise TypeError("%s() missing %i required %s argument%s: %s" %
1341 (f_name, missing,
1342 "positional" if pos else "keyword-only",
1343 "" if missing == 1 else "s", s))
1344
1345def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001346 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001347 kwonly_given = len([arg for arg in kwonly if arg in values])
1348 if varargs:
1349 plural = atleast != 1
1350 sig = "at least %d" % (atleast,)
1351 elif defcount:
1352 plural = True
1353 sig = "from %d to %d" % (atleast, len(args))
1354 else:
1355 plural = len(args) != 1
1356 sig = str(len(args))
1357 kwonly_sig = ""
1358 if kwonly_given:
1359 msg = " positional argument%s (and %d keyword-only argument%s)"
1360 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1361 "s" if kwonly_given != 1 else ""))
1362 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1363 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1364 "was" if given == 1 and not kwonly_given else "were"))
1365
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03001366def getcallargs(func, /, *positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001367 """Get the mapping of arguments to values.
1368
1369 A dict is returned, with keys the function argument names (including the
1370 names of the * and ** arguments, if any), and values the respective bound
1371 values from 'positional' and 'named'."""
1372 spec = getfullargspec(func)
Pablo Galindod5d2b452019-04-30 02:01:14 +01001373 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001374 f_name = func.__name__
1375 arg2value = {}
1376
Benjamin Petersonb204a422011-06-05 22:04:07 -05001377
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001378 if ismethod(func) and func.__self__ is not None:
1379 # implicit 'self' (or 'cls' for classmethods) argument
1380 positional = (func.__self__,) + positional
1381 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001382 num_args = len(args)
1383 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001384
Benjamin Petersonb204a422011-06-05 22:04:07 -05001385 n = min(num_pos, num_args)
1386 for i in range(n):
Pablo Galindod5d2b452019-04-30 02:01:14 +01001387 arg2value[args[i]] = positional[i]
Benjamin Petersonb204a422011-06-05 22:04:07 -05001388 if varargs:
1389 arg2value[varargs] = tuple(positional[n:])
1390 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001391 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001392 arg2value[varkw] = {}
1393 for kw, value in named.items():
1394 if kw not in possible_kwargs:
1395 if not varkw:
1396 raise TypeError("%s() got an unexpected keyword argument %r" %
1397 (f_name, kw))
1398 arg2value[varkw][kw] = value
1399 continue
1400 if kw in arg2value:
1401 raise TypeError("%s() got multiple values for argument %r" %
1402 (f_name, kw))
1403 arg2value[kw] = value
1404 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001405 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1406 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001407 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001408 req = args[:num_args - num_defaults]
1409 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001410 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001411 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001412 for i, arg in enumerate(args[num_args - num_defaults:]):
1413 if arg not in arg2value:
1414 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001415 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001416 for kwarg in kwonlyargs:
1417 if kwarg not in arg2value:
Yury Selivanov875df202014-03-27 18:23:03 -04001418 if kwonlydefaults and kwarg in kwonlydefaults:
Benjamin Petersone109c702011-06-24 09:37:26 -05001419 arg2value[kwarg] = kwonlydefaults[kwarg]
1420 else:
1421 missing += 1
1422 if missing:
1423 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001424 return arg2value
1425
Nick Coghlan2f92e542012-06-23 19:39:55 +10001426ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1427
1428def getclosurevars(func):
1429 """
1430 Get the mapping of free variables to their current values.
1431
Meador Inge8fda3592012-07-19 21:33:21 -05001432 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001433 and builtin references as seen by the body of the function. A final
1434 set of unbound names that could not be resolved is also provided.
1435 """
1436
1437 if ismethod(func):
1438 func = func.__func__
1439
1440 if not isfunction(func):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001441 raise TypeError("{!r} is not a Python function".format(func))
Nick Coghlan2f92e542012-06-23 19:39:55 +10001442
1443 code = func.__code__
1444 # Nonlocal references are named in co_freevars and resolved
1445 # by looking them up in __closure__ by positional index
1446 if func.__closure__ is None:
1447 nonlocal_vars = {}
1448 else:
1449 nonlocal_vars = {
1450 var : cell.cell_contents
1451 for var, cell in zip(code.co_freevars, func.__closure__)
1452 }
1453
1454 # Global and builtin references are named in co_names and resolved
1455 # by looking them up in __globals__ or __builtins__
1456 global_ns = func.__globals__
1457 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1458 if ismodule(builtin_ns):
1459 builtin_ns = builtin_ns.__dict__
1460 global_vars = {}
1461 builtin_vars = {}
1462 unbound_names = set()
1463 for name in code.co_names:
1464 if name in ("None", "True", "False"):
1465 # Because these used to be builtins instead of keywords, they
1466 # may still show up as name references. We ignore them.
1467 continue
1468 try:
1469 global_vars[name] = global_ns[name]
1470 except KeyError:
1471 try:
1472 builtin_vars[name] = builtin_ns[name]
1473 except KeyError:
1474 unbound_names.add(name)
1475
1476 return ClosureVars(nonlocal_vars, global_vars,
1477 builtin_vars, unbound_names)
1478
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001479# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001480
1481Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1482
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001483def getframeinfo(frame, context=1):
1484 """Get information about a frame or traceback object.
1485
1486 A tuple of five things is returned: the filename, the line number of
1487 the current line, the function name, a list of lines of context from
1488 the source code, and the index of the current line within that list.
1489 The optional second argument specifies the number of lines of context
1490 to return, which are centered around the current line."""
1491 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001492 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001493 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001494 else:
1495 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001496 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001497 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001498
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001499 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001500 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001501 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001502 try:
1503 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001504 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001505 lines = index = None
1506 else:
Raymond Hettingera0501712004-06-15 11:22:53 +00001507 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001508 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001509 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001510 else:
1511 lines = index = None
1512
Christian Heimes25bb7832008-01-11 16:17:00 +00001513 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001514
1515def getlineno(frame):
1516 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001517 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1518 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001519
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001520FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
1521
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001522def getouterframes(frame, context=1):
1523 """Get a list of records for a frame and all higher (calling) frames.
1524
1525 Each record contains a frame object, filename, line number, function
1526 name, a list of lines of context, and index within the context."""
1527 framelist = []
1528 while frame:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001529 frameinfo = (frame,) + getframeinfo(frame, context)
1530 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001531 frame = frame.f_back
1532 return framelist
1533
1534def getinnerframes(tb, context=1):
1535 """Get a list of records for a traceback's frame and all lower frames.
1536
1537 Each record contains a frame object, filename, line number, function
1538 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001539 framelist = []
1540 while tb:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001541 frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
1542 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001543 tb = tb.tb_next
1544 return framelist
1545
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001546def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001547 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001548 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001549
1550def stack(context=1):
1551 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001552 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001553
1554def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001555 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001556 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001557
1558
1559# ------------------------------------------------ static version of getattr
1560
1561_sentinel = object()
1562
Michael Foorde5162652010-11-20 16:40:44 +00001563def _static_getmro(klass):
1564 return type.__dict__['__mro__'].__get__(klass)
1565
Michael Foord95fc51d2010-11-20 15:07:30 +00001566def _check_instance(obj, attr):
1567 instance_dict = {}
1568 try:
1569 instance_dict = object.__getattribute__(obj, "__dict__")
1570 except AttributeError:
1571 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001572 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001573
1574
1575def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001576 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001577 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001578 try:
1579 return entry.__dict__[attr]
1580 except KeyError:
1581 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001582 return _sentinel
1583
Michael Foord35184ed2010-11-20 16:58:30 +00001584def _is_type(obj):
1585 try:
1586 _static_getmro(obj)
1587 except TypeError:
1588 return False
1589 return True
1590
Michael Foorddcebe0f2011-03-15 19:20:44 -04001591def _shadowed_dict(klass):
1592 dict_attr = type.__dict__["__dict__"]
1593 for entry in _static_getmro(klass):
1594 try:
1595 class_dict = dict_attr.__get__(entry)["__dict__"]
1596 except KeyError:
1597 pass
1598 else:
Inada Naoki8f9cc872019-09-05 13:07:08 +09001599 if not (type(class_dict) is types.GetSetDescriptorType and
Michael Foorddcebe0f2011-03-15 19:20:44 -04001600 class_dict.__name__ == "__dict__" and
1601 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001602 return class_dict
1603 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001604
1605def getattr_static(obj, attr, default=_sentinel):
1606 """Retrieve attributes without triggering dynamic lookup via the
1607 descriptor protocol, __getattr__ or __getattribute__.
1608
1609 Note: this function may not be able to retrieve all attributes
1610 that getattr can fetch (like dynamically created attributes)
1611 and may find attributes that getattr can't (like descriptors
1612 that raise AttributeError). It can also return descriptor objects
1613 instead of instance members in some cases. See the
1614 documentation for details.
1615 """
1616 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001617 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001618 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001619 dict_attr = _shadowed_dict(klass)
1620 if (dict_attr is _sentinel or
Inada Naoki8f9cc872019-09-05 13:07:08 +09001621 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001622 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001623 else:
1624 klass = obj
1625
1626 klass_result = _check_class(klass, attr)
1627
1628 if instance_result is not _sentinel and klass_result is not _sentinel:
1629 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1630 _check_class(type(klass_result), '__set__') is not _sentinel):
1631 return klass_result
1632
1633 if instance_result is not _sentinel:
1634 return instance_result
1635 if klass_result is not _sentinel:
1636 return klass_result
1637
1638 if obj is klass:
1639 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001640 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001641 if _shadowed_dict(type(entry)) is _sentinel:
1642 try:
1643 return entry.__dict__[attr]
1644 except KeyError:
1645 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001646 if default is not _sentinel:
1647 return default
1648 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001649
1650
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001651# ------------------------------------------------ generator introspection
1652
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001653GEN_CREATED = 'GEN_CREATED'
1654GEN_RUNNING = 'GEN_RUNNING'
1655GEN_SUSPENDED = 'GEN_SUSPENDED'
1656GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001657
1658def getgeneratorstate(generator):
1659 """Get current state of a generator-iterator.
1660
1661 Possible states are:
1662 GEN_CREATED: Waiting to start execution.
1663 GEN_RUNNING: Currently being executed by the interpreter.
1664 GEN_SUSPENDED: Currently suspended at a yield expression.
1665 GEN_CLOSED: Execution has completed.
1666 """
1667 if generator.gi_running:
1668 return GEN_RUNNING
1669 if generator.gi_frame is None:
1670 return GEN_CLOSED
1671 if generator.gi_frame.f_lasti == -1:
1672 return GEN_CREATED
1673 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001674
1675
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001676def getgeneratorlocals(generator):
1677 """
1678 Get the mapping of generator local variables to their current values.
1679
1680 A dict is returned, with the keys the local variable names and values the
1681 bound values."""
1682
1683 if not isgenerator(generator):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001684 raise TypeError("{!r} is not a Python generator".format(generator))
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001685
1686 frame = getattr(generator, "gi_frame", None)
1687 if frame is not None:
1688 return generator.gi_frame.f_locals
1689 else:
1690 return {}
1691
Yury Selivanov5376ba92015-06-22 12:19:30 -04001692
1693# ------------------------------------------------ coroutine introspection
1694
1695CORO_CREATED = 'CORO_CREATED'
1696CORO_RUNNING = 'CORO_RUNNING'
1697CORO_SUSPENDED = 'CORO_SUSPENDED'
1698CORO_CLOSED = 'CORO_CLOSED'
1699
1700def getcoroutinestate(coroutine):
1701 """Get current state of a coroutine object.
1702
1703 Possible states are:
1704 CORO_CREATED: Waiting to start execution.
1705 CORO_RUNNING: Currently being executed by the interpreter.
1706 CORO_SUSPENDED: Currently suspended at an await expression.
1707 CORO_CLOSED: Execution has completed.
1708 """
1709 if coroutine.cr_running:
1710 return CORO_RUNNING
1711 if coroutine.cr_frame is None:
1712 return CORO_CLOSED
1713 if coroutine.cr_frame.f_lasti == -1:
1714 return CORO_CREATED
1715 return CORO_SUSPENDED
1716
1717
1718def getcoroutinelocals(coroutine):
1719 """
1720 Get the mapping of coroutine local variables to their current values.
1721
1722 A dict is returned, with the keys the local variable names and values the
1723 bound values."""
1724 frame = getattr(coroutine, "cr_frame", None)
1725 if frame is not None:
1726 return frame.f_locals
1727 else:
1728 return {}
1729
1730
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001731###############################################################################
1732### Function Signature Object (PEP 362)
1733###############################################################################
1734
1735
1736_WrapperDescriptor = type(type.__call__)
1737_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001738_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001739
1740_NonUserDefinedCallables = (_WrapperDescriptor,
1741 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001742 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001743 types.BuiltinFunctionType)
1744
1745
Yury Selivanov421f0c72014-01-29 12:05:40 -05001746def _signature_get_user_defined_method(cls, method_name):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001747 """Private helper. Checks if ``cls`` has an attribute
1748 named ``method_name`` and returns it only if it is a
1749 pure python function.
1750 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001751 try:
1752 meth = getattr(cls, method_name)
1753 except AttributeError:
1754 return
1755 else:
1756 if not isinstance(meth, _NonUserDefinedCallables):
1757 # Once '__signature__' will be added to 'C'-level
1758 # callables, this check won't be necessary
1759 return meth
1760
1761
Yury Selivanov62560fb2014-01-28 12:26:24 -05001762def _signature_get_partial(wrapped_sig, partial, extra_args=()):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001763 """Private helper to calculate how 'wrapped_sig' signature will
1764 look like after applying a 'functools.partial' object (or alike)
1765 on it.
1766 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001767
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001768 old_params = wrapped_sig.parameters
Inada Naoki21105512020-03-02 18:54:49 +09001769 new_params = OrderedDict(old_params.items())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001770
1771 partial_args = partial.args or ()
1772 partial_keywords = partial.keywords or {}
1773
1774 if extra_args:
1775 partial_args = extra_args + partial_args
1776
1777 try:
1778 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1779 except TypeError as ex:
1780 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1781 raise ValueError(msg) from ex
1782
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001783
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001784 transform_to_kwonly = False
1785 for param_name, param in old_params.items():
1786 try:
1787 arg_value = ba.arguments[param_name]
1788 except KeyError:
1789 pass
1790 else:
1791 if param.kind is _POSITIONAL_ONLY:
1792 # If positional-only parameter is bound by partial,
1793 # it effectively disappears from the signature
Inada Naoki21105512020-03-02 18:54:49 +09001794 new_params.pop(param_name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001795 continue
1796
1797 if param.kind is _POSITIONAL_OR_KEYWORD:
1798 if param_name in partial_keywords:
1799 # This means that this parameter, and all parameters
1800 # after it should be keyword-only (and var-positional
1801 # should be removed). Here's why. Consider the following
1802 # function:
1803 # foo(a, b, *args, c):
1804 # pass
1805 #
1806 # "partial(foo, a='spam')" will have the following
1807 # signature: "(*, a='spam', b, c)". Because attempting
1808 # to call that partial with "(10, 20)" arguments will
1809 # raise a TypeError, saying that "a" argument received
1810 # multiple values.
1811 transform_to_kwonly = True
1812 # Set the new default value
Inada Naoki21105512020-03-02 18:54:49 +09001813 new_params[param_name] = param.replace(default=arg_value)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001814 else:
1815 # was passed as a positional argument
Inada Naoki21105512020-03-02 18:54:49 +09001816 new_params.pop(param.name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001817 continue
1818
1819 if param.kind is _KEYWORD_ONLY:
1820 # Set the new default value
Inada Naoki21105512020-03-02 18:54:49 +09001821 new_params[param_name] = param.replace(default=arg_value)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001822
1823 if transform_to_kwonly:
1824 assert param.kind is not _POSITIONAL_ONLY
1825
1826 if param.kind is _POSITIONAL_OR_KEYWORD:
Inada Naoki21105512020-03-02 18:54:49 +09001827 new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1828 new_params[param_name] = new_param
1829 new_params.move_to_end(param_name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001830 elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
Inada Naoki21105512020-03-02 18:54:49 +09001831 new_params.move_to_end(param_name)
1832 elif param.kind is _VAR_POSITIONAL:
1833 new_params.pop(param.name)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001834
1835 return wrapped_sig.replace(parameters=new_params.values())
1836
1837
Yury Selivanov62560fb2014-01-28 12:26:24 -05001838def _signature_bound_method(sig):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001839 """Private helper to transform signatures for unbound
1840 functions to bound methods.
1841 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001842
1843 params = tuple(sig.parameters.values())
1844
1845 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1846 raise ValueError('invalid method signature')
1847
1848 kind = params[0].kind
1849 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1850 # Drop first parameter:
1851 # '(p1, p2[, ...])' -> '(p2[, ...])'
1852 params = params[1:]
1853 else:
1854 if kind is not _VAR_POSITIONAL:
1855 # Unless we add a new parameter type we never
1856 # get here
1857 raise ValueError('invalid argument type')
1858 # It's a var-positional parameter.
1859 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1860
1861 return sig.replace(parameters=params)
1862
1863
Yury Selivanovb77511d2014-01-29 10:46:14 -05001864def _signature_is_builtin(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001865 """Private helper to test if `obj` is a callable that might
1866 support Argument Clinic's __text_signature__ protocol.
1867 """
Yury Selivanov1d241832014-02-02 12:51:20 -05001868 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001869 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001870 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001871 # Can't test 'isinstance(type)' here, as it would
1872 # also be True for regular python classes
1873 obj in (type, object))
1874
1875
Yury Selivanov63da7c72014-01-31 14:48:37 -05001876def _signature_is_functionlike(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001877 """Private helper to test if `obj` is a duck type of FunctionType.
1878 A good example of such objects are functions compiled with
1879 Cython, which have all attributes that a pure Python function
1880 would have, but have their code statically compiled.
1881 """
Yury Selivanov63da7c72014-01-31 14:48:37 -05001882
1883 if not callable(obj) or isclass(obj):
1884 # All function-like objects are obviously callables,
1885 # and not classes.
1886 return False
1887
1888 name = getattr(obj, '__name__', None)
1889 code = getattr(obj, '__code__', None)
1890 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
1891 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
1892 annotations = getattr(obj, '__annotations__', None)
1893
1894 return (isinstance(code, types.CodeType) and
1895 isinstance(name, str) and
1896 (defaults is None or isinstance(defaults, tuple)) and
1897 (kwdefaults is None or isinstance(kwdefaults, dict)) and
1898 isinstance(annotations, dict))
1899
1900
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001901def _signature_get_bound_param(spec):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001902 """ Private helper to get first parameter name from a
1903 __text_signature__ of a builtin method, which should
1904 be in the following format: '($param1, ...)'.
1905 Assumptions are that the first argument won't have
1906 a default value or an annotation.
1907 """
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001908
1909 assert spec.startswith('($')
1910
1911 pos = spec.find(',')
1912 if pos == -1:
1913 pos = spec.find(')')
1914
1915 cpos = spec.find(':')
1916 assert cpos == -1 or cpos > pos
1917
1918 cpos = spec.find('=')
1919 assert cpos == -1 or cpos > pos
1920
1921 return spec[2:pos]
1922
1923
Larry Hastings2623c8c2014-02-08 22:15:29 -08001924def _signature_strip_non_python_syntax(signature):
1925 """
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001926 Private helper function. Takes a signature in Argument Clinic's
1927 extended signature format.
1928
Larry Hastings2623c8c2014-02-08 22:15:29 -08001929 Returns a tuple of three things:
1930 * that signature re-rendered in standard Python syntax,
1931 * the index of the "self" parameter (generally 0), or None if
1932 the function does not have a "self" parameter, and
1933 * the index of the last "positional only" parameter,
1934 or None if the signature has no positional-only parameters.
1935 """
1936
1937 if not signature:
1938 return signature, None, None
1939
1940 self_parameter = None
1941 last_positional_only = None
1942
1943 lines = [l.encode('ascii') for l in signature.split('\n')]
1944 generator = iter(lines).__next__
1945 token_stream = tokenize.tokenize(generator)
1946
1947 delayed_comma = False
1948 skip_next_comma = False
1949 text = []
1950 add = text.append
1951
1952 current_parameter = 0
1953 OP = token.OP
1954 ERRORTOKEN = token.ERRORTOKEN
1955
1956 # token stream always starts with ENCODING token, skip it
1957 t = next(token_stream)
1958 assert t.type == tokenize.ENCODING
1959
1960 for t in token_stream:
1961 type, string = t.type, t.string
1962
1963 if type == OP:
1964 if string == ',':
1965 if skip_next_comma:
1966 skip_next_comma = False
1967 else:
1968 assert not delayed_comma
1969 delayed_comma = True
1970 current_parameter += 1
1971 continue
1972
1973 if string == '/':
1974 assert not skip_next_comma
1975 assert last_positional_only is None
1976 skip_next_comma = True
1977 last_positional_only = current_parameter - 1
1978 continue
1979
1980 if (type == ERRORTOKEN) and (string == '$'):
1981 assert self_parameter is None
1982 self_parameter = current_parameter
1983 continue
1984
1985 if delayed_comma:
1986 delayed_comma = False
1987 if not ((type == OP) and (string == ')')):
1988 add(', ')
1989 add(string)
1990 if (string == ','):
1991 add(' ')
1992 clean_signature = ''.join(text)
1993 return clean_signature, self_parameter, last_positional_only
1994
1995
Yury Selivanov57d240e2014-02-19 16:27:23 -05001996def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001997 """Private helper to parse content of '__text_signature__'
1998 and return a Signature based on it.
1999 """
INADA Naoki37420de2018-01-27 10:10:06 +09002000 # Lazy import ast because it's relatively heavy and
2001 # it's not used for other than this function.
2002 import ast
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002003
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002004 Parameter = cls._parameter_cls
2005
Larry Hastings2623c8c2014-02-08 22:15:29 -08002006 clean_signature, self_parameter, last_positional_only = \
2007 _signature_strip_non_python_syntax(s)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002008
Larry Hastings2623c8c2014-02-08 22:15:29 -08002009 program = "def foo" + clean_signature + ": pass"
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002010
2011 try:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002012 module = ast.parse(program)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002013 except SyntaxError:
2014 module = None
2015
2016 if not isinstance(module, ast.Module):
2017 raise ValueError("{!r} builtin has invalid signature".format(obj))
2018
2019 f = module.body[0]
2020
2021 parameters = []
2022 empty = Parameter.empty
2023 invalid = object()
2024
2025 module = None
2026 module_dict = {}
2027 module_name = getattr(obj, '__module__', None)
2028 if module_name:
2029 module = sys.modules.get(module_name, None)
2030 if module:
2031 module_dict = module.__dict__
INADA Naoki6f85b822018-10-05 01:47:09 +09002032 sys_module_dict = sys.modules.copy()
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002033
2034 def parse_name(node):
2035 assert isinstance(node, ast.arg)
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05302036 if node.annotation is not None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002037 raise ValueError("Annotations are not currently supported")
2038 return node.arg
2039
2040 def wrap_value(s):
2041 try:
2042 value = eval(s, module_dict)
2043 except NameError:
2044 try:
2045 value = eval(s, sys_module_dict)
2046 except NameError:
2047 raise RuntimeError()
2048
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002049 if isinstance(value, (str, int, float, bytes, bool, type(None))):
2050 return ast.Constant(value)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002051 raise RuntimeError()
2052
2053 class RewriteSymbolics(ast.NodeTransformer):
2054 def visit_Attribute(self, node):
2055 a = []
2056 n = node
2057 while isinstance(n, ast.Attribute):
2058 a.append(n.attr)
2059 n = n.value
2060 if not isinstance(n, ast.Name):
2061 raise RuntimeError()
2062 a.append(n.id)
2063 value = ".".join(reversed(a))
2064 return wrap_value(value)
2065
2066 def visit_Name(self, node):
2067 if not isinstance(node.ctx, ast.Load):
2068 raise ValueError()
2069 return wrap_value(node.id)
2070
2071 def p(name_node, default_node, default=empty):
2072 name = parse_name(name_node)
2073 if name is invalid:
2074 return None
2075 if default_node and default_node is not _empty:
2076 try:
2077 default_node = RewriteSymbolics().visit(default_node)
2078 o = ast.literal_eval(default_node)
2079 except ValueError:
2080 o = invalid
2081 if o is invalid:
2082 return None
2083 default = o if o is not invalid else default
2084 parameters.append(Parameter(name, kind, default=default, annotation=empty))
2085
2086 # non-keyword-only parameters
2087 args = reversed(f.args.args)
2088 defaults = reversed(f.args.defaults)
2089 iter = itertools.zip_longest(args, defaults, fillvalue=None)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002090 if last_positional_only is not None:
2091 kind = Parameter.POSITIONAL_ONLY
2092 else:
2093 kind = Parameter.POSITIONAL_OR_KEYWORD
2094 for i, (name, default) in enumerate(reversed(list(iter))):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002095 p(name, default)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002096 if i == last_positional_only:
2097 kind = Parameter.POSITIONAL_OR_KEYWORD
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002098
2099 # *args
2100 if f.args.vararg:
2101 kind = Parameter.VAR_POSITIONAL
2102 p(f.args.vararg, empty)
2103
2104 # keyword-only arguments
2105 kind = Parameter.KEYWORD_ONLY
2106 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
2107 p(name, default)
2108
2109 # **kwargs
2110 if f.args.kwarg:
2111 kind = Parameter.VAR_KEYWORD
2112 p(f.args.kwarg, empty)
2113
Larry Hastings2623c8c2014-02-08 22:15:29 -08002114 if self_parameter is not None:
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002115 # Possibly strip the bound argument:
2116 # - We *always* strip first bound argument if
2117 # it is a module.
2118 # - We don't strip first bound argument if
2119 # skip_bound_arg is False.
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002120 assert parameters
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002121 _self = getattr(obj, '__self__', None)
2122 self_isbound = _self is not None
2123 self_ismodule = ismodule(_self)
2124 if self_isbound and (self_ismodule or skip_bound_arg):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002125 parameters.pop(0)
2126 else:
2127 # for builtins, self parameter is always positional-only!
2128 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
2129 parameters[0] = p
2130
2131 return cls(parameters, return_annotation=cls.empty)
2132
2133
Yury Selivanov57d240e2014-02-19 16:27:23 -05002134def _signature_from_builtin(cls, func, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002135 """Private helper function to get signature for
2136 builtin callables.
2137 """
2138
Yury Selivanov57d240e2014-02-19 16:27:23 -05002139 if not _signature_is_builtin(func):
2140 raise TypeError("{!r} is not a Python builtin "
2141 "function".format(func))
2142
2143 s = getattr(func, "__text_signature__", None)
2144 if not s:
2145 raise ValueError("no signature found for builtin {!r}".format(func))
2146
2147 return _signature_fromstr(cls, func, s, skip_bound_arg)
2148
2149
Serhiy Storchakad53cf992019-05-06 22:40:27 +03002150def _signature_from_function(cls, func, skip_bound_arg=True):
Yury Selivanovcf45f022015-05-20 14:38:50 -04002151 """Private helper: constructs Signature for the given python function."""
2152
2153 is_duck_function = False
2154 if not isfunction(func):
2155 if _signature_is_functionlike(func):
2156 is_duck_function = True
2157 else:
2158 # If it's not a pure Python function, and not a duck type
2159 # of pure function:
2160 raise TypeError('{!r} is not a Python function'.format(func))
2161
Serhiy Storchakad53cf992019-05-06 22:40:27 +03002162 s = getattr(func, "__text_signature__", None)
2163 if s:
2164 return _signature_fromstr(cls, func, s, skip_bound_arg)
2165
Yury Selivanovcf45f022015-05-20 14:38:50 -04002166 Parameter = cls._parameter_cls
2167
2168 # Parameter information.
2169 func_code = func.__code__
2170 pos_count = func_code.co_argcount
2171 arg_names = func_code.co_varnames
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002172 posonly_count = func_code.co_posonlyargcount
Pablo Galindocd74e662019-06-01 18:08:04 +01002173 positional = arg_names[:pos_count]
Yury Selivanovcf45f022015-05-20 14:38:50 -04002174 keyword_only_count = func_code.co_kwonlyargcount
Pablo Galindocd74e662019-06-01 18:08:04 +01002175 keyword_only = arg_names[pos_count:pos_count + keyword_only_count]
Yury Selivanovcf45f022015-05-20 14:38:50 -04002176 annotations = func.__annotations__
2177 defaults = func.__defaults__
2178 kwdefaults = func.__kwdefaults__
2179
2180 if defaults:
2181 pos_default_count = len(defaults)
2182 else:
2183 pos_default_count = 0
2184
2185 parameters = []
2186
Pablo Galindocd74e662019-06-01 18:08:04 +01002187 non_default_count = pos_count - pos_default_count
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002188 posonly_left = posonly_count
2189
Yury Selivanovcf45f022015-05-20 14:38:50 -04002190 # Non-keyword-only parameters w/o defaults.
Pablo Galindocd74e662019-06-01 18:08:04 +01002191 for name in positional[:non_default_count]:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002192 kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD
Yury Selivanovcf45f022015-05-20 14:38:50 -04002193 annotation = annotations.get(name, _empty)
2194 parameters.append(Parameter(name, annotation=annotation,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002195 kind=kind))
2196 if posonly_left:
2197 posonly_left -= 1
Yury Selivanovcf45f022015-05-20 14:38:50 -04002198
2199 # ... w/ defaults.
Pablo Galindocd74e662019-06-01 18:08:04 +01002200 for offset, name in enumerate(positional[non_default_count:]):
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002201 kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD
Yury Selivanovcf45f022015-05-20 14:38:50 -04002202 annotation = annotations.get(name, _empty)
2203 parameters.append(Parameter(name, annotation=annotation,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002204 kind=kind,
Yury Selivanovcf45f022015-05-20 14:38:50 -04002205 default=defaults[offset]))
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002206 if posonly_left:
2207 posonly_left -= 1
Yury Selivanovcf45f022015-05-20 14:38:50 -04002208
2209 # *args
2210 if func_code.co_flags & CO_VARARGS:
Pablo Galindocd74e662019-06-01 18:08:04 +01002211 name = arg_names[pos_count + keyword_only_count]
Yury Selivanovcf45f022015-05-20 14:38:50 -04002212 annotation = annotations.get(name, _empty)
2213 parameters.append(Parameter(name, annotation=annotation,
2214 kind=_VAR_POSITIONAL))
2215
2216 # Keyword-only parameters.
2217 for name in keyword_only:
2218 default = _empty
2219 if kwdefaults is not None:
2220 default = kwdefaults.get(name, _empty)
2221
2222 annotation = annotations.get(name, _empty)
2223 parameters.append(Parameter(name, annotation=annotation,
2224 kind=_KEYWORD_ONLY,
2225 default=default))
2226 # **kwargs
2227 if func_code.co_flags & CO_VARKEYWORDS:
Pablo Galindocd74e662019-06-01 18:08:04 +01002228 index = pos_count + keyword_only_count
Yury Selivanovcf45f022015-05-20 14:38:50 -04002229 if func_code.co_flags & CO_VARARGS:
2230 index += 1
2231
2232 name = arg_names[index]
2233 annotation = annotations.get(name, _empty)
2234 parameters.append(Parameter(name, annotation=annotation,
2235 kind=_VAR_KEYWORD))
2236
2237 # Is 'func' is a pure Python function - don't validate the
2238 # parameters list (for correct order and defaults), it should be OK.
2239 return cls(parameters,
2240 return_annotation=annotations.get('return', _empty),
2241 __validate_parameters__=is_duck_function)
2242
2243
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002244def _signature_from_callable(obj, *,
2245 follow_wrapper_chains=True,
2246 skip_bound_arg=True,
2247 sigcls):
2248
2249 """Private helper function to get signature for arbitrary
2250 callable objects.
2251 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002252
2253 if not callable(obj):
2254 raise TypeError('{!r} is not a callable object'.format(obj))
2255
2256 if isinstance(obj, types.MethodType):
2257 # In this case we skip the first parameter of the underlying
2258 # function (usually `self` or `cls`).
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002259 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002260 obj.__func__,
2261 follow_wrapper_chains=follow_wrapper_chains,
2262 skip_bound_arg=skip_bound_arg,
2263 sigcls=sigcls)
2264
Yury Selivanov57d240e2014-02-19 16:27:23 -05002265 if skip_bound_arg:
2266 return _signature_bound_method(sig)
2267 else:
2268 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002269
Nick Coghlane8c45d62013-07-28 20:00:01 +10002270 # Was this function wrapped by a decorator?
Yury Selivanov57d240e2014-02-19 16:27:23 -05002271 if follow_wrapper_chains:
2272 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
Yury Selivanov46c759d2015-05-27 21:56:53 -04002273 if isinstance(obj, types.MethodType):
2274 # If the unwrapped object is a *method*, we might want to
2275 # skip its first parameter (self).
2276 # See test_signature_wrapped_bound_method for details.
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002277 return _signature_from_callable(
Yury Selivanov46c759d2015-05-27 21:56:53 -04002278 obj,
2279 follow_wrapper_chains=follow_wrapper_chains,
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002280 skip_bound_arg=skip_bound_arg,
2281 sigcls=sigcls)
Nick Coghlane8c45d62013-07-28 20:00:01 +10002282
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002283 try:
2284 sig = obj.__signature__
2285 except AttributeError:
2286 pass
2287 else:
2288 if sig is not None:
Yury Selivanov42407ab2014-06-23 10:23:50 -07002289 if not isinstance(sig, Signature):
2290 raise TypeError(
2291 'unexpected object {!r} in __signature__ '
2292 'attribute'.format(sig))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002293 return sig
2294
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002295 try:
2296 partialmethod = obj._partialmethod
2297 except AttributeError:
2298 pass
2299 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05002300 if isinstance(partialmethod, functools.partialmethod):
2301 # Unbound partialmethod (see functools.partialmethod)
2302 # This means, that we need to calculate the signature
2303 # as if it's a regular partial object, but taking into
2304 # account that the first positional argument
2305 # (usually `self`, or `cls`) will not be passed
2306 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002307
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002308 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002309 partialmethod.func,
2310 follow_wrapper_chains=follow_wrapper_chains,
2311 skip_bound_arg=skip_bound_arg,
2312 sigcls=sigcls)
2313
Yury Selivanov0486f812014-01-29 12:18:59 -05002314 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanov0486f812014-01-29 12:18:59 -05002315 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
Dong-hee Na378d7062017-05-18 04:00:51 +09002316 if first_wrapped_param.kind is Parameter.VAR_POSITIONAL:
2317 # First argument of the wrapped callable is `*args`, as in
2318 # `partialmethod(lambda *args)`.
2319 return sig
2320 else:
2321 sig_params = tuple(sig.parameters.values())
Yury Selivanov8a387212018-03-06 12:59:45 -05002322 assert (not sig_params or
2323 first_wrapped_param is not sig_params[0])
Dong-hee Na378d7062017-05-18 04:00:51 +09002324 new_params = (first_wrapped_param,) + sig_params
2325 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002326
Yury Selivanov63da7c72014-01-31 14:48:37 -05002327 if isfunction(obj) or _signature_is_functionlike(obj):
2328 # If it's a pure Python function, or an object that is duck type
2329 # of a Python function (Cython functions, for instance), then:
Serhiy Storchakad53cf992019-05-06 22:40:27 +03002330 return _signature_from_function(sigcls, obj,
2331 skip_bound_arg=skip_bound_arg)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002332
Yury Selivanova773de02014-02-21 18:30:53 -05002333 if _signature_is_builtin(obj):
Yury Selivanovda396452014-03-27 12:09:24 -04002334 return _signature_from_builtin(sigcls, obj,
Yury Selivanova773de02014-02-21 18:30:53 -05002335 skip_bound_arg=skip_bound_arg)
2336
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002337 if isinstance(obj, functools.partial):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002338 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002339 obj.func,
2340 follow_wrapper_chains=follow_wrapper_chains,
2341 skip_bound_arg=skip_bound_arg,
2342 sigcls=sigcls)
Yury Selivanov62560fb2014-01-28 12:26:24 -05002343 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002344
2345 sig = None
2346 if isinstance(obj, type):
2347 # obj is a class or a metaclass
2348
2349 # First, let's see if it has an overloaded __call__ defined
2350 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05002351 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002352 if call is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002353 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002354 call,
2355 follow_wrapper_chains=follow_wrapper_chains,
2356 skip_bound_arg=skip_bound_arg,
2357 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002358 else:
2359 # Now we check if the 'obj' class has a '__new__' method
Yury Selivanov421f0c72014-01-29 12:05:40 -05002360 new = _signature_get_user_defined_method(obj, '__new__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002361 if new is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002362 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002363 new,
2364 follow_wrapper_chains=follow_wrapper_chains,
2365 skip_bound_arg=skip_bound_arg,
2366 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002367 else:
2368 # Finally, we should have at least __init__ implemented
Yury Selivanov421f0c72014-01-29 12:05:40 -05002369 init = _signature_get_user_defined_method(obj, '__init__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002370 if init is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002371 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002372 init,
2373 follow_wrapper_chains=follow_wrapper_chains,
2374 skip_bound_arg=skip_bound_arg,
2375 sigcls=sigcls)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002376
2377 if sig is None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002378 # At this point we know, that `obj` is a class, with no user-
2379 # defined '__init__', '__new__', or class-level '__call__'
2380
Larry Hastings2623c8c2014-02-08 22:15:29 -08002381 for base in obj.__mro__[:-1]:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002382 # Since '__text_signature__' is implemented as a
2383 # descriptor that extracts text signature from the
2384 # class docstring, if 'obj' is derived from a builtin
2385 # class, its own '__text_signature__' may be 'None'.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002386 # Therefore, we go through the MRO (except the last
2387 # class in there, which is 'object') to find the first
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002388 # class with non-empty text signature.
2389 try:
2390 text_sig = base.__text_signature__
2391 except AttributeError:
2392 pass
2393 else:
2394 if text_sig:
2395 # If 'obj' class has a __text_signature__ attribute:
2396 # return a signature based on it
Yury Selivanovda396452014-03-27 12:09:24 -04002397 return _signature_fromstr(sigcls, obj, text_sig)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002398
2399 # No '__text_signature__' was found for the 'obj' class.
2400 # Last option is to check if its '__init__' is
2401 # object.__init__ or type.__init__.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002402 if type not in obj.__mro__:
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002403 # We have a class (not metaclass), but no user-defined
2404 # __init__ or __new__ for it
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002405 if (obj.__init__ is object.__init__ and
2406 obj.__new__ is object.__new__):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002407 # Return a signature of 'object' builtin.
Gregory P. Smith5b9ff7a2019-09-13 17:13:51 +01002408 return sigcls.from_callable(object)
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002409 else:
2410 raise ValueError(
2411 'no signature found for builtin type {!r}'.format(obj))
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002412
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002413 elif not isinstance(obj, _NonUserDefinedCallables):
2414 # An object with __call__
2415 # We also check that the 'obj' is not an instance of
2416 # _WrapperDescriptor or _MethodWrapper to avoid
2417 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05002418 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002419 if call is not None:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002420 try:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002421 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002422 call,
2423 follow_wrapper_chains=follow_wrapper_chains,
2424 skip_bound_arg=skip_bound_arg,
2425 sigcls=sigcls)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002426 except ValueError as ex:
2427 msg = 'no signature found for {!r}'.format(obj)
2428 raise ValueError(msg) from ex
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002429
2430 if sig is not None:
2431 # For classes and objects we skip the first parameter of their
2432 # __call__, __new__, or __init__ methods
Yury Selivanov57d240e2014-02-19 16:27:23 -05002433 if skip_bound_arg:
2434 return _signature_bound_method(sig)
2435 else:
2436 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002437
2438 if isinstance(obj, types.BuiltinFunctionType):
2439 # Raise a nicer error message for builtins
2440 msg = 'no signature found for builtin function {!r}'.format(obj)
2441 raise ValueError(msg)
2442
2443 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2444
2445
2446class _void:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002447 """A private marker - used in Parameter & Signature."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002448
2449
2450class _empty:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002451 """Marker object for Signature.empty and Parameter.empty."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002452
2453
Yury Selivanov21e83a52014-03-27 11:23:13 -04002454class _ParameterKind(enum.IntEnum):
2455 POSITIONAL_ONLY = 0
2456 POSITIONAL_OR_KEYWORD = 1
2457 VAR_POSITIONAL = 2
2458 KEYWORD_ONLY = 3
2459 VAR_KEYWORD = 4
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002460
2461 def __str__(self):
Yury Selivanov21e83a52014-03-27 11:23:13 -04002462 return self._name_
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002463
Dong-hee Na4aa30062018-06-08 12:46:31 +09002464 @property
2465 def description(self):
2466 return _PARAM_NAME_MAPPING[self]
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002467
Yury Selivanov21e83a52014-03-27 11:23:13 -04002468_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
2469_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
2470_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
2471_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
2472_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002473
Dong-hee Naa9cab432018-05-30 00:04:08 +09002474_PARAM_NAME_MAPPING = {
2475 _POSITIONAL_ONLY: 'positional-only',
2476 _POSITIONAL_OR_KEYWORD: 'positional or keyword',
2477 _VAR_POSITIONAL: 'variadic positional',
2478 _KEYWORD_ONLY: 'keyword-only',
2479 _VAR_KEYWORD: 'variadic keyword'
2480}
2481
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002482
2483class Parameter:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002484 """Represents a parameter in a function signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002485
2486 Has the following public attributes:
2487
2488 * name : str
2489 The name of the parameter as a string.
2490 * default : object
2491 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002492 parameter has no default value, this attribute is set to
2493 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002494 * annotation
2495 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002496 parameter has no annotation, this attribute is set to
2497 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002498 * kind : str
2499 Describes how argument values are bound to the parameter.
2500 Possible values: `Parameter.POSITIONAL_ONLY`,
2501 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
2502 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002503 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002504
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002505 __slots__ = ('_name', '_kind', '_default', '_annotation')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002506
2507 POSITIONAL_ONLY = _POSITIONAL_ONLY
2508 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
2509 VAR_POSITIONAL = _VAR_POSITIONAL
2510 KEYWORD_ONLY = _KEYWORD_ONLY
2511 VAR_KEYWORD = _VAR_KEYWORD
2512
2513 empty = _empty
2514
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002515 def __init__(self, name, kind, *, default=_empty, annotation=_empty):
Dong-hee Naa9cab432018-05-30 00:04:08 +09002516 try:
2517 self._kind = _ParameterKind(kind)
2518 except ValueError:
2519 raise ValueError(f'value {kind!r} is not a valid Parameter.kind')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002520 if default is not _empty:
Dong-hee Naa9cab432018-05-30 00:04:08 +09002521 if self._kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2522 msg = '{} parameters cannot have default values'
Dong-hee Na4aa30062018-06-08 12:46:31 +09002523 msg = msg.format(self._kind.description)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002524 raise ValueError(msg)
2525 self._default = default
2526 self._annotation = annotation
2527
Yury Selivanov2393dca2014-01-27 15:07:58 -05002528 if name is _empty:
2529 raise ValueError('name is a required attribute for Parameter')
2530
2531 if not isinstance(name, str):
Dong-hee Naa9cab432018-05-30 00:04:08 +09002532 msg = 'name must be a str, not a {}'.format(type(name).__name__)
2533 raise TypeError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002534
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002535 if name[0] == '.' and name[1:].isdigit():
2536 # These are implicit arguments generated by comprehensions. In
2537 # order to provide a friendlier interface to users, we recast
2538 # their name as "implicitN" and treat them as positional-only.
2539 # See issue 19611.
Dong-hee Naa9cab432018-05-30 00:04:08 +09002540 if self._kind != _POSITIONAL_OR_KEYWORD:
2541 msg = (
2542 'implicit arguments must be passed as '
2543 'positional or keyword arguments, not {}'
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002544 )
Dong-hee Na4aa30062018-06-08 12:46:31 +09002545 msg = msg.format(self._kind.description)
Dong-hee Naa9cab432018-05-30 00:04:08 +09002546 raise ValueError(msg)
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002547 self._kind = _POSITIONAL_ONLY
2548 name = 'implicit{}'.format(name[1:])
2549
Yury Selivanov2393dca2014-01-27 15:07:58 -05002550 if not name.isidentifier():
2551 raise ValueError('{!r} is not a valid parameter name'.format(name))
2552
2553 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002554
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002555 def __reduce__(self):
2556 return (type(self),
2557 (self._name, self._kind),
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002558 {'_default': self._default,
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002559 '_annotation': self._annotation})
2560
2561 def __setstate__(self, state):
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002562 self._default = state['_default']
2563 self._annotation = state['_annotation']
2564
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002565 @property
2566 def name(self):
2567 return self._name
2568
2569 @property
2570 def default(self):
2571 return self._default
2572
2573 @property
2574 def annotation(self):
2575 return self._annotation
2576
2577 @property
2578 def kind(self):
2579 return self._kind
2580
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002581 def replace(self, *, name=_void, kind=_void,
2582 annotation=_void, default=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002583 """Creates a customized copy of the Parameter."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002584
2585 if name is _void:
2586 name = self._name
2587
2588 if kind is _void:
2589 kind = self._kind
2590
2591 if annotation is _void:
2592 annotation = self._annotation
2593
2594 if default is _void:
2595 default = self._default
2596
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002597 return type(self)(name, kind, default=default, annotation=annotation)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002598
2599 def __str__(self):
2600 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002601 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002602
2603 # Add annotation and default value
2604 if self._annotation is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002605 formatted = '{}: {}'.format(formatted,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002606 formatannotation(self._annotation))
2607
2608 if self._default is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002609 if self._annotation is not _empty:
2610 formatted = '{} = {}'.format(formatted, repr(self._default))
2611 else:
2612 formatted = '{}={}'.format(formatted, repr(self._default))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002613
2614 if kind == _VAR_POSITIONAL:
2615 formatted = '*' + formatted
2616 elif kind == _VAR_KEYWORD:
2617 formatted = '**' + formatted
2618
2619 return formatted
2620
2621 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002622 return '<{} "{}">'.format(self.__class__.__name__, self)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002623
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002624 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002625 return hash((self.name, self.kind, self.annotation, self.default))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002626
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002627 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002628 if self is other:
2629 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002630 if not isinstance(other, Parameter):
2631 return NotImplemented
2632 return (self._name == other._name and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002633 self._kind == other._kind and
2634 self._default == other._default and
2635 self._annotation == other._annotation)
2636
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002637
2638class BoundArguments:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002639 """Result of `Signature.bind` call. Holds the mapping of arguments
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002640 to the function's parameters.
2641
2642 Has the following public attributes:
2643
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002644 * arguments : dict
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002645 An ordered mutable mapping of parameters' names to arguments' values.
2646 Does not contain arguments' default values.
2647 * signature : Signature
2648 The Signature object that created this instance.
2649 * args : tuple
2650 Tuple of positional arguments values.
2651 * kwargs : dict
2652 Dict of keyword arguments values.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002653 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002654
Yury Selivanov6abe0322015-05-13 17:18:41 -04002655 __slots__ = ('arguments', '_signature', '__weakref__')
2656
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002657 def __init__(self, signature, arguments):
2658 self.arguments = arguments
2659 self._signature = signature
2660
2661 @property
2662 def signature(self):
2663 return self._signature
2664
2665 @property
2666 def args(self):
2667 args = []
2668 for param_name, param in self._signature.parameters.items():
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002669 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002670 break
2671
2672 try:
2673 arg = self.arguments[param_name]
2674 except KeyError:
2675 # We're done here. Other arguments
2676 # will be mapped in 'BoundArguments.kwargs'
2677 break
2678 else:
2679 if param.kind == _VAR_POSITIONAL:
2680 # *args
2681 args.extend(arg)
2682 else:
2683 # plain argument
2684 args.append(arg)
2685
2686 return tuple(args)
2687
2688 @property
2689 def kwargs(self):
2690 kwargs = {}
2691 kwargs_started = False
2692 for param_name, param in self._signature.parameters.items():
2693 if not kwargs_started:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002694 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002695 kwargs_started = True
2696 else:
2697 if param_name not in self.arguments:
2698 kwargs_started = True
2699 continue
2700
2701 if not kwargs_started:
2702 continue
2703
2704 try:
2705 arg = self.arguments[param_name]
2706 except KeyError:
2707 pass
2708 else:
2709 if param.kind == _VAR_KEYWORD:
2710 # **kwargs
2711 kwargs.update(arg)
2712 else:
2713 # plain keyword argument
2714 kwargs[param_name] = arg
2715
2716 return kwargs
2717
Yury Selivanovb907a512015-05-16 13:45:09 -04002718 def apply_defaults(self):
2719 """Set default values for missing arguments.
2720
2721 For variable-positional arguments (*args) the default is an
2722 empty tuple.
2723
2724 For variable-keyword arguments (**kwargs) the default is an
2725 empty dict.
2726 """
2727 arguments = self.arguments
Yury Selivanovb907a512015-05-16 13:45:09 -04002728 new_arguments = []
2729 for name, param in self._signature.parameters.items():
2730 try:
2731 new_arguments.append((name, arguments[name]))
2732 except KeyError:
2733 if param.default is not _empty:
2734 val = param.default
2735 elif param.kind is _VAR_POSITIONAL:
2736 val = ()
2737 elif param.kind is _VAR_KEYWORD:
2738 val = {}
2739 else:
2740 # This BoundArguments was likely produced by
2741 # Signature.bind_partial().
2742 continue
2743 new_arguments.append((name, val))
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002744 self.arguments = dict(new_arguments)
Yury Selivanovb907a512015-05-16 13:45:09 -04002745
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002746 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002747 if self is other:
2748 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002749 if not isinstance(other, BoundArguments):
2750 return NotImplemented
2751 return (self.signature == other.signature and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002752 self.arguments == other.arguments)
2753
Yury Selivanov6abe0322015-05-13 17:18:41 -04002754 def __setstate__(self, state):
2755 self._signature = state['_signature']
2756 self.arguments = state['arguments']
2757
2758 def __getstate__(self):
2759 return {'_signature': self._signature, 'arguments': self.arguments}
2760
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002761 def __repr__(self):
2762 args = []
2763 for arg, value in self.arguments.items():
2764 args.append('{}={!r}'.format(arg, value))
Yury Selivanovf229bc52015-05-15 12:53:56 -04002765 return '<{} ({})>'.format(self.__class__.__name__, ', '.join(args))
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002766
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002767
2768class Signature:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002769 """A Signature object represents the overall signature of a function.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002770 It stores a Parameter object for each parameter accepted by the
2771 function, as well as information specific to the function itself.
2772
2773 A Signature object has the following public attributes and methods:
2774
Jens Reidel611836a2020-03-18 03:22:46 +01002775 * parameters : OrderedDict
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002776 An ordered mapping of parameters' names to the corresponding
2777 Parameter objects (keyword-only arguments are in the same order
2778 as listed in `code.co_varnames`).
2779 * return_annotation : object
2780 The annotation for the return type of the function if specified.
2781 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002782 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002783 * bind(*args, **kwargs) -> BoundArguments
2784 Creates a mapping from positional and keyword arguments to
2785 parameters.
2786 * bind_partial(*args, **kwargs) -> BoundArguments
2787 Creates a partial mapping from positional and keyword arguments
2788 to parameters (simulating 'functools.partial' behavior.)
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002789 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002790
2791 __slots__ = ('_return_annotation', '_parameters')
2792
2793 _parameter_cls = Parameter
2794 _bound_arguments_cls = BoundArguments
2795
2796 empty = _empty
2797
2798 def __init__(self, parameters=None, *, return_annotation=_empty,
2799 __validate_parameters__=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002800 """Constructs Signature from the given list of Parameter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002801 objects and 'return_annotation'. All arguments are optional.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002802 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002803
2804 if parameters is None:
Jens Reidel611836a2020-03-18 03:22:46 +01002805 params = OrderedDict()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002806 else:
2807 if __validate_parameters__:
Jens Reidel611836a2020-03-18 03:22:46 +01002808 params = OrderedDict()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002809 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002810 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002811
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002812 for param in parameters:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002813 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002814 name = param.name
2815
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002816 if kind < top_kind:
Dong-hee Naa9cab432018-05-30 00:04:08 +09002817 msg = (
2818 'wrong parameter order: {} parameter before {} '
2819 'parameter'
2820 )
Dong-hee Na4aa30062018-06-08 12:46:31 +09002821 msg = msg.format(top_kind.description,
2822 kind.description)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002823 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002824 elif kind > top_kind:
2825 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002826 top_kind = kind
2827
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002828 if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
Yury Selivanov07a9e452014-01-29 10:58:16 -05002829 if param.default is _empty:
2830 if kind_defaults:
2831 # No default for this parameter, but the
2832 # previous parameter of the same kind had
2833 # a default
2834 msg = 'non-default argument follows default ' \
2835 'argument'
2836 raise ValueError(msg)
2837 else:
2838 # There is a default for this parameter.
2839 kind_defaults = True
2840
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002841 if name in params:
2842 msg = 'duplicate parameter name: {!r}'.format(name)
2843 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002844
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002845 params[name] = param
2846 else:
Jens Reidel611836a2020-03-18 03:22:46 +01002847 params = OrderedDict((param.name, param) for param in parameters)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002848
2849 self._parameters = types.MappingProxyType(params)
2850 self._return_annotation = return_annotation
2851
2852 @classmethod
2853 def from_function(cls, func):
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002854 """Constructs Signature for the given python function.
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002855
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002856 Deprecated since Python 3.5, use `Signature.from_callable()`.
2857 """
2858
2859 warnings.warn("inspect.Signature.from_function() is deprecated since "
2860 "Python 3.5, use Signature.from_callable()",
Berker Peksagb5601582015-05-21 23:40:54 +03002861 DeprecationWarning, stacklevel=2)
Yury Selivanovcf45f022015-05-20 14:38:50 -04002862 return _signature_from_function(cls, func)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002863
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002864 @classmethod
2865 def from_builtin(cls, func):
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002866 """Constructs Signature for the given builtin function.
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002867
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002868 Deprecated since Python 3.5, use `Signature.from_callable()`.
2869 """
2870
2871 warnings.warn("inspect.Signature.from_builtin() is deprecated since "
2872 "Python 3.5, use Signature.from_callable()",
Berker Peksagb5601582015-05-21 23:40:54 +03002873 DeprecationWarning, stacklevel=2)
Yury Selivanov57d240e2014-02-19 16:27:23 -05002874 return _signature_from_builtin(cls, func)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002875
Yury Selivanovda396452014-03-27 12:09:24 -04002876 @classmethod
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002877 def from_callable(cls, obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002878 """Constructs Signature for the given callable object."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002879 return _signature_from_callable(obj, sigcls=cls,
2880 follow_wrapper_chains=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04002881
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002882 @property
2883 def parameters(self):
2884 return self._parameters
2885
2886 @property
2887 def return_annotation(self):
2888 return self._return_annotation
2889
2890 def replace(self, *, parameters=_void, return_annotation=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002891 """Creates a customized copy of the Signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002892 Pass 'parameters' and/or 'return_annotation' arguments
2893 to override them in the new copy.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002894 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002895
2896 if parameters is _void:
2897 parameters = self.parameters.values()
2898
2899 if return_annotation is _void:
2900 return_annotation = self._return_annotation
2901
2902 return type(self)(parameters,
2903 return_annotation=return_annotation)
2904
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002905 def _hash_basis(self):
2906 params = tuple(param for param in self.parameters.values()
2907 if param.kind != _KEYWORD_ONLY)
2908
2909 kwo_params = {param.name: param for param in self.parameters.values()
2910 if param.kind == _KEYWORD_ONLY}
2911
2912 return params, kwo_params, self.return_annotation
2913
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002914 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002915 params, kwo_params, return_annotation = self._hash_basis()
2916 kwo_params = frozenset(kwo_params.values())
2917 return hash((params, kwo_params, return_annotation))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002918
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002919 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002920 if self is other:
2921 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002922 if not isinstance(other, Signature):
2923 return NotImplemented
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002924 return self._hash_basis() == other._hash_basis()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002925
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002926 def _bind(self, args, kwargs, *, partial=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002927 """Private method. Don't use directly."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002928
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002929 arguments = {}
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002930
2931 parameters = iter(self.parameters.values())
2932 parameters_ex = ()
2933 arg_vals = iter(args)
2934
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002935 while True:
2936 # Let's iterate through the positional arguments and corresponding
2937 # parameters
2938 try:
2939 arg_val = next(arg_vals)
2940 except StopIteration:
2941 # No more positional arguments
2942 try:
2943 param = next(parameters)
2944 except StopIteration:
2945 # No more parameters. That's it. Just need to check that
2946 # we have no `kwargs` after this while loop
2947 break
2948 else:
2949 if param.kind == _VAR_POSITIONAL:
2950 # That's OK, just empty *args. Let's start parsing
2951 # kwargs
2952 break
2953 elif param.name in kwargs:
2954 if param.kind == _POSITIONAL_ONLY:
2955 msg = '{arg!r} parameter is positional only, ' \
2956 'but was passed as a keyword'
2957 msg = msg.format(arg=param.name)
2958 raise TypeError(msg) from None
2959 parameters_ex = (param,)
2960 break
2961 elif (param.kind == _VAR_KEYWORD or
2962 param.default is not _empty):
2963 # That's fine too - we have a default value for this
2964 # parameter. So, lets start parsing `kwargs`, starting
2965 # with the current parameter
2966 parameters_ex = (param,)
2967 break
2968 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002969 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
2970 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002971 if partial:
2972 parameters_ex = (param,)
2973 break
2974 else:
Yury Selivanov86872752015-05-19 00:27:49 -04002975 msg = 'missing a required argument: {arg!r}'
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002976 msg = msg.format(arg=param.name)
2977 raise TypeError(msg) from None
2978 else:
2979 # We have a positional argument to process
2980 try:
2981 param = next(parameters)
2982 except StopIteration:
2983 raise TypeError('too many positional arguments') from None
2984 else:
2985 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
2986 # Looks like we have no parameter for this positional
2987 # argument
Yury Selivanov86872752015-05-19 00:27:49 -04002988 raise TypeError(
2989 'too many positional arguments') from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002990
2991 if param.kind == _VAR_POSITIONAL:
2992 # We have an '*args'-like argument, let's fill it with
2993 # all positional arguments we have left and move on to
2994 # the next phase
2995 values = [arg_val]
2996 values.extend(arg_vals)
2997 arguments[param.name] = tuple(values)
2998 break
2999
Pablo Galindof3ef06a2019-10-15 12:40:02 +01003000 if param.name in kwargs and param.kind != _POSITIONAL_ONLY:
Yury Selivanov86872752015-05-19 00:27:49 -04003001 raise TypeError(
3002 'multiple values for argument {arg!r}'.format(
3003 arg=param.name)) from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003004
3005 arguments[param.name] = arg_val
3006
3007 # Now, we iterate through the remaining parameters to process
3008 # keyword arguments
3009 kwargs_param = None
3010 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003011 if param.kind == _VAR_KEYWORD:
3012 # Memorize that we have a '**kwargs'-like parameter
3013 kwargs_param = param
3014 continue
3015
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05003016 if param.kind == _VAR_POSITIONAL:
3017 # Named arguments don't refer to '*args'-like parameters.
3018 # We only arrive here if the positional arguments ended
3019 # before reaching the last parameter before *args.
3020 continue
3021
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003022 param_name = param.name
3023 try:
3024 arg_val = kwargs.pop(param_name)
3025 except KeyError:
3026 # We have no value for this parameter. It's fine though,
3027 # if it has a default value, or it is an '*args'-like
3028 # parameter, left alone by the processing of positional
3029 # arguments.
3030 if (not partial and param.kind != _VAR_POSITIONAL and
3031 param.default is _empty):
Yury Selivanov86872752015-05-19 00:27:49 -04003032 raise TypeError('missing a required argument: {arg!r}'. \
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003033 format(arg=param_name)) from None
3034
3035 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05003036 if param.kind == _POSITIONAL_ONLY:
3037 # This should never happen in case of a properly built
3038 # Signature object (but let's have this check here
3039 # to ensure correct behaviour just in case)
3040 raise TypeError('{arg!r} parameter is positional only, '
3041 'but was passed as a keyword'. \
3042 format(arg=param.name))
3043
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003044 arguments[param_name] = arg_val
3045
3046 if kwargs:
3047 if kwargs_param is not None:
3048 # Process our '**kwargs'-like parameter
3049 arguments[kwargs_param.name] = kwargs
3050 else:
Yury Selivanov86872752015-05-19 00:27:49 -04003051 raise TypeError(
3052 'got an unexpected keyword argument {arg!r}'.format(
3053 arg=next(iter(kwargs))))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003054
3055 return self._bound_arguments_cls(self, arguments)
3056
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003057 def bind(self, /, *args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003058 """Get a BoundArguments object, that maps the passed `args`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003059 and `kwargs` to the function's signature. Raises `TypeError`
3060 if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003061 """
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003062 return self._bind(args, kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003063
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003064 def bind_partial(self, /, *args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003065 """Get a BoundArguments object, that partially maps the
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003066 passed `args` and `kwargs` to the function's signature.
3067 Raises `TypeError` if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003068 """
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003069 return self._bind(args, kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003070
Yury Selivanova5d63dd2014-03-27 11:31:43 -04003071 def __reduce__(self):
3072 return (type(self),
3073 (tuple(self._parameters.values()),),
3074 {'_return_annotation': self._return_annotation})
3075
3076 def __setstate__(self, state):
3077 self._return_annotation = state['_return_annotation']
3078
Yury Selivanov374375d2014-03-27 12:41:53 -04003079 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04003080 return '<{} {}>'.format(self.__class__.__name__, self)
Yury Selivanov374375d2014-03-27 12:41:53 -04003081
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003082 def __str__(self):
3083 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05003084 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003085 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05003086 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003087 formatted = str(param)
3088
3089 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05003090
3091 if kind == _POSITIONAL_ONLY:
3092 render_pos_only_separator = True
3093 elif render_pos_only_separator:
3094 # It's not a positional-only parameter, and the flag
3095 # is set to 'True' (there were pos-only params before.)
3096 result.append('/')
3097 render_pos_only_separator = False
3098
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003099 if kind == _VAR_POSITIONAL:
3100 # OK, we have an '*args'-like parameter, so we won't need
3101 # a '*' to separate keyword-only arguments
3102 render_kw_only_separator = False
3103 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
3104 # We have a keyword-only parameter to render and we haven't
3105 # rendered an '*args'-like parameter before, so add a '*'
3106 # separator to the parameters list ("foo(arg1, *, arg2)" case)
3107 result.append('*')
3108 # This condition should be only triggered once, so
3109 # reset the flag
3110 render_kw_only_separator = False
3111
3112 result.append(formatted)
3113
Yury Selivanov2393dca2014-01-27 15:07:58 -05003114 if render_pos_only_separator:
3115 # There were only positional-only parameters, hence the
3116 # flag was not reset to 'False'
3117 result.append('/')
3118
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003119 rendered = '({})'.format(', '.join(result))
3120
3121 if self.return_annotation is not _empty:
3122 anno = formatannotation(self.return_annotation)
3123 rendered += ' -> {}'.format(anno)
3124
3125 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003126
Yury Selivanovda396452014-03-27 12:09:24 -04003127
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04003128def signature(obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003129 """Get a signature object for the passed callable."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04003130 return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04003131
3132
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003133def _main():
3134 """ Logic for inspecting an object given at command line """
3135 import argparse
3136 import importlib
3137
3138 parser = argparse.ArgumentParser()
3139 parser.add_argument(
3140 'object',
3141 help="The object to be analysed. "
3142 "It supports the 'module:qualname' syntax")
3143 parser.add_argument(
3144 '-d', '--details', action='store_true',
3145 help='Display info about the module rather than its source code')
3146
3147 args = parser.parse_args()
3148
3149 target = args.object
3150 mod_name, has_attrs, attrs = target.partition(":")
3151 try:
3152 obj = module = importlib.import_module(mod_name)
3153 except Exception as exc:
3154 msg = "Failed to import {} ({}: {})".format(mod_name,
3155 type(exc).__name__,
3156 exc)
3157 print(msg, file=sys.stderr)
Alan Yeee3c59a72019-09-09 07:15:43 -07003158 sys.exit(2)
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003159
3160 if has_attrs:
3161 parts = attrs.split(".")
3162 obj = module
3163 for part in parts:
3164 obj = getattr(obj, part)
3165
3166 if module.__name__ in sys.builtin_module_names:
3167 print("Can't get info for builtin modules.", file=sys.stderr)
Alan Yeee3c59a72019-09-09 07:15:43 -07003168 sys.exit(1)
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003169
3170 if args.details:
3171 print('Target: {}'.format(target))
3172 print('Origin: {}'.format(getsourcefile(module)))
3173 print('Cached: {}'.format(module.__cached__))
3174 if obj is module:
3175 print('Loader: {}'.format(repr(module.__loader__)))
3176 if hasattr(module, '__path__'):
3177 print('Submodule search path: {}'.format(module.__path__))
3178 else:
3179 try:
3180 __, lineno = findsource(obj)
3181 except Exception:
3182 pass
3183 else:
3184 print('Line: {}'.format(lineno))
3185
3186 print('\n')
3187 else:
3188 print(getsource(obj))
3189
3190
3191if __name__ == "__main__":
3192 _main()