blob: 4a285072e8393f0379511eef4a97644cb62468bf [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
Guido van Rossum2e65f892007-02-28 22:03:49 +000020 getfullargspec() - same, with support for Python-3000 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
Brett Cannoncb66eb02012-05-11 12:58:42 -040034import importlib.machinery
35import itertools
Christian Heimes7131fd92008-02-19 14:21:46 +000036import linecache
Brett Cannoncb66eb02012-05-11 12:58:42 -040037import os
38import re
39import sys
40import tokenize
41import types
Brett Cannon2b88fcf2012-06-02 22:28:42 -040042import warnings
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070043import functools
Nick Coghlan2f92e542012-06-23 19:39:55 +100044import builtins
Raymond Hettingera1a992c2005-03-11 06:46:45 +000045from operator import attrgetter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070046from collections import namedtuple, OrderedDict
Nick Coghlan09c81232010-08-17 10:18:16 +000047
48# Create constants for the compiler flags in Include/code.h
49# We try to get them from dis to avoid duplication, but fall
50# back to hardcording so the dependency is optional
51try:
52 from dis import COMPILER_FLAG_NAMES as _flag_names
Brett Cannoncd171c82013-07-04 17:43:24 -040053except ImportError:
Nick Coghlan09c81232010-08-17 10:18:16 +000054 CO_OPTIMIZED, CO_NEWLOCALS = 0x1, 0x2
55 CO_VARARGS, CO_VARKEYWORDS = 0x4, 0x8
56 CO_NESTED, CO_GENERATOR, CO_NOFREE = 0x10, 0x20, 0x40
57else:
58 mod_dict = globals()
59 for k, v in _flag_names.items():
60 mod_dict["CO_" + v] = k
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000061
Christian Heimesbe5b30b2008-03-03 19:18:51 +000062# See Include/object.h
63TPFLAGS_IS_ABSTRACT = 1 << 20
64
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000065# ----------------------------------------------------------- type-checking
66def ismodule(object):
67 """Return true if the object is a module.
68
69 Module objects provide these attributes:
Barry Warsaw28a691b2010-04-17 00:19:56 +000070 __cached__ pathname to byte compiled file
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000071 __doc__ documentation string
72 __file__ filename (missing for built-in modules)"""
Tim Peters28bc59f2001-09-16 08:40:16 +000073 return isinstance(object, types.ModuleType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000074
75def isclass(object):
76 """Return true if the object is a class.
77
78 Class objects provide these attributes:
79 __doc__ documentation string
80 __module__ name of module in which this class was defined"""
Benjamin Petersonc4656002009-01-17 22:41:18 +000081 return isinstance(object, type)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000082
83def ismethod(object):
84 """Return true if the object is an instance method.
85
86 Instance method objects provide these attributes:
87 __doc__ documentation string
88 __name__ name with which this method was defined
Christian Heimesff737952007-11-27 10:40:20 +000089 __func__ function object containing implementation of method
90 __self__ instance to which this method is bound"""
Tim Peters28bc59f2001-09-16 08:40:16 +000091 return isinstance(object, types.MethodType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000092
Tim Peters536d2262001-09-20 05:13:38 +000093def ismethoddescriptor(object):
Tim Petersf1d90b92001-09-20 05:47:55 +000094 """Return true if the object is a method descriptor.
95
96 But not if ismethod() or isclass() or isfunction() are true.
Tim Peters536d2262001-09-20 05:13:38 +000097
98 This is new in Python 2.2, and, for example, is true of int.__add__.
99 An object passing this test has a __get__ attribute but not a __set__
100 attribute, but beyond that the set of attributes varies. __name__ is
101 usually sensible, and __doc__ often is.
102
Tim Petersf1d90b92001-09-20 05:47:55 +0000103 Methods implemented via descriptors that also pass one of the other
104 tests return false from the ismethoddescriptor() test, simply because
105 the other tests promise more -- you can, e.g., count on having the
Christian Heimesff737952007-11-27 10:40:20 +0000106 __func__ attribute (etc) when an object passes ismethod()."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100107 if isclass(object) or ismethod(object) or isfunction(object):
108 # mutual exclusion
109 return False
110 tp = type(object)
111 return hasattr(tp, "__get__") and not hasattr(tp, "__set__")
Tim Peters536d2262001-09-20 05:13:38 +0000112
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000113def isdatadescriptor(object):
114 """Return true if the object is a data descriptor.
115
116 Data descriptors have both a __get__ and a __set__ attribute. Examples are
117 properties (defined in Python) and getsets and members (defined in C).
118 Typically, data descriptors will also have __name__ and __doc__ attributes
119 (properties, getsets, and members have both of these attributes), but this
120 is not guaranteed."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100121 if isclass(object) or ismethod(object) or isfunction(object):
122 # mutual exclusion
123 return False
124 tp = type(object)
125 return hasattr(tp, "__set__") and hasattr(tp, "__get__")
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000126
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000127if hasattr(types, 'MemberDescriptorType'):
128 # CPython and equivalent
129 def ismemberdescriptor(object):
130 """Return true if the object is a member descriptor.
131
132 Member descriptors are specialized descriptors defined in extension
133 modules."""
134 return isinstance(object, types.MemberDescriptorType)
135else:
136 # Other implementations
137 def ismemberdescriptor(object):
138 """Return true if the object is a member descriptor.
139
140 Member descriptors are specialized descriptors defined in extension
141 modules."""
142 return False
143
144if hasattr(types, 'GetSetDescriptorType'):
145 # CPython and equivalent
146 def isgetsetdescriptor(object):
147 """Return true if the object is a getset descriptor.
148
149 getset descriptors are specialized descriptors defined in extension
150 modules."""
151 return isinstance(object, types.GetSetDescriptorType)
152else:
153 # Other implementations
154 def isgetsetdescriptor(object):
155 """Return true if the object is a getset descriptor.
156
157 getset descriptors are specialized descriptors defined in extension
158 modules."""
159 return False
160
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000161def isfunction(object):
162 """Return true if the object is a user-defined function.
163
164 Function objects provide these attributes:
165 __doc__ documentation string
166 __name__ name with which this function was defined
Neal Norwitz221085d2007-02-25 20:55:47 +0000167 __code__ code object containing compiled function bytecode
168 __defaults__ tuple of any default values for arguments
169 __globals__ global namespace in which this function was defined
170 __annotations__ dict of parameter annotations
171 __kwdefaults__ dict of keyword only parameters with defaults"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000172 return isinstance(object, types.FunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000173
Christian Heimes7131fd92008-02-19 14:21:46 +0000174def isgeneratorfunction(object):
175 """Return true if the object is a user-defined generator function.
176
177 Generator function objects provides same attributes as functions.
178
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000179 See help(isfunction) for attributes listing."""
Georg Brandlb1441c72009-01-03 22:33:39 +0000180 return bool((isfunction(object) or ismethod(object)) and
181 object.__code__.co_flags & CO_GENERATOR)
Christian Heimes7131fd92008-02-19 14:21:46 +0000182
183def isgenerator(object):
184 """Return true if the object is a generator.
185
186 Generator objects provide these attributes:
187 __iter__ defined to support interation over container
188 close raises a new GeneratorExit exception inside the
189 generator to terminate the iteration
190 gi_code code object
191 gi_frame frame object or possibly None once the generator has
192 been exhausted
193 gi_running set to 1 when generator is executing, 0 otherwise
194 next return the next item from the container
195 send resumes the generator and "sends" a value that becomes
196 the result of the current yield-expression
197 throw used to raise an exception inside the generator"""
198 return isinstance(object, types.GeneratorType)
199
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000200def istraceback(object):
201 """Return true if the object is a traceback.
202
203 Traceback objects provide these attributes:
204 tb_frame frame object at this level
205 tb_lasti index of last attempted instruction in bytecode
206 tb_lineno current line number in Python source code
207 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000208 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000209
210def isframe(object):
211 """Return true if the object is a frame object.
212
213 Frame objects provide these attributes:
214 f_back next outer frame object (this frame's caller)
215 f_builtins built-in namespace seen by this frame
216 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000217 f_globals global namespace seen by this frame
218 f_lasti index of last attempted instruction in bytecode
219 f_lineno current line number in Python source code
220 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000221 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000222 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000223
224def iscode(object):
225 """Return true if the object is a code object.
226
227 Code objects provide these attributes:
228 co_argcount number of arguments (not including * or ** args)
229 co_code string of raw compiled bytecode
230 co_consts tuple of constants used in the bytecode
231 co_filename name of file in which this code object was created
232 co_firstlineno number of first line in Python source code
233 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
234 co_lnotab encoded mapping of line numbers to bytecode indices
235 co_name name with which this code object was defined
236 co_names tuple of names of local variables
237 co_nlocals number of local variables
238 co_stacksize virtual machine stack space required
239 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000240 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000241
242def isbuiltin(object):
243 """Return true if the object is a built-in function or method.
244
245 Built-in functions and methods provide these attributes:
246 __doc__ documentation string
247 __name__ original name of this function or method
248 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000249 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000250
251def isroutine(object):
252 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000253 return (isbuiltin(object)
254 or isfunction(object)
255 or ismethod(object)
256 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000257
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000258def isabstract(object):
259 """Return true if the object is an abstract base class (ABC)."""
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000260 return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT)
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000261
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000262def getmembers(object, predicate=None):
263 """Return all members of an object as (name, value) pairs sorted by name.
264 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100265 if isclass(object):
266 mro = (object,) + getmro(object)
267 else:
268 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000269 results = []
270 for key in dir(object):
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100271 # First try to get the value via __dict__. Some descriptors don't
272 # like calling their __get__ (see bug #1785).
273 for base in mro:
274 if key in base.__dict__:
275 value = base.__dict__[key]
276 break
277 else:
278 try:
279 value = getattr(object, key)
280 except AttributeError:
281 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000282 if not predicate or predicate(value):
283 results.append((key, value))
284 results.sort()
285 return results
286
Christian Heimes25bb7832008-01-11 16:17:00 +0000287Attribute = namedtuple('Attribute', 'name kind defining_class object')
288
Tim Peters13b49d32001-09-23 02:00:29 +0000289def classify_class_attrs(cls):
290 """Return list of attribute-descriptor tuples.
291
292 For each name in dir(cls), the return list contains a 4-tuple
293 with these elements:
294
295 0. The name (a string).
296
297 1. The kind of attribute this is, one of these strings:
298 'class method' created via classmethod()
299 'static method' created via staticmethod()
300 'property' created via property()
301 'method' any other flavor of method
302 'data' not a method
303
304 2. The class which defined this attribute (a class).
305
306 3. The object as obtained directly from the defining class's
307 __dict__, not via getattr. This is especially important for
308 data attributes: C.data is just a data object, but
309 C.__dict__['data'] may be a data descriptor with additional
310 info, like a __doc__ string.
311 """
312
313 mro = getmro(cls)
314 names = dir(cls)
315 result = []
316 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100317 # Get the object associated with the name, and where it was defined.
Tim Peters13b49d32001-09-23 02:00:29 +0000318 # Getting an obj from the __dict__ sometimes reveals more than
319 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100320 # Furthermore, some objects may raise an Exception when fetched with
321 # getattr(). This is the case with some descriptors (bug #1785).
322 # Thus, we only use getattr() as a last resort.
323 homecls = None
324 for base in (cls,) + mro:
325 if name in base.__dict__:
326 obj = base.__dict__[name]
327 homecls = base
328 break
Tim Peters13b49d32001-09-23 02:00:29 +0000329 else:
330 obj = getattr(cls, name)
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100331 homecls = getattr(obj, "__objclass__", homecls)
Tim Peters13b49d32001-09-23 02:00:29 +0000332
333 # Classify the object.
334 if isinstance(obj, staticmethod):
335 kind = "static method"
336 elif isinstance(obj, classmethod):
337 kind = "class method"
338 elif isinstance(obj, property):
339 kind = "property"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100340 elif ismethoddescriptor(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000341 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100342 elif isdatadescriptor(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000343 kind = "data"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100344 else:
345 obj_via_getattr = getattr(cls, name)
346 if (isfunction(obj_via_getattr) or
347 ismethoddescriptor(obj_via_getattr)):
348 kind = "method"
349 else:
350 kind = "data"
351 obj = obj_via_getattr
Tim Peters13b49d32001-09-23 02:00:29 +0000352
Christian Heimes25bb7832008-01-11 16:17:00 +0000353 result.append(Attribute(name, kind, homecls, obj))
Tim Peters13b49d32001-09-23 02:00:29 +0000354
355 return result
356
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000357# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000358
359def getmro(cls):
360 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000361 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000362
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000363# -------------------------------------------------- source code extraction
364def indentsize(line):
365 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000366 expline = line.expandtabs()
367 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000368
369def getdoc(object):
370 """Get the documentation string for an object.
371
372 All tabs are expanded to spaces. To clean up docstrings that are
373 indented to line up with blocks of code, any whitespace than can be
374 uniformly removed from the second line onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000375 try:
376 doc = object.__doc__
377 except AttributeError:
378 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000379 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000380 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000381 return cleandoc(doc)
382
383def cleandoc(doc):
384 """Clean up indentation from docstrings.
385
386 Any whitespace that can be uniformly removed from the second line
387 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000388 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000389 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000390 except UnicodeError:
391 return None
392 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000393 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000394 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000395 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000396 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000397 if content:
398 indent = len(line) - content
399 margin = min(margin, indent)
400 # Remove indentation.
401 if lines:
402 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000403 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000404 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000405 # Remove any trailing or leading blank lines.
406 while lines and not lines[-1]:
407 lines.pop()
408 while lines and not lines[0]:
409 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000410 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000411
412def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000413 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000414 if ismodule(object):
415 if hasattr(object, '__file__'):
416 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000417 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000418 if isclass(object):
Ka-Ping Yeec99e0f12001-04-13 12:10:40 +0000419 object = sys.modules.get(object.__module__)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000420 if hasattr(object, '__file__'):
421 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000422 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000423 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000424 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000425 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000426 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000427 if istraceback(object):
428 object = object.tb_frame
429 if isframe(object):
430 object = object.f_code
431 if iscode(object):
432 return object.co_filename
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000433 raise TypeError('{!r} is not a module, class, method, '
434 'function, traceback, frame, or code object'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000435
Christian Heimes25bb7832008-01-11 16:17:00 +0000436ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')
437
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000438def getmoduleinfo(path):
439 """Get the module name, suffix, mode, and module type for a given file."""
Brett Cannoncb66eb02012-05-11 12:58:42 -0400440 warnings.warn('inspect.getmoduleinfo() is deprecated', DeprecationWarning,
441 2)
Brett Cannone4f41de2013-06-16 13:13:40 -0400442 with warnings.catch_warnings():
443 warnings.simplefilter('ignore', PendingDeprecationWarning)
444 import imp
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000445 filename = os.path.basename(path)
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000446 suffixes = [(-len(suffix), suffix, mode, mtype)
447 for suffix, mode, mtype in imp.get_suffixes()]
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000448 suffixes.sort() # try longest suffixes first, in case they overlap
449 for neglen, suffix, mode, mtype in suffixes:
450 if filename[neglen:] == suffix:
Christian Heimes25bb7832008-01-11 16:17:00 +0000451 return ModuleInfo(filename[:neglen], suffix, mode, mtype)
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000452
453def getmodulename(path):
454 """Return the module name for a given file, or None."""
Nick Coghlan76e07702012-07-18 23:14:57 +1000455 fname = os.path.basename(path)
456 # Check for paths that look like an actual module file
457 suffixes = [(-len(suffix), suffix)
458 for suffix in importlib.machinery.all_suffixes()]
459 suffixes.sort() # try longest suffixes first, in case they overlap
460 for neglen, suffix in suffixes:
461 if fname.endswith(suffix):
462 return fname[:neglen]
463 return None
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000464
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000465def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000466 """Return the filename that can be used to locate an object's source.
467 Return None if no way can be identified to get the source.
468 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000469 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400470 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
471 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
472 if any(filename.endswith(s) for s in all_bytecode_suffixes):
473 filename = (os.path.splitext(filename)[0] +
474 importlib.machinery.SOURCE_SUFFIXES[0])
475 elif any(filename.endswith(s) for s in
476 importlib.machinery.EXTENSION_SUFFIXES):
477 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000478 if os.path.exists(filename):
479 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000480 # only return a non-existent filename if the module has a PEP 302 loader
Brett Cannon4c14b5d2013-05-04 13:56:58 -0400481 if getattr(getmodule(object, filename), '__loader__', None) is not None:
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000482 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000483 # or it is in the linecache
484 if filename in linecache.cache:
485 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000486
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000487def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000488 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000489
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000490 The idea is for each object to have a unique origin, so this routine
491 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000492 if _filename is None:
493 _filename = getsourcefile(object) or getfile(object)
494 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000495
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000496modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000497_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000498
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000499def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000500 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000501 if ismodule(object):
502 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000503 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000504 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000505 # Try the filename to modulename cache
506 if _filename is not None and _filename in modulesbyfile:
507 return sys.modules.get(modulesbyfile[_filename])
508 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000509 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000510 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000511 except TypeError:
512 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000513 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000514 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000515 # Update the filename to module name cache and check yet again
516 # Copy sys.modules in order to cope with changes while iterating
Éric Araujoa74f8ef2011-11-29 16:58:53 +0100517 for modname, module in list(sys.modules.items()):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000518 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000519 f = module.__file__
520 if f == _filesbymodname.get(modname, None):
521 # Have already mapped this module, so skip it
522 continue
523 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000524 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000525 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000526 modulesbyfile[f] = modulesbyfile[
527 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000528 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000529 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000530 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000531 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000532 if not hasattr(object, '__name__'):
533 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000534 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000535 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000536 if mainobject is object:
537 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000538 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000539 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000540 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000541 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000542 if builtinobject is object:
543 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000544
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000545def findsource(object):
546 """Return the entire source file and starting line number for an object.
547
548 The argument may be a module, class, method, function, traceback, frame,
549 or code object. The source code is returned as a list of all the lines
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200550 in the file and the line number indexes a line in that list. An OSError
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000551 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500552
553 file = getfile(object)
554 sourcefile = getsourcefile(object)
Ezio Melotti1b145922013-03-30 05:17:24 +0200555 if not sourcefile and file[:1] + file[-1:] != '<>':
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200556 raise OSError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500557 file = sourcefile if sourcefile else file
558
Thomas Wouters89f507f2006-12-13 04:49:30 +0000559 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000560 if module:
561 lines = linecache.getlines(file, module.__dict__)
562 else:
563 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000564 if not lines:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200565 raise OSError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000566
567 if ismodule(object):
568 return lines, 0
569
570 if isclass(object):
571 name = object.__name__
Thomas Wouters89f507f2006-12-13 04:49:30 +0000572 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
573 # make some effort to find the best matching class definition:
574 # use the one with the least indentation, which is the one
575 # that's most probably not inside a function definition.
576 candidates = []
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000577 for i in range(len(lines)):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000578 match = pat.match(lines[i])
579 if match:
580 # if it's at toplevel, it's already the best one
581 if lines[i][0] == 'c':
582 return lines, i
583 # else add whitespace to candidate list
584 candidates.append((match.group(1), i))
585 if candidates:
586 # this will sort by whitespace, and by line number,
587 # less whitespace first
588 candidates.sort()
589 return lines, candidates[0][1]
Jeremy Hyltonab919022003-06-27 18:41:20 +0000590 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200591 raise OSError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000592
593 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000594 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000595 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000596 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000597 if istraceback(object):
598 object = object.tb_frame
599 if isframe(object):
600 object = object.f_code
601 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000602 if not hasattr(object, 'co_firstlineno'):
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200603 raise OSError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000604 lnum = object.co_firstlineno - 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000605 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000606 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000607 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000608 lnum = lnum - 1
609 return lines, lnum
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200610 raise OSError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000611
612def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000613 """Get lines of comments immediately preceding an object's source code.
614
615 Returns None when source can't be found.
616 """
617 try:
618 lines, lnum = findsource(object)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200619 except (OSError, TypeError):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000620 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000621
622 if ismodule(object):
623 # Look for a comment block at the top of the file.
624 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000625 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000626 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000627 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000628 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000629 comments = []
630 end = start
631 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000632 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000633 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000634 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000635
636 # Look for a preceding block of comments at the same indentation.
637 elif lnum > 0:
638 indent = indentsize(lines[lnum])
639 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000640 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000641 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000642 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000643 if end > 0:
644 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000645 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000646 while comment[:1] == '#' and indentsize(lines[end]) == indent:
647 comments[:0] = [comment]
648 end = end - 1
649 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000650 comment = lines[end].expandtabs().lstrip()
651 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000652 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000653 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000654 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000655 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000656
Tim Peters4efb6e92001-06-29 23:51:08 +0000657class EndOfBlock(Exception): pass
658
659class BlockFinder:
660 """Provide a tokeneater() method to detect the end of a code block."""
661 def __init__(self):
662 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000663 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000664 self.started = False
665 self.passline = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000666 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000667
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000668 def tokeneater(self, type, token, srowcol, erowcol, line):
Tim Peters4efb6e92001-06-29 23:51:08 +0000669 if not self.started:
Armin Rigodd5c0232005-09-25 11:45:45 +0000670 # look for the first "def", "class" or "lambda"
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000671 if token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000672 if token == "lambda":
673 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000674 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000675 self.passline = True # skip to the end of the line
Tim Peters4efb6e92001-06-29 23:51:08 +0000676 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000677 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000678 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000679 if self.islambda: # lambdas always end at the first NEWLINE
680 raise EndOfBlock
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000681 elif self.passline:
682 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000683 elif type == tokenize.INDENT:
684 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000685 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000686 elif type == tokenize.DEDENT:
687 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000688 # the end of matching indent/dedent pairs end a block
689 # (note that this only works for "def"/"class" blocks,
690 # not e.g. for "if: else:" or "try: finally:" blocks)
691 if self.indent <= 0:
692 raise EndOfBlock
693 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
694 # any other token on the same indentation level end the previous
695 # block as well, except the pseudo-tokens COMMENT and NL.
696 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000697
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000698def getblock(lines):
699 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000700 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000701 try:
Trent Nelson428de652008-03-18 22:41:35 +0000702 tokens = tokenize.generate_tokens(iter(lines).__next__)
703 for _token in tokens:
704 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000705 except (EndOfBlock, IndentationError):
706 pass
707 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000708
709def getsourcelines(object):
710 """Return a list of source lines and starting line number for an object.
711
712 The argument may be a module, class, method, function, traceback, frame,
713 or code object. The source code is returned as a list of the lines
714 corresponding to the object and the line number indicates where in the
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200715 original source file the first line of code was found. An OSError is
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000716 raised if the source code cannot be retrieved."""
717 lines, lnum = findsource(object)
718
719 if ismodule(object): return lines, 0
720 else: return getblock(lines[lnum:]), lnum + 1
721
722def getsource(object):
723 """Return the text of the source code for an object.
724
725 The argument may be a module, class, method, function, traceback, frame,
726 or code object. The source code is returned as a single string. An
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200727 OSError is raised if the source code cannot be retrieved."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000728 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000729 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000730
731# --------------------------------------------------- class tree extraction
732def walktree(classes, children, parent):
733 """Recursive helper function for getclasstree()."""
734 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +0000735 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000736 for c in classes:
737 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +0000738 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000739 results.append(walktree(children[c], children, c))
740 return results
741
Georg Brandl5ce83a02009-06-01 17:23:51 +0000742def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000743 """Arrange the given list of classes into a hierarchy of nested lists.
744
745 Where a nested list appears, it contains classes derived from the class
746 whose entry immediately precedes the list. Each entry is a 2-tuple
747 containing a class and a tuple of its base classes. If the 'unique'
748 argument is true, exactly one entry appears in the returned structure
749 for each class in the given list. Otherwise, classes using multiple
750 inheritance and their descendants will appear multiple times."""
751 children = {}
752 roots = []
753 for c in classes:
754 if c.__bases__:
755 for parent in c.__bases__:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000756 if not parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000757 children[parent] = []
758 children[parent].append(c)
759 if unique and parent in classes: break
760 elif c not in roots:
761 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +0000762 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000763 if parent not in classes:
764 roots.append(parent)
765 return walktree(roots, children, None)
766
767# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +0000768Arguments = namedtuple('Arguments', 'args, varargs, varkw')
769
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000770def getargs(co):
771 """Get information about the arguments accepted by a code object.
772
Guido van Rossum2e65f892007-02-28 22:03:49 +0000773 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000774 'args' is the list of argument names. Keyword-only arguments are
775 appended. 'varargs' and 'varkw' are the names of the * and **
776 arguments or None."""
Guido van Rossum2e65f892007-02-28 22:03:49 +0000777 args, varargs, kwonlyargs, varkw = _getfullargs(co)
Christian Heimes25bb7832008-01-11 16:17:00 +0000778 return Arguments(args + kwonlyargs, varargs, varkw)
Guido van Rossum2e65f892007-02-28 22:03:49 +0000779
780def _getfullargs(co):
781 """Get information about the arguments accepted by a code object.
782
783 Four things are returned: (args, varargs, kwonlyargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000784 'args' and 'kwonlyargs' are lists of argument names, and 'varargs'
785 and 'varkw' are the names of the * and ** arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +0000786
787 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000788 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000789
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000790 nargs = co.co_argcount
791 names = co.co_varnames
Guido van Rossum2e65f892007-02-28 22:03:49 +0000792 nkwargs = co.co_kwonlyargcount
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000793 args = list(names[:nargs])
Guido van Rossum2e65f892007-02-28 22:03:49 +0000794 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000795 step = 0
796
Guido van Rossum2e65f892007-02-28 22:03:49 +0000797 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000798 varargs = None
799 if co.co_flags & CO_VARARGS:
800 varargs = co.co_varnames[nargs]
801 nargs = nargs + 1
802 varkw = None
803 if co.co_flags & CO_VARKEYWORDS:
804 varkw = co.co_varnames[nargs]
Guido van Rossum2e65f892007-02-28 22:03:49 +0000805 return args, varargs, kwonlyargs, varkw
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000806
Christian Heimes25bb7832008-01-11 16:17:00 +0000807
808ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
809
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000810def getargspec(func):
811 """Get the names and default values of a function's arguments.
812
813 A tuple of four things is returned: (args, varargs, varkw, defaults).
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000814 'args' is a list of the argument names.
Guido van Rossum2e65f892007-02-28 22:03:49 +0000815 'args' will include keyword-only argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000816 'varargs' and 'varkw' are the names of the * and ** arguments or None.
Jeremy Hylton64967882003-06-27 18:14:39 +0000817 'defaults' is an n-tuple of the default values of the last n arguments.
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000818
Guido van Rossum2e65f892007-02-28 22:03:49 +0000819 Use the getfullargspec() API for Python-3000 code, as annotations
820 and keyword arguments are supported. getargspec() will raise ValueError
821 if the func has either annotations or keyword arguments.
822 """
823
824 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
825 getfullargspec(func)
826 if kwonlyargs or ann:
Collin Winterce36ad82007-08-30 01:19:48 +0000827 raise ValueError("Function has keyword-only arguments or annotations"
828 ", use getfullargspec() API which can support them")
Christian Heimes25bb7832008-01-11 16:17:00 +0000829 return ArgSpec(args, varargs, varkw, defaults)
830
831FullArgSpec = namedtuple('FullArgSpec',
Benjamin Peterson3d4ca742008-11-12 21:39:01 +0000832 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +0000833
834def getfullargspec(func):
835 """Get the names and default values of a function's arguments.
836
Brett Cannon504d8852007-09-07 02:12:14 +0000837 A tuple of seven things is returned:
838 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults annotations).
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000839 'args' is a list of the argument names.
Guido van Rossum2e65f892007-02-28 22:03:49 +0000840 'varargs' and 'varkw' are the names of the * and ** arguments or None.
841 'defaults' is an n-tuple of the default values of the last n arguments.
842 'kwonlyargs' is a list of keyword-only argument names.
843 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
844 'annotations' is a dictionary mapping argument names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000845
Guido van Rossum2e65f892007-02-28 22:03:49 +0000846 The first four items in the tuple correspond to getargspec().
Jeremy Hylton64967882003-06-27 18:14:39 +0000847 """
848
849 if ismethod(func):
Christian Heimesff737952007-11-27 10:40:20 +0000850 func = func.__func__
Jeremy Hylton64967882003-06-27 18:14:39 +0000851 if not isfunction(func):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000852 raise TypeError('{!r} is not a Python function'.format(func))
Guido van Rossum2e65f892007-02-28 22:03:49 +0000853 args, varargs, kwonlyargs, varkw = _getfullargs(func.__code__)
Christian Heimes25bb7832008-01-11 16:17:00 +0000854 return FullArgSpec(args, varargs, varkw, func.__defaults__,
Guido van Rossum2e65f892007-02-28 22:03:49 +0000855 kwonlyargs, func.__kwdefaults__, func.__annotations__)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000856
Christian Heimes25bb7832008-01-11 16:17:00 +0000857ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
858
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000859def getargvalues(frame):
860 """Get information about arguments passed into a particular frame.
861
862 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000863 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000864 'varargs' and 'varkw' are the names of the * and ** arguments or None.
865 'locals' is the locals dictionary of the given frame."""
866 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000867 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000868
Guido van Rossum2e65f892007-02-28 22:03:49 +0000869def formatannotation(annotation, base_module=None):
870 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000871 if annotation.__module__ in ('builtins', base_module):
Guido van Rossum2e65f892007-02-28 22:03:49 +0000872 return annotation.__name__
873 return annotation.__module__+'.'+annotation.__name__
874 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000875
Guido van Rossum2e65f892007-02-28 22:03:49 +0000876def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000877 module = getattr(object, '__module__', None)
878 def _formatannotation(annotation):
879 return formatannotation(annotation, module)
880 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +0000881
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000882def formatargspec(args, varargs=None, varkw=None, defaults=None,
Guido van Rossum2e65f892007-02-28 22:03:49 +0000883 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000884 formatarg=str,
885 formatvarargs=lambda name: '*' + name,
886 formatvarkw=lambda name: '**' + name,
887 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +0000888 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000889 formatannotation=formatannotation):
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000890 """Format an argument spec from the values returned by getargspec
Guido van Rossum2e65f892007-02-28 22:03:49 +0000891 or getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000892
Guido van Rossum2e65f892007-02-28 22:03:49 +0000893 The first seven arguments are (args, varargs, varkw, defaults,
894 kwonlyargs, kwonlydefaults, annotations). The other five arguments
895 are the corresponding optional formatting functions that are called to
896 turn names and values into strings. The last argument is an optional
897 function to format the sequence of arguments."""
898 def formatargandannotation(arg):
899 result = formatarg(arg)
900 if arg in annotations:
901 result += ': ' + formatannotation(annotations[arg])
902 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000903 specs = []
904 if defaults:
905 firstdefault = len(args) - len(defaults)
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000906 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000907 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000908 if defaults and i >= firstdefault:
909 spec = spec + formatvalue(defaults[i - firstdefault])
910 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +0000911 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +0000912 specs.append(formatvarargs(formatargandannotation(varargs)))
913 else:
914 if kwonlyargs:
915 specs.append('*')
916 if kwonlyargs:
917 for kwonlyarg in kwonlyargs:
918 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +0000919 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +0000920 spec += formatvalue(kwonlydefaults[kwonlyarg])
921 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +0000922 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +0000923 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000924 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +0000925 if 'return' in annotations:
926 result += formatreturns(formatannotation(annotations['return']))
927 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000928
929def formatargvalues(args, varargs, varkw, locals,
930 formatarg=str,
931 formatvarargs=lambda name: '*' + name,
932 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000933 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000934 """Format an argument spec from the 4 values returned by getargvalues.
935
936 The first four arguments are (args, varargs, varkw, locals). The
937 next four arguments are the corresponding optional formatting functions
938 that are called to turn names and values into strings. The ninth
939 argument is an optional function to format the sequence of arguments."""
940 def convert(name, locals=locals,
941 formatarg=formatarg, formatvalue=formatvalue):
942 return formatarg(name) + formatvalue(locals[name])
943 specs = []
944 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000945 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000946 if varargs:
947 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
948 if varkw:
949 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000950 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000951
Benjamin Petersone109c702011-06-24 09:37:26 -0500952def _missing_arguments(f_name, argnames, pos, values):
953 names = [repr(name) for name in argnames if name not in values]
954 missing = len(names)
955 if missing == 1:
956 s = names[0]
957 elif missing == 2:
958 s = "{} and {}".format(*names)
959 else:
960 tail = ", {} and {}".format(names[-2:])
961 del names[-2:]
962 s = ", ".join(names) + tail
963 raise TypeError("%s() missing %i required %s argument%s: %s" %
964 (f_name, missing,
965 "positional" if pos else "keyword-only",
966 "" if missing == 1 else "s", s))
967
968def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -0500969 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -0500970 kwonly_given = len([arg for arg in kwonly if arg in values])
971 if varargs:
972 plural = atleast != 1
973 sig = "at least %d" % (atleast,)
974 elif defcount:
975 plural = True
976 sig = "from %d to %d" % (atleast, len(args))
977 else:
978 plural = len(args) != 1
979 sig = str(len(args))
980 kwonly_sig = ""
981 if kwonly_given:
982 msg = " positional argument%s (and %d keyword-only argument%s)"
983 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
984 "s" if kwonly_given != 1 else ""))
985 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
986 (f_name, sig, "s" if plural else "", given, kwonly_sig,
987 "was" if given == 1 and not kwonly_given else "were"))
988
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +0000989def getcallargs(func, *positional, **named):
990 """Get the mapping of arguments to values.
991
992 A dict is returned, with keys the function argument names (including the
993 names of the * and ** arguments, if any), and values the respective bound
994 values from 'positional' and 'named'."""
995 spec = getfullargspec(func)
996 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
997 f_name = func.__name__
998 arg2value = {}
999
Benjamin Petersonb204a422011-06-05 22:04:07 -05001000
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001001 if ismethod(func) and func.__self__ is not None:
1002 # implicit 'self' (or 'cls' for classmethods) argument
1003 positional = (func.__self__,) + positional
1004 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001005 num_args = len(args)
1006 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001007
Benjamin Petersonb204a422011-06-05 22:04:07 -05001008 n = min(num_pos, num_args)
1009 for i in range(n):
1010 arg2value[args[i]] = positional[i]
1011 if varargs:
1012 arg2value[varargs] = tuple(positional[n:])
1013 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001014 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001015 arg2value[varkw] = {}
1016 for kw, value in named.items():
1017 if kw not in possible_kwargs:
1018 if not varkw:
1019 raise TypeError("%s() got an unexpected keyword argument %r" %
1020 (f_name, kw))
1021 arg2value[varkw][kw] = value
1022 continue
1023 if kw in arg2value:
1024 raise TypeError("%s() got multiple values for argument %r" %
1025 (f_name, kw))
1026 arg2value[kw] = value
1027 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001028 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1029 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001030 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001031 req = args[:num_args - num_defaults]
1032 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001033 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001034 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001035 for i, arg in enumerate(args[num_args - num_defaults:]):
1036 if arg not in arg2value:
1037 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001038 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001039 for kwarg in kwonlyargs:
1040 if kwarg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001041 if kwarg in kwonlydefaults:
1042 arg2value[kwarg] = kwonlydefaults[kwarg]
1043 else:
1044 missing += 1
1045 if missing:
1046 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001047 return arg2value
1048
Nick Coghlan2f92e542012-06-23 19:39:55 +10001049ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1050
1051def getclosurevars(func):
1052 """
1053 Get the mapping of free variables to their current values.
1054
Meador Inge8fda3592012-07-19 21:33:21 -05001055 Returns a named tuple of dicts mapping the current nonlocal, global
Nick Coghlan2f92e542012-06-23 19:39:55 +10001056 and builtin references as seen by the body of the function. A final
1057 set of unbound names that could not be resolved is also provided.
1058 """
1059
1060 if ismethod(func):
1061 func = func.__func__
1062
1063 if not isfunction(func):
1064 raise TypeError("'{!r}' is not a Python function".format(func))
1065
1066 code = func.__code__
1067 # Nonlocal references are named in co_freevars and resolved
1068 # by looking them up in __closure__ by positional index
1069 if func.__closure__ is None:
1070 nonlocal_vars = {}
1071 else:
1072 nonlocal_vars = {
1073 var : cell.cell_contents
1074 for var, cell in zip(code.co_freevars, func.__closure__)
1075 }
1076
1077 # Global and builtin references are named in co_names and resolved
1078 # by looking them up in __globals__ or __builtins__
1079 global_ns = func.__globals__
1080 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1081 if ismodule(builtin_ns):
1082 builtin_ns = builtin_ns.__dict__
1083 global_vars = {}
1084 builtin_vars = {}
1085 unbound_names = set()
1086 for name in code.co_names:
1087 if name in ("None", "True", "False"):
1088 # Because these used to be builtins instead of keywords, they
1089 # may still show up as name references. We ignore them.
1090 continue
1091 try:
1092 global_vars[name] = global_ns[name]
1093 except KeyError:
1094 try:
1095 builtin_vars[name] = builtin_ns[name]
1096 except KeyError:
1097 unbound_names.add(name)
1098
1099 return ClosureVars(nonlocal_vars, global_vars,
1100 builtin_vars, unbound_names)
1101
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001102# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001103
1104Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1105
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001106def getframeinfo(frame, context=1):
1107 """Get information about a frame or traceback object.
1108
1109 A tuple of five things is returned: the filename, the line number of
1110 the current line, the function name, a list of lines of context from
1111 the source code, and the index of the current line within that list.
1112 The optional second argument specifies the number of lines of context
1113 to return, which are centered around the current line."""
1114 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001115 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001116 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001117 else:
1118 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001119 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001120 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001121
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001122 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001123 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001124 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001125 try:
1126 lines, lnum = findsource(frame)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +02001127 except OSError:
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001128 lines = index = None
1129 else:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001130 start = max(start, 1)
Raymond Hettingera0501712004-06-15 11:22:53 +00001131 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001132 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001133 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001134 else:
1135 lines = index = None
1136
Christian Heimes25bb7832008-01-11 16:17:00 +00001137 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001138
1139def getlineno(frame):
1140 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001141 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1142 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001143
1144def getouterframes(frame, context=1):
1145 """Get a list of records for a frame and all higher (calling) frames.
1146
1147 Each record contains a frame object, filename, line number, function
1148 name, a list of lines of context, and index within the context."""
1149 framelist = []
1150 while frame:
1151 framelist.append((frame,) + getframeinfo(frame, context))
1152 frame = frame.f_back
1153 return framelist
1154
1155def getinnerframes(tb, context=1):
1156 """Get a list of records for a traceback's frame and all lower frames.
1157
1158 Each record contains a frame object, filename, line number, function
1159 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001160 framelist = []
1161 while tb:
1162 framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
1163 tb = tb.tb_next
1164 return framelist
1165
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001166def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001167 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001168 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001169
1170def stack(context=1):
1171 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001172 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001173
1174def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001175 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001176 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001177
1178
1179# ------------------------------------------------ static version of getattr
1180
1181_sentinel = object()
1182
Michael Foorde5162652010-11-20 16:40:44 +00001183def _static_getmro(klass):
1184 return type.__dict__['__mro__'].__get__(klass)
1185
Michael Foord95fc51d2010-11-20 15:07:30 +00001186def _check_instance(obj, attr):
1187 instance_dict = {}
1188 try:
1189 instance_dict = object.__getattribute__(obj, "__dict__")
1190 except AttributeError:
1191 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001192 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001193
1194
1195def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001196 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001197 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001198 try:
1199 return entry.__dict__[attr]
1200 except KeyError:
1201 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001202 return _sentinel
1203
Michael Foord35184ed2010-11-20 16:58:30 +00001204def _is_type(obj):
1205 try:
1206 _static_getmro(obj)
1207 except TypeError:
1208 return False
1209 return True
1210
Michael Foorddcebe0f2011-03-15 19:20:44 -04001211def _shadowed_dict(klass):
1212 dict_attr = type.__dict__["__dict__"]
1213 for entry in _static_getmro(klass):
1214 try:
1215 class_dict = dict_attr.__get__(entry)["__dict__"]
1216 except KeyError:
1217 pass
1218 else:
1219 if not (type(class_dict) is types.GetSetDescriptorType and
1220 class_dict.__name__ == "__dict__" and
1221 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001222 return class_dict
1223 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001224
1225def getattr_static(obj, attr, default=_sentinel):
1226 """Retrieve attributes without triggering dynamic lookup via the
1227 descriptor protocol, __getattr__ or __getattribute__.
1228
1229 Note: this function may not be able to retrieve all attributes
1230 that getattr can fetch (like dynamically created attributes)
1231 and may find attributes that getattr can't (like descriptors
1232 that raise AttributeError). It can also return descriptor objects
1233 instead of instance members in some cases. See the
1234 documentation for details.
1235 """
1236 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001237 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001238 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001239 dict_attr = _shadowed_dict(klass)
1240 if (dict_attr is _sentinel or
1241 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001242 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001243 else:
1244 klass = obj
1245
1246 klass_result = _check_class(klass, attr)
1247
1248 if instance_result is not _sentinel and klass_result is not _sentinel:
1249 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1250 _check_class(type(klass_result), '__set__') is not _sentinel):
1251 return klass_result
1252
1253 if instance_result is not _sentinel:
1254 return instance_result
1255 if klass_result is not _sentinel:
1256 return klass_result
1257
1258 if obj is klass:
1259 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001260 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001261 if _shadowed_dict(type(entry)) is _sentinel:
1262 try:
1263 return entry.__dict__[attr]
1264 except KeyError:
1265 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001266 if default is not _sentinel:
1267 return default
1268 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001269
1270
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001271# ------------------------------------------------ generator introspection
1272
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001273GEN_CREATED = 'GEN_CREATED'
1274GEN_RUNNING = 'GEN_RUNNING'
1275GEN_SUSPENDED = 'GEN_SUSPENDED'
1276GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001277
1278def getgeneratorstate(generator):
1279 """Get current state of a generator-iterator.
1280
1281 Possible states are:
1282 GEN_CREATED: Waiting to start execution.
1283 GEN_RUNNING: Currently being executed by the interpreter.
1284 GEN_SUSPENDED: Currently suspended at a yield expression.
1285 GEN_CLOSED: Execution has completed.
1286 """
1287 if generator.gi_running:
1288 return GEN_RUNNING
1289 if generator.gi_frame is None:
1290 return GEN_CLOSED
1291 if generator.gi_frame.f_lasti == -1:
1292 return GEN_CREATED
1293 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001294
1295
Nick Coghlan04e2e3f2012-06-23 19:52:05 +10001296def getgeneratorlocals(generator):
1297 """
1298 Get the mapping of generator local variables to their current values.
1299
1300 A dict is returned, with the keys the local variable names and values the
1301 bound values."""
1302
1303 if not isgenerator(generator):
1304 raise TypeError("'{!r}' is not a Python generator".format(generator))
1305
1306 frame = getattr(generator, "gi_frame", None)
1307 if frame is not None:
1308 return generator.gi_frame.f_locals
1309 else:
1310 return {}
1311
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001312###############################################################################
1313### Function Signature Object (PEP 362)
1314###############################################################################
1315
1316
1317_WrapperDescriptor = type(type.__call__)
1318_MethodWrapper = type(all.__call__)
1319
1320_NonUserDefinedCallables = (_WrapperDescriptor,
1321 _MethodWrapper,
1322 types.BuiltinFunctionType)
1323
1324
1325def _get_user_defined_method(cls, method_name):
1326 try:
1327 meth = getattr(cls, method_name)
1328 except AttributeError:
1329 return
1330 else:
1331 if not isinstance(meth, _NonUserDefinedCallables):
1332 # Once '__signature__' will be added to 'C'-level
1333 # callables, this check won't be necessary
1334 return meth
1335
1336
1337def signature(obj):
1338 '''Get a signature object for the passed callable.'''
1339
1340 if not callable(obj):
1341 raise TypeError('{!r} is not a callable object'.format(obj))
1342
1343 if isinstance(obj, types.MethodType):
1344 # In this case we skip the first parameter of the underlying
1345 # function (usually `self` or `cls`).
1346 sig = signature(obj.__func__)
1347 return sig.replace(parameters=tuple(sig.parameters.values())[1:])
1348
1349 try:
1350 sig = obj.__signature__
1351 except AttributeError:
1352 pass
1353 else:
1354 if sig is not None:
1355 return sig
1356
1357 try:
1358 # Was this function wrapped by a decorator?
1359 wrapped = obj.__wrapped__
1360 except AttributeError:
1361 pass
1362 else:
1363 return signature(wrapped)
1364
1365 if isinstance(obj, types.FunctionType):
1366 return Signature.from_function(obj)
1367
1368 if isinstance(obj, functools.partial):
1369 sig = signature(obj.func)
1370
1371 new_params = OrderedDict(sig.parameters.items())
1372
1373 partial_args = obj.args or ()
1374 partial_keywords = obj.keywords or {}
1375 try:
1376 ba = sig.bind_partial(*partial_args, **partial_keywords)
1377 except TypeError as ex:
1378 msg = 'partial object {!r} has incorrect arguments'.format(obj)
1379 raise ValueError(msg) from ex
1380
1381 for arg_name, arg_value in ba.arguments.items():
1382 param = new_params[arg_name]
1383 if arg_name in partial_keywords:
1384 # We set a new default value, because the following code
1385 # is correct:
1386 #
1387 # >>> def foo(a): print(a)
1388 # >>> print(partial(partial(foo, a=10), a=20)())
1389 # 20
1390 # >>> print(partial(partial(foo, a=10), a=20)(a=30))
1391 # 30
1392 #
1393 # So, with 'partial' objects, passing a keyword argument is
1394 # like setting a new default value for the corresponding
1395 # parameter
1396 #
1397 # We also mark this parameter with '_partial_kwarg'
1398 # flag. Later, in '_bind', the 'default' value of this
1399 # parameter will be added to 'kwargs', to simulate
1400 # the 'functools.partial' real call.
1401 new_params[arg_name] = param.replace(default=arg_value,
1402 _partial_kwarg=True)
1403
1404 elif (param.kind not in (_VAR_KEYWORD, _VAR_POSITIONAL) and
1405 not param._partial_kwarg):
1406 new_params.pop(arg_name)
1407
1408 return sig.replace(parameters=new_params.values())
1409
1410 sig = None
1411 if isinstance(obj, type):
1412 # obj is a class or a metaclass
1413
1414 # First, let's see if it has an overloaded __call__ defined
1415 # in its metaclass
1416 call = _get_user_defined_method(type(obj), '__call__')
1417 if call is not None:
1418 sig = signature(call)
1419 else:
1420 # Now we check if the 'obj' class has a '__new__' method
1421 new = _get_user_defined_method(obj, '__new__')
1422 if new is not None:
1423 sig = signature(new)
1424 else:
1425 # Finally, we should have at least __init__ implemented
1426 init = _get_user_defined_method(obj, '__init__')
1427 if init is not None:
1428 sig = signature(init)
1429 elif not isinstance(obj, _NonUserDefinedCallables):
1430 # An object with __call__
1431 # We also check that the 'obj' is not an instance of
1432 # _WrapperDescriptor or _MethodWrapper to avoid
1433 # infinite recursion (and even potential segfault)
1434 call = _get_user_defined_method(type(obj), '__call__')
1435 if call is not None:
1436 sig = signature(call)
1437
1438 if sig is not None:
1439 # For classes and objects we skip the first parameter of their
1440 # __call__, __new__, or __init__ methods
1441 return sig.replace(parameters=tuple(sig.parameters.values())[1:])
1442
1443 if isinstance(obj, types.BuiltinFunctionType):
1444 # Raise a nicer error message for builtins
1445 msg = 'no signature found for builtin function {!r}'.format(obj)
1446 raise ValueError(msg)
1447
1448 raise ValueError('callable {!r} is not supported by signature'.format(obj))
1449
1450
1451class _void:
1452 '''A private marker - used in Parameter & Signature'''
1453
1454
1455class _empty:
1456 pass
1457
1458
1459class _ParameterKind(int):
1460 def __new__(self, *args, name):
1461 obj = int.__new__(self, *args)
1462 obj._name = name
1463 return obj
1464
1465 def __str__(self):
1466 return self._name
1467
1468 def __repr__(self):
1469 return '<_ParameterKind: {!r}>'.format(self._name)
1470
1471
1472_POSITIONAL_ONLY = _ParameterKind(0, name='POSITIONAL_ONLY')
1473_POSITIONAL_OR_KEYWORD = _ParameterKind(1, name='POSITIONAL_OR_KEYWORD')
1474_VAR_POSITIONAL = _ParameterKind(2, name='VAR_POSITIONAL')
1475_KEYWORD_ONLY = _ParameterKind(3, name='KEYWORD_ONLY')
1476_VAR_KEYWORD = _ParameterKind(4, name='VAR_KEYWORD')
1477
1478
1479class Parameter:
1480 '''Represents a parameter in a function signature.
1481
1482 Has the following public attributes:
1483
1484 * name : str
1485 The name of the parameter as a string.
1486 * default : object
1487 The default value for the parameter if specified. If the
1488 parameter has no default value, this attribute is not set.
1489 * annotation
1490 The annotation for the parameter if specified. If the
1491 parameter has no annotation, this attribute is not set.
1492 * kind : str
1493 Describes how argument values are bound to the parameter.
1494 Possible values: `Parameter.POSITIONAL_ONLY`,
1495 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
1496 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
1497 '''
1498
1499 __slots__ = ('_name', '_kind', '_default', '_annotation', '_partial_kwarg')
1500
1501 POSITIONAL_ONLY = _POSITIONAL_ONLY
1502 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
1503 VAR_POSITIONAL = _VAR_POSITIONAL
1504 KEYWORD_ONLY = _KEYWORD_ONLY
1505 VAR_KEYWORD = _VAR_KEYWORD
1506
1507 empty = _empty
1508
1509 def __init__(self, name, kind, *, default=_empty, annotation=_empty,
1510 _partial_kwarg=False):
1511
1512 if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
1513 _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
1514 raise ValueError("invalid value for 'Parameter.kind' attribute")
1515 self._kind = kind
1516
1517 if default is not _empty:
1518 if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
1519 msg = '{} parameters cannot have default values'.format(kind)
1520 raise ValueError(msg)
1521 self._default = default
1522 self._annotation = annotation
1523
1524 if name is None:
1525 if kind != _POSITIONAL_ONLY:
1526 raise ValueError("None is not a valid name for a "
1527 "non-positional-only parameter")
1528 self._name = name
1529 else:
1530 name = str(name)
1531 if kind != _POSITIONAL_ONLY and not name.isidentifier():
1532 msg = '{!r} is not a valid parameter name'.format(name)
1533 raise ValueError(msg)
1534 self._name = name
1535
1536 self._partial_kwarg = _partial_kwarg
1537
1538 @property
1539 def name(self):
1540 return self._name
1541
1542 @property
1543 def default(self):
1544 return self._default
1545
1546 @property
1547 def annotation(self):
1548 return self._annotation
1549
1550 @property
1551 def kind(self):
1552 return self._kind
1553
1554 def replace(self, *, name=_void, kind=_void, annotation=_void,
1555 default=_void, _partial_kwarg=_void):
1556 '''Creates a customized copy of the Parameter.'''
1557
1558 if name is _void:
1559 name = self._name
1560
1561 if kind is _void:
1562 kind = self._kind
1563
1564 if annotation is _void:
1565 annotation = self._annotation
1566
1567 if default is _void:
1568 default = self._default
1569
1570 if _partial_kwarg is _void:
1571 _partial_kwarg = self._partial_kwarg
1572
1573 return type(self)(name, kind, default=default, annotation=annotation,
1574 _partial_kwarg=_partial_kwarg)
1575
1576 def __str__(self):
1577 kind = self.kind
1578
1579 formatted = self._name
1580 if kind == _POSITIONAL_ONLY:
1581 if formatted is None:
1582 formatted = ''
1583 formatted = '<{}>'.format(formatted)
1584
1585 # Add annotation and default value
1586 if self._annotation is not _empty:
1587 formatted = '{}:{}'.format(formatted,
1588 formatannotation(self._annotation))
1589
1590 if self._default is not _empty:
1591 formatted = '{}={}'.format(formatted, repr(self._default))
1592
1593 if kind == _VAR_POSITIONAL:
1594 formatted = '*' + formatted
1595 elif kind == _VAR_KEYWORD:
1596 formatted = '**' + formatted
1597
1598 return formatted
1599
1600 def __repr__(self):
1601 return '<{} at {:#x} {!r}>'.format(self.__class__.__name__,
1602 id(self), self.name)
1603
1604 def __eq__(self, other):
1605 return (issubclass(other.__class__, Parameter) and
1606 self._name == other._name and
1607 self._kind == other._kind and
1608 self._default == other._default and
1609 self._annotation == other._annotation)
1610
1611 def __ne__(self, other):
1612 return not self.__eq__(other)
1613
1614
1615class BoundArguments:
1616 '''Result of `Signature.bind` call. Holds the mapping of arguments
1617 to the function's parameters.
1618
1619 Has the following public attributes:
1620
1621 * arguments : OrderedDict
1622 An ordered mutable mapping of parameters' names to arguments' values.
1623 Does not contain arguments' default values.
1624 * signature : Signature
1625 The Signature object that created this instance.
1626 * args : tuple
1627 Tuple of positional arguments values.
1628 * kwargs : dict
1629 Dict of keyword arguments values.
1630 '''
1631
1632 def __init__(self, signature, arguments):
1633 self.arguments = arguments
1634 self._signature = signature
1635
1636 @property
1637 def signature(self):
1638 return self._signature
1639
1640 @property
1641 def args(self):
1642 args = []
1643 for param_name, param in self._signature.parameters.items():
1644 if (param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY) or
1645 param._partial_kwarg):
1646 # Keyword arguments mapped by 'functools.partial'
1647 # (Parameter._partial_kwarg is True) are mapped
1648 # in 'BoundArguments.kwargs', along with VAR_KEYWORD &
1649 # KEYWORD_ONLY
1650 break
1651
1652 try:
1653 arg = self.arguments[param_name]
1654 except KeyError:
1655 # We're done here. Other arguments
1656 # will be mapped in 'BoundArguments.kwargs'
1657 break
1658 else:
1659 if param.kind == _VAR_POSITIONAL:
1660 # *args
1661 args.extend(arg)
1662 else:
1663 # plain argument
1664 args.append(arg)
1665
1666 return tuple(args)
1667
1668 @property
1669 def kwargs(self):
1670 kwargs = {}
1671 kwargs_started = False
1672 for param_name, param in self._signature.parameters.items():
1673 if not kwargs_started:
1674 if (param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY) or
1675 param._partial_kwarg):
1676 kwargs_started = True
1677 else:
1678 if param_name not in self.arguments:
1679 kwargs_started = True
1680 continue
1681
1682 if not kwargs_started:
1683 continue
1684
1685 try:
1686 arg = self.arguments[param_name]
1687 except KeyError:
1688 pass
1689 else:
1690 if param.kind == _VAR_KEYWORD:
1691 # **kwargs
1692 kwargs.update(arg)
1693 else:
1694 # plain keyword argument
1695 kwargs[param_name] = arg
1696
1697 return kwargs
1698
1699 def __eq__(self, other):
1700 return (issubclass(other.__class__, BoundArguments) and
1701 self.signature == other.signature and
1702 self.arguments == other.arguments)
1703
1704 def __ne__(self, other):
1705 return not self.__eq__(other)
1706
1707
1708class Signature:
1709 '''A Signature object represents the overall signature of a function.
1710 It stores a Parameter object for each parameter accepted by the
1711 function, as well as information specific to the function itself.
1712
1713 A Signature object has the following public attributes and methods:
1714
1715 * parameters : OrderedDict
1716 An ordered mapping of parameters' names to the corresponding
1717 Parameter objects (keyword-only arguments are in the same order
1718 as listed in `code.co_varnames`).
1719 * return_annotation : object
1720 The annotation for the return type of the function if specified.
1721 If the function has no annotation for its return type, this
1722 attribute is not set.
1723 * bind(*args, **kwargs) -> BoundArguments
1724 Creates a mapping from positional and keyword arguments to
1725 parameters.
1726 * bind_partial(*args, **kwargs) -> BoundArguments
1727 Creates a partial mapping from positional and keyword arguments
1728 to parameters (simulating 'functools.partial' behavior.)
1729 '''
1730
1731 __slots__ = ('_return_annotation', '_parameters')
1732
1733 _parameter_cls = Parameter
1734 _bound_arguments_cls = BoundArguments
1735
1736 empty = _empty
1737
1738 def __init__(self, parameters=None, *, return_annotation=_empty,
1739 __validate_parameters__=True):
1740 '''Constructs Signature from the given list of Parameter
1741 objects and 'return_annotation'. All arguments are optional.
1742 '''
1743
1744 if parameters is None:
1745 params = OrderedDict()
1746 else:
1747 if __validate_parameters__:
1748 params = OrderedDict()
1749 top_kind = _POSITIONAL_ONLY
1750
1751 for idx, param in enumerate(parameters):
1752 kind = param.kind
1753 if kind < top_kind:
1754 msg = 'wrong parameter order: {} before {}'
1755 msg = msg.format(top_kind, param.kind)
1756 raise ValueError(msg)
1757 else:
1758 top_kind = kind
1759
1760 name = param.name
1761 if name is None:
1762 name = str(idx)
1763 param = param.replace(name=name)
1764
1765 if name in params:
1766 msg = 'duplicate parameter name: {!r}'.format(name)
1767 raise ValueError(msg)
1768 params[name] = param
1769 else:
1770 params = OrderedDict(((param.name, param)
1771 for param in parameters))
1772
1773 self._parameters = types.MappingProxyType(params)
1774 self._return_annotation = return_annotation
1775
1776 @classmethod
1777 def from_function(cls, func):
1778 '''Constructs Signature for the given python function'''
1779
1780 if not isinstance(func, types.FunctionType):
1781 raise TypeError('{!r} is not a Python function'.format(func))
1782
1783 Parameter = cls._parameter_cls
1784
1785 # Parameter information.
1786 func_code = func.__code__
1787 pos_count = func_code.co_argcount
1788 arg_names = func_code.co_varnames
1789 positional = tuple(arg_names[:pos_count])
1790 keyword_only_count = func_code.co_kwonlyargcount
1791 keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)]
1792 annotations = func.__annotations__
1793 defaults = func.__defaults__
1794 kwdefaults = func.__kwdefaults__
1795
1796 if defaults:
1797 pos_default_count = len(defaults)
1798 else:
1799 pos_default_count = 0
1800
1801 parameters = []
1802
1803 # Non-keyword-only parameters w/o defaults.
1804 non_default_count = pos_count - pos_default_count
1805 for name in positional[:non_default_count]:
1806 annotation = annotations.get(name, _empty)
1807 parameters.append(Parameter(name, annotation=annotation,
1808 kind=_POSITIONAL_OR_KEYWORD))
1809
1810 # ... w/ defaults.
1811 for offset, name in enumerate(positional[non_default_count:]):
1812 annotation = annotations.get(name, _empty)
1813 parameters.append(Parameter(name, annotation=annotation,
1814 kind=_POSITIONAL_OR_KEYWORD,
1815 default=defaults[offset]))
1816
1817 # *args
1818 if func_code.co_flags & 0x04:
1819 name = arg_names[pos_count + keyword_only_count]
1820 annotation = annotations.get(name, _empty)
1821 parameters.append(Parameter(name, annotation=annotation,
1822 kind=_VAR_POSITIONAL))
1823
1824 # Keyword-only parameters.
1825 for name in keyword_only:
1826 default = _empty
1827 if kwdefaults is not None:
1828 default = kwdefaults.get(name, _empty)
1829
1830 annotation = annotations.get(name, _empty)
1831 parameters.append(Parameter(name, annotation=annotation,
1832 kind=_KEYWORD_ONLY,
1833 default=default))
1834 # **kwargs
1835 if func_code.co_flags & 0x08:
1836 index = pos_count + keyword_only_count
1837 if func_code.co_flags & 0x04:
1838 index += 1
1839
1840 name = arg_names[index]
1841 annotation = annotations.get(name, _empty)
1842 parameters.append(Parameter(name, annotation=annotation,
1843 kind=_VAR_KEYWORD))
1844
1845 return cls(parameters,
1846 return_annotation=annotations.get('return', _empty),
1847 __validate_parameters__=False)
1848
1849 @property
1850 def parameters(self):
1851 return self._parameters
1852
1853 @property
1854 def return_annotation(self):
1855 return self._return_annotation
1856
1857 def replace(self, *, parameters=_void, return_annotation=_void):
1858 '''Creates a customized copy of the Signature.
1859 Pass 'parameters' and/or 'return_annotation' arguments
1860 to override them in the new copy.
1861 '''
1862
1863 if parameters is _void:
1864 parameters = self.parameters.values()
1865
1866 if return_annotation is _void:
1867 return_annotation = self._return_annotation
1868
1869 return type(self)(parameters,
1870 return_annotation=return_annotation)
1871
1872 def __eq__(self, other):
1873 if (not issubclass(type(other), Signature) or
1874 self.return_annotation != other.return_annotation or
1875 len(self.parameters) != len(other.parameters)):
1876 return False
1877
1878 other_positions = {param: idx
1879 for idx, param in enumerate(other.parameters.keys())}
1880
1881 for idx, (param_name, param) in enumerate(self.parameters.items()):
1882 if param.kind == _KEYWORD_ONLY:
1883 try:
1884 other_param = other.parameters[param_name]
1885 except KeyError:
1886 return False
1887 else:
1888 if param != other_param:
1889 return False
1890 else:
1891 try:
1892 other_idx = other_positions[param_name]
1893 except KeyError:
1894 return False
1895 else:
1896 if (idx != other_idx or
1897 param != other.parameters[param_name]):
1898 return False
1899
1900 return True
1901
1902 def __ne__(self, other):
1903 return not self.__eq__(other)
1904
1905 def _bind(self, args, kwargs, *, partial=False):
1906 '''Private method. Don't use directly.'''
1907
1908 arguments = OrderedDict()
1909
1910 parameters = iter(self.parameters.values())
1911 parameters_ex = ()
1912 arg_vals = iter(args)
1913
1914 if partial:
1915 # Support for binding arguments to 'functools.partial' objects.
1916 # See 'functools.partial' case in 'signature()' implementation
1917 # for details.
1918 for param_name, param in self.parameters.items():
1919 if (param._partial_kwarg and param_name not in kwargs):
1920 # Simulating 'functools.partial' behavior
1921 kwargs[param_name] = param.default
1922
1923 while True:
1924 # Let's iterate through the positional arguments and corresponding
1925 # parameters
1926 try:
1927 arg_val = next(arg_vals)
1928 except StopIteration:
1929 # No more positional arguments
1930 try:
1931 param = next(parameters)
1932 except StopIteration:
1933 # No more parameters. That's it. Just need to check that
1934 # we have no `kwargs` after this while loop
1935 break
1936 else:
1937 if param.kind == _VAR_POSITIONAL:
1938 # That's OK, just empty *args. Let's start parsing
1939 # kwargs
1940 break
1941 elif param.name in kwargs:
1942 if param.kind == _POSITIONAL_ONLY:
1943 msg = '{arg!r} parameter is positional only, ' \
1944 'but was passed as a keyword'
1945 msg = msg.format(arg=param.name)
1946 raise TypeError(msg) from None
1947 parameters_ex = (param,)
1948 break
1949 elif (param.kind == _VAR_KEYWORD or
1950 param.default is not _empty):
1951 # That's fine too - we have a default value for this
1952 # parameter. So, lets start parsing `kwargs`, starting
1953 # with the current parameter
1954 parameters_ex = (param,)
1955 break
1956 else:
1957 if partial:
1958 parameters_ex = (param,)
1959 break
1960 else:
1961 msg = '{arg!r} parameter lacking default value'
1962 msg = msg.format(arg=param.name)
1963 raise TypeError(msg) from None
1964 else:
1965 # We have a positional argument to process
1966 try:
1967 param = next(parameters)
1968 except StopIteration:
1969 raise TypeError('too many positional arguments') from None
1970 else:
1971 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1972 # Looks like we have no parameter for this positional
1973 # argument
1974 raise TypeError('too many positional arguments')
1975
1976 if param.kind == _VAR_POSITIONAL:
1977 # We have an '*args'-like argument, let's fill it with
1978 # all positional arguments we have left and move on to
1979 # the next phase
1980 values = [arg_val]
1981 values.extend(arg_vals)
1982 arguments[param.name] = tuple(values)
1983 break
1984
1985 if param.name in kwargs:
1986 raise TypeError('multiple values for argument '
1987 '{arg!r}'.format(arg=param.name))
1988
1989 arguments[param.name] = arg_val
1990
1991 # Now, we iterate through the remaining parameters to process
1992 # keyword arguments
1993 kwargs_param = None
1994 for param in itertools.chain(parameters_ex, parameters):
1995 if param.kind == _POSITIONAL_ONLY:
1996 # This should never happen in case of a properly built
1997 # Signature object (but let's have this check here
1998 # to ensure correct behaviour just in case)
1999 raise TypeError('{arg!r} parameter is positional only, '
2000 'but was passed as a keyword'. \
2001 format(arg=param.name))
2002
2003 if param.kind == _VAR_KEYWORD:
2004 # Memorize that we have a '**kwargs'-like parameter
2005 kwargs_param = param
2006 continue
2007
2008 param_name = param.name
2009 try:
2010 arg_val = kwargs.pop(param_name)
2011 except KeyError:
2012 # We have no value for this parameter. It's fine though,
2013 # if it has a default value, or it is an '*args'-like
2014 # parameter, left alone by the processing of positional
2015 # arguments.
2016 if (not partial and param.kind != _VAR_POSITIONAL and
2017 param.default is _empty):
2018 raise TypeError('{arg!r} parameter lacking default value'. \
2019 format(arg=param_name)) from None
2020
2021 else:
2022 arguments[param_name] = arg_val
2023
2024 if kwargs:
2025 if kwargs_param is not None:
2026 # Process our '**kwargs'-like parameter
2027 arguments[kwargs_param.name] = kwargs
2028 else:
2029 raise TypeError('too many keyword arguments')
2030
2031 return self._bound_arguments_cls(self, arguments)
2032
Antoine Pitroubd41d1b2013-01-29 21:20:57 +01002033 def bind(__bind_self, *args, **kwargs):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002034 '''Get a BoundArguments object, that maps the passed `args`
2035 and `kwargs` to the function's signature. Raises `TypeError`
2036 if the passed arguments can not be bound.
2037 '''
Antoine Pitroubd41d1b2013-01-29 21:20:57 +01002038 return __bind_self._bind(args, kwargs)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002039
Antoine Pitroubd41d1b2013-01-29 21:20:57 +01002040 def bind_partial(__bind_self, *args, **kwargs):
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002041 '''Get a BoundArguments object, that partially maps the
2042 passed `args` and `kwargs` to the function's signature.
2043 Raises `TypeError` if the passed arguments can not be bound.
2044 '''
Antoine Pitroubd41d1b2013-01-29 21:20:57 +01002045 return __bind_self._bind(args, kwargs, partial=True)
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07002046
2047 def __str__(self):
2048 result = []
2049 render_kw_only_separator = True
2050 for idx, param in enumerate(self.parameters.values()):
2051 formatted = str(param)
2052
2053 kind = param.kind
2054 if kind == _VAR_POSITIONAL:
2055 # OK, we have an '*args'-like parameter, so we won't need
2056 # a '*' to separate keyword-only arguments
2057 render_kw_only_separator = False
2058 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
2059 # We have a keyword-only parameter to render and we haven't
2060 # rendered an '*args'-like parameter before, so add a '*'
2061 # separator to the parameters list ("foo(arg1, *, arg2)" case)
2062 result.append('*')
2063 # This condition should be only triggered once, so
2064 # reset the flag
2065 render_kw_only_separator = False
2066
2067 result.append(formatted)
2068
2069 rendered = '({})'.format(', '.join(result))
2070
2071 if self.return_annotation is not _empty:
2072 anno = formatannotation(self.return_annotation)
2073 rendered += ' -> {}'.format(anno)
2074
2075 return rendered