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