blob: 7412d0e837cf14f6fa74cf4eb72fc7aa56c3f765 [file] [log] [blame]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001"""Get useful information from live Python objects.
2
3This module encapsulates the interface provided by the internal special
Neal Norwitz221085d2007-02-25 20:55:47 +00004attributes (co_*, im_*, tb_*, etc.) in a friendlier fashion.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00005It also provides some help for examining source code and class layout.
6
7Here are some of the useful functions provided by this module:
8
Christian Heimes7131fd92008-02-19 14:21:46 +00009 ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
10 isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(),
11 isroutine() - check object types
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000012 getmembers() - get members of an object that satisfy a given condition
13
14 getfile(), getsourcefile(), getsource() - find an object's source code
15 getdoc(), getcomments() - get documentation on an object
16 getmodule() - determine the module that an object came from
17 getclasstree() - arrange classes so as to represent their hierarchy
18
Berker Peksagfa3922c2015-07-31 04:11:29 +030019 getargvalues(), getcallargs() - get info about function arguments
Yury Selivanov0cf3ed62014-04-01 10:17:08 -040020 getfullargspec() - same, with support for Python 3 features
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +020021 formatargvalues() - format an argument spec
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000022 getouterframes(), getinnerframes() - get info about frames
23 currentframe() - get the current stack frame
24 stack(), trace() - get info about frames on the stack or in a traceback
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070025
26 signature() - get a Signature object for the callable
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000027"""
28
29# This module is in the public domain. No warranties.
30
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070031__author__ = ('Ka-Ping Yee <ping@lfw.org>',
32 'Yury Selivanov <yselivanov@sprymix.com>')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000033
Natefcfe80e2017-04-24 10:06:15 -070034import abc
Karthikeyan Singaravelan696136b2020-04-18 21:49:32 +053035import ast
Antoine Pitroua8723a02015-04-15 00:41:29 +020036import dis
Yury Selivanov75445082015-05-11 22:57:16 -040037import collections.abc
Yury Selivanov21e83a52014-03-27 11:23:13 -040038import enum
Brett Cannoncb66eb02012-05-11 12:58:42 -040039import importlib.machinery
40import itertools
Christian Heimes7131fd92008-02-19 14:21:46 +000041import linecache
Brett Cannoncb66eb02012-05-11 12:58:42 -040042import os
43import re
44import sys
45import tokenize
Larry Hastings2623c8c2014-02-08 22:15:29 -080046import token
Brett Cannoncb66eb02012-05-11 12:58:42 -040047import types
Batuhan Taskaya044a1042020-10-06 23:03:02 +030048import typing
Brett Cannon2b88fcf2012-06-02 22:28:42 -040049import warnings
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070050import functools
Nick Coghlan2f92e542012-06-23 19:39:55 +100051import builtins
Raymond Hettingera1a992c2005-03-11 06:46:45 +000052from operator import attrgetter
Inada Naoki21105512020-03-02 18:54:49 +090053from collections import namedtuple, OrderedDict
Nick Coghlan09c81232010-08-17 10:18:16 +000054
55# Create constants for the compiler flags in Include/code.h
Antoine Pitroua8723a02015-04-15 00:41:29 +020056# We try to get them from dis to avoid duplication
57mod_dict = globals()
58for k, v in dis.COMPILER_FLAG_NAMES.items():
59 mod_dict["CO_" + v] = k
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000060
Christian Heimesbe5b30b2008-03-03 19:18:51 +000061# See Include/object.h
62TPFLAGS_IS_ABSTRACT = 1 << 20
63
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000064# ----------------------------------------------------------- type-checking
65def ismodule(object):
66 """Return true if the object is a module.
67
68 Module objects provide these attributes:
Barry Warsaw28a691b2010-04-17 00:19:56 +000069 __cached__ pathname to byte compiled file
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000070 __doc__ documentation string
71 __file__ filename (missing for built-in modules)"""
Tim Peters28bc59f2001-09-16 08:40:16 +000072 return isinstance(object, types.ModuleType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000073
74def isclass(object):
75 """Return true if the object is a class.
76
77 Class objects provide these attributes:
78 __doc__ documentation string
79 __module__ name of module in which this class was defined"""
Benjamin Petersonc4656002009-01-17 22:41:18 +000080 return isinstance(object, type)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000081
82def ismethod(object):
83 """Return true if the object is an instance method.
84
85 Instance method objects provide these attributes:
86 __doc__ documentation string
87 __name__ name with which this method was defined
Christian Heimesff737952007-11-27 10:40:20 +000088 __func__ function object containing implementation of method
89 __self__ instance to which this method is bound"""
Tim Peters28bc59f2001-09-16 08:40:16 +000090 return isinstance(object, types.MethodType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000091
Tim Peters536d2262001-09-20 05:13:38 +000092def ismethoddescriptor(object):
Tim Petersf1d90b92001-09-20 05:47:55 +000093 """Return true if the object is a method descriptor.
94
95 But not if ismethod() or isclass() or isfunction() are true.
Tim Peters536d2262001-09-20 05:13:38 +000096
97 This is new in Python 2.2, and, for example, is true of int.__add__.
98 An object passing this test has a __get__ attribute but not a __set__
99 attribute, but beyond that the set of attributes varies. __name__ is
100 usually sensible, and __doc__ often is.
101
Tim Petersf1d90b92001-09-20 05:47:55 +0000102 Methods implemented via descriptors that also pass one of the other
103 tests return false from the ismethoddescriptor() test, simply because
104 the other tests promise more -- you can, e.g., count on having the
Christian Heimesff737952007-11-27 10:40:20 +0000105 __func__ attribute (etc) when an object passes ismethod()."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100106 if isclass(object) or ismethod(object) or isfunction(object):
107 # mutual exclusion
108 return False
109 tp = type(object)
110 return hasattr(tp, "__get__") and not hasattr(tp, "__set__")
Tim Peters536d2262001-09-20 05:13:38 +0000111
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000112def isdatadescriptor(object):
113 """Return true if the object is a data descriptor.
114
Aaron Hall, MBA4054b172018-05-20 19:46:42 -0400115 Data descriptors have a __set__ or a __delete__ attribute. Examples are
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000116 properties (defined in Python) and getsets and members (defined in C).
117 Typically, data descriptors will also have __name__ and __doc__ attributes
118 (properties, getsets, and members have both of these attributes), but this
119 is not guaranteed."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100120 if isclass(object) or ismethod(object) or isfunction(object):
121 # mutual exclusion
122 return False
123 tp = type(object)
Aaron Hall, MBA4054b172018-05-20 19:46:42 -0400124 return hasattr(tp, "__set__") or hasattr(tp, "__delete__")
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000125
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000126if hasattr(types, 'MemberDescriptorType'):
127 # CPython and equivalent
128 def ismemberdescriptor(object):
129 """Return true if the object is a member descriptor.
130
131 Member descriptors are specialized descriptors defined in extension
132 modules."""
133 return isinstance(object, types.MemberDescriptorType)
134else:
135 # Other implementations
136 def ismemberdescriptor(object):
137 """Return true if the object is a member descriptor.
138
139 Member descriptors are specialized descriptors defined in extension
140 modules."""
141 return False
142
143if hasattr(types, 'GetSetDescriptorType'):
144 # CPython and equivalent
145 def isgetsetdescriptor(object):
146 """Return true if the object is a getset descriptor.
147
148 getset descriptors are specialized descriptors defined in extension
149 modules."""
150 return isinstance(object, types.GetSetDescriptorType)
151else:
152 # Other implementations
153 def isgetsetdescriptor(object):
154 """Return true if the object is a getset descriptor.
155
156 getset descriptors are specialized descriptors defined in extension
157 modules."""
158 return False
159
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000160def isfunction(object):
161 """Return true if the object is a user-defined function.
162
163 Function objects provide these attributes:
164 __doc__ documentation string
165 __name__ name with which this function was defined
Neal Norwitz221085d2007-02-25 20:55:47 +0000166 __code__ code object containing compiled function bytecode
167 __defaults__ tuple of any default values for arguments
168 __globals__ global namespace in which this function was defined
169 __annotations__ dict of parameter annotations
170 __kwdefaults__ dict of keyword only parameters with defaults"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000171 return isinstance(object, types.FunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000172
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200173def _has_code_flag(f, flag):
174 """Return true if ``f`` is a function (or a method or functools.partial
175 wrapper wrapping a function) whose code object has the given ``flag``
176 set in its flags."""
177 while ismethod(f):
178 f = f.__func__
179 f = functools._unwrap_partial(f)
180 if not isfunction(f):
181 return False
182 return bool(f.__code__.co_flags & flag)
183
Pablo Galindo7cd25432018-10-26 12:19:14 +0100184def isgeneratorfunction(obj):
Christian Heimes7131fd92008-02-19 14:21:46 +0000185 """Return true if the object is a user-defined generator function.
186
Martin Panter0f0eac42016-09-07 11:04:41 +0000187 Generator function objects provide the same attributes as functions.
188 See help(isfunction) for a list of attributes."""
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200189 return _has_code_flag(obj, CO_GENERATOR)
Yury Selivanov75445082015-05-11 22:57:16 -0400190
Pablo Galindo7cd25432018-10-26 12:19:14 +0100191def iscoroutinefunction(obj):
Yury Selivanov75445082015-05-11 22:57:16 -0400192 """Return true if the object is a coroutine function.
193
Yury Selivanov4778e132016-11-08 12:23:09 -0500194 Coroutine functions are defined with "async def" syntax.
Yury Selivanov75445082015-05-11 22:57:16 -0400195 """
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200196 return _has_code_flag(obj, CO_COROUTINE)
Yury Selivanov75445082015-05-11 22:57:16 -0400197
Pablo Galindo7cd25432018-10-26 12:19:14 +0100198def isasyncgenfunction(obj):
Yury Selivanov4778e132016-11-08 12:23:09 -0500199 """Return true if the object is an asynchronous generator function.
200
201 Asynchronous generator functions are defined with "async def"
202 syntax and have "yield" expressions in their body.
203 """
Jeroen Demeyerfcef60f2019-04-02 16:03:42 +0200204 return _has_code_flag(obj, CO_ASYNC_GENERATOR)
Yury Selivanoveb636452016-09-08 22:01:51 -0700205
206def isasyncgen(object):
Yury Selivanov4778e132016-11-08 12:23:09 -0500207 """Return true if the object is an asynchronous generator."""
Yury Selivanoveb636452016-09-08 22:01:51 -0700208 return isinstance(object, types.AsyncGeneratorType)
209
Christian Heimes7131fd92008-02-19 14:21:46 +0000210def isgenerator(object):
211 """Return true if the object is a generator.
212
213 Generator objects provide these attributes:
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300214 __iter__ defined to support iteration over container
Christian Heimes7131fd92008-02-19 14:21:46 +0000215 close raises a new GeneratorExit exception inside the
216 generator to terminate the iteration
217 gi_code code object
218 gi_frame frame object or possibly None once the generator has
219 been exhausted
220 gi_running set to 1 when generator is executing, 0 otherwise
221 next return the next item from the container
222 send resumes the generator and "sends" a value that becomes
223 the result of the current yield-expression
224 throw used to raise an exception inside the generator"""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400225 return isinstance(object, types.GeneratorType)
Yury Selivanov75445082015-05-11 22:57:16 -0400226
227def iscoroutine(object):
228 """Return true if the object is a coroutine."""
Yury Selivanov5376ba92015-06-22 12:19:30 -0400229 return isinstance(object, types.CoroutineType)
Christian Heimes7131fd92008-02-19 14:21:46 +0000230
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400231def isawaitable(object):
Yury Selivanovc0215df2016-11-08 19:57:44 -0500232 """Return true if object can be passed to an ``await`` expression."""
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400233 return (isinstance(object, types.CoroutineType) or
234 isinstance(object, types.GeneratorType) and
Yury Selivanovc0215df2016-11-08 19:57:44 -0500235 bool(object.gi_code.co_flags & CO_ITERABLE_COROUTINE) or
Yury Selivanovfdbeb2b2015-07-03 13:11:35 -0400236 isinstance(object, collections.abc.Awaitable))
237
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000238def istraceback(object):
239 """Return true if the object is a traceback.
240
241 Traceback objects provide these attributes:
242 tb_frame frame object at this level
243 tb_lasti index of last attempted instruction in bytecode
244 tb_lineno current line number in Python source code
245 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000246 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000247
248def isframe(object):
249 """Return true if the object is a frame object.
250
251 Frame objects provide these attributes:
252 f_back next outer frame object (this frame's caller)
253 f_builtins built-in namespace seen by this frame
254 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000255 f_globals global namespace seen by this frame
256 f_lasti index of last attempted instruction in bytecode
257 f_lineno current line number in Python source code
258 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000259 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000260 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000261
262def iscode(object):
263 """Return true if the object is a code object.
264
265 Code objects provide these attributes:
Xiang Zhanga6902e62017-04-13 10:38:28 +0800266 co_argcount number of arguments (not including *, ** args
267 or keyword only arguments)
268 co_code string of raw compiled bytecode
269 co_cellvars tuple of names of cell variables
270 co_consts tuple of constants used in the bytecode
271 co_filename name of file in which this code object was created
272 co_firstlineno number of first line in Python source code
273 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
274 | 16=nested | 32=generator | 64=nofree | 128=coroutine
275 | 256=iterable_coroutine | 512=async_generator
276 co_freevars tuple of names of free variables
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100277 co_posonlyargcount number of positional only arguments
Xiang Zhanga6902e62017-04-13 10:38:28 +0800278 co_kwonlyargcount number of keyword only arguments (not including ** arg)
279 co_lnotab encoded mapping of line numbers to bytecode indices
280 co_name name with which this code object was defined
281 co_names tuple of names of local variables
282 co_nlocals number of local variables
283 co_stacksize virtual machine stack space required
284 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000285 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000286
287def isbuiltin(object):
288 """Return true if the object is a built-in function or method.
289
290 Built-in functions and methods provide these attributes:
291 __doc__ documentation string
292 __name__ original name of this function or method
293 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000294 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000295
296def isroutine(object):
297 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000298 return (isbuiltin(object)
299 or isfunction(object)
300 or ismethod(object)
301 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000302
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000303def isabstract(object):
304 """Return true if the object is an abstract base class (ABC)."""
Natefcfe80e2017-04-24 10:06:15 -0700305 if not isinstance(object, type):
306 return False
307 if object.__flags__ & TPFLAGS_IS_ABSTRACT:
308 return True
309 if not issubclass(type(object), abc.ABCMeta):
310 return False
311 if hasattr(object, '__abstractmethods__'):
312 # It looks like ABCMeta.__new__ has finished running;
313 # TPFLAGS_IS_ABSTRACT should have been accurate.
314 return False
315 # It looks like ABCMeta.__new__ has not finished running yet; we're
316 # probably in __init_subclass__. We'll look for abstractmethods manually.
317 for name, value in object.__dict__.items():
318 if getattr(value, "__isabstractmethod__", False):
319 return True
320 for base in object.__bases__:
321 for name in getattr(base, "__abstractmethods__", ()):
322 value = getattr(object, name, None)
323 if getattr(value, "__isabstractmethod__", False):
324 return True
325 return False
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000326
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000327def getmembers(object, predicate=None):
328 """Return all members of an object as (name, value) pairs sorted by name.
329 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100330 if isclass(object):
331 mro = (object,) + getmro(object)
332 else:
333 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000334 results = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700335 processed = set()
336 names = dir(object)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700337 # :dd any DynamicClassAttributes to the list of names if object is a class;
Ethan Furmane03ea372013-09-25 07:14:41 -0700338 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700339 # attribute with the same name as a DynamicClassAttribute exists
Ethan Furmane03ea372013-09-25 07:14:41 -0700340 try:
341 for base in object.__bases__:
342 for k, v in base.__dict__.items():
343 if isinstance(v, types.DynamicClassAttribute):
344 names.append(k)
345 except AttributeError:
346 pass
347 for key in names:
Ethan Furman63c141c2013-10-18 00:27:39 -0700348 # First try to get the value via getattr. Some descriptors don't
349 # like calling their __get__ (see bug #1785), so fall back to
350 # looking in the __dict__.
351 try:
352 value = getattr(object, key)
353 # handle the duplicate key
354 if key in processed:
355 raise AttributeError
356 except AttributeError:
357 for base in mro:
358 if key in base.__dict__:
359 value = base.__dict__[key]
360 break
361 else:
362 # could be a (currently) missing slot member, or a buggy
363 # __dir__; discard and move on
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100364 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000365 if not predicate or predicate(value):
366 results.append((key, value))
Ethan Furmane03ea372013-09-25 07:14:41 -0700367 processed.add(key)
368 results.sort(key=lambda pair: pair[0])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000369 return results
370
Christian Heimes25bb7832008-01-11 16:17:00 +0000371Attribute = namedtuple('Attribute', 'name kind defining_class object')
372
Tim Peters13b49d32001-09-23 02:00:29 +0000373def classify_class_attrs(cls):
374 """Return list of attribute-descriptor tuples.
375
376 For each name in dir(cls), the return list contains a 4-tuple
377 with these elements:
378
379 0. The name (a string).
380
381 1. The kind of attribute this is, one of these strings:
382 'class method' created via classmethod()
383 'static method' created via staticmethod()
384 'property' created via property()
Ethan Furmane03ea372013-09-25 07:14:41 -0700385 'method' any other flavor of method or descriptor
Tim Peters13b49d32001-09-23 02:00:29 +0000386 'data' not a method
387
388 2. The class which defined this attribute (a class).
389
Ethan Furmane03ea372013-09-25 07:14:41 -0700390 3. The object as obtained by calling getattr; if this fails, or if the
391 resulting object does not live anywhere in the class' mro (including
392 metaclasses) then the object is looked up in the defining class's
393 dict (found by walking the mro).
Ethan Furman668dede2013-09-14 18:53:26 -0700394
395 If one of the items in dir(cls) is stored in the metaclass it will now
396 be discovered and not have None be listed as the class in which it was
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700397 defined. Any items whose home class cannot be discovered are skipped.
Tim Peters13b49d32001-09-23 02:00:29 +0000398 """
399
400 mro = getmro(cls)
Ethan Furman668dede2013-09-14 18:53:26 -0700401 metamro = getmro(type(cls)) # for attributes stored in the metaclass
Jon Dufresne39726282017-05-18 07:35:54 -0700402 metamro = tuple(cls for cls in metamro if cls not in (type, object))
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700403 class_bases = (cls,) + mro
404 all_bases = class_bases + metamro
Tim Peters13b49d32001-09-23 02:00:29 +0000405 names = dir(cls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700406 # :dd any DynamicClassAttributes to the list of names;
Ethan Furmane03ea372013-09-25 07:14:41 -0700407 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700408 # attribute with the same name as a DynamicClassAttribute exists.
Ethan Furman63c141c2013-10-18 00:27:39 -0700409 for base in mro:
Ethan Furmane03ea372013-09-25 07:14:41 -0700410 for k, v in base.__dict__.items():
411 if isinstance(v, types.DynamicClassAttribute):
412 names.append(k)
Tim Peters13b49d32001-09-23 02:00:29 +0000413 result = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700414 processed = set()
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700415
Tim Peters13b49d32001-09-23 02:00:29 +0000416 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100417 # Get the object associated with the name, and where it was defined.
Ethan Furmane03ea372013-09-25 07:14:41 -0700418 # Normal objects will be looked up with both getattr and directly in
419 # its class' dict (in case getattr fails [bug #1785], and also to look
420 # for a docstring).
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700421 # For DynamicClassAttributes on the second pass we only look in the
Ethan Furmane03ea372013-09-25 07:14:41 -0700422 # class's dict.
423 #
Tim Peters13b49d32001-09-23 02:00:29 +0000424 # Getting an obj from the __dict__ sometimes reveals more than
425 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100426 homecls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700427 get_obj = None
428 dict_obj = None
Ethan Furmane03ea372013-09-25 07:14:41 -0700429 if name not in processed:
430 try:
Ethan Furmana8b07072013-10-18 01:22:08 -0700431 if name == '__dict__':
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700432 raise Exception("__dict__ is special, don't want the proxy")
Ethan Furmane03ea372013-09-25 07:14:41 -0700433 get_obj = getattr(cls, name)
434 except Exception as exc:
435 pass
436 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700437 homecls = getattr(get_obj, "__objclass__", homecls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700438 if homecls not in class_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700439 # if the resulting object does not live somewhere in the
Ethan Furman63c141c2013-10-18 00:27:39 -0700440 # mro, drop it and search the mro manually
Ethan Furmane03ea372013-09-25 07:14:41 -0700441 homecls = None
Ethan Furman63c141c2013-10-18 00:27:39 -0700442 last_cls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700443 # first look in the classes
444 for srch_cls in class_bases:
Ethan Furman63c141c2013-10-18 00:27:39 -0700445 srch_obj = getattr(srch_cls, name, None)
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400446 if srch_obj is get_obj:
Ethan Furman63c141c2013-10-18 00:27:39 -0700447 last_cls = srch_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700448 # then check the metaclasses
449 for srch_cls in metamro:
450 try:
451 srch_obj = srch_cls.__getattr__(cls, name)
452 except AttributeError:
453 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400454 if srch_obj is get_obj:
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700455 last_cls = srch_cls
Ethan Furman63c141c2013-10-18 00:27:39 -0700456 if last_cls is not None:
457 homecls = last_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700458 for base in all_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700459 if name in base.__dict__:
460 dict_obj = base.__dict__[name]
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700461 if homecls not in metamro:
462 homecls = base
Ethan Furmane03ea372013-09-25 07:14:41 -0700463 break
Ethan Furman63c141c2013-10-18 00:27:39 -0700464 if homecls is None:
465 # unable to locate the attribute anywhere, most likely due to
466 # buggy custom __dir__; discard and move on
467 continue
Yury Selivanovbf341fb2015-05-21 15:41:57 -0400468 obj = get_obj if get_obj is not None else dict_obj
Ethan Furmane03ea372013-09-25 07:14:41 -0700469 # Classify the object or its descriptor.
Serhiy Storchaka3327a2d2017-12-15 14:13:41 +0200470 if isinstance(dict_obj, (staticmethod, types.BuiltinMethodType)):
Tim Peters13b49d32001-09-23 02:00:29 +0000471 kind = "static method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700472 obj = dict_obj
Serhiy Storchaka3327a2d2017-12-15 14:13:41 +0200473 elif isinstance(dict_obj, (classmethod, types.ClassMethodDescriptorType)):
Tim Peters13b49d32001-09-23 02:00:29 +0000474 kind = "class method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700475 obj = dict_obj
476 elif isinstance(dict_obj, property):
Tim Peters13b49d32001-09-23 02:00:29 +0000477 kind = "property"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700478 obj = dict_obj
Yury Selivanov0860a0b2014-01-31 14:28:44 -0500479 elif isroutine(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000480 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100481 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700482 kind = "data"
Christian Heimes25bb7832008-01-11 16:17:00 +0000483 result.append(Attribute(name, kind, homecls, obj))
Ethan Furmane03ea372013-09-25 07:14:41 -0700484 processed.add(name)
Tim Peters13b49d32001-09-23 02:00:29 +0000485 return result
486
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000487# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000488
489def getmro(cls):
490 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000491 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000492
Nick Coghlane8c45d62013-07-28 20:00:01 +1000493# -------------------------------------------------------- function helpers
494
495def unwrap(func, *, stop=None):
496 """Get the object wrapped by *func*.
497
498 Follows the chain of :attr:`__wrapped__` attributes returning the last
499 object in the chain.
500
501 *stop* is an optional callback accepting an object in the wrapper chain
502 as its sole argument that allows the unwrapping to be terminated early if
503 the callback returns a true value. If the callback never returns a true
504 value, the last object in the chain is returned as usual. For example,
505 :func:`signature` uses this to stop unwrapping if any object in the
506 chain has a ``__signature__`` attribute defined.
507
508 :exc:`ValueError` is raised if a cycle is encountered.
509
510 """
511 if stop is None:
512 def _is_wrapper(f):
513 return hasattr(f, '__wrapped__')
514 else:
515 def _is_wrapper(f):
516 return hasattr(f, '__wrapped__') and not stop(f)
517 f = func # remember the original func for error reporting
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100518 # Memoise by id to tolerate non-hashable objects, but store objects to
519 # ensure they aren't destroyed, which would allow their IDs to be reused.
520 memo = {id(f): f}
521 recursion_limit = sys.getrecursionlimit()
Nick Coghlane8c45d62013-07-28 20:00:01 +1000522 while _is_wrapper(func):
523 func = func.__wrapped__
524 id_func = id(func)
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100525 if (id_func in memo) or (len(memo) >= recursion_limit):
Nick Coghlane8c45d62013-07-28 20:00:01 +1000526 raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
Thomas Kluyverf9169ce2017-05-23 04:27:52 +0100527 memo[id_func] = func
Nick Coghlane8c45d62013-07-28 20:00:01 +1000528 return func
529
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000530# -------------------------------------------------- source code extraction
531def indentsize(line):
532 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000533 expline = line.expandtabs()
534 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000535
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300536def _findclass(func):
537 cls = sys.modules.get(func.__module__)
538 if cls is None:
539 return None
540 for name in func.__qualname__.split('.')[:-1]:
541 cls = getattr(cls, name)
542 if not isclass(cls):
543 return None
544 return cls
545
546def _finddoc(obj):
Serhiy Storchaka08b47c32020-05-18 20:25:07 +0300547 if isclass(obj):
548 for base in obj.__mro__:
549 if base is not object:
550 try:
551 doc = base.__doc__
552 except AttributeError:
553 continue
554 if doc is not None:
555 return doc
556 return None
557
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300558 if ismethod(obj):
559 name = obj.__func__.__name__
560 self = obj.__self__
561 if (isclass(self) and
562 getattr(getattr(self, name, None), '__func__') is obj.__func__):
563 # classmethod
564 cls = self
565 else:
566 cls = self.__class__
567 elif isfunction(obj):
568 name = obj.__name__
569 cls = _findclass(obj)
570 if cls is None or getattr(cls, name) is not obj:
571 return None
572 elif isbuiltin(obj):
573 name = obj.__name__
574 self = obj.__self__
575 if (isclass(self) and
576 self.__qualname__ + '.' + name == obj.__qualname__):
577 # classmethod
578 cls = self
579 else:
580 cls = self.__class__
Serhiy Storchakaac4bdcc2015-10-29 08:15:50 +0200581 # Should be tested before isdatadescriptor().
582 elif isinstance(obj, property):
583 func = obj.fget
584 name = func.__name__
585 cls = _findclass(func)
586 if cls is None or getattr(cls, name) is not obj:
587 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300588 elif ismethoddescriptor(obj) or isdatadescriptor(obj):
589 name = obj.__name__
590 cls = obj.__objclass__
591 if getattr(cls, name) is not obj:
592 return None
Raymond Hettingerd1e768a2019-03-25 13:01:13 -0700593 if ismemberdescriptor(obj):
594 slots = getattr(cls, '__slots__', None)
595 if isinstance(slots, dict) and name in slots:
596 return slots[name]
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300597 else:
598 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300599 for base in cls.__mro__:
600 try:
Serhiy Storchaka08b47c32020-05-18 20:25:07 +0300601 doc = getattr(base, name).__doc__
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300602 except AttributeError:
603 continue
604 if doc is not None:
605 return doc
606 return None
607
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000608def getdoc(object):
609 """Get the documentation string for an object.
610
611 All tabs are expanded to spaces. To clean up docstrings that are
612 indented to line up with blocks of code, any whitespace than can be
613 uniformly removed from the second line onwards is removed."""
Serhiy Storchaka08b47c32020-05-18 20:25:07 +0300614 try:
615 doc = object.__doc__
616 except AttributeError:
617 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300618 if doc is None:
619 try:
620 doc = _finddoc(object)
621 except (AttributeError, TypeError):
622 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000623 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000624 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000625 return cleandoc(doc)
626
627def cleandoc(doc):
628 """Clean up indentation from docstrings.
629
630 Any whitespace that can be uniformly removed from the second line
631 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000632 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000633 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000634 except UnicodeError:
635 return None
636 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000637 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000638 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000639 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000640 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000641 if content:
642 indent = len(line) - content
643 margin = min(margin, indent)
644 # Remove indentation.
645 if lines:
646 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000647 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000648 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000649 # Remove any trailing or leading blank lines.
650 while lines and not lines[-1]:
651 lines.pop()
652 while lines and not lines[0]:
653 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000654 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000655
656def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000657 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000658 if ismodule(object):
Jason R. Coombsb9650a02018-03-05 18:29:08 -0500659 if getattr(object, '__file__', None):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000660 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000661 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000662 if isclass(object):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500663 if hasattr(object, '__module__'):
Philipp Ad407d2a2019-06-08 14:05:46 +0200664 module = sys.modules.get(object.__module__)
665 if getattr(module, '__file__', None):
666 return module.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000667 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000668 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000669 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000670 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000671 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000672 if istraceback(object):
673 object = object.tb_frame
674 if isframe(object):
675 object = object.f_code
676 if iscode(object):
677 return object.co_filename
Thomas Kluyvere968bc732017-10-24 13:42:36 +0100678 raise TypeError('module, class, method, function, traceback, frame, or '
679 'code object was expected, got {}'.format(
680 type(object).__name__))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000681
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000682def getmodulename(path):
683 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000684 fname = os.path.basename(path)
685 # Check for paths that look like an actual module file
686 suffixes = [(-len(suffix), suffix)
687 for suffix in importlib.machinery.all_suffixes()]
688 suffixes.sort() # try longest suffixes first, in case they overlap
689 for neglen, suffix in suffixes:
690 if fname.endswith(suffix):
691 return fname[:neglen]
692 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000693
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000694def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000695 """Return the filename that can be used to locate an object's source.
696 Return None if no way can be identified to get the source.
697 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000698 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400699 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
700 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
701 if any(filename.endswith(s) for s in all_bytecode_suffixes):
702 filename = (os.path.splitext(filename)[0] +
703 importlib.machinery.SOURCE_SUFFIXES[0])
704 elif any(filename.endswith(s) for s in
705 importlib.machinery.EXTENSION_SUFFIXES):
706 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000707 if os.path.exists(filename):
708 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000709 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon825ac382020-11-06 18:45:56 -0800710 module = getmodule(object, filename)
711 if getattr(module, '__loader__', None) is not None:
712 return filename
713 elif getattr(getattr(module, "__spec__", None), "loader", None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000714 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000715 # or it is in the linecache
Brett Cannon825ac382020-11-06 18:45:56 -0800716 elif filename in linecache.cache:
R. David Murraya1b37402010-06-17 02:04:29 +0000717 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000718
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000719def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000720 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000721
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000722 The idea is for each object to have a unique origin, so this routine
723 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000724 if _filename is None:
725 _filename = getsourcefile(object) or getfile(object)
726 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000727
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000728modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000729_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000730
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000731def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000732 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000733 if ismodule(object):
734 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000735 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000736 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000737 # Try the filename to modulename cache
738 if _filename is not None and _filename in modulesbyfile:
739 return sys.modules.get(modulesbyfile[_filename])
740 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000741 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000742 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000743 except TypeError:
744 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000745 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000746 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000747 # Update the filename to module name cache and check yet again
748 # Copy sys.modules in order to cope with changes while iterating
Gregory P. Smith85cf1d52020-03-04 16:45:22 -0800749 for modname, module in sys.modules.copy().items():
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000750 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000751 f = module.__file__
752 if f == _filesbymodname.get(modname, None):
753 # Have already mapped this module, so skip it
754 continue
755 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000756 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000757 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000758 modulesbyfile[f] = modulesbyfile[
759 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000760 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000761 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000762 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000763 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000764 if not hasattr(object, '__name__'):
765 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000766 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000767 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000768 if mainobject is object:
769 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000770 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000771 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000772 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000773 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000774 if builtinobject is object:
775 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000776
Karthikeyan Singaravelan696136b2020-04-18 21:49:32 +0530777
778class ClassFoundException(Exception):
779 pass
780
781
782class _ClassFinder(ast.NodeVisitor):
783
784 def __init__(self, qualname):
785 self.stack = []
786 self.qualname = qualname
787
788 def visit_FunctionDef(self, node):
789 self.stack.append(node.name)
790 self.stack.append('<locals>')
791 self.generic_visit(node)
792 self.stack.pop()
793 self.stack.pop()
794
795 visit_AsyncFunctionDef = visit_FunctionDef
796
797 def visit_ClassDef(self, node):
798 self.stack.append(node.name)
799 if self.qualname == '.'.join(self.stack):
800 # Return the decorator for the class if present
801 if node.decorator_list:
802 line_number = node.decorator_list[0].lineno
803 else:
804 line_number = node.lineno
805
806 # decrement by one since lines starts with indexing by zero
807 line_number -= 1
808 raise ClassFoundException(line_number)
809 self.generic_visit(node)
810 self.stack.pop()
811
812
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000813def findsource(object):
814 """Return the entire source file and starting line number for an object.
815
816 The argument may be a module, class, method, function, traceback, frame,
817 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200818 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000819 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500820
Yury Selivanovef1e7502014-12-08 16:05:34 -0500821 file = getsourcefile(object)
822 if file:
823 # Invalidate cache if needed.
824 linecache.checkcache(file)
825 else:
826 file = getfile(object)
827 # Allow filenames in form of "<something>" to pass through.
828 # `doctest` monkeypatches `linecache` module to enable
829 # inspection, so let `linecache.getlines` to be called.
830 if not (file.startswith('<') and file.endswith('>')):
831 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500832
Thomas Wouters89f507f2006-12-13 04:49:30 +0000833 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000834 if module:
835 lines = linecache.getlines(file, module.__dict__)
836 else:
837 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000838 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200839 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000840
841 if ismodule(object):
842 return lines, 0
843
844 if isclass(object):
Karthikeyan Singaravelan696136b2020-04-18 21:49:32 +0530845 qualname = object.__qualname__
846 source = ''.join(lines)
847 tree = ast.parse(source)
848 class_finder = _ClassFinder(qualname)
849 try:
850 class_finder.visit(tree)
851 except ClassFoundException as e:
852 line_number = e.args[0]
853 return lines, line_number
Jeremy Hyltonab919022003-06-27 18:41:20 +0000854 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200855 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000856
857 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000858 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000859 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000860 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000861 if istraceback(object):
862 object = object.tb_frame
863 if isframe(object):
864 object = object.f_code
865 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000866 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200867 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000868 lnum = object.co_firstlineno - 1
Yury Selivanove4e811d2015-07-21 19:01:52 +0300869 pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000870 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000871 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000872 lnum = lnum - 1
873 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200874 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000875
876def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000877 """Get lines of comments immediately preceding an object's source code.
878
879 Returns None when source can't be found.
880 """
881 try:
882 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200883 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000884 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000885
886 if ismodule(object):
887 # Look for a comment block at the top of the file.
888 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000889 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000890 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000891 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000892 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000893 comments = []
894 end = start
895 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000896 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000897 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000898 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000899
900 # Look for a preceding block of comments at the same indentation.
901 elif lnum > 0:
902 indent = indentsize(lines[lnum])
903 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000904 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000905 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000906 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000907 if end > 0:
908 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000909 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000910 while comment[:1] == '#' and indentsize(lines[end]) == indent:
911 comments[:0] = [comment]
912 end = end - 1
913 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000914 comment = lines[end].expandtabs().lstrip()
915 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000916 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000917 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000918 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000919 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000920
Tim Peters4efb6e92001-06-29 23:51:08 +0000921class EndOfBlock(Exception): pass
922
923class BlockFinder:
924 """Provide a tokeneater() method to detect the end of a code block."""
925 def __init__(self):
926 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000927 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000928 self.started = False
929 self.passline = False
Meador Inge5b718d72015-07-23 22:49:37 -0500930 self.indecorator = False
931 self.decoratorhasargs = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000932 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000933
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000934 def tokeneater(self, type, token, srowcol, erowcol, line):
Meador Inge5b718d72015-07-23 22:49:37 -0500935 if not self.started and not self.indecorator:
936 # skip any decorators
937 if token == "@":
938 self.indecorator = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000939 # look for the first "def", "class" or "lambda"
Meador Inge5b718d72015-07-23 22:49:37 -0500940 elif token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000941 if token == "lambda":
942 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000943 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000944 self.passline = True # skip to the end of the line
Meador Inge5b718d72015-07-23 22:49:37 -0500945 elif token == "(":
946 if self.indecorator:
947 self.decoratorhasargs = True
948 elif token == ")":
949 if self.indecorator:
950 self.indecorator = False
951 self.decoratorhasargs = False
Tim Peters4efb6e92001-06-29 23:51:08 +0000952 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000953 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000954 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000955 if self.islambda: # lambdas always end at the first NEWLINE
956 raise EndOfBlock
Meador Inge5b718d72015-07-23 22:49:37 -0500957 # hitting a NEWLINE when in a decorator without args
958 # ends the decorator
959 if self.indecorator and not self.decoratorhasargs:
960 self.indecorator = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000961 elif self.passline:
962 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000963 elif type == tokenize.INDENT:
964 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000965 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000966 elif type == tokenize.DEDENT:
967 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000968 # the end of matching indent/dedent pairs end a block
969 # (note that this only works for "def"/"class" blocks,
970 # not e.g. for "if: else:" or "try: finally:" blocks)
971 if self.indent <= 0:
972 raise EndOfBlock
973 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
974 # any other token on the same indentation level end the previous
975 # block as well, except the pseudo-tokens COMMENT and NL.
976 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000977
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000978def getblock(lines):
979 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000980 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000981 try:
Trent Nelson428de652008-03-18 22:41:35 +0000982 tokens = tokenize.generate_tokens(iter(lines).__next__)
983 for _token in tokens:
984 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000985 except (EndOfBlock, IndentationError):
986 pass
987 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000988
989def getsourcelines(object):
990 """Return a list of source lines and starting line number for an object.
991
992 The argument may be a module, class, method, function, traceback, frame,
993 or code object. The source code is returned as a list of the lines
994 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200995 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000996 raised if the source code cannot be retrieved."""
Yury Selivanov081bbf62014-09-26 17:34:54 -0400997 object = unwrap(object)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000998 lines, lnum = findsource(object)
999
Vladimir Matveev91cb2982018-08-24 07:18:00 -07001000 if istraceback(object):
1001 object = object.tb_frame
1002
1003 # for module or frame that corresponds to module, return all source lines
1004 if (ismodule(object) or
1005 (isframe(object) and object.f_code.co_name == "<module>")):
Meador Inge5b718d72015-07-23 22:49:37 -05001006 return lines, 0
1007 else:
1008 return getblock(lines[lnum:]), lnum + 1
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001009
1010def getsource(object):
1011 """Return the text of the source code for an object.
1012
1013 The argument may be a module, class, method, function, traceback, frame,
1014 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001015 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001016 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001017 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001018
1019# --------------------------------------------------- class tree extraction
1020def walktree(classes, children, parent):
1021 """Recursive helper function for getclasstree()."""
1022 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +00001023 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001024 for c in classes:
1025 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +00001026 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001027 results.append(walktree(children[c], children, c))
1028 return results
1029
Georg Brandl5ce83a02009-06-01 17:23:51 +00001030def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001031 """Arrange the given list of classes into a hierarchy of nested lists.
1032
1033 Where a nested list appears, it contains classes derived from the class
1034 whose entry immediately precedes the list. Each entry is a 2-tuple
1035 containing a class and a tuple of its base classes. If the 'unique'
1036 argument is true, exactly one entry appears in the returned structure
1037 for each class in the given list. Otherwise, classes using multiple
1038 inheritance and their descendants will appear multiple times."""
1039 children = {}
1040 roots = []
1041 for c in classes:
1042 if c.__bases__:
1043 for parent in c.__bases__:
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05301044 if parent not in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001045 children[parent] = []
Serhiy Storchaka362c1b52013-09-05 17:14:32 +03001046 if c not in children[parent]:
1047 children[parent].append(c)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001048 if unique and parent in classes: break
1049 elif c not in roots:
1050 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +00001051 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001052 if parent not in classes:
1053 roots.append(parent)
1054 return walktree(roots, children, None)
1055
1056# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001057Arguments = namedtuple('Arguments', 'args, varargs, varkw')
1058
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001059def getargs(co):
1060 """Get information about the arguments accepted by a code object.
1061
Guido van Rossum2e65f892007-02-28 22:03:49 +00001062 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001063 'args' is the list of argument names. Keyword-only arguments are
1064 appended. 'varargs' and 'varkw' are the names of the * and **
1065 arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +00001066 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001067 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001068
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001069 names = co.co_varnames
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001070 nargs = co.co_argcount
Guido van Rossum2e65f892007-02-28 22:03:49 +00001071 nkwargs = co.co_kwonlyargcount
Pablo Galindocd74e662019-06-01 18:08:04 +01001072 args = list(names[:nargs])
1073 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001074 step = 0
1075
Guido van Rossum2e65f892007-02-28 22:03:49 +00001076 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001077 varargs = None
1078 if co.co_flags & CO_VARARGS:
1079 varargs = co.co_varnames[nargs]
1080 nargs = nargs + 1
1081 varkw = None
1082 if co.co_flags & CO_VARKEYWORDS:
1083 varkw = co.co_varnames[nargs]
Pablo Galindocd74e662019-06-01 18:08:04 +01001084 return Arguments(args + kwonlyargs, varargs, varkw)
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001085
1086ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
1087
1088def getargspec(func):
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001089 """Get the names and default values of a function's parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001090
1091 A tuple of four things is returned: (args, varargs, keywords, defaults).
1092 'args' is a list of the argument names, including keyword-only argument names.
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001093 'varargs' and 'keywords' are the names of the * and ** parameters or None.
1094 'defaults' is an n-tuple of the default values of the last n parameters.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001095
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001096 This function is deprecated, as it does not support annotations or
1097 keyword-only parameters and will raise ValueError if either is present
1098 on the supplied callable.
1099
1100 For a more structured introspection API, use inspect.signature() instead.
1101
1102 Alternatively, use getfullargspec() for an API with a similar namedtuple
1103 based interface, but full support for annotations and keyword-only
1104 parameters.
Matthias Bussonnierded87d82018-10-19 16:40:45 -07001105
1106 Deprecated since Python 3.5, use `inspect.getfullargspec()`.
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001107 """
Matthias Bussonnierded87d82018-10-19 16:40:45 -07001108 warnings.warn("inspect.getargspec() is deprecated since Python 3.0, "
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001109 "use inspect.signature() or inspect.getfullargspec()",
1110 DeprecationWarning, stacklevel=2)
Pablo Galindod5d2b452019-04-30 02:01:14 +01001111 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
1112 getfullargspec(func)
1113 if kwonlyargs or ann:
1114 raise ValueError("Function has keyword-only parameters or annotations"
1115 ", use inspect.signature() API which can support them")
Yury Selivanov37dc2b22016-01-11 15:15:01 -05001116 return ArgSpec(args, varargs, varkw, defaults)
1117
Christian Heimes25bb7832008-01-11 16:17:00 +00001118FullArgSpec = namedtuple('FullArgSpec',
Pablo Galindod5d2b452019-04-30 02:01:14 +01001119 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001120
1121def getfullargspec(func):
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001122 """Get the names and default values of a callable object's parameters.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001123
Brett Cannon504d8852007-09-07 02:12:14 +00001124 A tuple of seven things is returned:
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001125 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations).
1126 'args' is a list of the parameter names.
1127 'varargs' and 'varkw' are the names of the * and ** parameters or None.
1128 'defaults' is an n-tuple of the default values of the last n parameters.
1129 'kwonlyargs' is a list of keyword-only parameter names.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001130 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001131 'annotations' is a dictionary mapping parameter names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001132
Nick Coghlan3c35fdb2016-12-02 20:29:57 +10001133 Notable differences from inspect.signature():
1134 - the "self" parameter is always reported, even for bound methods
1135 - wrapper chains defined by __wrapped__ *not* unwrapped automatically
Jeremy Hylton64967882003-06-27 18:14:39 +00001136 """
Yury Selivanov57d240e2014-02-19 16:27:23 -05001137 try:
1138 # Re: `skip_bound_arg=False`
1139 #
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001140 # There is a notable difference in behaviour between getfullargspec
1141 # and Signature: the former always returns 'self' parameter for bound
1142 # methods, whereas the Signature always shows the actual calling
1143 # signature of the passed object.
1144 #
1145 # To simulate this behaviour, we "unbind" bound methods, to trick
1146 # inspect.signature to always return their first parameter ("self",
1147 # usually)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001148
Yury Selivanov57d240e2014-02-19 16:27:23 -05001149 # Re: `follow_wrapper_chains=False`
1150 #
1151 # getfullargspec() historically ignored __wrapped__ attributes,
1152 # so we ensure that remains the case in 3.3+
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001153
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001154 sig = _signature_from_callable(func,
1155 follow_wrapper_chains=False,
1156 skip_bound_arg=False,
1157 sigcls=Signature)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001158 except Exception as ex:
1159 # Most of the times 'signature' will raise ValueError.
1160 # But, it can also raise AttributeError, and, maybe something
1161 # else. So to be fully backwards compatible, we catch all
1162 # possible exceptions here, and reraise a TypeError.
1163 raise TypeError('unsupported callable') from ex
1164
1165 args = []
1166 varargs = None
1167 varkw = None
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001168 posonlyargs = []
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001169 kwonlyargs = []
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001170 annotations = {}
1171 defaults = ()
1172 kwdefaults = {}
1173
1174 if sig.return_annotation is not sig.empty:
1175 annotations['return'] = sig.return_annotation
1176
1177 for param in sig.parameters.values():
1178 kind = param.kind
1179 name = param.name
1180
1181 if kind is _POSITIONAL_ONLY:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001182 posonlyargs.append(name)
1183 if param.default is not param.empty:
1184 defaults += (param.default,)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001185 elif kind is _POSITIONAL_OR_KEYWORD:
1186 args.append(name)
1187 if param.default is not param.empty:
1188 defaults += (param.default,)
1189 elif kind is _VAR_POSITIONAL:
1190 varargs = name
1191 elif kind is _KEYWORD_ONLY:
1192 kwonlyargs.append(name)
1193 if param.default is not param.empty:
1194 kwdefaults[name] = param.default
1195 elif kind is _VAR_KEYWORD:
1196 varkw = name
1197
1198 if param.annotation is not param.empty:
1199 annotations[name] = param.annotation
1200
1201 if not kwdefaults:
1202 # compatibility with 'func.__kwdefaults__'
1203 kwdefaults = None
1204
1205 if not defaults:
1206 # compatibility with 'func.__defaults__'
1207 defaults = None
1208
Pablo Galindod5d2b452019-04-30 02:01:14 +01001209 return FullArgSpec(posonlyargs + args, varargs, varkw, defaults,
1210 kwonlyargs, kwdefaults, annotations)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001211
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001212
Christian Heimes25bb7832008-01-11 16:17:00 +00001213ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
1214
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001215def getargvalues(frame):
1216 """Get information about arguments passed into a particular frame.
1217
1218 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001219 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001220 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1221 'locals' is the locals dictionary of the given frame."""
1222 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +00001223 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001224
Guido van Rossum2e65f892007-02-28 22:03:49 +00001225def formatannotation(annotation, base_module=None):
Guido van Rossum52e50042016-10-22 07:55:18 -07001226 if getattr(annotation, '__module__', None) == 'typing':
1227 return repr(annotation).replace('typing.', '')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001228 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001229 if annotation.__module__ in ('builtins', base_module):
Serhiy Storchaka521e5862014-07-22 15:00:37 +03001230 return annotation.__qualname__
1231 return annotation.__module__+'.'+annotation.__qualname__
Guido van Rossum2e65f892007-02-28 22:03:49 +00001232 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001233
Guido van Rossum2e65f892007-02-28 22:03:49 +00001234def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001235 module = getattr(object, '__module__', None)
1236 def _formatannotation(annotation):
1237 return formatannotation(annotation, module)
1238 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001239
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001240def formatargspec(args, varargs=None, varkw=None, defaults=None,
Pablo Galindod5d2b452019-04-30 02:01:14 +01001241 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001242 formatarg=str,
1243 formatvarargs=lambda name: '*' + name,
1244 formatvarkw=lambda name: '**' + name,
1245 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001246 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001247 formatannotation=formatannotation):
Berker Peksagfa3922c2015-07-31 04:11:29 +03001248 """Format an argument spec from the values returned by getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001249
Guido van Rossum2e65f892007-02-28 22:03:49 +00001250 The first seven arguments are (args, varargs, varkw, defaults,
1251 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1252 are the corresponding optional formatting functions that are called to
1253 turn names and values into strings. The last argument is an optional
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +02001254 function to format the sequence of arguments.
1255
1256 Deprecated since Python 3.5: use the `signature` function and `Signature`
1257 objects.
1258 """
1259
1260 from warnings import warn
1261
1262 warn("`formatargspec` is deprecated since Python 3.5. Use `signature` and "
Zackery Spytz41254eb2018-06-11 21:16:18 -06001263 "the `Signature` object directly",
Matthias Bussonnier46c5cd02018-06-11 22:08:16 +02001264 DeprecationWarning,
1265 stacklevel=2)
1266
Guido van Rossum2e65f892007-02-28 22:03:49 +00001267 def formatargandannotation(arg):
1268 result = formatarg(arg)
1269 if arg in annotations:
1270 result += ': ' + formatannotation(annotations[arg])
1271 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001272 specs = []
1273 if defaults:
Pablo Galindod5d2b452019-04-30 02:01:14 +01001274 firstdefault = len(args) - len(defaults)
1275 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001276 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001277 if defaults and i >= firstdefault:
1278 spec = spec + formatvalue(defaults[i - firstdefault])
1279 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001280 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001281 specs.append(formatvarargs(formatargandannotation(varargs)))
1282 else:
1283 if kwonlyargs:
1284 specs.append('*')
1285 if kwonlyargs:
1286 for kwonlyarg in kwonlyargs:
1287 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001288 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001289 spec += formatvalue(kwonlydefaults[kwonlyarg])
1290 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001291 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001292 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001293 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001294 if 'return' in annotations:
1295 result += formatreturns(formatannotation(annotations['return']))
1296 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001297
1298def formatargvalues(args, varargs, varkw, locals,
1299 formatarg=str,
1300 formatvarargs=lambda name: '*' + name,
1301 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001302 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001303 """Format an argument spec from the 4 values returned by getargvalues.
1304
1305 The first four arguments are (args, varargs, varkw, locals). The
1306 next four arguments are the corresponding optional formatting functions
1307 that are called to turn names and values into strings. The ninth
1308 argument is an optional function to format the sequence of arguments."""
1309 def convert(name, locals=locals,
1310 formatarg=formatarg, formatvalue=formatvalue):
1311 return formatarg(name) + formatvalue(locals[name])
1312 specs = []
1313 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001314 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001315 if varargs:
1316 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1317 if varkw:
1318 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001319 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001320
Benjamin Petersone109c702011-06-24 09:37:26 -05001321def _missing_arguments(f_name, argnames, pos, values):
1322 names = [repr(name) for name in argnames if name not in values]
1323 missing = len(names)
1324 if missing == 1:
1325 s = names[0]
1326 elif missing == 2:
1327 s = "{} and {}".format(*names)
1328 else:
Yury Selivanovdccfa132014-03-27 18:42:52 -04001329 tail = ", {} and {}".format(*names[-2:])
Benjamin Petersone109c702011-06-24 09:37:26 -05001330 del names[-2:]
1331 s = ", ".join(names) + tail
1332 raise TypeError("%s() missing %i required %s argument%s: %s" %
1333 (f_name, missing,
1334 "positional" if pos else "keyword-only",
1335 "" if missing == 1 else "s", s))
1336
1337def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001338 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001339 kwonly_given = len([arg for arg in kwonly if arg in values])
1340 if varargs:
1341 plural = atleast != 1
1342 sig = "at least %d" % (atleast,)
1343 elif defcount:
1344 plural = True
1345 sig = "from %d to %d" % (atleast, len(args))
1346 else:
1347 plural = len(args) != 1
1348 sig = str(len(args))
1349 kwonly_sig = ""
1350 if kwonly_given:
1351 msg = " positional argument%s (and %d keyword-only argument%s)"
1352 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1353 "s" if kwonly_given != 1 else ""))
1354 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1355 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1356 "was" if given == 1 and not kwonly_given else "were"))
1357
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03001358def getcallargs(func, /, *positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001359 """Get the mapping of arguments to values.
1360
1361 A dict is returned, with keys the function argument names (including the
1362 names of the * and ** arguments, if any), and values the respective bound
1363 values from 'positional' and 'named'."""
1364 spec = getfullargspec(func)
Pablo Galindod5d2b452019-04-30 02:01:14 +01001365 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001366 f_name = func.__name__
1367 arg2value = {}
1368
Benjamin Petersonb204a422011-06-05 22:04:07 -05001369
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001370 if ismethod(func) and func.__self__ is not None:
1371 # implicit 'self' (or 'cls' for classmethods) argument
1372 positional = (func.__self__,) + positional
1373 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001374 num_args = len(args)
1375 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001376
Benjamin Petersonb204a422011-06-05 22:04:07 -05001377 n = min(num_pos, num_args)
1378 for i in range(n):
Pablo Galindod5d2b452019-04-30 02:01:14 +01001379 arg2value[args[i]] = positional[i]
Benjamin Petersonb204a422011-06-05 22:04:07 -05001380 if varargs:
1381 arg2value[varargs] = tuple(positional[n:])
1382 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001383 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001384 arg2value[varkw] = {}
1385 for kw, value in named.items():
1386 if kw not in possible_kwargs:
1387 if not varkw:
1388 raise TypeError("%s() got an unexpected keyword argument %r" %
1389 (f_name, kw))
1390 arg2value[varkw][kw] = value
1391 continue
1392 if kw in arg2value:
1393 raise TypeError("%s() got multiple values for argument %r" %
1394 (f_name, kw))
1395 arg2value[kw] = value
1396 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001397 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1398 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001399 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001400 req = args[:num_args - num_defaults]
1401 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001402 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001403 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001404 for i, arg in enumerate(args[num_args - num_defaults:]):
1405 if arg not in arg2value:
1406 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001407 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001408 for kwarg in kwonlyargs:
1409 if kwarg not in arg2value:
Yury Selivanov875df202014-03-27 18:23:03 -04001410 if kwonlydefaults and kwarg in kwonlydefaults:
Benjamin Petersone109c702011-06-24 09:37:26 -05001411 arg2value[kwarg] = kwonlydefaults[kwarg]
1412 else:
1413 missing += 1
1414 if missing:
1415 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001416 return arg2value
1417
Nick Coghlan2f92e542012-06-23 19:39:55 +10001418ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1419
1420def getclosurevars(func):
1421 """
1422 Get the mapping of free variables to their current values.
1423
Meador Inge8fda3592012-07-19 21:33:21 -05001424 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001425 and builtin references as seen by the body of the function. A final
1426 set of unbound names that could not be resolved is also provided.
1427 """
1428
1429 if ismethod(func):
1430 func = func.__func__
1431
1432 if not isfunction(func):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001433 raise TypeError("{!r} is not a Python function".format(func))
Nick Coghlan2f92e542012-06-23 19:39:55 +10001434
1435 code = func.__code__
1436 # Nonlocal references are named in co_freevars and resolved
1437 # by looking them up in __closure__ by positional index
1438 if func.__closure__ is None:
1439 nonlocal_vars = {}
1440 else:
1441 nonlocal_vars = {
1442 var : cell.cell_contents
1443 for var, cell in zip(code.co_freevars, func.__closure__)
1444 }
1445
1446 # Global and builtin references are named in co_names and resolved
1447 # by looking them up in __globals__ or __builtins__
1448 global_ns = func.__globals__
1449 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1450 if ismodule(builtin_ns):
1451 builtin_ns = builtin_ns.__dict__
1452 global_vars = {}
1453 builtin_vars = {}
1454 unbound_names = set()
1455 for name in code.co_names:
1456 if name in ("None", "True", "False"):
1457 # Because these used to be builtins instead of keywords, they
1458 # may still show up as name references. We ignore them.
1459 continue
1460 try:
1461 global_vars[name] = global_ns[name]
1462 except KeyError:
1463 try:
1464 builtin_vars[name] = builtin_ns[name]
1465 except KeyError:
1466 unbound_names.add(name)
1467
1468 return ClosureVars(nonlocal_vars, global_vars,
1469 builtin_vars, unbound_names)
1470
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001471# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001472
1473Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1474
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001475def getframeinfo(frame, context=1):
1476 """Get information about a frame or traceback object.
1477
1478 A tuple of five things is returned: the filename, the line number of
1479 the current line, the function name, a list of lines of context from
1480 the source code, and the index of the current line within that list.
1481 The optional second argument specifies the number of lines of context
1482 to return, which are centered around the current line."""
1483 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001484 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001485 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001486 else:
1487 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001488 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001489 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001490
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001491 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001492 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001493 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001494 try:
1495 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001496 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001497 lines = index = None
1498 else:
Raymond Hettingera0501712004-06-15 11:22:53 +00001499 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001500 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001501 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001502 else:
1503 lines = index = None
1504
Christian Heimes25bb7832008-01-11 16:17:00 +00001505 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001506
1507def getlineno(frame):
1508 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001509 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1510 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001511
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001512FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
1513
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001514def getouterframes(frame, context=1):
1515 """Get a list of records for a frame and all higher (calling) frames.
1516
1517 Each record contains a frame object, filename, line number, function
1518 name, a list of lines of context, and index within the context."""
1519 framelist = []
1520 while frame:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001521 frameinfo = (frame,) + getframeinfo(frame, context)
1522 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001523 frame = frame.f_back
1524 return framelist
1525
1526def getinnerframes(tb, context=1):
1527 """Get a list of records for a traceback's frame and all lower frames.
1528
1529 Each record contains a frame object, filename, line number, function
1530 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001531 framelist = []
1532 while tb:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001533 frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
1534 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001535 tb = tb.tb_next
1536 return framelist
1537
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001538def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001539 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001540 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001541
1542def stack(context=1):
1543 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001544 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001545
1546def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001547 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001548 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001549
1550
1551# ------------------------------------------------ static version of getattr
1552
1553_sentinel = object()
1554
Michael Foorde5162652010-11-20 16:40:44 +00001555def _static_getmro(klass):
1556 return type.__dict__['__mro__'].__get__(klass)
1557
Michael Foord95fc51d2010-11-20 15:07:30 +00001558def _check_instance(obj, attr):
1559 instance_dict = {}
1560 try:
1561 instance_dict = object.__getattribute__(obj, "__dict__")
1562 except AttributeError:
1563 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001564 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001565
1566
1567def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001568 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001569 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001570 try:
1571 return entry.__dict__[attr]
1572 except KeyError:
1573 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001574 return _sentinel
1575
Michael Foord35184ed2010-11-20 16:58:30 +00001576def _is_type(obj):
1577 try:
1578 _static_getmro(obj)
1579 except TypeError:
1580 return False
1581 return True
1582
Michael Foorddcebe0f2011-03-15 19:20:44 -04001583def _shadowed_dict(klass):
1584 dict_attr = type.__dict__["__dict__"]
1585 for entry in _static_getmro(klass):
1586 try:
1587 class_dict = dict_attr.__get__(entry)["__dict__"]
1588 except KeyError:
1589 pass
1590 else:
Inada Naoki8f9cc872019-09-05 13:07:08 +09001591 if not (type(class_dict) is types.GetSetDescriptorType and
Michael Foorddcebe0f2011-03-15 19:20:44 -04001592 class_dict.__name__ == "__dict__" and
1593 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001594 return class_dict
1595 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001596
1597def getattr_static(obj, attr, default=_sentinel):
1598 """Retrieve attributes without triggering dynamic lookup via the
1599 descriptor protocol, __getattr__ or __getattribute__.
1600
1601 Note: this function may not be able to retrieve all attributes
1602 that getattr can fetch (like dynamically created attributes)
1603 and may find attributes that getattr can't (like descriptors
1604 that raise AttributeError). It can also return descriptor objects
1605 instead of instance members in some cases. See the
1606 documentation for details.
1607 """
1608 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001609 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001610 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001611 dict_attr = _shadowed_dict(klass)
1612 if (dict_attr is _sentinel or
Inada Naoki8f9cc872019-09-05 13:07:08 +09001613 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001614 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001615 else:
1616 klass = obj
1617
1618 klass_result = _check_class(klass, attr)
1619
1620 if instance_result is not _sentinel and klass_result is not _sentinel:
1621 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1622 _check_class(type(klass_result), '__set__') is not _sentinel):
1623 return klass_result
1624
1625 if instance_result is not _sentinel:
1626 return instance_result
1627 if klass_result is not _sentinel:
1628 return klass_result
1629
1630 if obj is klass:
1631 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001632 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001633 if _shadowed_dict(type(entry)) is _sentinel:
1634 try:
1635 return entry.__dict__[attr]
1636 except KeyError:
1637 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001638 if default is not _sentinel:
1639 return default
1640 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001641
1642
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001643# ------------------------------------------------ generator introspection
1644
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001645GEN_CREATED = 'GEN_CREATED'
1646GEN_RUNNING = 'GEN_RUNNING'
1647GEN_SUSPENDED = 'GEN_SUSPENDED'
1648GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001649
1650def getgeneratorstate(generator):
1651 """Get current state of a generator-iterator.
1652
1653 Possible states are:
1654 GEN_CREATED: Waiting to start execution.
1655 GEN_RUNNING: Currently being executed by the interpreter.
1656 GEN_SUSPENDED: Currently suspended at a yield expression.
1657 GEN_CLOSED: Execution has completed.
1658 """
1659 if generator.gi_running:
1660 return GEN_RUNNING
1661 if generator.gi_frame is None:
1662 return GEN_CLOSED
1663 if generator.gi_frame.f_lasti == -1:
1664 return GEN_CREATED
1665 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001666
1667
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001668def getgeneratorlocals(generator):
1669 """
1670 Get the mapping of generator local variables to their current values.
1671
1672 A dict is returned, with the keys the local variable names and values the
1673 bound values."""
1674
1675 if not isgenerator(generator):
Serhiy Storchakaa4a30202017-11-28 22:54:42 +02001676 raise TypeError("{!r} is not a Python generator".format(generator))
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001677
1678 frame = getattr(generator, "gi_frame", None)
1679 if frame is not None:
1680 return generator.gi_frame.f_locals
1681 else:
1682 return {}
1683
Yury Selivanov5376ba92015-06-22 12:19:30 -04001684
1685# ------------------------------------------------ coroutine introspection
1686
1687CORO_CREATED = 'CORO_CREATED'
1688CORO_RUNNING = 'CORO_RUNNING'
1689CORO_SUSPENDED = 'CORO_SUSPENDED'
1690CORO_CLOSED = 'CORO_CLOSED'
1691
1692def getcoroutinestate(coroutine):
1693 """Get current state of a coroutine object.
1694
1695 Possible states are:
1696 CORO_CREATED: Waiting to start execution.
1697 CORO_RUNNING: Currently being executed by the interpreter.
1698 CORO_SUSPENDED: Currently suspended at an await expression.
1699 CORO_CLOSED: Execution has completed.
1700 """
1701 if coroutine.cr_running:
1702 return CORO_RUNNING
1703 if coroutine.cr_frame is None:
1704 return CORO_CLOSED
1705 if coroutine.cr_frame.f_lasti == -1:
1706 return CORO_CREATED
1707 return CORO_SUSPENDED
1708
1709
1710def getcoroutinelocals(coroutine):
1711 """
1712 Get the mapping of coroutine local variables to their current values.
1713
1714 A dict is returned, with the keys the local variable names and values the
1715 bound values."""
1716 frame = getattr(coroutine, "cr_frame", None)
1717 if frame is not None:
1718 return frame.f_locals
1719 else:
1720 return {}
1721
1722
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001723###############################################################################
1724### Function Signature Object (PEP 362)
1725###############################################################################
1726
1727
1728_WrapperDescriptor = type(type.__call__)
1729_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001730_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001731
1732_NonUserDefinedCallables = (_WrapperDescriptor,
1733 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001734 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001735 types.BuiltinFunctionType)
1736
1737
Yury Selivanov421f0c72014-01-29 12:05:40 -05001738def _signature_get_user_defined_method(cls, method_name):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001739 """Private helper. Checks if ``cls`` has an attribute
1740 named ``method_name`` and returns it only if it is a
1741 pure python function.
1742 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001743 try:
1744 meth = getattr(cls, method_name)
1745 except AttributeError:
1746 return
1747 else:
1748 if not isinstance(meth, _NonUserDefinedCallables):
1749 # Once '__signature__' will be added to 'C'-level
1750 # callables, this check won't be necessary
1751 return meth
1752
1753
Yury Selivanov62560fb2014-01-28 12:26:24 -05001754def _signature_get_partial(wrapped_sig, partial, extra_args=()):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001755 """Private helper to calculate how 'wrapped_sig' signature will
1756 look like after applying a 'functools.partial' object (or alike)
1757 on it.
1758 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001759
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001760 old_params = wrapped_sig.parameters
Inada Naoki21105512020-03-02 18:54:49 +09001761 new_params = OrderedDict(old_params.items())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001762
1763 partial_args = partial.args or ()
1764 partial_keywords = partial.keywords or {}
1765
1766 if extra_args:
1767 partial_args = extra_args + partial_args
1768
1769 try:
1770 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1771 except TypeError as ex:
1772 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1773 raise ValueError(msg) from ex
1774
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001775
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001776 transform_to_kwonly = False
1777 for param_name, param in old_params.items():
1778 try:
1779 arg_value = ba.arguments[param_name]
1780 except KeyError:
1781 pass
1782 else:
1783 if param.kind is _POSITIONAL_ONLY:
1784 # If positional-only parameter is bound by partial,
1785 # it effectively disappears from the signature
Inada Naoki21105512020-03-02 18:54:49 +09001786 new_params.pop(param_name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001787 continue
1788
1789 if param.kind is _POSITIONAL_OR_KEYWORD:
1790 if param_name in partial_keywords:
1791 # This means that this parameter, and all parameters
1792 # after it should be keyword-only (and var-positional
1793 # should be removed). Here's why. Consider the following
1794 # function:
1795 # foo(a, b, *args, c):
1796 # pass
1797 #
1798 # "partial(foo, a='spam')" will have the following
1799 # signature: "(*, a='spam', b, c)". Because attempting
1800 # to call that partial with "(10, 20)" arguments will
1801 # raise a TypeError, saying that "a" argument received
1802 # multiple values.
1803 transform_to_kwonly = True
1804 # Set the new default value
Inada Naoki21105512020-03-02 18:54:49 +09001805 new_params[param_name] = param.replace(default=arg_value)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001806 else:
1807 # was passed as a positional argument
Inada Naoki21105512020-03-02 18:54:49 +09001808 new_params.pop(param.name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001809 continue
1810
1811 if param.kind is _KEYWORD_ONLY:
1812 # Set the new default value
Inada Naoki21105512020-03-02 18:54:49 +09001813 new_params[param_name] = param.replace(default=arg_value)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001814
1815 if transform_to_kwonly:
1816 assert param.kind is not _POSITIONAL_ONLY
1817
1818 if param.kind is _POSITIONAL_OR_KEYWORD:
Inada Naoki21105512020-03-02 18:54:49 +09001819 new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1820 new_params[param_name] = new_param
1821 new_params.move_to_end(param_name)
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001822 elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
Inada Naoki21105512020-03-02 18:54:49 +09001823 new_params.move_to_end(param_name)
1824 elif param.kind is _VAR_POSITIONAL:
1825 new_params.pop(param.name)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001826
1827 return wrapped_sig.replace(parameters=new_params.values())
1828
1829
Yury Selivanov62560fb2014-01-28 12:26:24 -05001830def _signature_bound_method(sig):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001831 """Private helper to transform signatures for unbound
1832 functions to bound methods.
1833 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001834
1835 params = tuple(sig.parameters.values())
1836
1837 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1838 raise ValueError('invalid method signature')
1839
1840 kind = params[0].kind
1841 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1842 # Drop first parameter:
1843 # '(p1, p2[, ...])' -> '(p2[, ...])'
1844 params = params[1:]
1845 else:
1846 if kind is not _VAR_POSITIONAL:
1847 # Unless we add a new parameter type we never
1848 # get here
1849 raise ValueError('invalid argument type')
1850 # It's a var-positional parameter.
1851 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1852
1853 return sig.replace(parameters=params)
1854
1855
Yury Selivanovb77511d2014-01-29 10:46:14 -05001856def _signature_is_builtin(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001857 """Private helper to test if `obj` is a callable that might
1858 support Argument Clinic's __text_signature__ protocol.
1859 """
Yury Selivanov1d241832014-02-02 12:51:20 -05001860 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001861 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001862 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001863 # Can't test 'isinstance(type)' here, as it would
1864 # also be True for regular python classes
1865 obj in (type, object))
1866
1867
Yury Selivanov63da7c72014-01-31 14:48:37 -05001868def _signature_is_functionlike(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001869 """Private helper to test if `obj` is a duck type of FunctionType.
1870 A good example of such objects are functions compiled with
1871 Cython, which have all attributes that a pure Python function
1872 would have, but have their code statically compiled.
1873 """
Yury Selivanov63da7c72014-01-31 14:48:37 -05001874
1875 if not callable(obj) or isclass(obj):
1876 # All function-like objects are obviously callables,
1877 # and not classes.
1878 return False
1879
1880 name = getattr(obj, '__name__', None)
1881 code = getattr(obj, '__code__', None)
1882 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
1883 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
Batuhan Taskaya044a1042020-10-06 23:03:02 +03001884 try:
1885 annotations = _get_type_hints(obj)
1886 except AttributeError:
1887 annotations = None
Yury Selivanov63da7c72014-01-31 14:48:37 -05001888
1889 return (isinstance(code, types.CodeType) and
1890 isinstance(name, str) and
1891 (defaults is None or isinstance(defaults, tuple)) and
1892 (kwdefaults is None or isinstance(kwdefaults, dict)) and
1893 isinstance(annotations, dict))
1894
1895
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001896def _signature_get_bound_param(spec):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001897 """ Private helper to get first parameter name from a
1898 __text_signature__ of a builtin method, which should
1899 be in the following format: '($param1, ...)'.
1900 Assumptions are that the first argument won't have
1901 a default value or an annotation.
1902 """
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001903
1904 assert spec.startswith('($')
1905
1906 pos = spec.find(',')
1907 if pos == -1:
1908 pos = spec.find(')')
1909
1910 cpos = spec.find(':')
1911 assert cpos == -1 or cpos > pos
1912
1913 cpos = spec.find('=')
1914 assert cpos == -1 or cpos > pos
1915
1916 return spec[2:pos]
1917
1918
Larry Hastings2623c8c2014-02-08 22:15:29 -08001919def _signature_strip_non_python_syntax(signature):
1920 """
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001921 Private helper function. Takes a signature in Argument Clinic's
1922 extended signature format.
1923
Larry Hastings2623c8c2014-02-08 22:15:29 -08001924 Returns a tuple of three things:
1925 * that signature re-rendered in standard Python syntax,
1926 * the index of the "self" parameter (generally 0), or None if
1927 the function does not have a "self" parameter, and
1928 * the index of the last "positional only" parameter,
1929 or None if the signature has no positional-only parameters.
1930 """
1931
1932 if not signature:
1933 return signature, None, None
1934
1935 self_parameter = None
1936 last_positional_only = None
1937
1938 lines = [l.encode('ascii') for l in signature.split('\n')]
1939 generator = iter(lines).__next__
1940 token_stream = tokenize.tokenize(generator)
1941
1942 delayed_comma = False
1943 skip_next_comma = False
1944 text = []
1945 add = text.append
1946
1947 current_parameter = 0
1948 OP = token.OP
1949 ERRORTOKEN = token.ERRORTOKEN
1950
1951 # token stream always starts with ENCODING token, skip it
1952 t = next(token_stream)
1953 assert t.type == tokenize.ENCODING
1954
1955 for t in token_stream:
1956 type, string = t.type, t.string
1957
1958 if type == OP:
1959 if string == ',':
1960 if skip_next_comma:
1961 skip_next_comma = False
1962 else:
1963 assert not delayed_comma
1964 delayed_comma = True
1965 current_parameter += 1
1966 continue
1967
1968 if string == '/':
1969 assert not skip_next_comma
1970 assert last_positional_only is None
1971 skip_next_comma = True
1972 last_positional_only = current_parameter - 1
1973 continue
1974
1975 if (type == ERRORTOKEN) and (string == '$'):
1976 assert self_parameter is None
1977 self_parameter = current_parameter
1978 continue
1979
1980 if delayed_comma:
1981 delayed_comma = False
1982 if not ((type == OP) and (string == ')')):
1983 add(', ')
1984 add(string)
1985 if (string == ','):
1986 add(' ')
1987 clean_signature = ''.join(text)
1988 return clean_signature, self_parameter, last_positional_only
1989
1990
Yury Selivanov57d240e2014-02-19 16:27:23 -05001991def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001992 """Private helper to parse content of '__text_signature__'
1993 and return a Signature based on it.
1994 """
INADA Naoki37420de2018-01-27 10:10:06 +09001995 # Lazy import ast because it's relatively heavy and
1996 # it's not used for other than this function.
1997 import ast
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001998
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001999 Parameter = cls._parameter_cls
2000
Larry Hastings2623c8c2014-02-08 22:15:29 -08002001 clean_signature, self_parameter, last_positional_only = \
2002 _signature_strip_non_python_syntax(s)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002003
Larry Hastings2623c8c2014-02-08 22:15:29 -08002004 program = "def foo" + clean_signature + ": pass"
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002005
2006 try:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002007 module = ast.parse(program)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002008 except SyntaxError:
2009 module = None
2010
2011 if not isinstance(module, ast.Module):
2012 raise ValueError("{!r} builtin has invalid signature".format(obj))
2013
2014 f = module.body[0]
2015
2016 parameters = []
2017 empty = Parameter.empty
2018 invalid = object()
2019
2020 module = None
2021 module_dict = {}
2022 module_name = getattr(obj, '__module__', None)
2023 if module_name:
2024 module = sys.modules.get(module_name, None)
2025 if module:
2026 module_dict = module.__dict__
INADA Naoki6f85b822018-10-05 01:47:09 +09002027 sys_module_dict = sys.modules.copy()
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002028
2029 def parse_name(node):
2030 assert isinstance(node, ast.arg)
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి)feaefc72018-02-09 15:29:19 +05302031 if node.annotation is not None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002032 raise ValueError("Annotations are not currently supported")
2033 return node.arg
2034
2035 def wrap_value(s):
2036 try:
2037 value = eval(s, module_dict)
2038 except NameError:
2039 try:
2040 value = eval(s, sys_module_dict)
2041 except NameError:
2042 raise RuntimeError()
2043
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002044 if isinstance(value, (str, int, float, bytes, bool, type(None))):
2045 return ast.Constant(value)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002046 raise RuntimeError()
2047
2048 class RewriteSymbolics(ast.NodeTransformer):
2049 def visit_Attribute(self, node):
2050 a = []
2051 n = node
2052 while isinstance(n, ast.Attribute):
2053 a.append(n.attr)
2054 n = n.value
2055 if not isinstance(n, ast.Name):
2056 raise RuntimeError()
2057 a.append(n.id)
2058 value = ".".join(reversed(a))
2059 return wrap_value(value)
2060
2061 def visit_Name(self, node):
2062 if not isinstance(node.ctx, ast.Load):
2063 raise ValueError()
2064 return wrap_value(node.id)
2065
2066 def p(name_node, default_node, default=empty):
2067 name = parse_name(name_node)
2068 if name is invalid:
2069 return None
2070 if default_node and default_node is not _empty:
2071 try:
2072 default_node = RewriteSymbolics().visit(default_node)
2073 o = ast.literal_eval(default_node)
2074 except ValueError:
2075 o = invalid
2076 if o is invalid:
2077 return None
2078 default = o if o is not invalid else default
2079 parameters.append(Parameter(name, kind, default=default, annotation=empty))
2080
2081 # non-keyword-only parameters
2082 args = reversed(f.args.args)
2083 defaults = reversed(f.args.defaults)
2084 iter = itertools.zip_longest(args, defaults, fillvalue=None)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002085 if last_positional_only is not None:
2086 kind = Parameter.POSITIONAL_ONLY
2087 else:
2088 kind = Parameter.POSITIONAL_OR_KEYWORD
2089 for i, (name, default) in enumerate(reversed(list(iter))):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002090 p(name, default)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002091 if i == last_positional_only:
2092 kind = Parameter.POSITIONAL_OR_KEYWORD
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002093
2094 # *args
2095 if f.args.vararg:
2096 kind = Parameter.VAR_POSITIONAL
2097 p(f.args.vararg, empty)
2098
2099 # keyword-only arguments
2100 kind = Parameter.KEYWORD_ONLY
2101 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
2102 p(name, default)
2103
2104 # **kwargs
2105 if f.args.kwarg:
2106 kind = Parameter.VAR_KEYWORD
2107 p(f.args.kwarg, empty)
2108
Larry Hastings2623c8c2014-02-08 22:15:29 -08002109 if self_parameter is not None:
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002110 # Possibly strip the bound argument:
2111 # - We *always* strip first bound argument if
2112 # it is a module.
2113 # - We don't strip first bound argument if
2114 # skip_bound_arg is False.
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002115 assert parameters
Yury Selivanov8c185ee2014-02-21 01:32:42 -05002116 _self = getattr(obj, '__self__', None)
2117 self_isbound = _self is not None
2118 self_ismodule = ismodule(_self)
2119 if self_isbound and (self_ismodule or skip_bound_arg):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002120 parameters.pop(0)
2121 else:
2122 # for builtins, self parameter is always positional-only!
2123 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
2124 parameters[0] = p
2125
2126 return cls(parameters, return_annotation=cls.empty)
2127
Batuhan Taskaya044a1042020-10-06 23:03:02 +03002128def _get_type_hints(func):
2129 try:
2130 return typing.get_type_hints(func)
2131 except Exception:
2132 # First, try to use the get_type_hints to resolve
2133 # annotations. But for keeping the behavior intact
2134 # if there was a problem with that (like the namespace
2135 # can't resolve some annotation) continue to use
2136 # string annotations
2137 return func.__annotations__
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002138
Yury Selivanov57d240e2014-02-19 16:27:23 -05002139def _signature_from_builtin(cls, func, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002140 """Private helper function to get signature for
2141 builtin callables.
2142 """
2143
Yury Selivanov57d240e2014-02-19 16:27:23 -05002144 if not _signature_is_builtin(func):
2145 raise TypeError("{!r} is not a Python builtin "
2146 "function".format(func))
2147
2148 s = getattr(func, "__text_signature__", None)
2149 if not s:
2150 raise ValueError("no signature found for builtin {!r}".format(func))
2151
2152 return _signature_fromstr(cls, func, s, skip_bound_arg)
2153
2154
Serhiy Storchakad53cf992019-05-06 22:40:27 +03002155def _signature_from_function(cls, func, skip_bound_arg=True):
Yury Selivanovcf45f022015-05-20 14:38:50 -04002156 """Private helper: constructs Signature for the given python function."""
2157
2158 is_duck_function = False
2159 if not isfunction(func):
2160 if _signature_is_functionlike(func):
2161 is_duck_function = True
2162 else:
2163 # If it's not a pure Python function, and not a duck type
2164 # of pure function:
2165 raise TypeError('{!r} is not a Python function'.format(func))
2166
Serhiy Storchakad53cf992019-05-06 22:40:27 +03002167 s = getattr(func, "__text_signature__", None)
2168 if s:
2169 return _signature_fromstr(cls, func, s, skip_bound_arg)
2170
Yury Selivanovcf45f022015-05-20 14:38:50 -04002171 Parameter = cls._parameter_cls
2172
2173 # Parameter information.
2174 func_code = func.__code__
2175 pos_count = func_code.co_argcount
2176 arg_names = func_code.co_varnames
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002177 posonly_count = func_code.co_posonlyargcount
Pablo Galindocd74e662019-06-01 18:08:04 +01002178 positional = arg_names[:pos_count]
Yury Selivanovcf45f022015-05-20 14:38:50 -04002179 keyword_only_count = func_code.co_kwonlyargcount
Pablo Galindocd74e662019-06-01 18:08:04 +01002180 keyword_only = arg_names[pos_count:pos_count + keyword_only_count]
Batuhan Taskaya044a1042020-10-06 23:03:02 +03002181 annotations = _get_type_hints(func)
2182
Yury Selivanovcf45f022015-05-20 14:38:50 -04002183 defaults = func.__defaults__
2184 kwdefaults = func.__kwdefaults__
2185
2186 if defaults:
2187 pos_default_count = len(defaults)
2188 else:
2189 pos_default_count = 0
2190
2191 parameters = []
2192
Pablo Galindocd74e662019-06-01 18:08:04 +01002193 non_default_count = pos_count - pos_default_count
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002194 posonly_left = posonly_count
2195
Yury Selivanovcf45f022015-05-20 14:38:50 -04002196 # Non-keyword-only parameters w/o defaults.
Pablo Galindocd74e662019-06-01 18:08:04 +01002197 for name in positional[:non_default_count]:
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002198 kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD
Yury Selivanovcf45f022015-05-20 14:38:50 -04002199 annotation = annotations.get(name, _empty)
2200 parameters.append(Parameter(name, annotation=annotation,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002201 kind=kind))
2202 if posonly_left:
2203 posonly_left -= 1
Yury Selivanovcf45f022015-05-20 14:38:50 -04002204
2205 # ... w/ defaults.
Pablo Galindocd74e662019-06-01 18:08:04 +01002206 for offset, name in enumerate(positional[non_default_count:]):
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002207 kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD
Yury Selivanovcf45f022015-05-20 14:38:50 -04002208 annotation = annotations.get(name, _empty)
2209 parameters.append(Parameter(name, annotation=annotation,
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002210 kind=kind,
Yury Selivanovcf45f022015-05-20 14:38:50 -04002211 default=defaults[offset]))
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002212 if posonly_left:
2213 posonly_left -= 1
Yury Selivanovcf45f022015-05-20 14:38:50 -04002214
2215 # *args
2216 if func_code.co_flags & CO_VARARGS:
Pablo Galindocd74e662019-06-01 18:08:04 +01002217 name = arg_names[pos_count + keyword_only_count]
Yury Selivanovcf45f022015-05-20 14:38:50 -04002218 annotation = annotations.get(name, _empty)
2219 parameters.append(Parameter(name, annotation=annotation,
2220 kind=_VAR_POSITIONAL))
2221
2222 # Keyword-only parameters.
2223 for name in keyword_only:
2224 default = _empty
2225 if kwdefaults is not None:
2226 default = kwdefaults.get(name, _empty)
2227
2228 annotation = annotations.get(name, _empty)
2229 parameters.append(Parameter(name, annotation=annotation,
2230 kind=_KEYWORD_ONLY,
2231 default=default))
2232 # **kwargs
2233 if func_code.co_flags & CO_VARKEYWORDS:
Pablo Galindocd74e662019-06-01 18:08:04 +01002234 index = pos_count + keyword_only_count
Yury Selivanovcf45f022015-05-20 14:38:50 -04002235 if func_code.co_flags & CO_VARARGS:
2236 index += 1
2237
2238 name = arg_names[index]
2239 annotation = annotations.get(name, _empty)
2240 parameters.append(Parameter(name, annotation=annotation,
2241 kind=_VAR_KEYWORD))
2242
2243 # Is 'func' is a pure Python function - don't validate the
2244 # parameters list (for correct order and defaults), it should be OK.
2245 return cls(parameters,
2246 return_annotation=annotations.get('return', _empty),
2247 __validate_parameters__=is_duck_function)
2248
2249
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002250def _signature_from_callable(obj, *,
2251 follow_wrapper_chains=True,
2252 skip_bound_arg=True,
2253 sigcls):
2254
2255 """Private helper function to get signature for arbitrary
2256 callable objects.
2257 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002258
2259 if not callable(obj):
2260 raise TypeError('{!r} is not a callable object'.format(obj))
2261
2262 if isinstance(obj, types.MethodType):
2263 # In this case we skip the first parameter of the underlying
2264 # function (usually `self` or `cls`).
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002265 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002266 obj.__func__,
2267 follow_wrapper_chains=follow_wrapper_chains,
2268 skip_bound_arg=skip_bound_arg,
2269 sigcls=sigcls)
2270
Yury Selivanov57d240e2014-02-19 16:27:23 -05002271 if skip_bound_arg:
2272 return _signature_bound_method(sig)
2273 else:
2274 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002275
Nick Coghlane8c45d62013-07-28 20:00:01 +10002276 # Was this function wrapped by a decorator?
Yury Selivanov57d240e2014-02-19 16:27:23 -05002277 if follow_wrapper_chains:
2278 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
Yury Selivanov46c759d2015-05-27 21:56:53 -04002279 if isinstance(obj, types.MethodType):
2280 # If the unwrapped object is a *method*, we might want to
2281 # skip its first parameter (self).
2282 # See test_signature_wrapped_bound_method for details.
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002283 return _signature_from_callable(
Yury Selivanov46c759d2015-05-27 21:56:53 -04002284 obj,
2285 follow_wrapper_chains=follow_wrapper_chains,
Yury Selivanov507cd3c2015-05-27 21:59:03 -04002286 skip_bound_arg=skip_bound_arg,
2287 sigcls=sigcls)
Nick Coghlane8c45d62013-07-28 20:00:01 +10002288
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002289 try:
2290 sig = obj.__signature__
2291 except AttributeError:
2292 pass
2293 else:
2294 if sig is not None:
Yury Selivanov42407ab2014-06-23 10:23:50 -07002295 if not isinstance(sig, Signature):
2296 raise TypeError(
2297 'unexpected object {!r} in __signature__ '
2298 'attribute'.format(sig))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002299 return sig
2300
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002301 try:
2302 partialmethod = obj._partialmethod
2303 except AttributeError:
2304 pass
2305 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05002306 if isinstance(partialmethod, functools.partialmethod):
2307 # Unbound partialmethod (see functools.partialmethod)
2308 # This means, that we need to calculate the signature
2309 # as if it's a regular partial object, but taking into
2310 # account that the first positional argument
2311 # (usually `self`, or `cls`) will not be passed
2312 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002313
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002314 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002315 partialmethod.func,
2316 follow_wrapper_chains=follow_wrapper_chains,
2317 skip_bound_arg=skip_bound_arg,
2318 sigcls=sigcls)
2319
Yury Selivanov0486f812014-01-29 12:18:59 -05002320 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanov0486f812014-01-29 12:18:59 -05002321 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
Dong-hee Na378d7062017-05-18 04:00:51 +09002322 if first_wrapped_param.kind is Parameter.VAR_POSITIONAL:
2323 # First argument of the wrapped callable is `*args`, as in
2324 # `partialmethod(lambda *args)`.
2325 return sig
2326 else:
2327 sig_params = tuple(sig.parameters.values())
Yury Selivanov8a387212018-03-06 12:59:45 -05002328 assert (not sig_params or
2329 first_wrapped_param is not sig_params[0])
Dong-hee Na378d7062017-05-18 04:00:51 +09002330 new_params = (first_wrapped_param,) + sig_params
2331 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002332
Yury Selivanov63da7c72014-01-31 14:48:37 -05002333 if isfunction(obj) or _signature_is_functionlike(obj):
2334 # If it's a pure Python function, or an object that is duck type
2335 # of a Python function (Cython functions, for instance), then:
Serhiy Storchakad53cf992019-05-06 22:40:27 +03002336 return _signature_from_function(sigcls, obj,
2337 skip_bound_arg=skip_bound_arg)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002338
Yury Selivanova773de02014-02-21 18:30:53 -05002339 if _signature_is_builtin(obj):
Yury Selivanovda396452014-03-27 12:09:24 -04002340 return _signature_from_builtin(sigcls, obj,
Yury Selivanova773de02014-02-21 18:30:53 -05002341 skip_bound_arg=skip_bound_arg)
2342
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002343 if isinstance(obj, functools.partial):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002344 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002345 obj.func,
2346 follow_wrapper_chains=follow_wrapper_chains,
2347 skip_bound_arg=skip_bound_arg,
2348 sigcls=sigcls)
Yury Selivanov62560fb2014-01-28 12:26:24 -05002349 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002350
2351 sig = None
2352 if isinstance(obj, type):
2353 # obj is a class or a metaclass
2354
2355 # First, let's see if it has an overloaded __call__ defined
2356 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05002357 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002358 if call is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002359 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002360 call,
2361 follow_wrapper_chains=follow_wrapper_chains,
2362 skip_bound_arg=skip_bound_arg,
2363 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002364 else:
2365 # Now we check if the 'obj' class has a '__new__' method
Yury Selivanov421f0c72014-01-29 12:05:40 -05002366 new = _signature_get_user_defined_method(obj, '__new__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002367 if new is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002368 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002369 new,
2370 follow_wrapper_chains=follow_wrapper_chains,
2371 skip_bound_arg=skip_bound_arg,
2372 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002373 else:
2374 # Finally, we should have at least __init__ implemented
Yury Selivanov421f0c72014-01-29 12:05:40 -05002375 init = _signature_get_user_defined_method(obj, '__init__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002376 if init is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002377 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002378 init,
2379 follow_wrapper_chains=follow_wrapper_chains,
2380 skip_bound_arg=skip_bound_arg,
2381 sigcls=sigcls)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002382
2383 if sig is None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002384 # At this point we know, that `obj` is a class, with no user-
2385 # defined '__init__', '__new__', or class-level '__call__'
2386
Larry Hastings2623c8c2014-02-08 22:15:29 -08002387 for base in obj.__mro__[:-1]:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002388 # Since '__text_signature__' is implemented as a
2389 # descriptor that extracts text signature from the
2390 # class docstring, if 'obj' is derived from a builtin
2391 # class, its own '__text_signature__' may be 'None'.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002392 # Therefore, we go through the MRO (except the last
2393 # class in there, which is 'object') to find the first
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002394 # class with non-empty text signature.
2395 try:
2396 text_sig = base.__text_signature__
2397 except AttributeError:
2398 pass
2399 else:
2400 if text_sig:
2401 # If 'obj' class has a __text_signature__ attribute:
2402 # return a signature based on it
Yury Selivanovda396452014-03-27 12:09:24 -04002403 return _signature_fromstr(sigcls, obj, text_sig)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002404
2405 # No '__text_signature__' was found for the 'obj' class.
2406 # Last option is to check if its '__init__' is
2407 # object.__init__ or type.__init__.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002408 if type not in obj.__mro__:
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002409 # We have a class (not metaclass), but no user-defined
2410 # __init__ or __new__ for it
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002411 if (obj.__init__ is object.__init__ and
2412 obj.__new__ is object.__new__):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002413 # Return a signature of 'object' builtin.
Gregory P. Smith5b9ff7a2019-09-13 17:13:51 +01002414 return sigcls.from_callable(object)
Yury Selivanovbf304fc2015-05-30 17:08:36 -04002415 else:
2416 raise ValueError(
2417 'no signature found for builtin type {!r}'.format(obj))
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002418
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002419 elif not isinstance(obj, _NonUserDefinedCallables):
2420 # An object with __call__
2421 # We also check that the 'obj' is not an instance of
2422 # _WrapperDescriptor or _MethodWrapper to avoid
2423 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05002424 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002425 if call is not None:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002426 try:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002427 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002428 call,
2429 follow_wrapper_chains=follow_wrapper_chains,
2430 skip_bound_arg=skip_bound_arg,
2431 sigcls=sigcls)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002432 except ValueError as ex:
2433 msg = 'no signature found for {!r}'.format(obj)
2434 raise ValueError(msg) from ex
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002435
2436 if sig is not None:
2437 # For classes and objects we skip the first parameter of their
2438 # __call__, __new__, or __init__ methods
Yury Selivanov57d240e2014-02-19 16:27:23 -05002439 if skip_bound_arg:
2440 return _signature_bound_method(sig)
2441 else:
2442 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002443
2444 if isinstance(obj, types.BuiltinFunctionType):
2445 # Raise a nicer error message for builtins
2446 msg = 'no signature found for builtin function {!r}'.format(obj)
2447 raise ValueError(msg)
2448
2449 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2450
2451
2452class _void:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002453 """A private marker - used in Parameter & Signature."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002454
2455
2456class _empty:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002457 """Marker object for Signature.empty and Parameter.empty."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002458
2459
Yury Selivanov21e83a52014-03-27 11:23:13 -04002460class _ParameterKind(enum.IntEnum):
2461 POSITIONAL_ONLY = 0
2462 POSITIONAL_OR_KEYWORD = 1
2463 VAR_POSITIONAL = 2
2464 KEYWORD_ONLY = 3
2465 VAR_KEYWORD = 4
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002466
2467 def __str__(self):
Yury Selivanov21e83a52014-03-27 11:23:13 -04002468 return self._name_
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002469
Dong-hee Na4aa30062018-06-08 12:46:31 +09002470 @property
2471 def description(self):
2472 return _PARAM_NAME_MAPPING[self]
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002473
Yury Selivanov21e83a52014-03-27 11:23:13 -04002474_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
2475_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
2476_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
2477_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
2478_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002479
Dong-hee Naa9cab432018-05-30 00:04:08 +09002480_PARAM_NAME_MAPPING = {
2481 _POSITIONAL_ONLY: 'positional-only',
2482 _POSITIONAL_OR_KEYWORD: 'positional or keyword',
2483 _VAR_POSITIONAL: 'variadic positional',
2484 _KEYWORD_ONLY: 'keyword-only',
2485 _VAR_KEYWORD: 'variadic keyword'
2486}
2487
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002488
2489class Parameter:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002490 """Represents a parameter in a function signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002491
2492 Has the following public attributes:
2493
2494 * name : str
2495 The name of the parameter as a string.
2496 * default : object
2497 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002498 parameter has no default value, this attribute is set to
2499 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002500 * annotation
2501 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002502 parameter has no annotation, this attribute is set to
2503 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002504 * kind : str
2505 Describes how argument values are bound to the parameter.
2506 Possible values: `Parameter.POSITIONAL_ONLY`,
2507 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
2508 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002509 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002510
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002511 __slots__ = ('_name', '_kind', '_default', '_annotation')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002512
2513 POSITIONAL_ONLY = _POSITIONAL_ONLY
2514 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
2515 VAR_POSITIONAL = _VAR_POSITIONAL
2516 KEYWORD_ONLY = _KEYWORD_ONLY
2517 VAR_KEYWORD = _VAR_KEYWORD
2518
2519 empty = _empty
2520
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002521 def __init__(self, name, kind, *, default=_empty, annotation=_empty):
Dong-hee Naa9cab432018-05-30 00:04:08 +09002522 try:
2523 self._kind = _ParameterKind(kind)
2524 except ValueError:
2525 raise ValueError(f'value {kind!r} is not a valid Parameter.kind')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002526 if default is not _empty:
Dong-hee Naa9cab432018-05-30 00:04:08 +09002527 if self._kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2528 msg = '{} parameters cannot have default values'
Dong-hee Na4aa30062018-06-08 12:46:31 +09002529 msg = msg.format(self._kind.description)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002530 raise ValueError(msg)
2531 self._default = default
2532 self._annotation = annotation
2533
Yury Selivanov2393dca2014-01-27 15:07:58 -05002534 if name is _empty:
2535 raise ValueError('name is a required attribute for Parameter')
2536
2537 if not isinstance(name, str):
Dong-hee Naa9cab432018-05-30 00:04:08 +09002538 msg = 'name must be a str, not a {}'.format(type(name).__name__)
2539 raise TypeError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002540
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002541 if name[0] == '.' and name[1:].isdigit():
2542 # These are implicit arguments generated by comprehensions. In
2543 # order to provide a friendlier interface to users, we recast
2544 # their name as "implicitN" and treat them as positional-only.
2545 # See issue 19611.
Dong-hee Naa9cab432018-05-30 00:04:08 +09002546 if self._kind != _POSITIONAL_OR_KEYWORD:
2547 msg = (
2548 'implicit arguments must be passed as '
2549 'positional or keyword arguments, not {}'
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002550 )
Dong-hee Na4aa30062018-06-08 12:46:31 +09002551 msg = msg.format(self._kind.description)
Dong-hee Naa9cab432018-05-30 00:04:08 +09002552 raise ValueError(msg)
Nick Coghlanb4b966e2016-06-04 14:40:03 -07002553 self._kind = _POSITIONAL_ONLY
2554 name = 'implicit{}'.format(name[1:])
2555
Yury Selivanov2393dca2014-01-27 15:07:58 -05002556 if not name.isidentifier():
2557 raise ValueError('{!r} is not a valid parameter name'.format(name))
2558
2559 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002560
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002561 def __reduce__(self):
2562 return (type(self),
2563 (self._name, self._kind),
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002564 {'_default': self._default,
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002565 '_annotation': self._annotation})
2566
2567 def __setstate__(self, state):
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002568 self._default = state['_default']
2569 self._annotation = state['_annotation']
2570
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002571 @property
2572 def name(self):
2573 return self._name
2574
2575 @property
2576 def default(self):
2577 return self._default
2578
2579 @property
2580 def annotation(self):
2581 return self._annotation
2582
2583 @property
2584 def kind(self):
2585 return self._kind
2586
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002587 def replace(self, *, name=_void, kind=_void,
2588 annotation=_void, default=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002589 """Creates a customized copy of the Parameter."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002590
2591 if name is _void:
2592 name = self._name
2593
2594 if kind is _void:
2595 kind = self._kind
2596
2597 if annotation is _void:
2598 annotation = self._annotation
2599
2600 if default is _void:
2601 default = self._default
2602
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002603 return type(self)(name, kind, default=default, annotation=annotation)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002604
2605 def __str__(self):
2606 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002607 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002608
2609 # Add annotation and default value
2610 if self._annotation is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002611 formatted = '{}: {}'.format(formatted,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002612 formatannotation(self._annotation))
2613
2614 if self._default is not _empty:
Dong-hee Na762b9572017-11-16 03:30:59 +09002615 if self._annotation is not _empty:
2616 formatted = '{} = {}'.format(formatted, repr(self._default))
2617 else:
2618 formatted = '{}={}'.format(formatted, repr(self._default))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002619
2620 if kind == _VAR_POSITIONAL:
2621 formatted = '*' + formatted
2622 elif kind == _VAR_KEYWORD:
2623 formatted = '**' + formatted
2624
2625 return formatted
2626
2627 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04002628 return '<{} "{}">'.format(self.__class__.__name__, self)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002629
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002630 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002631 return hash((self.name, self.kind, self.annotation, self.default))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002632
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002633 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002634 if self is other:
2635 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002636 if not isinstance(other, Parameter):
2637 return NotImplemented
2638 return (self._name == other._name and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002639 self._kind == other._kind and
2640 self._default == other._default and
2641 self._annotation == other._annotation)
2642
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002643
2644class BoundArguments:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002645 """Result of `Signature.bind` call. Holds the mapping of arguments
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002646 to the function's parameters.
2647
2648 Has the following public attributes:
2649
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002650 * arguments : dict
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002651 An ordered mutable mapping of parameters' names to arguments' values.
2652 Does not contain arguments' default values.
2653 * signature : Signature
2654 The Signature object that created this instance.
2655 * args : tuple
2656 Tuple of positional arguments values.
2657 * kwargs : dict
2658 Dict of keyword arguments values.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002659 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002660
Yury Selivanov6abe0322015-05-13 17:18:41 -04002661 __slots__ = ('arguments', '_signature', '__weakref__')
2662
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002663 def __init__(self, signature, arguments):
2664 self.arguments = arguments
2665 self._signature = signature
2666
2667 @property
2668 def signature(self):
2669 return self._signature
2670
2671 @property
2672 def args(self):
2673 args = []
2674 for param_name, param in self._signature.parameters.items():
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002675 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002676 break
2677
2678 try:
2679 arg = self.arguments[param_name]
2680 except KeyError:
2681 # We're done here. Other arguments
2682 # will be mapped in 'BoundArguments.kwargs'
2683 break
2684 else:
2685 if param.kind == _VAR_POSITIONAL:
2686 # *args
2687 args.extend(arg)
2688 else:
2689 # plain argument
2690 args.append(arg)
2691
2692 return tuple(args)
2693
2694 @property
2695 def kwargs(self):
2696 kwargs = {}
2697 kwargs_started = False
2698 for param_name, param in self._signature.parameters.items():
2699 if not kwargs_started:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002700 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002701 kwargs_started = True
2702 else:
2703 if param_name not in self.arguments:
2704 kwargs_started = True
2705 continue
2706
2707 if not kwargs_started:
2708 continue
2709
2710 try:
2711 arg = self.arguments[param_name]
2712 except KeyError:
2713 pass
2714 else:
2715 if param.kind == _VAR_KEYWORD:
2716 # **kwargs
2717 kwargs.update(arg)
2718 else:
2719 # plain keyword argument
2720 kwargs[param_name] = arg
2721
2722 return kwargs
2723
Yury Selivanovb907a512015-05-16 13:45:09 -04002724 def apply_defaults(self):
2725 """Set default values for missing arguments.
2726
2727 For variable-positional arguments (*args) the default is an
2728 empty tuple.
2729
2730 For variable-keyword arguments (**kwargs) the default is an
2731 empty dict.
2732 """
2733 arguments = self.arguments
Yury Selivanovb907a512015-05-16 13:45:09 -04002734 new_arguments = []
2735 for name, param in self._signature.parameters.items():
2736 try:
2737 new_arguments.append((name, arguments[name]))
2738 except KeyError:
2739 if param.default is not _empty:
2740 val = param.default
2741 elif param.kind is _VAR_POSITIONAL:
2742 val = ()
2743 elif param.kind is _VAR_KEYWORD:
2744 val = {}
2745 else:
2746 # This BoundArguments was likely produced by
2747 # Signature.bind_partial().
2748 continue
2749 new_arguments.append((name, val))
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002750 self.arguments = dict(new_arguments)
Yury Selivanovb907a512015-05-16 13:45:09 -04002751
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002752 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002753 if self is other:
2754 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002755 if not isinstance(other, BoundArguments):
2756 return NotImplemented
2757 return (self.signature == other.signature and
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002758 self.arguments == other.arguments)
2759
Yury Selivanov6abe0322015-05-13 17:18:41 -04002760 def __setstate__(self, state):
2761 self._signature = state['_signature']
2762 self.arguments = state['arguments']
2763
2764 def __getstate__(self):
2765 return {'_signature': self._signature, 'arguments': self.arguments}
2766
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002767 def __repr__(self):
2768 args = []
2769 for arg, value in self.arguments.items():
2770 args.append('{}={!r}'.format(arg, value))
Yury Selivanovf229bc52015-05-15 12:53:56 -04002771 return '<{} ({})>'.format(self.__class__.__name__, ', '.join(args))
Yury Selivanov3f6538f2015-05-14 18:47:17 -04002772
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002773
2774class Signature:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002775 """A Signature object represents the overall signature of a function.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002776 It stores a Parameter object for each parameter accepted by the
2777 function, as well as information specific to the function itself.
2778
2779 A Signature object has the following public attributes and methods:
2780
Jens Reidel611836a2020-03-18 03:22:46 +01002781 * parameters : OrderedDict
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002782 An ordered mapping of parameters' names to the corresponding
2783 Parameter objects (keyword-only arguments are in the same order
2784 as listed in `code.co_varnames`).
2785 * return_annotation : object
2786 The annotation for the return type of the function if specified.
2787 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002788 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002789 * bind(*args, **kwargs) -> BoundArguments
2790 Creates a mapping from positional and keyword arguments to
2791 parameters.
2792 * bind_partial(*args, **kwargs) -> BoundArguments
2793 Creates a partial mapping from positional and keyword arguments
2794 to parameters (simulating 'functools.partial' behavior.)
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002795 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002796
2797 __slots__ = ('_return_annotation', '_parameters')
2798
2799 _parameter_cls = Parameter
2800 _bound_arguments_cls = BoundArguments
2801
2802 empty = _empty
2803
2804 def __init__(self, parameters=None, *, return_annotation=_empty,
2805 __validate_parameters__=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002806 """Constructs Signature from the given list of Parameter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002807 objects and 'return_annotation'. All arguments are optional.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002808 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002809
2810 if parameters is None:
Jens Reidel611836a2020-03-18 03:22:46 +01002811 params = OrderedDict()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002812 else:
2813 if __validate_parameters__:
Jens Reidel611836a2020-03-18 03:22:46 +01002814 params = OrderedDict()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002815 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002816 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002817
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002818 for param in parameters:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002819 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002820 name = param.name
2821
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002822 if kind < top_kind:
Dong-hee Naa9cab432018-05-30 00:04:08 +09002823 msg = (
2824 'wrong parameter order: {} parameter before {} '
2825 'parameter'
2826 )
Dong-hee Na4aa30062018-06-08 12:46:31 +09002827 msg = msg.format(top_kind.description,
2828 kind.description)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002829 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002830 elif kind > top_kind:
2831 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002832 top_kind = kind
2833
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002834 if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
Yury Selivanov07a9e452014-01-29 10:58:16 -05002835 if param.default is _empty:
2836 if kind_defaults:
2837 # No default for this parameter, but the
2838 # previous parameter of the same kind had
2839 # a default
2840 msg = 'non-default argument follows default ' \
2841 'argument'
2842 raise ValueError(msg)
2843 else:
2844 # There is a default for this parameter.
2845 kind_defaults = True
2846
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002847 if name in params:
2848 msg = 'duplicate parameter name: {!r}'.format(name)
2849 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002850
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002851 params[name] = param
2852 else:
Jens Reidel611836a2020-03-18 03:22:46 +01002853 params = OrderedDict((param.name, param) for param in parameters)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002854
2855 self._parameters = types.MappingProxyType(params)
2856 self._return_annotation = return_annotation
2857
2858 @classmethod
2859 def from_function(cls, func):
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002860 """Constructs Signature for the given python function.
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002861
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002862 Deprecated since Python 3.5, use `Signature.from_callable()`.
2863 """
2864
2865 warnings.warn("inspect.Signature.from_function() is deprecated since "
2866 "Python 3.5, use Signature.from_callable()",
Berker Peksagb5601582015-05-21 23:40:54 +03002867 DeprecationWarning, stacklevel=2)
Yury Selivanovcf45f022015-05-20 14:38:50 -04002868 return _signature_from_function(cls, func)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002869
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002870 @classmethod
2871 def from_builtin(cls, func):
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002872 """Constructs Signature for the given builtin function.
Yury Selivanov57c74fc2015-05-20 23:07:02 -04002873
Matthias Bussonnierded87d82018-10-19 16:40:45 -07002874 Deprecated since Python 3.5, use `Signature.from_callable()`.
2875 """
2876
2877 warnings.warn("inspect.Signature.from_builtin() is deprecated since "
2878 "Python 3.5, use Signature.from_callable()",
Berker Peksagb5601582015-05-21 23:40:54 +03002879 DeprecationWarning, stacklevel=2)
Yury Selivanov57d240e2014-02-19 16:27:23 -05002880 return _signature_from_builtin(cls, func)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002881
Yury Selivanovda396452014-03-27 12:09:24 -04002882 @classmethod
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002883 def from_callable(cls, obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002884 """Constructs Signature for the given callable object."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04002885 return _signature_from_callable(obj, sigcls=cls,
2886 follow_wrapper_chains=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04002887
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002888 @property
2889 def parameters(self):
2890 return self._parameters
2891
2892 @property
2893 def return_annotation(self):
2894 return self._return_annotation
2895
2896 def replace(self, *, parameters=_void, return_annotation=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002897 """Creates a customized copy of the Signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002898 Pass 'parameters' and/or 'return_annotation' arguments
2899 to override them in the new copy.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002900 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002901
2902 if parameters is _void:
2903 parameters = self.parameters.values()
2904
2905 if return_annotation is _void:
2906 return_annotation = self._return_annotation
2907
2908 return type(self)(parameters,
2909 return_annotation=return_annotation)
2910
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002911 def _hash_basis(self):
2912 params = tuple(param for param in self.parameters.values()
2913 if param.kind != _KEYWORD_ONLY)
2914
2915 kwo_params = {param.name: param for param in self.parameters.values()
2916 if param.kind == _KEYWORD_ONLY}
2917
2918 return params, kwo_params, self.return_annotation
2919
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002920 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002921 params, kwo_params, return_annotation = self._hash_basis()
2922 kwo_params = frozenset(kwo_params.values())
2923 return hash((params, kwo_params, return_annotation))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002924
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002925 def __eq__(self, other):
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002926 if self is other:
2927 return True
Serhiy Storchaka3018cc42015-07-18 23:19:05 +03002928 if not isinstance(other, Signature):
2929 return NotImplemented
Serhiy Storchaka2489bd52015-07-18 23:20:50 +03002930 return self._hash_basis() == other._hash_basis()
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002931
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002932 def _bind(self, args, kwargs, *, partial=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002933 """Private method. Don't use directly."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002934
Rémi Lapeyre2cca8ef2020-01-28 13:47:03 +01002935 arguments = {}
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002936
2937 parameters = iter(self.parameters.values())
2938 parameters_ex = ()
2939 arg_vals = iter(args)
2940
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002941 while True:
2942 # Let's iterate through the positional arguments and corresponding
2943 # parameters
2944 try:
2945 arg_val = next(arg_vals)
2946 except StopIteration:
2947 # No more positional arguments
2948 try:
2949 param = next(parameters)
2950 except StopIteration:
2951 # No more parameters. That's it. Just need to check that
2952 # we have no `kwargs` after this while loop
2953 break
2954 else:
2955 if param.kind == _VAR_POSITIONAL:
2956 # That's OK, just empty *args. Let's start parsing
2957 # kwargs
2958 break
2959 elif param.name in kwargs:
2960 if param.kind == _POSITIONAL_ONLY:
2961 msg = '{arg!r} parameter is positional only, ' \
2962 'but was passed as a keyword'
2963 msg = msg.format(arg=param.name)
2964 raise TypeError(msg) from None
2965 parameters_ex = (param,)
2966 break
2967 elif (param.kind == _VAR_KEYWORD or
2968 param.default is not _empty):
2969 # That's fine too - we have a default value for this
2970 # parameter. So, lets start parsing `kwargs`, starting
2971 # with the current parameter
2972 parameters_ex = (param,)
2973 break
2974 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002975 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
2976 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002977 if partial:
2978 parameters_ex = (param,)
2979 break
2980 else:
Yury Selivanov86872752015-05-19 00:27:49 -04002981 msg = 'missing a required argument: {arg!r}'
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002982 msg = msg.format(arg=param.name)
2983 raise TypeError(msg) from None
2984 else:
2985 # We have a positional argument to process
2986 try:
2987 param = next(parameters)
2988 except StopIteration:
2989 raise TypeError('too many positional arguments') from None
2990 else:
2991 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
2992 # Looks like we have no parameter for this positional
2993 # argument
Yury Selivanov86872752015-05-19 00:27:49 -04002994 raise TypeError(
2995 'too many positional arguments') from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002996
2997 if param.kind == _VAR_POSITIONAL:
2998 # We have an '*args'-like argument, let's fill it with
2999 # all positional arguments we have left and move on to
3000 # the next phase
3001 values = [arg_val]
3002 values.extend(arg_vals)
3003 arguments[param.name] = tuple(values)
3004 break
3005
Pablo Galindof3ef06a2019-10-15 12:40:02 +01003006 if param.name in kwargs and param.kind != _POSITIONAL_ONLY:
Yury Selivanov86872752015-05-19 00:27:49 -04003007 raise TypeError(
3008 'multiple values for argument {arg!r}'.format(
3009 arg=param.name)) from None
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003010
3011 arguments[param.name] = arg_val
3012
3013 # Now, we iterate through the remaining parameters to process
3014 # keyword arguments
3015 kwargs_param = None
3016 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003017 if param.kind == _VAR_KEYWORD:
3018 # Memorize that we have a '**kwargs'-like parameter
3019 kwargs_param = param
3020 continue
3021
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05003022 if param.kind == _VAR_POSITIONAL:
3023 # Named arguments don't refer to '*args'-like parameters.
3024 # We only arrive here if the positional arguments ended
3025 # before reaching the last parameter before *args.
3026 continue
3027
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003028 param_name = param.name
3029 try:
3030 arg_val = kwargs.pop(param_name)
3031 except KeyError:
3032 # We have no value for this parameter. It's fine though,
3033 # if it has a default value, or it is an '*args'-like
3034 # parameter, left alone by the processing of positional
3035 # arguments.
3036 if (not partial and param.kind != _VAR_POSITIONAL and
3037 param.default is _empty):
Yury Selivanov86872752015-05-19 00:27:49 -04003038 raise TypeError('missing a required argument: {arg!r}'. \
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003039 format(arg=param_name)) from None
3040
3041 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05003042 if param.kind == _POSITIONAL_ONLY:
3043 # This should never happen in case of a properly built
3044 # Signature object (but let's have this check here
3045 # to ensure correct behaviour just in case)
3046 raise TypeError('{arg!r} parameter is positional only, '
3047 'but was passed as a keyword'. \
3048 format(arg=param.name))
3049
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003050 arguments[param_name] = arg_val
3051
3052 if kwargs:
3053 if kwargs_param is not None:
3054 # Process our '**kwargs'-like parameter
3055 arguments[kwargs_param.name] = kwargs
3056 else:
Yury Selivanov86872752015-05-19 00:27:49 -04003057 raise TypeError(
3058 'got an unexpected keyword argument {arg!r}'.format(
3059 arg=next(iter(kwargs))))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003060
3061 return self._bound_arguments_cls(self, arguments)
3062
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003063 def bind(self, /, *args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003064 """Get a BoundArguments object, that maps the passed `args`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003065 and `kwargs` to the function's signature. Raises `TypeError`
3066 if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003067 """
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003068 return self._bind(args, kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003069
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003070 def bind_partial(self, /, *args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003071 """Get a BoundArguments object, that partially maps the
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003072 passed `args` and `kwargs` to the function's signature.
3073 Raises `TypeError` if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003074 """
Serhiy Storchaka2085bd02019-06-01 11:00:15 +03003075 return self._bind(args, kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003076
Yury Selivanova5d63dd2014-03-27 11:31:43 -04003077 def __reduce__(self):
3078 return (type(self),
3079 (tuple(self._parameters.values()),),
3080 {'_return_annotation': self._return_annotation})
3081
3082 def __setstate__(self, state):
3083 self._return_annotation = state['_return_annotation']
3084
Yury Selivanov374375d2014-03-27 12:41:53 -04003085 def __repr__(self):
Yury Selivanovf229bc52015-05-15 12:53:56 -04003086 return '<{} {}>'.format(self.__class__.__name__, self)
Yury Selivanov374375d2014-03-27 12:41:53 -04003087
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003088 def __str__(self):
3089 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05003090 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003091 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05003092 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003093 formatted = str(param)
3094
3095 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05003096
3097 if kind == _POSITIONAL_ONLY:
3098 render_pos_only_separator = True
3099 elif render_pos_only_separator:
3100 # It's not a positional-only parameter, and the flag
3101 # is set to 'True' (there were pos-only params before.)
3102 result.append('/')
3103 render_pos_only_separator = False
3104
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003105 if kind == _VAR_POSITIONAL:
3106 # OK, we have an '*args'-like parameter, so we won't need
3107 # a '*' to separate keyword-only arguments
3108 render_kw_only_separator = False
3109 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
3110 # We have a keyword-only parameter to render and we haven't
3111 # rendered an '*args'-like parameter before, so add a '*'
3112 # separator to the parameters list ("foo(arg1, *, arg2)" case)
3113 result.append('*')
3114 # This condition should be only triggered once, so
3115 # reset the flag
3116 render_kw_only_separator = False
3117
3118 result.append(formatted)
3119
Yury Selivanov2393dca2014-01-27 15:07:58 -05003120 if render_pos_only_separator:
3121 # There were only positional-only parameters, hence the
3122 # flag was not reset to 'False'
3123 result.append('/')
3124
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07003125 rendered = '({})'.format(', '.join(result))
3126
3127 if self.return_annotation is not _empty:
3128 anno = formatannotation(self.return_annotation)
3129 rendered += ' -> {}'.format(anno)
3130
3131 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003132
Yury Selivanovda396452014-03-27 12:09:24 -04003133
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04003134def signature(obj, *, follow_wrapped=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04003135 """Get a signature object for the passed callable."""
Yury Selivanovbcd4fc12015-05-20 14:30:08 -04003136 return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
Yury Selivanovda396452014-03-27 12:09:24 -04003137
3138
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003139def _main():
3140 """ Logic for inspecting an object given at command line """
3141 import argparse
3142 import importlib
3143
3144 parser = argparse.ArgumentParser()
3145 parser.add_argument(
3146 'object',
3147 help="The object to be analysed. "
3148 "It supports the 'module:qualname' syntax")
3149 parser.add_argument(
3150 '-d', '--details', action='store_true',
3151 help='Display info about the module rather than its source code')
3152
3153 args = parser.parse_args()
3154
3155 target = args.object
3156 mod_name, has_attrs, attrs = target.partition(":")
3157 try:
3158 obj = module = importlib.import_module(mod_name)
3159 except Exception as exc:
3160 msg = "Failed to import {} ({}: {})".format(mod_name,
3161 type(exc).__name__,
3162 exc)
3163 print(msg, file=sys.stderr)
Alan Yeee3c59a72019-09-09 07:15:43 -07003164 sys.exit(2)
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003165
3166 if has_attrs:
3167 parts = attrs.split(".")
3168 obj = module
3169 for part in parts:
3170 obj = getattr(obj, part)
3171
3172 if module.__name__ in sys.builtin_module_names:
3173 print("Can't get info for builtin modules.", file=sys.stderr)
Alan Yeee3c59a72019-09-09 07:15:43 -07003174 sys.exit(1)
Nick Coghlanf94a16b2013-09-22 22:46:49 +10003175
3176 if args.details:
3177 print('Target: {}'.format(target))
3178 print('Origin: {}'.format(getsourcefile(module)))
3179 print('Cached: {}'.format(module.__cached__))
3180 if obj is module:
3181 print('Loader: {}'.format(repr(module.__loader__)))
3182 if hasattr(module, '__path__'):
3183 print('Submodule search path: {}'.format(module.__path__))
3184 else:
3185 try:
3186 __, lineno = findsource(obj)
3187 except Exception:
3188 pass
3189 else:
3190 print('Line: {}'.format(lineno))
3191
3192 print('\n')
3193 else:
3194 print(getsource(obj))
3195
3196
3197if __name__ == "__main__":
3198 _main()