blob: 81b1ce87098bde19cc335b91cecaf5582a0892fe [file] [log] [blame]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001"""Get useful information from live Python objects.
2
3This module encapsulates the interface provided by the internal special
Neal Norwitz221085d2007-02-25 20:55:47 +00004attributes (co_*, im_*, tb_*, etc.) in a friendlier fashion.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00005It also provides some help for examining source code and class layout.
6
7Here are some of the useful functions provided by this module:
8
Christian Heimes7131fd92008-02-19 14:21:46 +00009 ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
10 isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(),
11 isroutine() - check object types
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000012 getmembers() - get members of an object that satisfy a given condition
13
14 getfile(), getsourcefile(), getsource() - find an object's source code
15 getdoc(), getcomments() - get documentation on an object
16 getmodule() - determine the module that an object came from
17 getclasstree() - arrange classes so as to represent their hierarchy
18
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +000019 getargspec(), getargvalues(), getcallargs() - get info about function arguments
Yury Selivanov0cf3ed62014-04-01 10:17:08 -040020 getfullargspec() - same, with support for Python 3 features
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000021 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 Hastings7c7cbfc2012-06-22 15:19:35 -070025
26 signature() - get a Signature object for the callable
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000027"""
28
29# This module is in the public domain. No warranties.
30
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070031__author__ = ('Ka-Ping Yee <ping@lfw.org>',
32 'Yury Selivanov <yselivanov@sprymix.com>')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000033
Larry Hastings44e2eaa2013-11-23 15:37:55 -080034import ast
Yury Selivanov21e83a52014-03-27 11:23:13 -040035import enum
Brett Cannoncb66eb02012-05-11 12:58:42 -040036import importlib.machinery
37import itertools
Christian Heimes7131fd92008-02-19 14:21:46 +000038import linecache
Brett Cannoncb66eb02012-05-11 12:58:42 -040039import os
40import re
41import sys
42import tokenize
Larry Hastings2623c8c2014-02-08 22:15:29 -080043import token
Brett Cannoncb66eb02012-05-11 12:58:42 -040044import types
Brett Cannon2b88fcf2012-06-02 22:28:42 -040045import warnings
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070046import functools
Nick Coghlan2f92e542012-06-23 19:39:55 +100047import builtins
Raymond Hettingera1a992c2005-03-11 06:46:45 +000048from operator import attrgetter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070049from collections import namedtuple, OrderedDict
Nick Coghlan09c81232010-08-17 10:18:16 +000050
51# Create constants for the compiler flags in Include/code.h
52# We try to get them from dis to avoid duplication, but fall
Berker Peksagf23530f2014-10-19 18:04:38 +030053# back to hard-coding so the dependency is optional
Nick Coghlan09c81232010-08-17 10:18:16 +000054try:
55 from dis import COMPILER_FLAG_NAMES as _flag_names
Brett Cannoncd171c82013-07-04 17:43:24 -040056except ImportError:
Nick Coghlan09c81232010-08-17 10:18:16 +000057 CO_OPTIMIZED, CO_NEWLOCALS = 0x1, 0x2
58 CO_VARARGS, CO_VARKEYWORDS = 0x4, 0x8
59 CO_NESTED, CO_GENERATOR, CO_NOFREE = 0x10, 0x20, 0x40
60else:
61 mod_dict = globals()
62 for k, v in _flag_names.items():
63 mod_dict["CO_" + v] = k
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000064
Christian Heimesbe5b30b2008-03-03 19:18:51 +000065# See Include/object.h
66TPFLAGS_IS_ABSTRACT = 1 << 20
67
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000068# ----------------------------------------------------------- type-checking
69def ismodule(object):
70 """Return true if the object is a module.
71
72 Module objects provide these attributes:
Barry Warsaw28a691b2010-04-17 00:19:56 +000073 __cached__ pathname to byte compiled file
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000074 __doc__ documentation string
75 __file__ filename (missing for built-in modules)"""
Tim Peters28bc59f2001-09-16 08:40:16 +000076 return isinstance(object, types.ModuleType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000077
78def isclass(object):
79 """Return true if the object is a class.
80
81 Class objects provide these attributes:
82 __doc__ documentation string
83 __module__ name of module in which this class was defined"""
Benjamin Petersonc4656002009-01-17 22:41:18 +000084 return isinstance(object, type)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000085
86def ismethod(object):
87 """Return true if the object is an instance method.
88
89 Instance method objects provide these attributes:
90 __doc__ documentation string
91 __name__ name with which this method was defined
Christian Heimesff737952007-11-27 10:40:20 +000092 __func__ function object containing implementation of method
93 __self__ instance to which this method is bound"""
Tim Peters28bc59f2001-09-16 08:40:16 +000094 return isinstance(object, types.MethodType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000095
Tim Peters536d2262001-09-20 05:13:38 +000096def ismethoddescriptor(object):
Tim Petersf1d90b92001-09-20 05:47:55 +000097 """Return true if the object is a method descriptor.
98
99 But not if ismethod() or isclass() or isfunction() are true.
Tim Peters536d2262001-09-20 05:13:38 +0000100
101 This is new in Python 2.2, and, for example, is true of int.__add__.
102 An object passing this test has a __get__ attribute but not a __set__
103 attribute, but beyond that the set of attributes varies. __name__ is
104 usually sensible, and __doc__ often is.
105
Tim Petersf1d90b92001-09-20 05:47:55 +0000106 Methods implemented via descriptors that also pass one of the other
107 tests return false from the ismethoddescriptor() test, simply because
108 the other tests promise more -- you can, e.g., count on having the
Christian Heimesff737952007-11-27 10:40:20 +0000109 __func__ attribute (etc) when an object passes ismethod()."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100110 if isclass(object) or ismethod(object) or isfunction(object):
111 # mutual exclusion
112 return False
113 tp = type(object)
114 return hasattr(tp, "__get__") and not hasattr(tp, "__set__")
Tim Peters536d2262001-09-20 05:13:38 +0000115
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000116def isdatadescriptor(object):
117 """Return true if the object is a data descriptor.
118
119 Data descriptors have both a __get__ and a __set__ attribute. Examples are
120 properties (defined in Python) and getsets and members (defined in C).
121 Typically, data descriptors will also have __name__ and __doc__ attributes
122 (properties, getsets, and members have both of these attributes), but this
123 is not guaranteed."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100124 if isclass(object) or ismethod(object) or isfunction(object):
125 # mutual exclusion
126 return False
127 tp = type(object)
128 return hasattr(tp, "__set__") and hasattr(tp, "__get__")
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000129
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000130if hasattr(types, 'MemberDescriptorType'):
131 # CPython and equivalent
132 def ismemberdescriptor(object):
133 """Return true if the object is a member descriptor.
134
135 Member descriptors are specialized descriptors defined in extension
136 modules."""
137 return isinstance(object, types.MemberDescriptorType)
138else:
139 # Other implementations
140 def ismemberdescriptor(object):
141 """Return true if the object is a member descriptor.
142
143 Member descriptors are specialized descriptors defined in extension
144 modules."""
145 return False
146
147if hasattr(types, 'GetSetDescriptorType'):
148 # CPython and equivalent
149 def isgetsetdescriptor(object):
150 """Return true if the object is a getset descriptor.
151
152 getset descriptors are specialized descriptors defined in extension
153 modules."""
154 return isinstance(object, types.GetSetDescriptorType)
155else:
156 # Other implementations
157 def isgetsetdescriptor(object):
158 """Return true if the object is a getset descriptor.
159
160 getset descriptors are specialized descriptors defined in extension
161 modules."""
162 return False
163
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000164def isfunction(object):
165 """Return true if the object is a user-defined function.
166
167 Function objects provide these attributes:
168 __doc__ documentation string
169 __name__ name with which this function was defined
Neal Norwitz221085d2007-02-25 20:55:47 +0000170 __code__ code object containing compiled function bytecode
171 __defaults__ tuple of any default values for arguments
172 __globals__ global namespace in which this function was defined
173 __annotations__ dict of parameter annotations
174 __kwdefaults__ dict of keyword only parameters with defaults"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000175 return isinstance(object, types.FunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000176
Christian Heimes7131fd92008-02-19 14:21:46 +0000177def isgeneratorfunction(object):
178 """Return true if the object is a user-defined generator function.
179
180 Generator function objects provides same attributes as functions.
181
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000182 See help(isfunction) for attributes listing."""
Georg Brandlb1441c72009-01-03 22:33:39 +0000183 return bool((isfunction(object) or ismethod(object)) and
184 object.__code__.co_flags & CO_GENERATOR)
Christian Heimes7131fd92008-02-19 14:21:46 +0000185
186def isgenerator(object):
187 """Return true if the object is a generator.
188
189 Generator objects provide these attributes:
Ezio Melotti30b9d5d2013-08-17 15:50:46 +0300190 __iter__ defined to support iteration over container
Christian Heimes7131fd92008-02-19 14:21:46 +0000191 close raises a new GeneratorExit exception inside the
192 generator to terminate the iteration
193 gi_code code object
194 gi_frame frame object or possibly None once the generator has
195 been exhausted
196 gi_running set to 1 when generator is executing, 0 otherwise
197 next return the next item from the container
198 send resumes the generator and "sends" a value that becomes
199 the result of the current yield-expression
200 throw used to raise an exception inside the generator"""
201 return isinstance(object, types.GeneratorType)
202
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000203def istraceback(object):
204 """Return true if the object is a traceback.
205
206 Traceback objects provide these attributes:
207 tb_frame frame object at this level
208 tb_lasti index of last attempted instruction in bytecode
209 tb_lineno current line number in Python source code
210 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000211 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000212
213def isframe(object):
214 """Return true if the object is a frame object.
215
216 Frame objects provide these attributes:
217 f_back next outer frame object (this frame's caller)
218 f_builtins built-in namespace seen by this frame
219 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000220 f_globals global namespace seen by this frame
221 f_lasti index of last attempted instruction in bytecode
222 f_lineno current line number in Python source code
223 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000224 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000225 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000226
227def iscode(object):
228 """Return true if the object is a code object.
229
230 Code objects provide these attributes:
231 co_argcount number of arguments (not including * or ** args)
232 co_code string of raw compiled bytecode
233 co_consts tuple of constants used in the bytecode
234 co_filename name of file in which this code object was created
235 co_firstlineno number of first line in Python source code
236 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
237 co_lnotab encoded mapping of line numbers to bytecode indices
238 co_name name with which this code object was defined
239 co_names tuple of names of local variables
240 co_nlocals number of local variables
241 co_stacksize virtual machine stack space required
242 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000243 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000244
245def isbuiltin(object):
246 """Return true if the object is a built-in function or method.
247
248 Built-in functions and methods provide these attributes:
249 __doc__ documentation string
250 __name__ original name of this function or method
251 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000252 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000253
254def isroutine(object):
255 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000256 return (isbuiltin(object)
257 or isfunction(object)
258 or ismethod(object)
259 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000260
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000261def isabstract(object):
262 """Return true if the object is an abstract base class (ABC)."""
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000263 return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT)
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000264
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000265def getmembers(object, predicate=None):
266 """Return all members of an object as (name, value) pairs sorted by name.
267 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100268 if isclass(object):
269 mro = (object,) + getmro(object)
270 else:
271 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000272 results = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700273 processed = set()
274 names = dir(object)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700275 # :dd any DynamicClassAttributes to the list of names if object is a class;
Ethan Furmane03ea372013-09-25 07:14:41 -0700276 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700277 # attribute with the same name as a DynamicClassAttribute exists
Ethan Furmane03ea372013-09-25 07:14:41 -0700278 try:
279 for base in object.__bases__:
280 for k, v in base.__dict__.items():
281 if isinstance(v, types.DynamicClassAttribute):
282 names.append(k)
283 except AttributeError:
284 pass
285 for key in names:
Ethan Furman63c141c2013-10-18 00:27:39 -0700286 # First try to get the value via getattr. Some descriptors don't
287 # like calling their __get__ (see bug #1785), so fall back to
288 # looking in the __dict__.
289 try:
290 value = getattr(object, key)
291 # handle the duplicate key
292 if key in processed:
293 raise AttributeError
294 except AttributeError:
295 for base in mro:
296 if key in base.__dict__:
297 value = base.__dict__[key]
298 break
299 else:
300 # could be a (currently) missing slot member, or a buggy
301 # __dir__; discard and move on
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100302 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000303 if not predicate or predicate(value):
304 results.append((key, value))
Ethan Furmane03ea372013-09-25 07:14:41 -0700305 processed.add(key)
306 results.sort(key=lambda pair: pair[0])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000307 return results
308
Christian Heimes25bb7832008-01-11 16:17:00 +0000309Attribute = namedtuple('Attribute', 'name kind defining_class object')
310
Tim Peters13b49d32001-09-23 02:00:29 +0000311def classify_class_attrs(cls):
312 """Return list of attribute-descriptor tuples.
313
314 For each name in dir(cls), the return list contains a 4-tuple
315 with these elements:
316
317 0. The name (a string).
318
319 1. The kind of attribute this is, one of these strings:
320 'class method' created via classmethod()
321 'static method' created via staticmethod()
322 'property' created via property()
Ethan Furmane03ea372013-09-25 07:14:41 -0700323 'method' any other flavor of method or descriptor
Tim Peters13b49d32001-09-23 02:00:29 +0000324 'data' not a method
325
326 2. The class which defined this attribute (a class).
327
Ethan Furmane03ea372013-09-25 07:14:41 -0700328 3. The object as obtained by calling getattr; if this fails, or if the
329 resulting object does not live anywhere in the class' mro (including
330 metaclasses) then the object is looked up in the defining class's
331 dict (found by walking the mro).
Ethan Furman668dede2013-09-14 18:53:26 -0700332
333 If one of the items in dir(cls) is stored in the metaclass it will now
334 be discovered and not have None be listed as the class in which it was
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700335 defined. Any items whose home class cannot be discovered are skipped.
Tim Peters13b49d32001-09-23 02:00:29 +0000336 """
337
338 mro = getmro(cls)
Ethan Furman668dede2013-09-14 18:53:26 -0700339 metamro = getmro(type(cls)) # for attributes stored in the metaclass
Ethan Furmane03ea372013-09-25 07:14:41 -0700340 metamro = tuple([cls for cls in metamro if cls not in (type, object)])
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700341 class_bases = (cls,) + mro
342 all_bases = class_bases + metamro
Tim Peters13b49d32001-09-23 02:00:29 +0000343 names = dir(cls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700344 # :dd any DynamicClassAttributes to the list of names;
Ethan Furmane03ea372013-09-25 07:14:41 -0700345 # this may result in duplicate entries if, for example, a virtual
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700346 # attribute with the same name as a DynamicClassAttribute exists.
Ethan Furman63c141c2013-10-18 00:27:39 -0700347 for base in mro:
Ethan Furmane03ea372013-09-25 07:14:41 -0700348 for k, v in base.__dict__.items():
349 if isinstance(v, types.DynamicClassAttribute):
350 names.append(k)
Tim Peters13b49d32001-09-23 02:00:29 +0000351 result = []
Ethan Furmane03ea372013-09-25 07:14:41 -0700352 processed = set()
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700353
Tim Peters13b49d32001-09-23 02:00:29 +0000354 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100355 # Get the object associated with the name, and where it was defined.
Ethan Furmane03ea372013-09-25 07:14:41 -0700356 # Normal objects will be looked up with both getattr and directly in
357 # its class' dict (in case getattr fails [bug #1785], and also to look
358 # for a docstring).
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700359 # For DynamicClassAttributes on the second pass we only look in the
Ethan Furmane03ea372013-09-25 07:14:41 -0700360 # class's dict.
361 #
Tim Peters13b49d32001-09-23 02:00:29 +0000362 # Getting an obj from the __dict__ sometimes reveals more than
363 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100364 homecls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700365 get_obj = None
366 dict_obj = None
Ethan Furmane03ea372013-09-25 07:14:41 -0700367 if name not in processed:
368 try:
Ethan Furmana8b07072013-10-18 01:22:08 -0700369 if name == '__dict__':
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700370 raise Exception("__dict__ is special, don't want the proxy")
Ethan Furmane03ea372013-09-25 07:14:41 -0700371 get_obj = getattr(cls, name)
372 except Exception as exc:
373 pass
374 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700375 homecls = getattr(get_obj, "__objclass__", homecls)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700376 if homecls not in class_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700377 # if the resulting object does not live somewhere in the
Ethan Furman63c141c2013-10-18 00:27:39 -0700378 # mro, drop it and search the mro manually
Ethan Furmane03ea372013-09-25 07:14:41 -0700379 homecls = None
Ethan Furman63c141c2013-10-18 00:27:39 -0700380 last_cls = None
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700381 # first look in the classes
382 for srch_cls in class_bases:
Ethan Furman63c141c2013-10-18 00:27:39 -0700383 srch_obj = getattr(srch_cls, name, None)
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700384 if srch_obj == get_obj:
Ethan Furman63c141c2013-10-18 00:27:39 -0700385 last_cls = srch_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700386 # then check the metaclasses
387 for srch_cls in metamro:
388 try:
389 srch_obj = srch_cls.__getattr__(cls, name)
390 except AttributeError:
391 continue
392 if srch_obj == get_obj:
393 last_cls = srch_cls
Ethan Furman63c141c2013-10-18 00:27:39 -0700394 if last_cls is not None:
395 homecls = last_cls
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700396 for base in all_bases:
Ethan Furmane03ea372013-09-25 07:14:41 -0700397 if name in base.__dict__:
398 dict_obj = base.__dict__[name]
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700399 if homecls not in metamro:
400 homecls = base
Ethan Furmane03ea372013-09-25 07:14:41 -0700401 break
Ethan Furman63c141c2013-10-18 00:27:39 -0700402 if homecls is None:
403 # unable to locate the attribute anywhere, most likely due to
404 # buggy custom __dir__; discard and move on
405 continue
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700406 obj = get_obj or dict_obj
Ethan Furmane03ea372013-09-25 07:14:41 -0700407 # Classify the object or its descriptor.
Ethan Furman63c141c2013-10-18 00:27:39 -0700408 if isinstance(dict_obj, staticmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000409 kind = "static method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700410 obj = dict_obj
Ethan Furman63c141c2013-10-18 00:27:39 -0700411 elif isinstance(dict_obj, classmethod):
Tim Peters13b49d32001-09-23 02:00:29 +0000412 kind = "class method"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700413 obj = dict_obj
414 elif isinstance(dict_obj, property):
Tim Peters13b49d32001-09-23 02:00:29 +0000415 kind = "property"
Ethan Furmanb0c84cd2013-10-20 22:37:39 -0700416 obj = dict_obj
Yury Selivanov0860a0b2014-01-31 14:28:44 -0500417 elif isroutine(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000418 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100419 else:
Ethan Furmane03ea372013-09-25 07:14:41 -0700420 kind = "data"
Christian Heimes25bb7832008-01-11 16:17:00 +0000421 result.append(Attribute(name, kind, homecls, obj))
Ethan Furmane03ea372013-09-25 07:14:41 -0700422 processed.add(name)
Tim Peters13b49d32001-09-23 02:00:29 +0000423 return result
424
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000425# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000426
427def getmro(cls):
428 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000429 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000430
Nick Coghlane8c45d62013-07-28 20:00:01 +1000431# -------------------------------------------------------- function helpers
432
433def unwrap(func, *, stop=None):
434 """Get the object wrapped by *func*.
435
436 Follows the chain of :attr:`__wrapped__` attributes returning the last
437 object in the chain.
438
439 *stop* is an optional callback accepting an object in the wrapper chain
440 as its sole argument that allows the unwrapping to be terminated early if
441 the callback returns a true value. If the callback never returns a true
442 value, the last object in the chain is returned as usual. For example,
443 :func:`signature` uses this to stop unwrapping if any object in the
444 chain has a ``__signature__`` attribute defined.
445
446 :exc:`ValueError` is raised if a cycle is encountered.
447
448 """
449 if stop is None:
450 def _is_wrapper(f):
451 return hasattr(f, '__wrapped__')
452 else:
453 def _is_wrapper(f):
454 return hasattr(f, '__wrapped__') and not stop(f)
455 f = func # remember the original func for error reporting
456 memo = {id(f)} # Memoise by id to tolerate non-hashable objects
457 while _is_wrapper(func):
458 func = func.__wrapped__
459 id_func = id(func)
460 if id_func in memo:
461 raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
462 memo.add(id_func)
463 return func
464
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000465# -------------------------------------------------- source code extraction
466def indentsize(line):
467 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000468 expline = line.expandtabs()
469 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000470
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300471def _findclass(func):
472 cls = sys.modules.get(func.__module__)
473 if cls is None:
474 return None
475 for name in func.__qualname__.split('.')[:-1]:
476 cls = getattr(cls, name)
477 if not isclass(cls):
478 return None
479 return cls
480
481def _finddoc(obj):
482 if isclass(obj):
483 for base in obj.__mro__:
484 if base is not object:
485 try:
486 doc = base.__doc__
487 except AttributeError:
488 continue
489 if doc is not None:
490 return doc
491 return None
492
493 if ismethod(obj):
494 name = obj.__func__.__name__
495 self = obj.__self__
496 if (isclass(self) and
497 getattr(getattr(self, name, None), '__func__') is obj.__func__):
498 # classmethod
499 cls = self
500 else:
501 cls = self.__class__
502 elif isfunction(obj):
503 name = obj.__name__
504 cls = _findclass(obj)
505 if cls is None or getattr(cls, name) is not obj:
506 return None
507 elif isbuiltin(obj):
508 name = obj.__name__
509 self = obj.__self__
510 if (isclass(self) and
511 self.__qualname__ + '.' + name == obj.__qualname__):
512 # classmethod
513 cls = self
514 else:
515 cls = self.__class__
516 elif ismethoddescriptor(obj) or isdatadescriptor(obj):
517 name = obj.__name__
518 cls = obj.__objclass__
519 if getattr(cls, name) is not obj:
520 return None
521 elif isinstance(obj, property):
522 func = f.fget
523 name = func.__name__
524 cls = _findclass(func)
525 if cls is None or getattr(cls, name) is not obj:
526 return None
527 else:
528 return None
529
530 for base in cls.__mro__:
531 try:
532 doc = getattr(base, name).__doc__
533 except AttributeError:
534 continue
535 if doc is not None:
536 return doc
537 return None
538
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000539def getdoc(object):
540 """Get the documentation string for an object.
541
542 All tabs are expanded to spaces. To clean up docstrings that are
543 indented to line up with blocks of code, any whitespace than can be
544 uniformly removed from the second line onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000545 try:
546 doc = object.__doc__
547 except AttributeError:
548 return None
Serhiy Storchaka5cf2b722015-04-03 22:38:53 +0300549 if doc is None:
550 try:
551 doc = _finddoc(object)
552 except (AttributeError, TypeError):
553 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000554 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000555 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000556 return cleandoc(doc)
557
558def cleandoc(doc):
559 """Clean up indentation from docstrings.
560
561 Any whitespace that can be uniformly removed from the second line
562 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000563 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000564 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000565 except UnicodeError:
566 return None
567 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000568 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000569 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000570 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000571 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000572 if content:
573 indent = len(line) - content
574 margin = min(margin, indent)
575 # Remove indentation.
576 if lines:
577 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000578 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000579 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000580 # Remove any trailing or leading blank lines.
581 while lines and not lines[-1]:
582 lines.pop()
583 while lines and not lines[0]:
584 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000585 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000586
587def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000588 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000589 if ismodule(object):
590 if hasattr(object, '__file__'):
591 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000592 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000593 if isclass(object):
Yury Selivanov2eed8b72014-01-27 13:24:56 -0500594 if hasattr(object, '__module__'):
595 object = sys.modules.get(object.__module__)
596 if hasattr(object, '__file__'):
597 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000598 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000599 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000600 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000601 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000602 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000603 if istraceback(object):
604 object = object.tb_frame
605 if isframe(object):
606 object = object.f_code
607 if iscode(object):
608 return object.co_filename
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000609 raise TypeError('{!r} is not a module, class, method, '
610 'function, traceback, frame, or code object'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000611
Christian Heimes25bb7832008-01-11 16:17:00 +0000612ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')
613
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000614def getmoduleinfo(path):
615 """Get the module name, suffix, mode, and module type for a given file."""
Brett Cannoncb66eb02012-05-11 12:58:42 -0400616 warnings.warn('inspect.getmoduleinfo() is deprecated', DeprecationWarning,
617 2)
Brett Cannone4f41de2013-06-16 13:13:40 -0400618 with warnings.catch_warnings():
619 warnings.simplefilter('ignore', PendingDeprecationWarning)
620 import imp
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000621 filename = os.path.basename(path)
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000622 suffixes = [(-len(suffix), suffix, mode, mtype)
623 for suffix, mode, mtype in imp.get_suffixes()]
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000624 suffixes.sort() # try longest suffixes first, in case they overlap
625 for neglen, suffix, mode, mtype in suffixes:
626 if filename[neglen:] == suffix:
Christian Heimes25bb7832008-01-11 16:17:00 +0000627 return ModuleInfo(filename[:neglen], suffix, mode, mtype)
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000628
629def getmodulename(path):
630 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000631 fname = os.path.basename(path)
632 # Check for paths that look like an actual module file
633 suffixes = [(-len(suffix), suffix)
634 for suffix in importlib.machinery.all_suffixes()]
635 suffixes.sort() # try longest suffixes first, in case they overlap
636 for neglen, suffix in suffixes:
637 if fname.endswith(suffix):
638 return fname[:neglen]
639 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000640
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000641def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000642 """Return the filename that can be used to locate an object's source.
643 Return None if no way can be identified to get the source.
644 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000645 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400646 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
647 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
648 if any(filename.endswith(s) for s in all_bytecode_suffixes):
649 filename = (os.path.splitext(filename)[0] +
650 importlib.machinery.SOURCE_SUFFIXES[0])
651 elif any(filename.endswith(s) for s in
652 importlib.machinery.EXTENSION_SUFFIXES):
653 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000654 if os.path.exists(filename):
655 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000656 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400657 if getattr(getmodule(object, filename), '__loader__', None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000658 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000659 # or it is in the linecache
660 if filename in linecache.cache:
661 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000662
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000663def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000664 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000665
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000666 The idea is for each object to have a unique origin, so this routine
667 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000668 if _filename is None:
669 _filename = getsourcefile(object) or getfile(object)
670 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000671
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000672modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000673_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000674
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000675def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000676 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000677 if ismodule(object):
678 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000679 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000680 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000681 # Try the filename to modulename cache
682 if _filename is not None and _filename in modulesbyfile:
683 return sys.modules.get(modulesbyfile[_filename])
684 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000685 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000686 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000687 except TypeError:
688 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000689 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000690 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000691 # Update the filename to module name cache and check yet again
692 # Copy sys.modules in order to cope with changes while iterating
Éric Araujoa74f8ef2011-11-29 16:58:53 +0100693 for modname, module in list(sys.modules.items()):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000694 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000695 f = module.__file__
696 if f == _filesbymodname.get(modname, None):
697 # Have already mapped this module, so skip it
698 continue
699 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000700 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000701 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000702 modulesbyfile[f] = modulesbyfile[
703 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000704 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000705 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000706 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000707 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000708 if not hasattr(object, '__name__'):
709 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000710 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000711 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000712 if mainobject is object:
713 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000714 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000715 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000716 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000717 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000718 if builtinobject is object:
719 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000720
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000721def findsource(object):
722 """Return the entire source file and starting line number for an object.
723
724 The argument may be a module, class, method, function, traceback, frame,
725 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200726 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000727 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500728
Yury Selivanovef1e7502014-12-08 16:05:34 -0500729 file = getsourcefile(object)
730 if file:
731 # Invalidate cache if needed.
732 linecache.checkcache(file)
733 else:
734 file = getfile(object)
735 # Allow filenames in form of "<something>" to pass through.
736 # `doctest` monkeypatches `linecache` module to enable
737 # inspection, so let `linecache.getlines` to be called.
738 if not (file.startswith('<') and file.endswith('>')):
739 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500740
Thomas Wouters89f507f2006-12-13 04:49:30 +0000741 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000742 if module:
743 lines = linecache.getlines(file, module.__dict__)
744 else:
745 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000746 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200747 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000748
749 if ismodule(object):
750 return lines, 0
751
752 if isclass(object):
753 name = object.__name__
Thomas Wouters89f507f2006-12-13 04:49:30 +0000754 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
755 # make some effort to find the best matching class definition:
756 # use the one with the least indentation, which is the one
757 # that's most probably not inside a function definition.
758 candidates = []
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000759 for i in range(len(lines)):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000760 match = pat.match(lines[i])
761 if match:
762 # if it's at toplevel, it's already the best one
763 if lines[i][0] == 'c':
764 return lines, i
765 # else add whitespace to candidate list
766 candidates.append((match.group(1), i))
767 if candidates:
768 # this will sort by whitespace, and by line number,
769 # less whitespace first
770 candidates.sort()
771 return lines, candidates[0][1]
Jeremy Hyltonab919022003-06-27 18:41:20 +0000772 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200773 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000774
775 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000776 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000777 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000778 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000779 if istraceback(object):
780 object = object.tb_frame
781 if isframe(object):
782 object = object.f_code
783 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000784 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200785 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000786 lnum = object.co_firstlineno - 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000787 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000788 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000789 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000790 lnum = lnum - 1
791 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200792 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000793
794def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000795 """Get lines of comments immediately preceding an object's source code.
796
797 Returns None when source can't be found.
798 """
799 try:
800 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200801 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000802 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000803
804 if ismodule(object):
805 # Look for a comment block at the top of the file.
806 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000807 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000808 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000809 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000810 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000811 comments = []
812 end = start
813 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000814 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000815 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000816 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000817
818 # Look for a preceding block of comments at the same indentation.
819 elif lnum > 0:
820 indent = indentsize(lines[lnum])
821 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000822 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000823 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000824 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000825 if end > 0:
826 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000827 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000828 while comment[:1] == '#' and indentsize(lines[end]) == indent:
829 comments[:0] = [comment]
830 end = end - 1
831 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000832 comment = lines[end].expandtabs().lstrip()
833 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000834 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000835 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000836 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000837 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000838
Tim Peters4efb6e92001-06-29 23:51:08 +0000839class EndOfBlock(Exception): pass
840
841class BlockFinder:
842 """Provide a tokeneater() method to detect the end of a code block."""
843 def __init__(self):
844 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000845 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000846 self.started = False
847 self.passline = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000848 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000849
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000850 def tokeneater(self, type, token, srowcol, erowcol, line):
Tim Peters4efb6e92001-06-29 23:51:08 +0000851 if not self.started:
Armin Rigodd5c0232005-09-25 11:45:45 +0000852 # look for the first "def", "class" or "lambda"
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000853 if token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000854 if token == "lambda":
855 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000856 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000857 self.passline = True # skip to the end of the line
Tim Peters4efb6e92001-06-29 23:51:08 +0000858 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000859 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000860 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000861 if self.islambda: # lambdas always end at the first NEWLINE
862 raise EndOfBlock
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000863 elif self.passline:
864 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000865 elif type == tokenize.INDENT:
866 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000867 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000868 elif type == tokenize.DEDENT:
869 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000870 # the end of matching indent/dedent pairs end a block
871 # (note that this only works for "def"/"class" blocks,
872 # not e.g. for "if: else:" or "try: finally:" blocks)
873 if self.indent <= 0:
874 raise EndOfBlock
875 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
876 # any other token on the same indentation level end the previous
877 # block as well, except the pseudo-tokens COMMENT and NL.
878 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000879
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000880def getblock(lines):
881 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000882 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000883 try:
Trent Nelson428de652008-03-18 22:41:35 +0000884 tokens = tokenize.generate_tokens(iter(lines).__next__)
885 for _token in tokens:
886 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000887 except (EndOfBlock, IndentationError):
888 pass
889 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000890
891def getsourcelines(object):
892 """Return a list of source lines and starting line number for an object.
893
894 The argument may be a module, class, method, function, traceback, frame,
895 or code object. The source code is returned as a list of the lines
896 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200897 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000898 raised if the source code cannot be retrieved."""
Yury Selivanov081bbf62014-09-26 17:34:54 -0400899 object = unwrap(object)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000900 lines, lnum = findsource(object)
901
902 if ismodule(object): return lines, 0
903 else: return getblock(lines[lnum:]), lnum + 1
904
905def getsource(object):
906 """Return the text of the source code for an object.
907
908 The argument may be a module, class, method, function, traceback, frame,
909 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200910 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000911 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000912 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000913
914# --------------------------------------------------- class tree extraction
915def walktree(classes, children, parent):
916 """Recursive helper function for getclasstree()."""
917 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +0000918 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000919 for c in classes:
920 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +0000921 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000922 results.append(walktree(children[c], children, c))
923 return results
924
Georg Brandl5ce83a02009-06-01 17:23:51 +0000925def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000926 """Arrange the given list of classes into a hierarchy of nested lists.
927
928 Where a nested list appears, it contains classes derived from the class
929 whose entry immediately precedes the list. Each entry is a 2-tuple
930 containing a class and a tuple of its base classes. If the 'unique'
931 argument is true, exactly one entry appears in the returned structure
932 for each class in the given list. Otherwise, classes using multiple
933 inheritance and their descendants will appear multiple times."""
934 children = {}
935 roots = []
936 for c in classes:
937 if c.__bases__:
938 for parent in c.__bases__:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000939 if not parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000940 children[parent] = []
Serhiy Storchaka362c1b52013-09-05 17:14:32 +0300941 if c not in children[parent]:
942 children[parent].append(c)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000943 if unique and parent in classes: break
944 elif c not in roots:
945 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +0000946 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000947 if parent not in classes:
948 roots.append(parent)
949 return walktree(roots, children, None)
950
951# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +0000952Arguments = namedtuple('Arguments', 'args, varargs, varkw')
953
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000954def getargs(co):
955 """Get information about the arguments accepted by a code object.
956
Guido van Rossum2e65f892007-02-28 22:03:49 +0000957 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000958 'args' is the list of argument names. Keyword-only arguments are
959 appended. 'varargs' and 'varkw' are the names of the * and **
960 arguments or None."""
Guido van Rossum2e65f892007-02-28 22:03:49 +0000961 args, varargs, kwonlyargs, varkw = _getfullargs(co)
Christian Heimes25bb7832008-01-11 16:17:00 +0000962 return Arguments(args + kwonlyargs, varargs, varkw)
Guido van Rossum2e65f892007-02-28 22:03:49 +0000963
964def _getfullargs(co):
965 """Get information about the arguments accepted by a code object.
966
967 Four things are returned: (args, varargs, kwonlyargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000968 'args' and 'kwonlyargs' are lists of argument names, and 'varargs'
969 and 'varkw' are the names of the * and ** arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +0000970
971 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000972 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000973
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000974 nargs = co.co_argcount
975 names = co.co_varnames
Guido van Rossum2e65f892007-02-28 22:03:49 +0000976 nkwargs = co.co_kwonlyargcount
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000977 args = list(names[:nargs])
Guido van Rossum2e65f892007-02-28 22:03:49 +0000978 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000979 step = 0
980
Guido van Rossum2e65f892007-02-28 22:03:49 +0000981 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000982 varargs = None
983 if co.co_flags & CO_VARARGS:
984 varargs = co.co_varnames[nargs]
985 nargs = nargs + 1
986 varkw = None
987 if co.co_flags & CO_VARKEYWORDS:
988 varkw = co.co_varnames[nargs]
Guido van Rossum2e65f892007-02-28 22:03:49 +0000989 return args, varargs, kwonlyargs, varkw
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000990
Christian Heimes25bb7832008-01-11 16:17:00 +0000991
992ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
993
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000994def getargspec(func):
995 """Get the names and default values of a function's arguments.
996
Guido van Rossume82881c2014-07-15 12:29:11 -0700997 A tuple of four things is returned: (args, varargs, keywords, defaults).
998 'args' is a list of the argument names, including keyword-only argument names.
999 'varargs' and 'keywords' are the names of the * and ** arguments or None.
Jeremy Hylton64967882003-06-27 18:14:39 +00001000 'defaults' is an n-tuple of the default values of the last n arguments.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001001
Yury Selivanov0cf3ed62014-04-01 10:17:08 -04001002 Use the getfullargspec() API for Python 3 code, as annotations
Guido van Rossum2e65f892007-02-28 22:03:49 +00001003 and keyword arguments are supported. getargspec() will raise ValueError
1004 if the func has either annotations or keyword arguments.
1005 """
1006
1007 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
1008 getfullargspec(func)
1009 if kwonlyargs or ann:
Collin Winterce36ad82007-08-30 01:19:48 +00001010 raise ValueError("Function has keyword-only arguments or annotations"
1011 ", use getfullargspec() API which can support them")
Christian Heimes25bb7832008-01-11 16:17:00 +00001012 return ArgSpec(args, varargs, varkw, defaults)
1013
1014FullArgSpec = namedtuple('FullArgSpec',
Benjamin Peterson3d4ca742008-11-12 21:39:01 +00001015 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +00001016
1017def getfullargspec(func):
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001018 """Get the names and default values of a callable object's arguments.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001019
Brett Cannon504d8852007-09-07 02:12:14 +00001020 A tuple of seven things is returned:
1021 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults annotations).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001022 'args' is a list of the argument names.
Guido van Rossum2e65f892007-02-28 22:03:49 +00001023 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1024 'defaults' is an n-tuple of the default values of the last n arguments.
1025 'kwonlyargs' is a list of keyword-only argument names.
1026 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
1027 'annotations' is a dictionary mapping argument names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001028
Guido van Rossum2e65f892007-02-28 22:03:49 +00001029 The first four items in the tuple correspond to getargspec().
Jeremy Hylton64967882003-06-27 18:14:39 +00001030 """
1031
Yury Selivanov57d240e2014-02-19 16:27:23 -05001032 try:
1033 # Re: `skip_bound_arg=False`
1034 #
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001035 # There is a notable difference in behaviour between getfullargspec
1036 # and Signature: the former always returns 'self' parameter for bound
1037 # methods, whereas the Signature always shows the actual calling
1038 # signature of the passed object.
1039 #
1040 # To simulate this behaviour, we "unbind" bound methods, to trick
1041 # inspect.signature to always return their first parameter ("self",
1042 # usually)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001043
Yury Selivanov57d240e2014-02-19 16:27:23 -05001044 # Re: `follow_wrapper_chains=False`
1045 #
1046 # getfullargspec() historically ignored __wrapped__ attributes,
1047 # so we ensure that remains the case in 3.3+
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001048
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001049 sig = _signature_from_callable(func,
1050 follow_wrapper_chains=False,
1051 skip_bound_arg=False,
1052 sigcls=Signature)
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001053 except Exception as ex:
1054 # Most of the times 'signature' will raise ValueError.
1055 # But, it can also raise AttributeError, and, maybe something
1056 # else. So to be fully backwards compatible, we catch all
1057 # possible exceptions here, and reraise a TypeError.
1058 raise TypeError('unsupported callable') from ex
1059
1060 args = []
1061 varargs = None
1062 varkw = None
1063 kwonlyargs = []
1064 defaults = ()
1065 annotations = {}
1066 defaults = ()
1067 kwdefaults = {}
1068
1069 if sig.return_annotation is not sig.empty:
1070 annotations['return'] = sig.return_annotation
1071
1072 for param in sig.parameters.values():
1073 kind = param.kind
1074 name = param.name
1075
1076 if kind is _POSITIONAL_ONLY:
1077 args.append(name)
1078 elif kind is _POSITIONAL_OR_KEYWORD:
1079 args.append(name)
1080 if param.default is not param.empty:
1081 defaults += (param.default,)
1082 elif kind is _VAR_POSITIONAL:
1083 varargs = name
1084 elif kind is _KEYWORD_ONLY:
1085 kwonlyargs.append(name)
1086 if param.default is not param.empty:
1087 kwdefaults[name] = param.default
1088 elif kind is _VAR_KEYWORD:
1089 varkw = name
1090
1091 if param.annotation is not param.empty:
1092 annotations[name] = param.annotation
1093
1094 if not kwdefaults:
1095 # compatibility with 'func.__kwdefaults__'
1096 kwdefaults = None
1097
1098 if not defaults:
1099 # compatibility with 'func.__defaults__'
1100 defaults = None
1101
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001102 return FullArgSpec(args, varargs, varkw, defaults,
1103 kwonlyargs, kwdefaults, annotations)
1104
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001105
Christian Heimes25bb7832008-01-11 16:17:00 +00001106ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
1107
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001108def getargvalues(frame):
1109 """Get information about arguments passed into a particular frame.
1110
1111 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001112 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001113 'varargs' and 'varkw' are the names of the * and ** arguments or None.
1114 'locals' is the locals dictionary of the given frame."""
1115 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +00001116 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001117
Guido van Rossum2e65f892007-02-28 22:03:49 +00001118def formatannotation(annotation, base_module=None):
1119 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +00001120 if annotation.__module__ in ('builtins', base_module):
Serhiy Storchaka521e5862014-07-22 15:00:37 +03001121 return annotation.__qualname__
1122 return annotation.__module__+'.'+annotation.__qualname__
Guido van Rossum2e65f892007-02-28 22:03:49 +00001123 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001124
Guido van Rossum2e65f892007-02-28 22:03:49 +00001125def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001126 module = getattr(object, '__module__', None)
1127 def _formatannotation(annotation):
1128 return formatannotation(annotation, module)
1129 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +00001130
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001131def formatargspec(args, varargs=None, varkw=None, defaults=None,
Guido van Rossum2e65f892007-02-28 22:03:49 +00001132 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001133 formatarg=str,
1134 formatvarargs=lambda name: '*' + name,
1135 formatvarkw=lambda name: '**' + name,
1136 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +00001137 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001138 formatannotation=formatannotation):
Guido van Rossuma8add0e2007-05-14 22:03:55 +00001139 """Format an argument spec from the values returned by getargspec
Guido van Rossum2e65f892007-02-28 22:03:49 +00001140 or getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001141
Guido van Rossum2e65f892007-02-28 22:03:49 +00001142 The first seven arguments are (args, varargs, varkw, defaults,
1143 kwonlyargs, kwonlydefaults, annotations). The other five arguments
1144 are the corresponding optional formatting functions that are called to
1145 turn names and values into strings. The last argument is an optional
1146 function to format the sequence of arguments."""
1147 def formatargandannotation(arg):
1148 result = formatarg(arg)
1149 if arg in annotations:
1150 result += ': ' + formatannotation(annotations[arg])
1151 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001152 specs = []
1153 if defaults:
1154 firstdefault = len(args) - len(defaults)
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001155 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001156 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001157 if defaults and i >= firstdefault:
1158 spec = spec + formatvalue(defaults[i - firstdefault])
1159 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001160 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001161 specs.append(formatvarargs(formatargandannotation(varargs)))
1162 else:
1163 if kwonlyargs:
1164 specs.append('*')
1165 if kwonlyargs:
1166 for kwonlyarg in kwonlyargs:
1167 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +00001168 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001169 spec += formatvalue(kwonlydefaults[kwonlyarg])
1170 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +00001171 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +00001172 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001173 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +00001174 if 'return' in annotations:
1175 result += formatreturns(formatannotation(annotations['return']))
1176 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001177
1178def formatargvalues(args, varargs, varkw, locals,
1179 formatarg=str,
1180 formatvarargs=lambda name: '*' + name,
1181 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001182 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001183 """Format an argument spec from the 4 values returned by getargvalues.
1184
1185 The first four arguments are (args, varargs, varkw, locals). The
1186 next four arguments are the corresponding optional formatting functions
1187 that are called to turn names and values into strings. The ninth
1188 argument is an optional function to format the sequence of arguments."""
1189 def convert(name, locals=locals,
1190 formatarg=formatarg, formatvalue=formatvalue):
1191 return formatarg(name) + formatvalue(locals[name])
1192 specs = []
1193 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +00001194 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001195 if varargs:
1196 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
1197 if varkw:
1198 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +00001199 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001200
Benjamin Petersone109c702011-06-24 09:37:26 -05001201def _missing_arguments(f_name, argnames, pos, values):
1202 names = [repr(name) for name in argnames if name not in values]
1203 missing = len(names)
1204 if missing == 1:
1205 s = names[0]
1206 elif missing == 2:
1207 s = "{} and {}".format(*names)
1208 else:
Yury Selivanovdccfa132014-03-27 18:42:52 -04001209 tail = ", {} and {}".format(*names[-2:])
Benjamin Petersone109c702011-06-24 09:37:26 -05001210 del names[-2:]
1211 s = ", ".join(names) + tail
1212 raise TypeError("%s() missing %i required %s argument%s: %s" %
1213 (f_name, missing,
1214 "positional" if pos else "keyword-only",
1215 "" if missing == 1 else "s", s))
1216
1217def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -05001218 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -05001219 kwonly_given = len([arg for arg in kwonly if arg in values])
1220 if varargs:
1221 plural = atleast != 1
1222 sig = "at least %d" % (atleast,)
1223 elif defcount:
1224 plural = True
1225 sig = "from %d to %d" % (atleast, len(args))
1226 else:
1227 plural = len(args) != 1
1228 sig = str(len(args))
1229 kwonly_sig = ""
1230 if kwonly_given:
1231 msg = " positional argument%s (and %d keyword-only argument%s)"
1232 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
1233 "s" if kwonly_given != 1 else ""))
1234 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
1235 (f_name, sig, "s" if plural else "", given, kwonly_sig,
1236 "was" if given == 1 and not kwonly_given else "were"))
1237
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001238def getcallargs(*func_and_positional, **named):
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001239 """Get the mapping of arguments to values.
1240
1241 A dict is returned, with keys the function argument names (including the
1242 names of the * and ** arguments, if any), and values the respective bound
1243 values from 'positional' and 'named'."""
Benjamin Peterson3e6ab172014-01-02 12:24:08 -06001244 func = func_and_positional[0]
1245 positional = func_and_positional[1:]
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001246 spec = getfullargspec(func)
1247 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
1248 f_name = func.__name__
1249 arg2value = {}
1250
Benjamin Petersonb204a422011-06-05 22:04:07 -05001251
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001252 if ismethod(func) and func.__self__ is not None:
1253 # implicit 'self' (or 'cls' for classmethods) argument
1254 positional = (func.__self__,) + positional
1255 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001256 num_args = len(args)
1257 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001258
Benjamin Petersonb204a422011-06-05 22:04:07 -05001259 n = min(num_pos, num_args)
1260 for i in range(n):
1261 arg2value[args[i]] = positional[i]
1262 if varargs:
1263 arg2value[varargs] = tuple(positional[n:])
1264 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001265 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001266 arg2value[varkw] = {}
1267 for kw, value in named.items():
1268 if kw not in possible_kwargs:
1269 if not varkw:
1270 raise TypeError("%s() got an unexpected keyword argument %r" %
1271 (f_name, kw))
1272 arg2value[varkw][kw] = value
1273 continue
1274 if kw in arg2value:
1275 raise TypeError("%s() got multiple values for argument %r" %
1276 (f_name, kw))
1277 arg2value[kw] = value
1278 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001279 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1280 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001281 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001282 req = args[:num_args - num_defaults]
1283 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001284 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001285 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001286 for i, arg in enumerate(args[num_args - num_defaults:]):
1287 if arg not in arg2value:
1288 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001289 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001290 for kwarg in kwonlyargs:
1291 if kwarg not in arg2value:
Yury Selivanov875df202014-03-27 18:23:03 -04001292 if kwonlydefaults and kwarg in kwonlydefaults:
Benjamin Petersone109c702011-06-24 09:37:26 -05001293 arg2value[kwarg] = kwonlydefaults[kwarg]
1294 else:
1295 missing += 1
1296 if missing:
1297 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001298 return arg2value
1299
Nick Coghlan2f92e542012-06-23 19:39:55 +10001300ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1301
1302def getclosurevars(func):
1303 """
1304 Get the mapping of free variables to their current values.
1305
Meador Inge8fda3592012-07-19 21:33:21 -05001306 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001307 and builtin references as seen by the body of the function. A final
1308 set of unbound names that could not be resolved is also provided.
1309 """
1310
1311 if ismethod(func):
1312 func = func.__func__
1313
1314 if not isfunction(func):
1315 raise TypeError("'{!r}' is not a Python function".format(func))
1316
1317 code = func.__code__
1318 # Nonlocal references are named in co_freevars and resolved
1319 # by looking them up in __closure__ by positional index
1320 if func.__closure__ is None:
1321 nonlocal_vars = {}
1322 else:
1323 nonlocal_vars = {
1324 var : cell.cell_contents
1325 for var, cell in zip(code.co_freevars, func.__closure__)
1326 }
1327
1328 # Global and builtin references are named in co_names and resolved
1329 # by looking them up in __globals__ or __builtins__
1330 global_ns = func.__globals__
1331 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1332 if ismodule(builtin_ns):
1333 builtin_ns = builtin_ns.__dict__
1334 global_vars = {}
1335 builtin_vars = {}
1336 unbound_names = set()
1337 for name in code.co_names:
1338 if name in ("None", "True", "False"):
1339 # Because these used to be builtins instead of keywords, they
1340 # may still show up as name references. We ignore them.
1341 continue
1342 try:
1343 global_vars[name] = global_ns[name]
1344 except KeyError:
1345 try:
1346 builtin_vars[name] = builtin_ns[name]
1347 except KeyError:
1348 unbound_names.add(name)
1349
1350 return ClosureVars(nonlocal_vars, global_vars,
1351 builtin_vars, unbound_names)
1352
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001353# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001354
1355Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1356
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001357def getframeinfo(frame, context=1):
1358 """Get information about a frame or traceback object.
1359
1360 A tuple of five things is returned: the filename, the line number of
1361 the current line, the function name, a list of lines of context from
1362 the source code, and the index of the current line within that list.
1363 The optional second argument specifies the number of lines of context
1364 to return, which are centered around the current line."""
1365 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001366 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001367 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001368 else:
1369 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001370 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001371 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001372
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001373 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001374 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001375 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001376 try:
1377 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001378 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001379 lines = index = None
1380 else:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001381 start = max(start, 1)
Raymond Hettingera0501712004-06-15 11:22:53 +00001382 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001383 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001384 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001385 else:
1386 lines = index = None
1387
Christian Heimes25bb7832008-01-11 16:17:00 +00001388 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001389
1390def getlineno(frame):
1391 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001392 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1393 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001394
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001395FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
1396
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001397def getouterframes(frame, context=1):
1398 """Get a list of records for a frame and all higher (calling) frames.
1399
1400 Each record contains a frame object, filename, line number, function
1401 name, a list of lines of context, and index within the context."""
1402 framelist = []
1403 while frame:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001404 frameinfo = (frame,) + getframeinfo(frame, context)
1405 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001406 frame = frame.f_back
1407 return framelist
1408
1409def getinnerframes(tb, context=1):
1410 """Get a list of records for a traceback's frame and all lower frames.
1411
1412 Each record contains a frame object, filename, line number, function
1413 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001414 framelist = []
1415 while tb:
Antoine Pitroucdcafb72014-08-24 10:50:28 -04001416 frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
1417 framelist.append(FrameInfo(*frameinfo))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001418 tb = tb.tb_next
1419 return framelist
1420
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001421def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001422 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001423 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001424
1425def stack(context=1):
1426 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001427 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001428
1429def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001430 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001431 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001432
1433
1434# ------------------------------------------------ static version of getattr
1435
1436_sentinel = object()
1437
Michael Foorde5162652010-11-20 16:40:44 +00001438def _static_getmro(klass):
1439 return type.__dict__['__mro__'].__get__(klass)
1440
Michael Foord95fc51d2010-11-20 15:07:30 +00001441def _check_instance(obj, attr):
1442 instance_dict = {}
1443 try:
1444 instance_dict = object.__getattribute__(obj, "__dict__")
1445 except AttributeError:
1446 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001447 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001448
1449
1450def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001451 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001452 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001453 try:
1454 return entry.__dict__[attr]
1455 except KeyError:
1456 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001457 return _sentinel
1458
Michael Foord35184ed2010-11-20 16:58:30 +00001459def _is_type(obj):
1460 try:
1461 _static_getmro(obj)
1462 except TypeError:
1463 return False
1464 return True
1465
Michael Foorddcebe0f2011-03-15 19:20:44 -04001466def _shadowed_dict(klass):
1467 dict_attr = type.__dict__["__dict__"]
1468 for entry in _static_getmro(klass):
1469 try:
1470 class_dict = dict_attr.__get__(entry)["__dict__"]
1471 except KeyError:
1472 pass
1473 else:
1474 if not (type(class_dict) is types.GetSetDescriptorType and
1475 class_dict.__name__ == "__dict__" and
1476 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001477 return class_dict
1478 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001479
1480def getattr_static(obj, attr, default=_sentinel):
1481 """Retrieve attributes without triggering dynamic lookup via the
1482 descriptor protocol, __getattr__ or __getattribute__.
1483
1484 Note: this function may not be able to retrieve all attributes
1485 that getattr can fetch (like dynamically created attributes)
1486 and may find attributes that getattr can't (like descriptors
1487 that raise AttributeError). It can also return descriptor objects
1488 instead of instance members in some cases. See the
1489 documentation for details.
1490 """
1491 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001492 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001493 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001494 dict_attr = _shadowed_dict(klass)
1495 if (dict_attr is _sentinel or
1496 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001497 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001498 else:
1499 klass = obj
1500
1501 klass_result = _check_class(klass, attr)
1502
1503 if instance_result is not _sentinel and klass_result is not _sentinel:
1504 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1505 _check_class(type(klass_result), '__set__') is not _sentinel):
1506 return klass_result
1507
1508 if instance_result is not _sentinel:
1509 return instance_result
1510 if klass_result is not _sentinel:
1511 return klass_result
1512
1513 if obj is klass:
1514 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001515 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001516 if _shadowed_dict(type(entry)) is _sentinel:
1517 try:
1518 return entry.__dict__[attr]
1519 except KeyError:
1520 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001521 if default is not _sentinel:
1522 return default
1523 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001524
1525
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001526# ------------------------------------------------ generator introspection
1527
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001528GEN_CREATED = 'GEN_CREATED'
1529GEN_RUNNING = 'GEN_RUNNING'
1530GEN_SUSPENDED = 'GEN_SUSPENDED'
1531GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001532
1533def getgeneratorstate(generator):
1534 """Get current state of a generator-iterator.
1535
1536 Possible states are:
1537 GEN_CREATED: Waiting to start execution.
1538 GEN_RUNNING: Currently being executed by the interpreter.
1539 GEN_SUSPENDED: Currently suspended at a yield expression.
1540 GEN_CLOSED: Execution has completed.
1541 """
1542 if generator.gi_running:
1543 return GEN_RUNNING
1544 if generator.gi_frame is None:
1545 return GEN_CLOSED
1546 if generator.gi_frame.f_lasti == -1:
1547 return GEN_CREATED
1548 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001549
1550
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001551def getgeneratorlocals(generator):
1552 """
1553 Get the mapping of generator local variables to their current values.
1554
1555 A dict is returned, with the keys the local variable names and values the
1556 bound values."""
1557
1558 if not isgenerator(generator):
1559 raise TypeError("'{!r}' is not a Python generator".format(generator))
1560
1561 frame = getattr(generator, "gi_frame", None)
1562 if frame is not None:
1563 return generator.gi_frame.f_locals
1564 else:
1565 return {}
1566
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001567###############################################################################
1568### Function Signature Object (PEP 362)
1569###############################################################################
1570
1571
1572_WrapperDescriptor = type(type.__call__)
1573_MethodWrapper = type(all.__call__)
Larry Hastings5c661892014-01-24 06:17:25 -08001574_ClassMethodWrapper = type(int.__dict__['from_bytes'])
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001575
1576_NonUserDefinedCallables = (_WrapperDescriptor,
1577 _MethodWrapper,
Larry Hastings5c661892014-01-24 06:17:25 -08001578 _ClassMethodWrapper,
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001579 types.BuiltinFunctionType)
1580
1581
Yury Selivanov421f0c72014-01-29 12:05:40 -05001582def _signature_get_user_defined_method(cls, method_name):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001583 """Private helper. Checks if ``cls`` has an attribute
1584 named ``method_name`` and returns it only if it is a
1585 pure python function.
1586 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001587 try:
1588 meth = getattr(cls, method_name)
1589 except AttributeError:
1590 return
1591 else:
1592 if not isinstance(meth, _NonUserDefinedCallables):
1593 # Once '__signature__' will be added to 'C'-level
1594 # callables, this check won't be necessary
1595 return meth
1596
1597
Yury Selivanov62560fb2014-01-28 12:26:24 -05001598def _signature_get_partial(wrapped_sig, partial, extra_args=()):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001599 """Private helper to calculate how 'wrapped_sig' signature will
1600 look like after applying a 'functools.partial' object (or alike)
1601 on it.
1602 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001603
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001604 old_params = wrapped_sig.parameters
1605 new_params = OrderedDict(old_params.items())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001606
1607 partial_args = partial.args or ()
1608 partial_keywords = partial.keywords or {}
1609
1610 if extra_args:
1611 partial_args = extra_args + partial_args
1612
1613 try:
1614 ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
1615 except TypeError as ex:
1616 msg = 'partial object {!r} has incorrect arguments'.format(partial)
1617 raise ValueError(msg) from ex
1618
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001619
Yury Selivanov3f73ca22014-04-08 11:30:45 -04001620 transform_to_kwonly = False
1621 for param_name, param in old_params.items():
1622 try:
1623 arg_value = ba.arguments[param_name]
1624 except KeyError:
1625 pass
1626 else:
1627 if param.kind is _POSITIONAL_ONLY:
1628 # If positional-only parameter is bound by partial,
1629 # it effectively disappears from the signature
1630 new_params.pop(param_name)
1631 continue
1632
1633 if param.kind is _POSITIONAL_OR_KEYWORD:
1634 if param_name in partial_keywords:
1635 # This means that this parameter, and all parameters
1636 # after it should be keyword-only (and var-positional
1637 # should be removed). Here's why. Consider the following
1638 # function:
1639 # foo(a, b, *args, c):
1640 # pass
1641 #
1642 # "partial(foo, a='spam')" will have the following
1643 # signature: "(*, a='spam', b, c)". Because attempting
1644 # to call that partial with "(10, 20)" arguments will
1645 # raise a TypeError, saying that "a" argument received
1646 # multiple values.
1647 transform_to_kwonly = True
1648 # Set the new default value
1649 new_params[param_name] = param.replace(default=arg_value)
1650 else:
1651 # was passed as a positional argument
1652 new_params.pop(param.name)
1653 continue
1654
1655 if param.kind is _KEYWORD_ONLY:
1656 # Set the new default value
1657 new_params[param_name] = param.replace(default=arg_value)
1658
1659 if transform_to_kwonly:
1660 assert param.kind is not _POSITIONAL_ONLY
1661
1662 if param.kind is _POSITIONAL_OR_KEYWORD:
1663 new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
1664 new_params[param_name] = new_param
1665 new_params.move_to_end(param_name)
1666 elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
1667 new_params.move_to_end(param_name)
1668 elif param.kind is _VAR_POSITIONAL:
1669 new_params.pop(param.name)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05001670
1671 return wrapped_sig.replace(parameters=new_params.values())
1672
1673
Yury Selivanov62560fb2014-01-28 12:26:24 -05001674def _signature_bound_method(sig):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001675 """Private helper to transform signatures for unbound
1676 functions to bound methods.
1677 """
Yury Selivanov62560fb2014-01-28 12:26:24 -05001678
1679 params = tuple(sig.parameters.values())
1680
1681 if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1682 raise ValueError('invalid method signature')
1683
1684 kind = params[0].kind
1685 if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
1686 # Drop first parameter:
1687 # '(p1, p2[, ...])' -> '(p2[, ...])'
1688 params = params[1:]
1689 else:
1690 if kind is not _VAR_POSITIONAL:
1691 # Unless we add a new parameter type we never
1692 # get here
1693 raise ValueError('invalid argument type')
1694 # It's a var-positional parameter.
1695 # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
1696
1697 return sig.replace(parameters=params)
1698
1699
Yury Selivanovb77511d2014-01-29 10:46:14 -05001700def _signature_is_builtin(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001701 """Private helper to test if `obj` is a callable that might
1702 support Argument Clinic's __text_signature__ protocol.
1703 """
Yury Selivanov1d241832014-02-02 12:51:20 -05001704 return (isbuiltin(obj) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001705 ismethoddescriptor(obj) or
Yury Selivanov1d241832014-02-02 12:51:20 -05001706 isinstance(obj, _NonUserDefinedCallables) or
Yury Selivanovb77511d2014-01-29 10:46:14 -05001707 # Can't test 'isinstance(type)' here, as it would
1708 # also be True for regular python classes
1709 obj in (type, object))
1710
1711
Yury Selivanov63da7c72014-01-31 14:48:37 -05001712def _signature_is_functionlike(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001713 """Private helper to test if `obj` is a duck type of FunctionType.
1714 A good example of such objects are functions compiled with
1715 Cython, which have all attributes that a pure Python function
1716 would have, but have their code statically compiled.
1717 """
Yury Selivanov63da7c72014-01-31 14:48:37 -05001718
1719 if not callable(obj) or isclass(obj):
1720 # All function-like objects are obviously callables,
1721 # and not classes.
1722 return False
1723
1724 name = getattr(obj, '__name__', None)
1725 code = getattr(obj, '__code__', None)
1726 defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
1727 kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
1728 annotations = getattr(obj, '__annotations__', None)
1729
1730 return (isinstance(code, types.CodeType) and
1731 isinstance(name, str) and
1732 (defaults is None or isinstance(defaults, tuple)) and
1733 (kwdefaults is None or isinstance(kwdefaults, dict)) and
1734 isinstance(annotations, dict))
1735
1736
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001737def _signature_get_bound_param(spec):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001738 """ Private helper to get first parameter name from a
1739 __text_signature__ of a builtin method, which should
1740 be in the following format: '($param1, ...)'.
1741 Assumptions are that the first argument won't have
1742 a default value or an annotation.
1743 """
Yury Selivanovd82eddc2014-01-29 11:24:39 -05001744
1745 assert spec.startswith('($')
1746
1747 pos = spec.find(',')
1748 if pos == -1:
1749 pos = spec.find(')')
1750
1751 cpos = spec.find(':')
1752 assert cpos == -1 or cpos > pos
1753
1754 cpos = spec.find('=')
1755 assert cpos == -1 or cpos > pos
1756
1757 return spec[2:pos]
1758
1759
Larry Hastings2623c8c2014-02-08 22:15:29 -08001760def _signature_strip_non_python_syntax(signature):
1761 """
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001762 Private helper function. Takes a signature in Argument Clinic's
1763 extended signature format.
1764
Larry Hastings2623c8c2014-02-08 22:15:29 -08001765 Returns a tuple of three things:
1766 * that signature re-rendered in standard Python syntax,
1767 * the index of the "self" parameter (generally 0), or None if
1768 the function does not have a "self" parameter, and
1769 * the index of the last "positional only" parameter,
1770 or None if the signature has no positional-only parameters.
1771 """
1772
1773 if not signature:
1774 return signature, None, None
1775
1776 self_parameter = None
1777 last_positional_only = None
1778
1779 lines = [l.encode('ascii') for l in signature.split('\n')]
1780 generator = iter(lines).__next__
1781 token_stream = tokenize.tokenize(generator)
1782
1783 delayed_comma = False
1784 skip_next_comma = False
1785 text = []
1786 add = text.append
1787
1788 current_parameter = 0
1789 OP = token.OP
1790 ERRORTOKEN = token.ERRORTOKEN
1791
1792 # token stream always starts with ENCODING token, skip it
1793 t = next(token_stream)
1794 assert t.type == tokenize.ENCODING
1795
1796 for t in token_stream:
1797 type, string = t.type, t.string
1798
1799 if type == OP:
1800 if string == ',':
1801 if skip_next_comma:
1802 skip_next_comma = False
1803 else:
1804 assert not delayed_comma
1805 delayed_comma = True
1806 current_parameter += 1
1807 continue
1808
1809 if string == '/':
1810 assert not skip_next_comma
1811 assert last_positional_only is None
1812 skip_next_comma = True
1813 last_positional_only = current_parameter - 1
1814 continue
1815
1816 if (type == ERRORTOKEN) and (string == '$'):
1817 assert self_parameter is None
1818 self_parameter = current_parameter
1819 continue
1820
1821 if delayed_comma:
1822 delayed_comma = False
1823 if not ((type == OP) and (string == ')')):
1824 add(', ')
1825 add(string)
1826 if (string == ','):
1827 add(' ')
1828 clean_signature = ''.join(text)
1829 return clean_signature, self_parameter, last_positional_only
1830
1831
Yury Selivanov57d240e2014-02-19 16:27:23 -05001832def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001833 """Private helper to parse content of '__text_signature__'
1834 and return a Signature based on it.
1835 """
1836
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001837 Parameter = cls._parameter_cls
1838
Larry Hastings2623c8c2014-02-08 22:15:29 -08001839 clean_signature, self_parameter, last_positional_only = \
1840 _signature_strip_non_python_syntax(s)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001841
Larry Hastings2623c8c2014-02-08 22:15:29 -08001842 program = "def foo" + clean_signature + ": pass"
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001843
1844 try:
Larry Hastings2623c8c2014-02-08 22:15:29 -08001845 module = ast.parse(program)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001846 except SyntaxError:
1847 module = None
1848
1849 if not isinstance(module, ast.Module):
1850 raise ValueError("{!r} builtin has invalid signature".format(obj))
1851
1852 f = module.body[0]
1853
1854 parameters = []
1855 empty = Parameter.empty
1856 invalid = object()
1857
1858 module = None
1859 module_dict = {}
1860 module_name = getattr(obj, '__module__', None)
1861 if module_name:
1862 module = sys.modules.get(module_name, None)
1863 if module:
1864 module_dict = module.__dict__
1865 sys_module_dict = sys.modules
1866
1867 def parse_name(node):
1868 assert isinstance(node, ast.arg)
1869 if node.annotation != None:
1870 raise ValueError("Annotations are not currently supported")
1871 return node.arg
1872
1873 def wrap_value(s):
1874 try:
1875 value = eval(s, module_dict)
1876 except NameError:
1877 try:
1878 value = eval(s, sys_module_dict)
1879 except NameError:
1880 raise RuntimeError()
1881
1882 if isinstance(value, str):
1883 return ast.Str(value)
1884 if isinstance(value, (int, float)):
1885 return ast.Num(value)
1886 if isinstance(value, bytes):
1887 return ast.Bytes(value)
1888 if value in (True, False, None):
1889 return ast.NameConstant(value)
1890 raise RuntimeError()
1891
1892 class RewriteSymbolics(ast.NodeTransformer):
1893 def visit_Attribute(self, node):
1894 a = []
1895 n = node
1896 while isinstance(n, ast.Attribute):
1897 a.append(n.attr)
1898 n = n.value
1899 if not isinstance(n, ast.Name):
1900 raise RuntimeError()
1901 a.append(n.id)
1902 value = ".".join(reversed(a))
1903 return wrap_value(value)
1904
1905 def visit_Name(self, node):
1906 if not isinstance(node.ctx, ast.Load):
1907 raise ValueError()
1908 return wrap_value(node.id)
1909
1910 def p(name_node, default_node, default=empty):
1911 name = parse_name(name_node)
1912 if name is invalid:
1913 return None
1914 if default_node and default_node is not _empty:
1915 try:
1916 default_node = RewriteSymbolics().visit(default_node)
1917 o = ast.literal_eval(default_node)
1918 except ValueError:
1919 o = invalid
1920 if o is invalid:
1921 return None
1922 default = o if o is not invalid else default
1923 parameters.append(Parameter(name, kind, default=default, annotation=empty))
1924
1925 # non-keyword-only parameters
1926 args = reversed(f.args.args)
1927 defaults = reversed(f.args.defaults)
1928 iter = itertools.zip_longest(args, defaults, fillvalue=None)
Larry Hastings2623c8c2014-02-08 22:15:29 -08001929 if last_positional_only is not None:
1930 kind = Parameter.POSITIONAL_ONLY
1931 else:
1932 kind = Parameter.POSITIONAL_OR_KEYWORD
1933 for i, (name, default) in enumerate(reversed(list(iter))):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001934 p(name, default)
Larry Hastings2623c8c2014-02-08 22:15:29 -08001935 if i == last_positional_only:
1936 kind = Parameter.POSITIONAL_OR_KEYWORD
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001937
1938 # *args
1939 if f.args.vararg:
1940 kind = Parameter.VAR_POSITIONAL
1941 p(f.args.vararg, empty)
1942
1943 # keyword-only arguments
1944 kind = Parameter.KEYWORD_ONLY
1945 for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
1946 p(name, default)
1947
1948 # **kwargs
1949 if f.args.kwarg:
1950 kind = Parameter.VAR_KEYWORD
1951 p(f.args.kwarg, empty)
1952
Larry Hastings2623c8c2014-02-08 22:15:29 -08001953 if self_parameter is not None:
Yury Selivanov8c185ee2014-02-21 01:32:42 -05001954 # Possibly strip the bound argument:
1955 # - We *always* strip first bound argument if
1956 # it is a module.
1957 # - We don't strip first bound argument if
1958 # skip_bound_arg is False.
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001959 assert parameters
Yury Selivanov8c185ee2014-02-21 01:32:42 -05001960 _self = getattr(obj, '__self__', None)
1961 self_isbound = _self is not None
1962 self_ismodule = ismodule(_self)
1963 if self_isbound and (self_ismodule or skip_bound_arg):
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05001964 parameters.pop(0)
1965 else:
1966 # for builtins, self parameter is always positional-only!
1967 p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
1968 parameters[0] = p
1969
1970 return cls(parameters, return_annotation=cls.empty)
1971
1972
Yury Selivanov57d240e2014-02-19 16:27:23 -05001973def _signature_from_builtin(cls, func, skip_bound_arg=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001974 """Private helper function to get signature for
1975 builtin callables.
1976 """
1977
Yury Selivanov57d240e2014-02-19 16:27:23 -05001978 if not _signature_is_builtin(func):
1979 raise TypeError("{!r} is not a Python builtin "
1980 "function".format(func))
1981
1982 s = getattr(func, "__text_signature__", None)
1983 if not s:
1984 raise ValueError("no signature found for builtin {!r}".format(func))
1985
1986 return _signature_fromstr(cls, func, s, skip_bound_arg)
1987
1988
Yury Selivanov5a23bd02014-03-29 13:47:11 -04001989def _signature_from_callable(obj, *,
1990 follow_wrapper_chains=True,
1991 skip_bound_arg=True,
1992 sigcls):
1993
1994 """Private helper function to get signature for arbitrary
1995 callable objects.
1996 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001997
1998 if not callable(obj):
1999 raise TypeError('{!r} is not a callable object'.format(obj))
2000
2001 if isinstance(obj, types.MethodType):
2002 # In this case we skip the first parameter of the underlying
2003 # function (usually `self` or `cls`).
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002004 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002005 obj.__func__,
2006 follow_wrapper_chains=follow_wrapper_chains,
2007 skip_bound_arg=skip_bound_arg,
2008 sigcls=sigcls)
2009
Yury Selivanov57d240e2014-02-19 16:27:23 -05002010 if skip_bound_arg:
2011 return _signature_bound_method(sig)
2012 else:
2013 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002014
Nick Coghlane8c45d62013-07-28 20:00:01 +10002015 # Was this function wrapped by a decorator?
Yury Selivanov57d240e2014-02-19 16:27:23 -05002016 if follow_wrapper_chains:
2017 obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
Nick Coghlane8c45d62013-07-28 20:00:01 +10002018
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002019 try:
2020 sig = obj.__signature__
2021 except AttributeError:
2022 pass
2023 else:
2024 if sig is not None:
Yury Selivanov42407ab2014-06-23 10:23:50 -07002025 if not isinstance(sig, Signature):
2026 raise TypeError(
2027 'unexpected object {!r} in __signature__ '
2028 'attribute'.format(sig))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002029 return sig
2030
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002031 try:
2032 partialmethod = obj._partialmethod
2033 except AttributeError:
2034 pass
2035 else:
Yury Selivanov0486f812014-01-29 12:18:59 -05002036 if isinstance(partialmethod, functools.partialmethod):
2037 # Unbound partialmethod (see functools.partialmethod)
2038 # This means, that we need to calculate the signature
2039 # as if it's a regular partial object, but taking into
2040 # account that the first positional argument
2041 # (usually `self`, or `cls`) will not be passed
2042 # automatically (as for boundmethods)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002043
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002044 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002045 partialmethod.func,
2046 follow_wrapper_chains=follow_wrapper_chains,
2047 skip_bound_arg=skip_bound_arg,
2048 sigcls=sigcls)
2049
Yury Selivanov0486f812014-01-29 12:18:59 -05002050 sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002051
Yury Selivanov0486f812014-01-29 12:18:59 -05002052 first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
2053 new_params = (first_wrapped_param,) + tuple(sig.parameters.values())
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002054
Yury Selivanov0486f812014-01-29 12:18:59 -05002055 return sig.replace(parameters=new_params)
Yury Selivanovda5fe4f2014-01-27 17:28:37 -05002056
Yury Selivanov63da7c72014-01-31 14:48:37 -05002057 if isfunction(obj) or _signature_is_functionlike(obj):
2058 # If it's a pure Python function, or an object that is duck type
2059 # of a Python function (Cython functions, for instance), then:
Yury Selivanovda396452014-03-27 12:09:24 -04002060 return sigcls.from_function(obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002061
Yury Selivanova773de02014-02-21 18:30:53 -05002062 if _signature_is_builtin(obj):
Yury Selivanovda396452014-03-27 12:09:24 -04002063 return _signature_from_builtin(sigcls, obj,
Yury Selivanova773de02014-02-21 18:30:53 -05002064 skip_bound_arg=skip_bound_arg)
2065
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002066 if isinstance(obj, functools.partial):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002067 wrapped_sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002068 obj.func,
2069 follow_wrapper_chains=follow_wrapper_chains,
2070 skip_bound_arg=skip_bound_arg,
2071 sigcls=sigcls)
Yury Selivanov62560fb2014-01-28 12:26:24 -05002072 return _signature_get_partial(wrapped_sig, obj)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002073
2074 sig = None
2075 if isinstance(obj, type):
2076 # obj is a class or a metaclass
2077
2078 # First, let's see if it has an overloaded __call__ defined
2079 # in its metaclass
Yury Selivanov421f0c72014-01-29 12:05:40 -05002080 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002081 if call is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002082 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002083 call,
2084 follow_wrapper_chains=follow_wrapper_chains,
2085 skip_bound_arg=skip_bound_arg,
2086 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002087 else:
2088 # Now we check if the 'obj' class has a '__new__' method
Yury Selivanov421f0c72014-01-29 12:05:40 -05002089 new = _signature_get_user_defined_method(obj, '__new__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002090 if new is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002091 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002092 new,
2093 follow_wrapper_chains=follow_wrapper_chains,
2094 skip_bound_arg=skip_bound_arg,
2095 sigcls=sigcls)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002096 else:
2097 # Finally, we should have at least __init__ implemented
Yury Selivanov421f0c72014-01-29 12:05:40 -05002098 init = _signature_get_user_defined_method(obj, '__init__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002099 if init is not None:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002100 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002101 init,
2102 follow_wrapper_chains=follow_wrapper_chains,
2103 skip_bound_arg=skip_bound_arg,
2104 sigcls=sigcls)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002105
2106 if sig is None:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002107 # At this point we know, that `obj` is a class, with no user-
2108 # defined '__init__', '__new__', or class-level '__call__'
2109
Larry Hastings2623c8c2014-02-08 22:15:29 -08002110 for base in obj.__mro__[:-1]:
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002111 # Since '__text_signature__' is implemented as a
2112 # descriptor that extracts text signature from the
2113 # class docstring, if 'obj' is derived from a builtin
2114 # class, its own '__text_signature__' may be 'None'.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002115 # Therefore, we go through the MRO (except the last
2116 # class in there, which is 'object') to find the first
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002117 # class with non-empty text signature.
2118 try:
2119 text_sig = base.__text_signature__
2120 except AttributeError:
2121 pass
2122 else:
2123 if text_sig:
2124 # If 'obj' class has a __text_signature__ attribute:
2125 # return a signature based on it
Yury Selivanovda396452014-03-27 12:09:24 -04002126 return _signature_fromstr(sigcls, obj, text_sig)
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002127
2128 # No '__text_signature__' was found for the 'obj' class.
2129 # Last option is to check if its '__init__' is
2130 # object.__init__ or type.__init__.
Larry Hastings2623c8c2014-02-08 22:15:29 -08002131 if type not in obj.__mro__:
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002132 # We have a class (not metaclass), but no user-defined
2133 # __init__ or __new__ for it
Yury Selivanov7d2bfed2014-02-03 02:46:07 -05002134 if obj.__init__ is object.__init__:
2135 # Return a signature of 'object' builtin.
2136 return signature(object)
Yury Selivanove7dcc5e2014-01-27 19:29:45 -05002137
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002138 elif not isinstance(obj, _NonUserDefinedCallables):
2139 # An object with __call__
2140 # We also check that the 'obj' is not an instance of
2141 # _WrapperDescriptor or _MethodWrapper to avoid
2142 # infinite recursion (and even potential segfault)
Yury Selivanov421f0c72014-01-29 12:05:40 -05002143 call = _signature_get_user_defined_method(type(obj), '__call__')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002144 if call is not None:
Larry Hastings2623c8c2014-02-08 22:15:29 -08002145 try:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002146 sig = _signature_from_callable(
Yury Selivanovda396452014-03-27 12:09:24 -04002147 call,
2148 follow_wrapper_chains=follow_wrapper_chains,
2149 skip_bound_arg=skip_bound_arg,
2150 sigcls=sigcls)
Larry Hastings2623c8c2014-02-08 22:15:29 -08002151 except ValueError as ex:
2152 msg = 'no signature found for {!r}'.format(obj)
2153 raise ValueError(msg) from ex
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002154
2155 if sig is not None:
2156 # For classes and objects we skip the first parameter of their
2157 # __call__, __new__, or __init__ methods
Yury Selivanov57d240e2014-02-19 16:27:23 -05002158 if skip_bound_arg:
2159 return _signature_bound_method(sig)
2160 else:
2161 return sig
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002162
2163 if isinstance(obj, types.BuiltinFunctionType):
2164 # Raise a nicer error message for builtins
2165 msg = 'no signature found for builtin function {!r}'.format(obj)
2166 raise ValueError(msg)
2167
2168 raise ValueError('callable {!r} is not supported by signature'.format(obj))
2169
2170
2171class _void:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002172 """A private marker - used in Parameter & Signature."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002173
2174
2175class _empty:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002176 """Marker object for Signature.empty and Parameter.empty."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002177
2178
Yury Selivanov21e83a52014-03-27 11:23:13 -04002179class _ParameterKind(enum.IntEnum):
2180 POSITIONAL_ONLY = 0
2181 POSITIONAL_OR_KEYWORD = 1
2182 VAR_POSITIONAL = 2
2183 KEYWORD_ONLY = 3
2184 VAR_KEYWORD = 4
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002185
2186 def __str__(self):
Yury Selivanov21e83a52014-03-27 11:23:13 -04002187 return self._name_
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002188
2189
Yury Selivanov21e83a52014-03-27 11:23:13 -04002190_POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
2191_POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
2192_VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
2193_KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
2194_VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002195
2196
2197class Parameter:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002198 """Represents a parameter in a function signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002199
2200 Has the following public attributes:
2201
2202 * name : str
2203 The name of the parameter as a string.
2204 * default : object
2205 The default value for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002206 parameter has no default value, this attribute is set to
2207 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002208 * annotation
2209 The annotation for the parameter if specified. If the
Yury Selivanov8757ead2014-01-28 16:39:25 -05002210 parameter has no annotation, this attribute is set to
2211 `Parameter.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002212 * kind : str
2213 Describes how argument values are bound to the parameter.
2214 Possible values: `Parameter.POSITIONAL_ONLY`,
2215 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
2216 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002217 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002218
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002219 __slots__ = ('_name', '_kind', '_default', '_annotation')
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002220
2221 POSITIONAL_ONLY = _POSITIONAL_ONLY
2222 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
2223 VAR_POSITIONAL = _VAR_POSITIONAL
2224 KEYWORD_ONLY = _KEYWORD_ONLY
2225 VAR_KEYWORD = _VAR_KEYWORD
2226
2227 empty = _empty
2228
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002229 def __init__(self, name, kind, *, default=_empty, annotation=_empty):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002230
2231 if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
2232 _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
2233 raise ValueError("invalid value for 'Parameter.kind' attribute")
2234 self._kind = kind
2235
2236 if default is not _empty:
2237 if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
2238 msg = '{} parameters cannot have default values'.format(kind)
2239 raise ValueError(msg)
2240 self._default = default
2241 self._annotation = annotation
2242
Yury Selivanov2393dca2014-01-27 15:07:58 -05002243 if name is _empty:
2244 raise ValueError('name is a required attribute for Parameter')
2245
2246 if not isinstance(name, str):
2247 raise TypeError("name must be a str, not a {!r}".format(name))
2248
2249 if not name.isidentifier():
2250 raise ValueError('{!r} is not a valid parameter name'.format(name))
2251
2252 self._name = name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002253
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002254 def __reduce__(self):
2255 return (type(self),
2256 (self._name, self._kind),
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002257 {'_default': self._default,
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002258 '_annotation': self._annotation})
2259
2260 def __setstate__(self, state):
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002261 self._default = state['_default']
2262 self._annotation = state['_annotation']
2263
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002264 @property
2265 def name(self):
2266 return self._name
2267
2268 @property
2269 def default(self):
2270 return self._default
2271
2272 @property
2273 def annotation(self):
2274 return self._annotation
2275
2276 @property
2277 def kind(self):
2278 return self._kind
2279
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002280 def replace(self, *, name=_void, kind=_void,
2281 annotation=_void, default=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002282 """Creates a customized copy of the Parameter."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002283
2284 if name is _void:
2285 name = self._name
2286
2287 if kind is _void:
2288 kind = self._kind
2289
2290 if annotation is _void:
2291 annotation = self._annotation
2292
2293 if default is _void:
2294 default = self._default
2295
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002296 return type(self)(name, kind, default=default, annotation=annotation)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002297
2298 def __str__(self):
2299 kind = self.kind
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002300 formatted = self._name
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002301
2302 # Add annotation and default value
2303 if self._annotation is not _empty:
2304 formatted = '{}:{}'.format(formatted,
2305 formatannotation(self._annotation))
2306
2307 if self._default is not _empty:
2308 formatted = '{}={}'.format(formatted, repr(self._default))
2309
2310 if kind == _VAR_POSITIONAL:
2311 formatted = '*' + formatted
2312 elif kind == _VAR_KEYWORD:
2313 formatted = '**' + formatted
2314
2315 return formatted
2316
2317 def __repr__(self):
Yury Selivanov374375d2014-03-27 12:41:53 -04002318 return '<{} at {:#x} "{}">'.format(self.__class__.__name__,
2319 id(self), self)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002320
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002321 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002322 return hash((self.name, self.kind, self.annotation, self.default))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002323
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002324 def __eq__(self, other):
2325 return (issubclass(other.__class__, Parameter) and
2326 self._name == other._name and
2327 self._kind == other._kind and
2328 self._default == other._default and
2329 self._annotation == other._annotation)
2330
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002331
2332class BoundArguments:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002333 """Result of `Signature.bind` call. Holds the mapping of arguments
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002334 to the function's parameters.
2335
2336 Has the following public attributes:
2337
2338 * arguments : OrderedDict
2339 An ordered mutable mapping of parameters' names to arguments' values.
2340 Does not contain arguments' default values.
2341 * signature : Signature
2342 The Signature object that created this instance.
2343 * args : tuple
2344 Tuple of positional arguments values.
2345 * kwargs : dict
2346 Dict of keyword arguments values.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002347 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002348
2349 def __init__(self, signature, arguments):
2350 self.arguments = arguments
2351 self._signature = signature
2352
2353 @property
2354 def signature(self):
2355 return self._signature
2356
2357 @property
2358 def args(self):
2359 args = []
2360 for param_name, param in self._signature.parameters.items():
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002361 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002362 break
2363
2364 try:
2365 arg = self.arguments[param_name]
2366 except KeyError:
2367 # We're done here. Other arguments
2368 # will be mapped in 'BoundArguments.kwargs'
2369 break
2370 else:
2371 if param.kind == _VAR_POSITIONAL:
2372 # *args
2373 args.extend(arg)
2374 else:
2375 # plain argument
2376 args.append(arg)
2377
2378 return tuple(args)
2379
2380 @property
2381 def kwargs(self):
2382 kwargs = {}
2383 kwargs_started = False
2384 for param_name, param in self._signature.parameters.items():
2385 if not kwargs_started:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002386 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002387 kwargs_started = True
2388 else:
2389 if param_name not in self.arguments:
2390 kwargs_started = True
2391 continue
2392
2393 if not kwargs_started:
2394 continue
2395
2396 try:
2397 arg = self.arguments[param_name]
2398 except KeyError:
2399 pass
2400 else:
2401 if param.kind == _VAR_KEYWORD:
2402 # **kwargs
2403 kwargs.update(arg)
2404 else:
2405 # plain keyword argument
2406 kwargs[param_name] = arg
2407
2408 return kwargs
2409
2410 def __eq__(self, other):
2411 return (issubclass(other.__class__, BoundArguments) and
2412 self.signature == other.signature and
2413 self.arguments == other.arguments)
2414
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002415
2416class Signature:
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002417 """A Signature object represents the overall signature of a function.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002418 It stores a Parameter object for each parameter accepted by the
2419 function, as well as information specific to the function itself.
2420
2421 A Signature object has the following public attributes and methods:
2422
2423 * parameters : OrderedDict
2424 An ordered mapping of parameters' names to the corresponding
2425 Parameter objects (keyword-only arguments are in the same order
2426 as listed in `code.co_varnames`).
2427 * return_annotation : object
2428 The annotation for the return type of the function if specified.
2429 If the function has no annotation for its return type, this
Yury Selivanov8757ead2014-01-28 16:39:25 -05002430 attribute is set to `Signature.empty`.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002431 * bind(*args, **kwargs) -> BoundArguments
2432 Creates a mapping from positional and keyword arguments to
2433 parameters.
2434 * bind_partial(*args, **kwargs) -> BoundArguments
2435 Creates a partial mapping from positional and keyword arguments
2436 to parameters (simulating 'functools.partial' behavior.)
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002437 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002438
2439 __slots__ = ('_return_annotation', '_parameters')
2440
2441 _parameter_cls = Parameter
2442 _bound_arguments_cls = BoundArguments
2443
2444 empty = _empty
2445
2446 def __init__(self, parameters=None, *, return_annotation=_empty,
2447 __validate_parameters__=True):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002448 """Constructs Signature from the given list of Parameter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002449 objects and 'return_annotation'. All arguments are optional.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002450 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002451
2452 if parameters is None:
2453 params = OrderedDict()
2454 else:
2455 if __validate_parameters__:
2456 params = OrderedDict()
2457 top_kind = _POSITIONAL_ONLY
Yury Selivanov07a9e452014-01-29 10:58:16 -05002458 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002459
2460 for idx, param in enumerate(parameters):
2461 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002462 name = param.name
2463
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002464 if kind < top_kind:
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002465 msg = 'wrong parameter order: {!r} before {!r}'
Yury Selivanov2393dca2014-01-27 15:07:58 -05002466 msg = msg.format(top_kind, kind)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002467 raise ValueError(msg)
Yury Selivanov07a9e452014-01-29 10:58:16 -05002468 elif kind > top_kind:
2469 kind_defaults = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002470 top_kind = kind
2471
Yury Selivanov3f73ca22014-04-08 11:30:45 -04002472 if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
Yury Selivanov07a9e452014-01-29 10:58:16 -05002473 if param.default is _empty:
2474 if kind_defaults:
2475 # No default for this parameter, but the
2476 # previous parameter of the same kind had
2477 # a default
2478 msg = 'non-default argument follows default ' \
2479 'argument'
2480 raise ValueError(msg)
2481 else:
2482 # There is a default for this parameter.
2483 kind_defaults = True
2484
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002485 if name in params:
2486 msg = 'duplicate parameter name: {!r}'.format(name)
2487 raise ValueError(msg)
Yury Selivanov2393dca2014-01-27 15:07:58 -05002488
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002489 params[name] = param
2490 else:
2491 params = OrderedDict(((param.name, param)
2492 for param in parameters))
2493
2494 self._parameters = types.MappingProxyType(params)
2495 self._return_annotation = return_annotation
2496
2497 @classmethod
2498 def from_function(cls, func):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002499 """Constructs Signature for the given python function."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002500
Yury Selivanov5334bcd2014-01-31 15:21:51 -05002501 is_duck_function = False
2502 if not isfunction(func):
2503 if _signature_is_functionlike(func):
2504 is_duck_function = True
2505 else:
2506 # If it's not a pure Python function, and not a duck type
2507 # of pure function:
2508 raise TypeError('{!r} is not a Python function'.format(func))
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002509
2510 Parameter = cls._parameter_cls
2511
2512 # Parameter information.
2513 func_code = func.__code__
2514 pos_count = func_code.co_argcount
2515 arg_names = func_code.co_varnames
2516 positional = tuple(arg_names[:pos_count])
2517 keyword_only_count = func_code.co_kwonlyargcount
2518 keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)]
2519 annotations = func.__annotations__
2520 defaults = func.__defaults__
2521 kwdefaults = func.__kwdefaults__
2522
2523 if defaults:
2524 pos_default_count = len(defaults)
2525 else:
2526 pos_default_count = 0
2527
2528 parameters = []
2529
2530 # Non-keyword-only parameters w/o defaults.
2531 non_default_count = pos_count - pos_default_count
2532 for name in positional[:non_default_count]:
2533 annotation = annotations.get(name, _empty)
2534 parameters.append(Parameter(name, annotation=annotation,
2535 kind=_POSITIONAL_OR_KEYWORD))
2536
2537 # ... w/ defaults.
2538 for offset, name in enumerate(positional[non_default_count:]):
2539 annotation = annotations.get(name, _empty)
2540 parameters.append(Parameter(name, annotation=annotation,
2541 kind=_POSITIONAL_OR_KEYWORD,
2542 default=defaults[offset]))
2543
2544 # *args
Yury Selivanov89ca85c2014-01-29 16:50:40 -05002545 if func_code.co_flags & CO_VARARGS:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002546 name = arg_names[pos_count + keyword_only_count]
2547 annotation = annotations.get(name, _empty)
2548 parameters.append(Parameter(name, annotation=annotation,
2549 kind=_VAR_POSITIONAL))
2550
2551 # Keyword-only parameters.
2552 for name in keyword_only:
2553 default = _empty
2554 if kwdefaults is not None:
2555 default = kwdefaults.get(name, _empty)
2556
2557 annotation = annotations.get(name, _empty)
2558 parameters.append(Parameter(name, annotation=annotation,
2559 kind=_KEYWORD_ONLY,
2560 default=default))
2561 # **kwargs
Yury Selivanov89ca85c2014-01-29 16:50:40 -05002562 if func_code.co_flags & CO_VARKEYWORDS:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002563 index = pos_count + keyword_only_count
Yury Selivanov89ca85c2014-01-29 16:50:40 -05002564 if func_code.co_flags & CO_VARARGS:
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002565 index += 1
2566
2567 name = arg_names[index]
2568 annotation = annotations.get(name, _empty)
2569 parameters.append(Parameter(name, annotation=annotation,
2570 kind=_VAR_KEYWORD))
2571
Yury Selivanov5334bcd2014-01-31 15:21:51 -05002572 # Is 'func' is a pure Python function - don't validate the
2573 # parameters list (for correct order and defaults), it should be OK.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002574 return cls(parameters,
2575 return_annotation=annotations.get('return', _empty),
Yury Selivanov5334bcd2014-01-31 15:21:51 -05002576 __validate_parameters__=is_duck_function)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002577
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002578 @classmethod
2579 def from_builtin(cls, func):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002580 """Constructs Signature for the given builtin function."""
Yury Selivanov57d240e2014-02-19 16:27:23 -05002581 return _signature_from_builtin(cls, func)
Larry Hastings44e2eaa2013-11-23 15:37:55 -08002582
Yury Selivanovda396452014-03-27 12:09:24 -04002583 @classmethod
2584 def from_callable(cls, obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002585 """Constructs Signature for the given callable object."""
2586 return _signature_from_callable(obj, sigcls=cls)
Yury Selivanovda396452014-03-27 12:09:24 -04002587
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002588 @property
2589 def parameters(self):
2590 return self._parameters
2591
2592 @property
2593 def return_annotation(self):
2594 return self._return_annotation
2595
2596 def replace(self, *, parameters=_void, return_annotation=_void):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002597 """Creates a customized copy of the Signature.
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002598 Pass 'parameters' and/or 'return_annotation' arguments
2599 to override them in the new copy.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002600 """
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002601
2602 if parameters is _void:
2603 parameters = self.parameters.values()
2604
2605 if return_annotation is _void:
2606 return_annotation = self._return_annotation
2607
2608 return type(self)(parameters,
2609 return_annotation=return_annotation)
2610
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002611 def _hash_basis(self):
2612 params = tuple(param for param in self.parameters.values()
2613 if param.kind != _KEYWORD_ONLY)
2614
2615 kwo_params = {param.name: param for param in self.parameters.values()
2616 if param.kind == _KEYWORD_ONLY}
2617
2618 return params, kwo_params, self.return_annotation
2619
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002620 def __hash__(self):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002621 params, kwo_params, return_annotation = self._hash_basis()
2622 kwo_params = frozenset(kwo_params.values())
2623 return hash((params, kwo_params, return_annotation))
Yury Selivanov67ae50e2014-04-08 11:46:50 -04002624
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002625 def __eq__(self, other):
Yury Selivanov08d4a4f2014-09-12 15:48:02 -04002626 return (isinstance(other, Signature) and
2627 self._hash_basis() == other._hash_basis())
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002628
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002629 def _bind(self, args, kwargs, *, partial=False):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002630 """Private method. Don't use directly."""
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002631
2632 arguments = OrderedDict()
2633
2634 parameters = iter(self.parameters.values())
2635 parameters_ex = ()
2636 arg_vals = iter(args)
2637
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002638 while True:
2639 # Let's iterate through the positional arguments and corresponding
2640 # parameters
2641 try:
2642 arg_val = next(arg_vals)
2643 except StopIteration:
2644 # No more positional arguments
2645 try:
2646 param = next(parameters)
2647 except StopIteration:
2648 # No more parameters. That's it. Just need to check that
2649 # we have no `kwargs` after this while loop
2650 break
2651 else:
2652 if param.kind == _VAR_POSITIONAL:
2653 # That's OK, just empty *args. Let's start parsing
2654 # kwargs
2655 break
2656 elif param.name in kwargs:
2657 if param.kind == _POSITIONAL_ONLY:
2658 msg = '{arg!r} parameter is positional only, ' \
2659 'but was passed as a keyword'
2660 msg = msg.format(arg=param.name)
2661 raise TypeError(msg) from None
2662 parameters_ex = (param,)
2663 break
2664 elif (param.kind == _VAR_KEYWORD or
2665 param.default is not _empty):
2666 # That's fine too - we have a default value for this
2667 # parameter. So, lets start parsing `kwargs`, starting
2668 # with the current parameter
2669 parameters_ex = (param,)
2670 break
2671 else:
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002672 # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
2673 # not in `kwargs`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002674 if partial:
2675 parameters_ex = (param,)
2676 break
2677 else:
2678 msg = '{arg!r} parameter lacking default value'
2679 msg = msg.format(arg=param.name)
2680 raise TypeError(msg) from None
2681 else:
2682 # We have a positional argument to process
2683 try:
2684 param = next(parameters)
2685 except StopIteration:
2686 raise TypeError('too many positional arguments') from None
2687 else:
2688 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
2689 # Looks like we have no parameter for this positional
2690 # argument
2691 raise TypeError('too many positional arguments')
2692
2693 if param.kind == _VAR_POSITIONAL:
2694 # We have an '*args'-like argument, let's fill it with
2695 # all positional arguments we have left and move on to
2696 # the next phase
2697 values = [arg_val]
2698 values.extend(arg_vals)
2699 arguments[param.name] = tuple(values)
2700 break
2701
2702 if param.name in kwargs:
2703 raise TypeError('multiple values for argument '
2704 '{arg!r}'.format(arg=param.name))
2705
2706 arguments[param.name] = arg_val
2707
2708 # Now, we iterate through the remaining parameters to process
2709 # keyword arguments
2710 kwargs_param = None
2711 for param in itertools.chain(parameters_ex, parameters):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002712 if param.kind == _VAR_KEYWORD:
2713 # Memorize that we have a '**kwargs'-like parameter
2714 kwargs_param = param
2715 continue
2716
Yury Selivanov38b0d5a2014-01-28 17:27:39 -05002717 if param.kind == _VAR_POSITIONAL:
2718 # Named arguments don't refer to '*args'-like parameters.
2719 # We only arrive here if the positional arguments ended
2720 # before reaching the last parameter before *args.
2721 continue
2722
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002723 param_name = param.name
2724 try:
2725 arg_val = kwargs.pop(param_name)
2726 except KeyError:
2727 # We have no value for this parameter. It's fine though,
2728 # if it has a default value, or it is an '*args'-like
2729 # parameter, left alone by the processing of positional
2730 # arguments.
2731 if (not partial and param.kind != _VAR_POSITIONAL and
2732 param.default is _empty):
2733 raise TypeError('{arg!r} parameter lacking default value'. \
2734 format(arg=param_name)) from None
2735
2736 else:
Yury Selivanov9b9ac952014-01-28 20:54:28 -05002737 if param.kind == _POSITIONAL_ONLY:
2738 # This should never happen in case of a properly built
2739 # Signature object (but let's have this check here
2740 # to ensure correct behaviour just in case)
2741 raise TypeError('{arg!r} parameter is positional only, '
2742 'but was passed as a keyword'. \
2743 format(arg=param.name))
2744
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002745 arguments[param_name] = arg_val
2746
2747 if kwargs:
2748 if kwargs_param is not None:
2749 # Process our '**kwargs'-like parameter
2750 arguments[kwargs_param.name] = kwargs
2751 else:
2752 raise TypeError('too many keyword arguments')
2753
2754 return self._bound_arguments_cls(self, arguments)
2755
Yury Selivanovc45873e2014-01-29 12:10:27 -05002756 def bind(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002757 """Get a BoundArguments object, that maps the passed `args`
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002758 and `kwargs` to the function's signature. Raises `TypeError`
2759 if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002760 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05002761 return args[0]._bind(args[1:], kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002762
Yury Selivanovc45873e2014-01-29 12:10:27 -05002763 def bind_partial(*args, **kwargs):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002764 """Get a BoundArguments object, that partially maps the
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002765 passed `args` and `kwargs` to the function's signature.
2766 Raises `TypeError` if the passed arguments can not be bound.
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002767 """
Yury Selivanovc45873e2014-01-29 12:10:27 -05002768 return args[0]._bind(args[1:], kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002769
Yury Selivanova5d63dd2014-03-27 11:31:43 -04002770 def __reduce__(self):
2771 return (type(self),
2772 (tuple(self._parameters.values()),),
2773 {'_return_annotation': self._return_annotation})
2774
2775 def __setstate__(self, state):
2776 self._return_annotation = state['_return_annotation']
2777
Yury Selivanov374375d2014-03-27 12:41:53 -04002778 def __repr__(self):
2779 return '<{} at {:#x} "{}">'.format(self.__class__.__name__,
2780 id(self), self)
2781
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002782 def __str__(self):
2783 result = []
Yury Selivanov2393dca2014-01-27 15:07:58 -05002784 render_pos_only_separator = False
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002785 render_kw_only_separator = True
Yury Selivanov2393dca2014-01-27 15:07:58 -05002786 for param in self.parameters.values():
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002787 formatted = str(param)
2788
2789 kind = param.kind
Yury Selivanov2393dca2014-01-27 15:07:58 -05002790
2791 if kind == _POSITIONAL_ONLY:
2792 render_pos_only_separator = True
2793 elif render_pos_only_separator:
2794 # It's not a positional-only parameter, and the flag
2795 # is set to 'True' (there were pos-only params before.)
2796 result.append('/')
2797 render_pos_only_separator = False
2798
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002799 if kind == _VAR_POSITIONAL:
2800 # OK, we have an '*args'-like parameter, so we won't need
2801 # a '*' to separate keyword-only arguments
2802 render_kw_only_separator = False
2803 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
2804 # We have a keyword-only parameter to render and we haven't
2805 # rendered an '*args'-like parameter before, so add a '*'
2806 # separator to the parameters list ("foo(arg1, *, arg2)" case)
2807 result.append('*')
2808 # This condition should be only triggered once, so
2809 # reset the flag
2810 render_kw_only_separator = False
2811
2812 result.append(formatted)
2813
Yury Selivanov2393dca2014-01-27 15:07:58 -05002814 if render_pos_only_separator:
2815 # There were only positional-only parameters, hence the
2816 # flag was not reset to 'False'
2817 result.append('/')
2818
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002819 rendered = '({})'.format(', '.join(result))
2820
2821 if self.return_annotation is not _empty:
2822 anno = formatannotation(self.return_annotation)
2823 rendered += ' -> {}'.format(anno)
2824
2825 return rendered
Nick Coghlanf94a16b2013-09-22 22:46:49 +10002826
Yury Selivanovda396452014-03-27 12:09:24 -04002827
2828def signature(obj):
Yury Selivanov5a23bd02014-03-29 13:47:11 -04002829 """Get a signature object for the passed callable."""
Yury Selivanovda396452014-03-27 12:09:24 -04002830 return Signature.from_callable(obj)
2831
2832
Nick Coghlanf94a16b2013-09-22 22:46:49 +10002833def _main():
2834 """ Logic for inspecting an object given at command line """
2835 import argparse
2836 import importlib
2837
2838 parser = argparse.ArgumentParser()
2839 parser.add_argument(
2840 'object',
2841 help="The object to be analysed. "
2842 "It supports the 'module:qualname' syntax")
2843 parser.add_argument(
2844 '-d', '--details', action='store_true',
2845 help='Display info about the module rather than its source code')
2846
2847 args = parser.parse_args()
2848
2849 target = args.object
2850 mod_name, has_attrs, attrs = target.partition(":")
2851 try:
2852 obj = module = importlib.import_module(mod_name)
2853 except Exception as exc:
2854 msg = "Failed to import {} ({}: {})".format(mod_name,
2855 type(exc).__name__,
2856 exc)
2857 print(msg, file=sys.stderr)
2858 exit(2)
2859
2860 if has_attrs:
2861 parts = attrs.split(".")
2862 obj = module
2863 for part in parts:
2864 obj = getattr(obj, part)
2865
2866 if module.__name__ in sys.builtin_module_names:
2867 print("Can't get info for builtin modules.", file=sys.stderr)
2868 exit(1)
2869
2870 if args.details:
2871 print('Target: {}'.format(target))
2872 print('Origin: {}'.format(getsourcefile(module)))
2873 print('Cached: {}'.format(module.__cached__))
2874 if obj is module:
2875 print('Loader: {}'.format(repr(module.__loader__)))
2876 if hasattr(module, '__path__'):
2877 print('Submodule search path: {}'.format(module.__path__))
2878 else:
2879 try:
2880 __, lineno = findsource(obj)
2881 except Exception:
2882 pass
2883 else:
2884 print('Line: {}'.format(lineno))
2885
2886 print('\n')
2887 else:
2888 print(getsource(obj))
2889
2890
2891if __name__ == "__main__":
2892 _main()