blob: ac127cbd725b9b0339ae1d39ad7078e88b3e63c4 [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
Batuhan Taskaya044a1042020-10-06 23:03:02 +030048import typing
Brett Cannon2b88fcf2012-06-02 22:28:42 -040049import warnings
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070050import functools
Nick Coghlan2f92e542012-06-23 19:39:55 +100051import builtins
Raymond Hettingera1a992c2005-03-11 06:46:45 +000052from operator import attrgetter
Inada Naoki21105512020-03-02 18:54:49 +090053from collections import namedtuple, OrderedDict
Nick Coghlan09c81232010-08-17 10:18:16 +000054
55# Create constants for the compiler flags in Include/code.h
Antoine Pitroua8723a02015-04-15 00:41:29 +020056# We try to get them from dis to avoid duplication
57mod_dict = globals()
58for k, v in dis.COMPILER_FLAG_NAMES.items():
59 mod_dict["CO_" + v] = k
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000060
Christian Heimesbe5b30b2008-03-03 19:18:51 +000061# See Include/object.h
62TPFLAGS_IS_ABSTRACT = 1 << 20
63
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000064# ----------------------------------------------------------- type-checking
65def ismodule(object):
66 """Return true if the object is a module.
67
68 Module objects provide these attributes:
Barry Warsaw28a691b2010-04-17 00:19:56 +000069 __cached__ pathname to byte compiled file
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000070 __doc__ documentation string
71 __file__ filename (missing for built-in modules)"""
Tim Peters28bc59f2001-09-16 08:40:16 +000072 return isinstance(object, types.ModuleType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000073
74def isclass(object):
75 """Return true if the object is a class.
76
77 Class objects provide these attributes:
78 __doc__ documentation string
79 __module__ name of module in which this class was defined"""
Benjamin Petersonc4656002009-01-17 22:41:18 +000080 return isinstance(object, type)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000081
82def ismethod(object):
83 """Return true if the object is an instance method.
84
85 Instance method objects provide these attributes:
86 __doc__ documentation string
87 __name__ name with which this method was defined
Christian Heimesff737952007-11-27 10:40:20 +000088 __func__ function object containing implementation of method
89 __self__ instance to which this method is bound"""
Tim Peters28bc59f2001-09-16 08:40:16 +000090 return isinstance(object, types.MethodType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000091
Tim Peters536d2262001-09-20 05:13:38 +000092def ismethoddescriptor(object):
Tim Petersf1d90b92001-09-20 05:47:55 +000093 """Return true if the object is a method descriptor.
94
95 But not if ismethod() or isclass() or isfunction() are true.
Tim Peters536d2262001-09-20 05:13:38 +000096
97 This is new in Python 2.2, and, for example, is true of int.__add__.
98 An object passing this test has a __get__ attribute but not a __set__
99 attribute, but beyond that the set of attributes varies. __name__ is
100 usually sensible, and __doc__ often is.
101
Tim Petersf1d90b92001-09-20 05:47:55 +0000102 Methods implemented via descriptors that also pass one of the other
103 tests return false from the ismethoddescriptor() test, simply because
104 the other tests promise more -- you can, e.g., count on having the
Christian Heimesff737952007-11-27 10:40:20 +0000105 __func__ attribute (etc) when an object passes ismethod()."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100106 if isclass(object) or ismethod(object) or isfunction(object):
107 # mutual exclusion
108 return False
109 tp = type(object)
110 return hasattr(tp, "__get__") and not hasattr(tp, "__set__")
Tim Peters536d2262001-09-20 05:13:38 +0000111
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000112def isdatadescriptor(object):
113 """Return true if the object is a data descriptor.
114
Aaron Hall, MBA4054b172018-05-20 19:46:42 -0400115 Data descriptors have a __set__ or a __delete__ attribute. Examples are
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000116 properties (defined in Python) and getsets and members (defined in C).
117 Typically, data descriptors will also have __name__ and __doc__ attributes
118 (properties, getsets, and members have both of these attributes), but this
119 is not guaranteed."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100120 if isclass(object) or ismethod(object) or isfunction(object):
121 # mutual exclusion
122 return False
123 tp = type(object)
Aaron Hall, MBA4054b172018-05-20 19:46:42 -0400124 return hasattr(tp, "__set__") or hasattr(tp, "__delete__")
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000125
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000126if hasattr(types, 'MemberDescriptorType'):
127 # CPython and equivalent
128 def ismemberdescriptor(object):
129 """Return true if the object is a member descriptor.
130
131 Member descriptors are specialized descriptors defined in extension
132 modules."""
133 return isinstance(object, types.MemberDescriptorType)
134else:
135 # Other implementations
136 def ismemberdescriptor(object):
137 """Return true if the object is a member descriptor.
138
139 Member descriptors are specialized descriptors defined in extension
140 modules."""
141 return False
142
143if hasattr(types, 'GetSetDescriptorType'):
144 # CPython and equivalent
145 def isgetsetdescriptor(object):
146 """Return true if the object is a getset descriptor.
147
148 getset descriptors are specialized descriptors defined in extension
149 modules."""
150 return isinstance(object, types.GetSetDescriptorType)
151else:
152 # Other implementations
153 def isgetsetdescriptor(object):
154 """Return true if the object is a getset descriptor.
155
156 getset descriptors are specialized descriptors defined in extension
157 modules."""
158 return False
159
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000160def isfunction(object):
161 """Return true if the object is a user-defined function.
162
163 Function objects provide these attributes:
164 __doc__ documentation string
165 __name__ name with which this function was defined
Neal Norwitz221085d2007-02-25 20:55:47 +0000166 __code__ code object containing compiled function bytecode
167 __defaults__ tuple of any default values for arguments
168 __globals__ global namespace in which this function was defined
169 __annotations__ dict of parameter annotations
170 __kwdefaults__ dict of keyword only parameters with defaults"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000171 return isinstance(object, types.FunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000172
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200173def _has_code_flag(f, flag):
174 """Return true if ``f`` is a function (or a method or functools.partial
175 wrapper wrapping a function) whose code object has the given ``flag``
176 set in its flags."""
177 while ismethod(f):
178 f = f.__func__
179 f = functools._unwrap_partial(f)
180 if not isfunction(f):
181 return False
182 return bool(f.__code__.co_flags & flag)
183
Pablo Galindo7cd25432018-10-26 12:19:14 +0100184def isgeneratorfunction(obj):
Christian Heimes7131fd92008-02-19 14:21:46 +0000185 """Return true if the object is a user-defined generator function.
186
Martin Panter0f0eac42016-09-07 11:04:41 +0000187 Generator function objects provide the same attributes as functions.
188 See help(isfunction) for a list of attributes."""
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200189 return _has_code_flag(obj, CO_GENERATOR)
Yury Selivanov75445082015-05-11 22:57:16 -0400190
Pablo Galindo7cd25432018-10-26 12:19:14 +0100191def iscoroutinefunction(obj):
Yury Selivanov75445082015-05-11 22:57:16 -0400192 """Return true if the object is a coroutine function.
193
Yury Selivanov4778e132016-11-08 12:23:09 -0500194 Coroutine functions are defined with "async def" syntax.
Yury Selivanov75445082015-05-11 22:57:16 -0400195 """
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200196 return _has_code_flag(obj, CO_COROUTINE)
Yury Selivanov75445082015-05-11 22:57:16 -0400197
Pablo Galindo7cd25432018-10-26 12:19:14 +0100198def isasyncgenfunction(obj):
Yury Selivanov4778e132016-11-08 12:23:09 -0500199 """Return true if the object is an asynchronous generator function.
200
201 Asynchronous generator functions are defined with "async def"
202 syntax and have "yield" expressions in their body.
203 """
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200204 return _has_code_flag(obj, CO_ASYNC_GENERATOR)
Yury Selivanoveb636452016-09-08 22:01:51 -0700205
206def isasyncgen(object):
Yury Selivanov4778e132016-11-08 12:23:09 -0500207 """Return true if the object is an asynchronous generator."""
Yury Selivanoveb636452016-09-08 22:01:51 -0700208 return isinstance(object, types.AsyncGeneratorType)
209
Christian Heimes7131fd92008-02-19 14:21:46 +0000210def isgenerator(object):
211 """Return true if the object is a generator.
212
213 Generator objects provide these attributes:
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300214 __iter__ defined to support iteration over container
Christian Heimes7131fd92008-02-19 14:21:46 +0000215 close raises a new GeneratorExit exception inside the
216 generator to terminate the iteration
217 gi_code code object
218 gi_frame frame object or possibly None once the generator has
219 been exhausted
220 gi_running set to 1 when generator is executing, 0 otherwise
221 next return the next item from the container
222 send resumes the generator and "sends" a value that becomes
223 the result of the current yield-expression
224 throw used to raise an exception inside the generator"""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400225 return isinstance(object, types.GeneratorType)
Yury Selivanov75445082015-05-11 22:57:16 -0400226
227def iscoroutine(object):
228 """Return true if the object is a coroutine."""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400229 return isinstance(object, types.CoroutineType)
Christian Heimes7131fd92008-02-19 14:21:46 +0000230
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400231def isawaitable(object):
Yury Selivanovc0215df2016-11-08 19:57:44 -0500232 """Return true if object can be passed to an ``await`` expression."""
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400233 return (isinstance(object, types.CoroutineType) or
234 isinstance(object, types.GeneratorType) and
Yury Selivanovc0215df2016-11-08 19:57:44 -0500235 bool(object.gi_code.co_flags & CO_ITERABLE_COROUTINE) or
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400236 isinstance(object, collections.abc.Awaitable))
237
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000238def istraceback(object):
239 """Return true if the object is a traceback.
240
241 Traceback objects provide these attributes:
242 tb_frame frame object at this level
243 tb_lasti index of last attempted instruction in bytecode
244 tb_lineno current line number in Python source code
245 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000246 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000247
248def isframe(object):
249 """Return true if the object is a frame object.
250
251 Frame objects provide these attributes:
252 f_back next outer frame object (this frame's caller)
253 f_builtins built-in namespace seen by this frame
254 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000255 f_globals global namespace seen by this frame
256 f_lasti index of last attempted instruction in bytecode
257 f_lineno current line number in Python source code
258 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000259 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000260 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000261
262def iscode(object):
263 """Return true if the object is a code object.
264
265 Code objects provide these attributes:
Xiang Zhanga6902e62017-04-13 10:38:28 +0800266 co_argcount number of arguments (not including *, ** args
267 or keyword only arguments)
268 co_code string of raw compiled bytecode
269 co_cellvars tuple of names of cell variables
270 co_consts tuple of constants used in the bytecode
271 co_filename name of file in which this code object was created
272 co_firstlineno number of first line in Python source code
273 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
274 | 16=nested | 32=generator | 64=nofree | 128=coroutine
275 | 256=iterable_coroutine | 512=async_generator
276 co_freevars tuple of names of free variables
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100277 co_posonlyargcount number of positional only arguments
Xiang Zhanga6902e62017-04-13 10:38:28 +0800278 co_kwonlyargcount number of keyword only arguments (not including ** arg)
279 co_lnotab encoded mapping of line numbers to bytecode indices
280 co_name name with which this code object was defined
281 co_names tuple of names of local variables
282 co_nlocals number of local variables
283 co_stacksize virtual machine stack space required
284 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000285 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000286
287def isbuiltin(object):
288 """Return true if the object is a built-in function or method.
289
290 Built-in functions and methods provide these attributes:
291 __doc__ documentation string
292 __name__ original name of this function or method
293 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000294 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000295
296def isroutine(object):
297 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000298 return (isbuiltin(object)
299 or isfunction(object)
300 or ismethod(object)
301 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000302
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000303def isabstract(object):
304 """Return true if the object is an abstract base class (ABC)."""
Natefcfe80e2017-04-24 10:06:15 -0700305 if not isinstance(object, type):
306 return False
307 if object.__flags__ & TPFLAGS_IS_ABSTRACT:
308 return True
309 if not issubclass(type(object), abc.ABCMeta):
310 return False
311 if hasattr(object, '__abstractmethods__'):
312 # It looks like ABCMeta.__new__ has finished running;
313 # TPFLAGS_IS_ABSTRACT should have been accurate.
314 return False
315 # It looks like ABCMeta.__new__ has not finished running yet; we're
316 # probably in __init_subclass__. We'll look for abstractmethods manually.
317 for name, value in object.__dict__.items():
318 if getattr(value, "__isabstractmethod__", False):
319 return True
320 for base in object.__bases__:
321 for name in getattr(base, "__abstractmethods__", ()):
322 value = getattr(object, name, None)
323 if getattr(value, "__isabstractmethod__", False):
324 return True
325 return False
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000326
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000327def getmembers(object, predicate=None):
328 """Return all members of an object as (name, value) pairs sorted by name.
329 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100330 if isclass(object):
331 mro = (object,) + getmro(object)
332 else:
333 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000334 results = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700335 processed = set()
336 names = dir(object)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700337 # :dd any DynamicClassAttributes to the list of names if object is a class;
Ethan Furmane03ea372013-09-25 07:14:41 -0700338 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700339 # attribute with the same name as a DynamicClassAttribute exists
Ethan Furmane03ea372013-09-25 07:14:41 -0700340 try:
341 for base in object.__bases__:
342 for k, v in base.__dict__.items():
343 if isinstance(v, types.DynamicClassAttribute):
344 names.append(k)
345 except AttributeError:
346 pass
347 for key in names:
Ethan Furman63c141c2013-10-18 00:27:39 -0700348 # First try to get the value via getattr. Some descriptors don't
349 # like calling their __get__ (see bug #1785), so fall back to
350 # looking in the __dict__.
351 try:
352 value = getattr(object, key)
353 # handle the duplicate key
354 if key in processed:
355 raise AttributeError
356 except AttributeError:
357 for base in mro:
358 if key in base.__dict__:
359 value = base.__dict__[key]
360 break
361 else:
362 # could be a (currently) missing slot member, or a buggy
363 # __dir__; discard and move on
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100364 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000365 if not predicate or predicate(value):
366 results.append((key, value))
Ethan Furmane03ea372013-09-25 07:14:41 -0700367 processed.add(key)
368 results.sort(key=lambda pair: pair[0])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000369 return results
370
Christian Heimes25bb7832008-01-11 16:17:00 +0000371Attribute = namedtuple('Attribute', 'name kind defining_class object')
372
Tim Peters13b49d32001-09-23 02:00:29 +0000373def classify_class_attrs(cls):
374 """Return list of attribute-descriptor tuples.
375
376 For each name in dir(cls), the return list contains a 4-tuple
377 with these elements:
378
379 0. The name (a string).
380
381 1. The kind of attribute this is, one of these strings:
382 'class method' created via classmethod()
383 'static method' created via staticmethod()
384 'property' created via property()
Ethan Furmane03ea372013-09-25 07:14:41 -0700385 'method' any other flavor of method or descriptor
Tim Peters13b49d32001-09-23 02:00:29 +0000386 'data' not a method
387
388 2. The class which defined this attribute (a class).
389
Ethan Furmane03ea372013-09-25 07:14:41 -0700390 3. The object as obtained by calling getattr; if this fails, or if the
391 resulting object does not live anywhere in the class' mro (including
392 metaclasses) then the object is looked up in the defining class's
393 dict (found by walking the mro).
Ethan Furman668dede2013-09-14 18:53:26 -0700394
395 If one of the items in dir(cls) is stored in the metaclass it will now
396 be discovered and not have None be listed as the class in which it was
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700397 defined. Any items whose home class cannot be discovered are skipped.
Tim Peters13b49d32001-09-23 02:00:29 +0000398 """
399
400 mro = getmro(cls)
Ethan Furman668dede2013-09-14 18:53:26 -0700401 metamro = getmro(type(cls)) # for attributes stored in the metaclass
Jon Dufresne39726282017-05-18 07:35:54 -0700402 metamro = tuple(cls for cls in metamro if cls not in (type, object))
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700403 class_bases = (cls,) + mro
404 all_bases = class_bases + metamro
Tim Peters13b49d32001-09-23 02:00:29 +0000405 names = dir(cls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700406 # :dd any DynamicClassAttributes to the list of names;
Ethan Furmane03ea372013-09-25 07:14:41 -0700407 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700408 # attribute with the same name as a DynamicClassAttribute exists.
Ethan Furman63c141c2013-10-18 00:27:39 -0700409 for base in mro:
Ethan Furmane03ea372013-09-25 07:14:41 -0700410 for k, v in base.__dict__.items():
411 if isinstance(v, types.DynamicClassAttribute):
412 names.append(k)
Tim Peters13b49d32001-09-23 02:00:29 +0000413 result = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700414 processed = set()
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700415
Tim Peters13b49d32001-09-23 02:00:29 +0000416 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100417 # Get the object associated with the name, and where it was defined.
Ethan Furmane03ea372013-09-25 07:14:41 -0700418 # Normal objects will be looked up with both getattr and directly in
419 # its class' dict (in case getattr fails [bug #1785], and also to look
420 # for a docstring).
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700421 # For DynamicClassAttributes on the second pass we only look in the
Ethan Furmane03ea372013-09-25 07:14:41 -0700422 # class's dict.
423 #
Tim Peters13b49d32001-09-23 02:00:29 +0000424 # Getting an obj from the __dict__ sometimes reveals more than
425 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100426 homecls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700427 get_obj = None
428 dict_obj = None
Ethan Furmane03ea372013-09-25 07:14:41 -0700429 if name not in processed:
430 try:
Ethan Furmana8b07072013-10-18 01:22:08 -0700431 if name == '__dict__':
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700432 raise Exception("__dict__ is special, don't want the proxy")
Ethan Furmane03ea372013-09-25 07:14:41 -0700433 get_obj = getattr(cls, name)
434 except Exception as exc:
435 pass
436 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700437 homecls = getattr(get_obj, "__objclass__", homecls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700438 if homecls not in class_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700439 # if the resulting object does not live somewhere in the
Ethan Furman63c141c2013-10-18 00:27:39 -0700440 # mro, drop it and search the mro manually
Ethan Furmane03ea372013-09-25 07:14:41 -0700441 homecls = None
Ethan Furman63c141c2013-10-18 00:27:39 -0700442 last_cls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700443 # first look in the classes
444 for srch_cls in class_bases:
Ethan Furman63c141c2013-10-18 00:27:39 -0700445 srch_obj = getattr(srch_cls, name, None)
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400446 if srch_obj is get_obj:
Ethan Furman63c141c2013-10-18 00:27:39 -0700447 last_cls = srch_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700448 # then check the metaclasses
449 for srch_cls in metamro:
450 try:
451 srch_obj = srch_cls.__getattr__(cls, name)
452 except AttributeError:
453 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400454 if srch_obj is get_obj:
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700455 last_cls = srch_cls
Ethan Furman63c141c2013-10-18 00:27:39 -0700456 if last_cls is not None:
457 homecls = last_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700458 for base in all_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700459 if name in base.__dict__:
460 dict_obj = base.__dict__[name]
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700461 if homecls not in metamro:
462 homecls = base
Ethan Furmane03ea372013-09-25 07:14:41 -0700463 break
Ethan Furman63c141c2013-10-18 00:27:39 -0700464 if homecls is None:
465 # unable to locate the attribute anywhere, most likely due to
466 # buggy custom __dir__; discard and move on
467 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400468 obj = get_obj if get_obj is not None else dict_obj
Ethan Furmane03ea372013-09-25 07:14:41 -0700469 # Classify the object or its descriptor.
Serhiy Storchaka3327a2d2017-12-15 14:13:41 +0200470 if isinstance(dict_obj, (staticmethod, types.BuiltinMethodType)):
Tim Peters13b49d32001-09-23 02:00:29 +0000471 kind = "static method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700472 obj = dict_obj
Serhiy Storchaka3327a2d2017-12-15 14:13:41 +0200473 elif isinstance(dict_obj, (classmethod, types.ClassMethodDescriptorType)):
Tim Peters13b49d32001-09-23 02:00:29 +0000474 kind = "class method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700475 obj = dict_obj
476 elif isinstance(dict_obj, property):
Tim Peters13b49d32001-09-23 02:00:29 +0000477 kind = "property"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700478 obj = dict_obj
Yury Selivanov0860a0b2014-01-31 14:28:44 -0500479 elif isroutine(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000480 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100481 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700482 kind = "data"
Christian Heimes25bb7832008-01-11 16:17:00 +0000483 result.append(Attribute(name, kind, homecls, obj))
Ethan Furmane03ea372013-09-25 07:14:41 -0700484 processed.add(name)
Tim Peters13b49d32001-09-23 02:00:29 +0000485 return result
486
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000487# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000488
489def getmro(cls):
490 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000491 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000492
Nick Coghlane8c45d62013-07-28 20:00:01 +1000493# -------------------------------------------------------- function helpers
494
495def unwrap(func, *, stop=None):
496 """Get the object wrapped by *func*.
497
498 Follows the chain of :attr:`__wrapped__` attributes returning the last
499 object in the chain.
500
501 *stop* is an optional callback accepting an object in the wrapper chain
502 as its sole argument that allows the unwrapping to be terminated early if
503 the callback returns a true value. If the callback never returns a true
504 value, the last object in the chain is returned as usual. For example,
505 :func:`signature` uses this to stop unwrapping if any object in the
506 chain has a ``__signature__`` attribute defined.
507
508 :exc:`ValueError` is raised if a cycle is encountered.
509
510 """
511 if stop is None:
512 def _is_wrapper(f):
513 return hasattr(f, '__wrapped__')
514 else:
515 def _is_wrapper(f):
516 return hasattr(f, '__wrapped__') and not stop(f)
517 f = func # remember the original func for error reporting
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100518 # Memoise by id to tolerate non-hashable objects, but store objects to
519 # ensure they aren't destroyed, which would allow their IDs to be reused.
520 memo = {id(f): f}
521 recursion_limit = sys.getrecursionlimit()
Nick Coghlane8c45d62013-07-28 20:00:01 +1000522 while _is_wrapper(func):
523 func = func.__wrapped__
524 id_func = id(func)
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100525 if (id_func in memo) or (len(memo) >= recursion_limit):
Nick Coghlane8c45d62013-07-28 20:00:01 +1000526 raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100527 memo[id_func] = func
Nick Coghlane8c45d62013-07-28 20:00:01 +1000528 return func
529
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000530# -------------------------------------------------- source code extraction
531def indentsize(line):
532 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000533 expline = line.expandtabs()
534 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000535
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300536def _findclass(func):
537 cls = sys.modules.get(func.__module__)
538 if cls is None:
539 return None
540 for name in func.__qualname__.split('.')[:-1]:
541 cls = getattr(cls, name)
542 if not isclass(cls):
543 return None
544 return cls
545
546def _finddoc(obj):
Serhiy Storchaka08b47c32020-05-18 20:25:07 +0300547 if isclass(obj):
548 for base in obj.__mro__:
549 if base is not object:
550 try:
551 doc = base.__doc__
552 except AttributeError:
553 continue
554 if doc is not None:
555 return doc
556 return None
557
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300558 if ismethod(obj):
559 name = obj.__func__.__name__
560 self = obj.__self__
561 if (isclass(self) and
562 getattr(getattr(self, name, None), '__func__') is obj.__func__):
563 # classmethod
564 cls = self
565 else:
566 cls = self.__class__
567 elif isfunction(obj):
568 name = obj.__name__
569 cls = _findclass(obj)
570 if cls is None or getattr(cls, name) is not obj:
571 return None
572 elif isbuiltin(obj):
573 name = obj.__name__
574 self = obj.__self__
575 if (isclass(self) and
576 self.__qualname__ + '.' + name == obj.__qualname__):
577 # classmethod
578 cls = self
579 else:
580 cls = self.__class__
Serhiy Storchakaac4bdcc2015-10-29 08:15:50 +0200581 # Should be tested before isdatadescriptor().
582 elif isinstance(obj, property):
583 func = obj.fget
584 name = func.__name__
585 cls = _findclass(func)
586 if cls is None or getattr(cls, name) is not obj:
587 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300588 elif ismethoddescriptor(obj) or isdatadescriptor(obj):
589 name = obj.__name__
590 cls = obj.__objclass__
591 if getattr(cls, name) is not obj:
592 return None
Raymond Hettingerd1e768a2019-03-25 13:01:13 -0700593 if ismemberdescriptor(obj):
594 slots = getattr(cls, '__slots__', None)
595 if isinstance(slots, dict) and name in slots:
596 return slots[name]
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300597 else:
598 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300599 for base in cls.__mro__:
600 try:
Serhiy Storchaka08b47c32020-05-18 20:25:07 +0300601 doc = getattr(base, name).__doc__
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300602 except AttributeError:
603 continue
604 if doc is not None:
605 return doc
606 return None
607
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000608def getdoc(object):
609 """Get the documentation string for an object.
610
611 All tabs are expanded to spaces. To clean up docstrings that are
612 indented to line up with blocks of code, any whitespace than can be
613 uniformly removed from the second line onwards is removed."""
Serhiy Storchaka08b47c32020-05-18 20:25:07 +0300614 try:
615 doc = object.__doc__
616 except AttributeError:
617 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300618 if doc is None:
619 try:
620 doc = _finddoc(object)
621 except (AttributeError, TypeError):
622 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000623 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000624 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000625 return cleandoc(doc)
626
627def cleandoc(doc):
628 """Clean up indentation from docstrings.
629
630 Any whitespace that can be uniformly removed from the second line
631 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000632 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000633 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000634 except UnicodeError:
635 return None
636 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000637 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000638 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000639 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000640 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000641 if content:
642 indent = len(line) - content
643 margin = min(margin, indent)
644 # Remove indentation.
645 if lines:
646 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000647 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000648 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000649 # Remove any trailing or leading blank lines.
650 while lines and not lines[-1]:
651 lines.pop()
652 while lines and not lines[0]:
653 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000654 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000655
656def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000657 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000658 if ismodule(object):
Jason R. Coombsb9650a02018-03-05 18:29:08 -0500659 if getattr(object, '__file__', None):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000660 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000661 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000662 if isclass(object):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500663 if hasattr(object, '__module__'):
Philipp Ad407d2a2019-06-08 14:05:46 +0200664 module = sys.modules.get(object.__module__)
665 if getattr(module, '__file__', None):
666 return module.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000667 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000668 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000669 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000670 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000671 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000672 if istraceback(object):
673 object = object.tb_frame
674 if isframe(object):
675 object = object.f_code
676 if iscode(object):
677 return object.co_filename
Thomas Kluyvere968bc732017-10-24 13:42:36 +0100678 raise TypeError('module, class, method, function, traceback, frame, or '
679 'code object was expected, got {}'.format(
680 type(object).__name__))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000681
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000682def getmodulename(path):
683 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000684 fname = os.path.basename(path)
685 # Check for paths that look like an actual module file
686 suffixes = [(-len(suffix), suffix)
687 for suffix in importlib.machinery.all_suffixes()]
688 suffixes.sort() # try longest suffixes first, in case they overlap
689 for neglen, suffix in suffixes:
690 if fname.endswith(suffix):
691 return fname[:neglen]
692 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000693
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000694def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000695 """Return the filename that can be used to locate an object's source.
696 Return None if no way can be identified to get the source.
697 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000698 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400699 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
700 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
701 if any(filename.endswith(s) for s in all_bytecode_suffixes):
702 filename = (os.path.splitext(filename)[0] +
703 importlib.machinery.SOURCE_SUFFIXES[0])
704 elif any(filename.endswith(s) for s in
705 importlib.machinery.EXTENSION_SUFFIXES):
706 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000707 if os.path.exists(filename):
708 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000709 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400710 if getattr(getmodule(object, filename), '__loader__', None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000711 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000712 # or it is in the linecache
713 if filename in linecache.cache:
714 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000715
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000716def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000717 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000718
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000719 The idea is for each object to have a unique origin, so this routine
720 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000721 if _filename is None:
722 _filename = getsourcefile(object) or getfile(object)
723 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000724
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000725modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000726_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000727
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000728def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000729 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000730 if ismodule(object):
731 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000732 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000733 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000734 # Try the filename to modulename cache
735 if _filename is not None and _filename in modulesbyfile:
736 return sys.modules.get(modulesbyfile[_filename])
737 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000738 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000739 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000740 except TypeError:
741 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000742 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000743 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000744 # Update the filename to module name cache and check yet again
745 # Copy sys.modules in order to cope with changes while iterating
Gregory P. Smith85cf1d52020-03-04 16:45:22 -0800746 for modname, module in sys.modules.copy().items():
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000747 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000748 f = module.__file__
749 if f == _filesbymodname.get(modname, None):
750 # Have already mapped this module, so skip it
751 continue
752 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000753 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000754 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000755 modulesbyfile[f] = modulesbyfile[
756 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000757 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000758 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000759 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000760 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000761 if not hasattr(object, '__name__'):
762 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000763 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000764 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000765 if mainobject is object:
766 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000767 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000768 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000769 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000770 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000771 if builtinobject is object:
772 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000773
Karthikeyan Singaravelan696136b2020-04-18 21:49:32 +0530774
775class ClassFoundException(Exception):
776 pass
777
778
779class _ClassFinder(ast.NodeVisitor):
780
781 def __init__(self, qualname):
782 self.stack = []
783 self.qualname = qualname
784
785 def visit_FunctionDef(self, node):
786 self.stack.append(node.name)
787 self.stack.append('<locals>')
788 self.generic_visit(node)
789 self.stack.pop()
790 self.stack.pop()
791
792 visit_AsyncFunctionDef = visit_FunctionDef
793
794 def visit_ClassDef(self, node):
795 self.stack.append(node.name)
796 if self.qualname == '.'.join(self.stack):
797 # Return the decorator for the class if present
798 if node.decorator_list:
799 line_number = node.decorator_list[0].lineno
800 else:
801 line_number = node.lineno
802
803 # decrement by one since lines starts with indexing by zero
804 line_number -= 1
805 raise ClassFoundException(line_number)
806 self.generic_visit(node)
807 self.stack.pop()
808
809
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000810def findsource(object):
811 """Return the entire source file and starting line number for an object.
812
813 The argument may be a module, class, method, function, traceback, frame,
814 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200815 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000816 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500817
Yury Selivanovef1e7502014-12-08 16:05:34 -0500818 file = getsourcefile(object)
819 if file:
820 # Invalidate cache if needed.
821 linecache.checkcache(file)
822 else:
823 file = getfile(object)
824 # Allow filenames in form of "<something>" to pass through.
825 # `doctest` monkeypatches `linecache` module to enable
826 # inspection, so let `linecache.getlines` to be called.
827 if not (file.startswith('<') and file.endswith('>')):
828 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500829
Thomas Wouters89f507f2006-12-13 04:49:30 +0000830 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000831 if module:
832 lines = linecache.getlines(file, module.__dict__)
833 else:
834 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000835 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200836 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000837
838 if ismodule(object):
839 return lines, 0
840
841 if isclass(object):
Karthikeyan Singaravelan696136b2020-04-18 21:49:32 +0530842 qualname = object.__qualname__
843 source = ''.join(lines)
844 tree = ast.parse(source)
845 class_finder = _ClassFinder(qualname)
846 try:
847 class_finder.visit(tree)
848 except ClassFoundException as e:
849 line_number = e.args[0]
850 return lines, line_number
Jeremy Hyltonab919022003-06-27 18:41:20 +0000851 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200852 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000853
854 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000855 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000856 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000857 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000858 if istraceback(object):
859 object = object.tb_frame
860 if isframe(object):
861 object = object.f_code
862 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000863 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200864 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000865 lnum = object.co_firstlineno - 1
Yury Selivanove4e811d2015-07-21 19:01:52 +0300866 pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000867 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000868 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000869 lnum = lnum - 1
870 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200871 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000872
873def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000874 """Get lines of comments immediately preceding an object's source code.
875
876 Returns None when source can't be found.
877 """
878 try:
879 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200880 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000881 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000882
883 if ismodule(object):
884 # Look for a comment block at the top of the file.
885 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000886 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000887 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000888 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000889 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000890 comments = []
891 end = start
892 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000893 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000894 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000895 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000896
897 # Look for a preceding block of comments at the same indentation.
898 elif lnum > 0:
899 indent = indentsize(lines[lnum])
900 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000901 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000902 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000903 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000904 if end > 0:
905 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000906 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000907 while comment[:1] == '#' and indentsize(lines[end]) == indent:
908 comments[:0] = [comment]
909 end = end - 1
910 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000911 comment = lines[end].expandtabs().lstrip()
912 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000913 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000914 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000915 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000916 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000917
Tim Peters4efb6e92001-06-29 23:51:08 +0000918class EndOfBlock(Exception): pass
919
920class BlockFinder:
921 """Provide a tokeneater() method to detect the end of a code block."""
922 def __init__(self):
923 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000924 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000925 self.started = False
926 self.passline = False
Meador Inge5b718d72015-07-23 22:49:37 -0500927 self.indecorator = False
928 self.decoratorhasargs = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000929 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000930
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000931 def tokeneater(self, type, token, srowcol, erowcol, line):
Meador Inge5b718d72015-07-23 22:49:37 -0500932 if not self.started and not self.indecorator:
933 # skip any decorators
934 if token == "@":
935 self.indecorator = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000936 # look for the first "def", "class" or "lambda"
Meador Inge5b718d72015-07-23 22:49:37 -0500937 elif token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000938 if token == "lambda":
939 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000940 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000941 self.passline = True # skip to the end of the line
Meador Inge5b718d72015-07-23 22:49:37 -0500942 elif token == "(":
943 if self.indecorator:
944 self.decoratorhasargs = True
945 elif token == ")":
946 if self.indecorator:
947 self.indecorator = False
948 self.decoratorhasargs = False
Tim Peters4efb6e92001-06-29 23:51:08 +0000949 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000950 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000951 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000952 if self.islambda: # lambdas always end at the first NEWLINE
953 raise EndOfBlock
Meador Inge5b718d72015-07-23 22:49:37 -0500954 # hitting a NEWLINE when in a decorator without args
955 # ends the decorator
956 if self.indecorator and not self.decoratorhasargs:
957 self.indecorator = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000958 elif self.passline:
959 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000960 elif type == tokenize.INDENT:
961 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000962 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000963 elif type == tokenize.DEDENT:
964 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000965 # the end of matching indent/dedent pairs end a block
966 # (note that this only works for "def"/"class" blocks,
967 # not e.g. for "if: else:" or "try: finally:" blocks)
968 if self.indent <= 0:
969 raise EndOfBlock
970 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
971 # any other token on the same indentation level end the previous
972 # block as well, except the pseudo-tokens COMMENT and NL.
973 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000974
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000975def getblock(lines):
976 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000977 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000978 try:
Trent Nelson428de652008-03-18 22:41:35 +0000979 tokens = tokenize.generate_tokens(iter(lines).__next__)
980 for _token in tokens:
981 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000982 except (EndOfBlock, IndentationError):
983 pass
984 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000985
986def getsourcelines(object):
987 """Return a list of source lines and starting line number for an object.
988
989 The argument may be a module, class, method, function, traceback, frame,
990 or code object. The source code is returned as a list of the lines
991 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200992 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000993 raised if the source code cannot be retrieved."""
Yury Selivanov081bbf62014-09-26 17:34:54 -0400994 object = unwrap(object)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000995 lines, lnum = findsource(object)
996
Vladimir Matveev91cb2982018-08-24 07:18:00 -0700997 if istraceback(object):
998 object = object.tb_frame
999
1000 # for module or frame that corresponds to module, return all source lines
1001 if (ismodule(object) or
1002 (isframe(object) and object.f_code.co_name == "<module>")):
Meador Inge5b718d72015-07-23 22:49:37 -05001003 return lines, 0
1004 else:
1005 return getblock(lines[lnum:]), lnum + 1
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001006
1007def getsource(object):
1008 """Return the text of the source code for an object.
1009
1010 The argument may be a module, class, method, function, traceback, frame,
1011 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001012 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001013 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001014 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001015
1016# --------------------------------------------------- class tree extraction
1017def walktree(classes, children, parent):
1018 """Recursive helper function for getclasstree()."""
1019 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +00001020 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001021 for c in classes:
1022 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +00001023 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001024 results.append(walktree(children[c], children, c))
1025 return results
1026
Georg Brandl5ce83a02009-06-01 17:23:51 +00001027def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001028 """Arrange the given list of classes into a hierarchy of nested lists.
1029
1030 Where a nested list appears, it contains classes derived from the class
1031 whose entry immediately precedes the list. Each entry is a 2-tuple
1032 containing a class and a tuple of its base classes. If the 'unique'
1033 argument is true, exactly one entry appears in the returned structure
1034 for each class in the given list. Otherwise, classes using multiple
1035 inheritance and their descendants will appear multiple times."""
1036 children = {}
1037 roots = []
1038 for c in classes:
1039 if c.__bases__:
1040 for parent in c.__bases__:
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05301041 if parent not in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001042 children[parent] = []
Serhiy Storchaka362c1b52013-09-05 17:14:32 +03001043 if c not in children[parent]:
1044 children[parent].append(c)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001045 if unique and parent in classes: break
1046 elif c not in roots:
1047 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +00001048 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001049 if parent not in classes:
1050 roots.append(parent)
1051 return walktree(roots, children, None)
1052
1053# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001054Arguments = namedtuple('Arguments', 'args, varargs, varkw')
1055
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001056def getargs(co):
1057 """Get information about the arguments accepted by a code object.
1058
Guido van Rossum2e65f892007-02-28 22:03:49 +00001059 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001060 'args' is the list of argument names. Keyword-only arguments are
1061 appended. 'varargs' and 'varkw' are the names of the * and **
1062 arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +00001063 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001064 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001065
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001066 names = co.co_varnames
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001067 nargs = co.co_argcount
Guido van Rossum2e65f892007-02-28 22:03:49 +00001068 nkwargs = co.co_kwonlyargcount
Pablo Galindocd74e662019-06-01 18:08:04 +01001069 args = list(names[:nargs])
1070 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001071 step = 0
1072
Guido van Rossum2e65f892007-02-28 22:03:49 +00001073 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001074 varargs = None
1075 if co.co_flags & CO_VARARGS:
1076 varargs = co.co_varnames[nargs]
1077 nargs = nargs + 1
1078 varkw = None
1079 if co.co_flags & CO_VARKEYWORDS:
1080 varkw = co.co_varnames[nargs]
Pablo Galindocd74e662019-06-01 18:08:04 +01001081 return Arguments(args + kwonlyargs, varargs, varkw)
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001082
1083ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
1084
1085def getargspec(func):
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001086 """Get the names and default values of a function's parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001087
1088 A tuple of four things is returned: (args, varargs, keywords, defaults).
1089 'args' is a list of the argument names, including keyword-only argument names.
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001090 'varargs' and 'keywords' are the names of the * and ** parameters or None.
1091 'defaults' is an n-tuple of the default values of the last n parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001092
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001093 This function is deprecated, as it does not support annotations or
1094 keyword-only parameters and will raise ValueError if either is present
1095 on the supplied callable.
1096
1097 For a more structured introspection API, use inspect.signature() instead.
1098
1099 Alternatively, use getfullargspec() for an API with a similar namedtuple
1100 based interface, but full support for annotations and keyword-only
1101 parameters.
Matthias Bussonnierded87d82018-10-19 16:40:45 -07001102
1103 Deprecated since Python 3.5, use `inspect.getfullargspec()`.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001104 """
Matthias Bussonnierded87d82018-10-19 16:40:45 -07001105 warnings.warn("inspect.getargspec() is deprecated since Python 3.0, "
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001106 "use inspect.signature() or inspect.getfullargspec()",
1107 DeprecationWarning, stacklevel=2)
Pablo Galindod5d2b452019-04-30 02:01:14 +01001108 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
1109 getfullargspec(func)
1110 if kwonlyargs or ann:
1111 raise ValueError("Function has keyword-only parameters or annotations"
1112 ", use inspect.signature() API which can support them")
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001113 return ArgSpec(args, varargs, varkw, defaults)
1114
Christian Heimes25bb7832008-01-11 16:17:00 +00001115FullArgSpec = namedtuple('FullArgSpec',
Pablo Galindod5d2b452019-04-30 02:01:14 +01001116 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001117
1118def getfullargspec(func):
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001119 """Get the names and default values of a callable object's parameters.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001120
Brett Cannon504d8852007-09-07 02:12:14 +00001121 A tuple of seven things is returned:
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001122 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations).
1123 'args' is a list of the parameter names.
1124 'varargs' and 'varkw' are the names of the * and ** parameters or None.
1125 'defaults' is an n-tuple of the default values of the last n parameters.
1126 'kwonlyargs' is a list of keyword-only parameter names.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001127 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001128 'annotations' is a dictionary mapping parameter names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001129
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001130 Notable differences from inspect.signature():
1131 - the "self" parameter is always reported, even for bound methods
1132 - wrapper chains defined by __wrapped__ *not* unwrapped automatically
Jeremy Hylton64967882003-06-27 18:14:39 +00001133 """
Yury Selivanov57d240e2014-02-19 16:27:23 -05001134 try:
1135 # Re: `skip_bound_arg=False`
1136 #
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001137 # There is a notable difference in behaviour between getfullargspec
1138 # and Signature: the former always returns 'self' parameter for bound
1139 # methods, whereas the Signature always shows the actual calling
1140 # signature of the passed object.
1141 #
1142 # To simulate this behaviour, we "unbind" bound methods, to trick
1143 # inspect.signature to always return their first parameter ("self",
1144 # usually)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001145
Yury Selivanov57d240e2014-02-19 16:27:23 -05001146 # Re: `follow_wrapper_chains=False`
1147 #
1148 # getfullargspec() historically ignored __wrapped__ attributes,
1149 # so we ensure that remains the case in 3.3+
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001150
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001151 sig = _signature_from_callable(func,
1152 follow_wrapper_chains=False,
1153 skip_bound_arg=False,
1154 sigcls=Signature)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001155 except Exception as ex:
1156 # Most of the times 'signature' will raise ValueError.
1157 # But, it can also raise AttributeError, and, maybe something
1158 # else. So to be fully backwards compatible, we catch all
1159 # possible exceptions here, and reraise a TypeError.
1160 raise TypeError('unsupported callable') from ex
1161
1162 args = []
1163 varargs = None
1164 varkw = None
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001165 posonlyargs = []
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001166 kwonlyargs = []
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001167 annotations = {}
1168 defaults = ()
1169 kwdefaults = {}
1170
1171 if sig.return_annotation is not sig.empty:
1172 annotations['return'] = sig.return_annotation
1173
1174 for param in sig.parameters.values():
1175 kind = param.kind
1176 name = param.name
1177
1178 if kind is _POSITIONAL_ONLY:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001179 posonlyargs.append(name)
1180 if param.default is not param.empty:
1181 defaults += (param.default,)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001182 elif kind is _POSITIONAL_OR_KEYWORD:
1183 args.append(name)
1184 if param.default is not param.empty:
1185 defaults += (param.default,)
1186 elif kind is _VAR_POSITIONAL:
1187 varargs = name
1188 elif kind is _KEYWORD_ONLY:
1189 kwonlyargs.append(name)
1190 if param.default is not param.empty:
1191 kwdefaults[name] = param.default
1192 elif kind is _VAR_KEYWORD:
1193 varkw = name
1194
1195 if param.annotation is not param.empty:
1196 annotations[name] = param.annotation
1197
1198 if not kwdefaults:
1199 # compatibility with 'func.__kwdefaults__'
1200 kwdefaults = None
1201
1202 if not defaults:
1203 # compatibility with 'func.__defaults__'
1204 defaults = None
1205
Pablo Galindod5d2b452019-04-30 02:01:14 +01001206 return FullArgSpec(posonlyargs + args, varargs, varkw, defaults,
1207 kwonlyargs, kwdefaults, annotations)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001208
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001209
Christian Heimes25bb7832008-01-11 16:17:00 +00001210ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
1211
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001212def getargvalues(frame):
1213 """Get information about arguments passed into a particular frame.
1214
1215 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001216 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001217 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1218 'locals' is the locals dictionary of the given frame."""
1219 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +00001220 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001221
Guido van Rossum2e65f892007-02-28 22:03:49 +00001222def formatannotation(annotation, base_module=None):
Guido van Rossum52e50042016-10-22 07:55:18 -07001223 if getattr(annotation, '__module__', None) == 'typing':
1224 return repr(annotation).replace('typing.', '')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001225 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001226 if annotation.__module__ in ('builtins', base_module):
Serhiy Storchaka521e5862014-07-22 15:00:37 +03001227 return annotation.__qualname__
1228 return annotation.__module__+'.'+annotation.__qualname__
Guido van Rossum2e65f892007-02-28 22:03:49 +00001229 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001230
Guido van Rossum2e65f892007-02-28 22:03:49 +00001231def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001232 module = getattr(object, '__module__', None)
1233 def _formatannotation(annotation):
1234 return formatannotation(annotation, module)
1235 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001236
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001237def formatargspec(args, varargs=None, varkw=None, defaults=None,
Pablo Galindod5d2b452019-04-30 02:01:14 +01001238 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001239 formatarg=str,
1240 formatvarargs=lambda name: '*' + name,
1241 formatvarkw=lambda name: '**' + name,
1242 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001243 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001244 formatannotation=formatannotation):
Berker Peksagfa3922c2015-07-31 04:11:29 +03001245 """Format an argument spec from the values returned by getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001246
Guido van Rossum2e65f892007-02-28 22:03:49 +00001247 The first seven arguments are (args, varargs, varkw, defaults,
1248 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1249 are the corresponding optional formatting functions that are called to
1250 turn names and values into strings. The last argument is an optional
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +02001251 function to format the sequence of arguments.
1252
1253 Deprecated since Python 3.5: use the `signature` function and `Signature`
1254 objects.
1255 """
1256
1257 from warnings import warn
1258
1259 warn("`formatargspec` is deprecated since Python 3.5. Use `signature` and "
Zackery Spytz41254eb2018-06-11 21:16:18 -06001260 "the `Signature` object directly",
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +02001261 DeprecationWarning,
1262 stacklevel=2)
1263
Guido van Rossum2e65f892007-02-28 22:03:49 +00001264 def formatargandannotation(arg):
1265 result = formatarg(arg)
1266 if arg in annotations:
1267 result += ': ' + formatannotation(annotations[arg])
1268 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001269 specs = []
1270 if defaults:
Pablo Galindod5d2b452019-04-30 02:01:14 +01001271 firstdefault = len(args) - len(defaults)
1272 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001273 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001274 if defaults and i >= firstdefault:
1275 spec = spec + formatvalue(defaults[i - firstdefault])
1276 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001277 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001278 specs.append(formatvarargs(formatargandannotation(varargs)))
1279 else:
1280 if kwonlyargs:
1281 specs.append('*')
1282 if kwonlyargs:
1283 for kwonlyarg in kwonlyargs:
1284 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001285 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001286 spec += formatvalue(kwonlydefaults[kwonlyarg])
1287 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001288 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001289 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001290 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001291 if 'return' in annotations:
1292 result += formatreturns(formatannotation(annotations['return']))
1293 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001294
1295def formatargvalues(args, varargs, varkw, locals,
1296 formatarg=str,
1297 formatvarargs=lambda name: '*' + name,
1298 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001299 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001300 """Format an argument spec from the 4 values returned by getargvalues.
1301
1302 The first four arguments are (args, varargs, varkw, locals). The
1303 next four arguments are the corresponding optional formatting functions
1304 that are called to turn names and values into strings. The ninth
1305 argument is an optional function to format the sequence of arguments."""
1306 def convert(name, locals=locals,
1307 formatarg=formatarg, formatvalue=formatvalue):
1308 return formatarg(name) + formatvalue(locals[name])
1309 specs = []
1310 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001311 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001312 if varargs:
1313 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1314 if varkw:
1315 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001316 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001317
Benjamin Petersone109c702011-06-24 09:37:26 -05001318def _missing_arguments(f_name, argnames, pos, values):
1319 names = [repr(name) for name in argnames if name not in values]
1320 missing = len(names)
1321 if missing == 1:
1322 s = names[0]
1323 elif missing == 2:
1324 s = "{} and {}".format(*names)
1325 else:
Yury Selivanovdccfa132014-03-27 18:42:52 -04001326 tail = ", {} and {}".format(*names[-2:])
Benjamin Petersone109c702011-06-24 09:37:26 -05001327 del names[-2:]
1328 s = ", ".join(names) + tail
1329 raise TypeError("%s() missing %i required %s argument%s: %s" %
1330 (f_name, missing,
1331 "positional" if pos else "keyword-only",
1332 "" if missing == 1 else "s", s))
1333
1334def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001335 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001336 kwonly_given = len([arg for arg in kwonly if arg in values])
1337 if varargs:
1338 plural = atleast != 1
1339 sig = "at least %d" % (atleast,)
1340 elif defcount:
1341 plural = True
1342 sig = "from %d to %d" % (atleast, len(args))
1343 else:
1344 plural = len(args) != 1
1345 sig = str(len(args))
1346 kwonly_sig = ""
1347 if kwonly_given:
1348 msg = " positional argument%s (and %d keyword-only argument%s)"
1349 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1350 "s" if kwonly_given != 1 else ""))
1351 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1352 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1353 "was" if given == 1 and not kwonly_given else "were"))
1354
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03001355def getcallargs(func, /, *positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001356 """Get the mapping of arguments to values.
1357
1358 A dict is returned, with keys the function argument names (including the
1359 names of the * and ** arguments, if any), and values the respective bound
1360 values from 'positional' and 'named'."""
1361 spec = getfullargspec(func)
Pablo Galindod5d2b452019-04-30 02:01:14 +01001362 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001363 f_name = func.__name__
1364 arg2value = {}
1365
Benjamin Petersonb204a422011-06-05 22:04:07 -05001366
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001367 if ismethod(func) and func.__self__ is not None:
1368 # implicit 'self' (or 'cls' for classmethods) argument
1369 positional = (func.__self__,) + positional
1370 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001371 num_args = len(args)
1372 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001373
Benjamin Petersonb204a422011-06-05 22:04:07 -05001374 n = min(num_pos, num_args)
1375 for i in range(n):
Pablo Galindod5d2b452019-04-30 02:01:14 +01001376 arg2value[args[i]] = positional[i]
Benjamin Petersonb204a422011-06-05 22:04:07 -05001377 if varargs:
1378 arg2value[varargs] = tuple(positional[n:])
1379 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001380 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001381 arg2value[varkw] = {}
1382 for kw, value in named.items():
1383 if kw not in possible_kwargs:
1384 if not varkw:
1385 raise TypeError("%s() got an unexpected keyword argument %r" %
1386 (f_name, kw))
1387 arg2value[varkw][kw] = value
1388 continue
1389 if kw in arg2value:
1390 raise TypeError("%s() got multiple values for argument %r" %
1391 (f_name, kw))
1392 arg2value[kw] = value
1393 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001394 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1395 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001396 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001397 req = args[:num_args - num_defaults]
1398 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001399 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001400 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001401 for i, arg in enumerate(args[num_args - num_defaults:]):
1402 if arg not in arg2value:
1403 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001404 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001405 for kwarg in kwonlyargs:
1406 if kwarg not in arg2value:
Yury Selivanov875df202014-03-27 18:23:03 -04001407 if kwonlydefaults and kwarg in kwonlydefaults:
Benjamin Petersone109c702011-06-24 09:37:26 -05001408 arg2value[kwarg] = kwonlydefaults[kwarg]
1409 else:
1410 missing += 1
1411 if missing:
1412 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001413 return arg2value
1414
Nick Coghlan2f92e542012-06-23 19:39:55 +10001415ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1416
1417def getclosurevars(func):
1418 """
1419 Get the mapping of free variables to their current values.
1420
Meador Inge8fda3592012-07-19 21:33:21 -05001421 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001422 and builtin references as seen by the body of the function. A final
1423 set of unbound names that could not be resolved is also provided.
1424 """
1425
1426 if ismethod(func):
1427 func = func.__func__
1428
1429 if not isfunction(func):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001430 raise TypeError("{!r} is not a Python function".format(func))
Nick Coghlan2f92e542012-06-23 19:39:55 +10001431
1432 code = func.__code__
1433 # Nonlocal references are named in co_freevars and resolved
1434 # by looking them up in __closure__ by positional index
1435 if func.__closure__ is None:
1436 nonlocal_vars = {}
1437 else:
1438 nonlocal_vars = {
1439 var : cell.cell_contents
1440 for var, cell in zip(code.co_freevars, func.__closure__)
1441 }
1442
1443 # Global and builtin references are named in co_names and resolved
1444 # by looking them up in __globals__ or __builtins__
1445 global_ns = func.__globals__
1446 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1447 if ismodule(builtin_ns):
1448 builtin_ns = builtin_ns.__dict__
1449 global_vars = {}
1450 builtin_vars = {}
1451 unbound_names = set()
1452 for name in code.co_names:
1453 if name in ("None", "True", "False"):
1454 # Because these used to be builtins instead of keywords, they
1455 # may still show up as name references. We ignore them.
1456 continue
1457 try:
1458 global_vars[name] = global_ns[name]
1459 except KeyError:
1460 try:
1461 builtin_vars[name] = builtin_ns[name]
1462 except KeyError:
1463 unbound_names.add(name)
1464
1465 return ClosureVars(nonlocal_vars, global_vars,
1466 builtin_vars, unbound_names)
1467
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001468# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001469
1470Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1471
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001472def getframeinfo(frame, context=1):
1473 """Get information about a frame or traceback object.
1474
1475 A tuple of five things is returned: the filename, the line number of
1476 the current line, the function name, a list of lines of context from
1477 the source code, and the index of the current line within that list.
1478 The optional second argument specifies the number of lines of context
1479 to return, which are centered around the current line."""
1480 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001481 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001482 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001483 else:
1484 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001485 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001486 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001487
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001488 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001489 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001490 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001491 try:
1492 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001493 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001494 lines = index = None
1495 else:
Raymond Hettingera0501712004-06-15 11:22:53 +00001496 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001497 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001498 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001499 else:
1500 lines = index = None
1501
Christian Heimes25bb7832008-01-11 16:17:00 +00001502 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001503
1504def getlineno(frame):
1505 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001506 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1507 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001508
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001509FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
1510
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001511def getouterframes(frame, context=1):
1512 """Get a list of records for a frame and all higher (calling) frames.
1513
1514 Each record contains a frame object, filename, line number, function
1515 name, a list of lines of context, and index within the context."""
1516 framelist = []
1517 while frame:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001518 frameinfo = (frame,) + getframeinfo(frame, context)
1519 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001520 frame = frame.f_back
1521 return framelist
1522
1523def getinnerframes(tb, context=1):
1524 """Get a list of records for a traceback's frame and all lower frames.
1525
1526 Each record contains a frame object, filename, line number, function
1527 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001528 framelist = []
1529 while tb:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001530 frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
1531 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001532 tb = tb.tb_next
1533 return framelist
1534
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001535def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001536 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001537 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001538
1539def stack(context=1):
1540 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001541 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001542
1543def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001544 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001545 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001546
1547
1548# ------------------------------------------------ static version of getattr
1549
1550_sentinel = object()
1551
Michael Foorde5162652010-11-20 16:40:44 +00001552def _static_getmro(klass):
1553 return type.__dict__['__mro__'].__get__(klass)
1554
Michael Foord95fc51d2010-11-20 15:07:30 +00001555def _check_instance(obj, attr):
1556 instance_dict = {}
1557 try:
1558 instance_dict = object.__getattribute__(obj, "__dict__")
1559 except AttributeError:
1560 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001561 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001562
1563
1564def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001565 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001566 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001567 try:
1568 return entry.__dict__[attr]
1569 except KeyError:
1570 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001571 return _sentinel
1572
Michael Foord35184ed2010-11-20 16:58:30 +00001573def _is_type(obj):
1574 try:
1575 _static_getmro(obj)
1576 except TypeError:
1577 return False
1578 return True
1579
Michael Foorddcebe0f2011-03-15 19:20:44 -04001580def _shadowed_dict(klass):
1581 dict_attr = type.__dict__["__dict__"]
1582 for entry in _static_getmro(klass):
1583 try:
1584 class_dict = dict_attr.__get__(entry)["__dict__"]
1585 except KeyError:
1586 pass
1587 else:
Inada Naoki8f9cc872019-09-05 13:07:08 +09001588 if not (type(class_dict) is types.GetSetDescriptorType and
Michael Foorddcebe0f2011-03-15 19:20:44 -04001589 class_dict.__name__ == "__dict__" and
1590 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001591 return class_dict
1592 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001593
1594def getattr_static(obj, attr, default=_sentinel):
1595 """Retrieve attributes without triggering dynamic lookup via the
1596 descriptor protocol, __getattr__ or __getattribute__.
1597
1598 Note: this function may not be able to retrieve all attributes
1599 that getattr can fetch (like dynamically created attributes)
1600 and may find attributes that getattr can't (like descriptors
1601 that raise AttributeError). It can also return descriptor objects
1602 instead of instance members in some cases. See the
1603 documentation for details.
1604 """
1605 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001606 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001607 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001608 dict_attr = _shadowed_dict(klass)
1609 if (dict_attr is _sentinel or
Inada Naoki8f9cc872019-09-05 13:07:08 +09001610 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001611 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001612 else:
1613 klass = obj
1614
1615 klass_result = _check_class(klass, attr)
1616
1617 if instance_result is not _sentinel and klass_result is not _sentinel:
1618 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1619 _check_class(type(klass_result), '__set__') is not _sentinel):
1620 return klass_result
1621
1622 if instance_result is not _sentinel:
1623 return instance_result
1624 if klass_result is not _sentinel:
1625 return klass_result
1626
1627 if obj is klass:
1628 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001629 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001630 if _shadowed_dict(type(entry)) is _sentinel:
1631 try:
1632 return entry.__dict__[attr]
1633 except KeyError:
1634 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001635 if default is not _sentinel:
1636 return default
1637 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001638
1639
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001640# ------------------------------------------------ generator introspection
1641
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001642GEN_CREATED = 'GEN_CREATED'
1643GEN_RUNNING = 'GEN_RUNNING'
1644GEN_SUSPENDED = 'GEN_SUSPENDED'
1645GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001646
1647def getgeneratorstate(generator):
1648 """Get current state of a generator-iterator.
1649
1650 Possible states are:
1651 GEN_CREATED: Waiting to start execution.
1652 GEN_RUNNING: Currently being executed by the interpreter.
1653 GEN_SUSPENDED: Currently suspended at a yield expression.
1654 GEN_CLOSED: Execution has completed.
1655 """
1656 if generator.gi_running:
1657 return GEN_RUNNING
1658 if generator.gi_frame is None:
1659 return GEN_CLOSED
1660 if generator.gi_frame.f_lasti == -1:
1661 return GEN_CREATED
1662 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001663
1664
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001665def getgeneratorlocals(generator):
1666 """
1667 Get the mapping of generator local variables to their current values.
1668
1669 A dict is returned, with the keys the local variable names and values the
1670 bound values."""
1671
1672 if not isgenerator(generator):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001673 raise TypeError("{!r} is not a Python generator".format(generator))
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001674
1675 frame = getattr(generator, "gi_frame", None)
1676 if frame is not None:
1677 return generator.gi_frame.f_locals
1678 else:
1679 return {}
1680
Yury Selivanov5376ba92015-06-22 12:19:30 -04001681
1682# ------------------------------------------------ coroutine introspection
1683
1684CORO_CREATED = 'CORO_CREATED'
1685CORO_RUNNING = 'CORO_RUNNING'
1686CORO_SUSPENDED = 'CORO_SUSPENDED'
1687CORO_CLOSED = 'CORO_CLOSED'
1688
1689def getcoroutinestate(coroutine):
1690 """Get current state of a coroutine object.
1691
1692 Possible states are:
1693 CORO_CREATED: Waiting to start execution.
1694 CORO_RUNNING: Currently being executed by the interpreter.
1695 CORO_SUSPENDED: Currently suspended at an await expression.
1696 CORO_CLOSED: Execution has completed.
1697 """
1698 if coroutine.cr_running:
1699 return CORO_RUNNING
1700 if coroutine.cr_frame is None:
1701 return CORO_CLOSED
1702 if coroutine.cr_frame.f_lasti == -1:
1703 return CORO_CREATED
1704 return CORO_SUSPENDED
1705
1706
1707def getcoroutinelocals(coroutine):
1708 """
1709 Get the mapping of coroutine local variables to their current values.
1710
1711 A dict is returned, with the keys the local variable names and values the
1712 bound values."""
1713 frame = getattr(coroutine, "cr_frame", None)
1714 if frame is not None:
1715 return frame.f_locals
1716 else:
1717 return {}
1718
1719
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001720###############################################################################
1721### Function Signature Object (PEP 362)
1722###############################################################################
1723
1724
1725_WrapperDescriptor = type(type.__call__)
1726_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001727_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001728
1729_NonUserDefinedCallables = (_WrapperDescriptor,
1730 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001731 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001732 types.BuiltinFunctionType)
1733
1734
Yury Selivanov421f0c72014-01-29 12:05:40 -05001735def _signature_get_user_defined_method(cls, method_name):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001736 """Private helper. Checks if ``cls`` has an attribute
1737 named ``method_name`` and returns it only if it is a
1738 pure python function.
1739 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001740 try:
1741 meth = getattr(cls, method_name)
1742 except AttributeError:
1743 return
1744 else:
1745 if not isinstance(meth, _NonUserDefinedCallables):
1746 # Once '__signature__' will be added to 'C'-level
1747 # callables, this check won't be necessary
1748 return meth
1749
1750
Yury Selivanov62560fb2014-01-28 12:26:24 -05001751def _signature_get_partial(wrapped_sig, partial, extra_args=()):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001752 """Private helper to calculate how 'wrapped_sig' signature will
1753 look like after applying a 'functools.partial' object (or alike)
1754 on it.
1755 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001756
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001757 old_params = wrapped_sig.parameters
Inada Naoki21105512020-03-02 18:54:49 +09001758 new_params = OrderedDict(old_params.items())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001759
1760 partial_args = partial.args or ()
1761 partial_keywords = partial.keywords or {}
1762
1763 if extra_args:
1764 partial_args = extra_args + partial_args
1765
1766 try:
1767 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1768 except TypeError as ex:
1769 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1770 raise ValueError(msg) from ex
1771
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001772
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001773 transform_to_kwonly = False
1774 for param_name, param in old_params.items():
1775 try:
1776 arg_value = ba.arguments[param_name]
1777 except KeyError:
1778 pass
1779 else:
1780 if param.kind is _POSITIONAL_ONLY:
1781 # If positional-only parameter is bound by partial,
1782 # it effectively disappears from the signature
Inada Naoki21105512020-03-02 18:54:49 +09001783 new_params.pop(param_name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001784 continue
1785
1786 if param.kind is _POSITIONAL_OR_KEYWORD:
1787 if param_name in partial_keywords:
1788 # This means that this parameter, and all parameters
1789 # after it should be keyword-only (and var-positional
1790 # should be removed). Here's why. Consider the following
1791 # function:
1792 # foo(a, b, *args, c):
1793 # pass
1794 #
1795 # "partial(foo, a='spam')" will have the following
1796 # signature: "(*, a='spam', b, c)". Because attempting
1797 # to call that partial with "(10, 20)" arguments will
1798 # raise a TypeError, saying that "a" argument received
1799 # multiple values.
1800 transform_to_kwonly = True
1801 # Set the new default value
Inada Naoki21105512020-03-02 18:54:49 +09001802 new_params[param_name] = param.replace(default=arg_value)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001803 else:
1804 # was passed as a positional argument
Inada Naoki21105512020-03-02 18:54:49 +09001805 new_params.pop(param.name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001806 continue
1807
1808 if param.kind is _KEYWORD_ONLY:
1809 # Set the new default value
Inada Naoki21105512020-03-02 18:54:49 +09001810 new_params[param_name] = param.replace(default=arg_value)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001811
1812 if transform_to_kwonly:
1813 assert param.kind is not _POSITIONAL_ONLY
1814
1815 if param.kind is _POSITIONAL_OR_KEYWORD:
Inada Naoki21105512020-03-02 18:54:49 +09001816 new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1817 new_params[param_name] = new_param
1818 new_params.move_to_end(param_name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001819 elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
Inada Naoki21105512020-03-02 18:54:49 +09001820 new_params.move_to_end(param_name)
1821 elif param.kind is _VAR_POSITIONAL:
1822 new_params.pop(param.name)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001823
1824 return wrapped_sig.replace(parameters=new_params.values())
1825
1826
Yury Selivanov62560fb2014-01-28 12:26:24 -05001827def _signature_bound_method(sig):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001828 """Private helper to transform signatures for unbound
1829 functions to bound methods.
1830 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001831
1832 params = tuple(sig.parameters.values())
1833
1834 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1835 raise ValueError('invalid method signature')
1836
1837 kind = params[0].kind
1838 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1839 # Drop first parameter:
1840 # '(p1, p2[, ...])' -> '(p2[, ...])'
1841 params = params[1:]
1842 else:
1843 if kind is not _VAR_POSITIONAL:
1844 # Unless we add a new parameter type we never
1845 # get here
1846 raise ValueError('invalid argument type')
1847 # It's a var-positional parameter.
1848 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1849
1850 return sig.replace(parameters=params)
1851
1852
Yury Selivanovb77511d2014-01-29 10:46:14 -05001853def _signature_is_builtin(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001854 """Private helper to test if `obj` is a callable that might
1855 support Argument Clinic's __text_signature__ protocol.
1856 """
Yury Selivanov1d241832014-02-02 12:51:20 -05001857 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001858 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001859 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001860 # Can't test 'isinstance(type)' here, as it would
1861 # also be True for regular python classes
1862 obj in (type, object))
1863
1864
Yury Selivanov63da7c72014-01-31 14:48:37 -05001865def _signature_is_functionlike(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001866 """Private helper to test if `obj` is a duck type of FunctionType.
1867 A good example of such objects are functions compiled with
1868 Cython, which have all attributes that a pure Python function
1869 would have, but have their code statically compiled.
1870 """
Yury Selivanov63da7c72014-01-31 14:48:37 -05001871
1872 if not callable(obj) or isclass(obj):
1873 # All function-like objects are obviously callables,
1874 # and not classes.
1875 return False
1876
1877 name = getattr(obj, '__name__', None)
1878 code = getattr(obj, '__code__', None)
1879 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
1880 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
Batuhan Taskaya044a1042020-10-06 23:03:02 +03001881 try:
1882 annotations = _get_type_hints(obj)
1883 except AttributeError:
1884 annotations = None
Yury Selivanov63da7c72014-01-31 14:48:37 -05001885
1886 return (isinstance(code, types.CodeType) and
1887 isinstance(name, str) and
1888 (defaults is None or isinstance(defaults, tuple)) and
1889 (kwdefaults is None or isinstance(kwdefaults, dict)) and
1890 isinstance(annotations, dict))
1891
1892
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001893def _signature_get_bound_param(spec):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001894 """ Private helper to get first parameter name from a
1895 __text_signature__ of a builtin method, which should
1896 be in the following format: '($param1, ...)'.
1897 Assumptions are that the first argument won't have
1898 a default value or an annotation.
1899 """
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001900
1901 assert spec.startswith('($')
1902
1903 pos = spec.find(',')
1904 if pos == -1:
1905 pos = spec.find(')')
1906
1907 cpos = spec.find(':')
1908 assert cpos == -1 or cpos > pos
1909
1910 cpos = spec.find('=')
1911 assert cpos == -1 or cpos > pos
1912
1913 return spec[2:pos]
1914
1915
Larry Hastings2623c8c2014-02-08 22:15:29 -08001916def _signature_strip_non_python_syntax(signature):
1917 """
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001918 Private helper function. Takes a signature in Argument Clinic's
1919 extended signature format.
1920
Larry Hastings2623c8c2014-02-08 22:15:29 -08001921 Returns a tuple of three things:
1922 * that signature re-rendered in standard Python syntax,
1923 * the index of the "self" parameter (generally 0), or None if
1924 the function does not have a "self" parameter, and
1925 * the index of the last "positional only" parameter,
1926 or None if the signature has no positional-only parameters.
1927 """
1928
1929 if not signature:
1930 return signature, None, None
1931
1932 self_parameter = None
1933 last_positional_only = None
1934
1935 lines = [l.encode('ascii') for l in signature.split('\n')]
1936 generator = iter(lines).__next__
1937 token_stream = tokenize.tokenize(generator)
1938
1939 delayed_comma = False
1940 skip_next_comma = False
1941 text = []
1942 add = text.append
1943
1944 current_parameter = 0
1945 OP = token.OP
1946 ERRORTOKEN = token.ERRORTOKEN
1947
1948 # token stream always starts with ENCODING token, skip it
1949 t = next(token_stream)
1950 assert t.type == tokenize.ENCODING
1951
1952 for t in token_stream:
1953 type, string = t.type, t.string
1954
1955 if type == OP:
1956 if string == ',':
1957 if skip_next_comma:
1958 skip_next_comma = False
1959 else:
1960 assert not delayed_comma
1961 delayed_comma = True
1962 current_parameter += 1
1963 continue
1964
1965 if string == '/':
1966 assert not skip_next_comma
1967 assert last_positional_only is None
1968 skip_next_comma = True
1969 last_positional_only = current_parameter - 1
1970 continue
1971
1972 if (type == ERRORTOKEN) and (string == '$'):
1973 assert self_parameter is None
1974 self_parameter = current_parameter
1975 continue
1976
1977 if delayed_comma:
1978 delayed_comma = False
1979 if not ((type == OP) and (string == ')')):
1980 add(', ')
1981 add(string)
1982 if (string == ','):
1983 add(' ')
1984 clean_signature = ''.join(text)
1985 return clean_signature, self_parameter, last_positional_only
1986
1987
Yury Selivanov57d240e2014-02-19 16:27:23 -05001988def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001989 """Private helper to parse content of '__text_signature__'
1990 and return a Signature based on it.
1991 """
INADA Naoki37420de2018-01-27 10:10:06 +09001992 # Lazy import ast because it's relatively heavy and
1993 # it's not used for other than this function.
1994 import ast
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001995
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001996 Parameter = cls._parameter_cls
1997
Larry Hastings2623c8c2014-02-08 22:15:29 -08001998 clean_signature, self_parameter, last_positional_only = \
1999 _signature_strip_non_python_syntax(s)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002000
Larry Hastings2623c8c2014-02-08 22:15:29 -08002001 program = "def foo" + clean_signature + ": pass"
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002002
2003 try:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002004 module = ast.parse(program)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002005 except SyntaxError:
2006 module = None
2007
2008 if not isinstance(module, ast.Module):
2009 raise ValueError("{!r} builtin has invalid signature".format(obj))
2010
2011 f = module.body[0]
2012
2013 parameters = []
2014 empty = Parameter.empty
2015 invalid = object()
2016
2017 module = None
2018 module_dict = {}
2019 module_name = getattr(obj, '__module__', None)
2020 if module_name:
2021 module = sys.modules.get(module_name, None)
2022 if module:
2023 module_dict = module.__dict__
INADA Naoki6f85b822018-10-05 01:47:09 +09002024 sys_module_dict = sys.modules.copy()
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002025
2026 def parse_name(node):
2027 assert isinstance(node, ast.arg)
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05302028 if node.annotation is not None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002029 raise ValueError("Annotations are not currently supported")
2030 return node.arg
2031
2032 def wrap_value(s):
2033 try:
2034 value = eval(s, module_dict)
2035 except NameError:
2036 try:
2037 value = eval(s, sys_module_dict)
2038 except NameError:
2039 raise RuntimeError()
2040
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002041 if isinstance(value, (str, int, float, bytes, bool, type(None))):
2042 return ast.Constant(value)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002043 raise RuntimeError()
2044
2045 class RewriteSymbolics(ast.NodeTransformer):
2046 def visit_Attribute(self, node):
2047 a = []
2048 n = node
2049 while isinstance(n, ast.Attribute):
2050 a.append(n.attr)
2051 n = n.value
2052 if not isinstance(n, ast.Name):
2053 raise RuntimeError()
2054 a.append(n.id)
2055 value = ".".join(reversed(a))
2056 return wrap_value(value)
2057
2058 def visit_Name(self, node):
2059 if not isinstance(node.ctx, ast.Load):
2060 raise ValueError()
2061 return wrap_value(node.id)
2062
2063 def p(name_node, default_node, default=empty):
2064 name = parse_name(name_node)
2065 if name is invalid:
2066 return None
2067 if default_node and default_node is not _empty:
2068 try:
2069 default_node = RewriteSymbolics().visit(default_node)
2070 o = ast.literal_eval(default_node)
2071 except ValueError:
2072 o = invalid
2073 if o is invalid:
2074 return None
2075 default = o if o is not invalid else default
2076 parameters.append(Parameter(name, kind, default=default, annotation=empty))
2077
2078 # non-keyword-only parameters
2079 args = reversed(f.args.args)
2080 defaults = reversed(f.args.defaults)
2081 iter = itertools.zip_longest(args, defaults, fillvalue=None)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002082 if last_positional_only is not None:
2083 kind = Parameter.POSITIONAL_ONLY
2084 else:
2085 kind = Parameter.POSITIONAL_OR_KEYWORD
2086 for i, (name, default) in enumerate(reversed(list(iter))):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002087 p(name, default)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002088 if i == last_positional_only:
2089 kind = Parameter.POSITIONAL_OR_KEYWORD
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002090
2091 # *args
2092 if f.args.vararg:
2093 kind = Parameter.VAR_POSITIONAL
2094 p(f.args.vararg, empty)
2095
2096 # keyword-only arguments
2097 kind = Parameter.KEYWORD_ONLY
2098 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
2099 p(name, default)
2100
2101 # **kwargs
2102 if f.args.kwarg:
2103 kind = Parameter.VAR_KEYWORD
2104 p(f.args.kwarg, empty)
2105
Larry Hastings2623c8c2014-02-08 22:15:29 -08002106 if self_parameter is not None:
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002107 # Possibly strip the bound argument:
2108 # - We *always* strip first bound argument if
2109 # it is a module.
2110 # - We don't strip first bound argument if
2111 # skip_bound_arg is False.
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002112 assert parameters
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002113 _self = getattr(obj, '__self__', None)
2114 self_isbound = _self is not None
2115 self_ismodule = ismodule(_self)
2116 if self_isbound and (self_ismodule or skip_bound_arg):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002117 parameters.pop(0)
2118 else:
2119 # for builtins, self parameter is always positional-only!
2120 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
2121 parameters[0] = p
2122
2123 return cls(parameters, return_annotation=cls.empty)
2124
Batuhan Taskaya044a1042020-10-06 23:03:02 +03002125def _get_type_hints(func):
2126 try:
2127 return typing.get_type_hints(func)
2128 except Exception:
2129 # First, try to use the get_type_hints to resolve
2130 # annotations. But for keeping the behavior intact
2131 # if there was a problem with that (like the namespace
2132 # can't resolve some annotation) continue to use
2133 # string annotations
2134 return func.__annotations__
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002135
Yury Selivanov57d240e2014-02-19 16:27:23 -05002136def _signature_from_builtin(cls, func, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002137 """Private helper function to get signature for
2138 builtin callables.
2139 """
2140
Yury Selivanov57d240e2014-02-19 16:27:23 -05002141 if not _signature_is_builtin(func):
2142 raise TypeError("{!r} is not a Python builtin "
2143 "function".format(func))
2144
2145 s = getattr(func, "__text_signature__", None)
2146 if not s:
2147 raise ValueError("no signature found for builtin {!r}".format(func))
2148
2149 return _signature_fromstr(cls, func, s, skip_bound_arg)
2150
2151
Serhiy Storchakad53cf992019-05-06 22:40:27 +03002152def _signature_from_function(cls, func, skip_bound_arg=True):
Yury Selivanovcf45f022015-05-20 14:38:50 -04002153 """Private helper: constructs Signature for the given python function."""
2154
2155 is_duck_function = False
2156 if not isfunction(func):
2157 if _signature_is_functionlike(func):
2158 is_duck_function = True
2159 else:
2160 # If it's not a pure Python function, and not a duck type
2161 # of pure function:
2162 raise TypeError('{!r} is not a Python function'.format(func))
2163
Serhiy Storchakad53cf992019-05-06 22:40:27 +03002164 s = getattr(func, "__text_signature__", None)
2165 if s:
2166 return _signature_fromstr(cls, func, s, skip_bound_arg)
2167
Yury Selivanovcf45f022015-05-20 14:38:50 -04002168 Parameter = cls._parameter_cls
2169
2170 # Parameter information.
2171 func_code = func.__code__
2172 pos_count = func_code.co_argcount
2173 arg_names = func_code.co_varnames
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002174 posonly_count = func_code.co_posonlyargcount
Pablo Galindocd74e662019-06-01 18:08:04 +01002175 positional = arg_names[:pos_count]
Yury Selivanovcf45f022015-05-20 14:38:50 -04002176 keyword_only_count = func_code.co_kwonlyargcount
Pablo Galindocd74e662019-06-01 18:08:04 +01002177 keyword_only = arg_names[pos_count:pos_count + keyword_only_count]
Batuhan Taskaya044a1042020-10-06 23:03:02 +03002178 annotations = _get_type_hints(func)
2179
Yury Selivanovcf45f022015-05-20 14:38:50 -04002180 defaults = func.__defaults__
2181 kwdefaults = func.__kwdefaults__
2182
2183 if defaults:
2184 pos_default_count = len(defaults)
2185 else:
2186 pos_default_count = 0
2187
2188 parameters = []
2189
Pablo Galindocd74e662019-06-01 18:08:04 +01002190 non_default_count = pos_count - pos_default_count
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002191 posonly_left = posonly_count
2192
Yury Selivanovcf45f022015-05-20 14:38:50 -04002193 # Non-keyword-only parameters w/o defaults.
Pablo Galindocd74e662019-06-01 18:08:04 +01002194 for name in positional[:non_default_count]:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002195 kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD
Yury Selivanovcf45f022015-05-20 14:38:50 -04002196 annotation = annotations.get(name, _empty)
2197 parameters.append(Parameter(name, annotation=annotation,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002198 kind=kind))
2199 if posonly_left:
2200 posonly_left -= 1
Yury Selivanovcf45f022015-05-20 14:38:50 -04002201
2202 # ... w/ defaults.
Pablo Galindocd74e662019-06-01 18:08:04 +01002203 for offset, name in enumerate(positional[non_default_count:]):
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002204 kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD
Yury Selivanovcf45f022015-05-20 14:38:50 -04002205 annotation = annotations.get(name, _empty)
2206 parameters.append(Parameter(name, annotation=annotation,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002207 kind=kind,
Yury Selivanovcf45f022015-05-20 14:38:50 -04002208 default=defaults[offset]))
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002209 if posonly_left:
2210 posonly_left -= 1
Yury Selivanovcf45f022015-05-20 14:38:50 -04002211
2212 # *args
2213 if func_code.co_flags & CO_VARARGS:
Pablo Galindocd74e662019-06-01 18:08:04 +01002214 name = arg_names[pos_count + keyword_only_count]
Yury Selivanovcf45f022015-05-20 14:38:50 -04002215 annotation = annotations.get(name, _empty)
2216 parameters.append(Parameter(name, annotation=annotation,
2217 kind=_VAR_POSITIONAL))
2218
2219 # Keyword-only parameters.
2220 for name in keyword_only:
2221 default = _empty
2222 if kwdefaults is not None:
2223 default = kwdefaults.get(name, _empty)
2224
2225 annotation = annotations.get(name, _empty)
2226 parameters.append(Parameter(name, annotation=annotation,
2227 kind=_KEYWORD_ONLY,
2228 default=default))
2229 # **kwargs
2230 if func_code.co_flags & CO_VARKEYWORDS:
Pablo Galindocd74e662019-06-01 18:08:04 +01002231 index = pos_count + keyword_only_count
Yury Selivanovcf45f022015-05-20 14:38:50 -04002232 if func_code.co_flags & CO_VARARGS:
2233 index += 1
2234
2235 name = arg_names[index]
2236 annotation = annotations.get(name, _empty)
2237 parameters.append(Parameter(name, annotation=annotation,
2238 kind=_VAR_KEYWORD))
2239
2240 # Is 'func' is a pure Python function - don't validate the
2241 # parameters list (for correct order and defaults), it should be OK.
2242 return cls(parameters,
2243 return_annotation=annotations.get('return', _empty),
2244 __validate_parameters__=is_duck_function)
2245
2246
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002247def _signature_from_callable(obj, *,
2248 follow_wrapper_chains=True,
2249 skip_bound_arg=True,
2250 sigcls):
2251
2252 """Private helper function to get signature for arbitrary
2253 callable objects.
2254 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002255
2256 if not callable(obj):
2257 raise TypeError('{!r} is not a callable object'.format(obj))
2258
2259 if isinstance(obj, types.MethodType):
2260 # In this case we skip the first parameter of the underlying
2261 # function (usually `self` or `cls`).
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002262 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002263 obj.__func__,
2264 follow_wrapper_chains=follow_wrapper_chains,
2265 skip_bound_arg=skip_bound_arg,
2266 sigcls=sigcls)
2267
Yury Selivanov57d240e2014-02-19 16:27:23 -05002268 if skip_bound_arg:
2269 return _signature_bound_method(sig)
2270 else:
2271 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002272
Nick Coghlane8c45d62013-07-28 20:00:01 +10002273 # Was this function wrapped by a decorator?
Yury Selivanov57d240e2014-02-19 16:27:23 -05002274 if follow_wrapper_chains:
2275 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
Yury Selivanov46c759d2015-05-27 21:56:53 -04002276 if isinstance(obj, types.MethodType):
2277 # If the unwrapped object is a *method*, we might want to
2278 # skip its first parameter (self).
2279 # See test_signature_wrapped_bound_method for details.
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002280 return _signature_from_callable(
Yury Selivanov46c759d2015-05-27 21:56:53 -04002281 obj,
2282 follow_wrapper_chains=follow_wrapper_chains,
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002283 skip_bound_arg=skip_bound_arg,
2284 sigcls=sigcls)
Nick Coghlane8c45d62013-07-28 20:00:01 +10002285
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002286 try:
2287 sig = obj.__signature__
2288 except AttributeError:
2289 pass
2290 else:
2291 if sig is not None:
Yury Selivanov42407ab2014-06-23 10:23:50 -07002292 if not isinstance(sig, Signature):
2293 raise TypeError(
2294 'unexpected object {!r} in __signature__ '
2295 'attribute'.format(sig))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002296 return sig
2297
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002298 try:
2299 partialmethod = obj._partialmethod
2300 except AttributeError:
2301 pass
2302 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05002303 if isinstance(partialmethod, functools.partialmethod):
2304 # Unbound partialmethod (see functools.partialmethod)
2305 # This means, that we need to calculate the signature
2306 # as if it's a regular partial object, but taking into
2307 # account that the first positional argument
2308 # (usually `self`, or `cls`) will not be passed
2309 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002310
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002311 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002312 partialmethod.func,
2313 follow_wrapper_chains=follow_wrapper_chains,
2314 skip_bound_arg=skip_bound_arg,
2315 sigcls=sigcls)
2316
Yury Selivanov0486f812014-01-29 12:18:59 -05002317 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanov0486f812014-01-29 12:18:59 -05002318 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
Dong-hee Na378d7062017-05-18 04:00:51 +09002319 if first_wrapped_param.kind is Parameter.VAR_POSITIONAL:
2320 # First argument of the wrapped callable is `*args`, as in
2321 # `partialmethod(lambda *args)`.
2322 return sig
2323 else:
2324 sig_params = tuple(sig.parameters.values())
Yury Selivanov8a387212018-03-06 12:59:45 -05002325 assert (not sig_params or
2326 first_wrapped_param is not sig_params[0])
Dong-hee Na378d7062017-05-18 04:00:51 +09002327 new_params = (first_wrapped_param,) + sig_params
2328 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002329
Yury Selivanov63da7c72014-01-31 14:48:37 -05002330 if isfunction(obj) or _signature_is_functionlike(obj):
2331 # If it's a pure Python function, or an object that is duck type
2332 # of a Python function (Cython functions, for instance), then:
Serhiy Storchakad53cf992019-05-06 22:40:27 +03002333 return _signature_from_function(sigcls, obj,
2334 skip_bound_arg=skip_bound_arg)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002335
Yury Selivanova773de02014-02-21 18:30:53 -05002336 if _signature_is_builtin(obj):
Yury Selivanovda396452014-03-27 12:09:24 -04002337 return _signature_from_builtin(sigcls, obj,
Yury Selivanova773de02014-02-21 18:30:53 -05002338 skip_bound_arg=skip_bound_arg)
2339
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002340 if isinstance(obj, functools.partial):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002341 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002342 obj.func,
2343 follow_wrapper_chains=follow_wrapper_chains,
2344 skip_bound_arg=skip_bound_arg,
2345 sigcls=sigcls)
Yury Selivanov62560fb2014-01-28 12:26:24 -05002346 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002347
2348 sig = None
2349 if isinstance(obj, type):
2350 # obj is a class or a metaclass
2351
2352 # First, let's see if it has an overloaded __call__ defined
2353 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05002354 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002355 if call is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002356 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002357 call,
2358 follow_wrapper_chains=follow_wrapper_chains,
2359 skip_bound_arg=skip_bound_arg,
2360 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002361 else:
2362 # Now we check if the 'obj' class has a '__new__' method
Yury Selivanov421f0c72014-01-29 12:05:40 -05002363 new = _signature_get_user_defined_method(obj, '__new__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002364 if new is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002365 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002366 new,
2367 follow_wrapper_chains=follow_wrapper_chains,
2368 skip_bound_arg=skip_bound_arg,
2369 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002370 else:
2371 # Finally, we should have at least __init__ implemented
Yury Selivanov421f0c72014-01-29 12:05:40 -05002372 init = _signature_get_user_defined_method(obj, '__init__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002373 if init is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002374 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002375 init,
2376 follow_wrapper_chains=follow_wrapper_chains,
2377 skip_bound_arg=skip_bound_arg,
2378 sigcls=sigcls)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002379
2380 if sig is None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002381 # At this point we know, that `obj` is a class, with no user-
2382 # defined '__init__', '__new__', or class-level '__call__'
2383
Larry Hastings2623c8c2014-02-08 22:15:29 -08002384 for base in obj.__mro__[:-1]:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002385 # Since '__text_signature__' is implemented as a
2386 # descriptor that extracts text signature from the
2387 # class docstring, if 'obj' is derived from a builtin
2388 # class, its own '__text_signature__' may be 'None'.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002389 # Therefore, we go through the MRO (except the last
2390 # class in there, which is 'object') to find the first
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002391 # class with non-empty text signature.
2392 try:
2393 text_sig = base.__text_signature__
2394 except AttributeError:
2395 pass
2396 else:
2397 if text_sig:
2398 # If 'obj' class has a __text_signature__ attribute:
2399 # return a signature based on it
Yury Selivanovda396452014-03-27 12:09:24 -04002400 return _signature_fromstr(sigcls, obj, text_sig)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002401
2402 # No '__text_signature__' was found for the 'obj' class.
2403 # Last option is to check if its '__init__' is
2404 # object.__init__ or type.__init__.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002405 if type not in obj.__mro__:
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002406 # We have a class (not metaclass), but no user-defined
2407 # __init__ or __new__ for it
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002408 if (obj.__init__ is object.__init__ and
2409 obj.__new__ is object.__new__):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002410 # Return a signature of 'object' builtin.
Gregory P. Smith5b9ff7a2019-09-13 17:13:51 +01002411 return sigcls.from_callable(object)
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002412 else:
2413 raise ValueError(
2414 'no signature found for builtin type {!r}'.format(obj))
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002415
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002416 elif not isinstance(obj, _NonUserDefinedCallables):
2417 # An object with __call__
2418 # We also check that the 'obj' is not an instance of
2419 # _WrapperDescriptor or _MethodWrapper to avoid
2420 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05002421 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002422 if call is not None:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002423 try:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002424 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002425 call,
2426 follow_wrapper_chains=follow_wrapper_chains,
2427 skip_bound_arg=skip_bound_arg,
2428 sigcls=sigcls)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002429 except ValueError as ex:
2430 msg = 'no signature found for {!r}'.format(obj)
2431 raise ValueError(msg) from ex
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002432
2433 if sig is not None:
2434 # For classes and objects we skip the first parameter of their
2435 # __call__, __new__, or __init__ methods
Yury Selivanov57d240e2014-02-19 16:27:23 -05002436 if skip_bound_arg:
2437 return _signature_bound_method(sig)
2438 else:
2439 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002440
2441 if isinstance(obj, types.BuiltinFunctionType):
2442 # Raise a nicer error message for builtins
2443 msg = 'no signature found for builtin function {!r}'.format(obj)
2444 raise ValueError(msg)
2445
2446 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2447
2448
2449class _void:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002450 """A private marker - used in Parameter & Signature."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002451
2452
2453class _empty:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002454 """Marker object for Signature.empty and Parameter.empty."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002455
2456
Yury Selivanov21e83a52014-03-27 11:23:13 -04002457class _ParameterKind(enum.IntEnum):
2458 POSITIONAL_ONLY = 0
2459 POSITIONAL_OR_KEYWORD = 1
2460 VAR_POSITIONAL = 2
2461 KEYWORD_ONLY = 3
2462 VAR_KEYWORD = 4
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002463
2464 def __str__(self):
Yury Selivanov21e83a52014-03-27 11:23:13 -04002465 return self._name_
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002466
Dong-hee Na4aa30062018-06-08 12:46:31 +09002467 @property
2468 def description(self):
2469 return _PARAM_NAME_MAPPING[self]
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002470
Yury Selivanov21e83a52014-03-27 11:23:13 -04002471_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
2472_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
2473_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
2474_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
2475_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002476
Dong-hee Naa9cab432018-05-30 00:04:08 +09002477_PARAM_NAME_MAPPING = {
2478 _POSITIONAL_ONLY: 'positional-only',
2479 _POSITIONAL_OR_KEYWORD: 'positional or keyword',
2480 _VAR_POSITIONAL: 'variadic positional',
2481 _KEYWORD_ONLY: 'keyword-only',
2482 _VAR_KEYWORD: 'variadic keyword'
2483}
2484
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002485
2486class Parameter:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002487 """Represents a parameter in a function signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002488
2489 Has the following public attributes:
2490
2491 * name : str
2492 The name of the parameter as a string.
2493 * default : object
2494 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002495 parameter has no default value, this attribute is set to
2496 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002497 * annotation
2498 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002499 parameter has no annotation, this attribute is set to
2500 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002501 * kind : str
2502 Describes how argument values are bound to the parameter.
2503 Possible values: `Parameter.POSITIONAL_ONLY`,
2504 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
2505 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002506 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002507
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002508 __slots__ = ('_name', '_kind', '_default', '_annotation')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002509
2510 POSITIONAL_ONLY = _POSITIONAL_ONLY
2511 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
2512 VAR_POSITIONAL = _VAR_POSITIONAL
2513 KEYWORD_ONLY = _KEYWORD_ONLY
2514 VAR_KEYWORD = _VAR_KEYWORD
2515
2516 empty = _empty
2517
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002518 def __init__(self, name, kind, *, default=_empty, annotation=_empty):
Dong-hee Naa9cab432018-05-30 00:04:08 +09002519 try:
2520 self._kind = _ParameterKind(kind)
2521 except ValueError:
2522 raise ValueError(f'value {kind!r} is not a valid Parameter.kind')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002523 if default is not _empty:
Dong-hee Naa9cab432018-05-30 00:04:08 +09002524 if self._kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2525 msg = '{} parameters cannot have default values'
Dong-hee Na4aa30062018-06-08 12:46:31 +09002526 msg = msg.format(self._kind.description)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002527 raise ValueError(msg)
2528 self._default = default
2529 self._annotation = annotation
2530
Yury Selivanov2393dca2014-01-27 15:07:58 -05002531 if name is _empty:
2532 raise ValueError('name is a required attribute for Parameter')
2533
2534 if not isinstance(name, str):
Dong-hee Naa9cab432018-05-30 00:04:08 +09002535 msg = 'name must be a str, not a {}'.format(type(name).__name__)
2536 raise TypeError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002537
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002538 if name[0] == '.' and name[1:].isdigit():
2539 # These are implicit arguments generated by comprehensions. In
2540 # order to provide a friendlier interface to users, we recast
2541 # their name as "implicitN" and treat them as positional-only.
2542 # See issue 19611.
Dong-hee Naa9cab432018-05-30 00:04:08 +09002543 if self._kind != _POSITIONAL_OR_KEYWORD:
2544 msg = (
2545 'implicit arguments must be passed as '
2546 'positional or keyword arguments, not {}'
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002547 )
Dong-hee Na4aa30062018-06-08 12:46:31 +09002548 msg = msg.format(self._kind.description)
Dong-hee Naa9cab432018-05-30 00:04:08 +09002549 raise ValueError(msg)
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002550 self._kind = _POSITIONAL_ONLY
2551 name = 'implicit{}'.format(name[1:])
2552
Yury Selivanov2393dca2014-01-27 15:07:58 -05002553 if not name.isidentifier():
2554 raise ValueError('{!r} is not a valid parameter name'.format(name))
2555
2556 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002557
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002558 def __reduce__(self):
2559 return (type(self),
2560 (self._name, self._kind),
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002561 {'_default': self._default,
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002562 '_annotation': self._annotation})
2563
2564 def __setstate__(self, state):
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002565 self._default = state['_default']
2566 self._annotation = state['_annotation']
2567
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002568 @property
2569 def name(self):
2570 return self._name
2571
2572 @property
2573 def default(self):
2574 return self._default
2575
2576 @property
2577 def annotation(self):
2578 return self._annotation
2579
2580 @property
2581 def kind(self):
2582 return self._kind
2583
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002584 def replace(self, *, name=_void, kind=_void,
2585 annotation=_void, default=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002586 """Creates a customized copy of the Parameter."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002587
2588 if name is _void:
2589 name = self._name
2590
2591 if kind is _void:
2592 kind = self._kind
2593
2594 if annotation is _void:
2595 annotation = self._annotation
2596
2597 if default is _void:
2598 default = self._default
2599
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002600 return type(self)(name, kind, default=default, annotation=annotation)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002601
2602 def __str__(self):
2603 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002604 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002605
2606 # Add annotation and default value
2607 if self._annotation is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002608 formatted = '{}: {}'.format(formatted,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002609 formatannotation(self._annotation))
2610
2611 if self._default is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002612 if self._annotation is not _empty:
2613 formatted = '{} = {}'.format(formatted, repr(self._default))
2614 else:
2615 formatted = '{}={}'.format(formatted, repr(self._default))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002616
2617 if kind == _VAR_POSITIONAL:
2618 formatted = '*' + formatted
2619 elif kind == _VAR_KEYWORD:
2620 formatted = '**' + formatted
2621
2622 return formatted
2623
2624 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002625 return '<{} "{}">'.format(self.__class__.__name__, self)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002626
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002627 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002628 return hash((self.name, self.kind, self.annotation, self.default))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002629
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002630 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002631 if self is other:
2632 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002633 if not isinstance(other, Parameter):
2634 return NotImplemented
2635 return (self._name == other._name and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002636 self._kind == other._kind and
2637 self._default == other._default and
2638 self._annotation == other._annotation)
2639
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002640
2641class BoundArguments:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002642 """Result of `Signature.bind` call. Holds the mapping of arguments
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002643 to the function's parameters.
2644
2645 Has the following public attributes:
2646
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002647 * arguments : dict
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002648 An ordered mutable mapping of parameters' names to arguments' values.
2649 Does not contain arguments' default values.
2650 * signature : Signature
2651 The Signature object that created this instance.
2652 * args : tuple
2653 Tuple of positional arguments values.
2654 * kwargs : dict
2655 Dict of keyword arguments values.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002656 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002657
Yury Selivanov6abe0322015-05-13 17:18:41 -04002658 __slots__ = ('arguments', '_signature', '__weakref__')
2659
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002660 def __init__(self, signature, arguments):
2661 self.arguments = arguments
2662 self._signature = signature
2663
2664 @property
2665 def signature(self):
2666 return self._signature
2667
2668 @property
2669 def args(self):
2670 args = []
2671 for param_name, param in self._signature.parameters.items():
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002672 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002673 break
2674
2675 try:
2676 arg = self.arguments[param_name]
2677 except KeyError:
2678 # We're done here. Other arguments
2679 # will be mapped in 'BoundArguments.kwargs'
2680 break
2681 else:
2682 if param.kind == _VAR_POSITIONAL:
2683 # *args
2684 args.extend(arg)
2685 else:
2686 # plain argument
2687 args.append(arg)
2688
2689 return tuple(args)
2690
2691 @property
2692 def kwargs(self):
2693 kwargs = {}
2694 kwargs_started = False
2695 for param_name, param in self._signature.parameters.items():
2696 if not kwargs_started:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002697 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002698 kwargs_started = True
2699 else:
2700 if param_name not in self.arguments:
2701 kwargs_started = True
2702 continue
2703
2704 if not kwargs_started:
2705 continue
2706
2707 try:
2708 arg = self.arguments[param_name]
2709 except KeyError:
2710 pass
2711 else:
2712 if param.kind == _VAR_KEYWORD:
2713 # **kwargs
2714 kwargs.update(arg)
2715 else:
2716 # plain keyword argument
2717 kwargs[param_name] = arg
2718
2719 return kwargs
2720
Yury Selivanovb907a512015-05-16 13:45:09 -04002721 def apply_defaults(self):
2722 """Set default values for missing arguments.
2723
2724 For variable-positional arguments (*args) the default is an
2725 empty tuple.
2726
2727 For variable-keyword arguments (**kwargs) the default is an
2728 empty dict.
2729 """
2730 arguments = self.arguments
Yury Selivanovb907a512015-05-16 13:45:09 -04002731 new_arguments = []
2732 for name, param in self._signature.parameters.items():
2733 try:
2734 new_arguments.append((name, arguments[name]))
2735 except KeyError:
2736 if param.default is not _empty:
2737 val = param.default
2738 elif param.kind is _VAR_POSITIONAL:
2739 val = ()
2740 elif param.kind is _VAR_KEYWORD:
2741 val = {}
2742 else:
2743 # This BoundArguments was likely produced by
2744 # Signature.bind_partial().
2745 continue
2746 new_arguments.append((name, val))
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002747 self.arguments = dict(new_arguments)
Yury Selivanovb907a512015-05-16 13:45:09 -04002748
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002749 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002750 if self is other:
2751 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002752 if not isinstance(other, BoundArguments):
2753 return NotImplemented
2754 return (self.signature == other.signature and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002755 self.arguments == other.arguments)
2756
Yury Selivanov6abe0322015-05-13 17:18:41 -04002757 def __setstate__(self, state):
2758 self._signature = state['_signature']
2759 self.arguments = state['arguments']
2760
2761 def __getstate__(self):
2762 return {'_signature': self._signature, 'arguments': self.arguments}
2763
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002764 def __repr__(self):
2765 args = []
2766 for arg, value in self.arguments.items():
2767 args.append('{}={!r}'.format(arg, value))
Yury Selivanovf229bc52015-05-15 12:53:56 -04002768 return '<{} ({})>'.format(self.__class__.__name__, ', '.join(args))
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002769
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002770
2771class Signature:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002772 """A Signature object represents the overall signature of a function.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002773 It stores a Parameter object for each parameter accepted by the
2774 function, as well as information specific to the function itself.
2775
2776 A Signature object has the following public attributes and methods:
2777
Jens Reidel611836a2020-03-18 03:22:46 +01002778 * parameters : OrderedDict
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002779 An ordered mapping of parameters' names to the corresponding
2780 Parameter objects (keyword-only arguments are in the same order
2781 as listed in `code.co_varnames`).
2782 * return_annotation : object
2783 The annotation for the return type of the function if specified.
2784 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002785 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002786 * bind(*args, **kwargs) -> BoundArguments
2787 Creates a mapping from positional and keyword arguments to
2788 parameters.
2789 * bind_partial(*args, **kwargs) -> BoundArguments
2790 Creates a partial mapping from positional and keyword arguments
2791 to parameters (simulating 'functools.partial' behavior.)
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002792 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002793
2794 __slots__ = ('_return_annotation', '_parameters')
2795
2796 _parameter_cls = Parameter
2797 _bound_arguments_cls = BoundArguments
2798
2799 empty = _empty
2800
2801 def __init__(self, parameters=None, *, return_annotation=_empty,
2802 __validate_parameters__=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002803 """Constructs Signature from the given list of Parameter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002804 objects and 'return_annotation'. All arguments are optional.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002805 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002806
2807 if parameters is None:
Jens Reidel611836a2020-03-18 03:22:46 +01002808 params = OrderedDict()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002809 else:
2810 if __validate_parameters__:
Jens Reidel611836a2020-03-18 03:22:46 +01002811 params = OrderedDict()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002812 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002813 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002814
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002815 for param in parameters:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002816 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002817 name = param.name
2818
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002819 if kind < top_kind:
Dong-hee Naa9cab432018-05-30 00:04:08 +09002820 msg = (
2821 'wrong parameter order: {} parameter before {} '
2822 'parameter'
2823 )
Dong-hee Na4aa30062018-06-08 12:46:31 +09002824 msg = msg.format(top_kind.description,
2825 kind.description)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002826 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002827 elif kind > top_kind:
2828 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002829 top_kind = kind
2830
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002831 if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
Yury Selivanov07a9e452014-01-29 10:58:16 -05002832 if param.default is _empty:
2833 if kind_defaults:
2834 # No default for this parameter, but the
2835 # previous parameter of the same kind had
2836 # a default
2837 msg = 'non-default argument follows default ' \
2838 'argument'
2839 raise ValueError(msg)
2840 else:
2841 # There is a default for this parameter.
2842 kind_defaults = True
2843
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002844 if name in params:
2845 msg = 'duplicate parameter name: {!r}'.format(name)
2846 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002847
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002848 params[name] = param
2849 else:
Jens Reidel611836a2020-03-18 03:22:46 +01002850 params = OrderedDict((param.name, param) for param in parameters)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002851
2852 self._parameters = types.MappingProxyType(params)
2853 self._return_annotation = return_annotation
2854
2855 @classmethod
2856 def from_function(cls, func):
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002857 """Constructs Signature for the given python function.
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002858
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002859 Deprecated since Python 3.5, use `Signature.from_callable()`.
2860 """
2861
2862 warnings.warn("inspect.Signature.from_function() is deprecated since "
2863 "Python 3.5, use Signature.from_callable()",
Berker Peksagb5601582015-05-21 23:40:54 +03002864 DeprecationWarning, stacklevel=2)
Yury Selivanovcf45f022015-05-20 14:38:50 -04002865 return _signature_from_function(cls, func)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002866
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002867 @classmethod
2868 def from_builtin(cls, func):
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002869 """Constructs Signature for the given builtin function.
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002870
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002871 Deprecated since Python 3.5, use `Signature.from_callable()`.
2872 """
2873
2874 warnings.warn("inspect.Signature.from_builtin() is deprecated since "
2875 "Python 3.5, use Signature.from_callable()",
Berker Peksagb5601582015-05-21 23:40:54 +03002876 DeprecationWarning, stacklevel=2)
Yury Selivanov57d240e2014-02-19 16:27:23 -05002877 return _signature_from_builtin(cls, func)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002878
Yury Selivanovda396452014-03-27 12:09:24 -04002879 @classmethod
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002880 def from_callable(cls, obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002881 """Constructs Signature for the given callable object."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002882 return _signature_from_callable(obj, sigcls=cls,
2883 follow_wrapper_chains=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04002884
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002885 @property
2886 def parameters(self):
2887 return self._parameters
2888
2889 @property
2890 def return_annotation(self):
2891 return self._return_annotation
2892
2893 def replace(self, *, parameters=_void, return_annotation=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002894 """Creates a customized copy of the Signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002895 Pass 'parameters' and/or 'return_annotation' arguments
2896 to override them in the new copy.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002897 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002898
2899 if parameters is _void:
2900 parameters = self.parameters.values()
2901
2902 if return_annotation is _void:
2903 return_annotation = self._return_annotation
2904
2905 return type(self)(parameters,
2906 return_annotation=return_annotation)
2907
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002908 def _hash_basis(self):
2909 params = tuple(param for param in self.parameters.values()
2910 if param.kind != _KEYWORD_ONLY)
2911
2912 kwo_params = {param.name: param for param in self.parameters.values()
2913 if param.kind == _KEYWORD_ONLY}
2914
2915 return params, kwo_params, self.return_annotation
2916
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002917 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002918 params, kwo_params, return_annotation = self._hash_basis()
2919 kwo_params = frozenset(kwo_params.values())
2920 return hash((params, kwo_params, return_annotation))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002921
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002922 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002923 if self is other:
2924 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002925 if not isinstance(other, Signature):
2926 return NotImplemented
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002927 return self._hash_basis() == other._hash_basis()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002928
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002929 def _bind(self, args, kwargs, *, partial=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002930 """Private method. Don't use directly."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002931
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002932 arguments = {}
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002933
2934 parameters = iter(self.parameters.values())
2935 parameters_ex = ()
2936 arg_vals = iter(args)
2937
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002938 while True:
2939 # Let's iterate through the positional arguments and corresponding
2940 # parameters
2941 try:
2942 arg_val = next(arg_vals)
2943 except StopIteration:
2944 # No more positional arguments
2945 try:
2946 param = next(parameters)
2947 except StopIteration:
2948 # No more parameters. That's it. Just need to check that
2949 # we have no `kwargs` after this while loop
2950 break
2951 else:
2952 if param.kind == _VAR_POSITIONAL:
2953 # That's OK, just empty *args. Let's start parsing
2954 # kwargs
2955 break
2956 elif param.name in kwargs:
2957 if param.kind == _POSITIONAL_ONLY:
2958 msg = '{arg!r} parameter is positional only, ' \
2959 'but was passed as a keyword'
2960 msg = msg.format(arg=param.name)
2961 raise TypeError(msg) from None
2962 parameters_ex = (param,)
2963 break
2964 elif (param.kind == _VAR_KEYWORD or
2965 param.default is not _empty):
2966 # That's fine too - we have a default value for this
2967 # parameter. So, lets start parsing `kwargs`, starting
2968 # with the current parameter
2969 parameters_ex = (param,)
2970 break
2971 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002972 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
2973 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002974 if partial:
2975 parameters_ex = (param,)
2976 break
2977 else:
Yury Selivanov86872752015-05-19 00:27:49 -04002978 msg = 'missing a required argument: {arg!r}'
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002979 msg = msg.format(arg=param.name)
2980 raise TypeError(msg) from None
2981 else:
2982 # We have a positional argument to process
2983 try:
2984 param = next(parameters)
2985 except StopIteration:
2986 raise TypeError('too many positional arguments') from None
2987 else:
2988 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
2989 # Looks like we have no parameter for this positional
2990 # argument
Yury Selivanov86872752015-05-19 00:27:49 -04002991 raise TypeError(
2992 'too many positional arguments') from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002993
2994 if param.kind == _VAR_POSITIONAL:
2995 # We have an '*args'-like argument, let's fill it with
2996 # all positional arguments we have left and move on to
2997 # the next phase
2998 values = [arg_val]
2999 values.extend(arg_vals)
3000 arguments[param.name] = tuple(values)
3001 break
3002
Pablo Galindof3ef06a2019-10-15 12:40:02 +01003003 if param.name in kwargs and param.kind != _POSITIONAL_ONLY:
Yury Selivanov86872752015-05-19 00:27:49 -04003004 raise TypeError(
3005 'multiple values for argument {arg!r}'.format(
3006 arg=param.name)) from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003007
3008 arguments[param.name] = arg_val
3009
3010 # Now, we iterate through the remaining parameters to process
3011 # keyword arguments
3012 kwargs_param = None
3013 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003014 if param.kind == _VAR_KEYWORD:
3015 # Memorize that we have a '**kwargs'-like parameter
3016 kwargs_param = param
3017 continue
3018
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05003019 if param.kind == _VAR_POSITIONAL:
3020 # Named arguments don't refer to '*args'-like parameters.
3021 # We only arrive here if the positional arguments ended
3022 # before reaching the last parameter before *args.
3023 continue
3024
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003025 param_name = param.name
3026 try:
3027 arg_val = kwargs.pop(param_name)
3028 except KeyError:
3029 # We have no value for this parameter. It's fine though,
3030 # if it has a default value, or it is an '*args'-like
3031 # parameter, left alone by the processing of positional
3032 # arguments.
3033 if (not partial and param.kind != _VAR_POSITIONAL and
3034 param.default is _empty):
Yury Selivanov86872752015-05-19 00:27:49 -04003035 raise TypeError('missing a required argument: {arg!r}'. \
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003036 format(arg=param_name)) from None
3037
3038 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05003039 if param.kind == _POSITIONAL_ONLY:
3040 # This should never happen in case of a properly built
3041 # Signature object (but let's have this check here
3042 # to ensure correct behaviour just in case)
3043 raise TypeError('{arg!r} parameter is positional only, '
3044 'but was passed as a keyword'. \
3045 format(arg=param.name))
3046
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003047 arguments[param_name] = arg_val
3048
3049 if kwargs:
3050 if kwargs_param is not None:
3051 # Process our '**kwargs'-like parameter
3052 arguments[kwargs_param.name] = kwargs
3053 else:
Yury Selivanov86872752015-05-19 00:27:49 -04003054 raise TypeError(
3055 'got an unexpected keyword argument {arg!r}'.format(
3056 arg=next(iter(kwargs))))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003057
3058 return self._bound_arguments_cls(self, arguments)
3059
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003060 def bind(self, /, *args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003061 """Get a BoundArguments object, that maps the passed `args`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003062 and `kwargs` to the function's signature. Raises `TypeError`
3063 if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003064 """
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003065 return self._bind(args, kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003066
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003067 def bind_partial(self, /, *args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003068 """Get a BoundArguments object, that partially maps the
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003069 passed `args` and `kwargs` to the function's signature.
3070 Raises `TypeError` if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003071 """
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003072 return self._bind(args, kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003073
Yury Selivanova5d63dd2014-03-27 11:31:43 -04003074 def __reduce__(self):
3075 return (type(self),
3076 (tuple(self._parameters.values()),),
3077 {'_return_annotation': self._return_annotation})
3078
3079 def __setstate__(self, state):
3080 self._return_annotation = state['_return_annotation']
3081
Yury Selivanov374375d2014-03-27 12:41:53 -04003082 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04003083 return '<{} {}>'.format(self.__class__.__name__, self)
Yury Selivanov374375d2014-03-27 12:41:53 -04003084
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003085 def __str__(self):
3086 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05003087 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003088 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05003089 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003090 formatted = str(param)
3091
3092 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05003093
3094 if kind == _POSITIONAL_ONLY:
3095 render_pos_only_separator = True
3096 elif render_pos_only_separator:
3097 # It's not a positional-only parameter, and the flag
3098 # is set to 'True' (there were pos-only params before.)
3099 result.append('/')
3100 render_pos_only_separator = False
3101
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003102 if kind == _VAR_POSITIONAL:
3103 # OK, we have an '*args'-like parameter, so we won't need
3104 # a '*' to separate keyword-only arguments
3105 render_kw_only_separator = False
3106 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
3107 # We have a keyword-only parameter to render and we haven't
3108 # rendered an '*args'-like parameter before, so add a '*'
3109 # separator to the parameters list ("foo(arg1, *, arg2)" case)
3110 result.append('*')
3111 # This condition should be only triggered once, so
3112 # reset the flag
3113 render_kw_only_separator = False
3114
3115 result.append(formatted)
3116
Yury Selivanov2393dca2014-01-27 15:07:58 -05003117 if render_pos_only_separator:
3118 # There were only positional-only parameters, hence the
3119 # flag was not reset to 'False'
3120 result.append('/')
3121
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003122 rendered = '({})'.format(', '.join(result))
3123
3124 if self.return_annotation is not _empty:
3125 anno = formatannotation(self.return_annotation)
3126 rendered += ' -> {}'.format(anno)
3127
3128 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003129
Yury Selivanovda396452014-03-27 12:09:24 -04003130
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04003131def signature(obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003132 """Get a signature object for the passed callable."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04003133 return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04003134
3135
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003136def _main():
3137 """ Logic for inspecting an object given at command line """
3138 import argparse
3139 import importlib
3140
3141 parser = argparse.ArgumentParser()
3142 parser.add_argument(
3143 'object',
3144 help="The object to be analysed. "
3145 "It supports the 'module:qualname' syntax")
3146 parser.add_argument(
3147 '-d', '--details', action='store_true',
3148 help='Display info about the module rather than its source code')
3149
3150 args = parser.parse_args()
3151
3152 target = args.object
3153 mod_name, has_attrs, attrs = target.partition(":")
3154 try:
3155 obj = module = importlib.import_module(mod_name)
3156 except Exception as exc:
3157 msg = "Failed to import {} ({}: {})".format(mod_name,
3158 type(exc).__name__,
3159 exc)
3160 print(msg, file=sys.stderr)
Alan Yeee3c59a72019-09-09 07:15:43 -07003161 sys.exit(2)
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003162
3163 if has_attrs:
3164 parts = attrs.split(".")
3165 obj = module
3166 for part in parts:
3167 obj = getattr(obj, part)
3168
3169 if module.__name__ in sys.builtin_module_names:
3170 print("Can't get info for builtin modules.", file=sys.stderr)
Alan Yeee3c59a72019-09-09 07:15:43 -07003171 sys.exit(1)
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003172
3173 if args.details:
3174 print('Target: {}'.format(target))
3175 print('Origin: {}'.format(getsourcefile(module)))
3176 print('Cached: {}'.format(module.__cached__))
3177 if obj is module:
3178 print('Loader: {}'.format(repr(module.__loader__)))
3179 if hasattr(module, '__path__'):
3180 print('Submodule search path: {}'.format(module.__path__))
3181 else:
3182 try:
3183 __, lineno = findsource(obj)
3184 except Exception:
3185 pass
3186 else:
3187 print('Line: {}'.format(lineno))
3188
3189 print('\n')
3190 else:
3191 print(getsource(obj))
3192
3193
3194if __name__ == "__main__":
3195 _main()