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