blob: dd2de6422123594eaba4c0db3bae11a0dc3b66dc [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
Christian Heimes7131fd92008-02-19 14:21:46 +000034import imp
Brett Cannoncb66eb02012-05-11 12:58:42 -040035import importlib.machinery
36import itertools
Christian Heimes7131fd92008-02-19 14:21:46 +000037import linecache
Brett Cannoncb66eb02012-05-11 12:58:42 -040038import os
39import re
40import sys
41import tokenize
42import types
Brett Cannon2b88fcf2012-06-02 22:28:42 -040043import warnings
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070044import functools
Nick Coghlan2f92e542012-06-23 19:39:55 +100045import builtins
Raymond Hettingera1a992c2005-03-11 06:46:45 +000046from operator import attrgetter
Larry Hastings7c7cbfc2012-06-22 15:19:35 -070047from collections import namedtuple, OrderedDict
Nick Coghlan09c81232010-08-17 10:18:16 +000048
49# Create constants for the compiler flags in Include/code.h
50# We try to get them from dis to avoid duplication, but fall
51# back to hardcording so the dependency is optional
52try:
53 from dis import COMPILER_FLAG_NAMES as _flag_names
54except ImportError:
55 CO_OPTIMIZED, CO_NEWLOCALS = 0x1, 0x2
56 CO_VARARGS, CO_VARKEYWORDS = 0x4, 0x8
57 CO_NESTED, CO_GENERATOR, CO_NOFREE = 0x10, 0x20, 0x40
58else:
59 mod_dict = globals()
60 for k, v in _flag_names.items():
61 mod_dict["CO_" + v] = k
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000062
Christian Heimesbe5b30b2008-03-03 19:18:51 +000063# See Include/object.h
64TPFLAGS_IS_ABSTRACT = 1 << 20
65
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000066# ----------------------------------------------------------- type-checking
67def ismodule(object):
68 """Return true if the object is a module.
69
70 Module objects provide these attributes:
Barry Warsaw28a691b2010-04-17 00:19:56 +000071 __cached__ pathname to byte compiled file
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000072 __doc__ documentation string
73 __file__ filename (missing for built-in modules)"""
Tim Peters28bc59f2001-09-16 08:40:16 +000074 return isinstance(object, types.ModuleType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000075
76def isclass(object):
77 """Return true if the object is a class.
78
79 Class objects provide these attributes:
80 __doc__ documentation string
81 __module__ name of module in which this class was defined"""
Benjamin Petersonc4656002009-01-17 22:41:18 +000082 return isinstance(object, type)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000083
84def ismethod(object):
85 """Return true if the object is an instance method.
86
87 Instance method objects provide these attributes:
88 __doc__ documentation string
89 __name__ name with which this method was defined
Christian Heimesff737952007-11-27 10:40:20 +000090 __func__ function object containing implementation of method
91 __self__ instance to which this method is bound"""
Tim Peters28bc59f2001-09-16 08:40:16 +000092 return isinstance(object, types.MethodType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +000093
Tim Peters536d2262001-09-20 05:13:38 +000094def ismethoddescriptor(object):
Tim Petersf1d90b92001-09-20 05:47:55 +000095 """Return true if the object is a method descriptor.
96
97 But not if ismethod() or isclass() or isfunction() are true.
Tim Peters536d2262001-09-20 05:13:38 +000098
99 This is new in Python 2.2, and, for example, is true of int.__add__.
100 An object passing this test has a __get__ attribute but not a __set__
101 attribute, but beyond that the set of attributes varies. __name__ is
102 usually sensible, and __doc__ often is.
103
Tim Petersf1d90b92001-09-20 05:47:55 +0000104 Methods implemented via descriptors that also pass one of the other
105 tests return false from the ismethoddescriptor() test, simply because
106 the other tests promise more -- you can, e.g., count on having the
Christian Heimesff737952007-11-27 10:40:20 +0000107 __func__ attribute (etc) when an object passes ismethod()."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100108 if isclass(object) or ismethod(object) or isfunction(object):
109 # mutual exclusion
110 return False
111 tp = type(object)
112 return hasattr(tp, "__get__") and not hasattr(tp, "__set__")
Tim Peters536d2262001-09-20 05:13:38 +0000113
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000114def isdatadescriptor(object):
115 """Return true if the object is a data descriptor.
116
117 Data descriptors have both a __get__ and a __set__ attribute. Examples are
118 properties (defined in Python) and getsets and members (defined in C).
119 Typically, data descriptors will also have __name__ and __doc__ attributes
120 (properties, getsets, and members have both of these attributes), but this
121 is not guaranteed."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100122 if isclass(object) or ismethod(object) or isfunction(object):
123 # mutual exclusion
124 return False
125 tp = type(object)
126 return hasattr(tp, "__set__") and hasattr(tp, "__get__")
Martin v. Löwise59e2ba2003-05-03 09:09:02 +0000127
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000128if hasattr(types, 'MemberDescriptorType'):
129 # CPython and equivalent
130 def ismemberdescriptor(object):
131 """Return true if the object is a member descriptor.
132
133 Member descriptors are specialized descriptors defined in extension
134 modules."""
135 return isinstance(object, types.MemberDescriptorType)
136else:
137 # Other implementations
138 def ismemberdescriptor(object):
139 """Return true if the object is a member descriptor.
140
141 Member descriptors are specialized descriptors defined in extension
142 modules."""
143 return False
144
145if hasattr(types, 'GetSetDescriptorType'):
146 # CPython and equivalent
147 def isgetsetdescriptor(object):
148 """Return true if the object is a getset descriptor.
149
150 getset descriptors are specialized descriptors defined in extension
151 modules."""
152 return isinstance(object, types.GetSetDescriptorType)
153else:
154 # Other implementations
155 def isgetsetdescriptor(object):
156 """Return true if the object is a getset descriptor.
157
158 getset descriptors are specialized descriptors defined in extension
159 modules."""
160 return False
161
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000162def isfunction(object):
163 """Return true if the object is a user-defined function.
164
165 Function objects provide these attributes:
166 __doc__ documentation string
167 __name__ name with which this function was defined
Neal Norwitz221085d2007-02-25 20:55:47 +0000168 __code__ code object containing compiled function bytecode
169 __defaults__ tuple of any default values for arguments
170 __globals__ global namespace in which this function was defined
171 __annotations__ dict of parameter annotations
172 __kwdefaults__ dict of keyword only parameters with defaults"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000173 return isinstance(object, types.FunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000174
Christian Heimes7131fd92008-02-19 14:21:46 +0000175def isgeneratorfunction(object):
176 """Return true if the object is a user-defined generator function.
177
178 Generator function objects provides same attributes as functions.
179
Alexander Belopolsky977a6842010-08-16 20:17:07 +0000180 See help(isfunction) for attributes listing."""
Georg Brandlb1441c72009-01-03 22:33:39 +0000181 return bool((isfunction(object) or ismethod(object)) and
182 object.__code__.co_flags & CO_GENERATOR)
Christian Heimes7131fd92008-02-19 14:21:46 +0000183
184def isgenerator(object):
185 """Return true if the object is a generator.
186
187 Generator objects provide these attributes:
188 __iter__ defined to support interation over container
189 close raises a new GeneratorExit exception inside the
190 generator to terminate the iteration
191 gi_code code object
192 gi_frame frame object or possibly None once the generator has
193 been exhausted
194 gi_running set to 1 when generator is executing, 0 otherwise
195 next return the next item from the container
196 send resumes the generator and "sends" a value that becomes
197 the result of the current yield-expression
198 throw used to raise an exception inside the generator"""
199 return isinstance(object, types.GeneratorType)
200
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000201def istraceback(object):
202 """Return true if the object is a traceback.
203
204 Traceback objects provide these attributes:
205 tb_frame frame object at this level
206 tb_lasti index of last attempted instruction in bytecode
207 tb_lineno current line number in Python source code
208 tb_next next inner traceback object (called by this level)"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000209 return isinstance(object, types.TracebackType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000210
211def isframe(object):
212 """Return true if the object is a frame object.
213
214 Frame objects provide these attributes:
215 f_back next outer frame object (this frame's caller)
216 f_builtins built-in namespace seen by this frame
217 f_code code object being executed in this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000218 f_globals global namespace seen by this frame
219 f_lasti index of last attempted instruction in bytecode
220 f_lineno current line number in Python source code
221 f_locals local namespace seen by this frame
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000222 f_trace tracing function for this frame, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000223 return isinstance(object, types.FrameType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000224
225def iscode(object):
226 """Return true if the object is a code object.
227
228 Code objects provide these attributes:
229 co_argcount number of arguments (not including * or ** args)
230 co_code string of raw compiled bytecode
231 co_consts tuple of constants used in the bytecode
232 co_filename name of file in which this code object was created
233 co_firstlineno number of first line in Python source code
234 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
235 co_lnotab encoded mapping of line numbers to bytecode indices
236 co_name name with which this code object was defined
237 co_names tuple of names of local variables
238 co_nlocals number of local variables
239 co_stacksize virtual machine stack space required
240 co_varnames tuple of names of arguments and local variables"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000241 return isinstance(object, types.CodeType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000242
243def isbuiltin(object):
244 """Return true if the object is a built-in function or method.
245
246 Built-in functions and methods provide these attributes:
247 __doc__ documentation string
248 __name__ original name of this function or method
249 __self__ instance to which a method is bound, or None"""
Tim Peters28bc59f2001-09-16 08:40:16 +0000250 return isinstance(object, types.BuiltinFunctionType)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000251
252def isroutine(object):
253 """Return true if the object is any kind of function or method."""
Tim Peters536d2262001-09-20 05:13:38 +0000254 return (isbuiltin(object)
255 or isfunction(object)
256 or ismethod(object)
257 or ismethoddescriptor(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000258
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000259def isabstract(object):
260 """Return true if the object is an abstract base class (ABC)."""
Benjamin Petersona0dfa822009-11-13 02:25:08 +0000261 return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT)
Christian Heimesbe5b30b2008-03-03 19:18:51 +0000262
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000263def getmembers(object, predicate=None):
264 """Return all members of an object as (name, value) pairs sorted by name.
265 Optionally, only return members that satisfy a given predicate."""
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100266 if isclass(object):
267 mro = (object,) + getmro(object)
268 else:
269 mro = ()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000270 results = []
271 for key in dir(object):
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100272 # First try to get the value via __dict__. Some descriptors don't
273 # like calling their __get__ (see bug #1785).
274 for base in mro:
275 if key in base.__dict__:
276 value = base.__dict__[key]
277 break
278 else:
279 try:
280 value = getattr(object, key)
281 except AttributeError:
282 continue
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000283 if not predicate or predicate(value):
284 results.append((key, value))
285 results.sort()
286 return results
287
Christian Heimes25bb7832008-01-11 16:17:00 +0000288Attribute = namedtuple('Attribute', 'name kind defining_class object')
289
Tim Peters13b49d32001-09-23 02:00:29 +0000290def classify_class_attrs(cls):
291 """Return list of attribute-descriptor tuples.
292
293 For each name in dir(cls), the return list contains a 4-tuple
294 with these elements:
295
296 0. The name (a string).
297
298 1. The kind of attribute this is, one of these strings:
299 'class method' created via classmethod()
300 'static method' created via staticmethod()
301 'property' created via property()
302 'method' any other flavor of method
303 'data' not a method
304
305 2. The class which defined this attribute (a class).
306
307 3. The object as obtained directly from the defining class's
308 __dict__, not via getattr. This is especially important for
309 data attributes: C.data is just a data object, but
310 C.__dict__['data'] may be a data descriptor with additional
311 info, like a __doc__ string.
312 """
313
314 mro = getmro(cls)
315 names = dir(cls)
316 result = []
317 for name in names:
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100318 # Get the object associated with the name, and where it was defined.
Tim Peters13b49d32001-09-23 02:00:29 +0000319 # Getting an obj from the __dict__ sometimes reveals more than
320 # using getattr. Static and class methods are dramatic examples.
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100321 # Furthermore, some objects may raise an Exception when fetched with
322 # getattr(). This is the case with some descriptors (bug #1785).
323 # Thus, we only use getattr() as a last resort.
324 homecls = None
325 for base in (cls,) + mro:
326 if name in base.__dict__:
327 obj = base.__dict__[name]
328 homecls = base
329 break
Tim Peters13b49d32001-09-23 02:00:29 +0000330 else:
331 obj = getattr(cls, name)
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100332 homecls = getattr(obj, "__objclass__", homecls)
Tim Peters13b49d32001-09-23 02:00:29 +0000333
334 # Classify the object.
335 if isinstance(obj, staticmethod):
336 kind = "static method"
337 elif isinstance(obj, classmethod):
338 kind = "class method"
339 elif isinstance(obj, property):
340 kind = "property"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100341 elif ismethoddescriptor(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000342 kind = "method"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100343 elif isdatadescriptor(obj):
Tim Peters13b49d32001-09-23 02:00:29 +0000344 kind = "data"
Antoine Pitrou86a8a9a2011-12-21 09:57:40 +0100345 else:
346 obj_via_getattr = getattr(cls, name)
347 if (isfunction(obj_via_getattr) or
348 ismethoddescriptor(obj_via_getattr)):
349 kind = "method"
350 else:
351 kind = "data"
352 obj = obj_via_getattr
Tim Peters13b49d32001-09-23 02:00:29 +0000353
Christian Heimes25bb7832008-01-11 16:17:00 +0000354 result.append(Attribute(name, kind, homecls, obj))
Tim Peters13b49d32001-09-23 02:00:29 +0000355
356 return result
357
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000358# ----------------------------------------------------------- class helpers
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000359
360def getmro(cls):
361 "Return tuple of base classes (including cls) in method resolution order."
Benjamin Petersonb82c8e52010-11-04 00:38:49 +0000362 return cls.__mro__
Tim Peterse0b2d7a2001-09-22 06:10:55 +0000363
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000364# -------------------------------------------------- source code extraction
365def indentsize(line):
366 """Return the indent size, in spaces, at the start of a line of text."""
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000367 expline = line.expandtabs()
368 return len(expline) - len(expline.lstrip())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000369
370def getdoc(object):
371 """Get the documentation string for an object.
372
373 All tabs are expanded to spaces. To clean up docstrings that are
374 indented to line up with blocks of code, any whitespace than can be
375 uniformly removed from the second line onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000376 try:
377 doc = object.__doc__
378 except AttributeError:
379 return None
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000380 if not isinstance(doc, str):
Tim Peters24008312002-03-17 18:56:20 +0000381 return None
Georg Brandl0c77a822008-06-10 16:37:50 +0000382 return cleandoc(doc)
383
384def cleandoc(doc):
385 """Clean up indentation from docstrings.
386
387 Any whitespace that can be uniformly removed from the second line
388 onwards is removed."""
Tim Peters24008312002-03-17 18:56:20 +0000389 try:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000390 lines = doc.expandtabs().split('\n')
Tim Peters24008312002-03-17 18:56:20 +0000391 except UnicodeError:
392 return None
393 else:
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000394 # Find minimum indentation of any non-blank lines after first line.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000395 margin = sys.maxsize
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000396 for line in lines[1:]:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000397 content = len(line.lstrip())
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000398 if content:
399 indent = len(line) - content
400 margin = min(margin, indent)
401 # Remove indentation.
402 if lines:
403 lines[0] = lines[0].lstrip()
Christian Heimesa37d4c62007-12-04 23:02:19 +0000404 if margin < sys.maxsize:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000405 for i in range(1, len(lines)): lines[i] = lines[i][margin:]
Ka-Ping Yeea59ef7b2002-11-30 03:53:15 +0000406 # Remove any trailing or leading blank lines.
407 while lines and not lines[-1]:
408 lines.pop()
409 while lines and not lines[0]:
410 lines.pop(0)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000411 return '\n'.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000412
413def getfile(object):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000414 """Work out which source or compiled file an object was defined in."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000415 if ismodule(object):
416 if hasattr(object, '__file__'):
417 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000418 raise TypeError('{!r} is a built-in module'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000419 if isclass(object):
Ka-Ping Yeec99e0f12001-04-13 12:10:40 +0000420 object = sys.modules.get(object.__module__)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000421 if hasattr(object, '__file__'):
422 return object.__file__
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000423 raise TypeError('{!r} is a built-in class'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000424 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000425 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000426 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000427 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000428 if istraceback(object):
429 object = object.tb_frame
430 if isframe(object):
431 object = object.f_code
432 if iscode(object):
433 return object.co_filename
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000434 raise TypeError('{!r} is not a module, class, method, '
435 'function, traceback, frame, or code object'.format(object))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000436
Christian Heimes25bb7832008-01-11 16:17:00 +0000437ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')
438
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000439def getmoduleinfo(path):
440 """Get the module name, suffix, mode, and module type for a given file."""
Brett Cannoncb66eb02012-05-11 12:58:42 -0400441 warnings.warn('inspect.getmoduleinfo() is deprecated', DeprecationWarning,
442 2)
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000443 filename = os.path.basename(path)
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000444 suffixes = [(-len(suffix), suffix, mode, mtype)
445 for suffix, mode, mtype in imp.get_suffixes()]
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000446 suffixes.sort() # try longest suffixes first, in case they overlap
447 for neglen, suffix, mode, mtype in suffixes:
448 if filename[neglen:] == suffix:
Christian Heimes25bb7832008-01-11 16:17:00 +0000449 return ModuleInfo(filename[:neglen], suffix, mode, mtype)
Ka-Ping Yee4d6fc7f2001-04-10 11:43:00 +0000450
451def getmodulename(path):
452 """Return the module name for a given file, or None."""
453 info = getmoduleinfo(path)
454 if info: return info[0]
455
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000456def getsourcefile(object):
R. David Murraya1b37402010-06-17 02:04:29 +0000457 """Return the filename that can be used to locate an object's source.
458 Return None if no way can be identified to get the source.
459 """
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000460 filename = getfile(object)
Brett Cannoncb66eb02012-05-11 12:58:42 -0400461 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
462 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
463 if any(filename.endswith(s) for s in all_bytecode_suffixes):
464 filename = (os.path.splitext(filename)[0] +
465 importlib.machinery.SOURCE_SUFFIXES[0])
466 elif any(filename.endswith(s) for s in
467 importlib.machinery.EXTENSION_SUFFIXES):
468 return None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000469 if os.path.exists(filename):
470 return filename
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000471 # only return a non-existent filename if the module has a PEP 302 loader
472 if hasattr(getmodule(object, filename), '__loader__'):
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000473 return filename
R. David Murraya1b37402010-06-17 02:04:29 +0000474 # or it is in the linecache
475 if filename in linecache.cache:
476 return filename
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000477
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000478def getabsfile(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000479 """Return an absolute path to the source or compiled file for an object.
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000480
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000481 The idea is for each object to have a unique origin, so this routine
482 normalizes the result as much as possible."""
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000483 if _filename is None:
484 _filename = getsourcefile(object) or getfile(object)
485 return os.path.normcase(os.path.abspath(_filename))
Ka-Ping Yeec113c242001-03-02 02:08:53 +0000486
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000487modulesbyfile = {}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000488_filesbymodname = {}
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000489
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000490def getmodule(object, _filename=None):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000491 """Return the module an object was defined in, or None if not found."""
Ka-Ping Yee202c99b2001-04-13 09:15:08 +0000492 if ismodule(object):
493 return object
Johannes Gijsbers93245262004-09-11 15:53:22 +0000494 if hasattr(object, '__module__'):
Ka-Ping Yee8b58b842001-03-01 13:56:16 +0000495 return sys.modules.get(object.__module__)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000496 # Try the filename to modulename cache
497 if _filename is not None and _filename in modulesbyfile:
498 return sys.modules.get(modulesbyfile[_filename])
499 # Try the cache again with the absolute file name
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000500 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000501 file = getabsfile(object, _filename)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000502 except TypeError:
503 return None
Raymond Hettinger54f02222002-06-01 14:18:47 +0000504 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000505 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000506 # Update the filename to module name cache and check yet again
507 # Copy sys.modules in order to cope with changes while iterating
Éric Araujoa74f8ef2011-11-29 16:58:53 +0100508 for modname, module in list(sys.modules.items()):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000509 if ismodule(module) and hasattr(module, '__file__'):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000510 f = module.__file__
511 if f == _filesbymodname.get(modname, None):
512 # Have already mapped this module, so skip it
513 continue
514 _filesbymodname[modname] = f
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000515 f = getabsfile(module)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000516 # Always map to the name the module knows itself by
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000517 modulesbyfile[f] = modulesbyfile[
518 os.path.realpath(f)] = module.__name__
Raymond Hettinger54f02222002-06-01 14:18:47 +0000519 if file in modulesbyfile:
Ka-Ping Yeeb38bbbd2003-03-28 16:29:50 +0000520 return sys.modules.get(modulesbyfile[file])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000521 # Check the main module
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000522 main = sys.modules['__main__']
Brett Cannon4a671fe2003-06-15 22:33:28 +0000523 if not hasattr(object, '__name__'):
524 return None
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000525 if hasattr(main, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000526 mainobject = getattr(main, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000527 if mainobject is object:
528 return main
Thomas Wouters89f507f2006-12-13 04:49:30 +0000529 # Check builtins
Georg Brandl1a3284e2007-12-02 09:40:06 +0000530 builtin = sys.modules['builtins']
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000531 if hasattr(builtin, object.__name__):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000532 builtinobject = getattr(builtin, object.__name__)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000533 if builtinobject is object:
534 return builtin
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000535
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000536def findsource(object):
537 """Return the entire source file and starting line number for an object.
538
539 The argument may be a module, class, method, function, traceback, frame,
540 or code object. The source code is returned as a list of all the lines
541 in the file and the line number indexes a line in that list. An IOError
542 is raised if the source code cannot be retrieved."""
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500543
544 file = getfile(object)
545 sourcefile = getsourcefile(object)
546 if not sourcefile and file[0] + file[-1] != '<>':
R. David Murray74b89242009-05-13 17:33:03 +0000547 raise IOError('source code not available')
Benjamin Peterson9620cc02011-06-11 15:53:11 -0500548 file = sourcefile if sourcefile else file
549
Thomas Wouters89f507f2006-12-13 04:49:30 +0000550 module = getmodule(object, file)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000551 if module:
552 lines = linecache.getlines(file, module.__dict__)
553 else:
554 lines = linecache.getlines(file)
Neil Schemenauerf06f8532002-03-23 23:51:04 +0000555 if not lines:
Jeremy Hyltonab919022003-06-27 18:41:20 +0000556 raise IOError('could not get source code')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000557
558 if ismodule(object):
559 return lines, 0
560
561 if isclass(object):
562 name = object.__name__
Thomas Wouters89f507f2006-12-13 04:49:30 +0000563 pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
564 # make some effort to find the best matching class definition:
565 # use the one with the least indentation, which is the one
566 # that's most probably not inside a function definition.
567 candidates = []
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000568 for i in range(len(lines)):
Thomas Wouters89f507f2006-12-13 04:49:30 +0000569 match = pat.match(lines[i])
570 if match:
571 # if it's at toplevel, it's already the best one
572 if lines[i][0] == 'c':
573 return lines, i
574 # else add whitespace to candidate list
575 candidates.append((match.group(1), i))
576 if candidates:
577 # this will sort by whitespace, and by line number,
578 # less whitespace first
579 candidates.sort()
580 return lines, candidates[0][1]
Jeremy Hyltonab919022003-06-27 18:41:20 +0000581 else:
582 raise IOError('could not find class definition')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000583
584 if ismethod(object):
Christian Heimesff737952007-11-27 10:40:20 +0000585 object = object.__func__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000586 if isfunction(object):
Neal Norwitz221085d2007-02-25 20:55:47 +0000587 object = object.__code__
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000588 if istraceback(object):
589 object = object.tb_frame
590 if isframe(object):
591 object = object.f_code
592 if iscode(object):
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000593 if not hasattr(object, 'co_firstlineno'):
Jeremy Hyltonab919022003-06-27 18:41:20 +0000594 raise IOError('could not find function definition')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000595 lnum = object.co_firstlineno - 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000596 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000597 while lnum > 0:
Ka-Ping Yeea6e59712001-03-10 09:31:55 +0000598 if pat.match(lines[lnum]): break
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +0000599 lnum = lnum - 1
600 return lines, lnum
Jeremy Hyltonab919022003-06-27 18:41:20 +0000601 raise IOError('could not find code object')
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000602
603def getcomments(object):
Jeremy Hyltonb4c17c82002-03-28 23:01:56 +0000604 """Get lines of comments immediately preceding an object's source code.
605
606 Returns None when source can't be found.
607 """
608 try:
609 lines, lnum = findsource(object)
610 except (IOError, TypeError):
611 return None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000612
613 if ismodule(object):
614 # Look for a comment block at the top of the file.
615 start = 0
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000616 if lines and lines[0][:2] == '#!': start = 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000617 while start < len(lines) and lines[start].strip() in ('', '#'):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000618 start = start + 1
Ka-Ping Yeeb910efe2001-04-12 13:17:17 +0000619 if start < len(lines) and lines[start][:1] == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000620 comments = []
621 end = start
622 while end < len(lines) and lines[end][:1] == '#':
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000623 comments.append(lines[end].expandtabs())
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000624 end = end + 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000625 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000626
627 # Look for a preceding block of comments at the same indentation.
628 elif lnum > 0:
629 indent = indentsize(lines[lnum])
630 end = lnum - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000631 if end >= 0 and lines[end].lstrip()[:1] == '#' and \
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000632 indentsize(lines[end]) == indent:
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000633 comments = [lines[end].expandtabs().lstrip()]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000634 if end > 0:
635 end = end - 1
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000636 comment = lines[end].expandtabs().lstrip()
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000637 while comment[:1] == '#' and indentsize(lines[end]) == indent:
638 comments[:0] = [comment]
639 end = end - 1
640 if end < 0: break
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000641 comment = lines[end].expandtabs().lstrip()
642 while comments and comments[0].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000643 comments[:1] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000644 while comments and comments[-1].strip() == '#':
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000645 comments[-1:] = []
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000646 return ''.join(comments)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000647
Tim Peters4efb6e92001-06-29 23:51:08 +0000648class EndOfBlock(Exception): pass
649
650class BlockFinder:
651 """Provide a tokeneater() method to detect the end of a code block."""
652 def __init__(self):
653 self.indent = 0
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000654 self.islambda = False
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000655 self.started = False
656 self.passline = False
Armin Rigodd5c0232005-09-25 11:45:45 +0000657 self.last = 1
Tim Peters4efb6e92001-06-29 23:51:08 +0000658
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000659 def tokeneater(self, type, token, srowcol, erowcol, line):
Tim Peters4efb6e92001-06-29 23:51:08 +0000660 if not self.started:
Armin Rigodd5c0232005-09-25 11:45:45 +0000661 # look for the first "def", "class" or "lambda"
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000662 if token in ("def", "class", "lambda"):
Johannes Gijsbersa5855d52005-03-12 16:37:11 +0000663 if token == "lambda":
664 self.islambda = True
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000665 self.started = True
Armin Rigodd5c0232005-09-25 11:45:45 +0000666 self.passline = True # skip to the end of the line
Tim Peters4efb6e92001-06-29 23:51:08 +0000667 elif type == tokenize.NEWLINE:
Armin Rigodd5c0232005-09-25 11:45:45 +0000668 self.passline = False # stop skipping when a NEWLINE is seen
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000669 self.last = srowcol[0]
Armin Rigodd5c0232005-09-25 11:45:45 +0000670 if self.islambda: # lambdas always end at the first NEWLINE
671 raise EndOfBlock
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000672 elif self.passline:
673 pass
Tim Peters4efb6e92001-06-29 23:51:08 +0000674 elif type == tokenize.INDENT:
675 self.indent = self.indent + 1
Johannes Gijsbers1542f342004-12-12 16:46:28 +0000676 self.passline = True
Tim Peters4efb6e92001-06-29 23:51:08 +0000677 elif type == tokenize.DEDENT:
678 self.indent = self.indent - 1
Armin Rigodd5c0232005-09-25 11:45:45 +0000679 # the end of matching indent/dedent pairs end a block
680 # (note that this only works for "def"/"class" blocks,
681 # not e.g. for "if: else:" or "try: finally:" blocks)
682 if self.indent <= 0:
683 raise EndOfBlock
684 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
685 # any other token on the same indentation level end the previous
686 # block as well, except the pseudo-tokens COMMENT and NL.
687 raise EndOfBlock
Tim Peters4efb6e92001-06-29 23:51:08 +0000688
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000689def getblock(lines):
690 """Extract the block of code at the top of the given list of lines."""
Armin Rigodd5c0232005-09-25 11:45:45 +0000691 blockfinder = BlockFinder()
Tim Peters4efb6e92001-06-29 23:51:08 +0000692 try:
Trent Nelson428de652008-03-18 22:41:35 +0000693 tokens = tokenize.generate_tokens(iter(lines).__next__)
694 for _token in tokens:
695 blockfinder.tokeneater(*_token)
Armin Rigodd5c0232005-09-25 11:45:45 +0000696 except (EndOfBlock, IndentationError):
697 pass
698 return lines[:blockfinder.last]
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000699
700def getsourcelines(object):
701 """Return a list of source lines and starting line number for an object.
702
703 The argument may be a module, class, method, function, traceback, frame,
704 or code object. The source code is returned as a list of the lines
705 corresponding to the object and the line number indicates where in the
706 original source file the first line of code was found. An IOError is
707 raised if the source code cannot be retrieved."""
708 lines, lnum = findsource(object)
709
710 if ismodule(object): return lines, 0
711 else: return getblock(lines[lnum:]), lnum + 1
712
713def getsource(object):
714 """Return the text of the source code for an object.
715
716 The argument may be a module, class, method, function, traceback, frame,
717 or code object. The source code is returned as a single string. An
718 IOError is raised if the source code cannot be retrieved."""
719 lines, lnum = getsourcelines(object)
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000720 return ''.join(lines)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000721
722# --------------------------------------------------- class tree extraction
723def walktree(classes, children, parent):
724 """Recursive helper function for getclasstree()."""
725 results = []
Raymond Hettingera1a992c2005-03-11 06:46:45 +0000726 classes.sort(key=attrgetter('__module__', '__name__'))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000727 for c in classes:
728 results.append((c, c.__bases__))
Raymond Hettinger54f02222002-06-01 14:18:47 +0000729 if c in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000730 results.append(walktree(children[c], children, c))
731 return results
732
Georg Brandl5ce83a02009-06-01 17:23:51 +0000733def getclasstree(classes, unique=False):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000734 """Arrange the given list of classes into a hierarchy of nested lists.
735
736 Where a nested list appears, it contains classes derived from the class
737 whose entry immediately precedes the list. Each entry is a 2-tuple
738 containing a class and a tuple of its base classes. If the 'unique'
739 argument is true, exactly one entry appears in the returned structure
740 for each class in the given list. Otherwise, classes using multiple
741 inheritance and their descendants will appear multiple times."""
742 children = {}
743 roots = []
744 for c in classes:
745 if c.__bases__:
746 for parent in c.__bases__:
Raymond Hettinger54f02222002-06-01 14:18:47 +0000747 if not parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000748 children[parent] = []
749 children[parent].append(c)
750 if unique and parent in classes: break
751 elif c not in roots:
752 roots.append(c)
Raymond Hettingere0d49722002-06-02 18:55:56 +0000753 for parent in children:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000754 if parent not in classes:
755 roots.append(parent)
756 return walktree(roots, children, None)
757
758# ------------------------------------------------ argument list extraction
Christian Heimes25bb7832008-01-11 16:17:00 +0000759Arguments = namedtuple('Arguments', 'args, varargs, varkw')
760
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000761def getargs(co):
762 """Get information about the arguments accepted by a code object.
763
Guido van Rossum2e65f892007-02-28 22:03:49 +0000764 Three things are returned: (args, varargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000765 'args' is the list of argument names. Keyword-only arguments are
766 appended. 'varargs' and 'varkw' are the names of the * and **
767 arguments or None."""
Guido van Rossum2e65f892007-02-28 22:03:49 +0000768 args, varargs, kwonlyargs, varkw = _getfullargs(co)
Christian Heimes25bb7832008-01-11 16:17:00 +0000769 return Arguments(args + kwonlyargs, varargs, varkw)
Guido van Rossum2e65f892007-02-28 22:03:49 +0000770
771def _getfullargs(co):
772 """Get information about the arguments accepted by a code object.
773
774 Four things are returned: (args, varargs, kwonlyargs, varkw), where
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000775 'args' and 'kwonlyargs' are lists of argument names, and 'varargs'
776 and 'varkw' are the names of the * and ** arguments or None."""
Jeremy Hylton64967882003-06-27 18:14:39 +0000777
778 if not iscode(co):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000779 raise TypeError('{!r} is not a code object'.format(co))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000780
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000781 nargs = co.co_argcount
782 names = co.co_varnames
Guido van Rossum2e65f892007-02-28 22:03:49 +0000783 nkwargs = co.co_kwonlyargcount
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000784 args = list(names[:nargs])
Guido van Rossum2e65f892007-02-28 22:03:49 +0000785 kwonlyargs = list(names[nargs:nargs+nkwargs])
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000786 step = 0
787
Guido van Rossum2e65f892007-02-28 22:03:49 +0000788 nargs += nkwargs
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000789 varargs = None
790 if co.co_flags & CO_VARARGS:
791 varargs = co.co_varnames[nargs]
792 nargs = nargs + 1
793 varkw = None
794 if co.co_flags & CO_VARKEYWORDS:
795 varkw = co.co_varnames[nargs]
Guido van Rossum2e65f892007-02-28 22:03:49 +0000796 return args, varargs, kwonlyargs, varkw
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000797
Christian Heimes25bb7832008-01-11 16:17:00 +0000798
799ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
800
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000801def getargspec(func):
802 """Get the names and default values of a function's arguments.
803
804 A tuple of four things is returned: (args, varargs, varkw, defaults).
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000805 'args' is a list of the argument names.
Guido van Rossum2e65f892007-02-28 22:03:49 +0000806 'args' will include keyword-only argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000807 'varargs' and 'varkw' are the names of the * and ** arguments or None.
Jeremy Hylton64967882003-06-27 18:14:39 +0000808 'defaults' is an n-tuple of the default values of the last n arguments.
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000809
Guido van Rossum2e65f892007-02-28 22:03:49 +0000810 Use the getfullargspec() API for Python-3000 code, as annotations
811 and keyword arguments are supported. getargspec() will raise ValueError
812 if the func has either annotations or keyword arguments.
813 """
814
815 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
816 getfullargspec(func)
817 if kwonlyargs or ann:
Collin Winterce36ad82007-08-30 01:19:48 +0000818 raise ValueError("Function has keyword-only arguments or annotations"
819 ", use getfullargspec() API which can support them")
Christian Heimes25bb7832008-01-11 16:17:00 +0000820 return ArgSpec(args, varargs, varkw, defaults)
821
822FullArgSpec = namedtuple('FullArgSpec',
Benjamin Peterson3d4ca742008-11-12 21:39:01 +0000823 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
Guido van Rossum2e65f892007-02-28 22:03:49 +0000824
825def getfullargspec(func):
826 """Get the names and default values of a function's arguments.
827
Brett Cannon504d8852007-09-07 02:12:14 +0000828 A tuple of seven things is returned:
829 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults annotations).
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000830 'args' is a list of the argument names.
Guido van Rossum2e65f892007-02-28 22:03:49 +0000831 'varargs' and 'varkw' are the names of the * and ** arguments or None.
832 'defaults' is an n-tuple of the default values of the last n arguments.
833 'kwonlyargs' is a list of keyword-only argument names.
834 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
835 'annotations' is a dictionary mapping argument names to annotations.
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000836
Guido van Rossum2e65f892007-02-28 22:03:49 +0000837 The first four items in the tuple correspond to getargspec().
Jeremy Hylton64967882003-06-27 18:14:39 +0000838 """
839
840 if ismethod(func):
Christian Heimesff737952007-11-27 10:40:20 +0000841 func = func.__func__
Jeremy Hylton64967882003-06-27 18:14:39 +0000842 if not isfunction(func):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000843 raise TypeError('{!r} is not a Python function'.format(func))
Guido van Rossum2e65f892007-02-28 22:03:49 +0000844 args, varargs, kwonlyargs, varkw = _getfullargs(func.__code__)
Christian Heimes25bb7832008-01-11 16:17:00 +0000845 return FullArgSpec(args, varargs, varkw, func.__defaults__,
Guido van Rossum2e65f892007-02-28 22:03:49 +0000846 kwonlyargs, func.__kwdefaults__, func.__annotations__)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000847
Christian Heimes25bb7832008-01-11 16:17:00 +0000848ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
849
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000850def getargvalues(frame):
851 """Get information about arguments passed into a particular frame.
852
853 A tuple of four things is returned: (args, varargs, varkw, locals).
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000854 'args' is a list of the argument names.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000855 'varargs' and 'varkw' are the names of the * and ** arguments or None.
856 'locals' is the locals dictionary of the given frame."""
857 args, varargs, varkw = getargs(frame.f_code)
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000858 return ArgInfo(args, varargs, varkw, frame.f_locals)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000859
Guido van Rossum2e65f892007-02-28 22:03:49 +0000860def formatannotation(annotation, base_module=None):
861 if isinstance(annotation, type):
Georg Brandl1a3284e2007-12-02 09:40:06 +0000862 if annotation.__module__ in ('builtins', base_module):
Guido van Rossum2e65f892007-02-28 22:03:49 +0000863 return annotation.__name__
864 return annotation.__module__+'.'+annotation.__name__
865 return repr(annotation)
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000866
Guido van Rossum2e65f892007-02-28 22:03:49 +0000867def formatannotationrelativeto(object):
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000868 module = getattr(object, '__module__', None)
869 def _formatannotation(annotation):
870 return formatannotation(annotation, module)
871 return _formatannotation
Guido van Rossum2e65f892007-02-28 22:03:49 +0000872
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000873def formatargspec(args, varargs=None, varkw=None, defaults=None,
Guido van Rossum2e65f892007-02-28 22:03:49 +0000874 kwonlyargs=(), kwonlydefaults={}, annotations={},
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000875 formatarg=str,
876 formatvarargs=lambda name: '*' + name,
877 formatvarkw=lambda name: '**' + name,
878 formatvalue=lambda value: '=' + repr(value),
Guido van Rossum2e65f892007-02-28 22:03:49 +0000879 formatreturns=lambda text: ' -> ' + text,
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000880 formatannotation=formatannotation):
Guido van Rossuma8add0e2007-05-14 22:03:55 +0000881 """Format an argument spec from the values returned by getargspec
Guido van Rossum2e65f892007-02-28 22:03:49 +0000882 or getfullargspec.
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000883
Guido van Rossum2e65f892007-02-28 22:03:49 +0000884 The first seven arguments are (args, varargs, varkw, defaults,
885 kwonlyargs, kwonlydefaults, annotations). The other five arguments
886 are the corresponding optional formatting functions that are called to
887 turn names and values into strings. The last argument is an optional
888 function to format the sequence of arguments."""
889 def formatargandannotation(arg):
890 result = formatarg(arg)
891 if arg in annotations:
892 result += ': ' + formatannotation(annotations[arg])
893 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000894 specs = []
895 if defaults:
896 firstdefault = len(args) - len(defaults)
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000897 for i, arg in enumerate(args):
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000898 spec = formatargandannotation(arg)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000899 if defaults and i >= firstdefault:
900 spec = spec + formatvalue(defaults[i - firstdefault])
901 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +0000902 if varargs is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +0000903 specs.append(formatvarargs(formatargandannotation(varargs)))
904 else:
905 if kwonlyargs:
906 specs.append('*')
907 if kwonlyargs:
908 for kwonlyarg in kwonlyargs:
909 spec = formatargandannotation(kwonlyarg)
Benjamin Peterson9953a8d2009-01-17 04:15:01 +0000910 if kwonlydefaults and kwonlyarg in kwonlydefaults:
Guido van Rossum2e65f892007-02-28 22:03:49 +0000911 spec += formatvalue(kwonlydefaults[kwonlyarg])
912 specs.append(spec)
Raymond Hettinger936654b2002-06-01 03:06:31 +0000913 if varkw is not None:
Guido van Rossum2e65f892007-02-28 22:03:49 +0000914 specs.append(formatvarkw(formatargandannotation(varkw)))
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000915 result = '(' + ', '.join(specs) + ')'
Guido van Rossum2e65f892007-02-28 22:03:49 +0000916 if 'return' in annotations:
917 result += formatreturns(formatannotation(annotations['return']))
918 return result
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000919
920def formatargvalues(args, varargs, varkw, locals,
921 formatarg=str,
922 formatvarargs=lambda name: '*' + name,
923 formatvarkw=lambda name: '**' + name,
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000924 formatvalue=lambda value: '=' + repr(value)):
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000925 """Format an argument spec from the 4 values returned by getargvalues.
926
927 The first four arguments are (args, varargs, varkw, locals). The
928 next four arguments are the corresponding optional formatting functions
929 that are called to turn names and values into strings. The ninth
930 argument is an optional function to format the sequence of arguments."""
931 def convert(name, locals=locals,
932 formatarg=formatarg, formatvalue=formatvalue):
933 return formatarg(name) + formatvalue(locals[name])
934 specs = []
935 for i in range(len(args)):
Georg Brandlc1c4bf82010-10-15 16:07:41 +0000936 specs.append(convert(args[i]))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000937 if varargs:
938 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
939 if varkw:
940 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
Neal Norwitz9d72bb42007-04-17 08:48:32 +0000941 return '(' + ', '.join(specs) + ')'
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +0000942
Benjamin Petersone109c702011-06-24 09:37:26 -0500943def _missing_arguments(f_name, argnames, pos, values):
944 names = [repr(name) for name in argnames if name not in values]
945 missing = len(names)
946 if missing == 1:
947 s = names[0]
948 elif missing == 2:
949 s = "{} and {}".format(*names)
950 else:
951 tail = ", {} and {}".format(names[-2:])
952 del names[-2:]
953 s = ", ".join(names) + tail
954 raise TypeError("%s() missing %i required %s argument%s: %s" %
955 (f_name, missing,
956 "positional" if pos else "keyword-only",
957 "" if missing == 1 else "s", s))
958
959def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
Benjamin Petersonb204a422011-06-05 22:04:07 -0500960 atleast = len(args) - defcount
Benjamin Petersonb204a422011-06-05 22:04:07 -0500961 kwonly_given = len([arg for arg in kwonly if arg in values])
962 if varargs:
963 plural = atleast != 1
964 sig = "at least %d" % (atleast,)
965 elif defcount:
966 plural = True
967 sig = "from %d to %d" % (atleast, len(args))
968 else:
969 plural = len(args) != 1
970 sig = str(len(args))
971 kwonly_sig = ""
972 if kwonly_given:
973 msg = " positional argument%s (and %d keyword-only argument%s)"
974 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
975 "s" if kwonly_given != 1 else ""))
976 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
977 (f_name, sig, "s" if plural else "", given, kwonly_sig,
978 "was" if given == 1 and not kwonly_given else "were"))
979
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +0000980def getcallargs(func, *positional, **named):
981 """Get the mapping of arguments to values.
982
983 A dict is returned, with keys the function argument names (including the
984 names of the * and ** arguments, if any), and values the respective bound
985 values from 'positional' and 'named'."""
986 spec = getfullargspec(func)
987 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
988 f_name = func.__name__
989 arg2value = {}
990
Benjamin Petersonb204a422011-06-05 22:04:07 -0500991
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +0000992 if ismethod(func) and func.__self__ is not None:
993 # implicit 'self' (or 'cls' for classmethods) argument
994 positional = (func.__self__,) + positional
995 num_pos = len(positional)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +0000996 num_args = len(args)
997 num_defaults = len(defaults) if defaults else 0
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +0000998
Benjamin Petersonb204a422011-06-05 22:04:07 -0500999 n = min(num_pos, num_args)
1000 for i in range(n):
1001 arg2value[args[i]] = positional[i]
1002 if varargs:
1003 arg2value[varargs] = tuple(positional[n:])
1004 possible_kwargs = set(args + kwonlyargs)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001005 if varkw:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001006 arg2value[varkw] = {}
1007 for kw, value in named.items():
1008 if kw not in possible_kwargs:
1009 if not varkw:
1010 raise TypeError("%s() got an unexpected keyword argument %r" %
1011 (f_name, kw))
1012 arg2value[varkw][kw] = value
1013 continue
1014 if kw in arg2value:
1015 raise TypeError("%s() got multiple values for argument %r" %
1016 (f_name, kw))
1017 arg2value[kw] = value
1018 if num_pos > num_args and not varargs:
Benjamin Petersone109c702011-06-24 09:37:26 -05001019 _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
1020 num_pos, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001021 if num_pos < num_args:
Benjamin Petersone109c702011-06-24 09:37:26 -05001022 req = args[:num_args - num_defaults]
1023 for arg in req:
Benjamin Petersonb204a422011-06-05 22:04:07 -05001024 if arg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001025 _missing_arguments(f_name, req, True, arg2value)
Benjamin Petersonb204a422011-06-05 22:04:07 -05001026 for i, arg in enumerate(args[num_args - num_defaults:]):
1027 if arg not in arg2value:
1028 arg2value[arg] = defaults[i]
Benjamin Petersone109c702011-06-24 09:37:26 -05001029 missing = 0
Benjamin Petersonb204a422011-06-05 22:04:07 -05001030 for kwarg in kwonlyargs:
1031 if kwarg not in arg2value:
Benjamin Petersone109c702011-06-24 09:37:26 -05001032 if kwarg in kwonlydefaults:
1033 arg2value[kwarg] = kwonlydefaults[kwarg]
1034 else:
1035 missing += 1
1036 if missing:
1037 _missing_arguments(f_name, kwonlyargs, False, arg2value)
Benjamin Peterson25cd7eb2010-03-30 18:42:32 +00001038 return arg2value
1039
Nick Coghlan2f92e542012-06-23 19:39:55 +10001040ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
1041
1042def getclosurevars(func):
1043 """
1044 Get the mapping of free variables to their current values.
1045
1046 Returns a named tuple of dics mapping the current nonlocal, global
1047 and builtin references as seen by the body of the function. A final
1048 set of unbound names that could not be resolved is also provided.
1049 """
1050
1051 if ismethod(func):
1052 func = func.__func__
1053
1054 if not isfunction(func):
1055 raise TypeError("'{!r}' is not a Python function".format(func))
1056
1057 code = func.__code__
1058 # Nonlocal references are named in co_freevars and resolved
1059 # by looking them up in __closure__ by positional index
1060 if func.__closure__ is None:
1061 nonlocal_vars = {}
1062 else:
1063 nonlocal_vars = {
1064 var : cell.cell_contents
1065 for var, cell in zip(code.co_freevars, func.__closure__)
1066 }
1067
1068 # Global and builtin references are named in co_names and resolved
1069 # by looking them up in __globals__ or __builtins__
1070 global_ns = func.__globals__
1071 builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
1072 if ismodule(builtin_ns):
1073 builtin_ns = builtin_ns.__dict__
1074 global_vars = {}
1075 builtin_vars = {}
1076 unbound_names = set()
1077 for name in code.co_names:
1078 if name in ("None", "True", "False"):
1079 # Because these used to be builtins instead of keywords, they
1080 # may still show up as name references. We ignore them.
1081 continue
1082 try:
1083 global_vars[name] = global_ns[name]
1084 except KeyError:
1085 try:
1086 builtin_vars[name] = builtin_ns[name]
1087 except KeyError:
1088 unbound_names.add(name)
1089
1090 return ClosureVars(nonlocal_vars, global_vars,
1091 builtin_vars, unbound_names)
1092
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001093# -------------------------------------------------- stack frame extraction
Christian Heimes25bb7832008-01-11 16:17:00 +00001094
1095Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
1096
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001097def getframeinfo(frame, context=1):
1098 """Get information about a frame or traceback object.
1099
1100 A tuple of five things is returned: the filename, the line number of
1101 the current line, the function name, a list of lines of context from
1102 the source code, and the index of the current line within that list.
1103 The optional second argument specifies the number of lines of context
1104 to return, which are centered around the current line."""
1105 if istraceback(frame):
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001106 lineno = frame.tb_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001107 frame = frame.tb_frame
Andrew M. Kuchlingba8b6bc2004-06-05 14:11:59 +00001108 else:
1109 lineno = frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001110 if not isframe(frame):
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +00001111 raise TypeError('{!r} is not a frame or traceback object'.format(frame))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001112
Neil Schemenauerf06f8532002-03-23 23:51:04 +00001113 filename = getsourcefile(frame) or getfile(frame)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001114 if context > 0:
Guido van Rossum54e54c62001-09-04 19:14:14 +00001115 start = lineno - 1 - context//2
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001116 try:
1117 lines, lnum = findsource(frame)
Ka-Ping Yee4eb0c002001-03-02 05:50:34 +00001118 except IOError:
1119 lines = index = None
1120 else:
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001121 start = max(start, 1)
Raymond Hettingera0501712004-06-15 11:22:53 +00001122 start = max(0, min(start, len(lines) - context))
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001123 lines = lines[start:start+context]
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001124 index = lineno - 1 - start
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001125 else:
1126 lines = index = None
1127
Christian Heimes25bb7832008-01-11 16:17:00 +00001128 return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
Ka-Ping Yee59ade082001-03-01 03:55:35 +00001129
1130def getlineno(frame):
1131 """Get the line number from a frame object, allowing for optimization."""
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001132 # FrameType.f_lineno is now a descriptor that grovels co_lnotab
1133 return frame.f_lineno
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001134
1135def getouterframes(frame, context=1):
1136 """Get a list of records for a frame and all higher (calling) frames.
1137
1138 Each record contains a frame object, filename, line number, function
1139 name, a list of lines of context, and index within the context."""
1140 framelist = []
1141 while frame:
1142 framelist.append((frame,) + getframeinfo(frame, context))
1143 frame = frame.f_back
1144 return framelist
1145
1146def getinnerframes(tb, context=1):
1147 """Get a list of records for a traceback's frame and all lower frames.
1148
1149 Each record contains a frame object, filename, line number, function
1150 name, a list of lines of context, and index within the context."""
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001151 framelist = []
1152 while tb:
1153 framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
1154 tb = tb.tb_next
1155 return framelist
1156
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001157def currentframe():
Benjamin Petersona3a3fc62010-08-09 15:49:56 +00001158 """Return the frame of the caller or None if this is not possible."""
Benjamin Peterson42ac4752010-08-09 13:05:35 +00001159 return sys._getframe(1) if hasattr(sys, "_getframe") else None
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001160
1161def stack(context=1):
1162 """Return a list of records for the stack above the caller's frame."""
Jeremy Hyltonab919022003-06-27 18:41:20 +00001163 return getouterframes(sys._getframe(1), context)
Ka-Ping Yee6397c7c2001-02-27 14:43:21 +00001164
1165def trace(context=1):
Tim Peters85ba6732001-02-28 08:26:44 +00001166 """Return a list of records for the stack below the current exception."""
Fred Draked451ec12002-04-26 02:29:55 +00001167 return getinnerframes(sys.exc_info()[2], context)
Michael Foord95fc51d2010-11-20 15:07:30 +00001168
1169
1170# ------------------------------------------------ static version of getattr
1171
1172_sentinel = object()
1173
Michael Foorde5162652010-11-20 16:40:44 +00001174def _static_getmro(klass):
1175 return type.__dict__['__mro__'].__get__(klass)
1176
Michael Foord95fc51d2010-11-20 15:07:30 +00001177def _check_instance(obj, attr):
1178 instance_dict = {}
1179 try:
1180 instance_dict = object.__getattribute__(obj, "__dict__")
1181 except AttributeError:
1182 pass
Michael Foorddcebe0f2011-03-15 19:20:44 -04001183 return dict.get(instance_dict, attr, _sentinel)
Michael Foord95fc51d2010-11-20 15:07:30 +00001184
1185
1186def _check_class(klass, attr):
Michael Foorde5162652010-11-20 16:40:44 +00001187 for entry in _static_getmro(klass):
Michael Foorda51623b2011-12-18 22:01:40 +00001188 if _shadowed_dict(type(entry)) is _sentinel:
Michael Foorddcebe0f2011-03-15 19:20:44 -04001189 try:
1190 return entry.__dict__[attr]
1191 except KeyError:
1192 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001193 return _sentinel
1194
Michael Foord35184ed2010-11-20 16:58:30 +00001195def _is_type(obj):
1196 try:
1197 _static_getmro(obj)
1198 except TypeError:
1199 return False
1200 return True
1201
Michael Foorddcebe0f2011-03-15 19:20:44 -04001202def _shadowed_dict(klass):
1203 dict_attr = type.__dict__["__dict__"]
1204 for entry in _static_getmro(klass):
1205 try:
1206 class_dict = dict_attr.__get__(entry)["__dict__"]
1207 except KeyError:
1208 pass
1209 else:
1210 if not (type(class_dict) is types.GetSetDescriptorType and
1211 class_dict.__name__ == "__dict__" and
1212 class_dict.__objclass__ is entry):
Michael Foorda51623b2011-12-18 22:01:40 +00001213 return class_dict
1214 return _sentinel
Michael Foord95fc51d2010-11-20 15:07:30 +00001215
1216def getattr_static(obj, attr, default=_sentinel):
1217 """Retrieve attributes without triggering dynamic lookup via the
1218 descriptor protocol, __getattr__ or __getattribute__.
1219
1220 Note: this function may not be able to retrieve all attributes
1221 that getattr can fetch (like dynamically created attributes)
1222 and may find attributes that getattr can't (like descriptors
1223 that raise AttributeError). It can also return descriptor objects
1224 instead of instance members in some cases. See the
1225 documentation for details.
1226 """
1227 instance_result = _sentinel
Michael Foord35184ed2010-11-20 16:58:30 +00001228 if not _is_type(obj):
Michael Foordcc7ebb82010-11-20 16:20:16 +00001229 klass = type(obj)
Michael Foorda51623b2011-12-18 22:01:40 +00001230 dict_attr = _shadowed_dict(klass)
1231 if (dict_attr is _sentinel or
1232 type(dict_attr) is types.MemberDescriptorType):
Michael Foorddcebe0f2011-03-15 19:20:44 -04001233 instance_result = _check_instance(obj, attr)
Michael Foord95fc51d2010-11-20 15:07:30 +00001234 else:
1235 klass = obj
1236
1237 klass_result = _check_class(klass, attr)
1238
1239 if instance_result is not _sentinel and klass_result is not _sentinel:
1240 if (_check_class(type(klass_result), '__get__') is not _sentinel and
1241 _check_class(type(klass_result), '__set__') is not _sentinel):
1242 return klass_result
1243
1244 if instance_result is not _sentinel:
1245 return instance_result
1246 if klass_result is not _sentinel:
1247 return klass_result
1248
1249 if obj is klass:
1250 # for types we check the metaclass too
Michael Foorde5162652010-11-20 16:40:44 +00001251 for entry in _static_getmro(type(klass)):
Michael Foord3ba95f82011-12-22 01:13:37 +00001252 if _shadowed_dict(type(entry)) is _sentinel:
1253 try:
1254 return entry.__dict__[attr]
1255 except KeyError:
1256 pass
Michael Foord95fc51d2010-11-20 15:07:30 +00001257 if default is not _sentinel:
1258 return default
1259 raise AttributeError(attr)
Nick Coghlane0f04652010-11-21 03:44:04 +00001260
1261
Nick Coghlan7921b9f2010-11-30 06:36:04 +00001262GEN_CREATED = 'GEN_CREATED'
1263GEN_RUNNING = 'GEN_RUNNING'
1264GEN_SUSPENDED = 'GEN_SUSPENDED'
1265GEN_CLOSED = 'GEN_CLOSED'
Nick Coghlane0f04652010-11-21 03:44:04 +00001266
1267def getgeneratorstate(generator):
1268 """Get current state of a generator-iterator.
1269
1270 Possible states are:
1271 GEN_CREATED: Waiting to start execution.
1272 GEN_RUNNING: Currently being executed by the interpreter.
1273 GEN_SUSPENDED: Currently suspended at a yield expression.
1274 GEN_CLOSED: Execution has completed.
1275 """
1276 if generator.gi_running:
1277 return GEN_RUNNING
1278 if generator.gi_frame is None:
1279 return GEN_CLOSED
1280 if generator.gi_frame.f_lasti == -1:
1281 return GEN_CREATED
1282 return GEN_SUSPENDED
Larry Hastings7c7cbfc2012-06-22 15:19:35 -07001283
1284
1285###############################################################################
1286### Function Signature Object (PEP 362)
1287###############################################################################
1288
1289
1290_WrapperDescriptor = type(type.__call__)
1291_MethodWrapper = type(all.__call__)
1292
1293_NonUserDefinedCallables = (_WrapperDescriptor,
1294 _MethodWrapper,
1295 types.BuiltinFunctionType)
1296
1297
1298def _get_user_defined_method(cls, method_name):
1299 try:
1300 meth = getattr(cls, method_name)
1301 except AttributeError:
1302 return
1303 else:
1304 if not isinstance(meth, _NonUserDefinedCallables):
1305 # Once '__signature__' will be added to 'C'-level
1306 # callables, this check won't be necessary
1307 return meth
1308
1309
1310def signature(obj):
1311 '''Get a signature object for the passed callable.'''
1312
1313 if not callable(obj):
1314 raise TypeError('{!r} is not a callable object'.format(obj))
1315
1316 if isinstance(obj, types.MethodType):
1317 # In this case we skip the first parameter of the underlying
1318 # function (usually `self` or `cls`).
1319 sig = signature(obj.__func__)
1320 return sig.replace(parameters=tuple(sig.parameters.values())[1:])
1321
1322 try:
1323 sig = obj.__signature__
1324 except AttributeError:
1325 pass
1326 else:
1327 if sig is not None:
1328 return sig
1329
1330 try:
1331 # Was this function wrapped by a decorator?
1332 wrapped = obj.__wrapped__
1333 except AttributeError:
1334 pass
1335 else:
1336 return signature(wrapped)
1337
1338 if isinstance(obj, types.FunctionType):
1339 return Signature.from_function(obj)
1340
1341 if isinstance(obj, functools.partial):
1342 sig = signature(obj.func)
1343
1344 new_params = OrderedDict(sig.parameters.items())
1345
1346 partial_args = obj.args or ()
1347 partial_keywords = obj.keywords or {}
1348 try:
1349 ba = sig.bind_partial(*partial_args, **partial_keywords)
1350 except TypeError as ex:
1351 msg = 'partial object {!r} has incorrect arguments'.format(obj)
1352 raise ValueError(msg) from ex
1353
1354 for arg_name, arg_value in ba.arguments.items():
1355 param = new_params[arg_name]
1356 if arg_name in partial_keywords:
1357 # We set a new default value, because the following code
1358 # is correct:
1359 #
1360 # >>> def foo(a): print(a)
1361 # >>> print(partial(partial(foo, a=10), a=20)())
1362 # 20
1363 # >>> print(partial(partial(foo, a=10), a=20)(a=30))
1364 # 30
1365 #
1366 # So, with 'partial' objects, passing a keyword argument is
1367 # like setting a new default value for the corresponding
1368 # parameter
1369 #
1370 # We also mark this parameter with '_partial_kwarg'
1371 # flag. Later, in '_bind', the 'default' value of this
1372 # parameter will be added to 'kwargs', to simulate
1373 # the 'functools.partial' real call.
1374 new_params[arg_name] = param.replace(default=arg_value,
1375 _partial_kwarg=True)
1376
1377 elif (param.kind not in (_VAR_KEYWORD, _VAR_POSITIONAL) and
1378 not param._partial_kwarg):
1379 new_params.pop(arg_name)
1380
1381 return sig.replace(parameters=new_params.values())
1382
1383 sig = None
1384 if isinstance(obj, type):
1385 # obj is a class or a metaclass
1386
1387 # First, let's see if it has an overloaded __call__ defined
1388 # in its metaclass
1389 call = _get_user_defined_method(type(obj), '__call__')
1390 if call is not None:
1391 sig = signature(call)
1392 else:
1393 # Now we check if the 'obj' class has a '__new__' method
1394 new = _get_user_defined_method(obj, '__new__')
1395 if new is not None:
1396 sig = signature(new)
1397 else:
1398 # Finally, we should have at least __init__ implemented
1399 init = _get_user_defined_method(obj, '__init__')
1400 if init is not None:
1401 sig = signature(init)
1402 elif not isinstance(obj, _NonUserDefinedCallables):
1403 # An object with __call__
1404 # We also check that the 'obj' is not an instance of
1405 # _WrapperDescriptor or _MethodWrapper to avoid
1406 # infinite recursion (and even potential segfault)
1407 call = _get_user_defined_method(type(obj), '__call__')
1408 if call is not None:
1409 sig = signature(call)
1410
1411 if sig is not None:
1412 # For classes and objects we skip the first parameter of their
1413 # __call__, __new__, or __init__ methods
1414 return sig.replace(parameters=tuple(sig.parameters.values())[1:])
1415
1416 if isinstance(obj, types.BuiltinFunctionType):
1417 # Raise a nicer error message for builtins
1418 msg = 'no signature found for builtin function {!r}'.format(obj)
1419 raise ValueError(msg)
1420
1421 raise ValueError('callable {!r} is not supported by signature'.format(obj))
1422
1423
1424class _void:
1425 '''A private marker - used in Parameter & Signature'''
1426
1427
1428class _empty:
1429 pass
1430
1431
1432class _ParameterKind(int):
1433 def __new__(self, *args, name):
1434 obj = int.__new__(self, *args)
1435 obj._name = name
1436 return obj
1437
1438 def __str__(self):
1439 return self._name
1440
1441 def __repr__(self):
1442 return '<_ParameterKind: {!r}>'.format(self._name)
1443
1444
1445_POSITIONAL_ONLY = _ParameterKind(0, name='POSITIONAL_ONLY')
1446_POSITIONAL_OR_KEYWORD = _ParameterKind(1, name='POSITIONAL_OR_KEYWORD')
1447_VAR_POSITIONAL = _ParameterKind(2, name='VAR_POSITIONAL')
1448_KEYWORD_ONLY = _ParameterKind(3, name='KEYWORD_ONLY')
1449_VAR_KEYWORD = _ParameterKind(4, name='VAR_KEYWORD')
1450
1451
1452class Parameter:
1453 '''Represents a parameter in a function signature.
1454
1455 Has the following public attributes:
1456
1457 * name : str
1458 The name of the parameter as a string.
1459 * default : object
1460 The default value for the parameter if specified. If the
1461 parameter has no default value, this attribute is not set.
1462 * annotation
1463 The annotation for the parameter if specified. If the
1464 parameter has no annotation, this attribute is not set.
1465 * kind : str
1466 Describes how argument values are bound to the parameter.
1467 Possible values: `Parameter.POSITIONAL_ONLY`,
1468 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
1469 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
1470 '''
1471
1472 __slots__ = ('_name', '_kind', '_default', '_annotation', '_partial_kwarg')
1473
1474 POSITIONAL_ONLY = _POSITIONAL_ONLY
1475 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
1476 VAR_POSITIONAL = _VAR_POSITIONAL
1477 KEYWORD_ONLY = _KEYWORD_ONLY
1478 VAR_KEYWORD = _VAR_KEYWORD
1479
1480 empty = _empty
1481
1482 def __init__(self, name, kind, *, default=_empty, annotation=_empty,
1483 _partial_kwarg=False):
1484
1485 if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
1486 _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
1487 raise ValueError("invalid value for 'Parameter.kind' attribute")
1488 self._kind = kind
1489
1490 if default is not _empty:
1491 if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
1492 msg = '{} parameters cannot have default values'.format(kind)
1493 raise ValueError(msg)
1494 self._default = default
1495 self._annotation = annotation
1496
1497 if name is None:
1498 if kind != _POSITIONAL_ONLY:
1499 raise ValueError("None is not a valid name for a "
1500 "non-positional-only parameter")
1501 self._name = name
1502 else:
1503 name = str(name)
1504 if kind != _POSITIONAL_ONLY and not name.isidentifier():
1505 msg = '{!r} is not a valid parameter name'.format(name)
1506 raise ValueError(msg)
1507 self._name = name
1508
1509 self._partial_kwarg = _partial_kwarg
1510
1511 @property
1512 def name(self):
1513 return self._name
1514
1515 @property
1516 def default(self):
1517 return self._default
1518
1519 @property
1520 def annotation(self):
1521 return self._annotation
1522
1523 @property
1524 def kind(self):
1525 return self._kind
1526
1527 def replace(self, *, name=_void, kind=_void, annotation=_void,
1528 default=_void, _partial_kwarg=_void):
1529 '''Creates a customized copy of the Parameter.'''
1530
1531 if name is _void:
1532 name = self._name
1533
1534 if kind is _void:
1535 kind = self._kind
1536
1537 if annotation is _void:
1538 annotation = self._annotation
1539
1540 if default is _void:
1541 default = self._default
1542
1543 if _partial_kwarg is _void:
1544 _partial_kwarg = self._partial_kwarg
1545
1546 return type(self)(name, kind, default=default, annotation=annotation,
1547 _partial_kwarg=_partial_kwarg)
1548
1549 def __str__(self):
1550 kind = self.kind
1551
1552 formatted = self._name
1553 if kind == _POSITIONAL_ONLY:
1554 if formatted is None:
1555 formatted = ''
1556 formatted = '<{}>'.format(formatted)
1557
1558 # Add annotation and default value
1559 if self._annotation is not _empty:
1560 formatted = '{}:{}'.format(formatted,
1561 formatannotation(self._annotation))
1562
1563 if self._default is not _empty:
1564 formatted = '{}={}'.format(formatted, repr(self._default))
1565
1566 if kind == _VAR_POSITIONAL:
1567 formatted = '*' + formatted
1568 elif kind == _VAR_KEYWORD:
1569 formatted = '**' + formatted
1570
1571 return formatted
1572
1573 def __repr__(self):
1574 return '<{} at {:#x} {!r}>'.format(self.__class__.__name__,
1575 id(self), self.name)
1576
1577 def __eq__(self, other):
1578 return (issubclass(other.__class__, Parameter) and
1579 self._name == other._name and
1580 self._kind == other._kind and
1581 self._default == other._default and
1582 self._annotation == other._annotation)
1583
1584 def __ne__(self, other):
1585 return not self.__eq__(other)
1586
1587
1588class BoundArguments:
1589 '''Result of `Signature.bind` call. Holds the mapping of arguments
1590 to the function's parameters.
1591
1592 Has the following public attributes:
1593
1594 * arguments : OrderedDict
1595 An ordered mutable mapping of parameters' names to arguments' values.
1596 Does not contain arguments' default values.
1597 * signature : Signature
1598 The Signature object that created this instance.
1599 * args : tuple
1600 Tuple of positional arguments values.
1601 * kwargs : dict
1602 Dict of keyword arguments values.
1603 '''
1604
1605 def __init__(self, signature, arguments):
1606 self.arguments = arguments
1607 self._signature = signature
1608
1609 @property
1610 def signature(self):
1611 return self._signature
1612
1613 @property
1614 def args(self):
1615 args = []
1616 for param_name, param in self._signature.parameters.items():
1617 if (param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY) or
1618 param._partial_kwarg):
1619 # Keyword arguments mapped by 'functools.partial'
1620 # (Parameter._partial_kwarg is True) are mapped
1621 # in 'BoundArguments.kwargs', along with VAR_KEYWORD &
1622 # KEYWORD_ONLY
1623 break
1624
1625 try:
1626 arg = self.arguments[param_name]
1627 except KeyError:
1628 # We're done here. Other arguments
1629 # will be mapped in 'BoundArguments.kwargs'
1630 break
1631 else:
1632 if param.kind == _VAR_POSITIONAL:
1633 # *args
1634 args.extend(arg)
1635 else:
1636 # plain argument
1637 args.append(arg)
1638
1639 return tuple(args)
1640
1641 @property
1642 def kwargs(self):
1643 kwargs = {}
1644 kwargs_started = False
1645 for param_name, param in self._signature.parameters.items():
1646 if not kwargs_started:
1647 if (param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY) or
1648 param._partial_kwarg):
1649 kwargs_started = True
1650 else:
1651 if param_name not in self.arguments:
1652 kwargs_started = True
1653 continue
1654
1655 if not kwargs_started:
1656 continue
1657
1658 try:
1659 arg = self.arguments[param_name]
1660 except KeyError:
1661 pass
1662 else:
1663 if param.kind == _VAR_KEYWORD:
1664 # **kwargs
1665 kwargs.update(arg)
1666 else:
1667 # plain keyword argument
1668 kwargs[param_name] = arg
1669
1670 return kwargs
1671
1672 def __eq__(self, other):
1673 return (issubclass(other.__class__, BoundArguments) and
1674 self.signature == other.signature and
1675 self.arguments == other.arguments)
1676
1677 def __ne__(self, other):
1678 return not self.__eq__(other)
1679
1680
1681class Signature:
1682 '''A Signature object represents the overall signature of a function.
1683 It stores a Parameter object for each parameter accepted by the
1684 function, as well as information specific to the function itself.
1685
1686 A Signature object has the following public attributes and methods:
1687
1688 * parameters : OrderedDict
1689 An ordered mapping of parameters' names to the corresponding
1690 Parameter objects (keyword-only arguments are in the same order
1691 as listed in `code.co_varnames`).
1692 * return_annotation : object
1693 The annotation for the return type of the function if specified.
1694 If the function has no annotation for its return type, this
1695 attribute is not set.
1696 * bind(*args, **kwargs) -> BoundArguments
1697 Creates a mapping from positional and keyword arguments to
1698 parameters.
1699 * bind_partial(*args, **kwargs) -> BoundArguments
1700 Creates a partial mapping from positional and keyword arguments
1701 to parameters (simulating 'functools.partial' behavior.)
1702 '''
1703
1704 __slots__ = ('_return_annotation', '_parameters')
1705
1706 _parameter_cls = Parameter
1707 _bound_arguments_cls = BoundArguments
1708
1709 empty = _empty
1710
1711 def __init__(self, parameters=None, *, return_annotation=_empty,
1712 __validate_parameters__=True):
1713 '''Constructs Signature from the given list of Parameter
1714 objects and 'return_annotation'. All arguments are optional.
1715 '''
1716
1717 if parameters is None:
1718 params = OrderedDict()
1719 else:
1720 if __validate_parameters__:
1721 params = OrderedDict()
1722 top_kind = _POSITIONAL_ONLY
1723
1724 for idx, param in enumerate(parameters):
1725 kind = param.kind
1726 if kind < top_kind:
1727 msg = 'wrong parameter order: {} before {}'
1728 msg = msg.format(top_kind, param.kind)
1729 raise ValueError(msg)
1730 else:
1731 top_kind = kind
1732
1733 name = param.name
1734 if name is None:
1735 name = str(idx)
1736 param = param.replace(name=name)
1737
1738 if name in params:
1739 msg = 'duplicate parameter name: {!r}'.format(name)
1740 raise ValueError(msg)
1741 params[name] = param
1742 else:
1743 params = OrderedDict(((param.name, param)
1744 for param in parameters))
1745
1746 self._parameters = types.MappingProxyType(params)
1747 self._return_annotation = return_annotation
1748
1749 @classmethod
1750 def from_function(cls, func):
1751 '''Constructs Signature for the given python function'''
1752
1753 if not isinstance(func, types.FunctionType):
1754 raise TypeError('{!r} is not a Python function'.format(func))
1755
1756 Parameter = cls._parameter_cls
1757
1758 # Parameter information.
1759 func_code = func.__code__
1760 pos_count = func_code.co_argcount
1761 arg_names = func_code.co_varnames
1762 positional = tuple(arg_names[:pos_count])
1763 keyword_only_count = func_code.co_kwonlyargcount
1764 keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)]
1765 annotations = func.__annotations__
1766 defaults = func.__defaults__
1767 kwdefaults = func.__kwdefaults__
1768
1769 if defaults:
1770 pos_default_count = len(defaults)
1771 else:
1772 pos_default_count = 0
1773
1774 parameters = []
1775
1776 # Non-keyword-only parameters w/o defaults.
1777 non_default_count = pos_count - pos_default_count
1778 for name in positional[:non_default_count]:
1779 annotation = annotations.get(name, _empty)
1780 parameters.append(Parameter(name, annotation=annotation,
1781 kind=_POSITIONAL_OR_KEYWORD))
1782
1783 # ... w/ defaults.
1784 for offset, name in enumerate(positional[non_default_count:]):
1785 annotation = annotations.get(name, _empty)
1786 parameters.append(Parameter(name, annotation=annotation,
1787 kind=_POSITIONAL_OR_KEYWORD,
1788 default=defaults[offset]))
1789
1790 # *args
1791 if func_code.co_flags & 0x04:
1792 name = arg_names[pos_count + keyword_only_count]
1793 annotation = annotations.get(name, _empty)
1794 parameters.append(Parameter(name, annotation=annotation,
1795 kind=_VAR_POSITIONAL))
1796
1797 # Keyword-only parameters.
1798 for name in keyword_only:
1799 default = _empty
1800 if kwdefaults is not None:
1801 default = kwdefaults.get(name, _empty)
1802
1803 annotation = annotations.get(name, _empty)
1804 parameters.append(Parameter(name, annotation=annotation,
1805 kind=_KEYWORD_ONLY,
1806 default=default))
1807 # **kwargs
1808 if func_code.co_flags & 0x08:
1809 index = pos_count + keyword_only_count
1810 if func_code.co_flags & 0x04:
1811 index += 1
1812
1813 name = arg_names[index]
1814 annotation = annotations.get(name, _empty)
1815 parameters.append(Parameter(name, annotation=annotation,
1816 kind=_VAR_KEYWORD))
1817
1818 return cls(parameters,
1819 return_annotation=annotations.get('return', _empty),
1820 __validate_parameters__=False)
1821
1822 @property
1823 def parameters(self):
1824 return self._parameters
1825
1826 @property
1827 def return_annotation(self):
1828 return self._return_annotation
1829
1830 def replace(self, *, parameters=_void, return_annotation=_void):
1831 '''Creates a customized copy of the Signature.
1832 Pass 'parameters' and/or 'return_annotation' arguments
1833 to override them in the new copy.
1834 '''
1835
1836 if parameters is _void:
1837 parameters = self.parameters.values()
1838
1839 if return_annotation is _void:
1840 return_annotation = self._return_annotation
1841
1842 return type(self)(parameters,
1843 return_annotation=return_annotation)
1844
1845 def __eq__(self, other):
1846 if (not issubclass(type(other), Signature) or
1847 self.return_annotation != other.return_annotation or
1848 len(self.parameters) != len(other.parameters)):
1849 return False
1850
1851 other_positions = {param: idx
1852 for idx, param in enumerate(other.parameters.keys())}
1853
1854 for idx, (param_name, param) in enumerate(self.parameters.items()):
1855 if param.kind == _KEYWORD_ONLY:
1856 try:
1857 other_param = other.parameters[param_name]
1858 except KeyError:
1859 return False
1860 else:
1861 if param != other_param:
1862 return False
1863 else:
1864 try:
1865 other_idx = other_positions[param_name]
1866 except KeyError:
1867 return False
1868 else:
1869 if (idx != other_idx or
1870 param != other.parameters[param_name]):
1871 return False
1872
1873 return True
1874
1875 def __ne__(self, other):
1876 return not self.__eq__(other)
1877
1878 def _bind(self, args, kwargs, *, partial=False):
1879 '''Private method. Don't use directly.'''
1880
1881 arguments = OrderedDict()
1882
1883 parameters = iter(self.parameters.values())
1884 parameters_ex = ()
1885 arg_vals = iter(args)
1886
1887 if partial:
1888 # Support for binding arguments to 'functools.partial' objects.
1889 # See 'functools.partial' case in 'signature()' implementation
1890 # for details.
1891 for param_name, param in self.parameters.items():
1892 if (param._partial_kwarg and param_name not in kwargs):
1893 # Simulating 'functools.partial' behavior
1894 kwargs[param_name] = param.default
1895
1896 while True:
1897 # Let's iterate through the positional arguments and corresponding
1898 # parameters
1899 try:
1900 arg_val = next(arg_vals)
1901 except StopIteration:
1902 # No more positional arguments
1903 try:
1904 param = next(parameters)
1905 except StopIteration:
1906 # No more parameters. That's it. Just need to check that
1907 # we have no `kwargs` after this while loop
1908 break
1909 else:
1910 if param.kind == _VAR_POSITIONAL:
1911 # That's OK, just empty *args. Let's start parsing
1912 # kwargs
1913 break
1914 elif param.name in kwargs:
1915 if param.kind == _POSITIONAL_ONLY:
1916 msg = '{arg!r} parameter is positional only, ' \
1917 'but was passed as a keyword'
1918 msg = msg.format(arg=param.name)
1919 raise TypeError(msg) from None
1920 parameters_ex = (param,)
1921 break
1922 elif (param.kind == _VAR_KEYWORD or
1923 param.default is not _empty):
1924 # That's fine too - we have a default value for this
1925 # parameter. So, lets start parsing `kwargs`, starting
1926 # with the current parameter
1927 parameters_ex = (param,)
1928 break
1929 else:
1930 if partial:
1931 parameters_ex = (param,)
1932 break
1933 else:
1934 msg = '{arg!r} parameter lacking default value'
1935 msg = msg.format(arg=param.name)
1936 raise TypeError(msg) from None
1937 else:
1938 # We have a positional argument to process
1939 try:
1940 param = next(parameters)
1941 except StopIteration:
1942 raise TypeError('too many positional arguments') from None
1943 else:
1944 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
1945 # Looks like we have no parameter for this positional
1946 # argument
1947 raise TypeError('too many positional arguments')
1948
1949 if param.kind == _VAR_POSITIONAL:
1950 # We have an '*args'-like argument, let's fill it with
1951 # all positional arguments we have left and move on to
1952 # the next phase
1953 values = [arg_val]
1954 values.extend(arg_vals)
1955 arguments[param.name] = tuple(values)
1956 break
1957
1958 if param.name in kwargs:
1959 raise TypeError('multiple values for argument '
1960 '{arg!r}'.format(arg=param.name))
1961
1962 arguments[param.name] = arg_val
1963
1964 # Now, we iterate through the remaining parameters to process
1965 # keyword arguments
1966 kwargs_param = None
1967 for param in itertools.chain(parameters_ex, parameters):
1968 if param.kind == _POSITIONAL_ONLY:
1969 # This should never happen in case of a properly built
1970 # Signature object (but let's have this check here
1971 # to ensure correct behaviour just in case)
1972 raise TypeError('{arg!r} parameter is positional only, '
1973 'but was passed as a keyword'. \
1974 format(arg=param.name))
1975
1976 if param.kind == _VAR_KEYWORD:
1977 # Memorize that we have a '**kwargs'-like parameter
1978 kwargs_param = param
1979 continue
1980
1981 param_name = param.name
1982 try:
1983 arg_val = kwargs.pop(param_name)
1984 except KeyError:
1985 # We have no value for this parameter. It's fine though,
1986 # if it has a default value, or it is an '*args'-like
1987 # parameter, left alone by the processing of positional
1988 # arguments.
1989 if (not partial and param.kind != _VAR_POSITIONAL and
1990 param.default is _empty):
1991 raise TypeError('{arg!r} parameter lacking default value'. \
1992 format(arg=param_name)) from None
1993
1994 else:
1995 arguments[param_name] = arg_val
1996
1997 if kwargs:
1998 if kwargs_param is not None:
1999 # Process our '**kwargs'-like parameter
2000 arguments[kwargs_param.name] = kwargs
2001 else:
2002 raise TypeError('too many keyword arguments')
2003
2004 return self._bound_arguments_cls(self, arguments)
2005
2006 def bind(self, *args, **kwargs):
2007 '''Get a BoundArguments object, that maps the passed `args`
2008 and `kwargs` to the function's signature. Raises `TypeError`
2009 if the passed arguments can not be bound.
2010 '''
2011 return self._bind(args, kwargs)
2012
2013 def bind_partial(self, *args, **kwargs):
2014 '''Get a BoundArguments object, that partially maps the
2015 passed `args` and `kwargs` to the function's signature.
2016 Raises `TypeError` if the passed arguments can not be bound.
2017 '''
2018 return self._bind(args, kwargs, partial=True)
2019
2020 def __str__(self):
2021 result = []
2022 render_kw_only_separator = True
2023 for idx, param in enumerate(self.parameters.values()):
2024 formatted = str(param)
2025
2026 kind = param.kind
2027 if kind == _VAR_POSITIONAL:
2028 # OK, we have an '*args'-like parameter, so we won't need
2029 # a '*' to separate keyword-only arguments
2030 render_kw_only_separator = False
2031 elif kind == _KEYWORD_ONLY and render_kw_only_separator:
2032 # We have a keyword-only parameter to render and we haven't
2033 # rendered an '*args'-like parameter before, so add a '*'
2034 # separator to the parameters list ("foo(arg1, *, arg2)" case)
2035 result.append('*')
2036 # This condition should be only triggered once, so
2037 # reset the flag
2038 render_kw_only_separator = False
2039
2040 result.append(formatted)
2041
2042 rendered = '({})'.format(', '.join(result))
2043
2044 if self.return_annotation is not _empty:
2045 anno = formatannotation(self.return_annotation)
2046 rendered += ' -> {}'.format(anno)
2047
2048 return rendered