Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1 | """Get useful information from live Python objects. |
| 2 | |
| 3 | This module encapsulates the interface provided by the internal special |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 4 | attributes (co_*, im_*, tb_*, etc.) in a friendlier fashion. |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 5 | It also provides some help for examining source code and class layout. |
| 6 | |
| 7 | Here are some of the useful functions provided by this module: |
| 8 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 9 | ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(), |
| 10 | isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(), |
| 11 | isroutine() - check object types |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 12 | 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 Peksag | fa3922c | 2015-07-31 04:11:29 +0300 | [diff] [blame] | 19 | getargvalues(), getcallargs() - get info about function arguments |
Yury Selivanov | 0cf3ed6 | 2014-04-01 10:17:08 -0400 | [diff] [blame] | 20 | getfullargspec() - same, with support for Python 3 features |
Matthias Bussonnier | 46c5cd0 | 2018-06-11 22:08:16 +0200 | [diff] [blame] | 21 | formatargvalues() - format an argument spec |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 22 | 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 Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 25 | |
| 26 | signature() - get a Signature object for the callable |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 27 | """ |
| 28 | |
| 29 | # This module is in the public domain. No warranties. |
| 30 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 31 | __author__ = ('Ka-Ping Yee <ping@lfw.org>', |
| 32 | 'Yury Selivanov <yselivanov@sprymix.com>') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 33 | |
Nate | fcfe80e | 2017-04-24 10:06:15 -0700 | [diff] [blame] | 34 | import abc |
Karthikeyan Singaravelan | 696136b | 2020-04-18 21:49:32 +0530 | [diff] [blame] | 35 | import ast |
Antoine Pitrou | a8723a0 | 2015-04-15 00:41:29 +0200 | [diff] [blame] | 36 | import dis |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 37 | import collections.abc |
Yury Selivanov | 21e83a5 | 2014-03-27 11:23:13 -0400 | [diff] [blame] | 38 | import enum |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 39 | import importlib.machinery |
| 40 | import itertools |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 41 | import linecache |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 42 | import os |
| 43 | import re |
| 44 | import sys |
| 45 | import tokenize |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 46 | import token |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 47 | import types |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 48 | import typing |
Brett Cannon | 2b88fcf | 2012-06-02 22:28:42 -0400 | [diff] [blame] | 49 | import warnings |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 50 | import functools |
Nick Coghlan | 2f92e54 | 2012-06-23 19:39:55 +1000 | [diff] [blame] | 51 | import builtins |
Raymond Hettinger | a1a992c | 2005-03-11 06:46:45 +0000 | [diff] [blame] | 52 | from operator import attrgetter |
Inada Naoki | 2110551 | 2020-03-02 18:54:49 +0900 | [diff] [blame] | 53 | from collections import namedtuple, OrderedDict |
Nick Coghlan | 09c8123 | 2010-08-17 10:18:16 +0000 | [diff] [blame] | 54 | |
| 55 | # Create constants for the compiler flags in Include/code.h |
Antoine Pitrou | a8723a0 | 2015-04-15 00:41:29 +0200 | [diff] [blame] | 56 | # We try to get them from dis to avoid duplication |
| 57 | mod_dict = globals() |
| 58 | for k, v in dis.COMPILER_FLAG_NAMES.items(): |
| 59 | mod_dict["CO_" + v] = k |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 60 | |
Christian Heimes | be5b30b | 2008-03-03 19:18:51 +0000 | [diff] [blame] | 61 | # See Include/object.h |
| 62 | TPFLAGS_IS_ABSTRACT = 1 << 20 |
| 63 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 64 | # ----------------------------------------------------------- type-checking |
| 65 | def ismodule(object): |
| 66 | """Return true if the object is a module. |
| 67 | |
| 68 | Module objects provide these attributes: |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 69 | __cached__ pathname to byte compiled file |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 70 | __doc__ documentation string |
| 71 | __file__ filename (missing for built-in modules)""" |
Tim Peters | 28bc59f | 2001-09-16 08:40:16 +0000 | [diff] [blame] | 72 | return isinstance(object, types.ModuleType) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 73 | |
| 74 | def 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 Peterson | c465600 | 2009-01-17 22:41:18 +0000 | [diff] [blame] | 80 | return isinstance(object, type) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 81 | |
| 82 | def 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 Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 88 | __func__ function object containing implementation of method |
| 89 | __self__ instance to which this method is bound""" |
Tim Peters | 28bc59f | 2001-09-16 08:40:16 +0000 | [diff] [blame] | 90 | return isinstance(object, types.MethodType) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 91 | |
Tim Peters | 536d226 | 2001-09-20 05:13:38 +0000 | [diff] [blame] | 92 | def ismethoddescriptor(object): |
Tim Peters | f1d90b9 | 2001-09-20 05:47:55 +0000 | [diff] [blame] | 93 | """Return true if the object is a method descriptor. |
| 94 | |
| 95 | But not if ismethod() or isclass() or isfunction() are true. |
Tim Peters | 536d226 | 2001-09-20 05:13:38 +0000 | [diff] [blame] | 96 | |
| 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 Peters | f1d90b9 | 2001-09-20 05:47:55 +0000 | [diff] [blame] | 102 | 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 Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 105 | __func__ attribute (etc) when an object passes ismethod().""" |
Antoine Pitrou | 86a8a9a | 2011-12-21 09:57:40 +0100 | [diff] [blame] | 106 | 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 Peters | 536d226 | 2001-09-20 05:13:38 +0000 | [diff] [blame] | 111 | |
Martin v. Löwis | e59e2ba | 2003-05-03 09:09:02 +0000 | [diff] [blame] | 112 | def isdatadescriptor(object): |
| 113 | """Return true if the object is a data descriptor. |
| 114 | |
Aaron Hall, MBA | 4054b17 | 2018-05-20 19:46:42 -0400 | [diff] [blame] | 115 | Data descriptors have a __set__ or a __delete__ attribute. Examples are |
Martin v. Löwis | e59e2ba | 2003-05-03 09:09:02 +0000 | [diff] [blame] | 116 | 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 Pitrou | 86a8a9a | 2011-12-21 09:57:40 +0100 | [diff] [blame] | 120 | if isclass(object) or ismethod(object) or isfunction(object): |
| 121 | # mutual exclusion |
| 122 | return False |
| 123 | tp = type(object) |
Aaron Hall, MBA | 4054b17 | 2018-05-20 19:46:42 -0400 | [diff] [blame] | 124 | return hasattr(tp, "__set__") or hasattr(tp, "__delete__") |
Martin v. Löwis | e59e2ba | 2003-05-03 09:09:02 +0000 | [diff] [blame] | 125 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 126 | if 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) |
| 134 | else: |
| 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 | |
| 143 | if 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) |
| 151 | else: |
| 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 Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 160 | def 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 Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 166 | __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 Peters | 28bc59f | 2001-09-16 08:40:16 +0000 | [diff] [blame] | 171 | return isinstance(object, types.FunctionType) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 172 | |
Jeroen Demeyer | fcef60f | 2019-04-02 16:03:42 +0200 | [diff] [blame] | 173 | def _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 Galindo | 7cd2543 | 2018-10-26 12:19:14 +0100 | [diff] [blame] | 184 | def isgeneratorfunction(obj): |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 185 | """Return true if the object is a user-defined generator function. |
| 186 | |
Martin Panter | 0f0eac4 | 2016-09-07 11:04:41 +0000 | [diff] [blame] | 187 | Generator function objects provide the same attributes as functions. |
| 188 | See help(isfunction) for a list of attributes.""" |
Jeroen Demeyer | fcef60f | 2019-04-02 16:03:42 +0200 | [diff] [blame] | 189 | return _has_code_flag(obj, CO_GENERATOR) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 190 | |
Pablo Galindo | 7cd2543 | 2018-10-26 12:19:14 +0100 | [diff] [blame] | 191 | def iscoroutinefunction(obj): |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 192 | """Return true if the object is a coroutine function. |
| 193 | |
Yury Selivanov | 4778e13 | 2016-11-08 12:23:09 -0500 | [diff] [blame] | 194 | Coroutine functions are defined with "async def" syntax. |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 195 | """ |
Jeroen Demeyer | fcef60f | 2019-04-02 16:03:42 +0200 | [diff] [blame] | 196 | return _has_code_flag(obj, CO_COROUTINE) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 197 | |
Pablo Galindo | 7cd2543 | 2018-10-26 12:19:14 +0100 | [diff] [blame] | 198 | def isasyncgenfunction(obj): |
Yury Selivanov | 4778e13 | 2016-11-08 12:23:09 -0500 | [diff] [blame] | 199 | """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 Demeyer | fcef60f | 2019-04-02 16:03:42 +0200 | [diff] [blame] | 204 | return _has_code_flag(obj, CO_ASYNC_GENERATOR) |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 205 | |
| 206 | def isasyncgen(object): |
Yury Selivanov | 4778e13 | 2016-11-08 12:23:09 -0500 | [diff] [blame] | 207 | """Return true if the object is an asynchronous generator.""" |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 208 | return isinstance(object, types.AsyncGeneratorType) |
| 209 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 210 | def isgenerator(object): |
| 211 | """Return true if the object is a generator. |
| 212 | |
| 213 | Generator objects provide these attributes: |
Ezio Melotti | 30b9d5d | 2013-08-17 15:50:46 +0300 | [diff] [blame] | 214 | __iter__ defined to support iteration over container |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 215 | 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 Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 225 | return isinstance(object, types.GeneratorType) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 226 | |
| 227 | def iscoroutine(object): |
| 228 | """Return true if the object is a coroutine.""" |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 229 | return isinstance(object, types.CoroutineType) |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 230 | |
Yury Selivanov | fdbeb2b | 2015-07-03 13:11:35 -0400 | [diff] [blame] | 231 | def isawaitable(object): |
Yury Selivanov | c0215df | 2016-11-08 19:57:44 -0500 | [diff] [blame] | 232 | """Return true if object can be passed to an ``await`` expression.""" |
Yury Selivanov | fdbeb2b | 2015-07-03 13:11:35 -0400 | [diff] [blame] | 233 | return (isinstance(object, types.CoroutineType) or |
| 234 | isinstance(object, types.GeneratorType) and |
Yury Selivanov | c0215df | 2016-11-08 19:57:44 -0500 | [diff] [blame] | 235 | bool(object.gi_code.co_flags & CO_ITERABLE_COROUTINE) or |
Yury Selivanov | fdbeb2b | 2015-07-03 13:11:35 -0400 | [diff] [blame] | 236 | isinstance(object, collections.abc.Awaitable)) |
| 237 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 238 | def 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 Peters | 28bc59f | 2001-09-16 08:40:16 +0000 | [diff] [blame] | 246 | return isinstance(object, types.TracebackType) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 247 | |
| 248 | def 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 Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 255 | 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 Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 259 | f_trace tracing function for this frame, or None""" |
Tim Peters | 28bc59f | 2001-09-16 08:40:16 +0000 | [diff] [blame] | 260 | return isinstance(object, types.FrameType) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 261 | |
| 262 | def iscode(object): |
| 263 | """Return true if the object is a code object. |
| 264 | |
| 265 | Code objects provide these attributes: |
Xiang Zhang | a6902e6 | 2017-04-13 10:38:28 +0800 | [diff] [blame] | 266 | 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 Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 277 | co_posonlyargcount number of positional only arguments |
Xiang Zhang | a6902e6 | 2017-04-13 10:38:28 +0800 | [diff] [blame] | 278 | 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 Peters | 28bc59f | 2001-09-16 08:40:16 +0000 | [diff] [blame] | 285 | return isinstance(object, types.CodeType) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 286 | |
| 287 | def 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 Peters | 28bc59f | 2001-09-16 08:40:16 +0000 | [diff] [blame] | 294 | return isinstance(object, types.BuiltinFunctionType) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 295 | |
| 296 | def isroutine(object): |
| 297 | """Return true if the object is any kind of function or method.""" |
Tim Peters | 536d226 | 2001-09-20 05:13:38 +0000 | [diff] [blame] | 298 | return (isbuiltin(object) |
| 299 | or isfunction(object) |
| 300 | or ismethod(object) |
| 301 | or ismethoddescriptor(object)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 302 | |
Christian Heimes | be5b30b | 2008-03-03 19:18:51 +0000 | [diff] [blame] | 303 | def isabstract(object): |
| 304 | """Return true if the object is an abstract base class (ABC).""" |
Nate | fcfe80e | 2017-04-24 10:06:15 -0700 | [diff] [blame] | 305 | 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 Heimes | be5b30b | 2008-03-03 19:18:51 +0000 | [diff] [blame] | 326 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 327 | def 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 Pitrou | 86a8a9a | 2011-12-21 09:57:40 +0100 | [diff] [blame] | 330 | if isclass(object): |
| 331 | mro = (object,) + getmro(object) |
| 332 | else: |
| 333 | mro = () |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 334 | results = [] |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 335 | processed = set() |
| 336 | names = dir(object) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 337 | # :dd any DynamicClassAttributes to the list of names if object is a class; |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 338 | # this may result in duplicate entries if, for example, a virtual |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 339 | # attribute with the same name as a DynamicClassAttribute exists |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 340 | 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 Furman | 63c141c | 2013-10-18 00:27:39 -0700 | [diff] [blame] | 348 | # 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 Pitrou | 86a8a9a | 2011-12-21 09:57:40 +0100 | [diff] [blame] | 364 | continue |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 365 | if not predicate or predicate(value): |
| 366 | results.append((key, value)) |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 367 | processed.add(key) |
| 368 | results.sort(key=lambda pair: pair[0]) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 369 | return results |
| 370 | |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 371 | Attribute = namedtuple('Attribute', 'name kind defining_class object') |
| 372 | |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 373 | def 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 Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 385 | 'method' any other flavor of method or descriptor |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 386 | 'data' not a method |
| 387 | |
| 388 | 2. The class which defined this attribute (a class). |
| 389 | |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 390 | 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 Furman | 668dede | 2013-09-14 18:53:26 -0700 | [diff] [blame] | 394 | |
| 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 Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 397 | defined. Any items whose home class cannot be discovered are skipped. |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 398 | """ |
| 399 | |
| 400 | mro = getmro(cls) |
Ethan Furman | 668dede | 2013-09-14 18:53:26 -0700 | [diff] [blame] | 401 | metamro = getmro(type(cls)) # for attributes stored in the metaclass |
Jon Dufresne | 3972628 | 2017-05-18 07:35:54 -0700 | [diff] [blame] | 402 | metamro = tuple(cls for cls in metamro if cls not in (type, object)) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 403 | class_bases = (cls,) + mro |
| 404 | all_bases = class_bases + metamro |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 405 | names = dir(cls) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 406 | # :dd any DynamicClassAttributes to the list of names; |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 407 | # this may result in duplicate entries if, for example, a virtual |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 408 | # attribute with the same name as a DynamicClassAttribute exists. |
Ethan Furman | 63c141c | 2013-10-18 00:27:39 -0700 | [diff] [blame] | 409 | for base in mro: |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 410 | for k, v in base.__dict__.items(): |
Ethan Furman | c314e60 | 2021-01-12 23:47:57 -0800 | [diff] [blame] | 411 | if isinstance(v, types.DynamicClassAttribute) and v.fget is not None: |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 412 | names.append(k) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 413 | result = [] |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 414 | processed = set() |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 415 | |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 416 | for name in names: |
Antoine Pitrou | 86a8a9a | 2011-12-21 09:57:40 +0100 | [diff] [blame] | 417 | # Get the object associated with the name, and where it was defined. |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 418 | # 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 Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 421 | # For DynamicClassAttributes on the second pass we only look in the |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 422 | # class's dict. |
| 423 | # |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 424 | # Getting an obj from the __dict__ sometimes reveals more than |
| 425 | # using getattr. Static and class methods are dramatic examples. |
Antoine Pitrou | 86a8a9a | 2011-12-21 09:57:40 +0100 | [diff] [blame] | 426 | homecls = None |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 427 | get_obj = None |
| 428 | dict_obj = None |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 429 | if name not in processed: |
| 430 | try: |
Ethan Furman | a8b0707 | 2013-10-18 01:22:08 -0700 | [diff] [blame] | 431 | if name == '__dict__': |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 432 | raise Exception("__dict__ is special, don't want the proxy") |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 433 | get_obj = getattr(cls, name) |
| 434 | except Exception as exc: |
| 435 | pass |
| 436 | else: |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 437 | homecls = getattr(get_obj, "__objclass__", homecls) |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 438 | if homecls not in class_bases: |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 439 | # if the resulting object does not live somewhere in the |
Ethan Furman | 63c141c | 2013-10-18 00:27:39 -0700 | [diff] [blame] | 440 | # mro, drop it and search the mro manually |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 441 | homecls = None |
Ethan Furman | 63c141c | 2013-10-18 00:27:39 -0700 | [diff] [blame] | 442 | last_cls = None |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 443 | # first look in the classes |
| 444 | for srch_cls in class_bases: |
Ethan Furman | 63c141c | 2013-10-18 00:27:39 -0700 | [diff] [blame] | 445 | srch_obj = getattr(srch_cls, name, None) |
Yury Selivanov | bf341fb | 2015-05-21 15:41:57 -0400 | [diff] [blame] | 446 | if srch_obj is get_obj: |
Ethan Furman | 63c141c | 2013-10-18 00:27:39 -0700 | [diff] [blame] | 447 | last_cls = srch_cls |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 448 | # 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 Selivanov | bf341fb | 2015-05-21 15:41:57 -0400 | [diff] [blame] | 454 | if srch_obj is get_obj: |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 455 | last_cls = srch_cls |
Ethan Furman | 63c141c | 2013-10-18 00:27:39 -0700 | [diff] [blame] | 456 | if last_cls is not None: |
| 457 | homecls = last_cls |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 458 | for base in all_bases: |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 459 | if name in base.__dict__: |
| 460 | dict_obj = base.__dict__[name] |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 461 | if homecls not in metamro: |
| 462 | homecls = base |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 463 | break |
Ethan Furman | 63c141c | 2013-10-18 00:27:39 -0700 | [diff] [blame] | 464 | 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 Selivanov | bf341fb | 2015-05-21 15:41:57 -0400 | [diff] [blame] | 468 | obj = get_obj if get_obj is not None else dict_obj |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 469 | # Classify the object or its descriptor. |
Serhiy Storchaka | 3327a2d | 2017-12-15 14:13:41 +0200 | [diff] [blame] | 470 | if isinstance(dict_obj, (staticmethod, types.BuiltinMethodType)): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 471 | kind = "static method" |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 472 | obj = dict_obj |
Serhiy Storchaka | 3327a2d | 2017-12-15 14:13:41 +0200 | [diff] [blame] | 473 | elif isinstance(dict_obj, (classmethod, types.ClassMethodDescriptorType)): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 474 | kind = "class method" |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 475 | obj = dict_obj |
| 476 | elif isinstance(dict_obj, property): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 477 | kind = "property" |
Ethan Furman | b0c84cd | 2013-10-20 22:37:39 -0700 | [diff] [blame] | 478 | obj = dict_obj |
Yury Selivanov | 0860a0b | 2014-01-31 14:28:44 -0500 | [diff] [blame] | 479 | elif isroutine(obj): |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 480 | kind = "method" |
Antoine Pitrou | 86a8a9a | 2011-12-21 09:57:40 +0100 | [diff] [blame] | 481 | else: |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 482 | kind = "data" |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 483 | result.append(Attribute(name, kind, homecls, obj)) |
Ethan Furman | e03ea37 | 2013-09-25 07:14:41 -0700 | [diff] [blame] | 484 | processed.add(name) |
Tim Peters | 13b49d3 | 2001-09-23 02:00:29 +0000 | [diff] [blame] | 485 | return result |
| 486 | |
Tim Peters | e0b2d7a | 2001-09-22 06:10:55 +0000 | [diff] [blame] | 487 | # ----------------------------------------------------------- class helpers |
Tim Peters | e0b2d7a | 2001-09-22 06:10:55 +0000 | [diff] [blame] | 488 | |
| 489 | def getmro(cls): |
| 490 | "Return tuple of base classes (including cls) in method resolution order." |
Benjamin Peterson | b82c8e5 | 2010-11-04 00:38:49 +0000 | [diff] [blame] | 491 | return cls.__mro__ |
Tim Peters | e0b2d7a | 2001-09-22 06:10:55 +0000 | [diff] [blame] | 492 | |
Nick Coghlan | e8c45d6 | 2013-07-28 20:00:01 +1000 | [diff] [blame] | 493 | # -------------------------------------------------------- function helpers |
| 494 | |
| 495 | def 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 Kluyver | f9169ce | 2017-05-23 04:27:52 +0100 | [diff] [blame] | 518 | # 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 Coghlan | e8c45d6 | 2013-07-28 20:00:01 +1000 | [diff] [blame] | 522 | while _is_wrapper(func): |
| 523 | func = func.__wrapped__ |
| 524 | id_func = id(func) |
Thomas Kluyver | f9169ce | 2017-05-23 04:27:52 +0100 | [diff] [blame] | 525 | if (id_func in memo) or (len(memo) >= recursion_limit): |
Nick Coghlan | e8c45d6 | 2013-07-28 20:00:01 +1000 | [diff] [blame] | 526 | raise ValueError('wrapper loop when unwrapping {!r}'.format(f)) |
Thomas Kluyver | f9169ce | 2017-05-23 04:27:52 +0100 | [diff] [blame] | 527 | memo[id_func] = func |
Nick Coghlan | e8c45d6 | 2013-07-28 20:00:01 +1000 | [diff] [blame] | 528 | return func |
| 529 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 530 | # -------------------------------------------------- source code extraction |
| 531 | def indentsize(line): |
| 532 | """Return the indent size, in spaces, at the start of a line of text.""" |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 533 | expline = line.expandtabs() |
| 534 | return len(expline) - len(expline.lstrip()) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 535 | |
Serhiy Storchaka | 5cf2b72 | 2015-04-03 22:38:53 +0300 | [diff] [blame] | 536 | def _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 | |
| 546 | def _finddoc(obj): |
Serhiy Storchaka | 08b47c3 | 2020-05-18 20:25:07 +0300 | [diff] [blame] | 547 | 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 Storchaka | 5cf2b72 | 2015-04-03 22:38:53 +0300 | [diff] [blame] | 558 | 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 Storchaka | ac4bdcc | 2015-10-29 08:15:50 +0200 | [diff] [blame] | 581 | # 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 Storchaka | 5cf2b72 | 2015-04-03 22:38:53 +0300 | [diff] [blame] | 588 | 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 Hettinger | d1e768a | 2019-03-25 13:01:13 -0700 | [diff] [blame] | 593 | if ismemberdescriptor(obj): |
| 594 | slots = getattr(cls, '__slots__', None) |
| 595 | if isinstance(slots, dict) and name in slots: |
| 596 | return slots[name] |
Serhiy Storchaka | 5cf2b72 | 2015-04-03 22:38:53 +0300 | [diff] [blame] | 597 | else: |
| 598 | return None |
Serhiy Storchaka | 5cf2b72 | 2015-04-03 22:38:53 +0300 | [diff] [blame] | 599 | for base in cls.__mro__: |
| 600 | try: |
Serhiy Storchaka | 08b47c3 | 2020-05-18 20:25:07 +0300 | [diff] [blame] | 601 | doc = getattr(base, name).__doc__ |
Serhiy Storchaka | 5cf2b72 | 2015-04-03 22:38:53 +0300 | [diff] [blame] | 602 | except AttributeError: |
| 603 | continue |
| 604 | if doc is not None: |
| 605 | return doc |
| 606 | return None |
| 607 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 608 | def 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 Storchaka | 08b47c3 | 2020-05-18 20:25:07 +0300 | [diff] [blame] | 614 | try: |
| 615 | doc = object.__doc__ |
| 616 | except AttributeError: |
| 617 | return None |
Serhiy Storchaka | 5cf2b72 | 2015-04-03 22:38:53 +0300 | [diff] [blame] | 618 | if doc is None: |
| 619 | try: |
| 620 | doc = _finddoc(object) |
| 621 | except (AttributeError, TypeError): |
| 622 | return None |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 623 | if not isinstance(doc, str): |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 624 | return None |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 625 | return cleandoc(doc) |
| 626 | |
| 627 | def 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 Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 632 | try: |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 633 | lines = doc.expandtabs().split('\n') |
Tim Peters | 2400831 | 2002-03-17 18:56:20 +0000 | [diff] [blame] | 634 | except UnicodeError: |
| 635 | return None |
| 636 | else: |
Ka-Ping Yee | a59ef7b | 2002-11-30 03:53:15 +0000 | [diff] [blame] | 637 | # Find minimum indentation of any non-blank lines after first line. |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 638 | margin = sys.maxsize |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 639 | for line in lines[1:]: |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 640 | content = len(line.lstrip()) |
Ka-Ping Yee | a59ef7b | 2002-11-30 03:53:15 +0000 | [diff] [blame] | 641 | 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 Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 647 | if margin < sys.maxsize: |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 648 | for i in range(1, len(lines)): lines[i] = lines[i][margin:] |
Ka-Ping Yee | a59ef7b | 2002-11-30 03:53:15 +0000 | [diff] [blame] | 649 | # 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 Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 654 | return '\n'.join(lines) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 655 | |
| 656 | def getfile(object): |
Ka-Ping Yee | c113c24 | 2001-03-02 02:08:53 +0000 | [diff] [blame] | 657 | """Work out which source or compiled file an object was defined in.""" |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 658 | if ismodule(object): |
Jason R. Coombs | b9650a0 | 2018-03-05 18:29:08 -0500 | [diff] [blame] | 659 | if getattr(object, '__file__', None): |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 660 | return object.__file__ |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 661 | raise TypeError('{!r} is a built-in module'.format(object)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 662 | if isclass(object): |
Yury Selivanov | 2eed8b7 | 2014-01-27 13:24:56 -0500 | [diff] [blame] | 663 | if hasattr(object, '__module__'): |
Philipp A | d407d2a | 2019-06-08 14:05:46 +0200 | [diff] [blame] | 664 | module = sys.modules.get(object.__module__) |
| 665 | if getattr(module, '__file__', None): |
| 666 | return module.__file__ |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 667 | raise TypeError('{!r} is a built-in class'.format(object)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 668 | if ismethod(object): |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 669 | object = object.__func__ |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 670 | if isfunction(object): |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 671 | object = object.__code__ |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 672 | 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 Kluyver | e968bc73 | 2017-10-24 13:42:36 +0100 | [diff] [blame] | 678 | raise TypeError('module, class, method, function, traceback, frame, or ' |
| 679 | 'code object was expected, got {}'.format( |
| 680 | type(object).__name__)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 681 | |
Ka-Ping Yee | 4d6fc7f | 2001-04-10 11:43:00 +0000 | [diff] [blame] | 682 | def getmodulename(path): |
| 683 | """Return the module name for a given file, or None.""" |
Nick Coghlan | 76e0770 | 2012-07-18 23:14:57 +1000 | [diff] [blame] | 684 | 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 Yee | 4d6fc7f | 2001-04-10 11:43:00 +0000 | [diff] [blame] | 693 | |
Ka-Ping Yee | c113c24 | 2001-03-02 02:08:53 +0000 | [diff] [blame] | 694 | def getsourcefile(object): |
R. David Murray | a1b3740 | 2010-06-17 02:04:29 +0000 | [diff] [blame] | 695 | """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 Yee | c113c24 | 2001-03-02 02:08:53 +0000 | [diff] [blame] | 698 | filename = getfile(object) |
Brett Cannon | cb66eb0 | 2012-05-11 12:58:42 -0400 | [diff] [blame] | 699 | 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 Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 707 | if os.path.exists(filename): |
| 708 | return filename |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 709 | # only return a non-existent filename if the module has a PEP 302 loader |
Brett Cannon | 825ac38 | 2020-11-06 18:45:56 -0800 | [diff] [blame] | 710 | 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 Yee | c113c24 | 2001-03-02 02:08:53 +0000 | [diff] [blame] | 714 | return filename |
R. David Murray | a1b3740 | 2010-06-17 02:04:29 +0000 | [diff] [blame] | 715 | # or it is in the linecache |
Brett Cannon | 825ac38 | 2020-11-06 18:45:56 -0800 | [diff] [blame] | 716 | elif filename in linecache.cache: |
R. David Murray | a1b3740 | 2010-06-17 02:04:29 +0000 | [diff] [blame] | 717 | return filename |
Ka-Ping Yee | c113c24 | 2001-03-02 02:08:53 +0000 | [diff] [blame] | 718 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 719 | def getabsfile(object, _filename=None): |
Ka-Ping Yee | 4eb0c00 | 2001-03-02 05:50:34 +0000 | [diff] [blame] | 720 | """Return an absolute path to the source or compiled file for an object. |
Ka-Ping Yee | c113c24 | 2001-03-02 02:08:53 +0000 | [diff] [blame] | 721 | |
Ka-Ping Yee | 4eb0c00 | 2001-03-02 05:50:34 +0000 | [diff] [blame] | 722 | The idea is for each object to have a unique origin, so this routine |
| 723 | normalizes the result as much as possible.""" |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 724 | if _filename is None: |
| 725 | _filename = getsourcefile(object) or getfile(object) |
| 726 | return os.path.normcase(os.path.abspath(_filename)) |
Ka-Ping Yee | c113c24 | 2001-03-02 02:08:53 +0000 | [diff] [blame] | 727 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 728 | modulesbyfile = {} |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 729 | _filesbymodname = {} |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 730 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 731 | def getmodule(object, _filename=None): |
Ka-Ping Yee | 4eb0c00 | 2001-03-02 05:50:34 +0000 | [diff] [blame] | 732 | """Return the module an object was defined in, or None if not found.""" |
Ka-Ping Yee | 202c99b | 2001-04-13 09:15:08 +0000 | [diff] [blame] | 733 | if ismodule(object): |
| 734 | return object |
Johannes Gijsbers | 9324526 | 2004-09-11 15:53:22 +0000 | [diff] [blame] | 735 | if hasattr(object, '__module__'): |
Ka-Ping Yee | 8b58b84 | 2001-03-01 13:56:16 +0000 | [diff] [blame] | 736 | return sys.modules.get(object.__module__) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 737 | # 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 Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 741 | try: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 742 | file = getabsfile(object, _filename) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 743 | except TypeError: |
| 744 | return None |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 745 | if file in modulesbyfile: |
Ka-Ping Yee | b38bbbd | 2003-03-28 16:29:50 +0000 | [diff] [blame] | 746 | return sys.modules.get(modulesbyfile[file]) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 747 | # 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. Smith | 85cf1d5 | 2020-03-04 16:45:22 -0800 | [diff] [blame] | 749 | for modname, module in sys.modules.copy().items(): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 750 | if ismodule(module) and hasattr(module, '__file__'): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 751 | 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 756 | f = getabsfile(module) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 757 | # Always map to the name the module knows itself by |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 758 | modulesbyfile[f] = modulesbyfile[ |
| 759 | os.path.realpath(f)] = module.__name__ |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 760 | if file in modulesbyfile: |
Ka-Ping Yee | b38bbbd | 2003-03-28 16:29:50 +0000 | [diff] [blame] | 761 | return sys.modules.get(modulesbyfile[file]) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 762 | # Check the main module |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 763 | main = sys.modules['__main__'] |
Brett Cannon | 4a671fe | 2003-06-15 22:33:28 +0000 | [diff] [blame] | 764 | if not hasattr(object, '__name__'): |
| 765 | return None |
Ka-Ping Yee | 4eb0c00 | 2001-03-02 05:50:34 +0000 | [diff] [blame] | 766 | if hasattr(main, object.__name__): |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 767 | mainobject = getattr(main, object.__name__) |
Ka-Ping Yee | 4eb0c00 | 2001-03-02 05:50:34 +0000 | [diff] [blame] | 768 | if mainobject is object: |
| 769 | return main |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 770 | # Check builtins |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 771 | builtin = sys.modules['builtins'] |
Ka-Ping Yee | 4eb0c00 | 2001-03-02 05:50:34 +0000 | [diff] [blame] | 772 | if hasattr(builtin, object.__name__): |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 773 | builtinobject = getattr(builtin, object.__name__) |
Ka-Ping Yee | 4eb0c00 | 2001-03-02 05:50:34 +0000 | [diff] [blame] | 774 | if builtinobject is object: |
| 775 | return builtin |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 776 | |
Karthikeyan Singaravelan | 696136b | 2020-04-18 21:49:32 +0530 | [diff] [blame] | 777 | |
| 778 | class ClassFoundException(Exception): |
| 779 | pass |
| 780 | |
| 781 | |
| 782 | class _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 Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 813 | def 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 Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 818 | in the file and the line number indexes a line in that list. An OSError |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 819 | is raised if the source code cannot be retrieved.""" |
Benjamin Peterson | 9620cc0 | 2011-06-11 15:53:11 -0500 | [diff] [blame] | 820 | |
Yury Selivanov | ef1e750 | 2014-12-08 16:05:34 -0500 | [diff] [blame] | 821 | 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 Peterson | 9620cc0 | 2011-06-11 15:53:11 -0500 | [diff] [blame] | 832 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 833 | module = getmodule(object, file) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 834 | if module: |
| 835 | lines = linecache.getlines(file, module.__dict__) |
| 836 | else: |
| 837 | lines = linecache.getlines(file) |
Neil Schemenauer | f06f853 | 2002-03-23 23:51:04 +0000 | [diff] [blame] | 838 | if not lines: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 839 | raise OSError('could not get source code') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 840 | |
| 841 | if ismodule(object): |
| 842 | return lines, 0 |
| 843 | |
| 844 | if isclass(object): |
Karthikeyan Singaravelan | 696136b | 2020-04-18 21:49:32 +0530 | [diff] [blame] | 845 | 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 Hylton | ab91902 | 2003-06-27 18:41:20 +0000 | [diff] [blame] | 854 | else: |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 855 | raise OSError('could not find class definition') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 856 | |
| 857 | if ismethod(object): |
Christian Heimes | ff73795 | 2007-11-27 10:40:20 +0000 | [diff] [blame] | 858 | object = object.__func__ |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 859 | if isfunction(object): |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 860 | object = object.__code__ |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 861 | if istraceback(object): |
| 862 | object = object.tb_frame |
| 863 | if isframe(object): |
| 864 | object = object.f_code |
| 865 | if iscode(object): |
Ka-Ping Yee | 4eb0c00 | 2001-03-02 05:50:34 +0000 | [diff] [blame] | 866 | if not hasattr(object, 'co_firstlineno'): |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 867 | raise OSError('could not find function definition') |
Ka-Ping Yee | 4eb0c00 | 2001-03-02 05:50:34 +0000 | [diff] [blame] | 868 | lnum = object.co_firstlineno - 1 |
Yury Selivanov | e4e811d | 2015-07-21 19:01:52 +0300 | [diff] [blame] | 869 | pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') |
Ka-Ping Yee | 4eb0c00 | 2001-03-02 05:50:34 +0000 | [diff] [blame] | 870 | while lnum > 0: |
Irit Katriel | 2e0760b | 2020-12-04 21:22:03 +0000 | [diff] [blame] | 871 | try: |
| 872 | line = lines[lnum] |
| 873 | except IndexError: |
| 874 | raise OSError('lineno is out of bounds') |
| 875 | if pat.match(line): |
| 876 | break |
Ka-Ping Yee | 4eb0c00 | 2001-03-02 05:50:34 +0000 | [diff] [blame] | 877 | lnum = lnum - 1 |
| 878 | return lines, lnum |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 879 | raise OSError('could not find code object') |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 880 | |
| 881 | def getcomments(object): |
Jeremy Hylton | b4c17c8 | 2002-03-28 23:01:56 +0000 | [diff] [blame] | 882 | """Get lines of comments immediately preceding an object's source code. |
| 883 | |
| 884 | Returns None when source can't be found. |
| 885 | """ |
| 886 | try: |
| 887 | lines, lnum = findsource(object) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 888 | except (OSError, TypeError): |
Jeremy Hylton | b4c17c8 | 2002-03-28 23:01:56 +0000 | [diff] [blame] | 889 | return None |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 890 | |
| 891 | if ismodule(object): |
| 892 | # Look for a comment block at the top of the file. |
| 893 | start = 0 |
Ka-Ping Yee | b910efe | 2001-04-12 13:17:17 +0000 | [diff] [blame] | 894 | if lines and lines[0][:2] == '#!': start = 1 |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 895 | while start < len(lines) and lines[start].strip() in ('', '#'): |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 896 | start = start + 1 |
Ka-Ping Yee | b910efe | 2001-04-12 13:17:17 +0000 | [diff] [blame] | 897 | if start < len(lines) and lines[start][:1] == '#': |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 898 | comments = [] |
| 899 | end = start |
| 900 | while end < len(lines) and lines[end][:1] == '#': |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 901 | comments.append(lines[end].expandtabs()) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 902 | end = end + 1 |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 903 | return ''.join(comments) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 904 | |
| 905 | # Look for a preceding block of comments at the same indentation. |
| 906 | elif lnum > 0: |
| 907 | indent = indentsize(lines[lnum]) |
| 908 | end = lnum - 1 |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 909 | if end >= 0 and lines[end].lstrip()[:1] == '#' and \ |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 910 | indentsize(lines[end]) == indent: |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 911 | comments = [lines[end].expandtabs().lstrip()] |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 912 | if end > 0: |
| 913 | end = end - 1 |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 914 | comment = lines[end].expandtabs().lstrip() |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 915 | while comment[:1] == '#' and indentsize(lines[end]) == indent: |
| 916 | comments[:0] = [comment] |
| 917 | end = end - 1 |
| 918 | if end < 0: break |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 919 | comment = lines[end].expandtabs().lstrip() |
| 920 | while comments and comments[0].strip() == '#': |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 921 | comments[:1] = [] |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 922 | while comments and comments[-1].strip() == '#': |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 923 | comments[-1:] = [] |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 924 | return ''.join(comments) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 925 | |
Tim Peters | 4efb6e9 | 2001-06-29 23:51:08 +0000 | [diff] [blame] | 926 | class EndOfBlock(Exception): pass |
| 927 | |
| 928 | class BlockFinder: |
| 929 | """Provide a tokeneater() method to detect the end of a code block.""" |
| 930 | def __init__(self): |
| 931 | self.indent = 0 |
Johannes Gijsbers | a5855d5 | 2005-03-12 16:37:11 +0000 | [diff] [blame] | 932 | self.islambda = False |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 933 | self.started = False |
| 934 | self.passline = False |
Meador Inge | 5b718d7 | 2015-07-23 22:49:37 -0500 | [diff] [blame] | 935 | self.indecorator = False |
| 936 | self.decoratorhasargs = False |
Armin Rigo | dd5c023 | 2005-09-25 11:45:45 +0000 | [diff] [blame] | 937 | self.last = 1 |
Irit Katriel | 6e1eec7 | 2020-12-04 16:45:38 +0000 | [diff] [blame] | 938 | self.body_col0 = None |
Tim Peters | 4efb6e9 | 2001-06-29 23:51:08 +0000 | [diff] [blame] | 939 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 940 | def tokeneater(self, type, token, srowcol, erowcol, line): |
Meador Inge | 5b718d7 | 2015-07-23 22:49:37 -0500 | [diff] [blame] | 941 | if not self.started and not self.indecorator: |
| 942 | # skip any decorators |
| 943 | if token == "@": |
| 944 | self.indecorator = True |
Armin Rigo | dd5c023 | 2005-09-25 11:45:45 +0000 | [diff] [blame] | 945 | # look for the first "def", "class" or "lambda" |
Meador Inge | 5b718d7 | 2015-07-23 22:49:37 -0500 | [diff] [blame] | 946 | elif token in ("def", "class", "lambda"): |
Johannes Gijsbers | a5855d5 | 2005-03-12 16:37:11 +0000 | [diff] [blame] | 947 | if token == "lambda": |
| 948 | self.islambda = True |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 949 | self.started = True |
Armin Rigo | dd5c023 | 2005-09-25 11:45:45 +0000 | [diff] [blame] | 950 | self.passline = True # skip to the end of the line |
Meador Inge | 5b718d7 | 2015-07-23 22:49:37 -0500 | [diff] [blame] | 951 | elif token == "(": |
| 952 | if self.indecorator: |
| 953 | self.decoratorhasargs = True |
| 954 | elif token == ")": |
| 955 | if self.indecorator: |
| 956 | self.indecorator = False |
| 957 | self.decoratorhasargs = False |
Tim Peters | 4efb6e9 | 2001-06-29 23:51:08 +0000 | [diff] [blame] | 958 | elif type == tokenize.NEWLINE: |
Armin Rigo | dd5c023 | 2005-09-25 11:45:45 +0000 | [diff] [blame] | 959 | self.passline = False # stop skipping when a NEWLINE is seen |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 960 | self.last = srowcol[0] |
Armin Rigo | dd5c023 | 2005-09-25 11:45:45 +0000 | [diff] [blame] | 961 | if self.islambda: # lambdas always end at the first NEWLINE |
| 962 | raise EndOfBlock |
Meador Inge | 5b718d7 | 2015-07-23 22:49:37 -0500 | [diff] [blame] | 963 | # hitting a NEWLINE when in a decorator without args |
| 964 | # ends the decorator |
| 965 | if self.indecorator and not self.decoratorhasargs: |
| 966 | self.indecorator = False |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 967 | elif self.passline: |
| 968 | pass |
Tim Peters | 4efb6e9 | 2001-06-29 23:51:08 +0000 | [diff] [blame] | 969 | elif type == tokenize.INDENT: |
Irit Katriel | 6e1eec7 | 2020-12-04 16:45:38 +0000 | [diff] [blame] | 970 | if self.body_col0 is None and self.started: |
| 971 | self.body_col0 = erowcol[1] |
Tim Peters | 4efb6e9 | 2001-06-29 23:51:08 +0000 | [diff] [blame] | 972 | self.indent = self.indent + 1 |
Johannes Gijsbers | 1542f34 | 2004-12-12 16:46:28 +0000 | [diff] [blame] | 973 | self.passline = True |
Tim Peters | 4efb6e9 | 2001-06-29 23:51:08 +0000 | [diff] [blame] | 974 | elif type == tokenize.DEDENT: |
| 975 | self.indent = self.indent - 1 |
Armin Rigo | dd5c023 | 2005-09-25 11:45:45 +0000 | [diff] [blame] | 976 | # the end of matching indent/dedent pairs end a block |
| 977 | # (note that this only works for "def"/"class" blocks, |
| 978 | # not e.g. for "if: else:" or "try: finally:" blocks) |
| 979 | if self.indent <= 0: |
| 980 | raise EndOfBlock |
Irit Katriel | 6e1eec7 | 2020-12-04 16:45:38 +0000 | [diff] [blame] | 981 | elif type == tokenize.COMMENT: |
| 982 | if self.body_col0 is not None and srowcol[1] >= self.body_col0: |
| 983 | # Include comments if indented at least as much as the block |
| 984 | self.last = srowcol[0] |
Armin Rigo | dd5c023 | 2005-09-25 11:45:45 +0000 | [diff] [blame] | 985 | elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL): |
| 986 | # any other token on the same indentation level end the previous |
| 987 | # block as well, except the pseudo-tokens COMMENT and NL. |
| 988 | raise EndOfBlock |
Tim Peters | 4efb6e9 | 2001-06-29 23:51:08 +0000 | [diff] [blame] | 989 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 990 | def getblock(lines): |
| 991 | """Extract the block of code at the top of the given list of lines.""" |
Armin Rigo | dd5c023 | 2005-09-25 11:45:45 +0000 | [diff] [blame] | 992 | blockfinder = BlockFinder() |
Tim Peters | 4efb6e9 | 2001-06-29 23:51:08 +0000 | [diff] [blame] | 993 | try: |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 994 | tokens = tokenize.generate_tokens(iter(lines).__next__) |
| 995 | for _token in tokens: |
| 996 | blockfinder.tokeneater(*_token) |
Armin Rigo | dd5c023 | 2005-09-25 11:45:45 +0000 | [diff] [blame] | 997 | except (EndOfBlock, IndentationError): |
| 998 | pass |
| 999 | return lines[:blockfinder.last] |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1000 | |
| 1001 | def getsourcelines(object): |
| 1002 | """Return a list of source lines and starting line number for an object. |
| 1003 | |
| 1004 | The argument may be a module, class, method, function, traceback, frame, |
| 1005 | or code object. The source code is returned as a list of the lines |
| 1006 | corresponding to the object and the line number indicates where in the |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1007 | original source file the first line of code was found. An OSError is |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1008 | raised if the source code cannot be retrieved.""" |
Yury Selivanov | 081bbf6 | 2014-09-26 17:34:54 -0400 | [diff] [blame] | 1009 | object = unwrap(object) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1010 | lines, lnum = findsource(object) |
| 1011 | |
Vladimir Matveev | 91cb298 | 2018-08-24 07:18:00 -0700 | [diff] [blame] | 1012 | if istraceback(object): |
| 1013 | object = object.tb_frame |
| 1014 | |
| 1015 | # for module or frame that corresponds to module, return all source lines |
| 1016 | if (ismodule(object) or |
| 1017 | (isframe(object) and object.f_code.co_name == "<module>")): |
Meador Inge | 5b718d7 | 2015-07-23 22:49:37 -0500 | [diff] [blame] | 1018 | return lines, 0 |
| 1019 | else: |
| 1020 | return getblock(lines[lnum:]), lnum + 1 |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1021 | |
| 1022 | def getsource(object): |
| 1023 | """Return the text of the source code for an object. |
| 1024 | |
| 1025 | The argument may be a module, class, method, function, traceback, frame, |
| 1026 | or code object. The source code is returned as a single string. An |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1027 | OSError is raised if the source code cannot be retrieved.""" |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1028 | lines, lnum = getsourcelines(object) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1029 | return ''.join(lines) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1030 | |
| 1031 | # --------------------------------------------------- class tree extraction |
| 1032 | def walktree(classes, children, parent): |
| 1033 | """Recursive helper function for getclasstree().""" |
| 1034 | results = [] |
Raymond Hettinger | a1a992c | 2005-03-11 06:46:45 +0000 | [diff] [blame] | 1035 | classes.sort(key=attrgetter('__module__', '__name__')) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1036 | for c in classes: |
| 1037 | results.append((c, c.__bases__)) |
Raymond Hettinger | 54f0222 | 2002-06-01 14:18:47 +0000 | [diff] [blame] | 1038 | if c in children: |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1039 | results.append(walktree(children[c], children, c)) |
| 1040 | return results |
| 1041 | |
Georg Brandl | 5ce83a0 | 2009-06-01 17:23:51 +0000 | [diff] [blame] | 1042 | def getclasstree(classes, unique=False): |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1043 | """Arrange the given list of classes into a hierarchy of nested lists. |
| 1044 | |
| 1045 | Where a nested list appears, it contains classes derived from the class |
| 1046 | whose entry immediately precedes the list. Each entry is a 2-tuple |
| 1047 | containing a class and a tuple of its base classes. If the 'unique' |
| 1048 | argument is true, exactly one entry appears in the returned structure |
| 1049 | for each class in the given list. Otherwise, classes using multiple |
| 1050 | inheritance and their descendants will appear multiple times.""" |
| 1051 | children = {} |
| 1052 | roots = [] |
| 1053 | for c in classes: |
| 1054 | if c.__bases__: |
| 1055 | for parent in c.__bases__: |
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి) | feaefc7 | 2018-02-09 15:29:19 +0530 | [diff] [blame] | 1056 | if parent not in children: |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1057 | children[parent] = [] |
Serhiy Storchaka | 362c1b5 | 2013-09-05 17:14:32 +0300 | [diff] [blame] | 1058 | if c not in children[parent]: |
| 1059 | children[parent].append(c) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1060 | if unique and parent in classes: break |
| 1061 | elif c not in roots: |
| 1062 | roots.append(c) |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 1063 | for parent in children: |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1064 | if parent not in classes: |
| 1065 | roots.append(parent) |
| 1066 | return walktree(roots, children, None) |
| 1067 | |
| 1068 | # ------------------------------------------------ argument list extraction |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1069 | Arguments = namedtuple('Arguments', 'args, varargs, varkw') |
| 1070 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1071 | def getargs(co): |
| 1072 | """Get information about the arguments accepted by a code object. |
| 1073 | |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1074 | Three things are returned: (args, varargs, varkw), where |
Georg Brandl | c1c4bf8 | 2010-10-15 16:07:41 +0000 | [diff] [blame] | 1075 | 'args' is the list of argument names. Keyword-only arguments are |
| 1076 | appended. 'varargs' and 'varkw' are the names of the * and ** |
| 1077 | arguments or None.""" |
Jeremy Hylton | 6496788 | 2003-06-27 18:14:39 +0000 | [diff] [blame] | 1078 | if not iscode(co): |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 1079 | raise TypeError('{!r} is not a code object'.format(co)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1080 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1081 | names = co.co_varnames |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1082 | nargs = co.co_argcount |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1083 | nkwargs = co.co_kwonlyargcount |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 1084 | args = list(names[:nargs]) |
| 1085 | kwonlyargs = list(names[nargs:nargs+nkwargs]) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1086 | step = 0 |
| 1087 | |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1088 | nargs += nkwargs |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1089 | varargs = None |
| 1090 | if co.co_flags & CO_VARARGS: |
| 1091 | varargs = co.co_varnames[nargs] |
| 1092 | nargs = nargs + 1 |
| 1093 | varkw = None |
| 1094 | if co.co_flags & CO_VARKEYWORDS: |
| 1095 | varkw = co.co_varnames[nargs] |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 1096 | return Arguments(args + kwonlyargs, varargs, varkw) |
Yury Selivanov | 37dc2b2 | 2016-01-11 15:15:01 -0500 | [diff] [blame] | 1097 | |
| 1098 | ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults') |
| 1099 | |
| 1100 | def getargspec(func): |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 1101 | """Get the names and default values of a function's parameters. |
Yury Selivanov | 37dc2b2 | 2016-01-11 15:15:01 -0500 | [diff] [blame] | 1102 | |
| 1103 | A tuple of four things is returned: (args, varargs, keywords, defaults). |
| 1104 | 'args' is a list of the argument names, including keyword-only argument names. |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 1105 | 'varargs' and 'keywords' are the names of the * and ** parameters or None. |
| 1106 | 'defaults' is an n-tuple of the default values of the last n parameters. |
Yury Selivanov | 37dc2b2 | 2016-01-11 15:15:01 -0500 | [diff] [blame] | 1107 | |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 1108 | This function is deprecated, as it does not support annotations or |
| 1109 | keyword-only parameters and will raise ValueError if either is present |
| 1110 | on the supplied callable. |
| 1111 | |
| 1112 | For a more structured introspection API, use inspect.signature() instead. |
| 1113 | |
| 1114 | Alternatively, use getfullargspec() for an API with a similar namedtuple |
| 1115 | based interface, but full support for annotations and keyword-only |
| 1116 | parameters. |
Matthias Bussonnier | ded87d8 | 2018-10-19 16:40:45 -0700 | [diff] [blame] | 1117 | |
| 1118 | Deprecated since Python 3.5, use `inspect.getfullargspec()`. |
Yury Selivanov | 37dc2b2 | 2016-01-11 15:15:01 -0500 | [diff] [blame] | 1119 | """ |
Matthias Bussonnier | ded87d8 | 2018-10-19 16:40:45 -0700 | [diff] [blame] | 1120 | warnings.warn("inspect.getargspec() is deprecated since Python 3.0, " |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 1121 | "use inspect.signature() or inspect.getfullargspec()", |
| 1122 | DeprecationWarning, stacklevel=2) |
Pablo Galindo | d5d2b45 | 2019-04-30 02:01:14 +0100 | [diff] [blame] | 1123 | args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \ |
| 1124 | getfullargspec(func) |
| 1125 | if kwonlyargs or ann: |
| 1126 | raise ValueError("Function has keyword-only parameters or annotations" |
| 1127 | ", use inspect.signature() API which can support them") |
Yury Selivanov | 37dc2b2 | 2016-01-11 15:15:01 -0500 | [diff] [blame] | 1128 | return ArgSpec(args, varargs, varkw, defaults) |
| 1129 | |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1130 | FullArgSpec = namedtuple('FullArgSpec', |
Pablo Galindo | d5d2b45 | 2019-04-30 02:01:14 +0100 | [diff] [blame] | 1131 | 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations') |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1132 | |
| 1133 | def getfullargspec(func): |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 1134 | """Get the names and default values of a callable object's parameters. |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1135 | |
Brett Cannon | 504d885 | 2007-09-07 02:12:14 +0000 | [diff] [blame] | 1136 | A tuple of seven things is returned: |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 1137 | (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations). |
| 1138 | 'args' is a list of the parameter names. |
| 1139 | 'varargs' and 'varkw' are the names of the * and ** parameters or None. |
| 1140 | 'defaults' is an n-tuple of the default values of the last n parameters. |
| 1141 | 'kwonlyargs' is a list of keyword-only parameter names. |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1142 | 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults. |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 1143 | 'annotations' is a dictionary mapping parameter names to annotations. |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 1144 | |
Nick Coghlan | 3c35fdb | 2016-12-02 20:29:57 +1000 | [diff] [blame] | 1145 | Notable differences from inspect.signature(): |
| 1146 | - the "self" parameter is always reported, even for bound methods |
| 1147 | - wrapper chains defined by __wrapped__ *not* unwrapped automatically |
Jeremy Hylton | 6496788 | 2003-06-27 18:14:39 +0000 | [diff] [blame] | 1148 | """ |
Yury Selivanov | 57d240e | 2014-02-19 16:27:23 -0500 | [diff] [blame] | 1149 | try: |
| 1150 | # Re: `skip_bound_arg=False` |
| 1151 | # |
Yury Selivanov | d82eddc | 2014-01-29 11:24:39 -0500 | [diff] [blame] | 1152 | # There is a notable difference in behaviour between getfullargspec |
| 1153 | # and Signature: the former always returns 'self' parameter for bound |
| 1154 | # methods, whereas the Signature always shows the actual calling |
| 1155 | # signature of the passed object. |
| 1156 | # |
| 1157 | # To simulate this behaviour, we "unbind" bound methods, to trick |
| 1158 | # inspect.signature to always return their first parameter ("self", |
| 1159 | # usually) |
Yury Selivanov | d82eddc | 2014-01-29 11:24:39 -0500 | [diff] [blame] | 1160 | |
Yury Selivanov | 57d240e | 2014-02-19 16:27:23 -0500 | [diff] [blame] | 1161 | # Re: `follow_wrapper_chains=False` |
| 1162 | # |
| 1163 | # getfullargspec() historically ignored __wrapped__ attributes, |
| 1164 | # so we ensure that remains the case in 3.3+ |
Yury Selivanov | d82eddc | 2014-01-29 11:24:39 -0500 | [diff] [blame] | 1165 | |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 1166 | sig = _signature_from_callable(func, |
| 1167 | follow_wrapper_chains=False, |
| 1168 | skip_bound_arg=False, |
| 1169 | sigcls=Signature) |
Yury Selivanov | d82eddc | 2014-01-29 11:24:39 -0500 | [diff] [blame] | 1170 | except Exception as ex: |
| 1171 | # Most of the times 'signature' will raise ValueError. |
| 1172 | # But, it can also raise AttributeError, and, maybe something |
| 1173 | # else. So to be fully backwards compatible, we catch all |
| 1174 | # possible exceptions here, and reraise a TypeError. |
| 1175 | raise TypeError('unsupported callable') from ex |
| 1176 | |
| 1177 | args = [] |
| 1178 | varargs = None |
| 1179 | varkw = None |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1180 | posonlyargs = [] |
Yury Selivanov | d82eddc | 2014-01-29 11:24:39 -0500 | [diff] [blame] | 1181 | kwonlyargs = [] |
Yury Selivanov | d82eddc | 2014-01-29 11:24:39 -0500 | [diff] [blame] | 1182 | annotations = {} |
| 1183 | defaults = () |
| 1184 | kwdefaults = {} |
| 1185 | |
| 1186 | if sig.return_annotation is not sig.empty: |
| 1187 | annotations['return'] = sig.return_annotation |
| 1188 | |
| 1189 | for param in sig.parameters.values(): |
| 1190 | kind = param.kind |
| 1191 | name = param.name |
| 1192 | |
| 1193 | if kind is _POSITIONAL_ONLY: |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1194 | posonlyargs.append(name) |
| 1195 | if param.default is not param.empty: |
| 1196 | defaults += (param.default,) |
Yury Selivanov | d82eddc | 2014-01-29 11:24:39 -0500 | [diff] [blame] | 1197 | elif kind is _POSITIONAL_OR_KEYWORD: |
| 1198 | args.append(name) |
| 1199 | if param.default is not param.empty: |
| 1200 | defaults += (param.default,) |
| 1201 | elif kind is _VAR_POSITIONAL: |
| 1202 | varargs = name |
| 1203 | elif kind is _KEYWORD_ONLY: |
| 1204 | kwonlyargs.append(name) |
| 1205 | if param.default is not param.empty: |
| 1206 | kwdefaults[name] = param.default |
| 1207 | elif kind is _VAR_KEYWORD: |
| 1208 | varkw = name |
| 1209 | |
| 1210 | if param.annotation is not param.empty: |
| 1211 | annotations[name] = param.annotation |
| 1212 | |
| 1213 | if not kwdefaults: |
| 1214 | # compatibility with 'func.__kwdefaults__' |
| 1215 | kwdefaults = None |
| 1216 | |
| 1217 | if not defaults: |
| 1218 | # compatibility with 'func.__defaults__' |
| 1219 | defaults = None |
| 1220 | |
Pablo Galindo | d5d2b45 | 2019-04-30 02:01:14 +0100 | [diff] [blame] | 1221 | return FullArgSpec(posonlyargs + args, varargs, varkw, defaults, |
| 1222 | kwonlyargs, kwdefaults, annotations) |
Yury Selivanov | d82eddc | 2014-01-29 11:24:39 -0500 | [diff] [blame] | 1223 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1224 | |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1225 | ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals') |
| 1226 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1227 | def getargvalues(frame): |
| 1228 | """Get information about arguments passed into a particular frame. |
| 1229 | |
| 1230 | A tuple of four things is returned: (args, varargs, varkw, locals). |
Georg Brandl | c1c4bf8 | 2010-10-15 16:07:41 +0000 | [diff] [blame] | 1231 | 'args' is a list of the argument names. |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1232 | 'varargs' and 'varkw' are the names of the * and ** arguments or None. |
| 1233 | 'locals' is the locals dictionary of the given frame.""" |
| 1234 | args, varargs, varkw = getargs(frame.f_code) |
Benjamin Peterson | 1a6e0d0 | 2008-10-25 15:49:17 +0000 | [diff] [blame] | 1235 | return ArgInfo(args, varargs, varkw, frame.f_locals) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1236 | |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1237 | def formatannotation(annotation, base_module=None): |
Guido van Rossum | 52e5004 | 2016-10-22 07:55:18 -0700 | [diff] [blame] | 1238 | if getattr(annotation, '__module__', None) == 'typing': |
| 1239 | return repr(annotation).replace('typing.', '') |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1240 | if isinstance(annotation, type): |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 1241 | if annotation.__module__ in ('builtins', base_module): |
Serhiy Storchaka | 521e586 | 2014-07-22 15:00:37 +0300 | [diff] [blame] | 1242 | return annotation.__qualname__ |
| 1243 | return annotation.__module__+'.'+annotation.__qualname__ |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1244 | return repr(annotation) |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 1245 | |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1246 | def formatannotationrelativeto(object): |
Guido van Rossum | a8add0e | 2007-05-14 22:03:55 +0000 | [diff] [blame] | 1247 | module = getattr(object, '__module__', None) |
| 1248 | def _formatannotation(annotation): |
| 1249 | return formatannotation(annotation, module) |
| 1250 | return _formatannotation |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1251 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1252 | def formatargspec(args, varargs=None, varkw=None, defaults=None, |
Pablo Galindo | d5d2b45 | 2019-04-30 02:01:14 +0100 | [diff] [blame] | 1253 | kwonlyargs=(), kwonlydefaults={}, annotations={}, |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1254 | formatarg=str, |
| 1255 | formatvarargs=lambda name: '*' + name, |
| 1256 | formatvarkw=lambda name: '**' + name, |
| 1257 | formatvalue=lambda value: '=' + repr(value), |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1258 | formatreturns=lambda text: ' -> ' + text, |
Georg Brandl | c1c4bf8 | 2010-10-15 16:07:41 +0000 | [diff] [blame] | 1259 | formatannotation=formatannotation): |
Berker Peksag | fa3922c | 2015-07-31 04:11:29 +0300 | [diff] [blame] | 1260 | """Format an argument spec from the values returned by getfullargspec. |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1261 | |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1262 | The first seven arguments are (args, varargs, varkw, defaults, |
| 1263 | kwonlyargs, kwonlydefaults, annotations). The other five arguments |
| 1264 | are the corresponding optional formatting functions that are called to |
| 1265 | turn names and values into strings. The last argument is an optional |
Matthias Bussonnier | 46c5cd0 | 2018-06-11 22:08:16 +0200 | [diff] [blame] | 1266 | function to format the sequence of arguments. |
| 1267 | |
| 1268 | Deprecated since Python 3.5: use the `signature` function and `Signature` |
| 1269 | objects. |
| 1270 | """ |
| 1271 | |
| 1272 | from warnings import warn |
| 1273 | |
| 1274 | warn("`formatargspec` is deprecated since Python 3.5. Use `signature` and " |
Zackery Spytz | 41254eb | 2018-06-11 21:16:18 -0600 | [diff] [blame] | 1275 | "the `Signature` object directly", |
Matthias Bussonnier | 46c5cd0 | 2018-06-11 22:08:16 +0200 | [diff] [blame] | 1276 | DeprecationWarning, |
| 1277 | stacklevel=2) |
| 1278 | |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1279 | def formatargandannotation(arg): |
| 1280 | result = formatarg(arg) |
| 1281 | if arg in annotations: |
| 1282 | result += ': ' + formatannotation(annotations[arg]) |
| 1283 | return result |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1284 | specs = [] |
| 1285 | if defaults: |
Pablo Galindo | d5d2b45 | 2019-04-30 02:01:14 +0100 | [diff] [blame] | 1286 | firstdefault = len(args) - len(defaults) |
| 1287 | for i, arg in enumerate(args): |
Georg Brandl | c1c4bf8 | 2010-10-15 16:07:41 +0000 | [diff] [blame] | 1288 | spec = formatargandannotation(arg) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1289 | if defaults and i >= firstdefault: |
| 1290 | spec = spec + formatvalue(defaults[i - firstdefault]) |
| 1291 | specs.append(spec) |
Raymond Hettinger | 936654b | 2002-06-01 03:06:31 +0000 | [diff] [blame] | 1292 | if varargs is not None: |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1293 | specs.append(formatvarargs(formatargandannotation(varargs))) |
| 1294 | else: |
| 1295 | if kwonlyargs: |
| 1296 | specs.append('*') |
| 1297 | if kwonlyargs: |
| 1298 | for kwonlyarg in kwonlyargs: |
| 1299 | spec = formatargandannotation(kwonlyarg) |
Benjamin Peterson | 9953a8d | 2009-01-17 04:15:01 +0000 | [diff] [blame] | 1300 | if kwonlydefaults and kwonlyarg in kwonlydefaults: |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1301 | spec += formatvalue(kwonlydefaults[kwonlyarg]) |
| 1302 | specs.append(spec) |
Raymond Hettinger | 936654b | 2002-06-01 03:06:31 +0000 | [diff] [blame] | 1303 | if varkw is not None: |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1304 | specs.append(formatvarkw(formatargandannotation(varkw))) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1305 | result = '(' + ', '.join(specs) + ')' |
Guido van Rossum | 2e65f89 | 2007-02-28 22:03:49 +0000 | [diff] [blame] | 1306 | if 'return' in annotations: |
| 1307 | result += formatreturns(formatannotation(annotations['return'])) |
| 1308 | return result |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1309 | |
| 1310 | def formatargvalues(args, varargs, varkw, locals, |
| 1311 | formatarg=str, |
| 1312 | formatvarargs=lambda name: '*' + name, |
| 1313 | formatvarkw=lambda name: '**' + name, |
Georg Brandl | c1c4bf8 | 2010-10-15 16:07:41 +0000 | [diff] [blame] | 1314 | formatvalue=lambda value: '=' + repr(value)): |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1315 | """Format an argument spec from the 4 values returned by getargvalues. |
| 1316 | |
| 1317 | The first four arguments are (args, varargs, varkw, locals). The |
| 1318 | next four arguments are the corresponding optional formatting functions |
| 1319 | that are called to turn names and values into strings. The ninth |
| 1320 | argument is an optional function to format the sequence of arguments.""" |
| 1321 | def convert(name, locals=locals, |
| 1322 | formatarg=formatarg, formatvalue=formatvalue): |
| 1323 | return formatarg(name) + formatvalue(locals[name]) |
| 1324 | specs = [] |
| 1325 | for i in range(len(args)): |
Georg Brandl | c1c4bf8 | 2010-10-15 16:07:41 +0000 | [diff] [blame] | 1326 | specs.append(convert(args[i])) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1327 | if varargs: |
| 1328 | specs.append(formatvarargs(varargs) + formatvalue(locals[varargs])) |
| 1329 | if varkw: |
| 1330 | specs.append(formatvarkw(varkw) + formatvalue(locals[varkw])) |
Neal Norwitz | 9d72bb4 | 2007-04-17 08:48:32 +0000 | [diff] [blame] | 1331 | return '(' + ', '.join(specs) + ')' |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1332 | |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 1333 | def _missing_arguments(f_name, argnames, pos, values): |
| 1334 | names = [repr(name) for name in argnames if name not in values] |
| 1335 | missing = len(names) |
| 1336 | if missing == 1: |
| 1337 | s = names[0] |
| 1338 | elif missing == 2: |
| 1339 | s = "{} and {}".format(*names) |
| 1340 | else: |
Yury Selivanov | dccfa13 | 2014-03-27 18:42:52 -0400 | [diff] [blame] | 1341 | tail = ", {} and {}".format(*names[-2:]) |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 1342 | del names[-2:] |
| 1343 | s = ", ".join(names) + tail |
| 1344 | raise TypeError("%s() missing %i required %s argument%s: %s" % |
| 1345 | (f_name, missing, |
| 1346 | "positional" if pos else "keyword-only", |
| 1347 | "" if missing == 1 else "s", s)) |
| 1348 | |
| 1349 | def _too_many(f_name, args, kwonly, varargs, defcount, given, values): |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 1350 | atleast = len(args) - defcount |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 1351 | kwonly_given = len([arg for arg in kwonly if arg in values]) |
| 1352 | if varargs: |
| 1353 | plural = atleast != 1 |
| 1354 | sig = "at least %d" % (atleast,) |
| 1355 | elif defcount: |
| 1356 | plural = True |
| 1357 | sig = "from %d to %d" % (atleast, len(args)) |
| 1358 | else: |
| 1359 | plural = len(args) != 1 |
| 1360 | sig = str(len(args)) |
| 1361 | kwonly_sig = "" |
| 1362 | if kwonly_given: |
| 1363 | msg = " positional argument%s (and %d keyword-only argument%s)" |
| 1364 | kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given, |
| 1365 | "s" if kwonly_given != 1 else "")) |
| 1366 | raise TypeError("%s() takes %s positional argument%s but %d%s %s given" % |
| 1367 | (f_name, sig, "s" if plural else "", given, kwonly_sig, |
| 1368 | "was" if given == 1 and not kwonly_given else "were")) |
| 1369 | |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 1370 | def getcallargs(func, /, *positional, **named): |
Benjamin Peterson | 25cd7eb | 2010-03-30 18:42:32 +0000 | [diff] [blame] | 1371 | """Get the mapping of arguments to values. |
| 1372 | |
| 1373 | A dict is returned, with keys the function argument names (including the |
| 1374 | names of the * and ** arguments, if any), and values the respective bound |
| 1375 | values from 'positional' and 'named'.""" |
| 1376 | spec = getfullargspec(func) |
Pablo Galindo | d5d2b45 | 2019-04-30 02:01:14 +0100 | [diff] [blame] | 1377 | args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec |
Benjamin Peterson | 25cd7eb | 2010-03-30 18:42:32 +0000 | [diff] [blame] | 1378 | f_name = func.__name__ |
| 1379 | arg2value = {} |
| 1380 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 1381 | |
Benjamin Peterson | 25cd7eb | 2010-03-30 18:42:32 +0000 | [diff] [blame] | 1382 | if ismethod(func) and func.__self__ is not None: |
| 1383 | # implicit 'self' (or 'cls' for classmethods) argument |
| 1384 | positional = (func.__self__,) + positional |
| 1385 | num_pos = len(positional) |
Benjamin Peterson | 25cd7eb | 2010-03-30 18:42:32 +0000 | [diff] [blame] | 1386 | num_args = len(args) |
| 1387 | num_defaults = len(defaults) if defaults else 0 |
Benjamin Peterson | 25cd7eb | 2010-03-30 18:42:32 +0000 | [diff] [blame] | 1388 | |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 1389 | n = min(num_pos, num_args) |
| 1390 | for i in range(n): |
Pablo Galindo | d5d2b45 | 2019-04-30 02:01:14 +0100 | [diff] [blame] | 1391 | arg2value[args[i]] = positional[i] |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 1392 | if varargs: |
| 1393 | arg2value[varargs] = tuple(positional[n:]) |
| 1394 | possible_kwargs = set(args + kwonlyargs) |
Benjamin Peterson | 25cd7eb | 2010-03-30 18:42:32 +0000 | [diff] [blame] | 1395 | if varkw: |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 1396 | arg2value[varkw] = {} |
| 1397 | for kw, value in named.items(): |
| 1398 | if kw not in possible_kwargs: |
| 1399 | if not varkw: |
| 1400 | raise TypeError("%s() got an unexpected keyword argument %r" % |
| 1401 | (f_name, kw)) |
| 1402 | arg2value[varkw][kw] = value |
| 1403 | continue |
| 1404 | if kw in arg2value: |
| 1405 | raise TypeError("%s() got multiple values for argument %r" % |
| 1406 | (f_name, kw)) |
| 1407 | arg2value[kw] = value |
| 1408 | if num_pos > num_args and not varargs: |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 1409 | _too_many(f_name, args, kwonlyargs, varargs, num_defaults, |
| 1410 | num_pos, arg2value) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 1411 | if num_pos < num_args: |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 1412 | req = args[:num_args - num_defaults] |
| 1413 | for arg in req: |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 1414 | if arg not in arg2value: |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 1415 | _missing_arguments(f_name, req, True, arg2value) |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 1416 | for i, arg in enumerate(args[num_args - num_defaults:]): |
| 1417 | if arg not in arg2value: |
| 1418 | arg2value[arg] = defaults[i] |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 1419 | missing = 0 |
Benjamin Peterson | b204a42 | 2011-06-05 22:04:07 -0500 | [diff] [blame] | 1420 | for kwarg in kwonlyargs: |
| 1421 | if kwarg not in arg2value: |
Yury Selivanov | 875df20 | 2014-03-27 18:23:03 -0400 | [diff] [blame] | 1422 | if kwonlydefaults and kwarg in kwonlydefaults: |
Benjamin Peterson | e109c70 | 2011-06-24 09:37:26 -0500 | [diff] [blame] | 1423 | arg2value[kwarg] = kwonlydefaults[kwarg] |
| 1424 | else: |
| 1425 | missing += 1 |
| 1426 | if missing: |
| 1427 | _missing_arguments(f_name, kwonlyargs, False, arg2value) |
Benjamin Peterson | 25cd7eb | 2010-03-30 18:42:32 +0000 | [diff] [blame] | 1428 | return arg2value |
| 1429 | |
Nick Coghlan | 2f92e54 | 2012-06-23 19:39:55 +1000 | [diff] [blame] | 1430 | ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound') |
| 1431 | |
| 1432 | def getclosurevars(func): |
| 1433 | """ |
| 1434 | Get the mapping of free variables to their current values. |
| 1435 | |
Meador Inge | 8fda359 | 2012-07-19 21:33:21 -0500 | [diff] [blame] | 1436 | Returns a named tuple of dicts mapping the current nonlocal, global |
Nick Coghlan | 2f92e54 | 2012-06-23 19:39:55 +1000 | [diff] [blame] | 1437 | and builtin references as seen by the body of the function. A final |
| 1438 | set of unbound names that could not be resolved is also provided. |
| 1439 | """ |
| 1440 | |
| 1441 | if ismethod(func): |
| 1442 | func = func.__func__ |
| 1443 | |
| 1444 | if not isfunction(func): |
Serhiy Storchaka | a4a3020 | 2017-11-28 22:54:42 +0200 | [diff] [blame] | 1445 | raise TypeError("{!r} is not a Python function".format(func)) |
Nick Coghlan | 2f92e54 | 2012-06-23 19:39:55 +1000 | [diff] [blame] | 1446 | |
| 1447 | code = func.__code__ |
| 1448 | # Nonlocal references are named in co_freevars and resolved |
| 1449 | # by looking them up in __closure__ by positional index |
| 1450 | if func.__closure__ is None: |
| 1451 | nonlocal_vars = {} |
| 1452 | else: |
| 1453 | nonlocal_vars = { |
| 1454 | var : cell.cell_contents |
| 1455 | for var, cell in zip(code.co_freevars, func.__closure__) |
| 1456 | } |
| 1457 | |
| 1458 | # Global and builtin references are named in co_names and resolved |
| 1459 | # by looking them up in __globals__ or __builtins__ |
| 1460 | global_ns = func.__globals__ |
| 1461 | builtin_ns = global_ns.get("__builtins__", builtins.__dict__) |
| 1462 | if ismodule(builtin_ns): |
| 1463 | builtin_ns = builtin_ns.__dict__ |
| 1464 | global_vars = {} |
| 1465 | builtin_vars = {} |
| 1466 | unbound_names = set() |
| 1467 | for name in code.co_names: |
| 1468 | if name in ("None", "True", "False"): |
| 1469 | # Because these used to be builtins instead of keywords, they |
| 1470 | # may still show up as name references. We ignore them. |
| 1471 | continue |
| 1472 | try: |
| 1473 | global_vars[name] = global_ns[name] |
| 1474 | except KeyError: |
| 1475 | try: |
| 1476 | builtin_vars[name] = builtin_ns[name] |
| 1477 | except KeyError: |
| 1478 | unbound_names.add(name) |
| 1479 | |
| 1480 | return ClosureVars(nonlocal_vars, global_vars, |
| 1481 | builtin_vars, unbound_names) |
| 1482 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1483 | # -------------------------------------------------- stack frame extraction |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1484 | |
| 1485 | Traceback = namedtuple('Traceback', 'filename lineno function code_context index') |
| 1486 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1487 | def getframeinfo(frame, context=1): |
| 1488 | """Get information about a frame or traceback object. |
| 1489 | |
| 1490 | A tuple of five things is returned: the filename, the line number of |
| 1491 | the current line, the function name, a list of lines of context from |
| 1492 | the source code, and the index of the current line within that list. |
| 1493 | The optional second argument specifies the number of lines of context |
| 1494 | to return, which are centered around the current line.""" |
| 1495 | if istraceback(frame): |
Andrew M. Kuchling | ba8b6bc | 2004-06-05 14:11:59 +0000 | [diff] [blame] | 1496 | lineno = frame.tb_lineno |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1497 | frame = frame.tb_frame |
Andrew M. Kuchling | ba8b6bc | 2004-06-05 14:11:59 +0000 | [diff] [blame] | 1498 | else: |
| 1499 | lineno = frame.f_lineno |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1500 | if not isframe(frame): |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 1501 | raise TypeError('{!r} is not a frame or traceback object'.format(frame)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1502 | |
Neil Schemenauer | f06f853 | 2002-03-23 23:51:04 +0000 | [diff] [blame] | 1503 | filename = getsourcefile(frame) or getfile(frame) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1504 | if context > 0: |
Guido van Rossum | 54e54c6 | 2001-09-04 19:14:14 +0000 | [diff] [blame] | 1505 | start = lineno - 1 - context//2 |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1506 | try: |
| 1507 | lines, lnum = findsource(frame) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 1508 | except OSError: |
Ka-Ping Yee | 4eb0c00 | 2001-03-02 05:50:34 +0000 | [diff] [blame] | 1509 | lines = index = None |
| 1510 | else: |
Raymond Hettinger | a050171 | 2004-06-15 11:22:53 +0000 | [diff] [blame] | 1511 | start = max(0, min(start, len(lines) - context)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1512 | lines = lines[start:start+context] |
Ka-Ping Yee | 59ade08 | 2001-03-01 03:55:35 +0000 | [diff] [blame] | 1513 | index = lineno - 1 - start |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1514 | else: |
| 1515 | lines = index = None |
| 1516 | |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1517 | return Traceback(filename, lineno, frame.f_code.co_name, lines, index) |
Ka-Ping Yee | 59ade08 | 2001-03-01 03:55:35 +0000 | [diff] [blame] | 1518 | |
| 1519 | def getlineno(frame): |
| 1520 | """Get the line number from a frame object, allowing for optimization.""" |
Michael W. Hudson | dd32a91 | 2002-08-15 14:59:02 +0000 | [diff] [blame] | 1521 | # FrameType.f_lineno is now a descriptor that grovels co_lnotab |
| 1522 | return frame.f_lineno |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1523 | |
Antoine Pitrou | cdcafb7 | 2014-08-24 10:50:28 -0400 | [diff] [blame] | 1524 | FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields) |
| 1525 | |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1526 | def getouterframes(frame, context=1): |
| 1527 | """Get a list of records for a frame and all higher (calling) 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.""" |
| 1531 | framelist = [] |
| 1532 | while frame: |
Antoine Pitrou | cdcafb7 | 2014-08-24 10:50:28 -0400 | [diff] [blame] | 1533 | frameinfo = (frame,) + getframeinfo(frame, context) |
| 1534 | framelist.append(FrameInfo(*frameinfo)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1535 | frame = frame.f_back |
| 1536 | return framelist |
| 1537 | |
| 1538 | def getinnerframes(tb, context=1): |
| 1539 | """Get a list of records for a traceback's frame and all lower frames. |
| 1540 | |
| 1541 | Each record contains a frame object, filename, line number, function |
| 1542 | name, a list of lines of context, and index within the context.""" |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1543 | framelist = [] |
| 1544 | while tb: |
Antoine Pitrou | cdcafb7 | 2014-08-24 10:50:28 -0400 | [diff] [blame] | 1545 | frameinfo = (tb.tb_frame,) + getframeinfo(tb, context) |
| 1546 | framelist.append(FrameInfo(*frameinfo)) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1547 | tb = tb.tb_next |
| 1548 | return framelist |
| 1549 | |
Benjamin Peterson | 42ac475 | 2010-08-09 13:05:35 +0000 | [diff] [blame] | 1550 | def currentframe(): |
Benjamin Peterson | a3a3fc6 | 2010-08-09 15:49:56 +0000 | [diff] [blame] | 1551 | """Return the frame of the caller or None if this is not possible.""" |
Benjamin Peterson | 42ac475 | 2010-08-09 13:05:35 +0000 | [diff] [blame] | 1552 | return sys._getframe(1) if hasattr(sys, "_getframe") else None |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1553 | |
| 1554 | def stack(context=1): |
| 1555 | """Return a list of records for the stack above the caller's frame.""" |
Jeremy Hylton | ab91902 | 2003-06-27 18:41:20 +0000 | [diff] [blame] | 1556 | return getouterframes(sys._getframe(1), context) |
Ka-Ping Yee | 6397c7c | 2001-02-27 14:43:21 +0000 | [diff] [blame] | 1557 | |
| 1558 | def trace(context=1): |
Tim Peters | 85ba673 | 2001-02-28 08:26:44 +0000 | [diff] [blame] | 1559 | """Return a list of records for the stack below the current exception.""" |
Fred Drake | d451ec1 | 2002-04-26 02:29:55 +0000 | [diff] [blame] | 1560 | return getinnerframes(sys.exc_info()[2], context) |
Michael Foord | 95fc51d | 2010-11-20 15:07:30 +0000 | [diff] [blame] | 1561 | |
| 1562 | |
| 1563 | # ------------------------------------------------ static version of getattr |
| 1564 | |
| 1565 | _sentinel = object() |
| 1566 | |
Michael Foord | e516265 | 2010-11-20 16:40:44 +0000 | [diff] [blame] | 1567 | def _static_getmro(klass): |
| 1568 | return type.__dict__['__mro__'].__get__(klass) |
| 1569 | |
Michael Foord | 95fc51d | 2010-11-20 15:07:30 +0000 | [diff] [blame] | 1570 | def _check_instance(obj, attr): |
| 1571 | instance_dict = {} |
| 1572 | try: |
| 1573 | instance_dict = object.__getattribute__(obj, "__dict__") |
| 1574 | except AttributeError: |
| 1575 | pass |
Michael Foord | dcebe0f | 2011-03-15 19:20:44 -0400 | [diff] [blame] | 1576 | return dict.get(instance_dict, attr, _sentinel) |
Michael Foord | 95fc51d | 2010-11-20 15:07:30 +0000 | [diff] [blame] | 1577 | |
| 1578 | |
| 1579 | def _check_class(klass, attr): |
Michael Foord | e516265 | 2010-11-20 16:40:44 +0000 | [diff] [blame] | 1580 | for entry in _static_getmro(klass): |
Michael Foord | a51623b | 2011-12-18 22:01:40 +0000 | [diff] [blame] | 1581 | if _shadowed_dict(type(entry)) is _sentinel: |
Michael Foord | dcebe0f | 2011-03-15 19:20:44 -0400 | [diff] [blame] | 1582 | try: |
| 1583 | return entry.__dict__[attr] |
| 1584 | except KeyError: |
| 1585 | pass |
Michael Foord | 95fc51d | 2010-11-20 15:07:30 +0000 | [diff] [blame] | 1586 | return _sentinel |
| 1587 | |
Michael Foord | 35184ed | 2010-11-20 16:58:30 +0000 | [diff] [blame] | 1588 | def _is_type(obj): |
| 1589 | try: |
| 1590 | _static_getmro(obj) |
| 1591 | except TypeError: |
| 1592 | return False |
| 1593 | return True |
| 1594 | |
Michael Foord | dcebe0f | 2011-03-15 19:20:44 -0400 | [diff] [blame] | 1595 | def _shadowed_dict(klass): |
| 1596 | dict_attr = type.__dict__["__dict__"] |
| 1597 | for entry in _static_getmro(klass): |
| 1598 | try: |
| 1599 | class_dict = dict_attr.__get__(entry)["__dict__"] |
| 1600 | except KeyError: |
| 1601 | pass |
| 1602 | else: |
Inada Naoki | 8f9cc87 | 2019-09-05 13:07:08 +0900 | [diff] [blame] | 1603 | if not (type(class_dict) is types.GetSetDescriptorType and |
Michael Foord | dcebe0f | 2011-03-15 19:20:44 -0400 | [diff] [blame] | 1604 | class_dict.__name__ == "__dict__" and |
| 1605 | class_dict.__objclass__ is entry): |
Michael Foord | a51623b | 2011-12-18 22:01:40 +0000 | [diff] [blame] | 1606 | return class_dict |
| 1607 | return _sentinel |
Michael Foord | 95fc51d | 2010-11-20 15:07:30 +0000 | [diff] [blame] | 1608 | |
| 1609 | def getattr_static(obj, attr, default=_sentinel): |
| 1610 | """Retrieve attributes without triggering dynamic lookup via the |
| 1611 | descriptor protocol, __getattr__ or __getattribute__. |
| 1612 | |
| 1613 | Note: this function may not be able to retrieve all attributes |
| 1614 | that getattr can fetch (like dynamically created attributes) |
| 1615 | and may find attributes that getattr can't (like descriptors |
| 1616 | that raise AttributeError). It can also return descriptor objects |
| 1617 | instead of instance members in some cases. See the |
| 1618 | documentation for details. |
| 1619 | """ |
| 1620 | instance_result = _sentinel |
Michael Foord | 35184ed | 2010-11-20 16:58:30 +0000 | [diff] [blame] | 1621 | if not _is_type(obj): |
Michael Foord | cc7ebb8 | 2010-11-20 16:20:16 +0000 | [diff] [blame] | 1622 | klass = type(obj) |
Michael Foord | a51623b | 2011-12-18 22:01:40 +0000 | [diff] [blame] | 1623 | dict_attr = _shadowed_dict(klass) |
| 1624 | if (dict_attr is _sentinel or |
Inada Naoki | 8f9cc87 | 2019-09-05 13:07:08 +0900 | [diff] [blame] | 1625 | type(dict_attr) is types.MemberDescriptorType): |
Michael Foord | dcebe0f | 2011-03-15 19:20:44 -0400 | [diff] [blame] | 1626 | instance_result = _check_instance(obj, attr) |
Michael Foord | 95fc51d | 2010-11-20 15:07:30 +0000 | [diff] [blame] | 1627 | else: |
| 1628 | klass = obj |
| 1629 | |
| 1630 | klass_result = _check_class(klass, attr) |
| 1631 | |
| 1632 | if instance_result is not _sentinel and klass_result is not _sentinel: |
| 1633 | if (_check_class(type(klass_result), '__get__') is not _sentinel and |
| 1634 | _check_class(type(klass_result), '__set__') is not _sentinel): |
| 1635 | return klass_result |
| 1636 | |
| 1637 | if instance_result is not _sentinel: |
| 1638 | return instance_result |
| 1639 | if klass_result is not _sentinel: |
| 1640 | return klass_result |
| 1641 | |
| 1642 | if obj is klass: |
| 1643 | # for types we check the metaclass too |
Michael Foord | e516265 | 2010-11-20 16:40:44 +0000 | [diff] [blame] | 1644 | for entry in _static_getmro(type(klass)): |
Michael Foord | 3ba95f8 | 2011-12-22 01:13:37 +0000 | [diff] [blame] | 1645 | if _shadowed_dict(type(entry)) is _sentinel: |
| 1646 | try: |
| 1647 | return entry.__dict__[attr] |
| 1648 | except KeyError: |
| 1649 | pass |
Michael Foord | 95fc51d | 2010-11-20 15:07:30 +0000 | [diff] [blame] | 1650 | if default is not _sentinel: |
| 1651 | return default |
| 1652 | raise AttributeError(attr) |
Nick Coghlan | e0f0465 | 2010-11-21 03:44:04 +0000 | [diff] [blame] | 1653 | |
| 1654 | |
Nick Coghlan | 04e2e3f | 2012-06-23 19:52:05 +1000 | [diff] [blame] | 1655 | # ------------------------------------------------ generator introspection |
| 1656 | |
Nick Coghlan | 7921b9f | 2010-11-30 06:36:04 +0000 | [diff] [blame] | 1657 | GEN_CREATED = 'GEN_CREATED' |
| 1658 | GEN_RUNNING = 'GEN_RUNNING' |
| 1659 | GEN_SUSPENDED = 'GEN_SUSPENDED' |
| 1660 | GEN_CLOSED = 'GEN_CLOSED' |
Nick Coghlan | e0f0465 | 2010-11-21 03:44:04 +0000 | [diff] [blame] | 1661 | |
| 1662 | def getgeneratorstate(generator): |
| 1663 | """Get current state of a generator-iterator. |
| 1664 | |
| 1665 | Possible states are: |
| 1666 | GEN_CREATED: Waiting to start execution. |
| 1667 | GEN_RUNNING: Currently being executed by the interpreter. |
| 1668 | GEN_SUSPENDED: Currently suspended at a yield expression. |
| 1669 | GEN_CLOSED: Execution has completed. |
| 1670 | """ |
| 1671 | if generator.gi_running: |
| 1672 | return GEN_RUNNING |
| 1673 | if generator.gi_frame is None: |
| 1674 | return GEN_CLOSED |
| 1675 | if generator.gi_frame.f_lasti == -1: |
| 1676 | return GEN_CREATED |
| 1677 | return GEN_SUSPENDED |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 1678 | |
| 1679 | |
Nick Coghlan | 04e2e3f | 2012-06-23 19:52:05 +1000 | [diff] [blame] | 1680 | def getgeneratorlocals(generator): |
| 1681 | """ |
| 1682 | Get the mapping of generator local variables to their current values. |
| 1683 | |
| 1684 | A dict is returned, with the keys the local variable names and values the |
| 1685 | bound values.""" |
| 1686 | |
| 1687 | if not isgenerator(generator): |
Serhiy Storchaka | a4a3020 | 2017-11-28 22:54:42 +0200 | [diff] [blame] | 1688 | raise TypeError("{!r} is not a Python generator".format(generator)) |
Nick Coghlan | 04e2e3f | 2012-06-23 19:52:05 +1000 | [diff] [blame] | 1689 | |
| 1690 | frame = getattr(generator, "gi_frame", None) |
| 1691 | if frame is not None: |
| 1692 | return generator.gi_frame.f_locals |
| 1693 | else: |
| 1694 | return {} |
| 1695 | |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1696 | |
| 1697 | # ------------------------------------------------ coroutine introspection |
| 1698 | |
| 1699 | CORO_CREATED = 'CORO_CREATED' |
| 1700 | CORO_RUNNING = 'CORO_RUNNING' |
| 1701 | CORO_SUSPENDED = 'CORO_SUSPENDED' |
| 1702 | CORO_CLOSED = 'CORO_CLOSED' |
| 1703 | |
| 1704 | def getcoroutinestate(coroutine): |
| 1705 | """Get current state of a coroutine object. |
| 1706 | |
| 1707 | Possible states are: |
| 1708 | CORO_CREATED: Waiting to start execution. |
| 1709 | CORO_RUNNING: Currently being executed by the interpreter. |
| 1710 | CORO_SUSPENDED: Currently suspended at an await expression. |
| 1711 | CORO_CLOSED: Execution has completed. |
| 1712 | """ |
| 1713 | if coroutine.cr_running: |
| 1714 | return CORO_RUNNING |
| 1715 | if coroutine.cr_frame is None: |
| 1716 | return CORO_CLOSED |
| 1717 | if coroutine.cr_frame.f_lasti == -1: |
| 1718 | return CORO_CREATED |
| 1719 | return CORO_SUSPENDED |
| 1720 | |
| 1721 | |
| 1722 | def getcoroutinelocals(coroutine): |
| 1723 | """ |
| 1724 | Get the mapping of coroutine local variables to their current values. |
| 1725 | |
| 1726 | A dict is returned, with the keys the local variable names and values the |
| 1727 | bound values.""" |
| 1728 | frame = getattr(coroutine, "cr_frame", None) |
| 1729 | if frame is not None: |
| 1730 | return frame.f_locals |
| 1731 | else: |
| 1732 | return {} |
| 1733 | |
| 1734 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 1735 | ############################################################################### |
| 1736 | ### Function Signature Object (PEP 362) |
| 1737 | ############################################################################### |
| 1738 | |
| 1739 | |
| 1740 | _WrapperDescriptor = type(type.__call__) |
| 1741 | _MethodWrapper = type(all.__call__) |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 1742 | _ClassMethodWrapper = type(int.__dict__['from_bytes']) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 1743 | |
| 1744 | _NonUserDefinedCallables = (_WrapperDescriptor, |
| 1745 | _MethodWrapper, |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 1746 | _ClassMethodWrapper, |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 1747 | types.BuiltinFunctionType) |
| 1748 | |
| 1749 | |
Yury Selivanov | 421f0c7 | 2014-01-29 12:05:40 -0500 | [diff] [blame] | 1750 | def _signature_get_user_defined_method(cls, method_name): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 1751 | """Private helper. Checks if ``cls`` has an attribute |
| 1752 | named ``method_name`` and returns it only if it is a |
| 1753 | pure python function. |
| 1754 | """ |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 1755 | try: |
| 1756 | meth = getattr(cls, method_name) |
| 1757 | except AttributeError: |
| 1758 | return |
| 1759 | else: |
| 1760 | if not isinstance(meth, _NonUserDefinedCallables): |
| 1761 | # Once '__signature__' will be added to 'C'-level |
| 1762 | # callables, this check won't be necessary |
| 1763 | return meth |
| 1764 | |
| 1765 | |
Yury Selivanov | 62560fb | 2014-01-28 12:26:24 -0500 | [diff] [blame] | 1766 | def _signature_get_partial(wrapped_sig, partial, extra_args=()): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 1767 | """Private helper to calculate how 'wrapped_sig' signature will |
| 1768 | look like after applying a 'functools.partial' object (or alike) |
| 1769 | on it. |
| 1770 | """ |
Yury Selivanov | 62560fb | 2014-01-28 12:26:24 -0500 | [diff] [blame] | 1771 | |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 1772 | old_params = wrapped_sig.parameters |
Inada Naoki | 2110551 | 2020-03-02 18:54:49 +0900 | [diff] [blame] | 1773 | new_params = OrderedDict(old_params.items()) |
Yury Selivanov | da5fe4f | 2014-01-27 17:28:37 -0500 | [diff] [blame] | 1774 | |
| 1775 | partial_args = partial.args or () |
| 1776 | partial_keywords = partial.keywords or {} |
| 1777 | |
| 1778 | if extra_args: |
| 1779 | partial_args = extra_args + partial_args |
| 1780 | |
| 1781 | try: |
| 1782 | ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords) |
| 1783 | except TypeError as ex: |
| 1784 | msg = 'partial object {!r} has incorrect arguments'.format(partial) |
| 1785 | raise ValueError(msg) from ex |
| 1786 | |
Yury Selivanov | da5fe4f | 2014-01-27 17:28:37 -0500 | [diff] [blame] | 1787 | |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 1788 | transform_to_kwonly = False |
| 1789 | for param_name, param in old_params.items(): |
| 1790 | try: |
| 1791 | arg_value = ba.arguments[param_name] |
| 1792 | except KeyError: |
| 1793 | pass |
| 1794 | else: |
| 1795 | if param.kind is _POSITIONAL_ONLY: |
| 1796 | # If positional-only parameter is bound by partial, |
| 1797 | # it effectively disappears from the signature |
Inada Naoki | 2110551 | 2020-03-02 18:54:49 +0900 | [diff] [blame] | 1798 | new_params.pop(param_name) |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 1799 | continue |
| 1800 | |
| 1801 | if param.kind is _POSITIONAL_OR_KEYWORD: |
| 1802 | if param_name in partial_keywords: |
| 1803 | # This means that this parameter, and all parameters |
| 1804 | # after it should be keyword-only (and var-positional |
| 1805 | # should be removed). Here's why. Consider the following |
| 1806 | # function: |
| 1807 | # foo(a, b, *args, c): |
| 1808 | # pass |
| 1809 | # |
| 1810 | # "partial(foo, a='spam')" will have the following |
| 1811 | # signature: "(*, a='spam', b, c)". Because attempting |
| 1812 | # to call that partial with "(10, 20)" arguments will |
| 1813 | # raise a TypeError, saying that "a" argument received |
| 1814 | # multiple values. |
| 1815 | transform_to_kwonly = True |
| 1816 | # Set the new default value |
Inada Naoki | 2110551 | 2020-03-02 18:54:49 +0900 | [diff] [blame] | 1817 | new_params[param_name] = param.replace(default=arg_value) |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 1818 | else: |
| 1819 | # was passed as a positional argument |
Inada Naoki | 2110551 | 2020-03-02 18:54:49 +0900 | [diff] [blame] | 1820 | new_params.pop(param.name) |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 1821 | continue |
| 1822 | |
| 1823 | if param.kind is _KEYWORD_ONLY: |
| 1824 | # Set the new default value |
Inada Naoki | 2110551 | 2020-03-02 18:54:49 +0900 | [diff] [blame] | 1825 | new_params[param_name] = param.replace(default=arg_value) |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 1826 | |
| 1827 | if transform_to_kwonly: |
| 1828 | assert param.kind is not _POSITIONAL_ONLY |
| 1829 | |
| 1830 | if param.kind is _POSITIONAL_OR_KEYWORD: |
Inada Naoki | 2110551 | 2020-03-02 18:54:49 +0900 | [diff] [blame] | 1831 | new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY) |
| 1832 | new_params[param_name] = new_param |
| 1833 | new_params.move_to_end(param_name) |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 1834 | elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD): |
Inada Naoki | 2110551 | 2020-03-02 18:54:49 +0900 | [diff] [blame] | 1835 | new_params.move_to_end(param_name) |
| 1836 | elif param.kind is _VAR_POSITIONAL: |
| 1837 | new_params.pop(param.name) |
Yury Selivanov | da5fe4f | 2014-01-27 17:28:37 -0500 | [diff] [blame] | 1838 | |
| 1839 | return wrapped_sig.replace(parameters=new_params.values()) |
| 1840 | |
| 1841 | |
Yury Selivanov | 62560fb | 2014-01-28 12:26:24 -0500 | [diff] [blame] | 1842 | def _signature_bound_method(sig): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 1843 | """Private helper to transform signatures for unbound |
| 1844 | functions to bound methods. |
| 1845 | """ |
Yury Selivanov | 62560fb | 2014-01-28 12:26:24 -0500 | [diff] [blame] | 1846 | |
| 1847 | params = tuple(sig.parameters.values()) |
| 1848 | |
| 1849 | if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY): |
| 1850 | raise ValueError('invalid method signature') |
| 1851 | |
| 1852 | kind = params[0].kind |
| 1853 | if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY): |
| 1854 | # Drop first parameter: |
| 1855 | # '(p1, p2[, ...])' -> '(p2[, ...])' |
| 1856 | params = params[1:] |
| 1857 | else: |
| 1858 | if kind is not _VAR_POSITIONAL: |
| 1859 | # Unless we add a new parameter type we never |
| 1860 | # get here |
| 1861 | raise ValueError('invalid argument type') |
| 1862 | # It's a var-positional parameter. |
| 1863 | # Do nothing. '(*args[, ...])' -> '(*args[, ...])' |
| 1864 | |
| 1865 | return sig.replace(parameters=params) |
| 1866 | |
| 1867 | |
Yury Selivanov | b77511d | 2014-01-29 10:46:14 -0500 | [diff] [blame] | 1868 | def _signature_is_builtin(obj): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 1869 | """Private helper to test if `obj` is a callable that might |
| 1870 | support Argument Clinic's __text_signature__ protocol. |
| 1871 | """ |
Yury Selivanov | 1d24183 | 2014-02-02 12:51:20 -0500 | [diff] [blame] | 1872 | return (isbuiltin(obj) or |
Yury Selivanov | b77511d | 2014-01-29 10:46:14 -0500 | [diff] [blame] | 1873 | ismethoddescriptor(obj) or |
Yury Selivanov | 1d24183 | 2014-02-02 12:51:20 -0500 | [diff] [blame] | 1874 | isinstance(obj, _NonUserDefinedCallables) or |
Yury Selivanov | b77511d | 2014-01-29 10:46:14 -0500 | [diff] [blame] | 1875 | # Can't test 'isinstance(type)' here, as it would |
| 1876 | # also be True for regular python classes |
| 1877 | obj in (type, object)) |
| 1878 | |
| 1879 | |
Yury Selivanov | 63da7c7 | 2014-01-31 14:48:37 -0500 | [diff] [blame] | 1880 | def _signature_is_functionlike(obj): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 1881 | """Private helper to test if `obj` is a duck type of FunctionType. |
| 1882 | A good example of such objects are functions compiled with |
| 1883 | Cython, which have all attributes that a pure Python function |
| 1884 | would have, but have their code statically compiled. |
| 1885 | """ |
Yury Selivanov | 63da7c7 | 2014-01-31 14:48:37 -0500 | [diff] [blame] | 1886 | |
| 1887 | if not callable(obj) or isclass(obj): |
| 1888 | # All function-like objects are obviously callables, |
| 1889 | # and not classes. |
| 1890 | return False |
| 1891 | |
| 1892 | name = getattr(obj, '__name__', None) |
| 1893 | code = getattr(obj, '__code__', None) |
| 1894 | defaults = getattr(obj, '__defaults__', _void) # Important to use _void ... |
| 1895 | kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 1896 | try: |
| 1897 | annotations = _get_type_hints(obj) |
| 1898 | except AttributeError: |
| 1899 | annotations = None |
Yury Selivanov | 63da7c7 | 2014-01-31 14:48:37 -0500 | [diff] [blame] | 1900 | |
| 1901 | return (isinstance(code, types.CodeType) and |
| 1902 | isinstance(name, str) and |
| 1903 | (defaults is None or isinstance(defaults, tuple)) and |
| 1904 | (kwdefaults is None or isinstance(kwdefaults, dict)) and |
| 1905 | isinstance(annotations, dict)) |
| 1906 | |
| 1907 | |
Yury Selivanov | d82eddc | 2014-01-29 11:24:39 -0500 | [diff] [blame] | 1908 | def _signature_get_bound_param(spec): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 1909 | """ Private helper to get first parameter name from a |
| 1910 | __text_signature__ of a builtin method, which should |
| 1911 | be in the following format: '($param1, ...)'. |
| 1912 | Assumptions are that the first argument won't have |
| 1913 | a default value or an annotation. |
| 1914 | """ |
Yury Selivanov | d82eddc | 2014-01-29 11:24:39 -0500 | [diff] [blame] | 1915 | |
| 1916 | assert spec.startswith('($') |
| 1917 | |
| 1918 | pos = spec.find(',') |
| 1919 | if pos == -1: |
| 1920 | pos = spec.find(')') |
| 1921 | |
| 1922 | cpos = spec.find(':') |
| 1923 | assert cpos == -1 or cpos > pos |
| 1924 | |
| 1925 | cpos = spec.find('=') |
| 1926 | assert cpos == -1 or cpos > pos |
| 1927 | |
| 1928 | return spec[2:pos] |
| 1929 | |
| 1930 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 1931 | def _signature_strip_non_python_syntax(signature): |
| 1932 | """ |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 1933 | Private helper function. Takes a signature in Argument Clinic's |
| 1934 | extended signature format. |
| 1935 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 1936 | Returns a tuple of three things: |
| 1937 | * that signature re-rendered in standard Python syntax, |
| 1938 | * the index of the "self" parameter (generally 0), or None if |
| 1939 | the function does not have a "self" parameter, and |
| 1940 | * the index of the last "positional only" parameter, |
| 1941 | or None if the signature has no positional-only parameters. |
| 1942 | """ |
| 1943 | |
| 1944 | if not signature: |
| 1945 | return signature, None, None |
| 1946 | |
| 1947 | self_parameter = None |
| 1948 | last_positional_only = None |
| 1949 | |
| 1950 | lines = [l.encode('ascii') for l in signature.split('\n')] |
| 1951 | generator = iter(lines).__next__ |
| 1952 | token_stream = tokenize.tokenize(generator) |
| 1953 | |
| 1954 | delayed_comma = False |
| 1955 | skip_next_comma = False |
| 1956 | text = [] |
| 1957 | add = text.append |
| 1958 | |
| 1959 | current_parameter = 0 |
| 1960 | OP = token.OP |
| 1961 | ERRORTOKEN = token.ERRORTOKEN |
| 1962 | |
| 1963 | # token stream always starts with ENCODING token, skip it |
| 1964 | t = next(token_stream) |
| 1965 | assert t.type == tokenize.ENCODING |
| 1966 | |
| 1967 | for t in token_stream: |
| 1968 | type, string = t.type, t.string |
| 1969 | |
| 1970 | if type == OP: |
| 1971 | if string == ',': |
| 1972 | if skip_next_comma: |
| 1973 | skip_next_comma = False |
| 1974 | else: |
| 1975 | assert not delayed_comma |
| 1976 | delayed_comma = True |
| 1977 | current_parameter += 1 |
| 1978 | continue |
| 1979 | |
| 1980 | if string == '/': |
| 1981 | assert not skip_next_comma |
| 1982 | assert last_positional_only is None |
| 1983 | skip_next_comma = True |
| 1984 | last_positional_only = current_parameter - 1 |
| 1985 | continue |
| 1986 | |
| 1987 | if (type == ERRORTOKEN) and (string == '$'): |
| 1988 | assert self_parameter is None |
| 1989 | self_parameter = current_parameter |
| 1990 | continue |
| 1991 | |
| 1992 | if delayed_comma: |
| 1993 | delayed_comma = False |
| 1994 | if not ((type == OP) and (string == ')')): |
| 1995 | add(', ') |
| 1996 | add(string) |
| 1997 | if (string == ','): |
| 1998 | add(' ') |
| 1999 | clean_signature = ''.join(text) |
| 2000 | return clean_signature, self_parameter, last_positional_only |
| 2001 | |
| 2002 | |
Yury Selivanov | 57d240e | 2014-02-19 16:27:23 -0500 | [diff] [blame] | 2003 | def _signature_fromstr(cls, obj, s, skip_bound_arg=True): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2004 | """Private helper to parse content of '__text_signature__' |
| 2005 | and return a Signature based on it. |
| 2006 | """ |
INADA Naoki | 37420de | 2018-01-27 10:10:06 +0900 | [diff] [blame] | 2007 | # Lazy import ast because it's relatively heavy and |
| 2008 | # it's not used for other than this function. |
| 2009 | import ast |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2010 | |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2011 | Parameter = cls._parameter_cls |
| 2012 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2013 | clean_signature, self_parameter, last_positional_only = \ |
| 2014 | _signature_strip_non_python_syntax(s) |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2015 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2016 | program = "def foo" + clean_signature + ": pass" |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2017 | |
| 2018 | try: |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2019 | module = ast.parse(program) |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2020 | except SyntaxError: |
| 2021 | module = None |
| 2022 | |
| 2023 | if not isinstance(module, ast.Module): |
| 2024 | raise ValueError("{!r} builtin has invalid signature".format(obj)) |
| 2025 | |
| 2026 | f = module.body[0] |
| 2027 | |
| 2028 | parameters = [] |
| 2029 | empty = Parameter.empty |
| 2030 | invalid = object() |
| 2031 | |
| 2032 | module = None |
| 2033 | module_dict = {} |
| 2034 | module_name = getattr(obj, '__module__', None) |
| 2035 | if module_name: |
| 2036 | module = sys.modules.get(module_name, None) |
| 2037 | if module: |
| 2038 | module_dict = module.__dict__ |
INADA Naoki | 6f85b82 | 2018-10-05 01:47:09 +0900 | [diff] [blame] | 2039 | sys_module_dict = sys.modules.copy() |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2040 | |
| 2041 | def parse_name(node): |
| 2042 | assert isinstance(node, ast.arg) |
Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి) | feaefc7 | 2018-02-09 15:29:19 +0530 | [diff] [blame] | 2043 | if node.annotation is not None: |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2044 | raise ValueError("Annotations are not currently supported") |
| 2045 | return node.arg |
| 2046 | |
| 2047 | def wrap_value(s): |
| 2048 | try: |
| 2049 | value = eval(s, module_dict) |
| 2050 | except NameError: |
| 2051 | try: |
| 2052 | value = eval(s, sys_module_dict) |
| 2053 | except NameError: |
| 2054 | raise RuntimeError() |
| 2055 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2056 | if isinstance(value, (str, int, float, bytes, bool, type(None))): |
| 2057 | return ast.Constant(value) |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2058 | raise RuntimeError() |
| 2059 | |
| 2060 | class RewriteSymbolics(ast.NodeTransformer): |
| 2061 | def visit_Attribute(self, node): |
| 2062 | a = [] |
| 2063 | n = node |
| 2064 | while isinstance(n, ast.Attribute): |
| 2065 | a.append(n.attr) |
| 2066 | n = n.value |
| 2067 | if not isinstance(n, ast.Name): |
| 2068 | raise RuntimeError() |
| 2069 | a.append(n.id) |
| 2070 | value = ".".join(reversed(a)) |
| 2071 | return wrap_value(value) |
| 2072 | |
| 2073 | def visit_Name(self, node): |
| 2074 | if not isinstance(node.ctx, ast.Load): |
| 2075 | raise ValueError() |
| 2076 | return wrap_value(node.id) |
| 2077 | |
| 2078 | def p(name_node, default_node, default=empty): |
| 2079 | name = parse_name(name_node) |
| 2080 | if name is invalid: |
| 2081 | return None |
| 2082 | if default_node and default_node is not _empty: |
| 2083 | try: |
| 2084 | default_node = RewriteSymbolics().visit(default_node) |
| 2085 | o = ast.literal_eval(default_node) |
| 2086 | except ValueError: |
| 2087 | o = invalid |
| 2088 | if o is invalid: |
| 2089 | return None |
| 2090 | default = o if o is not invalid else default |
| 2091 | parameters.append(Parameter(name, kind, default=default, annotation=empty)) |
| 2092 | |
| 2093 | # non-keyword-only parameters |
| 2094 | args = reversed(f.args.args) |
| 2095 | defaults = reversed(f.args.defaults) |
| 2096 | iter = itertools.zip_longest(args, defaults, fillvalue=None) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2097 | if last_positional_only is not None: |
| 2098 | kind = Parameter.POSITIONAL_ONLY |
| 2099 | else: |
| 2100 | kind = Parameter.POSITIONAL_OR_KEYWORD |
| 2101 | for i, (name, default) in enumerate(reversed(list(iter))): |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2102 | p(name, default) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2103 | if i == last_positional_only: |
| 2104 | kind = Parameter.POSITIONAL_OR_KEYWORD |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2105 | |
| 2106 | # *args |
| 2107 | if f.args.vararg: |
| 2108 | kind = Parameter.VAR_POSITIONAL |
| 2109 | p(f.args.vararg, empty) |
| 2110 | |
| 2111 | # keyword-only arguments |
| 2112 | kind = Parameter.KEYWORD_ONLY |
| 2113 | for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults): |
| 2114 | p(name, default) |
| 2115 | |
| 2116 | # **kwargs |
| 2117 | if f.args.kwarg: |
| 2118 | kind = Parameter.VAR_KEYWORD |
| 2119 | p(f.args.kwarg, empty) |
| 2120 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2121 | if self_parameter is not None: |
Yury Selivanov | 8c185ee | 2014-02-21 01:32:42 -0500 | [diff] [blame] | 2122 | # Possibly strip the bound argument: |
| 2123 | # - We *always* strip first bound argument if |
| 2124 | # it is a module. |
| 2125 | # - We don't strip first bound argument if |
| 2126 | # skip_bound_arg is False. |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2127 | assert parameters |
Yury Selivanov | 8c185ee | 2014-02-21 01:32:42 -0500 | [diff] [blame] | 2128 | _self = getattr(obj, '__self__', None) |
| 2129 | self_isbound = _self is not None |
| 2130 | self_ismodule = ismodule(_self) |
| 2131 | if self_isbound and (self_ismodule or skip_bound_arg): |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2132 | parameters.pop(0) |
| 2133 | else: |
| 2134 | # for builtins, self parameter is always positional-only! |
| 2135 | p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY) |
| 2136 | parameters[0] = p |
| 2137 | |
| 2138 | return cls(parameters, return_annotation=cls.empty) |
| 2139 | |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2140 | def _get_type_hints(func, **kwargs): |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 2141 | try: |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2142 | return typing.get_type_hints(func, **kwargs) |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 2143 | except Exception: |
| 2144 | # First, try to use the get_type_hints to resolve |
| 2145 | # annotations. But for keeping the behavior intact |
| 2146 | # if there was a problem with that (like the namespace |
| 2147 | # can't resolve some annotation) continue to use |
| 2148 | # string annotations |
| 2149 | return func.__annotations__ |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2150 | |
Yury Selivanov | 57d240e | 2014-02-19 16:27:23 -0500 | [diff] [blame] | 2151 | def _signature_from_builtin(cls, func, skip_bound_arg=True): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2152 | """Private helper function to get signature for |
| 2153 | builtin callables. |
| 2154 | """ |
| 2155 | |
Yury Selivanov | 57d240e | 2014-02-19 16:27:23 -0500 | [diff] [blame] | 2156 | if not _signature_is_builtin(func): |
| 2157 | raise TypeError("{!r} is not a Python builtin " |
| 2158 | "function".format(func)) |
| 2159 | |
| 2160 | s = getattr(func, "__text_signature__", None) |
| 2161 | if not s: |
| 2162 | raise ValueError("no signature found for builtin {!r}".format(func)) |
| 2163 | |
| 2164 | return _signature_fromstr(cls, func, s, skip_bound_arg) |
| 2165 | |
| 2166 | |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2167 | def _signature_from_function(cls, func, skip_bound_arg=True, |
| 2168 | globalns=None, localns=None): |
Yury Selivanov | cf45f02 | 2015-05-20 14:38:50 -0400 | [diff] [blame] | 2169 | """Private helper: constructs Signature for the given python function.""" |
| 2170 | |
| 2171 | is_duck_function = False |
| 2172 | if not isfunction(func): |
| 2173 | if _signature_is_functionlike(func): |
| 2174 | is_duck_function = True |
| 2175 | else: |
| 2176 | # If it's not a pure Python function, and not a duck type |
| 2177 | # of pure function: |
| 2178 | raise TypeError('{!r} is not a Python function'.format(func)) |
| 2179 | |
Serhiy Storchaka | d53cf99 | 2019-05-06 22:40:27 +0300 | [diff] [blame] | 2180 | s = getattr(func, "__text_signature__", None) |
| 2181 | if s: |
| 2182 | return _signature_fromstr(cls, func, s, skip_bound_arg) |
| 2183 | |
Yury Selivanov | cf45f02 | 2015-05-20 14:38:50 -0400 | [diff] [blame] | 2184 | Parameter = cls._parameter_cls |
| 2185 | |
| 2186 | # Parameter information. |
| 2187 | func_code = func.__code__ |
| 2188 | pos_count = func_code.co_argcount |
| 2189 | arg_names = func_code.co_varnames |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2190 | posonly_count = func_code.co_posonlyargcount |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 2191 | positional = arg_names[:pos_count] |
Yury Selivanov | cf45f02 | 2015-05-20 14:38:50 -0400 | [diff] [blame] | 2192 | keyword_only_count = func_code.co_kwonlyargcount |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 2193 | keyword_only = arg_names[pos_count:pos_count + keyword_only_count] |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2194 | annotations = _get_type_hints(func, globalns=globalns, localns=localns) |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 2195 | |
Yury Selivanov | cf45f02 | 2015-05-20 14:38:50 -0400 | [diff] [blame] | 2196 | defaults = func.__defaults__ |
| 2197 | kwdefaults = func.__kwdefaults__ |
| 2198 | |
| 2199 | if defaults: |
| 2200 | pos_default_count = len(defaults) |
| 2201 | else: |
| 2202 | pos_default_count = 0 |
| 2203 | |
| 2204 | parameters = [] |
| 2205 | |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 2206 | non_default_count = pos_count - pos_default_count |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2207 | posonly_left = posonly_count |
| 2208 | |
Yury Selivanov | cf45f02 | 2015-05-20 14:38:50 -0400 | [diff] [blame] | 2209 | # Non-keyword-only parameters w/o defaults. |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 2210 | for name in positional[:non_default_count]: |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2211 | kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD |
Yury Selivanov | cf45f02 | 2015-05-20 14:38:50 -0400 | [diff] [blame] | 2212 | annotation = annotations.get(name, _empty) |
| 2213 | parameters.append(Parameter(name, annotation=annotation, |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2214 | kind=kind)) |
| 2215 | if posonly_left: |
| 2216 | posonly_left -= 1 |
Yury Selivanov | cf45f02 | 2015-05-20 14:38:50 -0400 | [diff] [blame] | 2217 | |
| 2218 | # ... w/ defaults. |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 2219 | for offset, name in enumerate(positional[non_default_count:]): |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2220 | kind = _POSITIONAL_ONLY if posonly_left else _POSITIONAL_OR_KEYWORD |
Yury Selivanov | cf45f02 | 2015-05-20 14:38:50 -0400 | [diff] [blame] | 2221 | annotation = annotations.get(name, _empty) |
| 2222 | parameters.append(Parameter(name, annotation=annotation, |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2223 | kind=kind, |
Yury Selivanov | cf45f02 | 2015-05-20 14:38:50 -0400 | [diff] [blame] | 2224 | default=defaults[offset])) |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2225 | if posonly_left: |
| 2226 | posonly_left -= 1 |
Yury Selivanov | cf45f02 | 2015-05-20 14:38:50 -0400 | [diff] [blame] | 2227 | |
| 2228 | # *args |
| 2229 | if func_code.co_flags & CO_VARARGS: |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 2230 | name = arg_names[pos_count + keyword_only_count] |
Yury Selivanov | cf45f02 | 2015-05-20 14:38:50 -0400 | [diff] [blame] | 2231 | annotation = annotations.get(name, _empty) |
| 2232 | parameters.append(Parameter(name, annotation=annotation, |
| 2233 | kind=_VAR_POSITIONAL)) |
| 2234 | |
| 2235 | # Keyword-only parameters. |
| 2236 | for name in keyword_only: |
| 2237 | default = _empty |
| 2238 | if kwdefaults is not None: |
| 2239 | default = kwdefaults.get(name, _empty) |
| 2240 | |
| 2241 | annotation = annotations.get(name, _empty) |
| 2242 | parameters.append(Parameter(name, annotation=annotation, |
| 2243 | kind=_KEYWORD_ONLY, |
| 2244 | default=default)) |
| 2245 | # **kwargs |
| 2246 | if func_code.co_flags & CO_VARKEYWORDS: |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 2247 | index = pos_count + keyword_only_count |
Yury Selivanov | cf45f02 | 2015-05-20 14:38:50 -0400 | [diff] [blame] | 2248 | if func_code.co_flags & CO_VARARGS: |
| 2249 | index += 1 |
| 2250 | |
| 2251 | name = arg_names[index] |
| 2252 | annotation = annotations.get(name, _empty) |
| 2253 | parameters.append(Parameter(name, annotation=annotation, |
| 2254 | kind=_VAR_KEYWORD)) |
| 2255 | |
| 2256 | # Is 'func' is a pure Python function - don't validate the |
| 2257 | # parameters list (for correct order and defaults), it should be OK. |
| 2258 | return cls(parameters, |
| 2259 | return_annotation=annotations.get('return', _empty), |
| 2260 | __validate_parameters__=is_duck_function) |
| 2261 | |
| 2262 | |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2263 | def _signature_from_callable(obj, *, |
| 2264 | follow_wrapper_chains=True, |
| 2265 | skip_bound_arg=True, |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2266 | globalns=None, |
| 2267 | localns=None, |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2268 | sigcls): |
| 2269 | |
| 2270 | """Private helper function to get signature for arbitrary |
| 2271 | callable objects. |
| 2272 | """ |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2273 | |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2274 | _get_signature_of = functools.partial(_signature_from_callable, |
| 2275 | follow_wrapper_chains=follow_wrapper_chains, |
| 2276 | skip_bound_arg=skip_bound_arg, |
| 2277 | globalns=globalns, |
| 2278 | localns=localns, |
| 2279 | sigcls=sigcls) |
| 2280 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2281 | if not callable(obj): |
| 2282 | raise TypeError('{!r} is not a callable object'.format(obj)) |
| 2283 | |
| 2284 | if isinstance(obj, types.MethodType): |
| 2285 | # In this case we skip the first parameter of the underlying |
| 2286 | # function (usually `self` or `cls`). |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2287 | sig = _get_signature_of(obj.__func__) |
Yury Selivanov | da39645 | 2014-03-27 12:09:24 -0400 | [diff] [blame] | 2288 | |
Yury Selivanov | 57d240e | 2014-02-19 16:27:23 -0500 | [diff] [blame] | 2289 | if skip_bound_arg: |
| 2290 | return _signature_bound_method(sig) |
| 2291 | else: |
| 2292 | return sig |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2293 | |
Nick Coghlan | e8c45d6 | 2013-07-28 20:00:01 +1000 | [diff] [blame] | 2294 | # Was this function wrapped by a decorator? |
Yury Selivanov | 57d240e | 2014-02-19 16:27:23 -0500 | [diff] [blame] | 2295 | if follow_wrapper_chains: |
| 2296 | obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__"))) |
Yury Selivanov | 46c759d | 2015-05-27 21:56:53 -0400 | [diff] [blame] | 2297 | if isinstance(obj, types.MethodType): |
| 2298 | # If the unwrapped object is a *method*, we might want to |
| 2299 | # skip its first parameter (self). |
| 2300 | # See test_signature_wrapped_bound_method for details. |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2301 | return _get_signature_of(obj) |
Nick Coghlan | e8c45d6 | 2013-07-28 20:00:01 +1000 | [diff] [blame] | 2302 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2303 | try: |
| 2304 | sig = obj.__signature__ |
| 2305 | except AttributeError: |
| 2306 | pass |
| 2307 | else: |
| 2308 | if sig is not None: |
Yury Selivanov | 42407ab | 2014-06-23 10:23:50 -0700 | [diff] [blame] | 2309 | if not isinstance(sig, Signature): |
| 2310 | raise TypeError( |
| 2311 | 'unexpected object {!r} in __signature__ ' |
| 2312 | 'attribute'.format(sig)) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2313 | return sig |
| 2314 | |
Yury Selivanov | da5fe4f | 2014-01-27 17:28:37 -0500 | [diff] [blame] | 2315 | try: |
| 2316 | partialmethod = obj._partialmethod |
| 2317 | except AttributeError: |
| 2318 | pass |
| 2319 | else: |
Yury Selivanov | 0486f81 | 2014-01-29 12:18:59 -0500 | [diff] [blame] | 2320 | if isinstance(partialmethod, functools.partialmethod): |
| 2321 | # Unbound partialmethod (see functools.partialmethod) |
| 2322 | # This means, that we need to calculate the signature |
| 2323 | # as if it's a regular partial object, but taking into |
| 2324 | # account that the first positional argument |
| 2325 | # (usually `self`, or `cls`) will not be passed |
| 2326 | # automatically (as for boundmethods) |
Yury Selivanov | da5fe4f | 2014-01-27 17:28:37 -0500 | [diff] [blame] | 2327 | |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2328 | wrapped_sig = _get_signature_of(partialmethod.func) |
Yury Selivanov | da39645 | 2014-03-27 12:09:24 -0400 | [diff] [blame] | 2329 | |
Yury Selivanov | 0486f81 | 2014-01-29 12:18:59 -0500 | [diff] [blame] | 2330 | sig = _signature_get_partial(wrapped_sig, partialmethod, (None,)) |
Yury Selivanov | 0486f81 | 2014-01-29 12:18:59 -0500 | [diff] [blame] | 2331 | first_wrapped_param = tuple(wrapped_sig.parameters.values())[0] |
Dong-hee Na | 378d706 | 2017-05-18 04:00:51 +0900 | [diff] [blame] | 2332 | if first_wrapped_param.kind is Parameter.VAR_POSITIONAL: |
| 2333 | # First argument of the wrapped callable is `*args`, as in |
| 2334 | # `partialmethod(lambda *args)`. |
| 2335 | return sig |
| 2336 | else: |
| 2337 | sig_params = tuple(sig.parameters.values()) |
Yury Selivanov | 8a38721 | 2018-03-06 12:59:45 -0500 | [diff] [blame] | 2338 | assert (not sig_params or |
| 2339 | first_wrapped_param is not sig_params[0]) |
Dong-hee Na | 378d706 | 2017-05-18 04:00:51 +0900 | [diff] [blame] | 2340 | new_params = (first_wrapped_param,) + sig_params |
| 2341 | return sig.replace(parameters=new_params) |
Yury Selivanov | da5fe4f | 2014-01-27 17:28:37 -0500 | [diff] [blame] | 2342 | |
Yury Selivanov | 63da7c7 | 2014-01-31 14:48:37 -0500 | [diff] [blame] | 2343 | if isfunction(obj) or _signature_is_functionlike(obj): |
| 2344 | # If it's a pure Python function, or an object that is duck type |
| 2345 | # of a Python function (Cython functions, for instance), then: |
Serhiy Storchaka | d53cf99 | 2019-05-06 22:40:27 +0300 | [diff] [blame] | 2346 | return _signature_from_function(sigcls, obj, |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2347 | skip_bound_arg=skip_bound_arg, |
| 2348 | globalns=globalns, localns=localns) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2349 | |
Yury Selivanov | a773de0 | 2014-02-21 18:30:53 -0500 | [diff] [blame] | 2350 | if _signature_is_builtin(obj): |
Yury Selivanov | da39645 | 2014-03-27 12:09:24 -0400 | [diff] [blame] | 2351 | return _signature_from_builtin(sigcls, obj, |
Yury Selivanov | a773de0 | 2014-02-21 18:30:53 -0500 | [diff] [blame] | 2352 | skip_bound_arg=skip_bound_arg) |
| 2353 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2354 | if isinstance(obj, functools.partial): |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2355 | wrapped_sig = _get_signature_of(obj.func) |
Yury Selivanov | 62560fb | 2014-01-28 12:26:24 -0500 | [diff] [blame] | 2356 | return _signature_get_partial(wrapped_sig, obj) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2357 | |
| 2358 | sig = None |
| 2359 | if isinstance(obj, type): |
| 2360 | # obj is a class or a metaclass |
| 2361 | |
| 2362 | # First, let's see if it has an overloaded __call__ defined |
| 2363 | # in its metaclass |
Yury Selivanov | 421f0c7 | 2014-01-29 12:05:40 -0500 | [diff] [blame] | 2364 | call = _signature_get_user_defined_method(type(obj), '__call__') |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2365 | if call is not None: |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2366 | sig = _get_signature_of(call) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2367 | else: |
| 2368 | # Now we check if the 'obj' class has a '__new__' method |
Yury Selivanov | 421f0c7 | 2014-01-29 12:05:40 -0500 | [diff] [blame] | 2369 | new = _signature_get_user_defined_method(obj, '__new__') |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2370 | if new is not None: |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2371 | sig = _get_signature_of(new) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2372 | else: |
| 2373 | # Finally, we should have at least __init__ implemented |
Yury Selivanov | 421f0c7 | 2014-01-29 12:05:40 -0500 | [diff] [blame] | 2374 | init = _signature_get_user_defined_method(obj, '__init__') |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2375 | if init is not None: |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2376 | sig = _get_signature_of(init) |
Yury Selivanov | e7dcc5e | 2014-01-27 19:29:45 -0500 | [diff] [blame] | 2377 | |
| 2378 | if sig is None: |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2379 | # At this point we know, that `obj` is a class, with no user- |
| 2380 | # defined '__init__', '__new__', or class-level '__call__' |
| 2381 | |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2382 | for base in obj.__mro__[:-1]: |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2383 | # Since '__text_signature__' is implemented as a |
| 2384 | # descriptor that extracts text signature from the |
| 2385 | # class docstring, if 'obj' is derived from a builtin |
| 2386 | # class, its own '__text_signature__' may be 'None'. |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2387 | # Therefore, we go through the MRO (except the last |
| 2388 | # class in there, which is 'object') to find the first |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2389 | # class with non-empty text signature. |
| 2390 | try: |
| 2391 | text_sig = base.__text_signature__ |
| 2392 | except AttributeError: |
| 2393 | pass |
| 2394 | else: |
| 2395 | if text_sig: |
| 2396 | # If 'obj' class has a __text_signature__ attribute: |
| 2397 | # return a signature based on it |
Yury Selivanov | da39645 | 2014-03-27 12:09:24 -0400 | [diff] [blame] | 2398 | return _signature_fromstr(sigcls, obj, text_sig) |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2399 | |
| 2400 | # No '__text_signature__' was found for the 'obj' class. |
| 2401 | # Last option is to check if its '__init__' is |
| 2402 | # object.__init__ or type.__init__. |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2403 | if type not in obj.__mro__: |
Yury Selivanov | e7dcc5e | 2014-01-27 19:29:45 -0500 | [diff] [blame] | 2404 | # We have a class (not metaclass), but no user-defined |
| 2405 | # __init__ or __new__ for it |
Yury Selivanov | bf304fc | 2015-05-30 17:08:36 -0400 | [diff] [blame] | 2406 | if (obj.__init__ is object.__init__ and |
| 2407 | obj.__new__ is object.__new__): |
Yury Selivanov | 7d2bfed | 2014-02-03 02:46:07 -0500 | [diff] [blame] | 2408 | # Return a signature of 'object' builtin. |
Gregory P. Smith | 5b9ff7a | 2019-09-13 17:13:51 +0100 | [diff] [blame] | 2409 | return sigcls.from_callable(object) |
Yury Selivanov | bf304fc | 2015-05-30 17:08:36 -0400 | [diff] [blame] | 2410 | else: |
| 2411 | raise ValueError( |
| 2412 | 'no signature found for builtin type {!r}'.format(obj)) |
Yury Selivanov | e7dcc5e | 2014-01-27 19:29:45 -0500 | [diff] [blame] | 2413 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2414 | elif not isinstance(obj, _NonUserDefinedCallables): |
| 2415 | # An object with __call__ |
| 2416 | # We also check that the 'obj' is not an instance of |
| 2417 | # _WrapperDescriptor or _MethodWrapper to avoid |
| 2418 | # infinite recursion (and even potential segfault) |
Yury Selivanov | 421f0c7 | 2014-01-29 12:05:40 -0500 | [diff] [blame] | 2419 | call = _signature_get_user_defined_method(type(obj), '__call__') |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2420 | if call is not None: |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2421 | try: |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2422 | sig = _get_signature_of(call) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2423 | except ValueError as ex: |
| 2424 | msg = 'no signature found for {!r}'.format(obj) |
| 2425 | raise ValueError(msg) from ex |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2426 | |
| 2427 | if sig is not None: |
| 2428 | # For classes and objects we skip the first parameter of their |
| 2429 | # __call__, __new__, or __init__ methods |
Yury Selivanov | 57d240e | 2014-02-19 16:27:23 -0500 | [diff] [blame] | 2430 | if skip_bound_arg: |
| 2431 | return _signature_bound_method(sig) |
| 2432 | else: |
| 2433 | return sig |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2434 | |
| 2435 | if isinstance(obj, types.BuiltinFunctionType): |
| 2436 | # Raise a nicer error message for builtins |
| 2437 | msg = 'no signature found for builtin function {!r}'.format(obj) |
| 2438 | raise ValueError(msg) |
| 2439 | |
| 2440 | raise ValueError('callable {!r} is not supported by signature'.format(obj)) |
| 2441 | |
| 2442 | |
| 2443 | class _void: |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2444 | """A private marker - used in Parameter & Signature.""" |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2445 | |
| 2446 | |
| 2447 | class _empty: |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2448 | """Marker object for Signature.empty and Parameter.empty.""" |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2449 | |
| 2450 | |
Yury Selivanov | 21e83a5 | 2014-03-27 11:23:13 -0400 | [diff] [blame] | 2451 | class _ParameterKind(enum.IntEnum): |
| 2452 | POSITIONAL_ONLY = 0 |
| 2453 | POSITIONAL_OR_KEYWORD = 1 |
| 2454 | VAR_POSITIONAL = 2 |
| 2455 | KEYWORD_ONLY = 3 |
| 2456 | VAR_KEYWORD = 4 |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2457 | |
| 2458 | def __str__(self): |
Yury Selivanov | 21e83a5 | 2014-03-27 11:23:13 -0400 | [diff] [blame] | 2459 | return self._name_ |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2460 | |
Dong-hee Na | 4aa3006 | 2018-06-08 12:46:31 +0900 | [diff] [blame] | 2461 | @property |
| 2462 | def description(self): |
| 2463 | return _PARAM_NAME_MAPPING[self] |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2464 | |
Yury Selivanov | 21e83a5 | 2014-03-27 11:23:13 -0400 | [diff] [blame] | 2465 | _POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY |
| 2466 | _POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD |
| 2467 | _VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL |
| 2468 | _KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY |
| 2469 | _VAR_KEYWORD = _ParameterKind.VAR_KEYWORD |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2470 | |
Dong-hee Na | a9cab43 | 2018-05-30 00:04:08 +0900 | [diff] [blame] | 2471 | _PARAM_NAME_MAPPING = { |
| 2472 | _POSITIONAL_ONLY: 'positional-only', |
| 2473 | _POSITIONAL_OR_KEYWORD: 'positional or keyword', |
| 2474 | _VAR_POSITIONAL: 'variadic positional', |
| 2475 | _KEYWORD_ONLY: 'keyword-only', |
| 2476 | _VAR_KEYWORD: 'variadic keyword' |
| 2477 | } |
| 2478 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2479 | |
| 2480 | class Parameter: |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2481 | """Represents a parameter in a function signature. |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2482 | |
| 2483 | Has the following public attributes: |
| 2484 | |
| 2485 | * name : str |
| 2486 | The name of the parameter as a string. |
| 2487 | * default : object |
| 2488 | The default value for the parameter if specified. If the |
Yury Selivanov | 8757ead | 2014-01-28 16:39:25 -0500 | [diff] [blame] | 2489 | parameter has no default value, this attribute is set to |
| 2490 | `Parameter.empty`. |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2491 | * annotation |
| 2492 | The annotation for the parameter if specified. If the |
Yury Selivanov | 8757ead | 2014-01-28 16:39:25 -0500 | [diff] [blame] | 2493 | parameter has no annotation, this attribute is set to |
| 2494 | `Parameter.empty`. |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2495 | * kind : str |
| 2496 | Describes how argument values are bound to the parameter. |
| 2497 | Possible values: `Parameter.POSITIONAL_ONLY`, |
| 2498 | `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`, |
| 2499 | `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`. |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2500 | """ |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2501 | |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 2502 | __slots__ = ('_name', '_kind', '_default', '_annotation') |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2503 | |
| 2504 | POSITIONAL_ONLY = _POSITIONAL_ONLY |
| 2505 | POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD |
| 2506 | VAR_POSITIONAL = _VAR_POSITIONAL |
| 2507 | KEYWORD_ONLY = _KEYWORD_ONLY |
| 2508 | VAR_KEYWORD = _VAR_KEYWORD |
| 2509 | |
| 2510 | empty = _empty |
| 2511 | |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 2512 | def __init__(self, name, kind, *, default=_empty, annotation=_empty): |
Dong-hee Na | a9cab43 | 2018-05-30 00:04:08 +0900 | [diff] [blame] | 2513 | try: |
| 2514 | self._kind = _ParameterKind(kind) |
| 2515 | except ValueError: |
| 2516 | raise ValueError(f'value {kind!r} is not a valid Parameter.kind') |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2517 | if default is not _empty: |
Dong-hee Na | a9cab43 | 2018-05-30 00:04:08 +0900 | [diff] [blame] | 2518 | if self._kind in (_VAR_POSITIONAL, _VAR_KEYWORD): |
| 2519 | msg = '{} parameters cannot have default values' |
Dong-hee Na | 4aa3006 | 2018-06-08 12:46:31 +0900 | [diff] [blame] | 2520 | msg = msg.format(self._kind.description) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2521 | raise ValueError(msg) |
| 2522 | self._default = default |
| 2523 | self._annotation = annotation |
| 2524 | |
Yury Selivanov | 2393dca | 2014-01-27 15:07:58 -0500 | [diff] [blame] | 2525 | if name is _empty: |
| 2526 | raise ValueError('name is a required attribute for Parameter') |
| 2527 | |
| 2528 | if not isinstance(name, str): |
Dong-hee Na | a9cab43 | 2018-05-30 00:04:08 +0900 | [diff] [blame] | 2529 | msg = 'name must be a str, not a {}'.format(type(name).__name__) |
| 2530 | raise TypeError(msg) |
Yury Selivanov | 2393dca | 2014-01-27 15:07:58 -0500 | [diff] [blame] | 2531 | |
Nick Coghlan | b4b966e | 2016-06-04 14:40:03 -0700 | [diff] [blame] | 2532 | if name[0] == '.' and name[1:].isdigit(): |
| 2533 | # These are implicit arguments generated by comprehensions. In |
| 2534 | # order to provide a friendlier interface to users, we recast |
| 2535 | # their name as "implicitN" and treat them as positional-only. |
| 2536 | # See issue 19611. |
Dong-hee Na | a9cab43 | 2018-05-30 00:04:08 +0900 | [diff] [blame] | 2537 | if self._kind != _POSITIONAL_OR_KEYWORD: |
| 2538 | msg = ( |
| 2539 | 'implicit arguments must be passed as ' |
| 2540 | 'positional or keyword arguments, not {}' |
Nick Coghlan | b4b966e | 2016-06-04 14:40:03 -0700 | [diff] [blame] | 2541 | ) |
Dong-hee Na | 4aa3006 | 2018-06-08 12:46:31 +0900 | [diff] [blame] | 2542 | msg = msg.format(self._kind.description) |
Dong-hee Na | a9cab43 | 2018-05-30 00:04:08 +0900 | [diff] [blame] | 2543 | raise ValueError(msg) |
Nick Coghlan | b4b966e | 2016-06-04 14:40:03 -0700 | [diff] [blame] | 2544 | self._kind = _POSITIONAL_ONLY |
| 2545 | name = 'implicit{}'.format(name[1:]) |
| 2546 | |
Yury Selivanov | 2393dca | 2014-01-27 15:07:58 -0500 | [diff] [blame] | 2547 | if not name.isidentifier(): |
| 2548 | raise ValueError('{!r} is not a valid parameter name'.format(name)) |
| 2549 | |
| 2550 | self._name = name |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2551 | |
Yury Selivanov | a5d63dd | 2014-03-27 11:31:43 -0400 | [diff] [blame] | 2552 | def __reduce__(self): |
| 2553 | return (type(self), |
| 2554 | (self._name, self._kind), |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 2555 | {'_default': self._default, |
Yury Selivanov | a5d63dd | 2014-03-27 11:31:43 -0400 | [diff] [blame] | 2556 | '_annotation': self._annotation}) |
| 2557 | |
| 2558 | def __setstate__(self, state): |
Yury Selivanov | a5d63dd | 2014-03-27 11:31:43 -0400 | [diff] [blame] | 2559 | self._default = state['_default'] |
| 2560 | self._annotation = state['_annotation'] |
| 2561 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2562 | @property |
| 2563 | def name(self): |
| 2564 | return self._name |
| 2565 | |
| 2566 | @property |
| 2567 | def default(self): |
| 2568 | return self._default |
| 2569 | |
| 2570 | @property |
| 2571 | def annotation(self): |
| 2572 | return self._annotation |
| 2573 | |
| 2574 | @property |
| 2575 | def kind(self): |
| 2576 | return self._kind |
| 2577 | |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 2578 | def replace(self, *, name=_void, kind=_void, |
| 2579 | annotation=_void, default=_void): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2580 | """Creates a customized copy of the Parameter.""" |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2581 | |
| 2582 | if name is _void: |
| 2583 | name = self._name |
| 2584 | |
| 2585 | if kind is _void: |
| 2586 | kind = self._kind |
| 2587 | |
| 2588 | if annotation is _void: |
| 2589 | annotation = self._annotation |
| 2590 | |
| 2591 | if default is _void: |
| 2592 | default = self._default |
| 2593 | |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 2594 | return type(self)(name, kind, default=default, annotation=annotation) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2595 | |
| 2596 | def __str__(self): |
| 2597 | kind = self.kind |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2598 | formatted = self._name |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2599 | |
| 2600 | # Add annotation and default value |
| 2601 | if self._annotation is not _empty: |
Dong-hee Na | 762b957 | 2017-11-16 03:30:59 +0900 | [diff] [blame] | 2602 | formatted = '{}: {}'.format(formatted, |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2603 | formatannotation(self._annotation)) |
| 2604 | |
| 2605 | if self._default is not _empty: |
Dong-hee Na | 762b957 | 2017-11-16 03:30:59 +0900 | [diff] [blame] | 2606 | if self._annotation is not _empty: |
| 2607 | formatted = '{} = {}'.format(formatted, repr(self._default)) |
| 2608 | else: |
| 2609 | formatted = '{}={}'.format(formatted, repr(self._default)) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2610 | |
| 2611 | if kind == _VAR_POSITIONAL: |
| 2612 | formatted = '*' + formatted |
| 2613 | elif kind == _VAR_KEYWORD: |
| 2614 | formatted = '**' + formatted |
| 2615 | |
| 2616 | return formatted |
| 2617 | |
| 2618 | def __repr__(self): |
Yury Selivanov | f229bc5 | 2015-05-15 12:53:56 -0400 | [diff] [blame] | 2619 | return '<{} "{}">'.format(self.__class__.__name__, self) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2620 | |
Yury Selivanov | 67ae50e | 2014-04-08 11:46:50 -0400 | [diff] [blame] | 2621 | def __hash__(self): |
Yury Selivanov | 08d4a4f | 2014-09-12 15:48:02 -0400 | [diff] [blame] | 2622 | return hash((self.name, self.kind, self.annotation, self.default)) |
Yury Selivanov | 67ae50e | 2014-04-08 11:46:50 -0400 | [diff] [blame] | 2623 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2624 | def __eq__(self, other): |
Serhiy Storchaka | 2489bd5 | 2015-07-18 23:20:50 +0300 | [diff] [blame] | 2625 | if self is other: |
| 2626 | return True |
Serhiy Storchaka | 3018cc4 | 2015-07-18 23:19:05 +0300 | [diff] [blame] | 2627 | if not isinstance(other, Parameter): |
| 2628 | return NotImplemented |
| 2629 | return (self._name == other._name and |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2630 | self._kind == other._kind and |
| 2631 | self._default == other._default and |
| 2632 | self._annotation == other._annotation) |
| 2633 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2634 | |
| 2635 | class BoundArguments: |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2636 | """Result of `Signature.bind` call. Holds the mapping of arguments |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2637 | to the function's parameters. |
| 2638 | |
| 2639 | Has the following public attributes: |
| 2640 | |
RĂ©mi Lapeyre | 2cca8ef | 2020-01-28 13:47:03 +0100 | [diff] [blame] | 2641 | * arguments : dict |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2642 | An ordered mutable mapping of parameters' names to arguments' values. |
| 2643 | Does not contain arguments' default values. |
| 2644 | * signature : Signature |
| 2645 | The Signature object that created this instance. |
| 2646 | * args : tuple |
| 2647 | Tuple of positional arguments values. |
| 2648 | * kwargs : dict |
| 2649 | Dict of keyword arguments values. |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2650 | """ |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2651 | |
Yury Selivanov | 6abe032 | 2015-05-13 17:18:41 -0400 | [diff] [blame] | 2652 | __slots__ = ('arguments', '_signature', '__weakref__') |
| 2653 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2654 | def __init__(self, signature, arguments): |
| 2655 | self.arguments = arguments |
| 2656 | self._signature = signature |
| 2657 | |
| 2658 | @property |
| 2659 | def signature(self): |
| 2660 | return self._signature |
| 2661 | |
| 2662 | @property |
| 2663 | def args(self): |
| 2664 | args = [] |
| 2665 | for param_name, param in self._signature.parameters.items(): |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 2666 | if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY): |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2667 | break |
| 2668 | |
| 2669 | try: |
| 2670 | arg = self.arguments[param_name] |
| 2671 | except KeyError: |
| 2672 | # We're done here. Other arguments |
| 2673 | # will be mapped in 'BoundArguments.kwargs' |
| 2674 | break |
| 2675 | else: |
| 2676 | if param.kind == _VAR_POSITIONAL: |
| 2677 | # *args |
| 2678 | args.extend(arg) |
| 2679 | else: |
| 2680 | # plain argument |
| 2681 | args.append(arg) |
| 2682 | |
| 2683 | return tuple(args) |
| 2684 | |
| 2685 | @property |
| 2686 | def kwargs(self): |
| 2687 | kwargs = {} |
| 2688 | kwargs_started = False |
| 2689 | for param_name, param in self._signature.parameters.items(): |
| 2690 | if not kwargs_started: |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 2691 | if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY): |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2692 | kwargs_started = True |
| 2693 | else: |
| 2694 | if param_name not in self.arguments: |
| 2695 | kwargs_started = True |
| 2696 | continue |
| 2697 | |
| 2698 | if not kwargs_started: |
| 2699 | continue |
| 2700 | |
| 2701 | try: |
| 2702 | arg = self.arguments[param_name] |
| 2703 | except KeyError: |
| 2704 | pass |
| 2705 | else: |
| 2706 | if param.kind == _VAR_KEYWORD: |
| 2707 | # **kwargs |
| 2708 | kwargs.update(arg) |
| 2709 | else: |
| 2710 | # plain keyword argument |
| 2711 | kwargs[param_name] = arg |
| 2712 | |
| 2713 | return kwargs |
| 2714 | |
Yury Selivanov | b907a51 | 2015-05-16 13:45:09 -0400 | [diff] [blame] | 2715 | def apply_defaults(self): |
| 2716 | """Set default values for missing arguments. |
| 2717 | |
| 2718 | For variable-positional arguments (*args) the default is an |
| 2719 | empty tuple. |
| 2720 | |
| 2721 | For variable-keyword arguments (**kwargs) the default is an |
| 2722 | empty dict. |
| 2723 | """ |
| 2724 | arguments = self.arguments |
Yury Selivanov | b907a51 | 2015-05-16 13:45:09 -0400 | [diff] [blame] | 2725 | new_arguments = [] |
| 2726 | for name, param in self._signature.parameters.items(): |
| 2727 | try: |
| 2728 | new_arguments.append((name, arguments[name])) |
| 2729 | except KeyError: |
| 2730 | if param.default is not _empty: |
| 2731 | val = param.default |
| 2732 | elif param.kind is _VAR_POSITIONAL: |
| 2733 | val = () |
| 2734 | elif param.kind is _VAR_KEYWORD: |
| 2735 | val = {} |
| 2736 | else: |
| 2737 | # This BoundArguments was likely produced by |
| 2738 | # Signature.bind_partial(). |
| 2739 | continue |
| 2740 | new_arguments.append((name, val)) |
RĂ©mi Lapeyre | 2cca8ef | 2020-01-28 13:47:03 +0100 | [diff] [blame] | 2741 | self.arguments = dict(new_arguments) |
Yury Selivanov | b907a51 | 2015-05-16 13:45:09 -0400 | [diff] [blame] | 2742 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2743 | def __eq__(self, other): |
Serhiy Storchaka | 2489bd5 | 2015-07-18 23:20:50 +0300 | [diff] [blame] | 2744 | if self is other: |
| 2745 | return True |
Serhiy Storchaka | 3018cc4 | 2015-07-18 23:19:05 +0300 | [diff] [blame] | 2746 | if not isinstance(other, BoundArguments): |
| 2747 | return NotImplemented |
| 2748 | return (self.signature == other.signature and |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2749 | self.arguments == other.arguments) |
| 2750 | |
Yury Selivanov | 6abe032 | 2015-05-13 17:18:41 -0400 | [diff] [blame] | 2751 | def __setstate__(self, state): |
| 2752 | self._signature = state['_signature'] |
| 2753 | self.arguments = state['arguments'] |
| 2754 | |
| 2755 | def __getstate__(self): |
| 2756 | return {'_signature': self._signature, 'arguments': self.arguments} |
| 2757 | |
Yury Selivanov | 3f6538f | 2015-05-14 18:47:17 -0400 | [diff] [blame] | 2758 | def __repr__(self): |
| 2759 | args = [] |
| 2760 | for arg, value in self.arguments.items(): |
| 2761 | args.append('{}={!r}'.format(arg, value)) |
Yury Selivanov | f229bc5 | 2015-05-15 12:53:56 -0400 | [diff] [blame] | 2762 | return '<{} ({})>'.format(self.__class__.__name__, ', '.join(args)) |
Yury Selivanov | 3f6538f | 2015-05-14 18:47:17 -0400 | [diff] [blame] | 2763 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2764 | |
| 2765 | class Signature: |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2766 | """A Signature object represents the overall signature of a function. |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2767 | It stores a Parameter object for each parameter accepted by the |
| 2768 | function, as well as information specific to the function itself. |
| 2769 | |
| 2770 | A Signature object has the following public attributes and methods: |
| 2771 | |
Jens Reidel | 611836a | 2020-03-18 03:22:46 +0100 | [diff] [blame] | 2772 | * parameters : OrderedDict |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2773 | An ordered mapping of parameters' names to the corresponding |
| 2774 | Parameter objects (keyword-only arguments are in the same order |
| 2775 | as listed in `code.co_varnames`). |
| 2776 | * return_annotation : object |
| 2777 | The annotation for the return type of the function if specified. |
| 2778 | If the function has no annotation for its return type, this |
Yury Selivanov | 8757ead | 2014-01-28 16:39:25 -0500 | [diff] [blame] | 2779 | attribute is set to `Signature.empty`. |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2780 | * bind(*args, **kwargs) -> BoundArguments |
| 2781 | Creates a mapping from positional and keyword arguments to |
| 2782 | parameters. |
| 2783 | * bind_partial(*args, **kwargs) -> BoundArguments |
| 2784 | Creates a partial mapping from positional and keyword arguments |
| 2785 | to parameters (simulating 'functools.partial' behavior.) |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2786 | """ |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2787 | |
| 2788 | __slots__ = ('_return_annotation', '_parameters') |
| 2789 | |
| 2790 | _parameter_cls = Parameter |
| 2791 | _bound_arguments_cls = BoundArguments |
| 2792 | |
| 2793 | empty = _empty |
| 2794 | |
| 2795 | def __init__(self, parameters=None, *, return_annotation=_empty, |
| 2796 | __validate_parameters__=True): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2797 | """Constructs Signature from the given list of Parameter |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2798 | objects and 'return_annotation'. All arguments are optional. |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2799 | """ |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2800 | |
| 2801 | if parameters is None: |
Jens Reidel | 611836a | 2020-03-18 03:22:46 +0100 | [diff] [blame] | 2802 | params = OrderedDict() |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2803 | else: |
| 2804 | if __validate_parameters__: |
Jens Reidel | 611836a | 2020-03-18 03:22:46 +0100 | [diff] [blame] | 2805 | params = OrderedDict() |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2806 | top_kind = _POSITIONAL_ONLY |
Yury Selivanov | 07a9e45 | 2014-01-29 10:58:16 -0500 | [diff] [blame] | 2807 | kind_defaults = False |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2808 | |
RĂ©mi Lapeyre | 2cca8ef | 2020-01-28 13:47:03 +0100 | [diff] [blame] | 2809 | for param in parameters: |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2810 | kind = param.kind |
Yury Selivanov | 2393dca | 2014-01-27 15:07:58 -0500 | [diff] [blame] | 2811 | name = param.name |
| 2812 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2813 | if kind < top_kind: |
Dong-hee Na | a9cab43 | 2018-05-30 00:04:08 +0900 | [diff] [blame] | 2814 | msg = ( |
| 2815 | 'wrong parameter order: {} parameter before {} ' |
| 2816 | 'parameter' |
| 2817 | ) |
Dong-hee Na | 4aa3006 | 2018-06-08 12:46:31 +0900 | [diff] [blame] | 2818 | msg = msg.format(top_kind.description, |
| 2819 | kind.description) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2820 | raise ValueError(msg) |
Yury Selivanov | 07a9e45 | 2014-01-29 10:58:16 -0500 | [diff] [blame] | 2821 | elif kind > top_kind: |
| 2822 | kind_defaults = False |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2823 | top_kind = kind |
| 2824 | |
Yury Selivanov | 3f73ca2 | 2014-04-08 11:30:45 -0400 | [diff] [blame] | 2825 | if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD): |
Yury Selivanov | 07a9e45 | 2014-01-29 10:58:16 -0500 | [diff] [blame] | 2826 | if param.default is _empty: |
| 2827 | if kind_defaults: |
| 2828 | # No default for this parameter, but the |
| 2829 | # previous parameter of the same kind had |
| 2830 | # a default |
| 2831 | msg = 'non-default argument follows default ' \ |
| 2832 | 'argument' |
| 2833 | raise ValueError(msg) |
| 2834 | else: |
| 2835 | # There is a default for this parameter. |
| 2836 | kind_defaults = True |
| 2837 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2838 | if name in params: |
| 2839 | msg = 'duplicate parameter name: {!r}'.format(name) |
| 2840 | raise ValueError(msg) |
Yury Selivanov | 2393dca | 2014-01-27 15:07:58 -0500 | [diff] [blame] | 2841 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2842 | params[name] = param |
| 2843 | else: |
Jens Reidel | 611836a | 2020-03-18 03:22:46 +0100 | [diff] [blame] | 2844 | params = OrderedDict((param.name, param) for param in parameters) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2845 | |
| 2846 | self._parameters = types.MappingProxyType(params) |
| 2847 | self._return_annotation = return_annotation |
| 2848 | |
| 2849 | @classmethod |
| 2850 | def from_function(cls, func): |
Matthias Bussonnier | ded87d8 | 2018-10-19 16:40:45 -0700 | [diff] [blame] | 2851 | """Constructs Signature for the given python function. |
Yury Selivanov | 57c74fc | 2015-05-20 23:07:02 -0400 | [diff] [blame] | 2852 | |
Matthias Bussonnier | ded87d8 | 2018-10-19 16:40:45 -0700 | [diff] [blame] | 2853 | Deprecated since Python 3.5, use `Signature.from_callable()`. |
| 2854 | """ |
| 2855 | |
| 2856 | warnings.warn("inspect.Signature.from_function() is deprecated since " |
| 2857 | "Python 3.5, use Signature.from_callable()", |
Berker Peksag | b560158 | 2015-05-21 23:40:54 +0300 | [diff] [blame] | 2858 | DeprecationWarning, stacklevel=2) |
Yury Selivanov | cf45f02 | 2015-05-20 14:38:50 -0400 | [diff] [blame] | 2859 | return _signature_from_function(cls, func) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2860 | |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 2861 | @classmethod |
| 2862 | def from_builtin(cls, func): |
Matthias Bussonnier | ded87d8 | 2018-10-19 16:40:45 -0700 | [diff] [blame] | 2863 | """Constructs Signature for the given builtin function. |
Yury Selivanov | 57c74fc | 2015-05-20 23:07:02 -0400 | [diff] [blame] | 2864 | |
Matthias Bussonnier | ded87d8 | 2018-10-19 16:40:45 -0700 | [diff] [blame] | 2865 | Deprecated since Python 3.5, use `Signature.from_callable()`. |
| 2866 | """ |
| 2867 | |
| 2868 | warnings.warn("inspect.Signature.from_builtin() is deprecated since " |
| 2869 | "Python 3.5, use Signature.from_callable()", |
Berker Peksag | b560158 | 2015-05-21 23:40:54 +0300 | [diff] [blame] | 2870 | DeprecationWarning, stacklevel=2) |
Yury Selivanov | 57d240e | 2014-02-19 16:27:23 -0500 | [diff] [blame] | 2871 | return _signature_from_builtin(cls, func) |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 2872 | |
Yury Selivanov | da39645 | 2014-03-27 12:09:24 -0400 | [diff] [blame] | 2873 | @classmethod |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2874 | def from_callable(cls, obj, *, |
| 2875 | follow_wrapped=True, globalns=None, localns=None): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2876 | """Constructs Signature for the given callable object.""" |
Yury Selivanov | bcd4fc1 | 2015-05-20 14:30:08 -0400 | [diff] [blame] | 2877 | return _signature_from_callable(obj, sigcls=cls, |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 2878 | follow_wrapper_chains=follow_wrapped, |
| 2879 | globalns=globalns, localns=localns) |
Yury Selivanov | da39645 | 2014-03-27 12:09:24 -0400 | [diff] [blame] | 2880 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2881 | @property |
| 2882 | def parameters(self): |
| 2883 | return self._parameters |
| 2884 | |
| 2885 | @property |
| 2886 | def return_annotation(self): |
| 2887 | return self._return_annotation |
| 2888 | |
| 2889 | def replace(self, *, parameters=_void, return_annotation=_void): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2890 | """Creates a customized copy of the Signature. |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2891 | Pass 'parameters' and/or 'return_annotation' arguments |
| 2892 | to override them in the new copy. |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2893 | """ |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2894 | |
| 2895 | if parameters is _void: |
| 2896 | parameters = self.parameters.values() |
| 2897 | |
| 2898 | if return_annotation is _void: |
| 2899 | return_annotation = self._return_annotation |
| 2900 | |
| 2901 | return type(self)(parameters, |
| 2902 | return_annotation=return_annotation) |
| 2903 | |
Yury Selivanov | 08d4a4f | 2014-09-12 15:48:02 -0400 | [diff] [blame] | 2904 | def _hash_basis(self): |
| 2905 | params = tuple(param for param in self.parameters.values() |
| 2906 | if param.kind != _KEYWORD_ONLY) |
| 2907 | |
| 2908 | kwo_params = {param.name: param for param in self.parameters.values() |
| 2909 | if param.kind == _KEYWORD_ONLY} |
| 2910 | |
| 2911 | return params, kwo_params, self.return_annotation |
| 2912 | |
Yury Selivanov | 67ae50e | 2014-04-08 11:46:50 -0400 | [diff] [blame] | 2913 | def __hash__(self): |
Yury Selivanov | 08d4a4f | 2014-09-12 15:48:02 -0400 | [diff] [blame] | 2914 | params, kwo_params, return_annotation = self._hash_basis() |
| 2915 | kwo_params = frozenset(kwo_params.values()) |
| 2916 | return hash((params, kwo_params, return_annotation)) |
Yury Selivanov | 67ae50e | 2014-04-08 11:46:50 -0400 | [diff] [blame] | 2917 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2918 | def __eq__(self, other): |
Serhiy Storchaka | 2489bd5 | 2015-07-18 23:20:50 +0300 | [diff] [blame] | 2919 | if self is other: |
| 2920 | return True |
Serhiy Storchaka | 3018cc4 | 2015-07-18 23:19:05 +0300 | [diff] [blame] | 2921 | if not isinstance(other, Signature): |
| 2922 | return NotImplemented |
Serhiy Storchaka | 2489bd5 | 2015-07-18 23:20:50 +0300 | [diff] [blame] | 2923 | return self._hash_basis() == other._hash_basis() |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2924 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2925 | def _bind(self, args, kwargs, *, partial=False): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 2926 | """Private method. Don't use directly.""" |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2927 | |
RĂ©mi Lapeyre | 2cca8ef | 2020-01-28 13:47:03 +0100 | [diff] [blame] | 2928 | arguments = {} |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2929 | |
| 2930 | parameters = iter(self.parameters.values()) |
| 2931 | parameters_ex = () |
| 2932 | arg_vals = iter(args) |
| 2933 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2934 | while True: |
| 2935 | # Let's iterate through the positional arguments and corresponding |
| 2936 | # parameters |
| 2937 | try: |
| 2938 | arg_val = next(arg_vals) |
| 2939 | except StopIteration: |
| 2940 | # No more positional arguments |
| 2941 | try: |
| 2942 | param = next(parameters) |
| 2943 | except StopIteration: |
| 2944 | # No more parameters. That's it. Just need to check that |
| 2945 | # we have no `kwargs` after this while loop |
| 2946 | break |
| 2947 | else: |
| 2948 | if param.kind == _VAR_POSITIONAL: |
| 2949 | # That's OK, just empty *args. Let's start parsing |
| 2950 | # kwargs |
| 2951 | break |
| 2952 | elif param.name in kwargs: |
| 2953 | if param.kind == _POSITIONAL_ONLY: |
| 2954 | msg = '{arg!r} parameter is positional only, ' \ |
| 2955 | 'but was passed as a keyword' |
| 2956 | msg = msg.format(arg=param.name) |
| 2957 | raise TypeError(msg) from None |
| 2958 | parameters_ex = (param,) |
| 2959 | break |
| 2960 | elif (param.kind == _VAR_KEYWORD or |
| 2961 | param.default is not _empty): |
| 2962 | # That's fine too - we have a default value for this |
| 2963 | # parameter. So, lets start parsing `kwargs`, starting |
| 2964 | # with the current parameter |
| 2965 | parameters_ex = (param,) |
| 2966 | break |
| 2967 | else: |
Yury Selivanov | 38b0d5a | 2014-01-28 17:27:39 -0500 | [diff] [blame] | 2968 | # No default, not VAR_KEYWORD, not VAR_POSITIONAL, |
| 2969 | # not in `kwargs` |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2970 | if partial: |
| 2971 | parameters_ex = (param,) |
| 2972 | break |
| 2973 | else: |
Yury Selivanov | 8687275 | 2015-05-19 00:27:49 -0400 | [diff] [blame] | 2974 | msg = 'missing a required argument: {arg!r}' |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2975 | msg = msg.format(arg=param.name) |
| 2976 | raise TypeError(msg) from None |
| 2977 | else: |
| 2978 | # We have a positional argument to process |
| 2979 | try: |
| 2980 | param = next(parameters) |
| 2981 | except StopIteration: |
| 2982 | raise TypeError('too many positional arguments') from None |
| 2983 | else: |
| 2984 | if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY): |
| 2985 | # Looks like we have no parameter for this positional |
| 2986 | # argument |
Yury Selivanov | 8687275 | 2015-05-19 00:27:49 -0400 | [diff] [blame] | 2987 | raise TypeError( |
| 2988 | 'too many positional arguments') from None |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 2989 | |
| 2990 | if param.kind == _VAR_POSITIONAL: |
| 2991 | # We have an '*args'-like argument, let's fill it with |
| 2992 | # all positional arguments we have left and move on to |
| 2993 | # the next phase |
| 2994 | values = [arg_val] |
| 2995 | values.extend(arg_vals) |
| 2996 | arguments[param.name] = tuple(values) |
| 2997 | break |
| 2998 | |
Pablo Galindo | f3ef06a | 2019-10-15 12:40:02 +0100 | [diff] [blame] | 2999 | if param.name in kwargs and param.kind != _POSITIONAL_ONLY: |
Yury Selivanov | 8687275 | 2015-05-19 00:27:49 -0400 | [diff] [blame] | 3000 | raise TypeError( |
| 3001 | 'multiple values for argument {arg!r}'.format( |
| 3002 | arg=param.name)) from None |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3003 | |
| 3004 | arguments[param.name] = arg_val |
| 3005 | |
| 3006 | # Now, we iterate through the remaining parameters to process |
| 3007 | # keyword arguments |
| 3008 | kwargs_param = None |
| 3009 | for param in itertools.chain(parameters_ex, parameters): |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3010 | if param.kind == _VAR_KEYWORD: |
| 3011 | # Memorize that we have a '**kwargs'-like parameter |
| 3012 | kwargs_param = param |
| 3013 | continue |
| 3014 | |
Yury Selivanov | 38b0d5a | 2014-01-28 17:27:39 -0500 | [diff] [blame] | 3015 | if param.kind == _VAR_POSITIONAL: |
| 3016 | # Named arguments don't refer to '*args'-like parameters. |
| 3017 | # We only arrive here if the positional arguments ended |
| 3018 | # before reaching the last parameter before *args. |
| 3019 | continue |
| 3020 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3021 | param_name = param.name |
| 3022 | try: |
| 3023 | arg_val = kwargs.pop(param_name) |
| 3024 | except KeyError: |
| 3025 | # We have no value for this parameter. It's fine though, |
| 3026 | # if it has a default value, or it is an '*args'-like |
| 3027 | # parameter, left alone by the processing of positional |
| 3028 | # arguments. |
| 3029 | if (not partial and param.kind != _VAR_POSITIONAL and |
| 3030 | param.default is _empty): |
Yury Selivanov | 8687275 | 2015-05-19 00:27:49 -0400 | [diff] [blame] | 3031 | raise TypeError('missing a required argument: {arg!r}'. \ |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3032 | format(arg=param_name)) from None |
| 3033 | |
| 3034 | else: |
Yury Selivanov | 9b9ac95 | 2014-01-28 20:54:28 -0500 | [diff] [blame] | 3035 | if param.kind == _POSITIONAL_ONLY: |
| 3036 | # This should never happen in case of a properly built |
| 3037 | # Signature object (but let's have this check here |
| 3038 | # to ensure correct behaviour just in case) |
| 3039 | raise TypeError('{arg!r} parameter is positional only, ' |
| 3040 | 'but was passed as a keyword'. \ |
| 3041 | format(arg=param.name)) |
| 3042 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3043 | arguments[param_name] = arg_val |
| 3044 | |
| 3045 | if kwargs: |
| 3046 | if kwargs_param is not None: |
| 3047 | # Process our '**kwargs'-like parameter |
| 3048 | arguments[kwargs_param.name] = kwargs |
| 3049 | else: |
Yury Selivanov | 8687275 | 2015-05-19 00:27:49 -0400 | [diff] [blame] | 3050 | raise TypeError( |
| 3051 | 'got an unexpected keyword argument {arg!r}'.format( |
| 3052 | arg=next(iter(kwargs)))) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3053 | |
| 3054 | return self._bound_arguments_cls(self, arguments) |
| 3055 | |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 3056 | def bind(self, /, *args, **kwargs): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 3057 | """Get a BoundArguments object, that maps the passed `args` |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3058 | and `kwargs` to the function's signature. Raises `TypeError` |
| 3059 | if the passed arguments can not be bound. |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 3060 | """ |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 3061 | return self._bind(args, kwargs) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3062 | |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 3063 | def bind_partial(self, /, *args, **kwargs): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 3064 | """Get a BoundArguments object, that partially maps the |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3065 | passed `args` and `kwargs` to the function's signature. |
| 3066 | Raises `TypeError` if the passed arguments can not be bound. |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 3067 | """ |
Serhiy Storchaka | 2085bd0 | 2019-06-01 11:00:15 +0300 | [diff] [blame] | 3068 | return self._bind(args, kwargs, partial=True) |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3069 | |
Yury Selivanov | a5d63dd | 2014-03-27 11:31:43 -0400 | [diff] [blame] | 3070 | def __reduce__(self): |
| 3071 | return (type(self), |
| 3072 | (tuple(self._parameters.values()),), |
| 3073 | {'_return_annotation': self._return_annotation}) |
| 3074 | |
| 3075 | def __setstate__(self, state): |
| 3076 | self._return_annotation = state['_return_annotation'] |
| 3077 | |
Yury Selivanov | 374375d | 2014-03-27 12:41:53 -0400 | [diff] [blame] | 3078 | def __repr__(self): |
Yury Selivanov | f229bc5 | 2015-05-15 12:53:56 -0400 | [diff] [blame] | 3079 | return '<{} {}>'.format(self.__class__.__name__, self) |
Yury Selivanov | 374375d | 2014-03-27 12:41:53 -0400 | [diff] [blame] | 3080 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3081 | def __str__(self): |
| 3082 | result = [] |
Yury Selivanov | 2393dca | 2014-01-27 15:07:58 -0500 | [diff] [blame] | 3083 | render_pos_only_separator = False |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3084 | render_kw_only_separator = True |
Yury Selivanov | 2393dca | 2014-01-27 15:07:58 -0500 | [diff] [blame] | 3085 | for param in self.parameters.values(): |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3086 | formatted = str(param) |
| 3087 | |
| 3088 | kind = param.kind |
Yury Selivanov | 2393dca | 2014-01-27 15:07:58 -0500 | [diff] [blame] | 3089 | |
| 3090 | if kind == _POSITIONAL_ONLY: |
| 3091 | render_pos_only_separator = True |
| 3092 | elif render_pos_only_separator: |
| 3093 | # It's not a positional-only parameter, and the flag |
| 3094 | # is set to 'True' (there were pos-only params before.) |
| 3095 | result.append('/') |
| 3096 | render_pos_only_separator = False |
| 3097 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3098 | if kind == _VAR_POSITIONAL: |
| 3099 | # OK, we have an '*args'-like parameter, so we won't need |
| 3100 | # a '*' to separate keyword-only arguments |
| 3101 | render_kw_only_separator = False |
| 3102 | elif kind == _KEYWORD_ONLY and render_kw_only_separator: |
| 3103 | # We have a keyword-only parameter to render and we haven't |
| 3104 | # rendered an '*args'-like parameter before, so add a '*' |
| 3105 | # separator to the parameters list ("foo(arg1, *, arg2)" case) |
| 3106 | result.append('*') |
| 3107 | # This condition should be only triggered once, so |
| 3108 | # reset the flag |
| 3109 | render_kw_only_separator = False |
| 3110 | |
| 3111 | result.append(formatted) |
| 3112 | |
Yury Selivanov | 2393dca | 2014-01-27 15:07:58 -0500 | [diff] [blame] | 3113 | if render_pos_only_separator: |
| 3114 | # There were only positional-only parameters, hence the |
| 3115 | # flag was not reset to 'False' |
| 3116 | result.append('/') |
| 3117 | |
Larry Hastings | 7c7cbfc | 2012-06-22 15:19:35 -0700 | [diff] [blame] | 3118 | rendered = '({})'.format(', '.join(result)) |
| 3119 | |
| 3120 | if self.return_annotation is not _empty: |
| 3121 | anno = formatannotation(self.return_annotation) |
| 3122 | rendered += ' -> {}'.format(anno) |
| 3123 | |
| 3124 | return rendered |
Nick Coghlan | f94a16b | 2013-09-22 22:46:49 +1000 | [diff] [blame] | 3125 | |
Yury Selivanov | da39645 | 2014-03-27 12:09:24 -0400 | [diff] [blame] | 3126 | |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 3127 | def signature(obj, *, follow_wrapped=True, globalns=None, localns=None): |
Yury Selivanov | 5a23bd0 | 2014-03-29 13:47:11 -0400 | [diff] [blame] | 3128 | """Get a signature object for the passed callable.""" |
Batuhan Taskaya | eee1c77 | 2020-12-24 01:45:13 +0300 | [diff] [blame] | 3129 | return Signature.from_callable(obj, follow_wrapped=follow_wrapped, |
| 3130 | globalns=globalns, localns=localns) |
Yury Selivanov | da39645 | 2014-03-27 12:09:24 -0400 | [diff] [blame] | 3131 | |
| 3132 | |
Nick Coghlan | f94a16b | 2013-09-22 22:46:49 +1000 | [diff] [blame] | 3133 | def _main(): |
| 3134 | """ Logic for inspecting an object given at command line """ |
| 3135 | import argparse |
| 3136 | import importlib |
| 3137 | |
| 3138 | parser = argparse.ArgumentParser() |
| 3139 | parser.add_argument( |
| 3140 | 'object', |
| 3141 | help="The object to be analysed. " |
| 3142 | "It supports the 'module:qualname' syntax") |
| 3143 | parser.add_argument( |
| 3144 | '-d', '--details', action='store_true', |
| 3145 | help='Display info about the module rather than its source code') |
| 3146 | |
| 3147 | args = parser.parse_args() |
| 3148 | |
| 3149 | target = args.object |
| 3150 | mod_name, has_attrs, attrs = target.partition(":") |
| 3151 | try: |
| 3152 | obj = module = importlib.import_module(mod_name) |
| 3153 | except Exception as exc: |
| 3154 | msg = "Failed to import {} ({}: {})".format(mod_name, |
| 3155 | type(exc).__name__, |
| 3156 | exc) |
| 3157 | print(msg, file=sys.stderr) |
Alan Yee | e3c59a7 | 2019-09-09 07:15:43 -0700 | [diff] [blame] | 3158 | sys.exit(2) |
Nick Coghlan | f94a16b | 2013-09-22 22:46:49 +1000 | [diff] [blame] | 3159 | |
| 3160 | if has_attrs: |
| 3161 | parts = attrs.split(".") |
| 3162 | obj = module |
| 3163 | for part in parts: |
| 3164 | obj = getattr(obj, part) |
| 3165 | |
| 3166 | if module.__name__ in sys.builtin_module_names: |
| 3167 | print("Can't get info for builtin modules.", file=sys.stderr) |
Alan Yee | e3c59a7 | 2019-09-09 07:15:43 -0700 | [diff] [blame] | 3168 | sys.exit(1) |
Nick Coghlan | f94a16b | 2013-09-22 22:46:49 +1000 | [diff] [blame] | 3169 | |
| 3170 | if args.details: |
| 3171 | print('Target: {}'.format(target)) |
| 3172 | print('Origin: {}'.format(getsourcefile(module))) |
| 3173 | print('Cached: {}'.format(module.__cached__)) |
| 3174 | if obj is module: |
| 3175 | print('Loader: {}'.format(repr(module.__loader__))) |
| 3176 | if hasattr(module, '__path__'): |
| 3177 | print('Submodule search path: {}'.format(module.__path__)) |
| 3178 | else: |
| 3179 | try: |
| 3180 | __, lineno = findsource(obj) |
| 3181 | except Exception: |
| 3182 | pass |
| 3183 | else: |
| 3184 | print('Line: {}'.format(lineno)) |
| 3185 | |
| 3186 | print('\n') |
| 3187 | else: |
| 3188 | print(getsource(obj)) |
| 3189 | |
| 3190 | |
| 3191 | if __name__ == "__main__": |
| 3192 | _main() |