blob: 531b891283b5334ffbbf4697170447711cc6edbd [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
larryhastings74613a42021-04-29 21:16:28 -070027
28 get_annotations() - safely compute an object's annotations
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000029"""
30
31# This module is in the public domain. No warranties.
32
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070033__author__ = ('Ka-Ping Yee <ping@lfw.org>',
34 'Yury Selivanov <yselivanov@sprymix.com>')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000035
Natefcfe80e2017-04-24 10:06:15 -070036import abc
Karthikeyan Singaravelan696136b2020-04-18 21:49:32 +053037import ast
Antoine Pitroua8723a02015-04-15 00:41:29 +020038import dis
Yury Selivanov75445082015-05-11 22:57:16 -040039import collections.abc
Yury Selivanov21e83a52014-03-27 11:23:13 -040040import enum
Brett Cannoncb66eb02012-05-11 12:58:42 -040041import importlib.machinery
42import itertools
Christian Heimes7131fd92008-02-19 14:21:46 +000043import linecache
Brett Cannoncb66eb02012-05-11 12:58:42 -040044import os
45import re
46import sys
47import tokenize
Larry Hastings2623c8c2014-02-08 22:15:29 -080048import token
Brett Cannoncb66eb02012-05-11 12:58:42 -040049import types
Brett Cannon2b88fcf2012-06-02 22:28:42 -040050import warnings
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070051import functools
Nick Coghlan2f92e542012-06-23 19:39:55 +100052import builtins
Raymond Hettingera1a992c2005-03-11 06:46:45 +000053from operator import attrgetter
Inada Naoki21105512020-03-02 18:54:49 +090054from collections import namedtuple, OrderedDict
Nick Coghlan09c81232010-08-17 10:18:16 +000055
56# Create constants for the compiler flags in Include/code.h
Antoine Pitroua8723a02015-04-15 00:41:29 +020057# We try to get them from dis to avoid duplication
58mod_dict = globals()
59for k, v in dis.COMPILER_FLAG_NAMES.items():
60 mod_dict["CO_" + v] = k
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000061
Christian Heimesbe5b30b2008-03-03 19:18:51 +000062# See Include/object.h
63TPFLAGS_IS_ABSTRACT = 1 << 20
64
larryhastings74613a42021-04-29 21:16:28 -070065
66def get_annotations(obj, *, globals=None, locals=None, eval_str=False):
67 """Compute the annotations dict for an object.
68
69 obj may be a callable, class, or module.
70 Passing in an object of any other type raises TypeError.
71
72 Returns a dict. get_annotations() returns a new dict every time
73 it's called; calling it twice on the same object will return two
74 different but equivalent dicts.
75
76 This function handles several details for you:
77
78 * If eval_str is true, values of type str will
79 be un-stringized using eval(). This is intended
80 for use with stringized annotations
81 ("from __future__ import annotations").
82 * If obj doesn't have an annotations dict, returns an
83 empty dict. (Functions and methods always have an
84 annotations dict; classes, modules, and other types of
85 callables may not.)
86 * Ignores inherited annotations on classes. If a class
87 doesn't have its own annotations dict, returns an empty dict.
88 * All accesses to object members and dict values are done
89 using getattr() and dict.get() for safety.
90 * Always, always, always returns a freshly-created dict.
91
92 eval_str controls whether or not values of type str are replaced
93 with the result of calling eval() on those values:
94
95 * If eval_str is true, eval() is called on values of type str.
96 * If eval_str is false (the default), values of type str are unchanged.
97
98 globals and locals are passed in to eval(); see the documentation
99 for eval() for more information. If either globals or locals is
100 None, this function may replace that value with a context-specific
101 default, contingent on type(obj):
102
103 * If obj is a module, globals defaults to obj.__dict__.
104 * If obj is a class, globals defaults to
105 sys.modules[obj.__module__].__dict__ and locals
106 defaults to the obj class namespace.
107 * If obj is a callable, globals defaults to obj.__globals__,
108 although if obj is a wrapped function (using
109 functools.update_wrapper()) it is first unwrapped.
110 """
111 if isinstance(obj, type):
112 # class
113 obj_dict = getattr(obj, '__dict__', None)
114 if obj_dict and hasattr(obj_dict, 'get'):
115 ann = obj_dict.get('__annotations__', None)
116 if isinstance(ann, types.GetSetDescriptorType):
117 ann = None
118 else:
119 ann = None
120
121 obj_globals = None
122 module_name = getattr(obj, '__module__', None)
123 if module_name:
124 module = sys.modules.get(module_name, None)
125 if module:
126 obj_globals = getattr(module, '__dict__', None)
127 obj_locals = dict(vars(obj))
128 unwrap = obj
129 elif isinstance(obj, types.ModuleType):
130 # module
131 ann = getattr(obj, '__annotations__', None)
132 obj_globals = getattr(obj, '__dict__')
133 obj_locals = None
134 unwrap = None
135 elif callable(obj):
136 # this includes types.Function, types.BuiltinFunctionType,
137 # types.BuiltinMethodType, functools.partial, functools.singledispatch,
138 # "class funclike" from Lib/test/test_inspect... on and on it goes.
139 ann = getattr(obj, '__annotations__', None)
140 obj_globals = getattr(obj, '__globals__', None)
141 obj_locals = None
142 unwrap = obj
143 else:
144 raise TypeError(f"{obj!r} is not a module, class, or callable.")
145
146 if ann is None:
147 return {}
148
149 if not isinstance(ann, dict):
150 raise ValueError(f"{obj!r}.__annotations__ is neither a dict nor None")
151
152 if not ann:
153 return {}
154
155 if not eval_str:
156 return dict(ann)
157
158 if unwrap is not None:
159 while True:
160 if hasattr(unwrap, '__wrapped__'):
161 unwrap = unwrap.__wrapped__
162 continue
163 if isinstance(unwrap, functools.partial):
164 unwrap = unwrap.func
165 continue
166 break
167 if hasattr(unwrap, "__globals__"):
168 obj_globals = unwrap.__globals__
169
170 if globals is None:
171 globals = obj_globals
172 if locals is None:
173 locals = obj_locals
174
175 return_value = {key:
176 value if not isinstance(value, str) else eval(value, globals, locals)
177 for key, value in ann.items() }
178 return return_value
179
180
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000181# ----------------------------------------------------------- type-checking
182def ismodule(object):
183 """Return true if the object is a module.
184
185 Module objects provide these attributes:
Barry Warsaw28a691b2010-04-17 00:19:56 +0000186 __cached__ pathname to byte compiled file
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000187 __doc__ documentation string
188 __file__ filename (missing for built-in modules)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000189 return isinstance(object, types.ModuleType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000190
191def isclass(object):
192 """Return true if the object is a class.
193
194 Class objects provide these attributes:
195 __doc__ documentation string
196 __module__ name of module in which this class was defined"""
Benjamin Petersonc4656002009-01-17 22:41:18 +0000197 return isinstance(object, type)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000198
199def ismethod(object):
200 """Return true if the object is an instance method.
201
202 Instance method objects provide these attributes:
203 __doc__ documentation string
204 __name__ name with which this method was defined
Christian Heimesff737952007-11-27 10:40:20 +0000205 __func__ function object containing implementation of method
206 __self__ instance to which this method is bound"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000207 return isinstance(object, types.MethodType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000208
Tim Peters536d2262001-09-20 05:13:38 +0000209def ismethoddescriptor(object):
Tim Petersf1d90b92001-09-20 05:47:55 +0000210 """Return true if the object is a method descriptor.
211
212 But not if ismethod() or isclass() or isfunction() are true.
Tim Peters536d2262001-09-20 05:13:38 +0000213
214 This is new in Python 2.2, and, for example, is true of int.__add__.
215 An object passing this test has a __get__ attribute but not a __set__
216 attribute, but beyond that the set of attributes varies. __name__ is
217 usually sensible, and __doc__ often is.
218
Tim Petersf1d90b92001-09-20 05:47:55 +0000219 Methods implemented via descriptors that also pass one of the other
220 tests return false from the ismethoddescriptor() test, simply because
221 the other tests promise more -- you can, e.g., count on having the
Christian Heimesff737952007-11-27 10:40:20 +0000222 __func__ attribute (etc) when an object passes ismethod()."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100223 if isclass(object) or ismethod(object) or isfunction(object):
224 # mutual exclusion
225 return False
226 tp = type(object)
227 return hasattr(tp, "__get__") and not hasattr(tp, "__set__")
Tim Peters536d2262001-09-20 05:13:38 +0000228
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000229def isdatadescriptor(object):
230 """Return true if the object is a data descriptor.
231
Aaron Hall, MBA4054b172018-05-20 19:46:42 -0400232 Data descriptors have a __set__ or a __delete__ attribute. Examples are
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000233 properties (defined in Python) and getsets and members (defined in C).
234 Typically, data descriptors will also have __name__ and __doc__ attributes
235 (properties, getsets, and members have both of these attributes), but this
236 is not guaranteed."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100237 if isclass(object) or ismethod(object) or isfunction(object):
238 # mutual exclusion
239 return False
240 tp = type(object)
Aaron Hall, MBA4054b172018-05-20 19:46:42 -0400241 return hasattr(tp, "__set__") or hasattr(tp, "__delete__")
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000242
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000243if hasattr(types, 'MemberDescriptorType'):
244 # CPython and equivalent
245 def ismemberdescriptor(object):
246 """Return true if the object is a member descriptor.
247
248 Member descriptors are specialized descriptors defined in extension
249 modules."""
250 return isinstance(object, types.MemberDescriptorType)
251else:
252 # Other implementations
253 def ismemberdescriptor(object):
254 """Return true if the object is a member descriptor.
255
256 Member descriptors are specialized descriptors defined in extension
257 modules."""
258 return False
259
260if hasattr(types, 'GetSetDescriptorType'):
261 # CPython and equivalent
262 def isgetsetdescriptor(object):
263 """Return true if the object is a getset descriptor.
264
265 getset descriptors are specialized descriptors defined in extension
266 modules."""
267 return isinstance(object, types.GetSetDescriptorType)
268else:
269 # Other implementations
270 def isgetsetdescriptor(object):
271 """Return true if the object is a getset descriptor.
272
273 getset descriptors are specialized descriptors defined in extension
274 modules."""
275 return False
276
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000277def isfunction(object):
278 """Return true if the object is a user-defined function.
279
280 Function objects provide these attributes:
281 __doc__ documentation string
282 __name__ name with which this function was defined
Neal Norwitz221085d2007-02-25 20:55:47 +0000283 __code__ code object containing compiled function bytecode
284 __defaults__ tuple of any default values for arguments
285 __globals__ global namespace in which this function was defined
286 __annotations__ dict of parameter annotations
287 __kwdefaults__ dict of keyword only parameters with defaults"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000288 return isinstance(object, types.FunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000289
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200290def _has_code_flag(f, flag):
291 """Return true if ``f`` is a function (or a method or functools.partial
292 wrapper wrapping a function) whose code object has the given ``flag``
293 set in its flags."""
294 while ismethod(f):
295 f = f.__func__
296 f = functools._unwrap_partial(f)
297 if not isfunction(f):
298 return False
299 return bool(f.__code__.co_flags & flag)
300
Pablo Galindo7cd25432018-10-26 12:19:14 +0100301def isgeneratorfunction(obj):
Christian Heimes7131fd92008-02-19 14:21:46 +0000302 """Return true if the object is a user-defined generator function.
303
Martin Panter0f0eac42016-09-07 11:04:41 +0000304 Generator function objects provide the same attributes as functions.
305 See help(isfunction) for a list of attributes."""
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200306 return _has_code_flag(obj, CO_GENERATOR)
Yury Selivanov75445082015-05-11 22:57:16 -0400307
Pablo Galindo7cd25432018-10-26 12:19:14 +0100308def iscoroutinefunction(obj):
Yury Selivanov75445082015-05-11 22:57:16 -0400309 """Return true if the object is a coroutine function.
310
Yury Selivanov4778e132016-11-08 12:23:09 -0500311 Coroutine functions are defined with "async def" syntax.
Yury Selivanov75445082015-05-11 22:57:16 -0400312 """
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200313 return _has_code_flag(obj, CO_COROUTINE)
Yury Selivanov75445082015-05-11 22:57:16 -0400314
Pablo Galindo7cd25432018-10-26 12:19:14 +0100315def isasyncgenfunction(obj):
Yury Selivanov4778e132016-11-08 12:23:09 -0500316 """Return true if the object is an asynchronous generator function.
317
318 Asynchronous generator functions are defined with "async def"
319 syntax and have "yield" expressions in their body.
320 """
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200321 return _has_code_flag(obj, CO_ASYNC_GENERATOR)
Yury Selivanoveb636452016-09-08 22:01:51 -0700322
323def isasyncgen(object):
Yury Selivanov4778e132016-11-08 12:23:09 -0500324 """Return true if the object is an asynchronous generator."""
Yury Selivanoveb636452016-09-08 22:01:51 -0700325 return isinstance(object, types.AsyncGeneratorType)
326
Christian Heimes7131fd92008-02-19 14:21:46 +0000327def isgenerator(object):
328 """Return true if the object is a generator.
329
330 Generator objects provide these attributes:
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300331 __iter__ defined to support iteration over container
Christian Heimes7131fd92008-02-19 14:21:46 +0000332 close raises a new GeneratorExit exception inside the
333 generator to terminate the iteration
334 gi_code code object
335 gi_frame frame object or possibly None once the generator has
336 been exhausted
337 gi_running set to 1 when generator is executing, 0 otherwise
338 next return the next item from the container
339 send resumes the generator and "sends" a value that becomes
340 the result of the current yield-expression
341 throw used to raise an exception inside the generator"""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400342 return isinstance(object, types.GeneratorType)
Yury Selivanov75445082015-05-11 22:57:16 -0400343
344def iscoroutine(object):
345 """Return true if the object is a coroutine."""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400346 return isinstance(object, types.CoroutineType)
Christian Heimes7131fd92008-02-19 14:21:46 +0000347
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400348def isawaitable(object):
Yury Selivanovc0215df2016-11-08 19:57:44 -0500349 """Return true if object can be passed to an ``await`` expression."""
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400350 return (isinstance(object, types.CoroutineType) or
351 isinstance(object, types.GeneratorType) and
Yury Selivanovc0215df2016-11-08 19:57:44 -0500352 bool(object.gi_code.co_flags & CO_ITERABLE_COROUTINE) or
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400353 isinstance(object, collections.abc.Awaitable))
354
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000355def istraceback(object):
356 """Return true if the object is a traceback.
357
358 Traceback objects provide these attributes:
359 tb_frame frame object at this level
360 tb_lasti index of last attempted instruction in bytecode
361 tb_lineno current line number in Python source code
362 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000363 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000364
365def isframe(object):
366 """Return true if the object is a frame object.
367
368 Frame objects provide these attributes:
369 f_back next outer frame object (this frame's caller)
370 f_builtins built-in namespace seen by this frame
371 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000372 f_globals global namespace seen by this frame
373 f_lasti index of last attempted instruction in bytecode
374 f_lineno current line number in Python source code
375 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000376 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000377 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000378
379def iscode(object):
380 """Return true if the object is a code object.
381
382 Code objects provide these attributes:
Xiang Zhanga6902e62017-04-13 10:38:28 +0800383 co_argcount number of arguments (not including *, ** args
384 or keyword only arguments)
385 co_code string of raw compiled bytecode
386 co_cellvars tuple of names of cell variables
387 co_consts tuple of constants used in the bytecode
388 co_filename name of file in which this code object was created
389 co_firstlineno number of first line in Python source code
390 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
391 | 16=nested | 32=generator | 64=nofree | 128=coroutine
392 | 256=iterable_coroutine | 512=async_generator
393 co_freevars tuple of names of free variables
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100394 co_posonlyargcount number of positional only arguments
Xiang Zhanga6902e62017-04-13 10:38:28 +0800395 co_kwonlyargcount number of keyword only arguments (not including ** arg)
396 co_lnotab encoded mapping of line numbers to bytecode indices
397 co_name name with which this code object was defined
Miss Islington (bot)402d5f32021-09-24 03:38:55 -0700398 co_names tuple of names other than arguments and function locals
Xiang Zhanga6902e62017-04-13 10:38:28 +0800399 co_nlocals number of local variables
400 co_stacksize virtual machine stack space required
401 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000402 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000403
404def isbuiltin(object):
405 """Return true if the object is a built-in function or method.
406
407 Built-in functions and methods provide these attributes:
408 __doc__ documentation string
409 __name__ original name of this function or method
410 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000411 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000412
413def isroutine(object):
414 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000415 return (isbuiltin(object)
416 or isfunction(object)
417 or ismethod(object)
418 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000419
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000420def isabstract(object):
421 """Return true if the object is an abstract base class (ABC)."""
Natefcfe80e2017-04-24 10:06:15 -0700422 if not isinstance(object, type):
423 return False
424 if object.__flags__ & TPFLAGS_IS_ABSTRACT:
425 return True
426 if not issubclass(type(object), abc.ABCMeta):
427 return False
428 if hasattr(object, '__abstractmethods__'):
429 # It looks like ABCMeta.__new__ has finished running;
430 # TPFLAGS_IS_ABSTRACT should have been accurate.
431 return False
432 # It looks like ABCMeta.__new__ has not finished running yet; we're
433 # probably in __init_subclass__. We'll look for abstractmethods manually.
434 for name, value in object.__dict__.items():
435 if getattr(value, "__isabstractmethod__", False):
436 return True
437 for base in object.__bases__:
438 for name in getattr(base, "__abstractmethods__", ()):
439 value = getattr(object, name, None)
440 if getattr(value, "__isabstractmethod__", False):
441 return True
442 return False
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000443
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000444def getmembers(object, predicate=None):
445 """Return all members of an object as (name, value) pairs sorted by name.
446 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100447 if isclass(object):
448 mro = (object,) + getmro(object)
449 else:
450 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000451 results = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700452 processed = set()
453 names = dir(object)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700454 # :dd any DynamicClassAttributes to the list of names if object is a class;
Ethan Furmane03ea372013-09-25 07:14:41 -0700455 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700456 # attribute with the same name as a DynamicClassAttribute exists
Ethan Furmane03ea372013-09-25 07:14:41 -0700457 try:
458 for base in object.__bases__:
459 for k, v in base.__dict__.items():
460 if isinstance(v, types.DynamicClassAttribute):
461 names.append(k)
462 except AttributeError:
463 pass
464 for key in names:
Ethan Furman63c141c2013-10-18 00:27:39 -0700465 # First try to get the value via getattr. Some descriptors don't
466 # like calling their __get__ (see bug #1785), so fall back to
467 # looking in the __dict__.
468 try:
469 value = getattr(object, key)
470 # handle the duplicate key
471 if key in processed:
472 raise AttributeError
473 except AttributeError:
474 for base in mro:
475 if key in base.__dict__:
476 value = base.__dict__[key]
477 break
478 else:
479 # could be a (currently) missing slot member, or a buggy
480 # __dir__; discard and move on
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100481 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000482 if not predicate or predicate(value):
483 results.append((key, value))
Ethan Furmane03ea372013-09-25 07:14:41 -0700484 processed.add(key)
485 results.sort(key=lambda pair: pair[0])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000486 return results
487
Christian Heimes25bb7832008-01-11 16:17:00 +0000488Attribute = namedtuple('Attribute', 'name kind defining_class object')
489
Tim Peters13b49d32001-09-23 02:00:29 +0000490def classify_class_attrs(cls):
491 """Return list of attribute-descriptor tuples.
492
493 For each name in dir(cls), the return list contains a 4-tuple
494 with these elements:
495
496 0. The name (a string).
497
498 1. The kind of attribute this is, one of these strings:
499 'class method' created via classmethod()
500 'static method' created via staticmethod()
501 'property' created via property()
Ethan Furmane03ea372013-09-25 07:14:41 -0700502 'method' any other flavor of method or descriptor
Tim Peters13b49d32001-09-23 02:00:29 +0000503 'data' not a method
504
505 2. The class which defined this attribute (a class).
506
Ethan Furmane03ea372013-09-25 07:14:41 -0700507 3. The object as obtained by calling getattr; if this fails, or if the
508 resulting object does not live anywhere in the class' mro (including
509 metaclasses) then the object is looked up in the defining class's
510 dict (found by walking the mro).
Ethan Furman668dede2013-09-14 18:53:26 -0700511
512 If one of the items in dir(cls) is stored in the metaclass it will now
513 be discovered and not have None be listed as the class in which it was
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700514 defined. Any items whose home class cannot be discovered are skipped.
Tim Peters13b49d32001-09-23 02:00:29 +0000515 """
516
517 mro = getmro(cls)
Ethan Furman668dede2013-09-14 18:53:26 -0700518 metamro = getmro(type(cls)) # for attributes stored in the metaclass
Jon Dufresne39726282017-05-18 07:35:54 -0700519 metamro = tuple(cls for cls in metamro if cls not in (type, object))
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700520 class_bases = (cls,) + mro
521 all_bases = class_bases + metamro
Tim Peters13b49d32001-09-23 02:00:29 +0000522 names = dir(cls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700523 # :dd any DynamicClassAttributes to the list of names;
Ethan Furmane03ea372013-09-25 07:14:41 -0700524 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700525 # attribute with the same name as a DynamicClassAttribute exists.
Ethan Furman63c141c2013-10-18 00:27:39 -0700526 for base in mro:
Ethan Furmane03ea372013-09-25 07:14:41 -0700527 for k, v in base.__dict__.items():
Ethan Furmanc314e602021-01-12 23:47:57 -0800528 if isinstance(v, types.DynamicClassAttribute) and v.fget is not None:
Ethan Furmane03ea372013-09-25 07:14:41 -0700529 names.append(k)
Tim Peters13b49d32001-09-23 02:00:29 +0000530 result = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700531 processed = set()
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700532
Tim Peters13b49d32001-09-23 02:00:29 +0000533 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100534 # Get the object associated with the name, and where it was defined.
Ethan Furmane03ea372013-09-25 07:14:41 -0700535 # Normal objects will be looked up with both getattr and directly in
536 # its class' dict (in case getattr fails [bug #1785], and also to look
537 # for a docstring).
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700538 # For DynamicClassAttributes on the second pass we only look in the
Ethan Furmane03ea372013-09-25 07:14:41 -0700539 # class's dict.
540 #
Tim Peters13b49d32001-09-23 02:00:29 +0000541 # Getting an obj from the __dict__ sometimes reveals more than
542 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100543 homecls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700544 get_obj = None
545 dict_obj = None
Ethan Furmane03ea372013-09-25 07:14:41 -0700546 if name not in processed:
547 try:
Ethan Furmana8b07072013-10-18 01:22:08 -0700548 if name == '__dict__':
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700549 raise Exception("__dict__ is special, don't want the proxy")
Ethan Furmane03ea372013-09-25 07:14:41 -0700550 get_obj = getattr(cls, name)
551 except Exception as exc:
552 pass
553 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700554 homecls = getattr(get_obj, "__objclass__", homecls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700555 if homecls not in class_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700556 # if the resulting object does not live somewhere in the
Ethan Furman63c141c2013-10-18 00:27:39 -0700557 # mro, drop it and search the mro manually
Ethan Furmane03ea372013-09-25 07:14:41 -0700558 homecls = None
Ethan Furman63c141c2013-10-18 00:27:39 -0700559 last_cls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700560 # first look in the classes
561 for srch_cls in class_bases:
Ethan Furman63c141c2013-10-18 00:27:39 -0700562 srch_obj = getattr(srch_cls, name, None)
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400563 if srch_obj is get_obj:
Ethan Furman63c141c2013-10-18 00:27:39 -0700564 last_cls = srch_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700565 # then check the metaclasses
566 for srch_cls in metamro:
567 try:
568 srch_obj = srch_cls.__getattr__(cls, name)
569 except AttributeError:
570 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400571 if srch_obj is get_obj:
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700572 last_cls = srch_cls
Ethan Furman63c141c2013-10-18 00:27:39 -0700573 if last_cls is not None:
574 homecls = last_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700575 for base in all_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700576 if name in base.__dict__:
577 dict_obj = base.__dict__[name]
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700578 if homecls not in metamro:
579 homecls = base
Ethan Furmane03ea372013-09-25 07:14:41 -0700580 break
Ethan Furman63c141c2013-10-18 00:27:39 -0700581 if homecls is None:
582 # unable to locate the attribute anywhere, most likely due to
583 # buggy custom __dir__; discard and move on
584 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400585 obj = get_obj if get_obj is not None else dict_obj
Ethan Furmane03ea372013-09-25 07:14:41 -0700586 # Classify the object or its descriptor.
Serhiy Storchaka3327a2d2017-12-15 14:13:41 +0200587 if isinstance(dict_obj, (staticmethod, types.BuiltinMethodType)):
Tim Peters13b49d32001-09-23 02:00:29 +0000588 kind = "static method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700589 obj = dict_obj
Serhiy Storchaka3327a2d2017-12-15 14:13:41 +0200590 elif isinstance(dict_obj, (classmethod, types.ClassMethodDescriptorType)):
Tim Peters13b49d32001-09-23 02:00:29 +0000591 kind = "class method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700592 obj = dict_obj
593 elif isinstance(dict_obj, property):
Tim Peters13b49d32001-09-23 02:00:29 +0000594 kind = "property"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700595 obj = dict_obj
Yury Selivanov0860a0b2014-01-31 14:28:44 -0500596 elif isroutine(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000597 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100598 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700599 kind = "data"
Christian Heimes25bb7832008-01-11 16:17:00 +0000600 result.append(Attribute(name, kind, homecls, obj))
Ethan Furmane03ea372013-09-25 07:14:41 -0700601 processed.add(name)
Tim Peters13b49d32001-09-23 02:00:29 +0000602 return result
603
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000604# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000605
606def getmro(cls):
607 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000608 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000609
Nick Coghlane8c45d62013-07-28 20:00:01 +1000610# -------------------------------------------------------- function helpers
611
612def unwrap(func, *, stop=None):
613 """Get the object wrapped by *func*.
614
615 Follows the chain of :attr:`__wrapped__` attributes returning the last
616 object in the chain.
617
618 *stop* is an optional callback accepting an object in the wrapper chain
619 as its sole argument that allows the unwrapping to be terminated early if
620 the callback returns a true value. If the callback never returns a true
621 value, the last object in the chain is returned as usual. For example,
622 :func:`signature` uses this to stop unwrapping if any object in the
623 chain has a ``__signature__`` attribute defined.
624
625 :exc:`ValueError` is raised if a cycle is encountered.
626
627 """
628 if stop is None:
629 def _is_wrapper(f):
630 return hasattr(f, '__wrapped__')
631 else:
632 def _is_wrapper(f):
633 return hasattr(f, '__wrapped__') and not stop(f)
634 f = func # remember the original func for error reporting
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100635 # Memoise by id to tolerate non-hashable objects, but store objects to
636 # ensure they aren't destroyed, which would allow their IDs to be reused.
637 memo = {id(f): f}
638 recursion_limit = sys.getrecursionlimit()
Nick Coghlane8c45d62013-07-28 20:00:01 +1000639 while _is_wrapper(func):
640 func = func.__wrapped__
641 id_func = id(func)
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100642 if (id_func in memo) or (len(memo) >= recursion_limit):
Nick Coghlane8c45d62013-07-28 20:00:01 +1000643 raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100644 memo[id_func] = func
Nick Coghlane8c45d62013-07-28 20:00:01 +1000645 return func
646
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000647# -------------------------------------------------- source code extraction
648def indentsize(line):
649 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000650 expline = line.expandtabs()
651 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000652
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300653def _findclass(func):
654 cls = sys.modules.get(func.__module__)
655 if cls is None:
656 return None
657 for name in func.__qualname__.split('.')[:-1]:
658 cls = getattr(cls, name)
659 if not isclass(cls):
660 return None
661 return cls
662
663def _finddoc(obj):
Serhiy Storchaka08b47c32020-05-18 20:25:07 +0300664 if isclass(obj):
665 for base in obj.__mro__:
666 if base is not object:
667 try:
668 doc = base.__doc__
669 except AttributeError:
670 continue
671 if doc is not None:
672 return doc
673 return None
674
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300675 if ismethod(obj):
676 name = obj.__func__.__name__
677 self = obj.__self__
678 if (isclass(self) and
679 getattr(getattr(self, name, None), '__func__') is obj.__func__):
680 # classmethod
681 cls = self
682 else:
683 cls = self.__class__
684 elif isfunction(obj):
685 name = obj.__name__
686 cls = _findclass(obj)
687 if cls is None or getattr(cls, name) is not obj:
688 return None
689 elif isbuiltin(obj):
690 name = obj.__name__
691 self = obj.__self__
692 if (isclass(self) and
693 self.__qualname__ + '.' + name == obj.__qualname__):
694 # classmethod
695 cls = self
696 else:
697 cls = self.__class__
Serhiy Storchakaac4bdcc2015-10-29 08:15:50 +0200698 # Should be tested before isdatadescriptor().
699 elif isinstance(obj, property):
700 func = obj.fget
701 name = func.__name__
702 cls = _findclass(func)
703 if cls is None or getattr(cls, name) is not obj:
704 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300705 elif ismethoddescriptor(obj) or isdatadescriptor(obj):
706 name = obj.__name__
707 cls = obj.__objclass__
708 if getattr(cls, name) is not obj:
709 return None
Raymond Hettingerd1e768a2019-03-25 13:01:13 -0700710 if ismemberdescriptor(obj):
711 slots = getattr(cls, '__slots__', None)
712 if isinstance(slots, dict) and name in slots:
713 return slots[name]
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300714 else:
715 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300716 for base in cls.__mro__:
717 try:
Serhiy Storchaka08b47c32020-05-18 20:25:07 +0300718 doc = getattr(base, name).__doc__
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300719 except AttributeError:
720 continue
721 if doc is not None:
722 return doc
723 return None
724
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000725def getdoc(object):
726 """Get the documentation string for an object.
727
728 All tabs are expanded to spaces. To clean up docstrings that are
729 indented to line up with blocks of code, any whitespace than can be
730 uniformly removed from the second line onwards is removed."""
Serhiy Storchaka08b47c32020-05-18 20:25:07 +0300731 try:
732 doc = object.__doc__
733 except AttributeError:
734 return None
Serhiy Storchaka5cf2b7252015-04-03 22:38:53 +0300735 if doc is None:
736 try:
737 doc = _finddoc(object)
738 except (AttributeError, TypeError):
739 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000740 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000741 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000742 return cleandoc(doc)
743
744def cleandoc(doc):
745 """Clean up indentation from docstrings.
746
747 Any whitespace that can be uniformly removed from the second line
748 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000749 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000750 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000751 except UnicodeError:
752 return None
753 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000754 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000755 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000756 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000757 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000758 if content:
759 indent = len(line) - content
760 margin = min(margin, indent)
761 # Remove indentation.
762 if lines:
763 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000764 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000765 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000766 # Remove any trailing or leading blank lines.
767 while lines and not lines[-1]:
768 lines.pop()
769 while lines and not lines[0]:
770 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000771 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000772
773def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000774 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000775 if ismodule(object):
Jason R. Coombsb9650a02018-03-05 18:29:08 -0500776 if getattr(object, '__file__', None):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000777 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000778 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000779 if isclass(object):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500780 if hasattr(object, '__module__'):
Philipp Ad407d2a2019-06-08 14:05:46 +0200781 module = sys.modules.get(object.__module__)
782 if getattr(module, '__file__', None):
783 return module.__file__
Miss Islington (bot)f468ede2021-07-30 10:46:42 -0700784 if object.__module__ == '__main__':
785 raise OSError('source code not available')
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000786 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000787 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000788 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000789 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000790 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000791 if istraceback(object):
792 object = object.tb_frame
793 if isframe(object):
794 object = object.f_code
795 if iscode(object):
796 return object.co_filename
Thomas Kluyvere968bc732017-10-24 13:42:36 +0100797 raise TypeError('module, class, method, function, traceback, frame, or '
798 'code object was expected, got {}'.format(
799 type(object).__name__))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000800
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000801def getmodulename(path):
802 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000803 fname = os.path.basename(path)
804 # Check for paths that look like an actual module file
805 suffixes = [(-len(suffix), suffix)
806 for suffix in importlib.machinery.all_suffixes()]
807 suffixes.sort() # try longest suffixes first, in case they overlap
808 for neglen, suffix in suffixes:
809 if fname.endswith(suffix):
810 return fname[:neglen]
811 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000812
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000813def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000814 """Return the filename that can be used to locate an object's source.
815 Return None if no way can be identified to get the source.
816 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000817 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400818 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
819 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
820 if any(filename.endswith(s) for s in all_bytecode_suffixes):
821 filename = (os.path.splitext(filename)[0] +
822 importlib.machinery.SOURCE_SUFFIXES[0])
823 elif any(filename.endswith(s) for s in
824 importlib.machinery.EXTENSION_SUFFIXES):
825 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000826 if os.path.exists(filename):
827 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000828 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon825ac382020-11-06 18:45:56 -0800829 module = getmodule(object, filename)
830 if getattr(module, '__loader__', None) is not None:
831 return filename
832 elif getattr(getattr(module, "__spec__", None), "loader", None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000833 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000834 # or it is in the linecache
Brett Cannon825ac382020-11-06 18:45:56 -0800835 elif filename in linecache.cache:
R. David Murraya1b37402010-06-17 02:04:29 +0000836 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000837
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000838def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000839 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000840
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000841 The idea is for each object to have a unique origin, so this routine
842 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000843 if _filename is None:
844 _filename = getsourcefile(object) or getfile(object)
845 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000846
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000847modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000848_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000849
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000850def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000851 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000852 if ismodule(object):
853 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000854 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000855 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000856 # Try the filename to modulename cache
857 if _filename is not None and _filename in modulesbyfile:
858 return sys.modules.get(modulesbyfile[_filename])
859 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000860 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000861 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000862 except TypeError:
863 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000864 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000865 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000866 # Update the filename to module name cache and check yet again
867 # Copy sys.modules in order to cope with changes while iterating
Gregory P. Smith85cf1d52020-03-04 16:45:22 -0800868 for modname, module in sys.modules.copy().items():
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000869 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000870 f = module.__file__
871 if f == _filesbymodname.get(modname, None):
872 # Have already mapped this module, so skip it
873 continue
874 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000875 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000876 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000877 modulesbyfile[f] = modulesbyfile[
878 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000879 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000880 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000881 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000882 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000883 if not hasattr(object, '__name__'):
884 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000885 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000886 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000887 if mainobject is object:
888 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000889 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000890 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000891 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000892 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000893 if builtinobject is object:
894 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000895
Karthikeyan Singaravelan696136b2020-04-18 21:49:32 +0530896
897class ClassFoundException(Exception):
898 pass
899
900
901class _ClassFinder(ast.NodeVisitor):
902
903 def __init__(self, qualname):
904 self.stack = []
905 self.qualname = qualname
906
907 def visit_FunctionDef(self, node):
908 self.stack.append(node.name)
909 self.stack.append('<locals>')
910 self.generic_visit(node)
911 self.stack.pop()
912 self.stack.pop()
913
914 visit_AsyncFunctionDef = visit_FunctionDef
915
916 def visit_ClassDef(self, node):
917 self.stack.append(node.name)
918 if self.qualname == '.'.join(self.stack):
919 # Return the decorator for the class if present
920 if node.decorator_list:
921 line_number = node.decorator_list[0].lineno
922 else:
923 line_number = node.lineno
924
925 # decrement by one since lines starts with indexing by zero
926 line_number -= 1
927 raise ClassFoundException(line_number)
928 self.generic_visit(node)
929 self.stack.pop()
930
931
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000932def findsource(object):
933 """Return the entire source file and starting line number for an object.
934
935 The argument may be a module, class, method, function, traceback, frame,
936 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200937 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000938 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500939
Yury Selivanovef1e7502014-12-08 16:05:34 -0500940 file = getsourcefile(object)
941 if file:
942 # Invalidate cache if needed.
943 linecache.checkcache(file)
944 else:
945 file = getfile(object)
946 # Allow filenames in form of "<something>" to pass through.
947 # `doctest` monkeypatches `linecache` module to enable
948 # inspection, so let `linecache.getlines` to be called.
949 if not (file.startswith('<') and file.endswith('>')):
950 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500951
Thomas Wouters89f507f2006-12-13 04:49:30 +0000952 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953 if module:
954 lines = linecache.getlines(file, module.__dict__)
955 else:
956 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000957 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200958 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000959
960 if ismodule(object):
961 return lines, 0
962
963 if isclass(object):
Karthikeyan Singaravelan696136b2020-04-18 21:49:32 +0530964 qualname = object.__qualname__
965 source = ''.join(lines)
966 tree = ast.parse(source)
967 class_finder = _ClassFinder(qualname)
968 try:
969 class_finder.visit(tree)
970 except ClassFoundException as e:
971 line_number = e.args[0]
972 return lines, line_number
Jeremy Hyltonab919022003-06-27 18:41:20 +0000973 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200974 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000975
976 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000977 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000978 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000979 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000980 if istraceback(object):
981 object = object.tb_frame
982 if isframe(object):
983 object = object.f_code
984 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000985 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200986 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000987 lnum = object.co_firstlineno - 1
Yury Selivanove4e811d2015-07-21 19:01:52 +0300988 pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000989 while lnum > 0:
Irit Katriel2e0760b2020-12-04 21:22:03 +0000990 try:
991 line = lines[lnum]
992 except IndexError:
993 raise OSError('lineno is out of bounds')
994 if pat.match(line):
995 break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000996 lnum = lnum - 1
997 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200998 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000999
1000def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +00001001 """Get lines of comments immediately preceding an object's source code.
1002
1003 Returns None when source can't be found.
1004 """
1005 try:
1006 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001007 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +00001008 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001009
1010 if ismodule(object):
1011 # Look for a comment block at the top of the file.
1012 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +00001013 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001014 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001015 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +00001016 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001017 comments = []
1018 end = start
1019 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001020 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001021 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001022 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001023
1024 # Look for a preceding block of comments at the same indentation.
1025 elif lnum > 0:
1026 indent = indentsize(lines[lnum])
1027 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001028 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001029 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001030 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001031 if end > 0:
1032 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001033 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001034 while comment[:1] == '#' and indentsize(lines[end]) == indent:
1035 comments[:0] = [comment]
1036 end = end - 1
1037 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001038 comment = lines[end].expandtabs().lstrip()
1039 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001040 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001041 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001042 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001043 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001044
Tim Peters4efb6e92001-06-29 23:51:08 +00001045class EndOfBlock(Exception): pass
1046
1047class BlockFinder:
1048 """Provide a tokeneater() method to detect the end of a code block."""
1049 def __init__(self):
1050 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +00001051 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +00001052 self.started = False
1053 self.passline = False
Meador Inge5b718d72015-07-23 22:49:37 -05001054 self.indecorator = False
1055 self.decoratorhasargs = False
Armin Rigodd5c0232005-09-25 11:45:45 +00001056 self.last = 1
Irit Katriel6e1eec72020-12-04 16:45:38 +00001057 self.body_col0 = None
Tim Peters4efb6e92001-06-29 23:51:08 +00001058
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001059 def tokeneater(self, type, token, srowcol, erowcol, line):
Meador Inge5b718d72015-07-23 22:49:37 -05001060 if not self.started and not self.indecorator:
1061 # skip any decorators
1062 if token == "@":
1063 self.indecorator = True
Armin Rigodd5c0232005-09-25 11:45:45 +00001064 # look for the first "def", "class" or "lambda"
Meador Inge5b718d72015-07-23 22:49:37 -05001065 elif token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +00001066 if token == "lambda":
1067 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +00001068 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +00001069 self.passline = True # skip to the end of the line
Meador Inge5b718d72015-07-23 22:49:37 -05001070 elif token == "(":
1071 if self.indecorator:
1072 self.decoratorhasargs = True
1073 elif token == ")":
1074 if self.indecorator:
1075 self.indecorator = False
1076 self.decoratorhasargs = False
Tim Peters4efb6e92001-06-29 23:51:08 +00001077 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +00001078 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001079 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +00001080 if self.islambda: # lambdas always end at the first NEWLINE
1081 raise EndOfBlock
Meador Inge5b718d72015-07-23 22:49:37 -05001082 # hitting a NEWLINE when in a decorator without args
1083 # ends the decorator
1084 if self.indecorator and not self.decoratorhasargs:
1085 self.indecorator = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +00001086 elif self.passline:
1087 pass
Tim Peters4efb6e92001-06-29 23:51:08 +00001088 elif type == tokenize.INDENT:
Irit Katriel6e1eec72020-12-04 16:45:38 +00001089 if self.body_col0 is None and self.started:
1090 self.body_col0 = erowcol[1]
Tim Peters4efb6e92001-06-29 23:51:08 +00001091 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +00001092 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +00001093 elif type == tokenize.DEDENT:
1094 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +00001095 # the end of matching indent/dedent pairs end a block
1096 # (note that this only works for "def"/"class" blocks,
1097 # not e.g. for "if: else:" or "try: finally:" blocks)
1098 if self.indent <= 0:
1099 raise EndOfBlock
Irit Katriel6e1eec72020-12-04 16:45:38 +00001100 elif type == tokenize.COMMENT:
1101 if self.body_col0 is not None and srowcol[1] >= self.body_col0:
1102 # Include comments if indented at least as much as the block
1103 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +00001104 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
1105 # any other token on the same indentation level end the previous
1106 # block as well, except the pseudo-tokens COMMENT and NL.
1107 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +00001108
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001109def getblock(lines):
1110 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +00001111 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +00001112 try:
Trent Nelson428de652008-03-18 22:41:35 +00001113 tokens = tokenize.generate_tokens(iter(lines).__next__)
1114 for _token in tokens:
1115 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +00001116 except (EndOfBlock, IndentationError):
1117 pass
1118 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001119
1120def getsourcelines(object):
1121 """Return a list of source lines and starting line number for an object.
1122
1123 The argument may be a module, class, method, function, traceback, frame,
1124 or code object. The source code is returned as a list of the lines
1125 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001126 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001127 raised if the source code cannot be retrieved."""
Yury Selivanov081bbf62014-09-26 17:34:54 -04001128 object = unwrap(object)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001129 lines, lnum = findsource(object)
1130
Vladimir Matveev91cb2982018-08-24 07:18:00 -07001131 if istraceback(object):
1132 object = object.tb_frame
1133
1134 # for module or frame that corresponds to module, return all source lines
1135 if (ismodule(object) or
1136 (isframe(object) and object.f_code.co_name == "<module>")):
Meador Inge5b718d72015-07-23 22:49:37 -05001137 return lines, 0
1138 else:
1139 return getblock(lines[lnum:]), lnum + 1
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001140
1141def getsource(object):
1142 """Return the text of the source code for an object.
1143
1144 The argument may be a module, class, method, function, traceback, frame,
1145 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001146 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001147 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001148 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001149
1150# --------------------------------------------------- class tree extraction
1151def walktree(classes, children, parent):
1152 """Recursive helper function for getclasstree()."""
1153 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +00001154 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001155 for c in classes:
1156 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +00001157 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001158 results.append(walktree(children[c], children, c))
1159 return results
1160
Georg Brandl5ce83a02009-06-01 17:23:51 +00001161def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001162 """Arrange the given list of classes into a hierarchy of nested lists.
1163
1164 Where a nested list appears, it contains classes derived from the class
1165 whose entry immediately precedes the list. Each entry is a 2-tuple
1166 containing a class and a tuple of its base classes. If the 'unique'
1167 argument is true, exactly one entry appears in the returned structure
1168 for each class in the given list. Otherwise, classes using multiple
1169 inheritance and their descendants will appear multiple times."""
1170 children = {}
1171 roots = []
1172 for c in classes:
1173 if c.__bases__:
1174 for parent in c.__bases__:
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05301175 if parent not in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001176 children[parent] = []
Serhiy Storchaka362c1b52013-09-05 17:14:32 +03001177 if c not in children[parent]:
1178 children[parent].append(c)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001179 if unique and parent in classes: break
1180 elif c not in roots:
1181 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +00001182 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001183 if parent not in classes:
1184 roots.append(parent)
1185 return walktree(roots, children, None)
1186
1187# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001188Arguments = namedtuple('Arguments', 'args, varargs, varkw')
1189
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001190def getargs(co):
1191 """Get information about the arguments accepted by a code object.
1192
Guido van Rossum2e65f892007-02-28 22:03:49 +00001193 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001194 'args' is the list of argument names. Keyword-only arguments are
1195 appended. 'varargs' and 'varkw' are the names of the * and **
1196 arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +00001197 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001198 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001199
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001200 names = co.co_varnames
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001201 nargs = co.co_argcount
Guido van Rossum2e65f892007-02-28 22:03:49 +00001202 nkwargs = co.co_kwonlyargcount
Pablo Galindocd74e662019-06-01 18:08:04 +01001203 args = list(names[:nargs])
1204 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001205 step = 0
1206
Guido van Rossum2e65f892007-02-28 22:03:49 +00001207 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001208 varargs = None
1209 if co.co_flags & CO_VARARGS:
1210 varargs = co.co_varnames[nargs]
1211 nargs = nargs + 1
1212 varkw = None
1213 if co.co_flags & CO_VARKEYWORDS:
1214 varkw = co.co_varnames[nargs]
Pablo Galindocd74e662019-06-01 18:08:04 +01001215 return Arguments(args + kwonlyargs, varargs, varkw)
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001216
1217ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
1218
1219def getargspec(func):
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001220 """Get the names and default values of a function's parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001221
1222 A tuple of four things is returned: (args, varargs, keywords, defaults).
1223 'args' is a list of the argument names, including keyword-only argument names.
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001224 'varargs' and 'keywords' are the names of the * and ** parameters or None.
1225 'defaults' is an n-tuple of the default values of the last n parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001226
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001227 This function is deprecated, as it does not support annotations or
1228 keyword-only parameters and will raise ValueError if either is present
1229 on the supplied callable.
1230
1231 For a more structured introspection API, use inspect.signature() instead.
1232
1233 Alternatively, use getfullargspec() for an API with a similar namedtuple
1234 based interface, but full support for annotations and keyword-only
1235 parameters.
Matthias Bussonnierded87d82018-10-19 16:40:45 -07001236
1237 Deprecated since Python 3.5, use `inspect.getfullargspec()`.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001238 """
Matthias Bussonnierded87d82018-10-19 16:40:45 -07001239 warnings.warn("inspect.getargspec() is deprecated since Python 3.0, "
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001240 "use inspect.signature() or inspect.getfullargspec()",
1241 DeprecationWarning, stacklevel=2)
Pablo Galindod5d2b452019-04-30 02:01:14 +01001242 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
1243 getfullargspec(func)
1244 if kwonlyargs or ann:
1245 raise ValueError("Function has keyword-only parameters or annotations"
1246 ", use inspect.signature() API which can support them")
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001247 return ArgSpec(args, varargs, varkw, defaults)
1248
Christian Heimes25bb7832008-01-11 16:17:00 +00001249FullArgSpec = namedtuple('FullArgSpec',
Pablo Galindod5d2b452019-04-30 02:01:14 +01001250 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001251
1252def getfullargspec(func):
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001253 """Get the names and default values of a callable object's parameters.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001254
Brett Cannon504d8852007-09-07 02:12:14 +00001255 A tuple of seven things is returned:
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001256 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations).
1257 'args' is a list of the parameter names.
1258 'varargs' and 'varkw' are the names of the * and ** parameters or None.
1259 'defaults' is an n-tuple of the default values of the last n parameters.
1260 'kwonlyargs' is a list of keyword-only parameter names.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001261 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001262 'annotations' is a dictionary mapping parameter names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001263
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001264 Notable differences from inspect.signature():
1265 - the "self" parameter is always reported, even for bound methods
1266 - wrapper chains defined by __wrapped__ *not* unwrapped automatically
Jeremy Hylton64967882003-06-27 18:14:39 +00001267 """
Yury Selivanov57d240e2014-02-19 16:27:23 -05001268 try:
1269 # Re: `skip_bound_arg=False`
1270 #
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001271 # There is a notable difference in behaviour between getfullargspec
1272 # and Signature: the former always returns 'self' parameter for bound
1273 # methods, whereas the Signature always shows the actual calling
1274 # signature of the passed object.
1275 #
1276 # To simulate this behaviour, we "unbind" bound methods, to trick
1277 # inspect.signature to always return their first parameter ("self",
1278 # usually)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001279
Yury Selivanov57d240e2014-02-19 16:27:23 -05001280 # Re: `follow_wrapper_chains=False`
1281 #
1282 # getfullargspec() historically ignored __wrapped__ attributes,
1283 # so we ensure that remains the case in 3.3+
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001284
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001285 sig = _signature_from_callable(func,
1286 follow_wrapper_chains=False,
1287 skip_bound_arg=False,
larryhastings74613a42021-04-29 21:16:28 -07001288 sigcls=Signature,
1289 eval_str=False)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001290 except Exception as ex:
1291 # Most of the times 'signature' will raise ValueError.
1292 # But, it can also raise AttributeError, and, maybe something
1293 # else. So to be fully backwards compatible, we catch all
1294 # possible exceptions here, and reraise a TypeError.
1295 raise TypeError('unsupported callable') from ex
1296
1297 args = []
1298 varargs = None
1299 varkw = None
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001300 posonlyargs = []
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001301 kwonlyargs = []
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001302 annotations = {}
1303 defaults = ()
1304 kwdefaults = {}
1305
1306 if sig.return_annotation is not sig.empty:
1307 annotations['return'] = sig.return_annotation
1308
1309 for param in sig.parameters.values():
1310 kind = param.kind
1311 name = param.name
1312
1313 if kind is _POSITIONAL_ONLY:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001314 posonlyargs.append(name)
1315 if param.default is not param.empty:
1316 defaults += (param.default,)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001317 elif kind is _POSITIONAL_OR_KEYWORD:
1318 args.append(name)
1319 if param.default is not param.empty:
1320 defaults += (param.default,)
1321 elif kind is _VAR_POSITIONAL:
1322 varargs = name
1323 elif kind is _KEYWORD_ONLY:
1324 kwonlyargs.append(name)
1325 if param.default is not param.empty:
1326 kwdefaults[name] = param.default
1327 elif kind is _VAR_KEYWORD:
1328 varkw = name
1329
1330 if param.annotation is not param.empty:
1331 annotations[name] = param.annotation
1332
1333 if not kwdefaults:
1334 # compatibility with 'func.__kwdefaults__'
1335 kwdefaults = None
1336
1337 if not defaults:
1338 # compatibility with 'func.__defaults__'
1339 defaults = None
1340
Pablo Galindod5d2b452019-04-30 02:01:14 +01001341 return FullArgSpec(posonlyargs + args, varargs, varkw, defaults,
1342 kwonlyargs, kwdefaults, annotations)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001343
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001344
Christian Heimes25bb7832008-01-11 16:17:00 +00001345ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
1346
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001347def getargvalues(frame):
1348 """Get information about arguments passed into a particular frame.
1349
1350 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001351 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001352 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1353 'locals' is the locals dictionary of the given frame."""
1354 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +00001355 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001356
Guido van Rossum2e65f892007-02-28 22:03:49 +00001357def formatannotation(annotation, base_module=None):
Guido van Rossum52e50042016-10-22 07:55:18 -07001358 if getattr(annotation, '__module__', None) == 'typing':
1359 return repr(annotation).replace('typing.', '')
Miss Islington (bot)ce7a6af2021-10-27 14:57:07 -07001360 if isinstance(annotation, types.GenericAlias):
1361 return str(annotation)
Guido van Rossum2e65f892007-02-28 22:03:49 +00001362 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001363 if annotation.__module__ in ('builtins', base_module):
Serhiy Storchaka521e5862014-07-22 15:00:37 +03001364 return annotation.__qualname__
1365 return annotation.__module__+'.'+annotation.__qualname__
Guido van Rossum2e65f892007-02-28 22:03:49 +00001366 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001367
Guido van Rossum2e65f892007-02-28 22:03:49 +00001368def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001369 module = getattr(object, '__module__', None)
1370 def _formatannotation(annotation):
1371 return formatannotation(annotation, module)
1372 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001373
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001374def formatargspec(args, varargs=None, varkw=None, defaults=None,
Pablo Galindod5d2b452019-04-30 02:01:14 +01001375 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001376 formatarg=str,
1377 formatvarargs=lambda name: '*' + name,
1378 formatvarkw=lambda name: '**' + name,
1379 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001380 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001381 formatannotation=formatannotation):
Berker Peksagfa3922c2015-07-31 04:11:29 +03001382 """Format an argument spec from the values returned by getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001383
Guido van Rossum2e65f892007-02-28 22:03:49 +00001384 The first seven arguments are (args, varargs, varkw, defaults,
1385 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1386 are the corresponding optional formatting functions that are called to
1387 turn names and values into strings. The last argument is an optional
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +02001388 function to format the sequence of arguments.
1389
1390 Deprecated since Python 3.5: use the `signature` function and `Signature`
1391 objects.
1392 """
1393
1394 from warnings import warn
1395
1396 warn("`formatargspec` is deprecated since Python 3.5. Use `signature` and "
Zackery Spytz41254eb2018-06-11 21:16:18 -06001397 "the `Signature` object directly",
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +02001398 DeprecationWarning,
1399 stacklevel=2)
1400
Guido van Rossum2e65f892007-02-28 22:03:49 +00001401 def formatargandannotation(arg):
1402 result = formatarg(arg)
1403 if arg in annotations:
1404 result += ': ' + formatannotation(annotations[arg])
1405 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001406 specs = []
1407 if defaults:
Pablo Galindod5d2b452019-04-30 02:01:14 +01001408 firstdefault = len(args) - len(defaults)
1409 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001410 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001411 if defaults and i >= firstdefault:
1412 spec = spec + formatvalue(defaults[i - firstdefault])
1413 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001414 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001415 specs.append(formatvarargs(formatargandannotation(varargs)))
1416 else:
1417 if kwonlyargs:
1418 specs.append('*')
1419 if kwonlyargs:
1420 for kwonlyarg in kwonlyargs:
1421 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001422 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001423 spec += formatvalue(kwonlydefaults[kwonlyarg])
1424 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001425 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001426 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001427 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001428 if 'return' in annotations:
1429 result += formatreturns(formatannotation(annotations['return']))
1430 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001431
1432def formatargvalues(args, varargs, varkw, locals,
1433 formatarg=str,
1434 formatvarargs=lambda name: '*' + name,
1435 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001436 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001437 """Format an argument spec from the 4 values returned by getargvalues.
1438
1439 The first four arguments are (args, varargs, varkw, locals). The
1440 next four arguments are the corresponding optional formatting functions
1441 that are called to turn names and values into strings. The ninth
1442 argument is an optional function to format the sequence of arguments."""
1443 def convert(name, locals=locals,
1444 formatarg=formatarg, formatvalue=formatvalue):
1445 return formatarg(name) + formatvalue(locals[name])
1446 specs = []
1447 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001448 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001449 if varargs:
1450 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1451 if varkw:
1452 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001453 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001454
Benjamin Petersone109c702011-06-24 09:37:26 -05001455def _missing_arguments(f_name, argnames, pos, values):
1456 names = [repr(name) for name in argnames if name not in values]
1457 missing = len(names)
1458 if missing == 1:
1459 s = names[0]
1460 elif missing == 2:
1461 s = "{} and {}".format(*names)
1462 else:
Yury Selivanovdccfa132014-03-27 18:42:52 -04001463 tail = ", {} and {}".format(*names[-2:])
Benjamin Petersone109c702011-06-24 09:37:26 -05001464 del names[-2:]
1465 s = ", ".join(names) + tail
1466 raise TypeError("%s() missing %i required %s argument%s: %s" %
1467 (f_name, missing,
1468 "positional" if pos else "keyword-only",
1469 "" if missing == 1 else "s", s))
1470
1471def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001472 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001473 kwonly_given = len([arg for arg in kwonly if arg in values])
1474 if varargs:
1475 plural = atleast != 1
1476 sig = "at least %d" % (atleast,)
1477 elif defcount:
1478 plural = True
1479 sig = "from %d to %d" % (atleast, len(args))
1480 else:
1481 plural = len(args) != 1
1482 sig = str(len(args))
1483 kwonly_sig = ""
1484 if kwonly_given:
1485 msg = " positional argument%s (and %d keyword-only argument%s)"
1486 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1487 "s" if kwonly_given != 1 else ""))
1488 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1489 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1490 "was" if given == 1 and not kwonly_given else "were"))
1491
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03001492def getcallargs(func, /, *positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001493 """Get the mapping of arguments to values.
1494
1495 A dict is returned, with keys the function argument names (including the
1496 names of the * and ** arguments, if any), and values the respective bound
1497 values from 'positional' and 'named'."""
1498 spec = getfullargspec(func)
Pablo Galindod5d2b452019-04-30 02:01:14 +01001499 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001500 f_name = func.__name__
1501 arg2value = {}
1502
Benjamin Petersonb204a422011-06-05 22:04:07 -05001503
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001504 if ismethod(func) and func.__self__ is not None:
1505 # implicit 'self' (or 'cls' for classmethods) argument
1506 positional = (func.__self__,) + positional
1507 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001508 num_args = len(args)
1509 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001510
Benjamin Petersonb204a422011-06-05 22:04:07 -05001511 n = min(num_pos, num_args)
1512 for i in range(n):
Pablo Galindod5d2b452019-04-30 02:01:14 +01001513 arg2value[args[i]] = positional[i]
Benjamin Petersonb204a422011-06-05 22:04:07 -05001514 if varargs:
1515 arg2value[varargs] = tuple(positional[n:])
1516 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001517 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001518 arg2value[varkw] = {}
1519 for kw, value in named.items():
1520 if kw not in possible_kwargs:
1521 if not varkw:
1522 raise TypeError("%s() got an unexpected keyword argument %r" %
1523 (f_name, kw))
1524 arg2value[varkw][kw] = value
1525 continue
1526 if kw in arg2value:
1527 raise TypeError("%s() got multiple values for argument %r" %
1528 (f_name, kw))
1529 arg2value[kw] = value
1530 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001531 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1532 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001533 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001534 req = args[:num_args - num_defaults]
1535 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001536 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001537 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001538 for i, arg in enumerate(args[num_args - num_defaults:]):
1539 if arg not in arg2value:
1540 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001541 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001542 for kwarg in kwonlyargs:
1543 if kwarg not in arg2value:
Yury Selivanov875df202014-03-27 18:23:03 -04001544 if kwonlydefaults and kwarg in kwonlydefaults:
Benjamin Petersone109c702011-06-24 09:37:26 -05001545 arg2value[kwarg] = kwonlydefaults[kwarg]
1546 else:
1547 missing += 1
1548 if missing:
1549 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001550 return arg2value
1551
Nick Coghlan2f92e542012-06-23 19:39:55 +10001552ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1553
1554def getclosurevars(func):
1555 """
1556 Get the mapping of free variables to their current values.
1557
Meador Inge8fda3592012-07-19 21:33:21 -05001558 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001559 and builtin references as seen by the body of the function. A final
1560 set of unbound names that could not be resolved is also provided.
1561 """
1562
1563 if ismethod(func):
1564 func = func.__func__
1565
1566 if not isfunction(func):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001567 raise TypeError("{!r} is not a Python function".format(func))
Nick Coghlan2f92e542012-06-23 19:39:55 +10001568
1569 code = func.__code__
1570 # Nonlocal references are named in co_freevars and resolved
1571 # by looking them up in __closure__ by positional index
1572 if func.__closure__ is None:
1573 nonlocal_vars = {}
1574 else:
1575 nonlocal_vars = {
1576 var : cell.cell_contents
1577 for var, cell in zip(code.co_freevars, func.__closure__)
1578 }
1579
1580 # Global and builtin references are named in co_names and resolved
1581 # by looking them up in __globals__ or __builtins__
1582 global_ns = func.__globals__
1583 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1584 if ismodule(builtin_ns):
1585 builtin_ns = builtin_ns.__dict__
1586 global_vars = {}
1587 builtin_vars = {}
1588 unbound_names = set()
1589 for name in code.co_names:
1590 if name in ("None", "True", "False"):
1591 # Because these used to be builtins instead of keywords, they
1592 # may still show up as name references. We ignore them.
1593 continue
1594 try:
1595 global_vars[name] = global_ns[name]
1596 except KeyError:
1597 try:
1598 builtin_vars[name] = builtin_ns[name]
1599 except KeyError:
1600 unbound_names.add(name)
1601
1602 return ClosureVars(nonlocal_vars, global_vars,
1603 builtin_vars, unbound_names)
1604
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001605# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001606
1607Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1608
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001609def getframeinfo(frame, context=1):
1610 """Get information about a frame or traceback object.
1611
1612 A tuple of five things is returned: the filename, the line number of
1613 the current line, the function name, a list of lines of context from
1614 the source code, and the index of the current line within that list.
1615 The optional second argument specifies the number of lines of context
1616 to return, which are centered around the current line."""
1617 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001618 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001619 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001620 else:
1621 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001622 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001623 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001624
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001625 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001626 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001627 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001628 try:
1629 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001630 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001631 lines = index = None
1632 else:
Raymond Hettingera0501712004-06-15 11:22:53 +00001633 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001634 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001635 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001636 else:
1637 lines = index = None
1638
Christian Heimes25bb7832008-01-11 16:17:00 +00001639 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001640
1641def getlineno(frame):
1642 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001643 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1644 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001645
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001646FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
1647
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001648def getouterframes(frame, context=1):
1649 """Get a list of records for a frame and all higher (calling) frames.
1650
1651 Each record contains a frame object, filename, line number, function
1652 name, a list of lines of context, and index within the context."""
1653 framelist = []
1654 while frame:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001655 frameinfo = (frame,) + getframeinfo(frame, context)
1656 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001657 frame = frame.f_back
1658 return framelist
1659
1660def getinnerframes(tb, context=1):
1661 """Get a list of records for a traceback's frame and all lower frames.
1662
1663 Each record contains a frame object, filename, line number, function
1664 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001665 framelist = []
1666 while tb:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001667 frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
1668 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001669 tb = tb.tb_next
1670 return framelist
1671
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001672def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001673 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001674 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001675
1676def stack(context=1):
1677 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001678 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001679
1680def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001681 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001682 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001683
1684
1685# ------------------------------------------------ static version of getattr
1686
1687_sentinel = object()
1688
Michael Foorde5162652010-11-20 16:40:44 +00001689def _static_getmro(klass):
1690 return type.__dict__['__mro__'].__get__(klass)
1691
Michael Foord95fc51d2010-11-20 15:07:30 +00001692def _check_instance(obj, attr):
1693 instance_dict = {}
1694 try:
1695 instance_dict = object.__getattribute__(obj, "__dict__")
1696 except AttributeError:
1697 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001698 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001699
1700
1701def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001702 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001703 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001704 try:
1705 return entry.__dict__[attr]
1706 except KeyError:
1707 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001708 return _sentinel
1709
Michael Foord35184ed2010-11-20 16:58:30 +00001710def _is_type(obj):
1711 try:
1712 _static_getmro(obj)
1713 except TypeError:
1714 return False
1715 return True
1716
Michael Foorddcebe0f2011-03-15 19:20:44 -04001717def _shadowed_dict(klass):
1718 dict_attr = type.__dict__["__dict__"]
1719 for entry in _static_getmro(klass):
1720 try:
1721 class_dict = dict_attr.__get__(entry)["__dict__"]
1722 except KeyError:
1723 pass
1724 else:
Inada Naoki8f9cc872019-09-05 13:07:08 +09001725 if not (type(class_dict) is types.GetSetDescriptorType and
Michael Foorddcebe0f2011-03-15 19:20:44 -04001726 class_dict.__name__ == "__dict__" and
1727 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001728 return class_dict
1729 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001730
1731def getattr_static(obj, attr, default=_sentinel):
1732 """Retrieve attributes without triggering dynamic lookup via the
1733 descriptor protocol, __getattr__ or __getattribute__.
1734
1735 Note: this function may not be able to retrieve all attributes
1736 that getattr can fetch (like dynamically created attributes)
1737 and may find attributes that getattr can't (like descriptors
1738 that raise AttributeError). It can also return descriptor objects
1739 instead of instance members in some cases. See the
1740 documentation for details.
1741 """
1742 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001743 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001744 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001745 dict_attr = _shadowed_dict(klass)
1746 if (dict_attr is _sentinel or
Inada Naoki8f9cc872019-09-05 13:07:08 +09001747 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001748 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001749 else:
1750 klass = obj
1751
1752 klass_result = _check_class(klass, attr)
1753
1754 if instance_result is not _sentinel and klass_result is not _sentinel:
1755 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1756 _check_class(type(klass_result), '__set__') is not _sentinel):
1757 return klass_result
1758
1759 if instance_result is not _sentinel:
1760 return instance_result
1761 if klass_result is not _sentinel:
1762 return klass_result
1763
1764 if obj is klass:
1765 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001766 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001767 if _shadowed_dict(type(entry)) is _sentinel:
1768 try:
1769 return entry.__dict__[attr]
1770 except KeyError:
1771 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001772 if default is not _sentinel:
1773 return default
1774 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001775
1776
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001777# ------------------------------------------------ generator introspection
1778
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001779GEN_CREATED = 'GEN_CREATED'
1780GEN_RUNNING = 'GEN_RUNNING'
1781GEN_SUSPENDED = 'GEN_SUSPENDED'
1782GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001783
1784def getgeneratorstate(generator):
1785 """Get current state of a generator-iterator.
1786
1787 Possible states are:
1788 GEN_CREATED: Waiting to start execution.
1789 GEN_RUNNING: Currently being executed by the interpreter.
1790 GEN_SUSPENDED: Currently suspended at a yield expression.
1791 GEN_CLOSED: Execution has completed.
1792 """
1793 if generator.gi_running:
1794 return GEN_RUNNING
1795 if generator.gi_frame is None:
1796 return GEN_CLOSED
1797 if generator.gi_frame.f_lasti == -1:
1798 return GEN_CREATED
1799 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001800
1801
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001802def getgeneratorlocals(generator):
1803 """
1804 Get the mapping of generator local variables to their current values.
1805
1806 A dict is returned, with the keys the local variable names and values the
1807 bound values."""
1808
1809 if not isgenerator(generator):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001810 raise TypeError("{!r} is not a Python generator".format(generator))
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001811
1812 frame = getattr(generator, "gi_frame", None)
1813 if frame is not None:
1814 return generator.gi_frame.f_locals
1815 else:
1816 return {}
1817
Yury Selivanov5376ba92015-06-22 12:19:30 -04001818
1819# ------------------------------------------------ coroutine introspection
1820
1821CORO_CREATED = 'CORO_CREATED'
1822CORO_RUNNING = 'CORO_RUNNING'
1823CORO_SUSPENDED = 'CORO_SUSPENDED'
1824CORO_CLOSED = 'CORO_CLOSED'
1825
1826def getcoroutinestate(coroutine):
1827 """Get current state of a coroutine object.
1828
1829 Possible states are:
1830 CORO_CREATED: Waiting to start execution.
1831 CORO_RUNNING: Currently being executed by the interpreter.
1832 CORO_SUSPENDED: Currently suspended at an await expression.
1833 CORO_CLOSED: Execution has completed.
1834 """
1835 if coroutine.cr_running:
1836 return CORO_RUNNING
1837 if coroutine.cr_frame is None:
1838 return CORO_CLOSED
1839 if coroutine.cr_frame.f_lasti == -1:
1840 return CORO_CREATED
1841 return CORO_SUSPENDED
1842
1843
1844def getcoroutinelocals(coroutine):
1845 """
1846 Get the mapping of coroutine local variables to their current values.
1847
1848 A dict is returned, with the keys the local variable names and values the
1849 bound values."""
1850 frame = getattr(coroutine, "cr_frame", None)
1851 if frame is not None:
1852 return frame.f_locals
1853 else:
1854 return {}
1855
1856
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001857###############################################################################
1858### Function Signature Object (PEP 362)
1859###############################################################################
1860
1861
1862_WrapperDescriptor = type(type.__call__)
1863_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001864_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001865
1866_NonUserDefinedCallables = (_WrapperDescriptor,
1867 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001868 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001869 types.BuiltinFunctionType)
1870
1871
Yury Selivanov421f0c72014-01-29 12:05:40 -05001872def _signature_get_user_defined_method(cls, method_name):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001873 """Private helper. Checks if ``cls`` has an attribute
1874 named ``method_name`` and returns it only if it is a
1875 pure python function.
1876 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001877 try:
1878 meth = getattr(cls, method_name)
1879 except AttributeError:
1880 return
1881 else:
1882 if not isinstance(meth, _NonUserDefinedCallables):
1883 # Once '__signature__' will be added to 'C'-level
1884 # callables, this check won't be necessary
1885 return meth
1886
1887
Yury Selivanov62560fb2014-01-28 12:26:24 -05001888def _signature_get_partial(wrapped_sig, partial, extra_args=()):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001889 """Private helper to calculate how 'wrapped_sig' signature will
1890 look like after applying a 'functools.partial' object (or alike)
1891 on it.
1892 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001893
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001894 old_params = wrapped_sig.parameters
Inada Naoki21105512020-03-02 18:54:49 +09001895 new_params = OrderedDict(old_params.items())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001896
1897 partial_args = partial.args or ()
1898 partial_keywords = partial.keywords or {}
1899
1900 if extra_args:
1901 partial_args = extra_args + partial_args
1902
1903 try:
1904 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1905 except TypeError as ex:
1906 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1907 raise ValueError(msg) from ex
1908
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001909
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001910 transform_to_kwonly = False
1911 for param_name, param in old_params.items():
1912 try:
1913 arg_value = ba.arguments[param_name]
1914 except KeyError:
1915 pass
1916 else:
1917 if param.kind is _POSITIONAL_ONLY:
1918 # If positional-only parameter is bound by partial,
1919 # it effectively disappears from the signature
Inada Naoki21105512020-03-02 18:54:49 +09001920 new_params.pop(param_name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001921 continue
1922
1923 if param.kind is _POSITIONAL_OR_KEYWORD:
1924 if param_name in partial_keywords:
1925 # This means that this parameter, and all parameters
1926 # after it should be keyword-only (and var-positional
1927 # should be removed). Here's why. Consider the following
1928 # function:
1929 # foo(a, b, *args, c):
1930 # pass
1931 #
1932 # "partial(foo, a='spam')" will have the following
1933 # signature: "(*, a='spam', b, c)". Because attempting
1934 # to call that partial with "(10, 20)" arguments will
1935 # raise a TypeError, saying that "a" argument received
1936 # multiple values.
1937 transform_to_kwonly = True
1938 # Set the new default value
Inada Naoki21105512020-03-02 18:54:49 +09001939 new_params[param_name] = param.replace(default=arg_value)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001940 else:
1941 # was passed as a positional argument
Inada Naoki21105512020-03-02 18:54:49 +09001942 new_params.pop(param.name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001943 continue
1944
1945 if param.kind is _KEYWORD_ONLY:
1946 # Set the new default value
Inada Naoki21105512020-03-02 18:54:49 +09001947 new_params[param_name] = param.replace(default=arg_value)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001948
1949 if transform_to_kwonly:
1950 assert param.kind is not _POSITIONAL_ONLY
1951
1952 if param.kind is _POSITIONAL_OR_KEYWORD:
Inada Naoki21105512020-03-02 18:54:49 +09001953 new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1954 new_params[param_name] = new_param
1955 new_params.move_to_end(param_name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001956 elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
Inada Naoki21105512020-03-02 18:54:49 +09001957 new_params.move_to_end(param_name)
1958 elif param.kind is _VAR_POSITIONAL:
1959 new_params.pop(param.name)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001960
1961 return wrapped_sig.replace(parameters=new_params.values())
1962
1963
Yury Selivanov62560fb2014-01-28 12:26:24 -05001964def _signature_bound_method(sig):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001965 """Private helper to transform signatures for unbound
1966 functions to bound methods.
1967 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001968
1969 params = tuple(sig.parameters.values())
1970
1971 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1972 raise ValueError('invalid method signature')
1973
1974 kind = params[0].kind
1975 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1976 # Drop first parameter:
1977 # '(p1, p2[, ...])' -> '(p2[, ...])'
1978 params = params[1:]
1979 else:
1980 if kind is not _VAR_POSITIONAL:
1981 # Unless we add a new parameter type we never
1982 # get here
1983 raise ValueError('invalid argument type')
1984 # It's a var-positional parameter.
1985 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1986
1987 return sig.replace(parameters=params)
1988
1989
Yury Selivanovb77511d2014-01-29 10:46:14 -05001990def _signature_is_builtin(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001991 """Private helper to test if `obj` is a callable that might
1992 support Argument Clinic's __text_signature__ protocol.
1993 """
Yury Selivanov1d241832014-02-02 12:51:20 -05001994 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001995 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001996 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001997 # Can't test 'isinstance(type)' here, as it would
1998 # also be True for regular python classes
1999 obj in (type, object))
2000
2001
Yury Selivanov63da7c72014-01-31 14:48:37 -05002002def _signature_is_functionlike(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002003 """Private helper to test if `obj` is a duck type of FunctionType.
2004 A good example of such objects are functions compiled with
2005 Cython, which have all attributes that a pure Python function
2006 would have, but have their code statically compiled.
2007 """
Yury Selivanov63da7c72014-01-31 14:48:37 -05002008
2009 if not callable(obj) or isclass(obj):
2010 # All function-like objects are obviously callables,
2011 # and not classes.
2012 return False
2013
2014 name = getattr(obj, '__name__', None)
2015 code = getattr(obj, '__code__', None)
2016 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
2017 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
Pablo Galindob0544ba2021-04-21 12:41:19 +01002018 annotations = getattr(obj, '__annotations__', None)
Yury Selivanov63da7c72014-01-31 14:48:37 -05002019
2020 return (isinstance(code, types.CodeType) and
2021 isinstance(name, str) and
2022 (defaults is None or isinstance(defaults, tuple)) and
2023 (kwdefaults is None or isinstance(kwdefaults, dict)) and
larryhastings74613a42021-04-29 21:16:28 -07002024 (isinstance(annotations, (dict)) or annotations is None) )
Yury Selivanov63da7c72014-01-31 14:48:37 -05002025
2026
Yury Selivanovd82eddc2014-01-29 11:24:39 -05002027def _signature_get_bound_param(spec):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002028 """ Private helper to get first parameter name from a
2029 __text_signature__ of a builtin method, which should
2030 be in the following format: '($param1, ...)'.
2031 Assumptions are that the first argument won't have
2032 a default value or an annotation.
2033 """
Yury Selivanovd82eddc2014-01-29 11:24:39 -05002034
2035 assert spec.startswith('($')
2036
2037 pos = spec.find(',')
2038 if pos == -1:
2039 pos = spec.find(')')
2040
2041 cpos = spec.find(':')
2042 assert cpos == -1 or cpos > pos
2043
2044 cpos = spec.find('=')
2045 assert cpos == -1 or cpos > pos
2046
2047 return spec[2:pos]
2048
2049
Larry Hastings2623c8c2014-02-08 22:15:29 -08002050def _signature_strip_non_python_syntax(signature):
2051 """
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002052 Private helper function. Takes a signature in Argument Clinic's
2053 extended signature format.
2054
Larry Hastings2623c8c2014-02-08 22:15:29 -08002055 Returns a tuple of three things:
2056 * that signature re-rendered in standard Python syntax,
2057 * the index of the "self" parameter (generally 0), or None if
2058 the function does not have a "self" parameter, and
2059 * the index of the last "positional only" parameter,
2060 or None if the signature has no positional-only parameters.
2061 """
2062
2063 if not signature:
2064 return signature, None, None
2065
2066 self_parameter = None
2067 last_positional_only = None
2068
2069 lines = [l.encode('ascii') for l in signature.split('\n')]
2070 generator = iter(lines).__next__
2071 token_stream = tokenize.tokenize(generator)
2072
2073 delayed_comma = False
2074 skip_next_comma = False
2075 text = []
2076 add = text.append
2077
2078 current_parameter = 0
2079 OP = token.OP
2080 ERRORTOKEN = token.ERRORTOKEN
2081
2082 # token stream always starts with ENCODING token, skip it
2083 t = next(token_stream)
2084 assert t.type == tokenize.ENCODING
2085
2086 for t in token_stream:
2087 type, string = t.type, t.string
2088
2089 if type == OP:
2090 if string == ',':
2091 if skip_next_comma:
2092 skip_next_comma = False
2093 else:
2094 assert not delayed_comma
2095 delayed_comma = True
2096 current_parameter += 1
2097 continue
2098
2099 if string == '/':
2100 assert not skip_next_comma
2101 assert last_positional_only is None
2102 skip_next_comma = True
2103 last_positional_only = current_parameter - 1
2104 continue
2105
2106 if (type == ERRORTOKEN) and (string == '$'):
2107 assert self_parameter is None
2108 self_parameter = current_parameter
2109 continue
2110
2111 if delayed_comma:
2112 delayed_comma = False
2113 if not ((type == OP) and (string == ')')):
2114 add(', ')
2115 add(string)
2116 if (string == ','):
2117 add(' ')
2118 clean_signature = ''.join(text)
2119 return clean_signature, self_parameter, last_positional_only
2120
2121
Yury Selivanov57d240e2014-02-19 16:27:23 -05002122def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002123 """Private helper to parse content of '__text_signature__'
2124 and return a Signature based on it.
2125 """
INADA Naoki37420de2018-01-27 10:10:06 +09002126 # Lazy import ast because it's relatively heavy and
2127 # it's not used for other than this function.
2128 import ast
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002129
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002130 Parameter = cls._parameter_cls
2131
Larry Hastings2623c8c2014-02-08 22:15:29 -08002132 clean_signature, self_parameter, last_positional_only = \
2133 _signature_strip_non_python_syntax(s)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002134
Larry Hastings2623c8c2014-02-08 22:15:29 -08002135 program = "def foo" + clean_signature + ": pass"
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002136
2137 try:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002138 module = ast.parse(program)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002139 except SyntaxError:
2140 module = None
2141
2142 if not isinstance(module, ast.Module):
2143 raise ValueError("{!r} builtin has invalid signature".format(obj))
2144
2145 f = module.body[0]
2146
2147 parameters = []
2148 empty = Parameter.empty
2149 invalid = object()
2150
2151 module = None
2152 module_dict = {}
2153 module_name = getattr(obj, '__module__', None)
2154 if module_name:
2155 module = sys.modules.get(module_name, None)
2156 if module:
2157 module_dict = module.__dict__
INADA Naoki6f85b822018-10-05 01:47:09 +09002158 sys_module_dict = sys.modules.copy()
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002159
2160 def parse_name(node):
2161 assert isinstance(node, ast.arg)
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05302162 if node.annotation is not None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002163 raise ValueError("Annotations are not currently supported")
2164 return node.arg
2165
2166 def wrap_value(s):
2167 try:
2168 value = eval(s, module_dict)
2169 except NameError:
2170 try:
2171 value = eval(s, sys_module_dict)
2172 except NameError:
2173 raise RuntimeError()
2174
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002175 if isinstance(value, (str, int, float, bytes, bool, type(None))):
2176 return ast.Constant(value)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002177 raise RuntimeError()
2178
2179 class RewriteSymbolics(ast.NodeTransformer):
2180 def visit_Attribute(self, node):
2181 a = []
2182 n = node
2183 while isinstance(n, ast.Attribute):
2184 a.append(n.attr)
2185 n = n.value
2186 if not isinstance(n, ast.Name):
2187 raise RuntimeError()
2188 a.append(n.id)
2189 value = ".".join(reversed(a))
2190 return wrap_value(value)
2191
2192 def visit_Name(self, node):
2193 if not isinstance(node.ctx, ast.Load):
2194 raise ValueError()
2195 return wrap_value(node.id)
2196
2197 def p(name_node, default_node, default=empty):
2198 name = parse_name(name_node)
2199 if name is invalid:
2200 return None
2201 if default_node and default_node is not _empty:
2202 try:
2203 default_node = RewriteSymbolics().visit(default_node)
2204 o = ast.literal_eval(default_node)
2205 except ValueError:
2206 o = invalid
2207 if o is invalid:
2208 return None
2209 default = o if o is not invalid else default
2210 parameters.append(Parameter(name, kind, default=default, annotation=empty))
2211
2212 # non-keyword-only parameters
2213 args = reversed(f.args.args)
2214 defaults = reversed(f.args.defaults)
2215 iter = itertools.zip_longest(args, defaults, fillvalue=None)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002216 if last_positional_only is not None:
2217 kind = Parameter.POSITIONAL_ONLY
2218 else:
2219 kind = Parameter.POSITIONAL_OR_KEYWORD
2220 for i, (name, default) in enumerate(reversed(list(iter))):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002221 p(name, default)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002222 if i == last_positional_only:
2223 kind = Parameter.POSITIONAL_OR_KEYWORD
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002224
2225 # *args
2226 if f.args.vararg:
2227 kind = Parameter.VAR_POSITIONAL
2228 p(f.args.vararg, empty)
2229
2230 # keyword-only arguments
2231 kind = Parameter.KEYWORD_ONLY
2232 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
2233 p(name, default)
2234
2235 # **kwargs
2236 if f.args.kwarg:
2237 kind = Parameter.VAR_KEYWORD
2238 p(f.args.kwarg, empty)
2239
Larry Hastings2623c8c2014-02-08 22:15:29 -08002240 if self_parameter is not None:
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002241 # Possibly strip the bound argument:
2242 # - We *always* strip first bound argument if
2243 # it is a module.
2244 # - We don't strip first bound argument if
2245 # skip_bound_arg is False.
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002246 assert parameters
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002247 _self = getattr(obj, '__self__', None)
2248 self_isbound = _self is not None
2249 self_ismodule = ismodule(_self)
2250 if self_isbound and (self_ismodule or skip_bound_arg):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002251 parameters.pop(0)
2252 else:
2253 # for builtins, self parameter is always positional-only!
2254 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
2255 parameters[0] = p
2256
2257 return cls(parameters, return_annotation=cls.empty)
2258
2259
Yury Selivanov57d240e2014-02-19 16:27:23 -05002260def _signature_from_builtin(cls, func, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002261 """Private helper function to get signature for
2262 builtin callables.
2263 """
2264
Yury Selivanov57d240e2014-02-19 16:27:23 -05002265 if not _signature_is_builtin(func):
2266 raise TypeError("{!r} is not a Python builtin "
2267 "function".format(func))
2268
2269 s = getattr(func, "__text_signature__", None)
2270 if not s:
2271 raise ValueError("no signature found for builtin {!r}".format(func))
2272
2273 return _signature_fromstr(cls, func, s, skip_bound_arg)
2274
2275
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002276def _signature_from_function(cls, func, skip_bound_arg=True,
larryhastings74613a42021-04-29 21:16:28 -07002277 globals=None, locals=None, eval_str=False):
Yury Selivanovcf45f022015-05-20 14:38:50 -04002278 """Private helper: constructs Signature for the given python function."""
2279
2280 is_duck_function = False
2281 if not isfunction(func):
2282 if _signature_is_functionlike(func):
2283 is_duck_function = True
2284 else:
2285 # If it's not a pure Python function, and not a duck type
2286 # of pure function:
2287 raise TypeError('{!r} is not a Python function'.format(func))
2288
Serhiy Storchakad53cf992019-05-06 22:40:27 +03002289 s = getattr(func, "__text_signature__", None)
2290 if s:
2291 return _signature_fromstr(cls, func, s, skip_bound_arg)
2292
Yury Selivanovcf45f022015-05-20 14:38:50 -04002293 Parameter = cls._parameter_cls
2294
2295 # Parameter information.
2296 func_code = func.__code__
2297 pos_count = func_code.co_argcount
2298 arg_names = func_code.co_varnames
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002299 posonly_count = func_code.co_posonlyargcount
Pablo Galindocd74e662019-06-01 18:08:04 +01002300 positional = arg_names[:pos_count]
Yury Selivanovcf45f022015-05-20 14:38:50 -04002301 keyword_only_count = func_code.co_kwonlyargcount
Pablo Galindocd74e662019-06-01 18:08:04 +01002302 keyword_only = arg_names[pos_count:pos_count + keyword_only_count]
larryhastings74613a42021-04-29 21:16:28 -07002303 annotations = get_annotations(func, globals=globals, locals=locals, eval_str=eval_str)
Yury Selivanovcf45f022015-05-20 14:38:50 -04002304 defaults = func.__defaults__
2305 kwdefaults = func.__kwdefaults__
2306
2307 if defaults:
2308 pos_default_count = len(defaults)
2309 else:
2310 pos_default_count = 0
2311
2312 parameters = []
2313
Pablo Galindocd74e662019-06-01 18:08:04 +01002314 non_default_count = pos_count - pos_default_count
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002315 posonly_left = posonly_count
2316
Yury Selivanovcf45f022015-05-20 14:38:50 -04002317 # Non-keyword-only parameters w/o defaults.
Pablo Galindocd74e662019-06-01 18:08:04 +01002318 for name in positional[:non_default_count]:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002319 kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD
Yury Selivanovcf45f022015-05-20 14:38:50 -04002320 annotation = annotations.get(name, _empty)
2321 parameters.append(Parameter(name, annotation=annotation,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002322 kind=kind))
2323 if posonly_left:
2324 posonly_left -= 1
Yury Selivanovcf45f022015-05-20 14:38:50 -04002325
2326 # ... w/ defaults.
Pablo Galindocd74e662019-06-01 18:08:04 +01002327 for offset, name in enumerate(positional[non_default_count:]):
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002328 kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD
Yury Selivanovcf45f022015-05-20 14:38:50 -04002329 annotation = annotations.get(name, _empty)
2330 parameters.append(Parameter(name, annotation=annotation,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002331 kind=kind,
Yury Selivanovcf45f022015-05-20 14:38:50 -04002332 default=defaults[offset]))
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002333 if posonly_left:
2334 posonly_left -= 1
Yury Selivanovcf45f022015-05-20 14:38:50 -04002335
2336 # *args
2337 if func_code.co_flags & CO_VARARGS:
Pablo Galindocd74e662019-06-01 18:08:04 +01002338 name = arg_names[pos_count + keyword_only_count]
Yury Selivanovcf45f022015-05-20 14:38:50 -04002339 annotation = annotations.get(name, _empty)
2340 parameters.append(Parameter(name, annotation=annotation,
2341 kind=_VAR_POSITIONAL))
2342
2343 # Keyword-only parameters.
2344 for name in keyword_only:
2345 default = _empty
2346 if kwdefaults is not None:
2347 default = kwdefaults.get(name, _empty)
2348
2349 annotation = annotations.get(name, _empty)
2350 parameters.append(Parameter(name, annotation=annotation,
2351 kind=_KEYWORD_ONLY,
2352 default=default))
2353 # **kwargs
2354 if func_code.co_flags & CO_VARKEYWORDS:
Pablo Galindocd74e662019-06-01 18:08:04 +01002355 index = pos_count + keyword_only_count
Yury Selivanovcf45f022015-05-20 14:38:50 -04002356 if func_code.co_flags & CO_VARARGS:
2357 index += 1
2358
2359 name = arg_names[index]
2360 annotation = annotations.get(name, _empty)
2361 parameters.append(Parameter(name, annotation=annotation,
2362 kind=_VAR_KEYWORD))
2363
2364 # Is 'func' is a pure Python function - don't validate the
2365 # parameters list (for correct order and defaults), it should be OK.
2366 return cls(parameters,
2367 return_annotation=annotations.get('return', _empty),
2368 __validate_parameters__=is_duck_function)
2369
2370
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002371def _signature_from_callable(obj, *,
2372 follow_wrapper_chains=True,
2373 skip_bound_arg=True,
larryhastings74613a42021-04-29 21:16:28 -07002374 globals=None,
2375 locals=None,
2376 eval_str=False,
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002377 sigcls):
2378
2379 """Private helper function to get signature for arbitrary
2380 callable objects.
2381 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002382
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002383 _get_signature_of = functools.partial(_signature_from_callable,
2384 follow_wrapper_chains=follow_wrapper_chains,
2385 skip_bound_arg=skip_bound_arg,
larryhastings74613a42021-04-29 21:16:28 -07002386 globals=globals,
2387 locals=locals,
2388 sigcls=sigcls,
2389 eval_str=eval_str)
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002390
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002391 if not callable(obj):
2392 raise TypeError('{!r} is not a callable object'.format(obj))
2393
2394 if isinstance(obj, types.MethodType):
2395 # In this case we skip the first parameter of the underlying
2396 # function (usually `self` or `cls`).
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002397 sig = _get_signature_of(obj.__func__)
Yury Selivanovda396452014-03-27 12:09:24 -04002398
Yury Selivanov57d240e2014-02-19 16:27:23 -05002399 if skip_bound_arg:
2400 return _signature_bound_method(sig)
2401 else:
2402 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002403
Nick Coghlane8c45d62013-07-28 20:00:01 +10002404 # Was this function wrapped by a decorator?
Yury Selivanov57d240e2014-02-19 16:27:23 -05002405 if follow_wrapper_chains:
2406 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
Yury Selivanov46c759d2015-05-27 21:56:53 -04002407 if isinstance(obj, types.MethodType):
2408 # If the unwrapped object is a *method*, we might want to
2409 # skip its first parameter (self).
2410 # See test_signature_wrapped_bound_method for details.
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002411 return _get_signature_of(obj)
Nick Coghlane8c45d62013-07-28 20:00:01 +10002412
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002413 try:
2414 sig = obj.__signature__
2415 except AttributeError:
2416 pass
2417 else:
2418 if sig is not None:
Yury Selivanov42407ab2014-06-23 10:23:50 -07002419 if not isinstance(sig, Signature):
2420 raise TypeError(
2421 'unexpected object {!r} in __signature__ '
2422 'attribute'.format(sig))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002423 return sig
2424
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002425 try:
2426 partialmethod = obj._partialmethod
2427 except AttributeError:
2428 pass
2429 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05002430 if isinstance(partialmethod, functools.partialmethod):
2431 # Unbound partialmethod (see functools.partialmethod)
2432 # This means, that we need to calculate the signature
2433 # as if it's a regular partial object, but taking into
2434 # account that the first positional argument
2435 # (usually `self`, or `cls`) will not be passed
2436 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002437
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002438 wrapped_sig = _get_signature_of(partialmethod.func)
Yury Selivanovda396452014-03-27 12:09:24 -04002439
Yury Selivanov0486f812014-01-29 12:18:59 -05002440 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanov0486f812014-01-29 12:18:59 -05002441 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
Dong-hee Na378d7062017-05-18 04:00:51 +09002442 if first_wrapped_param.kind is Parameter.VAR_POSITIONAL:
2443 # First argument of the wrapped callable is `*args`, as in
2444 # `partialmethod(lambda *args)`.
2445 return sig
2446 else:
2447 sig_params = tuple(sig.parameters.values())
Yury Selivanov8a387212018-03-06 12:59:45 -05002448 assert (not sig_params or
2449 first_wrapped_param is not sig_params[0])
Dong-hee Na378d7062017-05-18 04:00:51 +09002450 new_params = (first_wrapped_param,) + sig_params
2451 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002452
Yury Selivanov63da7c72014-01-31 14:48:37 -05002453 if isfunction(obj) or _signature_is_functionlike(obj):
2454 # If it's a pure Python function, or an object that is duck type
2455 # of a Python function (Cython functions, for instance), then:
Serhiy Storchakad53cf992019-05-06 22:40:27 +03002456 return _signature_from_function(sigcls, obj,
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002457 skip_bound_arg=skip_bound_arg,
larryhastings74613a42021-04-29 21:16:28 -07002458 globals=globals, locals=locals, eval_str=eval_str)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002459
Yury Selivanova773de02014-02-21 18:30:53 -05002460 if _signature_is_builtin(obj):
Yury Selivanovda396452014-03-27 12:09:24 -04002461 return _signature_from_builtin(sigcls, obj,
Yury Selivanova773de02014-02-21 18:30:53 -05002462 skip_bound_arg=skip_bound_arg)
2463
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002464 if isinstance(obj, functools.partial):
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002465 wrapped_sig = _get_signature_of(obj.func)
Yury Selivanov62560fb2014-01-28 12:26:24 -05002466 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002467
2468 sig = None
2469 if isinstance(obj, type):
2470 # obj is a class or a metaclass
2471
2472 # First, let's see if it has an overloaded __call__ defined
2473 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05002474 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002475 if call is not None:
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002476 sig = _get_signature_of(call)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002477 else:
Miss Islington (bot)948e39a2021-07-16 06:25:57 -07002478 factory_method = None
Yury Selivanov421f0c72014-01-29 12:05:40 -05002479 new = _signature_get_user_defined_method(obj, '__new__')
Miss Islington (bot)948e39a2021-07-16 06:25:57 -07002480 init = _signature_get_user_defined_method(obj, '__init__')
2481 # Now we check if the 'obj' class has an own '__new__' method
2482 if '__new__' in obj.__dict__:
2483 factory_method = new
2484 # or an own '__init__' method
2485 elif '__init__' in obj.__dict__:
2486 factory_method = init
2487 # If not, we take inherited '__new__' or '__init__', if present
2488 elif new is not None:
2489 factory_method = new
2490 elif init is not None:
2491 factory_method = init
2492
2493 if factory_method is not None:
2494 sig = _get_signature_of(factory_method)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002495
2496 if sig is None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002497 # At this point we know, that `obj` is a class, with no user-
2498 # defined '__init__', '__new__', or class-level '__call__'
2499
Larry Hastings2623c8c2014-02-08 22:15:29 -08002500 for base in obj.__mro__[:-1]:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002501 # Since '__text_signature__' is implemented as a
2502 # descriptor that extracts text signature from the
2503 # class docstring, if 'obj' is derived from a builtin
2504 # class, its own '__text_signature__' may be 'None'.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002505 # Therefore, we go through the MRO (except the last
2506 # class in there, which is 'object') to find the first
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002507 # class with non-empty text signature.
2508 try:
2509 text_sig = base.__text_signature__
2510 except AttributeError:
2511 pass
2512 else:
2513 if text_sig:
2514 # If 'obj' class has a __text_signature__ attribute:
2515 # return a signature based on it
Yury Selivanovda396452014-03-27 12:09:24 -04002516 return _signature_fromstr(sigcls, obj, text_sig)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002517
2518 # No '__text_signature__' was found for the 'obj' class.
2519 # Last option is to check if its '__init__' is
2520 # object.__init__ or type.__init__.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002521 if type not in obj.__mro__:
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002522 # We have a class (not metaclass), but no user-defined
2523 # __init__ or __new__ for it
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002524 if (obj.__init__ is object.__init__ and
2525 obj.__new__ is object.__new__):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002526 # Return a signature of 'object' builtin.
Gregory P. Smith5b9ff7a2019-09-13 17:13:51 +01002527 return sigcls.from_callable(object)
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002528 else:
2529 raise ValueError(
2530 'no signature found for builtin type {!r}'.format(obj))
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002531
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002532 elif not isinstance(obj, _NonUserDefinedCallables):
2533 # An object with __call__
2534 # We also check that the 'obj' is not an instance of
2535 # _WrapperDescriptor or _MethodWrapper to avoid
2536 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05002537 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002538 if call is not None:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002539 try:
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002540 sig = _get_signature_of(call)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002541 except ValueError as ex:
2542 msg = 'no signature found for {!r}'.format(obj)
2543 raise ValueError(msg) from ex
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002544
2545 if sig is not None:
2546 # For classes and objects we skip the first parameter of their
2547 # __call__, __new__, or __init__ methods
Yury Selivanov57d240e2014-02-19 16:27:23 -05002548 if skip_bound_arg:
2549 return _signature_bound_method(sig)
2550 else:
2551 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002552
2553 if isinstance(obj, types.BuiltinFunctionType):
2554 # Raise a nicer error message for builtins
2555 msg = 'no signature found for builtin function {!r}'.format(obj)
2556 raise ValueError(msg)
2557
2558 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2559
2560
2561class _void:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002562 """A private marker - used in Parameter & Signature."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002563
2564
2565class _empty:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002566 """Marker object for Signature.empty and Parameter.empty."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002567
2568
Yury Selivanov21e83a52014-03-27 11:23:13 -04002569class _ParameterKind(enum.IntEnum):
2570 POSITIONAL_ONLY = 0
2571 POSITIONAL_OR_KEYWORD = 1
2572 VAR_POSITIONAL = 2
2573 KEYWORD_ONLY = 3
2574 VAR_KEYWORD = 4
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002575
Ethan Furman9bf7c2d2021-07-03 21:08:42 -07002576 def __str__(self):
2577 return self._name_
2578
Dong-hee Na4aa30062018-06-08 12:46:31 +09002579 @property
2580 def description(self):
2581 return _PARAM_NAME_MAPPING[self]
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002582
Yury Selivanov21e83a52014-03-27 11:23:13 -04002583_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
2584_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
2585_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
2586_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
2587_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002588
Dong-hee Naa9cab432018-05-30 00:04:08 +09002589_PARAM_NAME_MAPPING = {
2590 _POSITIONAL_ONLY: 'positional-only',
2591 _POSITIONAL_OR_KEYWORD: 'positional or keyword',
2592 _VAR_POSITIONAL: 'variadic positional',
2593 _KEYWORD_ONLY: 'keyword-only',
2594 _VAR_KEYWORD: 'variadic keyword'
2595}
2596
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002597
2598class Parameter:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002599 """Represents a parameter in a function signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002600
2601 Has the following public attributes:
2602
2603 * name : str
2604 The name of the parameter as a string.
2605 * default : object
2606 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002607 parameter has no default value, this attribute is set to
2608 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002609 * annotation
2610 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002611 parameter has no annotation, this attribute is set to
2612 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002613 * kind : str
2614 Describes how argument values are bound to the parameter.
2615 Possible values: `Parameter.POSITIONAL_ONLY`,
2616 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
2617 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002618 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002619
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002620 __slots__ = ('_name', '_kind', '_default', '_annotation')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002621
2622 POSITIONAL_ONLY = _POSITIONAL_ONLY
2623 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
2624 VAR_POSITIONAL = _VAR_POSITIONAL
2625 KEYWORD_ONLY = _KEYWORD_ONLY
2626 VAR_KEYWORD = _VAR_KEYWORD
2627
2628 empty = _empty
2629
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002630 def __init__(self, name, kind, *, default=_empty, annotation=_empty):
Dong-hee Naa9cab432018-05-30 00:04:08 +09002631 try:
2632 self._kind = _ParameterKind(kind)
2633 except ValueError:
2634 raise ValueError(f'value {kind!r} is not a valid Parameter.kind')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002635 if default is not _empty:
Dong-hee Naa9cab432018-05-30 00:04:08 +09002636 if self._kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2637 msg = '{} parameters cannot have default values'
Dong-hee Na4aa30062018-06-08 12:46:31 +09002638 msg = msg.format(self._kind.description)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002639 raise ValueError(msg)
2640 self._default = default
2641 self._annotation = annotation
2642
Yury Selivanov2393dca2014-01-27 15:07:58 -05002643 if name is _empty:
2644 raise ValueError('name is a required attribute for Parameter')
2645
2646 if not isinstance(name, str):
Dong-hee Naa9cab432018-05-30 00:04:08 +09002647 msg = 'name must be a str, not a {}'.format(type(name).__name__)
2648 raise TypeError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002649
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002650 if name[0] == '.' and name[1:].isdigit():
2651 # These are implicit arguments generated by comprehensions. In
2652 # order to provide a friendlier interface to users, we recast
2653 # their name as "implicitN" and treat them as positional-only.
2654 # See issue 19611.
Dong-hee Naa9cab432018-05-30 00:04:08 +09002655 if self._kind != _POSITIONAL_OR_KEYWORD:
2656 msg = (
2657 'implicit arguments must be passed as '
2658 'positional or keyword arguments, not {}'
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002659 )
Dong-hee Na4aa30062018-06-08 12:46:31 +09002660 msg = msg.format(self._kind.description)
Dong-hee Naa9cab432018-05-30 00:04:08 +09002661 raise ValueError(msg)
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002662 self._kind = _POSITIONAL_ONLY
2663 name = 'implicit{}'.format(name[1:])
2664
Yury Selivanov2393dca2014-01-27 15:07:58 -05002665 if not name.isidentifier():
2666 raise ValueError('{!r} is not a valid parameter name'.format(name))
2667
2668 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002669
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002670 def __reduce__(self):
2671 return (type(self),
2672 (self._name, self._kind),
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002673 {'_default': self._default,
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002674 '_annotation': self._annotation})
2675
2676 def __setstate__(self, state):
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002677 self._default = state['_default']
2678 self._annotation = state['_annotation']
2679
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002680 @property
2681 def name(self):
2682 return self._name
2683
2684 @property
2685 def default(self):
2686 return self._default
2687
2688 @property
2689 def annotation(self):
2690 return self._annotation
2691
2692 @property
2693 def kind(self):
2694 return self._kind
2695
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002696 def replace(self, *, name=_void, kind=_void,
2697 annotation=_void, default=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002698 """Creates a customized copy of the Parameter."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002699
2700 if name is _void:
2701 name = self._name
2702
2703 if kind is _void:
2704 kind = self._kind
2705
2706 if annotation is _void:
2707 annotation = self._annotation
2708
2709 if default is _void:
2710 default = self._default
2711
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002712 return type(self)(name, kind, default=default, annotation=annotation)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002713
2714 def __str__(self):
2715 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002716 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002717
2718 # Add annotation and default value
2719 if self._annotation is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002720 formatted = '{}: {}'.format(formatted,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002721 formatannotation(self._annotation))
2722
2723 if self._default is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002724 if self._annotation is not _empty:
2725 formatted = '{} = {}'.format(formatted, repr(self._default))
2726 else:
2727 formatted = '{}={}'.format(formatted, repr(self._default))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002728
2729 if kind == _VAR_POSITIONAL:
2730 formatted = '*' + formatted
2731 elif kind == _VAR_KEYWORD:
2732 formatted = '**' + formatted
2733
2734 return formatted
2735
2736 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002737 return '<{} "{}">'.format(self.__class__.__name__, self)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002738
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002739 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002740 return hash((self.name, self.kind, self.annotation, self.default))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002741
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002742 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002743 if self is other:
2744 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002745 if not isinstance(other, Parameter):
2746 return NotImplemented
2747 return (self._name == other._name and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002748 self._kind == other._kind and
2749 self._default == other._default and
2750 self._annotation == other._annotation)
2751
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002752
2753class BoundArguments:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002754 """Result of `Signature.bind` call. Holds the mapping of arguments
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002755 to the function's parameters.
2756
2757 Has the following public attributes:
2758
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002759 * arguments : dict
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002760 An ordered mutable mapping of parameters' names to arguments' values.
2761 Does not contain arguments' default values.
2762 * signature : Signature
2763 The Signature object that created this instance.
2764 * args : tuple
2765 Tuple of positional arguments values.
2766 * kwargs : dict
2767 Dict of keyword arguments values.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002768 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002769
Yury Selivanov6abe0322015-05-13 17:18:41 -04002770 __slots__ = ('arguments', '_signature', '__weakref__')
2771
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002772 def __init__(self, signature, arguments):
2773 self.arguments = arguments
2774 self._signature = signature
2775
2776 @property
2777 def signature(self):
2778 return self._signature
2779
2780 @property
2781 def args(self):
2782 args = []
2783 for param_name, param in self._signature.parameters.items():
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002784 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002785 break
2786
2787 try:
2788 arg = self.arguments[param_name]
2789 except KeyError:
2790 # We're done here. Other arguments
2791 # will be mapped in 'BoundArguments.kwargs'
2792 break
2793 else:
2794 if param.kind == _VAR_POSITIONAL:
2795 # *args
2796 args.extend(arg)
2797 else:
2798 # plain argument
2799 args.append(arg)
2800
2801 return tuple(args)
2802
2803 @property
2804 def kwargs(self):
2805 kwargs = {}
2806 kwargs_started = False
2807 for param_name, param in self._signature.parameters.items():
2808 if not kwargs_started:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002809 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002810 kwargs_started = True
2811 else:
2812 if param_name not in self.arguments:
2813 kwargs_started = True
2814 continue
2815
2816 if not kwargs_started:
2817 continue
2818
2819 try:
2820 arg = self.arguments[param_name]
2821 except KeyError:
2822 pass
2823 else:
2824 if param.kind == _VAR_KEYWORD:
2825 # **kwargs
2826 kwargs.update(arg)
2827 else:
2828 # plain keyword argument
2829 kwargs[param_name] = arg
2830
2831 return kwargs
2832
Yury Selivanovb907a512015-05-16 13:45:09 -04002833 def apply_defaults(self):
2834 """Set default values for missing arguments.
2835
2836 For variable-positional arguments (*args) the default is an
2837 empty tuple.
2838
2839 For variable-keyword arguments (**kwargs) the default is an
2840 empty dict.
2841 """
2842 arguments = self.arguments
Yury Selivanovb907a512015-05-16 13:45:09 -04002843 new_arguments = []
2844 for name, param in self._signature.parameters.items():
2845 try:
2846 new_arguments.append((name, arguments[name]))
2847 except KeyError:
2848 if param.default is not _empty:
2849 val = param.default
2850 elif param.kind is _VAR_POSITIONAL:
2851 val = ()
2852 elif param.kind is _VAR_KEYWORD:
2853 val = {}
2854 else:
2855 # This BoundArguments was likely produced by
2856 # Signature.bind_partial().
2857 continue
2858 new_arguments.append((name, val))
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002859 self.arguments = dict(new_arguments)
Yury Selivanovb907a512015-05-16 13:45:09 -04002860
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002861 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002862 if self is other:
2863 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002864 if not isinstance(other, BoundArguments):
2865 return NotImplemented
2866 return (self.signature == other.signature and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002867 self.arguments == other.arguments)
2868
Yury Selivanov6abe0322015-05-13 17:18:41 -04002869 def __setstate__(self, state):
2870 self._signature = state['_signature']
2871 self.arguments = state['arguments']
2872
2873 def __getstate__(self):
2874 return {'_signature': self._signature, 'arguments': self.arguments}
2875
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002876 def __repr__(self):
2877 args = []
2878 for arg, value in self.arguments.items():
2879 args.append('{}={!r}'.format(arg, value))
Yury Selivanovf229bc52015-05-15 12:53:56 -04002880 return '<{} ({})>'.format(self.__class__.__name__, ', '.join(args))
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002881
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002882
2883class Signature:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002884 """A Signature object represents the overall signature of a function.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002885 It stores a Parameter object for each parameter accepted by the
2886 function, as well as information specific to the function itself.
2887
2888 A Signature object has the following public attributes and methods:
2889
Jens Reidel611836a2020-03-18 03:22:46 +01002890 * parameters : OrderedDict
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002891 An ordered mapping of parameters' names to the corresponding
2892 Parameter objects (keyword-only arguments are in the same order
2893 as listed in `code.co_varnames`).
2894 * return_annotation : object
2895 The annotation for the return type of the function if specified.
2896 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002897 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002898 * bind(*args, **kwargs) -> BoundArguments
2899 Creates a mapping from positional and keyword arguments to
2900 parameters.
2901 * bind_partial(*args, **kwargs) -> BoundArguments
2902 Creates a partial mapping from positional and keyword arguments
2903 to parameters (simulating 'functools.partial' behavior.)
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002904 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002905
2906 __slots__ = ('_return_annotation', '_parameters')
2907
2908 _parameter_cls = Parameter
2909 _bound_arguments_cls = BoundArguments
2910
2911 empty = _empty
2912
2913 def __init__(self, parameters=None, *, return_annotation=_empty,
2914 __validate_parameters__=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002915 """Constructs Signature from the given list of Parameter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002916 objects and 'return_annotation'. All arguments are optional.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002917 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002918
2919 if parameters is None:
Jens Reidel611836a2020-03-18 03:22:46 +01002920 params = OrderedDict()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002921 else:
2922 if __validate_parameters__:
Jens Reidel611836a2020-03-18 03:22:46 +01002923 params = OrderedDict()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002924 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002925 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002926
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002927 for param in parameters:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002928 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002929 name = param.name
2930
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002931 if kind < top_kind:
Dong-hee Naa9cab432018-05-30 00:04:08 +09002932 msg = (
2933 'wrong parameter order: {} parameter before {} '
2934 'parameter'
2935 )
Dong-hee Na4aa30062018-06-08 12:46:31 +09002936 msg = msg.format(top_kind.description,
2937 kind.description)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002938 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002939 elif kind > top_kind:
2940 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002941 top_kind = kind
2942
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002943 if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
Yury Selivanov07a9e452014-01-29 10:58:16 -05002944 if param.default is _empty:
2945 if kind_defaults:
2946 # No default for this parameter, but the
2947 # previous parameter of the same kind had
2948 # a default
2949 msg = 'non-default argument follows default ' \
2950 'argument'
2951 raise ValueError(msg)
2952 else:
2953 # There is a default for this parameter.
2954 kind_defaults = True
2955
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002956 if name in params:
2957 msg = 'duplicate parameter name: {!r}'.format(name)
2958 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002959
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002960 params[name] = param
2961 else:
Jens Reidel611836a2020-03-18 03:22:46 +01002962 params = OrderedDict((param.name, param) for param in parameters)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002963
2964 self._parameters = types.MappingProxyType(params)
2965 self._return_annotation = return_annotation
2966
2967 @classmethod
2968 def from_function(cls, func):
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002969 """Constructs Signature for the given python function.
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002970
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002971 Deprecated since Python 3.5, use `Signature.from_callable()`.
2972 """
2973
2974 warnings.warn("inspect.Signature.from_function() is deprecated since "
2975 "Python 3.5, use Signature.from_callable()",
Berker Peksagb5601582015-05-21 23:40:54 +03002976 DeprecationWarning, stacklevel=2)
Yury Selivanovcf45f022015-05-20 14:38:50 -04002977 return _signature_from_function(cls, func)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002978
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002979 @classmethod
2980 def from_builtin(cls, func):
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002981 """Constructs Signature for the given builtin function.
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002982
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002983 Deprecated since Python 3.5, use `Signature.from_callable()`.
2984 """
2985
2986 warnings.warn("inspect.Signature.from_builtin() is deprecated since "
2987 "Python 3.5, use Signature.from_callable()",
Berker Peksagb5601582015-05-21 23:40:54 +03002988 DeprecationWarning, stacklevel=2)
Yury Selivanov57d240e2014-02-19 16:27:23 -05002989 return _signature_from_builtin(cls, func)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002990
Yury Selivanovda396452014-03-27 12:09:24 -04002991 @classmethod
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002992 def from_callable(cls, obj, *,
larryhastings74613a42021-04-29 21:16:28 -07002993 follow_wrapped=True, globals=None, locals=None, eval_str=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002994 """Constructs Signature for the given callable object."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002995 return _signature_from_callable(obj, sigcls=cls,
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002996 follow_wrapper_chains=follow_wrapped,
larryhastings74613a42021-04-29 21:16:28 -07002997 globals=globals, locals=locals, eval_str=eval_str)
Yury Selivanovda396452014-03-27 12:09:24 -04002998
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002999 @property
3000 def parameters(self):
3001 return self._parameters
3002
3003 @property
3004 def return_annotation(self):
3005 return self._return_annotation
3006
3007 def replace(self, *, parameters=_void, return_annotation=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003008 """Creates a customized copy of the Signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003009 Pass 'parameters' and/or 'return_annotation' arguments
3010 to override them in the new copy.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003011 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003012
3013 if parameters is _void:
3014 parameters = self.parameters.values()
3015
3016 if return_annotation is _void:
3017 return_annotation = self._return_annotation
3018
3019 return type(self)(parameters,
3020 return_annotation=return_annotation)
3021
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04003022 def _hash_basis(self):
3023 params = tuple(param for param in self.parameters.values()
3024 if param.kind != _KEYWORD_ONLY)
3025
3026 kwo_params = {param.name: param for param in self.parameters.values()
3027 if param.kind == _KEYWORD_ONLY}
3028
3029 return params, kwo_params, self.return_annotation
3030
Yury Selivanov67ae50e2014-04-08 11:46:50 -04003031 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04003032 params, kwo_params, return_annotation = self._hash_basis()
3033 kwo_params = frozenset(kwo_params.values())
3034 return hash((params, kwo_params, return_annotation))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04003035
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003036 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03003037 if self is other:
3038 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03003039 if not isinstance(other, Signature):
3040 return NotImplemented
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03003041 return self._hash_basis() == other._hash_basis()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003042
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003043 def _bind(self, args, kwargs, *, partial=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003044 """Private method. Don't use directly."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003045
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01003046 arguments = {}
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003047
3048 parameters = iter(self.parameters.values())
3049 parameters_ex = ()
3050 arg_vals = iter(args)
3051
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003052 while True:
3053 # Let's iterate through the positional arguments and corresponding
3054 # parameters
3055 try:
3056 arg_val = next(arg_vals)
3057 except StopIteration:
3058 # No more positional arguments
3059 try:
3060 param = next(parameters)
3061 except StopIteration:
3062 # No more parameters. That's it. Just need to check that
3063 # we have no `kwargs` after this while loop
3064 break
3065 else:
3066 if param.kind == _VAR_POSITIONAL:
3067 # That's OK, just empty *args. Let's start parsing
3068 # kwargs
3069 break
3070 elif param.name in kwargs:
3071 if param.kind == _POSITIONAL_ONLY:
3072 msg = '{arg!r} parameter is positional only, ' \
3073 'but was passed as a keyword'
3074 msg = msg.format(arg=param.name)
3075 raise TypeError(msg) from None
3076 parameters_ex = (param,)
3077 break
3078 elif (param.kind == _VAR_KEYWORD or
3079 param.default is not _empty):
3080 # That's fine too - we have a default value for this
3081 # parameter. So, lets start parsing `kwargs`, starting
3082 # with the current parameter
3083 parameters_ex = (param,)
3084 break
3085 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05003086 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
3087 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003088 if partial:
3089 parameters_ex = (param,)
3090 break
3091 else:
Yury Selivanov86872752015-05-19 00:27:49 -04003092 msg = 'missing a required argument: {arg!r}'
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003093 msg = msg.format(arg=param.name)
3094 raise TypeError(msg) from None
3095 else:
3096 # We have a positional argument to process
3097 try:
3098 param = next(parameters)
3099 except StopIteration:
3100 raise TypeError('too many positional arguments') from None
3101 else:
3102 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
3103 # Looks like we have no parameter for this positional
3104 # argument
Yury Selivanov86872752015-05-19 00:27:49 -04003105 raise TypeError(
3106 'too many positional arguments') from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003107
3108 if param.kind == _VAR_POSITIONAL:
3109 # We have an '*args'-like argument, let's fill it with
3110 # all positional arguments we have left and move on to
3111 # the next phase
3112 values = [arg_val]
3113 values.extend(arg_vals)
3114 arguments[param.name] = tuple(values)
3115 break
3116
Pablo Galindof3ef06a2019-10-15 12:40:02 +01003117 if param.name in kwargs and param.kind != _POSITIONAL_ONLY:
Yury Selivanov86872752015-05-19 00:27:49 -04003118 raise TypeError(
3119 'multiple values for argument {arg!r}'.format(
3120 arg=param.name)) from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003121
3122 arguments[param.name] = arg_val
3123
3124 # Now, we iterate through the remaining parameters to process
3125 # keyword arguments
3126 kwargs_param = None
3127 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003128 if param.kind == _VAR_KEYWORD:
3129 # Memorize that we have a '**kwargs'-like parameter
3130 kwargs_param = param
3131 continue
3132
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05003133 if param.kind == _VAR_POSITIONAL:
3134 # Named arguments don't refer to '*args'-like parameters.
3135 # We only arrive here if the positional arguments ended
3136 # before reaching the last parameter before *args.
3137 continue
3138
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003139 param_name = param.name
3140 try:
3141 arg_val = kwargs.pop(param_name)
3142 except KeyError:
3143 # We have no value for this parameter. It's fine though,
3144 # if it has a default value, or it is an '*args'-like
3145 # parameter, left alone by the processing of positional
3146 # arguments.
3147 if (not partial and param.kind != _VAR_POSITIONAL and
3148 param.default is _empty):
Yury Selivanov86872752015-05-19 00:27:49 -04003149 raise TypeError('missing a required argument: {arg!r}'. \
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003150 format(arg=param_name)) from None
3151
3152 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05003153 if param.kind == _POSITIONAL_ONLY:
3154 # This should never happen in case of a properly built
3155 # Signature object (but let's have this check here
3156 # to ensure correct behaviour just in case)
3157 raise TypeError('{arg!r} parameter is positional only, '
3158 'but was passed as a keyword'. \
3159 format(arg=param.name))
3160
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003161 arguments[param_name] = arg_val
3162
3163 if kwargs:
3164 if kwargs_param is not None:
3165 # Process our '**kwargs'-like parameter
3166 arguments[kwargs_param.name] = kwargs
3167 else:
Yury Selivanov86872752015-05-19 00:27:49 -04003168 raise TypeError(
3169 'got an unexpected keyword argument {arg!r}'.format(
3170 arg=next(iter(kwargs))))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003171
3172 return self._bound_arguments_cls(self, arguments)
3173
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003174 def bind(self, /, *args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003175 """Get a BoundArguments object, that maps the passed `args`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003176 and `kwargs` to the function's signature. Raises `TypeError`
3177 if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003178 """
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003179 return self._bind(args, kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003180
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003181 def bind_partial(self, /, *args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003182 """Get a BoundArguments object, that partially maps the
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003183 passed `args` and `kwargs` to the function's signature.
3184 Raises `TypeError` if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003185 """
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003186 return self._bind(args, kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003187
Yury Selivanova5d63dd2014-03-27 11:31:43 -04003188 def __reduce__(self):
3189 return (type(self),
3190 (tuple(self._parameters.values()),),
3191 {'_return_annotation': self._return_annotation})
3192
3193 def __setstate__(self, state):
3194 self._return_annotation = state['_return_annotation']
3195
Yury Selivanov374375d2014-03-27 12:41:53 -04003196 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04003197 return '<{} {}>'.format(self.__class__.__name__, self)
Yury Selivanov374375d2014-03-27 12:41:53 -04003198
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003199 def __str__(self):
3200 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05003201 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003202 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05003203 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003204 formatted = str(param)
3205
3206 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05003207
3208 if kind == _POSITIONAL_ONLY:
3209 render_pos_only_separator = True
3210 elif render_pos_only_separator:
3211 # It's not a positional-only parameter, and the flag
3212 # is set to 'True' (there were pos-only params before.)
3213 result.append('/')
3214 render_pos_only_separator = False
3215
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003216 if kind == _VAR_POSITIONAL:
3217 # OK, we have an '*args'-like parameter, so we won't need
3218 # a '*' to separate keyword-only arguments
3219 render_kw_only_separator = False
3220 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
3221 # We have a keyword-only parameter to render and we haven't
3222 # rendered an '*args'-like parameter before, so add a '*'
3223 # separator to the parameters list ("foo(arg1, *, arg2)" case)
3224 result.append('*')
3225 # This condition should be only triggered once, so
3226 # reset the flag
3227 render_kw_only_separator = False
3228
3229 result.append(formatted)
3230
Yury Selivanov2393dca2014-01-27 15:07:58 -05003231 if render_pos_only_separator:
3232 # There were only positional-only parameters, hence the
3233 # flag was not reset to 'False'
3234 result.append('/')
3235
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003236 rendered = '({})'.format(', '.join(result))
3237
3238 if self.return_annotation is not _empty:
3239 anno = formatannotation(self.return_annotation)
3240 rendered += ' -> {}'.format(anno)
3241
3242 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003243
Yury Selivanovda396452014-03-27 12:09:24 -04003244
larryhastings74613a42021-04-29 21:16:28 -07003245def signature(obj, *, follow_wrapped=True, globals=None, locals=None, eval_str=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003246 """Get a signature object for the passed callable."""
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03003247 return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
larryhastings74613a42021-04-29 21:16:28 -07003248 globals=globals, locals=locals, eval_str=eval_str)
Yury Selivanovda396452014-03-27 12:09:24 -04003249
3250
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003251def _main():
3252 """ Logic for inspecting an object given at command line """
3253 import argparse
3254 import importlib
3255
3256 parser = argparse.ArgumentParser()
3257 parser.add_argument(
3258 'object',
3259 help="The object to be analysed. "
3260 "It supports the 'module:qualname' syntax")
3261 parser.add_argument(
3262 '-d', '--details', action='store_true',
3263 help='Display info about the module rather than its source code')
3264
3265 args = parser.parse_args()
3266
3267 target = args.object
3268 mod_name, has_attrs, attrs = target.partition(":")
3269 try:
3270 obj = module = importlib.import_module(mod_name)
3271 except Exception as exc:
3272 msg = "Failed to import {} ({}: {})".format(mod_name,
3273 type(exc).__name__,
3274 exc)
3275 print(msg, file=sys.stderr)
Alan Yeee3c59a72019-09-09 07:15:43 -07003276 sys.exit(2)
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003277
3278 if has_attrs:
3279 parts = attrs.split(".")
3280 obj = module
3281 for part in parts:
3282 obj = getattr(obj, part)
3283
3284 if module.__name__ in sys.builtin_module_names:
3285 print("Can't get info for builtin modules.", file=sys.stderr)
Alan Yeee3c59a72019-09-09 07:15:43 -07003286 sys.exit(1)
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003287
3288 if args.details:
3289 print('Target: {}'.format(target))
3290 print('Origin: {}'.format(getsourcefile(module)))
3291 print('Cached: {}'.format(module.__cached__))
3292 if obj is module:
3293 print('Loader: {}'.format(repr(module.__loader__)))
3294 if hasattr(module, '__path__'):
3295 print('Submodule search path: {}'.format(module.__path__))
3296 else:
3297 try:
3298 __, lineno = findsource(obj)
3299 except Exception:
3300 pass
3301 else:
3302 print('Line: {}'.format(lineno))
3303
3304 print('\n')
3305 else:
3306 print(getsource(obj))
3307
3308
3309if __name__ == "__main__":
3310 _main()