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