blob: 7aedcf1a941b3fbe0ce5f25f6c44e451dde3076c [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
398 co_names tuple of names of local variables
399 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.', '')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001360 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001361 if annotation.__module__ in ('builtins', base_module):
Serhiy Storchaka521e5862014-07-22 15:00:37 +03001362 return annotation.__qualname__
1363 return annotation.__module__+'.'+annotation.__qualname__
Guido van Rossum2e65f892007-02-28 22:03:49 +00001364 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001365
Guido van Rossum2e65f892007-02-28 22:03:49 +00001366def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001367 module = getattr(object, '__module__', None)
1368 def _formatannotation(annotation):
1369 return formatannotation(annotation, module)
1370 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001371
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001372def formatargspec(args, varargs=None, varkw=None, defaults=None,
Pablo Galindod5d2b452019-04-30 02:01:14 +01001373 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001374 formatarg=str,
1375 formatvarargs=lambda name: '*' + name,
1376 formatvarkw=lambda name: '**' + name,
1377 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001378 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001379 formatannotation=formatannotation):
Berker Peksagfa3922c2015-07-31 04:11:29 +03001380 """Format an argument spec from the values returned by getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001381
Guido van Rossum2e65f892007-02-28 22:03:49 +00001382 The first seven arguments are (args, varargs, varkw, defaults,
1383 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1384 are the corresponding optional formatting functions that are called to
1385 turn names and values into strings. The last argument is an optional
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +02001386 function to format the sequence of arguments.
1387
1388 Deprecated since Python 3.5: use the `signature` function and `Signature`
1389 objects.
1390 """
1391
1392 from warnings import warn
1393
1394 warn("`formatargspec` is deprecated since Python 3.5. Use `signature` and "
Zackery Spytz41254eb2018-06-11 21:16:18 -06001395 "the `Signature` object directly",
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +02001396 DeprecationWarning,
1397 stacklevel=2)
1398
Guido van Rossum2e65f892007-02-28 22:03:49 +00001399 def formatargandannotation(arg):
1400 result = formatarg(arg)
1401 if arg in annotations:
1402 result += ': ' + formatannotation(annotations[arg])
1403 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001404 specs = []
1405 if defaults:
Pablo Galindod5d2b452019-04-30 02:01:14 +01001406 firstdefault = len(args) - len(defaults)
1407 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001408 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001409 if defaults and i >= firstdefault:
1410 spec = spec + formatvalue(defaults[i - firstdefault])
1411 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001412 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001413 specs.append(formatvarargs(formatargandannotation(varargs)))
1414 else:
1415 if kwonlyargs:
1416 specs.append('*')
1417 if kwonlyargs:
1418 for kwonlyarg in kwonlyargs:
1419 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001420 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001421 spec += formatvalue(kwonlydefaults[kwonlyarg])
1422 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001423 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001424 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001425 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001426 if 'return' in annotations:
1427 result += formatreturns(formatannotation(annotations['return']))
1428 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001429
1430def formatargvalues(args, varargs, varkw, locals,
1431 formatarg=str,
1432 formatvarargs=lambda name: '*' + name,
1433 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001434 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001435 """Format an argument spec from the 4 values returned by getargvalues.
1436
1437 The first four arguments are (args, varargs, varkw, locals). The
1438 next four arguments are the corresponding optional formatting functions
1439 that are called to turn names and values into strings. The ninth
1440 argument is an optional function to format the sequence of arguments."""
1441 def convert(name, locals=locals,
1442 formatarg=formatarg, formatvalue=formatvalue):
1443 return formatarg(name) + formatvalue(locals[name])
1444 specs = []
1445 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001446 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001447 if varargs:
1448 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1449 if varkw:
1450 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001451 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001452
Benjamin Petersone109c702011-06-24 09:37:26 -05001453def _missing_arguments(f_name, argnames, pos, values):
1454 names = [repr(name) for name in argnames if name not in values]
1455 missing = len(names)
1456 if missing == 1:
1457 s = names[0]
1458 elif missing == 2:
1459 s = "{} and {}".format(*names)
1460 else:
Yury Selivanovdccfa132014-03-27 18:42:52 -04001461 tail = ", {} and {}".format(*names[-2:])
Benjamin Petersone109c702011-06-24 09:37:26 -05001462 del names[-2:]
1463 s = ", ".join(names) + tail
1464 raise TypeError("%s() missing %i required %s argument%s: %s" %
1465 (f_name, missing,
1466 "positional" if pos else "keyword-only",
1467 "" if missing == 1 else "s", s))
1468
1469def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001470 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001471 kwonly_given = len([arg for arg in kwonly if arg in values])
1472 if varargs:
1473 plural = atleast != 1
1474 sig = "at least %d" % (atleast,)
1475 elif defcount:
1476 plural = True
1477 sig = "from %d to %d" % (atleast, len(args))
1478 else:
1479 plural = len(args) != 1
1480 sig = str(len(args))
1481 kwonly_sig = ""
1482 if kwonly_given:
1483 msg = " positional argument%s (and %d keyword-only argument%s)"
1484 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1485 "s" if kwonly_given != 1 else ""))
1486 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1487 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1488 "was" if given == 1 and not kwonly_given else "were"))
1489
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03001490def getcallargs(func, /, *positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001491 """Get the mapping of arguments to values.
1492
1493 A dict is returned, with keys the function argument names (including the
1494 names of the * and ** arguments, if any), and values the respective bound
1495 values from 'positional' and 'named'."""
1496 spec = getfullargspec(func)
Pablo Galindod5d2b452019-04-30 02:01:14 +01001497 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001498 f_name = func.__name__
1499 arg2value = {}
1500
Benjamin Petersonb204a422011-06-05 22:04:07 -05001501
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001502 if ismethod(func) and func.__self__ is not None:
1503 # implicit 'self' (or 'cls' for classmethods) argument
1504 positional = (func.__self__,) + positional
1505 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001506 num_args = len(args)
1507 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001508
Benjamin Petersonb204a422011-06-05 22:04:07 -05001509 n = min(num_pos, num_args)
1510 for i in range(n):
Pablo Galindod5d2b452019-04-30 02:01:14 +01001511 arg2value[args[i]] = positional[i]
Benjamin Petersonb204a422011-06-05 22:04:07 -05001512 if varargs:
1513 arg2value[varargs] = tuple(positional[n:])
1514 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001515 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001516 arg2value[varkw] = {}
1517 for kw, value in named.items():
1518 if kw not in possible_kwargs:
1519 if not varkw:
1520 raise TypeError("%s() got an unexpected keyword argument %r" %
1521 (f_name, kw))
1522 arg2value[varkw][kw] = value
1523 continue
1524 if kw in arg2value:
1525 raise TypeError("%s() got multiple values for argument %r" %
1526 (f_name, kw))
1527 arg2value[kw] = value
1528 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001529 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1530 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001531 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001532 req = args[:num_args - num_defaults]
1533 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001534 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001535 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001536 for i, arg in enumerate(args[num_args - num_defaults:]):
1537 if arg not in arg2value:
1538 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001539 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001540 for kwarg in kwonlyargs:
1541 if kwarg not in arg2value:
Yury Selivanov875df202014-03-27 18:23:03 -04001542 if kwonlydefaults and kwarg in kwonlydefaults:
Benjamin Petersone109c702011-06-24 09:37:26 -05001543 arg2value[kwarg] = kwonlydefaults[kwarg]
1544 else:
1545 missing += 1
1546 if missing:
1547 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001548 return arg2value
1549
Nick Coghlan2f92e542012-06-23 19:39:55 +10001550ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1551
1552def getclosurevars(func):
1553 """
1554 Get the mapping of free variables to their current values.
1555
Meador Inge8fda3592012-07-19 21:33:21 -05001556 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001557 and builtin references as seen by the body of the function. A final
1558 set of unbound names that could not be resolved is also provided.
1559 """
1560
1561 if ismethod(func):
1562 func = func.__func__
1563
1564 if not isfunction(func):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001565 raise TypeError("{!r} is not a Python function".format(func))
Nick Coghlan2f92e542012-06-23 19:39:55 +10001566
1567 code = func.__code__
1568 # Nonlocal references are named in co_freevars and resolved
1569 # by looking them up in __closure__ by positional index
1570 if func.__closure__ is None:
1571 nonlocal_vars = {}
1572 else:
1573 nonlocal_vars = {
1574 var : cell.cell_contents
1575 for var, cell in zip(code.co_freevars, func.__closure__)
1576 }
1577
1578 # Global and builtin references are named in co_names and resolved
1579 # by looking them up in __globals__ or __builtins__
1580 global_ns = func.__globals__
1581 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1582 if ismodule(builtin_ns):
1583 builtin_ns = builtin_ns.__dict__
1584 global_vars = {}
1585 builtin_vars = {}
1586 unbound_names = set()
1587 for name in code.co_names:
1588 if name in ("None", "True", "False"):
1589 # Because these used to be builtins instead of keywords, they
1590 # may still show up as name references. We ignore them.
1591 continue
1592 try:
1593 global_vars[name] = global_ns[name]
1594 except KeyError:
1595 try:
1596 builtin_vars[name] = builtin_ns[name]
1597 except KeyError:
1598 unbound_names.add(name)
1599
1600 return ClosureVars(nonlocal_vars, global_vars,
1601 builtin_vars, unbound_names)
1602
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001603# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001604
1605Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1606
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001607def getframeinfo(frame, context=1):
1608 """Get information about a frame or traceback object.
1609
1610 A tuple of five things is returned: the filename, the line number of
1611 the current line, the function name, a list of lines of context from
1612 the source code, and the index of the current line within that list.
1613 The optional second argument specifies the number of lines of context
1614 to return, which are centered around the current line."""
1615 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001616 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001617 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001618 else:
1619 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001620 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001621 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001622
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001623 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001624 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001625 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001626 try:
1627 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001628 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001629 lines = index = None
1630 else:
Raymond Hettingera0501712004-06-15 11:22:53 +00001631 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001632 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001633 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001634 else:
1635 lines = index = None
1636
Christian Heimes25bb7832008-01-11 16:17:00 +00001637 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001638
1639def getlineno(frame):
1640 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001641 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1642 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001643
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001644FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
1645
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001646def getouterframes(frame, context=1):
1647 """Get a list of records for a frame and all higher (calling) frames.
1648
1649 Each record contains a frame object, filename, line number, function
1650 name, a list of lines of context, and index within the context."""
1651 framelist = []
1652 while frame:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001653 frameinfo = (frame,) + getframeinfo(frame, context)
1654 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001655 frame = frame.f_back
1656 return framelist
1657
1658def getinnerframes(tb, context=1):
1659 """Get a list of records for a traceback's frame and all lower frames.
1660
1661 Each record contains a frame object, filename, line number, function
1662 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001663 framelist = []
1664 while tb:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001665 frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
1666 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001667 tb = tb.tb_next
1668 return framelist
1669
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001670def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001671 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001672 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001673
1674def stack(context=1):
1675 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001676 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001677
1678def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001679 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001680 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001681
1682
1683# ------------------------------------------------ static version of getattr
1684
1685_sentinel = object()
1686
Michael Foorde5162652010-11-20 16:40:44 +00001687def _static_getmro(klass):
1688 return type.__dict__['__mro__'].__get__(klass)
1689
Michael Foord95fc51d2010-11-20 15:07:30 +00001690def _check_instance(obj, attr):
1691 instance_dict = {}
1692 try:
1693 instance_dict = object.__getattribute__(obj, "__dict__")
1694 except AttributeError:
1695 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001696 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001697
1698
1699def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001700 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001701 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001702 try:
1703 return entry.__dict__[attr]
1704 except KeyError:
1705 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001706 return _sentinel
1707
Michael Foord35184ed2010-11-20 16:58:30 +00001708def _is_type(obj):
1709 try:
1710 _static_getmro(obj)
1711 except TypeError:
1712 return False
1713 return True
1714
Michael Foorddcebe0f2011-03-15 19:20:44 -04001715def _shadowed_dict(klass):
1716 dict_attr = type.__dict__["__dict__"]
1717 for entry in _static_getmro(klass):
1718 try:
1719 class_dict = dict_attr.__get__(entry)["__dict__"]
1720 except KeyError:
1721 pass
1722 else:
Inada Naoki8f9cc872019-09-05 13:07:08 +09001723 if not (type(class_dict) is types.GetSetDescriptorType and
Michael Foorddcebe0f2011-03-15 19:20:44 -04001724 class_dict.__name__ == "__dict__" and
1725 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001726 return class_dict
1727 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001728
1729def getattr_static(obj, attr, default=_sentinel):
1730 """Retrieve attributes without triggering dynamic lookup via the
1731 descriptor protocol, __getattr__ or __getattribute__.
1732
1733 Note: this function may not be able to retrieve all attributes
1734 that getattr can fetch (like dynamically created attributes)
1735 and may find attributes that getattr can't (like descriptors
1736 that raise AttributeError). It can also return descriptor objects
1737 instead of instance members in some cases. See the
1738 documentation for details.
1739 """
1740 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001741 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001742 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001743 dict_attr = _shadowed_dict(klass)
1744 if (dict_attr is _sentinel or
Inada Naoki8f9cc872019-09-05 13:07:08 +09001745 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001746 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001747 else:
1748 klass = obj
1749
1750 klass_result = _check_class(klass, attr)
1751
1752 if instance_result is not _sentinel and klass_result is not _sentinel:
1753 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1754 _check_class(type(klass_result), '__set__') is not _sentinel):
1755 return klass_result
1756
1757 if instance_result is not _sentinel:
1758 return instance_result
1759 if klass_result is not _sentinel:
1760 return klass_result
1761
1762 if obj is klass:
1763 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001764 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001765 if _shadowed_dict(type(entry)) is _sentinel:
1766 try:
1767 return entry.__dict__[attr]
1768 except KeyError:
1769 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001770 if default is not _sentinel:
1771 return default
1772 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001773
1774
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001775# ------------------------------------------------ generator introspection
1776
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001777GEN_CREATED = 'GEN_CREATED'
1778GEN_RUNNING = 'GEN_RUNNING'
1779GEN_SUSPENDED = 'GEN_SUSPENDED'
1780GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001781
1782def getgeneratorstate(generator):
1783 """Get current state of a generator-iterator.
1784
1785 Possible states are:
1786 GEN_CREATED: Waiting to start execution.
1787 GEN_RUNNING: Currently being executed by the interpreter.
1788 GEN_SUSPENDED: Currently suspended at a yield expression.
1789 GEN_CLOSED: Execution has completed.
1790 """
1791 if generator.gi_running:
1792 return GEN_RUNNING
1793 if generator.gi_frame is None:
1794 return GEN_CLOSED
1795 if generator.gi_frame.f_lasti == -1:
1796 return GEN_CREATED
1797 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001798
1799
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001800def getgeneratorlocals(generator):
1801 """
1802 Get the mapping of generator local variables to their current values.
1803
1804 A dict is returned, with the keys the local variable names and values the
1805 bound values."""
1806
1807 if not isgenerator(generator):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001808 raise TypeError("{!r} is not a Python generator".format(generator))
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001809
1810 frame = getattr(generator, "gi_frame", None)
1811 if frame is not None:
1812 return generator.gi_frame.f_locals
1813 else:
1814 return {}
1815
Yury Selivanov5376ba92015-06-22 12:19:30 -04001816
1817# ------------------------------------------------ coroutine introspection
1818
1819CORO_CREATED = 'CORO_CREATED'
1820CORO_RUNNING = 'CORO_RUNNING'
1821CORO_SUSPENDED = 'CORO_SUSPENDED'
1822CORO_CLOSED = 'CORO_CLOSED'
1823
1824def getcoroutinestate(coroutine):
1825 """Get current state of a coroutine object.
1826
1827 Possible states are:
1828 CORO_CREATED: Waiting to start execution.
1829 CORO_RUNNING: Currently being executed by the interpreter.
1830 CORO_SUSPENDED: Currently suspended at an await expression.
1831 CORO_CLOSED: Execution has completed.
1832 """
1833 if coroutine.cr_running:
1834 return CORO_RUNNING
1835 if coroutine.cr_frame is None:
1836 return CORO_CLOSED
1837 if coroutine.cr_frame.f_lasti == -1:
1838 return CORO_CREATED
1839 return CORO_SUSPENDED
1840
1841
1842def getcoroutinelocals(coroutine):
1843 """
1844 Get the mapping of coroutine local variables to their current values.
1845
1846 A dict is returned, with the keys the local variable names and values the
1847 bound values."""
1848 frame = getattr(coroutine, "cr_frame", None)
1849 if frame is not None:
1850 return frame.f_locals
1851 else:
1852 return {}
1853
1854
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001855###############################################################################
1856### Function Signature Object (PEP 362)
1857###############################################################################
1858
1859
1860_WrapperDescriptor = type(type.__call__)
1861_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001862_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001863
1864_NonUserDefinedCallables = (_WrapperDescriptor,
1865 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001866 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001867 types.BuiltinFunctionType)
1868
1869
Yury Selivanov421f0c72014-01-29 12:05:40 -05001870def _signature_get_user_defined_method(cls, method_name):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001871 """Private helper. Checks if ``cls`` has an attribute
1872 named ``method_name`` and returns it only if it is a
1873 pure python function.
1874 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001875 try:
1876 meth = getattr(cls, method_name)
1877 except AttributeError:
1878 return
1879 else:
1880 if not isinstance(meth, _NonUserDefinedCallables):
1881 # Once '__signature__' will be added to 'C'-level
1882 # callables, this check won't be necessary
1883 return meth
1884
1885
Yury Selivanov62560fb2014-01-28 12:26:24 -05001886def _signature_get_partial(wrapped_sig, partial, extra_args=()):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001887 """Private helper to calculate how 'wrapped_sig' signature will
1888 look like after applying a 'functools.partial' object (or alike)
1889 on it.
1890 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001891
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001892 old_params = wrapped_sig.parameters
Inada Naoki21105512020-03-02 18:54:49 +09001893 new_params = OrderedDict(old_params.items())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001894
1895 partial_args = partial.args or ()
1896 partial_keywords = partial.keywords or {}
1897
1898 if extra_args:
1899 partial_args = extra_args + partial_args
1900
1901 try:
1902 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1903 except TypeError as ex:
1904 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1905 raise ValueError(msg) from ex
1906
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001907
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001908 transform_to_kwonly = False
1909 for param_name, param in old_params.items():
1910 try:
1911 arg_value = ba.arguments[param_name]
1912 except KeyError:
1913 pass
1914 else:
1915 if param.kind is _POSITIONAL_ONLY:
1916 # If positional-only parameter is bound by partial,
1917 # it effectively disappears from the signature
Inada Naoki21105512020-03-02 18:54:49 +09001918 new_params.pop(param_name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001919 continue
1920
1921 if param.kind is _POSITIONAL_OR_KEYWORD:
1922 if param_name in partial_keywords:
1923 # This means that this parameter, and all parameters
1924 # after it should be keyword-only (and var-positional
1925 # should be removed). Here's why. Consider the following
1926 # function:
1927 # foo(a, b, *args, c):
1928 # pass
1929 #
1930 # "partial(foo, a='spam')" will have the following
1931 # signature: "(*, a='spam', b, c)". Because attempting
1932 # to call that partial with "(10, 20)" arguments will
1933 # raise a TypeError, saying that "a" argument received
1934 # multiple values.
1935 transform_to_kwonly = True
1936 # Set the new default value
Inada Naoki21105512020-03-02 18:54:49 +09001937 new_params[param_name] = param.replace(default=arg_value)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001938 else:
1939 # was passed as a positional argument
Inada Naoki21105512020-03-02 18:54:49 +09001940 new_params.pop(param.name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001941 continue
1942
1943 if param.kind is _KEYWORD_ONLY:
1944 # Set the new default value
Inada Naoki21105512020-03-02 18:54:49 +09001945 new_params[param_name] = param.replace(default=arg_value)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001946
1947 if transform_to_kwonly:
1948 assert param.kind is not _POSITIONAL_ONLY
1949
1950 if param.kind is _POSITIONAL_OR_KEYWORD:
Inada Naoki21105512020-03-02 18:54:49 +09001951 new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1952 new_params[param_name] = new_param
1953 new_params.move_to_end(param_name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001954 elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
Inada Naoki21105512020-03-02 18:54:49 +09001955 new_params.move_to_end(param_name)
1956 elif param.kind is _VAR_POSITIONAL:
1957 new_params.pop(param.name)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001958
1959 return wrapped_sig.replace(parameters=new_params.values())
1960
1961
Yury Selivanov62560fb2014-01-28 12:26:24 -05001962def _signature_bound_method(sig):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001963 """Private helper to transform signatures for unbound
1964 functions to bound methods.
1965 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001966
1967 params = tuple(sig.parameters.values())
1968
1969 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1970 raise ValueError('invalid method signature')
1971
1972 kind = params[0].kind
1973 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1974 # Drop first parameter:
1975 # '(p1, p2[, ...])' -> '(p2[, ...])'
1976 params = params[1:]
1977 else:
1978 if kind is not _VAR_POSITIONAL:
1979 # Unless we add a new parameter type we never
1980 # get here
1981 raise ValueError('invalid argument type')
1982 # It's a var-positional parameter.
1983 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1984
1985 return sig.replace(parameters=params)
1986
1987
Yury Selivanovb77511d2014-01-29 10:46:14 -05001988def _signature_is_builtin(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001989 """Private helper to test if `obj` is a callable that might
1990 support Argument Clinic's __text_signature__ protocol.
1991 """
Yury Selivanov1d241832014-02-02 12:51:20 -05001992 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001993 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001994 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001995 # Can't test 'isinstance(type)' here, as it would
1996 # also be True for regular python classes
1997 obj in (type, object))
1998
1999
Yury Selivanov63da7c72014-01-31 14:48:37 -05002000def _signature_is_functionlike(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002001 """Private helper to test if `obj` is a duck type of FunctionType.
2002 A good example of such objects are functions compiled with
2003 Cython, which have all attributes that a pure Python function
2004 would have, but have their code statically compiled.
2005 """
Yury Selivanov63da7c72014-01-31 14:48:37 -05002006
2007 if not callable(obj) or isclass(obj):
2008 # All function-like objects are obviously callables,
2009 # and not classes.
2010 return False
2011
2012 name = getattr(obj, '__name__', None)
2013 code = getattr(obj, '__code__', None)
2014 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
2015 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
Pablo Galindob0544ba2021-04-21 12:41:19 +01002016 annotations = getattr(obj, '__annotations__', None)
Yury Selivanov63da7c72014-01-31 14:48:37 -05002017
2018 return (isinstance(code, types.CodeType) and
2019 isinstance(name, str) and
2020 (defaults is None or isinstance(defaults, tuple)) and
2021 (kwdefaults is None or isinstance(kwdefaults, dict)) and
larryhastings74613a42021-04-29 21:16:28 -07002022 (isinstance(annotations, (dict)) or annotations is None) )
Yury Selivanov63da7c72014-01-31 14:48:37 -05002023
2024
Yury Selivanovd82eddc2014-01-29 11:24:39 -05002025def _signature_get_bound_param(spec):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002026 """ Private helper to get first parameter name from a
2027 __text_signature__ of a builtin method, which should
2028 be in the following format: '($param1, ...)'.
2029 Assumptions are that the first argument won't have
2030 a default value or an annotation.
2031 """
Yury Selivanovd82eddc2014-01-29 11:24:39 -05002032
2033 assert spec.startswith('($')
2034
2035 pos = spec.find(',')
2036 if pos == -1:
2037 pos = spec.find(')')
2038
2039 cpos = spec.find(':')
2040 assert cpos == -1 or cpos > pos
2041
2042 cpos = spec.find('=')
2043 assert cpos == -1 or cpos > pos
2044
2045 return spec[2:pos]
2046
2047
Larry Hastings2623c8c2014-02-08 22:15:29 -08002048def _signature_strip_non_python_syntax(signature):
2049 """
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002050 Private helper function. Takes a signature in Argument Clinic's
2051 extended signature format.
2052
Larry Hastings2623c8c2014-02-08 22:15:29 -08002053 Returns a tuple of three things:
2054 * that signature re-rendered in standard Python syntax,
2055 * the index of the "self" parameter (generally 0), or None if
2056 the function does not have a "self" parameter, and
2057 * the index of the last "positional only" parameter,
2058 or None if the signature has no positional-only parameters.
2059 """
2060
2061 if not signature:
2062 return signature, None, None
2063
2064 self_parameter = None
2065 last_positional_only = None
2066
2067 lines = [l.encode('ascii') for l in signature.split('\n')]
2068 generator = iter(lines).__next__
2069 token_stream = tokenize.tokenize(generator)
2070
2071 delayed_comma = False
2072 skip_next_comma = False
2073 text = []
2074 add = text.append
2075
2076 current_parameter = 0
2077 OP = token.OP
2078 ERRORTOKEN = token.ERRORTOKEN
2079
2080 # token stream always starts with ENCODING token, skip it
2081 t = next(token_stream)
2082 assert t.type == tokenize.ENCODING
2083
2084 for t in token_stream:
2085 type, string = t.type, t.string
2086
2087 if type == OP:
2088 if string == ',':
2089 if skip_next_comma:
2090 skip_next_comma = False
2091 else:
2092 assert not delayed_comma
2093 delayed_comma = True
2094 current_parameter += 1
2095 continue
2096
2097 if string == '/':
2098 assert not skip_next_comma
2099 assert last_positional_only is None
2100 skip_next_comma = True
2101 last_positional_only = current_parameter - 1
2102 continue
2103
2104 if (type == ERRORTOKEN) and (string == '$'):
2105 assert self_parameter is None
2106 self_parameter = current_parameter
2107 continue
2108
2109 if delayed_comma:
2110 delayed_comma = False
2111 if not ((type == OP) and (string == ')')):
2112 add(', ')
2113 add(string)
2114 if (string == ','):
2115 add(' ')
2116 clean_signature = ''.join(text)
2117 return clean_signature, self_parameter, last_positional_only
2118
2119
Yury Selivanov57d240e2014-02-19 16:27:23 -05002120def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002121 """Private helper to parse content of '__text_signature__'
2122 and return a Signature based on it.
2123 """
INADA Naoki37420de2018-01-27 10:10:06 +09002124 # Lazy import ast because it's relatively heavy and
2125 # it's not used for other than this function.
2126 import ast
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002127
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002128 Parameter = cls._parameter_cls
2129
Larry Hastings2623c8c2014-02-08 22:15:29 -08002130 clean_signature, self_parameter, last_positional_only = \
2131 _signature_strip_non_python_syntax(s)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002132
Larry Hastings2623c8c2014-02-08 22:15:29 -08002133 program = "def foo" + clean_signature + ": pass"
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002134
2135 try:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002136 module = ast.parse(program)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002137 except SyntaxError:
2138 module = None
2139
2140 if not isinstance(module, ast.Module):
2141 raise ValueError("{!r} builtin has invalid signature".format(obj))
2142
2143 f = module.body[0]
2144
2145 parameters = []
2146 empty = Parameter.empty
2147 invalid = object()
2148
2149 module = None
2150 module_dict = {}
2151 module_name = getattr(obj, '__module__', None)
2152 if module_name:
2153 module = sys.modules.get(module_name, None)
2154 if module:
2155 module_dict = module.__dict__
INADA Naoki6f85b822018-10-05 01:47:09 +09002156 sys_module_dict = sys.modules.copy()
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002157
2158 def parse_name(node):
2159 assert isinstance(node, ast.arg)
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05302160 if node.annotation is not None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002161 raise ValueError("Annotations are not currently supported")
2162 return node.arg
2163
2164 def wrap_value(s):
2165 try:
2166 value = eval(s, module_dict)
2167 except NameError:
2168 try:
2169 value = eval(s, sys_module_dict)
2170 except NameError:
2171 raise RuntimeError()
2172
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002173 if isinstance(value, (str, int, float, bytes, bool, type(None))):
2174 return ast.Constant(value)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002175 raise RuntimeError()
2176
2177 class RewriteSymbolics(ast.NodeTransformer):
2178 def visit_Attribute(self, node):
2179 a = []
2180 n = node
2181 while isinstance(n, ast.Attribute):
2182 a.append(n.attr)
2183 n = n.value
2184 if not isinstance(n, ast.Name):
2185 raise RuntimeError()
2186 a.append(n.id)
2187 value = ".".join(reversed(a))
2188 return wrap_value(value)
2189
2190 def visit_Name(self, node):
2191 if not isinstance(node.ctx, ast.Load):
2192 raise ValueError()
2193 return wrap_value(node.id)
2194
2195 def p(name_node, default_node, default=empty):
2196 name = parse_name(name_node)
2197 if name is invalid:
2198 return None
2199 if default_node and default_node is not _empty:
2200 try:
2201 default_node = RewriteSymbolics().visit(default_node)
2202 o = ast.literal_eval(default_node)
2203 except ValueError:
2204 o = invalid
2205 if o is invalid:
2206 return None
2207 default = o if o is not invalid else default
2208 parameters.append(Parameter(name, kind, default=default, annotation=empty))
2209
2210 # non-keyword-only parameters
2211 args = reversed(f.args.args)
2212 defaults = reversed(f.args.defaults)
2213 iter = itertools.zip_longest(args, defaults, fillvalue=None)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002214 if last_positional_only is not None:
2215 kind = Parameter.POSITIONAL_ONLY
2216 else:
2217 kind = Parameter.POSITIONAL_OR_KEYWORD
2218 for i, (name, default) in enumerate(reversed(list(iter))):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002219 p(name, default)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002220 if i == last_positional_only:
2221 kind = Parameter.POSITIONAL_OR_KEYWORD
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002222
2223 # *args
2224 if f.args.vararg:
2225 kind = Parameter.VAR_POSITIONAL
2226 p(f.args.vararg, empty)
2227
2228 # keyword-only arguments
2229 kind = Parameter.KEYWORD_ONLY
2230 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
2231 p(name, default)
2232
2233 # **kwargs
2234 if f.args.kwarg:
2235 kind = Parameter.VAR_KEYWORD
2236 p(f.args.kwarg, empty)
2237
Larry Hastings2623c8c2014-02-08 22:15:29 -08002238 if self_parameter is not None:
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002239 # Possibly strip the bound argument:
2240 # - We *always* strip first bound argument if
2241 # it is a module.
2242 # - We don't strip first bound argument if
2243 # skip_bound_arg is False.
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002244 assert parameters
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002245 _self = getattr(obj, '__self__', None)
2246 self_isbound = _self is not None
2247 self_ismodule = ismodule(_self)
2248 if self_isbound and (self_ismodule or skip_bound_arg):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002249 parameters.pop(0)
2250 else:
2251 # for builtins, self parameter is always positional-only!
2252 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
2253 parameters[0] = p
2254
2255 return cls(parameters, return_annotation=cls.empty)
2256
2257
Yury Selivanov57d240e2014-02-19 16:27:23 -05002258def _signature_from_builtin(cls, func, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002259 """Private helper function to get signature for
2260 builtin callables.
2261 """
2262
Yury Selivanov57d240e2014-02-19 16:27:23 -05002263 if not _signature_is_builtin(func):
2264 raise TypeError("{!r} is not a Python builtin "
2265 "function".format(func))
2266
2267 s = getattr(func, "__text_signature__", None)
2268 if not s:
2269 raise ValueError("no signature found for builtin {!r}".format(func))
2270
2271 return _signature_fromstr(cls, func, s, skip_bound_arg)
2272
2273
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002274def _signature_from_function(cls, func, skip_bound_arg=True,
larryhastings74613a42021-04-29 21:16:28 -07002275 globals=None, locals=None, eval_str=False):
Yury Selivanovcf45f022015-05-20 14:38:50 -04002276 """Private helper: constructs Signature for the given python function."""
2277
2278 is_duck_function = False
2279 if not isfunction(func):
2280 if _signature_is_functionlike(func):
2281 is_duck_function = True
2282 else:
2283 # If it's not a pure Python function, and not a duck type
2284 # of pure function:
2285 raise TypeError('{!r} is not a Python function'.format(func))
2286
Serhiy Storchakad53cf992019-05-06 22:40:27 +03002287 s = getattr(func, "__text_signature__", None)
2288 if s:
2289 return _signature_fromstr(cls, func, s, skip_bound_arg)
2290
Yury Selivanovcf45f022015-05-20 14:38:50 -04002291 Parameter = cls._parameter_cls
2292
2293 # Parameter information.
2294 func_code = func.__code__
2295 pos_count = func_code.co_argcount
2296 arg_names = func_code.co_varnames
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002297 posonly_count = func_code.co_posonlyargcount
Pablo Galindocd74e662019-06-01 18:08:04 +01002298 positional = arg_names[:pos_count]
Yury Selivanovcf45f022015-05-20 14:38:50 -04002299 keyword_only_count = func_code.co_kwonlyargcount
Pablo Galindocd74e662019-06-01 18:08:04 +01002300 keyword_only = arg_names[pos_count:pos_count + keyword_only_count]
larryhastings74613a42021-04-29 21:16:28 -07002301 annotations = get_annotations(func, globals=globals, locals=locals, eval_str=eval_str)
Yury Selivanovcf45f022015-05-20 14:38:50 -04002302 defaults = func.__defaults__
2303 kwdefaults = func.__kwdefaults__
2304
2305 if defaults:
2306 pos_default_count = len(defaults)
2307 else:
2308 pos_default_count = 0
2309
2310 parameters = []
2311
Pablo Galindocd74e662019-06-01 18:08:04 +01002312 non_default_count = pos_count - pos_default_count
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002313 posonly_left = posonly_count
2314
Yury Selivanovcf45f022015-05-20 14:38:50 -04002315 # Non-keyword-only parameters w/o defaults.
Pablo Galindocd74e662019-06-01 18:08:04 +01002316 for name in positional[:non_default_count]:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002317 kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD
Yury Selivanovcf45f022015-05-20 14:38:50 -04002318 annotation = annotations.get(name, _empty)
2319 parameters.append(Parameter(name, annotation=annotation,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002320 kind=kind))
2321 if posonly_left:
2322 posonly_left -= 1
Yury Selivanovcf45f022015-05-20 14:38:50 -04002323
2324 # ... w/ defaults.
Pablo Galindocd74e662019-06-01 18:08:04 +01002325 for offset, name in enumerate(positional[non_default_count:]):
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002326 kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD
Yury Selivanovcf45f022015-05-20 14:38:50 -04002327 annotation = annotations.get(name, _empty)
2328 parameters.append(Parameter(name, annotation=annotation,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002329 kind=kind,
Yury Selivanovcf45f022015-05-20 14:38:50 -04002330 default=defaults[offset]))
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002331 if posonly_left:
2332 posonly_left -= 1
Yury Selivanovcf45f022015-05-20 14:38:50 -04002333
2334 # *args
2335 if func_code.co_flags & CO_VARARGS:
Pablo Galindocd74e662019-06-01 18:08:04 +01002336 name = arg_names[pos_count + keyword_only_count]
Yury Selivanovcf45f022015-05-20 14:38:50 -04002337 annotation = annotations.get(name, _empty)
2338 parameters.append(Parameter(name, annotation=annotation,
2339 kind=_VAR_POSITIONAL))
2340
2341 # Keyword-only parameters.
2342 for name in keyword_only:
2343 default = _empty
2344 if kwdefaults is not None:
2345 default = kwdefaults.get(name, _empty)
2346
2347 annotation = annotations.get(name, _empty)
2348 parameters.append(Parameter(name, annotation=annotation,
2349 kind=_KEYWORD_ONLY,
2350 default=default))
2351 # **kwargs
2352 if func_code.co_flags & CO_VARKEYWORDS:
Pablo Galindocd74e662019-06-01 18:08:04 +01002353 index = pos_count + keyword_only_count
Yury Selivanovcf45f022015-05-20 14:38:50 -04002354 if func_code.co_flags & CO_VARARGS:
2355 index += 1
2356
2357 name = arg_names[index]
2358 annotation = annotations.get(name, _empty)
2359 parameters.append(Parameter(name, annotation=annotation,
2360 kind=_VAR_KEYWORD))
2361
2362 # Is 'func' is a pure Python function - don't validate the
2363 # parameters list (for correct order and defaults), it should be OK.
2364 return cls(parameters,
2365 return_annotation=annotations.get('return', _empty),
2366 __validate_parameters__=is_duck_function)
2367
2368
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002369def _signature_from_callable(obj, *,
2370 follow_wrapper_chains=True,
2371 skip_bound_arg=True,
larryhastings74613a42021-04-29 21:16:28 -07002372 globals=None,
2373 locals=None,
2374 eval_str=False,
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002375 sigcls):
2376
2377 """Private helper function to get signature for arbitrary
2378 callable objects.
2379 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002380
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002381 _get_signature_of = functools.partial(_signature_from_callable,
2382 follow_wrapper_chains=follow_wrapper_chains,
2383 skip_bound_arg=skip_bound_arg,
larryhastings74613a42021-04-29 21:16:28 -07002384 globals=globals,
2385 locals=locals,
2386 sigcls=sigcls,
2387 eval_str=eval_str)
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002388
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002389 if not callable(obj):
2390 raise TypeError('{!r} is not a callable object'.format(obj))
2391
2392 if isinstance(obj, types.MethodType):
2393 # In this case we skip the first parameter of the underlying
2394 # function (usually `self` or `cls`).
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002395 sig = _get_signature_of(obj.__func__)
Yury Selivanovda396452014-03-27 12:09:24 -04002396
Yury Selivanov57d240e2014-02-19 16:27:23 -05002397 if skip_bound_arg:
2398 return _signature_bound_method(sig)
2399 else:
2400 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002401
Nick Coghlane8c45d62013-07-28 20:00:01 +10002402 # Was this function wrapped by a decorator?
Yury Selivanov57d240e2014-02-19 16:27:23 -05002403 if follow_wrapper_chains:
2404 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
Yury Selivanov46c759d2015-05-27 21:56:53 -04002405 if isinstance(obj, types.MethodType):
2406 # If the unwrapped object is a *method*, we might want to
2407 # skip its first parameter (self).
2408 # See test_signature_wrapped_bound_method for details.
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002409 return _get_signature_of(obj)
Nick Coghlane8c45d62013-07-28 20:00:01 +10002410
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002411 try:
2412 sig = obj.__signature__
2413 except AttributeError:
2414 pass
2415 else:
2416 if sig is not None:
Yury Selivanov42407ab2014-06-23 10:23:50 -07002417 if not isinstance(sig, Signature):
2418 raise TypeError(
2419 'unexpected object {!r} in __signature__ '
2420 'attribute'.format(sig))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002421 return sig
2422
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002423 try:
2424 partialmethod = obj._partialmethod
2425 except AttributeError:
2426 pass
2427 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05002428 if isinstance(partialmethod, functools.partialmethod):
2429 # Unbound partialmethod (see functools.partialmethod)
2430 # This means, that we need to calculate the signature
2431 # as if it's a regular partial object, but taking into
2432 # account that the first positional argument
2433 # (usually `self`, or `cls`) will not be passed
2434 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002435
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002436 wrapped_sig = _get_signature_of(partialmethod.func)
Yury Selivanovda396452014-03-27 12:09:24 -04002437
Yury Selivanov0486f812014-01-29 12:18:59 -05002438 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanov0486f812014-01-29 12:18:59 -05002439 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
Dong-hee Na378d7062017-05-18 04:00:51 +09002440 if first_wrapped_param.kind is Parameter.VAR_POSITIONAL:
2441 # First argument of the wrapped callable is `*args`, as in
2442 # `partialmethod(lambda *args)`.
2443 return sig
2444 else:
2445 sig_params = tuple(sig.parameters.values())
Yury Selivanov8a387212018-03-06 12:59:45 -05002446 assert (not sig_params or
2447 first_wrapped_param is not sig_params[0])
Dong-hee Na378d7062017-05-18 04:00:51 +09002448 new_params = (first_wrapped_param,) + sig_params
2449 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002450
Yury Selivanov63da7c72014-01-31 14:48:37 -05002451 if isfunction(obj) or _signature_is_functionlike(obj):
2452 # If it's a pure Python function, or an object that is duck type
2453 # of a Python function (Cython functions, for instance), then:
Serhiy Storchakad53cf992019-05-06 22:40:27 +03002454 return _signature_from_function(sigcls, obj,
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002455 skip_bound_arg=skip_bound_arg,
larryhastings74613a42021-04-29 21:16:28 -07002456 globals=globals, locals=locals, eval_str=eval_str)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002457
Yury Selivanova773de02014-02-21 18:30:53 -05002458 if _signature_is_builtin(obj):
Yury Selivanovda396452014-03-27 12:09:24 -04002459 return _signature_from_builtin(sigcls, obj,
Yury Selivanova773de02014-02-21 18:30:53 -05002460 skip_bound_arg=skip_bound_arg)
2461
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002462 if isinstance(obj, functools.partial):
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002463 wrapped_sig = _get_signature_of(obj.func)
Yury Selivanov62560fb2014-01-28 12:26:24 -05002464 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002465
2466 sig = None
2467 if isinstance(obj, type):
2468 # obj is a class or a metaclass
2469
2470 # First, let's see if it has an overloaded __call__ defined
2471 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05002472 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002473 if call is not None:
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002474 sig = _get_signature_of(call)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002475 else:
Miss Islington (bot)948e39a2021-07-16 06:25:57 -07002476 factory_method = None
Yury Selivanov421f0c72014-01-29 12:05:40 -05002477 new = _signature_get_user_defined_method(obj, '__new__')
Miss Islington (bot)948e39a2021-07-16 06:25:57 -07002478 init = _signature_get_user_defined_method(obj, '__init__')
2479 # Now we check if the 'obj' class has an own '__new__' method
2480 if '__new__' in obj.__dict__:
2481 factory_method = new
2482 # or an own '__init__' method
2483 elif '__init__' in obj.__dict__:
2484 factory_method = init
2485 # If not, we take inherited '__new__' or '__init__', if present
2486 elif new is not None:
2487 factory_method = new
2488 elif init is not None:
2489 factory_method = init
2490
2491 if factory_method is not None:
2492 sig = _get_signature_of(factory_method)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002493
2494 if sig is None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002495 # At this point we know, that `obj` is a class, with no user-
2496 # defined '__init__', '__new__', or class-level '__call__'
2497
Larry Hastings2623c8c2014-02-08 22:15:29 -08002498 for base in obj.__mro__[:-1]:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002499 # Since '__text_signature__' is implemented as a
2500 # descriptor that extracts text signature from the
2501 # class docstring, if 'obj' is derived from a builtin
2502 # class, its own '__text_signature__' may be 'None'.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002503 # Therefore, we go through the MRO (except the last
2504 # class in there, which is 'object') to find the first
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002505 # class with non-empty text signature.
2506 try:
2507 text_sig = base.__text_signature__
2508 except AttributeError:
2509 pass
2510 else:
2511 if text_sig:
2512 # If 'obj' class has a __text_signature__ attribute:
2513 # return a signature based on it
Yury Selivanovda396452014-03-27 12:09:24 -04002514 return _signature_fromstr(sigcls, obj, text_sig)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002515
2516 # No '__text_signature__' was found for the 'obj' class.
2517 # Last option is to check if its '__init__' is
2518 # object.__init__ or type.__init__.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002519 if type not in obj.__mro__:
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002520 # We have a class (not metaclass), but no user-defined
2521 # __init__ or __new__ for it
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002522 if (obj.__init__ is object.__init__ and
2523 obj.__new__ is object.__new__):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002524 # Return a signature of 'object' builtin.
Gregory P. Smith5b9ff7a2019-09-13 17:13:51 +01002525 return sigcls.from_callable(object)
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002526 else:
2527 raise ValueError(
2528 'no signature found for builtin type {!r}'.format(obj))
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002529
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002530 elif not isinstance(obj, _NonUserDefinedCallables):
2531 # An object with __call__
2532 # We also check that the 'obj' is not an instance of
2533 # _WrapperDescriptor or _MethodWrapper to avoid
2534 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05002535 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002536 if call is not None:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002537 try:
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002538 sig = _get_signature_of(call)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002539 except ValueError as ex:
2540 msg = 'no signature found for {!r}'.format(obj)
2541 raise ValueError(msg) from ex
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002542
2543 if sig is not None:
2544 # For classes and objects we skip the first parameter of their
2545 # __call__, __new__, or __init__ methods
Yury Selivanov57d240e2014-02-19 16:27:23 -05002546 if skip_bound_arg:
2547 return _signature_bound_method(sig)
2548 else:
2549 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002550
2551 if isinstance(obj, types.BuiltinFunctionType):
2552 # Raise a nicer error message for builtins
2553 msg = 'no signature found for builtin function {!r}'.format(obj)
2554 raise ValueError(msg)
2555
2556 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2557
2558
2559class _void:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002560 """A private marker - used in Parameter & Signature."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002561
2562
2563class _empty:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002564 """Marker object for Signature.empty and Parameter.empty."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002565
2566
Yury Selivanov21e83a52014-03-27 11:23:13 -04002567class _ParameterKind(enum.IntEnum):
2568 POSITIONAL_ONLY = 0
2569 POSITIONAL_OR_KEYWORD = 1
2570 VAR_POSITIONAL = 2
2571 KEYWORD_ONLY = 3
2572 VAR_KEYWORD = 4
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002573
Ethan Furman9bf7c2d2021-07-03 21:08:42 -07002574 def __str__(self):
2575 return self._name_
2576
Dong-hee Na4aa30062018-06-08 12:46:31 +09002577 @property
2578 def description(self):
2579 return _PARAM_NAME_MAPPING[self]
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002580
Yury Selivanov21e83a52014-03-27 11:23:13 -04002581_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
2582_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
2583_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
2584_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
2585_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002586
Dong-hee Naa9cab432018-05-30 00:04:08 +09002587_PARAM_NAME_MAPPING = {
2588 _POSITIONAL_ONLY: 'positional-only',
2589 _POSITIONAL_OR_KEYWORD: 'positional or keyword',
2590 _VAR_POSITIONAL: 'variadic positional',
2591 _KEYWORD_ONLY: 'keyword-only',
2592 _VAR_KEYWORD: 'variadic keyword'
2593}
2594
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002595
2596class Parameter:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002597 """Represents a parameter in a function signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002598
2599 Has the following public attributes:
2600
2601 * name : str
2602 The name of the parameter as a string.
2603 * default : object
2604 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002605 parameter has no default value, this attribute is set to
2606 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002607 * annotation
2608 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002609 parameter has no annotation, this attribute is set to
2610 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002611 * kind : str
2612 Describes how argument values are bound to the parameter.
2613 Possible values: `Parameter.POSITIONAL_ONLY`,
2614 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
2615 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002616 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002617
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002618 __slots__ = ('_name', '_kind', '_default', '_annotation')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002619
2620 POSITIONAL_ONLY = _POSITIONAL_ONLY
2621 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
2622 VAR_POSITIONAL = _VAR_POSITIONAL
2623 KEYWORD_ONLY = _KEYWORD_ONLY
2624 VAR_KEYWORD = _VAR_KEYWORD
2625
2626 empty = _empty
2627
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002628 def __init__(self, name, kind, *, default=_empty, annotation=_empty):
Dong-hee Naa9cab432018-05-30 00:04:08 +09002629 try:
2630 self._kind = _ParameterKind(kind)
2631 except ValueError:
2632 raise ValueError(f'value {kind!r} is not a valid Parameter.kind')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002633 if default is not _empty:
Dong-hee Naa9cab432018-05-30 00:04:08 +09002634 if self._kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2635 msg = '{} parameters cannot have default values'
Dong-hee Na4aa30062018-06-08 12:46:31 +09002636 msg = msg.format(self._kind.description)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002637 raise ValueError(msg)
2638 self._default = default
2639 self._annotation = annotation
2640
Yury Selivanov2393dca2014-01-27 15:07:58 -05002641 if name is _empty:
2642 raise ValueError('name is a required attribute for Parameter')
2643
2644 if not isinstance(name, str):
Dong-hee Naa9cab432018-05-30 00:04:08 +09002645 msg = 'name must be a str, not a {}'.format(type(name).__name__)
2646 raise TypeError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002647
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002648 if name[0] == '.' and name[1:].isdigit():
2649 # These are implicit arguments generated by comprehensions. In
2650 # order to provide a friendlier interface to users, we recast
2651 # their name as "implicitN" and treat them as positional-only.
2652 # See issue 19611.
Dong-hee Naa9cab432018-05-30 00:04:08 +09002653 if self._kind != _POSITIONAL_OR_KEYWORD:
2654 msg = (
2655 'implicit arguments must be passed as '
2656 'positional or keyword arguments, not {}'
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002657 )
Dong-hee Na4aa30062018-06-08 12:46:31 +09002658 msg = msg.format(self._kind.description)
Dong-hee Naa9cab432018-05-30 00:04:08 +09002659 raise ValueError(msg)
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002660 self._kind = _POSITIONAL_ONLY
2661 name = 'implicit{}'.format(name[1:])
2662
Yury Selivanov2393dca2014-01-27 15:07:58 -05002663 if not name.isidentifier():
2664 raise ValueError('{!r} is not a valid parameter name'.format(name))
2665
2666 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002667
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002668 def __reduce__(self):
2669 return (type(self),
2670 (self._name, self._kind),
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002671 {'_default': self._default,
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002672 '_annotation': self._annotation})
2673
2674 def __setstate__(self, state):
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002675 self._default = state['_default']
2676 self._annotation = state['_annotation']
2677
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002678 @property
2679 def name(self):
2680 return self._name
2681
2682 @property
2683 def default(self):
2684 return self._default
2685
2686 @property
2687 def annotation(self):
2688 return self._annotation
2689
2690 @property
2691 def kind(self):
2692 return self._kind
2693
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002694 def replace(self, *, name=_void, kind=_void,
2695 annotation=_void, default=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002696 """Creates a customized copy of the Parameter."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002697
2698 if name is _void:
2699 name = self._name
2700
2701 if kind is _void:
2702 kind = self._kind
2703
2704 if annotation is _void:
2705 annotation = self._annotation
2706
2707 if default is _void:
2708 default = self._default
2709
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002710 return type(self)(name, kind, default=default, annotation=annotation)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002711
2712 def __str__(self):
2713 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002714 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002715
2716 # Add annotation and default value
2717 if self._annotation is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002718 formatted = '{}: {}'.format(formatted,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002719 formatannotation(self._annotation))
2720
2721 if self._default is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002722 if self._annotation is not _empty:
2723 formatted = '{} = {}'.format(formatted, repr(self._default))
2724 else:
2725 formatted = '{}={}'.format(formatted, repr(self._default))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002726
2727 if kind == _VAR_POSITIONAL:
2728 formatted = '*' + formatted
2729 elif kind == _VAR_KEYWORD:
2730 formatted = '**' + formatted
2731
2732 return formatted
2733
2734 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002735 return '<{} "{}">'.format(self.__class__.__name__, self)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002736
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002737 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002738 return hash((self.name, self.kind, self.annotation, self.default))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002739
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002740 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002741 if self is other:
2742 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002743 if not isinstance(other, Parameter):
2744 return NotImplemented
2745 return (self._name == other._name and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002746 self._kind == other._kind and
2747 self._default == other._default and
2748 self._annotation == other._annotation)
2749
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002750
2751class BoundArguments:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002752 """Result of `Signature.bind` call. Holds the mapping of arguments
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002753 to the function's parameters.
2754
2755 Has the following public attributes:
2756
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002757 * arguments : dict
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002758 An ordered mutable mapping of parameters' names to arguments' values.
2759 Does not contain arguments' default values.
2760 * signature : Signature
2761 The Signature object that created this instance.
2762 * args : tuple
2763 Tuple of positional arguments values.
2764 * kwargs : dict
2765 Dict of keyword arguments values.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002766 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002767
Yury Selivanov6abe0322015-05-13 17:18:41 -04002768 __slots__ = ('arguments', '_signature', '__weakref__')
2769
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002770 def __init__(self, signature, arguments):
2771 self.arguments = arguments
2772 self._signature = signature
2773
2774 @property
2775 def signature(self):
2776 return self._signature
2777
2778 @property
2779 def args(self):
2780 args = []
2781 for param_name, param in self._signature.parameters.items():
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002782 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002783 break
2784
2785 try:
2786 arg = self.arguments[param_name]
2787 except KeyError:
2788 # We're done here. Other arguments
2789 # will be mapped in 'BoundArguments.kwargs'
2790 break
2791 else:
2792 if param.kind == _VAR_POSITIONAL:
2793 # *args
2794 args.extend(arg)
2795 else:
2796 # plain argument
2797 args.append(arg)
2798
2799 return tuple(args)
2800
2801 @property
2802 def kwargs(self):
2803 kwargs = {}
2804 kwargs_started = False
2805 for param_name, param in self._signature.parameters.items():
2806 if not kwargs_started:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002807 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002808 kwargs_started = True
2809 else:
2810 if param_name not in self.arguments:
2811 kwargs_started = True
2812 continue
2813
2814 if not kwargs_started:
2815 continue
2816
2817 try:
2818 arg = self.arguments[param_name]
2819 except KeyError:
2820 pass
2821 else:
2822 if param.kind == _VAR_KEYWORD:
2823 # **kwargs
2824 kwargs.update(arg)
2825 else:
2826 # plain keyword argument
2827 kwargs[param_name] = arg
2828
2829 return kwargs
2830
Yury Selivanovb907a512015-05-16 13:45:09 -04002831 def apply_defaults(self):
2832 """Set default values for missing arguments.
2833
2834 For variable-positional arguments (*args) the default is an
2835 empty tuple.
2836
2837 For variable-keyword arguments (**kwargs) the default is an
2838 empty dict.
2839 """
2840 arguments = self.arguments
Yury Selivanovb907a512015-05-16 13:45:09 -04002841 new_arguments = []
2842 for name, param in self._signature.parameters.items():
2843 try:
2844 new_arguments.append((name, arguments[name]))
2845 except KeyError:
2846 if param.default is not _empty:
2847 val = param.default
2848 elif param.kind is _VAR_POSITIONAL:
2849 val = ()
2850 elif param.kind is _VAR_KEYWORD:
2851 val = {}
2852 else:
2853 # This BoundArguments was likely produced by
2854 # Signature.bind_partial().
2855 continue
2856 new_arguments.append((name, val))
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002857 self.arguments = dict(new_arguments)
Yury Selivanovb907a512015-05-16 13:45:09 -04002858
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002859 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002860 if self is other:
2861 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002862 if not isinstance(other, BoundArguments):
2863 return NotImplemented
2864 return (self.signature == other.signature and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002865 self.arguments == other.arguments)
2866
Yury Selivanov6abe0322015-05-13 17:18:41 -04002867 def __setstate__(self, state):
2868 self._signature = state['_signature']
2869 self.arguments = state['arguments']
2870
2871 def __getstate__(self):
2872 return {'_signature': self._signature, 'arguments': self.arguments}
2873
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002874 def __repr__(self):
2875 args = []
2876 for arg, value in self.arguments.items():
2877 args.append('{}={!r}'.format(arg, value))
Yury Selivanovf229bc52015-05-15 12:53:56 -04002878 return '<{} ({})>'.format(self.__class__.__name__, ', '.join(args))
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002879
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002880
2881class Signature:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002882 """A Signature object represents the overall signature of a function.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002883 It stores a Parameter object for each parameter accepted by the
2884 function, as well as information specific to the function itself.
2885
2886 A Signature object has the following public attributes and methods:
2887
Jens Reidel611836a2020-03-18 03:22:46 +01002888 * parameters : OrderedDict
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002889 An ordered mapping of parameters' names to the corresponding
2890 Parameter objects (keyword-only arguments are in the same order
2891 as listed in `code.co_varnames`).
2892 * return_annotation : object
2893 The annotation for the return type of the function if specified.
2894 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002895 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002896 * bind(*args, **kwargs) -> BoundArguments
2897 Creates a mapping from positional and keyword arguments to
2898 parameters.
2899 * bind_partial(*args, **kwargs) -> BoundArguments
2900 Creates a partial mapping from positional and keyword arguments
2901 to parameters (simulating 'functools.partial' behavior.)
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002902 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002903
2904 __slots__ = ('_return_annotation', '_parameters')
2905
2906 _parameter_cls = Parameter
2907 _bound_arguments_cls = BoundArguments
2908
2909 empty = _empty
2910
2911 def __init__(self, parameters=None, *, return_annotation=_empty,
2912 __validate_parameters__=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002913 """Constructs Signature from the given list of Parameter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002914 objects and 'return_annotation'. All arguments are optional.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002915 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002916
2917 if parameters is None:
Jens Reidel611836a2020-03-18 03:22:46 +01002918 params = OrderedDict()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002919 else:
2920 if __validate_parameters__:
Jens Reidel611836a2020-03-18 03:22:46 +01002921 params = OrderedDict()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002922 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002923 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002924
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002925 for param in parameters:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002926 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002927 name = param.name
2928
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002929 if kind < top_kind:
Dong-hee Naa9cab432018-05-30 00:04:08 +09002930 msg = (
2931 'wrong parameter order: {} parameter before {} '
2932 'parameter'
2933 )
Dong-hee Na4aa30062018-06-08 12:46:31 +09002934 msg = msg.format(top_kind.description,
2935 kind.description)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002936 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002937 elif kind > top_kind:
2938 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002939 top_kind = kind
2940
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002941 if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
Yury Selivanov07a9e452014-01-29 10:58:16 -05002942 if param.default is _empty:
2943 if kind_defaults:
2944 # No default for this parameter, but the
2945 # previous parameter of the same kind had
2946 # a default
2947 msg = 'non-default argument follows default ' \
2948 'argument'
2949 raise ValueError(msg)
2950 else:
2951 # There is a default for this parameter.
2952 kind_defaults = True
2953
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002954 if name in params:
2955 msg = 'duplicate parameter name: {!r}'.format(name)
2956 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002957
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002958 params[name] = param
2959 else:
Jens Reidel611836a2020-03-18 03:22:46 +01002960 params = OrderedDict((param.name, param) for param in parameters)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002961
2962 self._parameters = types.MappingProxyType(params)
2963 self._return_annotation = return_annotation
2964
2965 @classmethod
2966 def from_function(cls, func):
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002967 """Constructs Signature for the given python function.
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002968
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002969 Deprecated since Python 3.5, use `Signature.from_callable()`.
2970 """
2971
2972 warnings.warn("inspect.Signature.from_function() is deprecated since "
2973 "Python 3.5, use Signature.from_callable()",
Berker Peksagb5601582015-05-21 23:40:54 +03002974 DeprecationWarning, stacklevel=2)
Yury Selivanovcf45f022015-05-20 14:38:50 -04002975 return _signature_from_function(cls, func)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002976
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002977 @classmethod
2978 def from_builtin(cls, func):
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002979 """Constructs Signature for the given builtin function.
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002980
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002981 Deprecated since Python 3.5, use `Signature.from_callable()`.
2982 """
2983
2984 warnings.warn("inspect.Signature.from_builtin() is deprecated since "
2985 "Python 3.5, use Signature.from_callable()",
Berker Peksagb5601582015-05-21 23:40:54 +03002986 DeprecationWarning, stacklevel=2)
Yury Selivanov57d240e2014-02-19 16:27:23 -05002987 return _signature_from_builtin(cls, func)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002988
Yury Selivanovda396452014-03-27 12:09:24 -04002989 @classmethod
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002990 def from_callable(cls, obj, *,
larryhastings74613a42021-04-29 21:16:28 -07002991 follow_wrapped=True, globals=None, locals=None, eval_str=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002992 """Constructs Signature for the given callable object."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002993 return _signature_from_callable(obj, sigcls=cls,
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03002994 follow_wrapper_chains=follow_wrapped,
larryhastings74613a42021-04-29 21:16:28 -07002995 globals=globals, locals=locals, eval_str=eval_str)
Yury Selivanovda396452014-03-27 12:09:24 -04002996
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002997 @property
2998 def parameters(self):
2999 return self._parameters
3000
3001 @property
3002 def return_annotation(self):
3003 return self._return_annotation
3004
3005 def replace(self, *, parameters=_void, return_annotation=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003006 """Creates a customized copy of the Signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003007 Pass 'parameters' and/or 'return_annotation' arguments
3008 to override them in the new copy.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003009 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003010
3011 if parameters is _void:
3012 parameters = self.parameters.values()
3013
3014 if return_annotation is _void:
3015 return_annotation = self._return_annotation
3016
3017 return type(self)(parameters,
3018 return_annotation=return_annotation)
3019
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04003020 def _hash_basis(self):
3021 params = tuple(param for param in self.parameters.values()
3022 if param.kind != _KEYWORD_ONLY)
3023
3024 kwo_params = {param.name: param for param in self.parameters.values()
3025 if param.kind == _KEYWORD_ONLY}
3026
3027 return params, kwo_params, self.return_annotation
3028
Yury Selivanov67ae50e2014-04-08 11:46:50 -04003029 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04003030 params, kwo_params, return_annotation = self._hash_basis()
3031 kwo_params = frozenset(kwo_params.values())
3032 return hash((params, kwo_params, return_annotation))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04003033
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003034 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03003035 if self is other:
3036 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03003037 if not isinstance(other, Signature):
3038 return NotImplemented
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03003039 return self._hash_basis() == other._hash_basis()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003040
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003041 def _bind(self, args, kwargs, *, partial=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003042 """Private method. Don't use directly."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003043
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01003044 arguments = {}
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003045
3046 parameters = iter(self.parameters.values())
3047 parameters_ex = ()
3048 arg_vals = iter(args)
3049
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003050 while True:
3051 # Let's iterate through the positional arguments and corresponding
3052 # parameters
3053 try:
3054 arg_val = next(arg_vals)
3055 except StopIteration:
3056 # No more positional arguments
3057 try:
3058 param = next(parameters)
3059 except StopIteration:
3060 # No more parameters. That's it. Just need to check that
3061 # we have no `kwargs` after this while loop
3062 break
3063 else:
3064 if param.kind == _VAR_POSITIONAL:
3065 # That's OK, just empty *args. Let's start parsing
3066 # kwargs
3067 break
3068 elif param.name in kwargs:
3069 if param.kind == _POSITIONAL_ONLY:
3070 msg = '{arg!r} parameter is positional only, ' \
3071 'but was passed as a keyword'
3072 msg = msg.format(arg=param.name)
3073 raise TypeError(msg) from None
3074 parameters_ex = (param,)
3075 break
3076 elif (param.kind == _VAR_KEYWORD or
3077 param.default is not _empty):
3078 # That's fine too - we have a default value for this
3079 # parameter. So, lets start parsing `kwargs`, starting
3080 # with the current parameter
3081 parameters_ex = (param,)
3082 break
3083 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05003084 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
3085 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003086 if partial:
3087 parameters_ex = (param,)
3088 break
3089 else:
Yury Selivanov86872752015-05-19 00:27:49 -04003090 msg = 'missing a required argument: {arg!r}'
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003091 msg = msg.format(arg=param.name)
3092 raise TypeError(msg) from None
3093 else:
3094 # We have a positional argument to process
3095 try:
3096 param = next(parameters)
3097 except StopIteration:
3098 raise TypeError('too many positional arguments') from None
3099 else:
3100 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
3101 # Looks like we have no parameter for this positional
3102 # argument
Yury Selivanov86872752015-05-19 00:27:49 -04003103 raise TypeError(
3104 'too many positional arguments') from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003105
3106 if param.kind == _VAR_POSITIONAL:
3107 # We have an '*args'-like argument, let's fill it with
3108 # all positional arguments we have left and move on to
3109 # the next phase
3110 values = [arg_val]
3111 values.extend(arg_vals)
3112 arguments[param.name] = tuple(values)
3113 break
3114
Pablo Galindof3ef06a2019-10-15 12:40:02 +01003115 if param.name in kwargs and param.kind != _POSITIONAL_ONLY:
Yury Selivanov86872752015-05-19 00:27:49 -04003116 raise TypeError(
3117 'multiple values for argument {arg!r}'.format(
3118 arg=param.name)) from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003119
3120 arguments[param.name] = arg_val
3121
3122 # Now, we iterate through the remaining parameters to process
3123 # keyword arguments
3124 kwargs_param = None
3125 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003126 if param.kind == _VAR_KEYWORD:
3127 # Memorize that we have a '**kwargs'-like parameter
3128 kwargs_param = param
3129 continue
3130
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05003131 if param.kind == _VAR_POSITIONAL:
3132 # Named arguments don't refer to '*args'-like parameters.
3133 # We only arrive here if the positional arguments ended
3134 # before reaching the last parameter before *args.
3135 continue
3136
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003137 param_name = param.name
3138 try:
3139 arg_val = kwargs.pop(param_name)
3140 except KeyError:
3141 # We have no value for this parameter. It's fine though,
3142 # if it has a default value, or it is an '*args'-like
3143 # parameter, left alone by the processing of positional
3144 # arguments.
3145 if (not partial and param.kind != _VAR_POSITIONAL and
3146 param.default is _empty):
Yury Selivanov86872752015-05-19 00:27:49 -04003147 raise TypeError('missing a required argument: {arg!r}'. \
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003148 format(arg=param_name)) from None
3149
3150 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05003151 if param.kind == _POSITIONAL_ONLY:
3152 # This should never happen in case of a properly built
3153 # Signature object (but let's have this check here
3154 # to ensure correct behaviour just in case)
3155 raise TypeError('{arg!r} parameter is positional only, '
3156 'but was passed as a keyword'. \
3157 format(arg=param.name))
3158
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003159 arguments[param_name] = arg_val
3160
3161 if kwargs:
3162 if kwargs_param is not None:
3163 # Process our '**kwargs'-like parameter
3164 arguments[kwargs_param.name] = kwargs
3165 else:
Yury Selivanov86872752015-05-19 00:27:49 -04003166 raise TypeError(
3167 'got an unexpected keyword argument {arg!r}'.format(
3168 arg=next(iter(kwargs))))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003169
3170 return self._bound_arguments_cls(self, arguments)
3171
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003172 def bind(self, /, *args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003173 """Get a BoundArguments object, that maps the passed `args`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003174 and `kwargs` to the function's signature. Raises `TypeError`
3175 if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003176 """
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003177 return self._bind(args, kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003178
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003179 def bind_partial(self, /, *args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003180 """Get a BoundArguments object, that partially maps the
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003181 passed `args` and `kwargs` to the function's signature.
3182 Raises `TypeError` if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003183 """
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003184 return self._bind(args, kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003185
Yury Selivanova5d63dd2014-03-27 11:31:43 -04003186 def __reduce__(self):
3187 return (type(self),
3188 (tuple(self._parameters.values()),),
3189 {'_return_annotation': self._return_annotation})
3190
3191 def __setstate__(self, state):
3192 self._return_annotation = state['_return_annotation']
3193
Yury Selivanov374375d2014-03-27 12:41:53 -04003194 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04003195 return '<{} {}>'.format(self.__class__.__name__, self)
Yury Selivanov374375d2014-03-27 12:41:53 -04003196
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003197 def __str__(self):
3198 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05003199 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003200 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05003201 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003202 formatted = str(param)
3203
3204 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05003205
3206 if kind == _POSITIONAL_ONLY:
3207 render_pos_only_separator = True
3208 elif render_pos_only_separator:
3209 # It's not a positional-only parameter, and the flag
3210 # is set to 'True' (there were pos-only params before.)
3211 result.append('/')
3212 render_pos_only_separator = False
3213
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003214 if kind == _VAR_POSITIONAL:
3215 # OK, we have an '*args'-like parameter, so we won't need
3216 # a '*' to separate keyword-only arguments
3217 render_kw_only_separator = False
3218 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
3219 # We have a keyword-only parameter to render and we haven't
3220 # rendered an '*args'-like parameter before, so add a '*'
3221 # separator to the parameters list ("foo(arg1, *, arg2)" case)
3222 result.append('*')
3223 # This condition should be only triggered once, so
3224 # reset the flag
3225 render_kw_only_separator = False
3226
3227 result.append(formatted)
3228
Yury Selivanov2393dca2014-01-27 15:07:58 -05003229 if render_pos_only_separator:
3230 # There were only positional-only parameters, hence the
3231 # flag was not reset to 'False'
3232 result.append('/')
3233
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003234 rendered = '({})'.format(', '.join(result))
3235
3236 if self.return_annotation is not _empty:
3237 anno = formatannotation(self.return_annotation)
3238 rendered += ' -> {}'.format(anno)
3239
3240 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003241
Yury Selivanovda396452014-03-27 12:09:24 -04003242
larryhastings74613a42021-04-29 21:16:28 -07003243def signature(obj, *, follow_wrapped=True, globals=None, locals=None, eval_str=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003244 """Get a signature object for the passed callable."""
Batuhan Taskayaeee1c772020-12-24 01:45:13 +03003245 return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
larryhastings74613a42021-04-29 21:16:28 -07003246 globals=globals, locals=locals, eval_str=eval_str)
Yury Selivanovda396452014-03-27 12:09:24 -04003247
3248
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003249def _main():
3250 """ Logic for inspecting an object given at command line """
3251 import argparse
3252 import importlib
3253
3254 parser = argparse.ArgumentParser()
3255 parser.add_argument(
3256 'object',
3257 help="The object to be analysed. "
3258 "It supports the 'module:qualname' syntax")
3259 parser.add_argument(
3260 '-d', '--details', action='store_true',
3261 help='Display info about the module rather than its source code')
3262
3263 args = parser.parse_args()
3264
3265 target = args.object
3266 mod_name, has_attrs, attrs = target.partition(":")
3267 try:
3268 obj = module = importlib.import_module(mod_name)
3269 except Exception as exc:
3270 msg = "Failed to import {} ({}: {})".format(mod_name,
3271 type(exc).__name__,
3272 exc)
3273 print(msg, file=sys.stderr)
Alan Yeee3c59a72019-09-09 07:15:43 -07003274 sys.exit(2)
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003275
3276 if has_attrs:
3277 parts = attrs.split(".")
3278 obj = module
3279 for part in parts:
3280 obj = getattr(obj, part)
3281
3282 if module.__name__ in sys.builtin_module_names:
3283 print("Can't get info for builtin modules.", file=sys.stderr)
Alan Yeee3c59a72019-09-09 07:15:43 -07003284 sys.exit(1)
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003285
3286 if args.details:
3287 print('Target: {}'.format(target))
3288 print('Origin: {}'.format(getsourcefile(module)))
3289 print('Cached: {}'.format(module.__cached__))
3290 if obj is module:
3291 print('Loader: {}'.format(repr(module.__loader__)))
3292 if hasattr(module, '__path__'):
3293 print('Submodule search path: {}'.format(module.__path__))
3294 else:
3295 try:
3296 __, lineno = findsource(obj)
3297 except Exception:
3298 pass
3299 else:
3300 print('Line: {}'.format(lineno))
3301
3302 print('\n')
3303 else:
3304 print(getsource(obj))
3305
3306
3307if __name__ == "__main__":
3308 _main()