PageRenderTime 72ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/lib-python/2.7/inspect.py

https://bitbucket.org/yrttyr/pypy
Python | 1067 lines | 989 code | 7 blank | 71 comment | 13 complexity | 1dfe74f1058950a7ab91a1b7952a2ec7 MD5 | raw file
  1. # -*- coding: iso-8859-1 -*-
  2. """Get useful information from live Python objects.
  3. This module encapsulates the interface provided by the internal special
  4. attributes (func_*, co_*, im_*, tb_*, etc.) in a friendlier fashion.
  5. It also provides some help for examining source code and class layout.
  6. Here are some of the useful functions provided by this module:
  7. ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
  8. isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(),
  9. isroutine() - check object types
  10. getmembers() - get members of an object that satisfy a given condition
  11. getfile(), getsourcefile(), getsource() - find an object's source code
  12. getdoc(), getcomments() - get documentation on an object
  13. getmodule() - determine the module that an object came from
  14. getclasstree() - arrange classes so as to represent their hierarchy
  15. getargspec(), getargvalues(), getcallargs() - get info about function arguments
  16. formatargspec(), formatargvalues() - format an argument spec
  17. getouterframes(), getinnerframes() - get info about frames
  18. currentframe() - get the current stack frame
  19. stack(), trace() - get info about frames on the stack or in a traceback
  20. """
  21. # This module is in the public domain. No warranties.
  22. __author__ = 'Ka-Ping Yee <ping@lfw.org>'
  23. __date__ = '1 Jan 2001'
  24. import sys
  25. import os
  26. import types
  27. import string
  28. import re
  29. import dis
  30. import imp
  31. import tokenize
  32. import linecache
  33. from operator import attrgetter
  34. from collections import namedtuple
  35. # These constants are from Include/code.h.
  36. CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS = 0x1, 0x2, 0x4, 0x8
  37. CO_NESTED, CO_GENERATOR, CO_NOFREE = 0x10, 0x20, 0x40
  38. # See Include/object.h
  39. TPFLAGS_IS_ABSTRACT = 1 << 20
  40. # ----------------------------------------------------------- type-checking
  41. def ismodule(object):
  42. """Return true if the object is a module.
  43. Module objects provide these attributes:
  44. __doc__ documentation string
  45. __file__ filename (missing for built-in modules)"""
  46. return isinstance(object, types.ModuleType)
  47. def isclass(object):
  48. """Return true if the object is a class.
  49. Class objects provide these attributes:
  50. __doc__ documentation string
  51. __module__ name of module in which this class was defined"""
  52. return isinstance(object, (type, types.ClassType))
  53. def ismethod(object):
  54. """Return true if the object is an instance method.
  55. Instance method objects provide these attributes:
  56. __doc__ documentation string
  57. __name__ name with which this method was defined
  58. im_class class object in which this method belongs
  59. im_func function object containing implementation of method
  60. im_self instance to which this method is bound, or None"""
  61. return isinstance(object, types.MethodType)
  62. def ismethoddescriptor(object):
  63. """Return true if the object is a method descriptor.
  64. But not if ismethod() or isclass() or isfunction() are true.
  65. This is new in Python 2.2, and, for example, is true of int.__add__.
  66. An object passing this test has a __get__ attribute but not a __set__
  67. attribute, but beyond that the set of attributes varies. __name__ is
  68. usually sensible, and __doc__ often is.
  69. Methods implemented via descriptors that also pass one of the other
  70. tests return false from the ismethoddescriptor() test, simply because
  71. the other tests promise more -- you can, e.g., count on having the
  72. im_func attribute (etc) when an object passes ismethod()."""
  73. return (hasattr(object, "__get__")
  74. and not hasattr(object, "__set__") # else it's a data descriptor
  75. and not ismethod(object) # mutual exclusion
  76. and not isfunction(object)
  77. and not isclass(object))
  78. def isdatadescriptor(object):
  79. """Return true if the object is a data descriptor.
  80. Data descriptors have both a __get__ and a __set__ attribute. Examples are
  81. properties (defined in Python) and getsets and members (defined in C).
  82. Typically, data descriptors will also have __name__ and __doc__ attributes
  83. (properties, getsets, and members have both of these attributes), but this
  84. is not guaranteed."""
  85. return (hasattr(object, "__set__") and hasattr(object, "__get__"))
  86. if hasattr(types, 'MemberDescriptorType'):
  87. # CPython and equivalent
  88. def ismemberdescriptor(object):
  89. """Return true if the object is a member descriptor.
  90. Member descriptors are specialized descriptors defined in extension
  91. modules."""
  92. return isinstance(object, types.MemberDescriptorType)
  93. else:
  94. # Other implementations
  95. def ismemberdescriptor(object):
  96. """Return true if the object is a member descriptor.
  97. Member descriptors are specialized descriptors defined in extension
  98. modules."""
  99. return False
  100. if hasattr(types, 'GetSetDescriptorType'):
  101. # CPython and equivalent
  102. def isgetsetdescriptor(object):
  103. """Return true if the object is a getset descriptor.
  104. getset descriptors are specialized descriptors defined in extension
  105. modules."""
  106. return isinstance(object, types.GetSetDescriptorType)
  107. else:
  108. # Other implementations
  109. def isgetsetdescriptor(object):
  110. """Return true if the object is a getset descriptor.
  111. getset descriptors are specialized descriptors defined in extension
  112. modules."""
  113. return False
  114. def isfunction(object):
  115. """Return true if the object is a user-defined function.
  116. Function objects provide these attributes:
  117. __doc__ documentation string
  118. __name__ name with which this function was defined
  119. func_code code object containing compiled function bytecode
  120. func_defaults tuple of any default values for arguments
  121. func_doc (same as __doc__)
  122. func_globals global namespace in which this function was defined
  123. func_name (same as __name__)"""
  124. return isinstance(object, types.FunctionType)
  125. def isgeneratorfunction(object):
  126. """Return true if the object is a user-defined generator function.
  127. Generator function objects provides same attributes as functions.
  128. See help(isfunction) for attributes listing."""
  129. return bool((isfunction(object) or ismethod(object)) and
  130. object.func_code.co_flags & CO_GENERATOR)
  131. def isgenerator(object):
  132. """Return true if the object is a generator.
  133. Generator objects provide these attributes:
  134. __iter__ defined to support interation over container
  135. close raises a new GeneratorExit exception inside the
  136. generator to terminate the iteration
  137. gi_code code object
  138. gi_frame frame object or possibly None once the generator has
  139. been exhausted
  140. gi_running set to 1 when generator is executing, 0 otherwise
  141. next return the next item from the container
  142. send resumes the generator and "sends" a value that becomes
  143. the result of the current yield-expression
  144. throw used to raise an exception inside the generator"""
  145. return isinstance(object, types.GeneratorType)
  146. def istraceback(object):
  147. """Return true if the object is a traceback.
  148. Traceback objects provide these attributes:
  149. tb_frame frame object at this level
  150. tb_lasti index of last attempted instruction in bytecode
  151. tb_lineno current line number in Python source code
  152. tb_next next inner traceback object (called by this level)"""
  153. return isinstance(object, types.TracebackType)
  154. def isframe(object):
  155. """Return true if the object is a frame object.
  156. Frame objects provide these attributes:
  157. f_back next outer frame object (this frame's caller)
  158. f_builtins built-in namespace seen by this frame
  159. f_code code object being executed in this frame
  160. f_exc_traceback traceback if raised in this frame, or None
  161. f_exc_type exception type if raised in this frame, or None
  162. f_exc_value exception value if raised in this frame, or None
  163. f_globals global namespace seen by this frame
  164. f_lasti index of last attempted instruction in bytecode
  165. f_lineno current line number in Python source code
  166. f_locals local namespace seen by this frame
  167. f_restricted 0 or 1 if frame is in restricted execution mode
  168. f_trace tracing function for this frame, or None"""
  169. return isinstance(object, types.FrameType)
  170. def iscode(object):
  171. """Return true if the object is a code object.
  172. Code objects provide these attributes:
  173. co_argcount number of arguments (not including * or ** args)
  174. co_code string of raw compiled bytecode
  175. co_consts tuple of constants used in the bytecode
  176. co_filename name of file in which this code object was created
  177. co_firstlineno number of first line in Python source code
  178. co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
  179. co_lnotab encoded mapping of line numbers to bytecode indices
  180. co_name name with which this code object was defined
  181. co_names tuple of names of local variables
  182. co_nlocals number of local variables
  183. co_stacksize virtual machine stack space required
  184. co_varnames tuple of names of arguments and local variables"""
  185. return isinstance(object, types.CodeType)
  186. def isbuiltin(object):
  187. """Return true if the object is a built-in function or method.
  188. Built-in functions and methods provide these attributes:
  189. __doc__ documentation string
  190. __name__ original name of this function or method
  191. __self__ instance to which a method is bound, or None"""
  192. return isinstance(object, types.BuiltinFunctionType)
  193. def isroutine(object):
  194. """Return true if the object is any kind of function or method."""
  195. return (isbuiltin(object)
  196. or isfunction(object)
  197. or ismethod(object)
  198. or ismethoddescriptor(object))
  199. def isabstract(object):
  200. """Return true if the object is an abstract base class (ABC)."""
  201. return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT)
  202. def getmembers(object, predicate=None):
  203. """Return all members of an object as (name, value) pairs sorted by name.
  204. Optionally, only return members that satisfy a given predicate."""
  205. results = []
  206. for key in dir(object):
  207. try:
  208. value = getattr(object, key)
  209. except AttributeError:
  210. continue
  211. if not predicate or predicate(value):
  212. results.append((key, value))
  213. results.sort()
  214. return results
  215. Attribute = namedtuple('Attribute', 'name kind defining_class object')
  216. def classify_class_attrs(cls):
  217. """Return list of attribute-descriptor tuples.
  218. For each name in dir(cls), the return list contains a 4-tuple
  219. with these elements:
  220. 0. The name (a string).
  221. 1. The kind of attribute this is, one of these strings:
  222. 'class method' created via classmethod()
  223. 'static method' created via staticmethod()
  224. 'property' created via property()
  225. 'method' any other flavor of method
  226. 'data' not a method
  227. 2. The class which defined this attribute (a class).
  228. 3. The object as obtained directly from the defining class's
  229. __dict__, not via getattr. This is especially important for
  230. data attributes: C.data is just a data object, but
  231. C.__dict__['data'] may be a data descriptor with additional
  232. info, like a __doc__ string.
  233. """
  234. mro = getmro(cls)
  235. names = dir(cls)
  236. result = []
  237. for name in names:
  238. # Get the object associated with the name, and where it was defined.
  239. # Getting an obj from the __dict__ sometimes reveals more than
  240. # using getattr. Static and class methods are dramatic examples.
  241. # Furthermore, some objects may raise an Exception when fetched with
  242. # getattr(). This is the case with some descriptors (bug #1785).
  243. # Thus, we only use getattr() as a last resort.
  244. homecls = None
  245. for base in (cls,) + mro:
  246. if name in base.__dict__:
  247. obj = base.__dict__[name]
  248. homecls = base
  249. break
  250. else:
  251. obj = getattr(cls, name)
  252. homecls = getattr(obj, "__objclass__", homecls)
  253. # Classify the object.
  254. if isinstance(obj, staticmethod):
  255. kind = "static method"
  256. elif isinstance(obj, classmethod):
  257. kind = "class method"
  258. elif isinstance(obj, property):
  259. kind = "property"
  260. elif ismethoddescriptor(obj):
  261. kind = "method"
  262. elif isdatadescriptor(obj):
  263. kind = "data"
  264. else:
  265. obj_via_getattr = getattr(cls, name)
  266. if (ismethod(obj_via_getattr) or
  267. ismethoddescriptor(obj_via_getattr)):
  268. kind = "method"
  269. else:
  270. kind = "data"
  271. obj = obj_via_getattr
  272. result.append(Attribute(name, kind, homecls, obj))
  273. return result
  274. # ----------------------------------------------------------- class helpers
  275. def _searchbases(cls, accum):
  276. # Simulate the "classic class" search order.
  277. if cls in accum:
  278. return
  279. accum.append(cls)
  280. for base in cls.__bases__:
  281. _searchbases(base, accum)
  282. def getmro(cls):
  283. "Return tuple of base classes (including cls) in method resolution order."
  284. if hasattr(cls, "__mro__"):
  285. return cls.__mro__
  286. else:
  287. result = []
  288. _searchbases(cls, result)
  289. return tuple(result)
  290. # -------------------------------------------------- source code extraction
  291. def indentsize(line):
  292. """Return the indent size, in spaces, at the start of a line of text."""
  293. expline = string.expandtabs(line)
  294. return len(expline) - len(string.lstrip(expline))
  295. def getdoc(object):
  296. """Get the documentation string for an object.
  297. All tabs are expanded to spaces. To clean up docstrings that are
  298. indented to line up with blocks of code, any whitespace than can be
  299. uniformly removed from the second line onwards is removed."""
  300. try:
  301. doc = object.__doc__
  302. except AttributeError:
  303. return None
  304. if not isinstance(doc, types.StringTypes):
  305. return None
  306. return cleandoc(doc)
  307. def cleandoc(doc):
  308. """Clean up indentation from docstrings.
  309. Any whitespace that can be uniformly removed from the second line
  310. onwards is removed."""
  311. try:
  312. lines = string.split(string.expandtabs(doc), '\n')
  313. except UnicodeError:
  314. return None
  315. else:
  316. # Find minimum indentation of any non-blank lines after first line.
  317. margin = sys.maxint
  318. for line in lines[1:]:
  319. content = len(string.lstrip(line))
  320. if content:
  321. indent = len(line) - content
  322. margin = min(margin, indent)
  323. # Remove indentation.
  324. if lines:
  325. lines[0] = lines[0].lstrip()
  326. if margin < sys.maxint:
  327. for i in range(1, len(lines)): lines[i] = lines[i][margin:]
  328. # Remove any trailing or leading blank lines.
  329. while lines and not lines[-1]:
  330. lines.pop()
  331. while lines and not lines[0]:
  332. lines.pop(0)
  333. return string.join(lines, '\n')
  334. def getfile(object):
  335. """Work out which source or compiled file an object was defined in."""
  336. if ismodule(object):
  337. if hasattr(object, '__file__'):
  338. return object.__file__
  339. raise TypeError('{!r} is a built-in module'.format(object))
  340. if isclass(object):
  341. object = sys.modules.get(object.__module__)
  342. if hasattr(object, '__file__'):
  343. return object.__file__
  344. raise TypeError('{!r} is a built-in class'.format(object))
  345. if ismethod(object):
  346. object = object.im_func
  347. if isfunction(object):
  348. object = object.func_code
  349. if istraceback(object):
  350. object = object.tb_frame
  351. if isframe(object):
  352. object = object.f_code
  353. if iscode(object):
  354. return object.co_filename
  355. raise TypeError('{!r} is not a module, class, method, '
  356. 'function, traceback, frame, or code object'.format(object))
  357. ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')
  358. def getmoduleinfo(path):
  359. """Get the module name, suffix, mode, and module type for a given file."""
  360. filename = os.path.basename(path)
  361. suffixes = map(lambda info:
  362. (-len(info[0]), info[0], info[1], info[2]),
  363. imp.get_suffixes())
  364. suffixes.sort() # try longest suffixes first, in case they overlap
  365. for neglen, suffix, mode, mtype in suffixes:
  366. if filename[neglen:] == suffix:
  367. return ModuleInfo(filename[:neglen], suffix, mode, mtype)
  368. def getmodulename(path):
  369. """Return the module name for a given file, or None."""
  370. info = getmoduleinfo(path)
  371. if info: return info[0]
  372. def getsourcefile(object):
  373. """Return the filename that can be used to locate an object's source.
  374. Return None if no way can be identified to get the source.
  375. """
  376. filename = getfile(object)
  377. if string.lower(filename[-4:]) in ('.pyc', '.pyo'):
  378. filename = filename[:-4] + '.py'
  379. for suffix, mode, kind in imp.get_suffixes():
  380. if 'b' in mode and string.lower(filename[-len(suffix):]) == suffix:
  381. # Looks like a binary file. We want to only return a text file.
  382. return None
  383. if os.path.exists(filename):
  384. return filename
  385. # only return a non-existent filename if the module has a PEP 302 loader
  386. if hasattr(getmodule(object, filename), '__loader__'):
  387. return filename
  388. # or it is in the linecache
  389. if filename in linecache.cache:
  390. return filename
  391. def getabsfile(object, _filename=None):
  392. """Return an absolute path to the source or compiled file for an object.
  393. The idea is for each object to have a unique origin, so this routine
  394. normalizes the result as much as possible."""
  395. if _filename is None:
  396. _filename = getsourcefile(object) or getfile(object)
  397. return os.path.normcase(os.path.abspath(_filename))
  398. modulesbyfile = {}
  399. _filesbymodname = {}
  400. def getmodule(object, _filename=None):
  401. """Return the module an object was defined in, or None if not found."""
  402. if ismodule(object):
  403. return object
  404. if hasattr(object, '__module__'):
  405. return sys.modules.get(object.__module__)
  406. # Try the filename to modulename cache
  407. if _filename is not None and _filename in modulesbyfile:
  408. return sys.modules.get(modulesbyfile[_filename])
  409. # Try the cache again with the absolute file name
  410. try:
  411. file = getabsfile(object, _filename)
  412. except TypeError:
  413. return None
  414. if file in modulesbyfile:
  415. return sys.modules.get(modulesbyfile[file])
  416. # Update the filename to module name cache and check yet again
  417. # Copy sys.modules in order to cope with changes while iterating
  418. for modname, module in sys.modules.items():
  419. if ismodule(module) and hasattr(module, '__file__'):
  420. f = module.__file__
  421. if f == _filesbymodname.get(modname, None):
  422. # Have already mapped this module, so skip it
  423. continue
  424. _filesbymodname[modname] = f
  425. f = getabsfile(module)
  426. # Always map to the name the module knows itself by
  427. modulesbyfile[f] = modulesbyfile[
  428. os.path.realpath(f)] = module.__name__
  429. if file in modulesbyfile:
  430. return sys.modules.get(modulesbyfile[file])
  431. # Check the main module
  432. main = sys.modules['__main__']
  433. if not hasattr(object, '__name__'):
  434. return None
  435. if hasattr(main, object.__name__):
  436. mainobject = getattr(main, object.__name__)
  437. if mainobject is object:
  438. return main
  439. # Check builtins
  440. builtin = sys.modules['__builtin__']
  441. if hasattr(builtin, object.__name__):
  442. builtinobject = getattr(builtin, object.__name__)
  443. if builtinobject is object:
  444. return builtin
  445. def findsource(object):
  446. """Return the entire source file and starting line number for an object.
  447. The argument may be a module, class, method, function, traceback, frame,
  448. or code object. The source code is returned as a list of all the lines
  449. in the file and the line number indexes a line in that list. An IOError
  450. is raised if the source code cannot be retrieved."""
  451. file = getfile(object)
  452. sourcefile = getsourcefile(object)
  453. if not sourcefile and file[0] + file[-1] != '<>':
  454. raise IOError('source code not available')
  455. file = sourcefile if sourcefile else file
  456. module = getmodule(object, file)
  457. if module:
  458. lines = linecache.getlines(file, module.__dict__)
  459. else:
  460. lines = linecache.getlines(file)
  461. if not lines:
  462. raise IOError('could not get source code')
  463. if ismodule(object):
  464. return lines, 0
  465. if isclass(object):
  466. name = object.__name__
  467. pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
  468. # make some effort to find the best matching class definition:
  469. # use the one with the least indentation, which is the one
  470. # that's most probably not inside a function definition.
  471. candidates = []
  472. for i in range(len(lines)):
  473. match = pat.match(lines[i])
  474. if match:
  475. # if it's at toplevel, it's already the best one
  476. if lines[i][0] == 'c':
  477. return lines, i
  478. # else add whitespace to candidate list
  479. candidates.append((match.group(1), i))
  480. if candidates:
  481. # this will sort by whitespace, and by line number,
  482. # less whitespace first
  483. candidates.sort()
  484. return lines, candidates[0][1]
  485. else:
  486. raise IOError('could not find class definition')
  487. if ismethod(object):
  488. object = object.im_func
  489. if isfunction(object):
  490. object = object.func_code
  491. if istraceback(object):
  492. object = object.tb_frame
  493. if isframe(object):
  494. object = object.f_code
  495. if iscode(object):
  496. if not hasattr(object, 'co_firstlineno'):
  497. raise IOError('could not find function definition')
  498. lnum = object.co_firstlineno - 1
  499. pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
  500. while lnum > 0:
  501. if pat.match(lines[lnum]): break
  502. lnum = lnum - 1
  503. return lines, lnum
  504. raise IOError('could not find code object')
  505. def getcomments(object):
  506. """Get lines of comments immediately preceding an object's source code.
  507. Returns None when source can't be found.
  508. """
  509. try:
  510. lines, lnum = findsource(object)
  511. except (IOError, TypeError):
  512. return None
  513. if ismodule(object):
  514. # Look for a comment block at the top of the file.
  515. start = 0
  516. if lines and lines[0][:2] == '#!': start = 1
  517. while start < len(lines) and string.strip(lines[start]) in ('', '#'):
  518. start = start + 1
  519. if start < len(lines) and lines[start][:1] == '#':
  520. comments = []
  521. end = start
  522. while end < len(lines) and lines[end][:1] == '#':
  523. comments.append(string.expandtabs(lines[end]))
  524. end = end + 1
  525. return string.join(comments, '')
  526. # Look for a preceding block of comments at the same indentation.
  527. elif lnum > 0:
  528. indent = indentsize(lines[lnum])
  529. end = lnum - 1
  530. if end >= 0 and string.lstrip(lines[end])[:1] == '#' and \
  531. indentsize(lines[end]) == indent:
  532. comments = [string.lstrip(string.expandtabs(lines[end]))]
  533. if end > 0:
  534. end = end - 1
  535. comment = string.lstrip(string.expandtabs(lines[end]))
  536. while comment[:1] == '#' and indentsize(lines[end]) == indent:
  537. comments[:0] = [comment]
  538. end = end - 1
  539. if end < 0: break
  540. comment = string.lstrip(string.expandtabs(lines[end]))
  541. while comments and string.strip(comments[0]) == '#':
  542. comments[:1] = []
  543. while comments and string.strip(comments[-1]) == '#':
  544. comments[-1:] = []
  545. return string.join(comments, '')
  546. class EndOfBlock(Exception): pass
  547. class BlockFinder:
  548. """Provide a tokeneater() method to detect the end of a code block."""
  549. def __init__(self):
  550. self.indent = 0
  551. self.islambda = False
  552. self.started = False
  553. self.passline = False
  554. self.last = 1
  555. def tokeneater(self, type, token, srow_scol, erow_ecol, line):
  556. srow, scol = srow_scol
  557. erow, ecol = erow_ecol
  558. if not self.started:
  559. # look for the first "def", "class" or "lambda"
  560. if token in ("def", "class", "lambda"):
  561. if token == "lambda":
  562. self.islambda = True
  563. self.started = True
  564. self.passline = True # skip to the end of the line
  565. elif type == tokenize.NEWLINE:
  566. self.passline = False # stop skipping when a NEWLINE is seen
  567. self.last = srow
  568. if self.islambda: # lambdas always end at the first NEWLINE
  569. raise EndOfBlock
  570. elif self.passline:
  571. pass
  572. elif type == tokenize.INDENT:
  573. self.indent = self.indent + 1
  574. self.passline = True
  575. elif type == tokenize.DEDENT:
  576. self.indent = self.indent - 1
  577. # the end of matching indent/dedent pairs end a block
  578. # (note that this only works for "def"/"class" blocks,
  579. # not e.g. for "if: else:" or "try: finally:" blocks)
  580. if self.indent <= 0:
  581. raise EndOfBlock
  582. elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
  583. # any other token on the same indentation level end the previous
  584. # block as well, except the pseudo-tokens COMMENT and NL.
  585. raise EndOfBlock
  586. def getblock(lines):
  587. """Extract the block of code at the top of the given list of lines."""
  588. blockfinder = BlockFinder()
  589. try:
  590. tokenize.tokenize(iter(lines).next, blockfinder.tokeneater)
  591. except (EndOfBlock, IndentationError):
  592. pass
  593. return lines[:blockfinder.last]
  594. def getsourcelines(object):
  595. """Return a list of source lines and starting line number for an object.
  596. The argument may be a module, class, method, function, traceback, frame,
  597. or code object. The source code is returned as a list of the lines
  598. corresponding to the object and the line number indicates where in the
  599. original source file the first line of code was found. An IOError is
  600. raised if the source code cannot be retrieved."""
  601. lines, lnum = findsource(object)
  602. if ismodule(object): return lines, 0
  603. else: return getblock(lines[lnum:]), lnum + 1
  604. def getsource(object):
  605. """Return the text of the source code for an object.
  606. The argument may be a module, class, method, function, traceback, frame,
  607. or code object. The source code is returned as a single string. An
  608. IOError is raised if the source code cannot be retrieved."""
  609. lines, lnum = getsourcelines(object)
  610. return string.join(lines, '')
  611. # --------------------------------------------------- class tree extraction
  612. def walktree(classes, children, parent):
  613. """Recursive helper function for getclasstree()."""
  614. results = []
  615. classes.sort(key=attrgetter('__module__', '__name__'))
  616. for c in classes:
  617. results.append((c, c.__bases__))
  618. if c in children:
  619. results.append(walktree(children[c], children, c))
  620. return results
  621. def getclasstree(classes, unique=0):
  622. """Arrange the given list of classes into a hierarchy of nested lists.
  623. Where a nested list appears, it contains classes derived from the class
  624. whose entry immediately precedes the list. Each entry is a 2-tuple
  625. containing a class and a tuple of its base classes. If the 'unique'
  626. argument is true, exactly one entry appears in the returned structure
  627. for each class in the given list. Otherwise, classes using multiple
  628. inheritance and their descendants will appear multiple times."""
  629. children = {}
  630. roots = []
  631. for c in classes:
  632. if c.__bases__:
  633. for parent in c.__bases__:
  634. if not parent in children:
  635. children[parent] = []
  636. children[parent].append(c)
  637. if unique and parent in classes: break
  638. elif c not in roots:
  639. roots.append(c)
  640. for parent in children:
  641. if parent not in classes:
  642. roots.append(parent)
  643. return walktree(roots, children, None)
  644. # ------------------------------------------------ argument list extraction
  645. Arguments = namedtuple('Arguments', 'args varargs keywords')
  646. def getargs(co):
  647. """Get information about the arguments accepted by a code object.
  648. Three things are returned: (args, varargs, varkw), where 'args' is
  649. a list of argument names (possibly containing nested lists), and
  650. 'varargs' and 'varkw' are the names of the * and ** arguments or None."""
  651. if not iscode(co):
  652. if hasattr(len, 'func_code') and type(co) is type(len.func_code):
  653. # PyPy extension: built-in function objects have a func_code too.
  654. # There is no co_code on it, but co_argcount and co_varnames and
  655. # co_flags are present.
  656. pass
  657. else:
  658. raise TypeError('{!r} is not a code object'.format(co))
  659. code = getattr(co, 'co_code', '')
  660. nargs = co.co_argcount
  661. names = co.co_varnames
  662. args = list(names[:nargs])
  663. step = 0
  664. # The following acrobatics are for anonymous (tuple) arguments.
  665. for i in range(nargs):
  666. if args[i][:1] in ('', '.'):
  667. stack, remain, count = [], [], []
  668. while step < len(code):
  669. op = ord(code[step])
  670. step = step + 1
  671. if op >= dis.HAVE_ARGUMENT:
  672. opname = dis.opname[op]
  673. value = ord(code[step]) + ord(code[step+1])*256
  674. step = step + 2
  675. if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
  676. remain.append(value)
  677. count.append(value)
  678. elif opname == 'STORE_FAST':
  679. stack.append(names[value])
  680. # Special case for sublists of length 1: def foo((bar))
  681. # doesn't generate the UNPACK_TUPLE bytecode, so if
  682. # `remain` is empty here, we have such a sublist.
  683. if not remain:
  684. stack[0] = [stack[0]]
  685. break
  686. else:
  687. remain[-1] = remain[-1] - 1
  688. while remain[-1] == 0:
  689. remain.pop()
  690. size = count.pop()
  691. stack[-size:] = [stack[-size:]]
  692. if not remain: break
  693. remain[-1] = remain[-1] - 1
  694. if not remain: break
  695. args[i] = stack[0]
  696. varargs = None
  697. if co.co_flags & CO_VARARGS:
  698. varargs = co.co_varnames[nargs]
  699. nargs = nargs + 1
  700. varkw = None
  701. if co.co_flags & CO_VARKEYWORDS:
  702. varkw = co.co_varnames[nargs]
  703. return Arguments(args, varargs, varkw)
  704. ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
  705. def getargspec(func):
  706. """Get the names and default values of a function's arguments.
  707. A tuple of four things is returned: (args, varargs, varkw, defaults).
  708. 'args' is a list of the argument names (it may contain nested lists).
  709. 'varargs' and 'varkw' are the names of the * and ** arguments or None.
  710. 'defaults' is an n-tuple of the default values of the last n arguments.
  711. """
  712. if ismethod(func):
  713. func = func.im_func
  714. if not (isfunction(func) or
  715. isbuiltin(func) and hasattr(func, 'func_code')):
  716. # PyPy extension: this works for built-in functions too
  717. raise TypeError('{!r} is not a Python function'.format(func))
  718. args, varargs, varkw = getargs(func.func_code)
  719. return ArgSpec(args, varargs, varkw, func.func_defaults)
  720. ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
  721. def getargvalues(frame):
  722. """Get information about arguments passed into a particular frame.
  723. A tuple of four things is returned: (args, varargs, varkw, locals).
  724. 'args' is a list of the argument names (it may contain nested lists).
  725. 'varargs' and 'varkw' are the names of the * and ** arguments or None.
  726. 'locals' is the locals dictionary of the given frame."""
  727. args, varargs, varkw = getargs(frame.f_code)
  728. return ArgInfo(args, varargs, varkw, frame.f_locals)
  729. def joinseq(seq):
  730. if len(seq) == 1:
  731. return '(' + seq[0] + ',)'
  732. else:
  733. return '(' + string.join(seq, ', ') + ')'
  734. def strseq(object, convert, join=joinseq):
  735. """Recursively walk a sequence, stringifying each element."""
  736. if type(object) in (list, tuple):
  737. return join(map(lambda o, c=convert, j=join: strseq(o, c, j), object))
  738. else:
  739. return convert(object)
  740. def formatargspec(args, varargs=None, varkw=None, defaults=None,
  741. formatarg=str,
  742. formatvarargs=lambda name: '*' + name,
  743. formatvarkw=lambda name: '**' + name,
  744. formatvalue=lambda value: '=' + repr(value),
  745. join=joinseq):
  746. """Format an argument spec from the 4 values returned by getargspec.
  747. The first four arguments are (args, varargs, varkw, defaults). The
  748. other four arguments are the corresponding optional formatting functions
  749. that are called to turn names and values into strings. The ninth
  750. argument is an optional function to format the sequence of arguments."""
  751. specs = []
  752. if defaults:
  753. firstdefault = len(args) - len(defaults)
  754. for i, arg in enumerate(args):
  755. spec = strseq(arg, formatarg, join)
  756. if defaults and i >= firstdefault:
  757. spec = spec + formatvalue(defaults[i - firstdefault])
  758. specs.append(spec)
  759. if varargs is not None:
  760. specs.append(formatvarargs(varargs))
  761. if varkw is not None:
  762. specs.append(formatvarkw(varkw))
  763. return '(' + string.join(specs, ', ') + ')'
  764. def formatargvalues(args, varargs, varkw, locals,
  765. formatarg=str,
  766. formatvarargs=lambda name: '*' + name,
  767. formatvarkw=lambda name: '**' + name,
  768. formatvalue=lambda value: '=' + repr(value),
  769. join=joinseq):
  770. """Format an argument spec from the 4 values returned by getargvalues.
  771. The first four arguments are (args, varargs, varkw, locals). The
  772. next four arguments are the corresponding optional formatting functions
  773. that are called to turn names and values into strings. The ninth
  774. argument is an optional function to format the sequence of arguments."""
  775. def convert(name, locals=locals,
  776. formatarg=formatarg, formatvalue=formatvalue):
  777. return formatarg(name) + formatvalue(locals[name])
  778. specs = []
  779. for i in range(len(args)):
  780. specs.append(strseq(args[i], convert, join))
  781. if varargs:
  782. specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
  783. if varkw:
  784. specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
  785. return '(' + string.join(specs, ', ') + ')'
  786. def getcallargs(func, *positional, **named):
  787. """Get the mapping of arguments to values.
  788. A dict is returned, with keys the function argument names (including the
  789. names of the * and ** arguments, if any), and values the respective bound
  790. values from 'positional' and 'named'."""
  791. args, varargs, varkw, defaults = getargspec(func)
  792. f_name = func.__name__
  793. arg2value = {}
  794. # The following closures are basically because of tuple parameter unpacking.
  795. assigned_tuple_params = []
  796. def assign(arg, value):
  797. if isinstance(arg, str):
  798. arg2value[arg] = value
  799. else:
  800. assigned_tuple_params.append(arg)
  801. value = iter(value)
  802. for i, subarg in enumerate(arg):
  803. try:
  804. subvalue = next(value)
  805. except StopIteration:
  806. raise ValueError('need more than %d %s to unpack' %
  807. (i, 'values' if i > 1 else 'value'))
  808. assign(subarg,subvalue)
  809. try:
  810. next(value)
  811. except StopIteration:
  812. pass
  813. else:
  814. raise ValueError('too many values to unpack')
  815. def is_assigned(arg):
  816. if isinstance(arg,str):
  817. return arg in arg2value
  818. return arg in assigned_tuple_params
  819. if ismethod(func) and func.im_self is not None:
  820. # implicit 'self' (or 'cls' for classmethods) argument
  821. positional = (func.im_self,) + positional
  822. num_pos = len(positional)
  823. num_total = num_pos + len(named)
  824. num_args = len(args)
  825. num_defaults = len(defaults) if defaults else 0
  826. for arg, value in zip(args, positional):
  827. assign(arg, value)
  828. if varargs:
  829. if num_pos > num_args:
  830. assign(varargs, positional[-(num_pos-num_args):])
  831. else:
  832. assign(varargs, ())
  833. elif 0 < num_args < num_pos:
  834. raise TypeError('%s() takes %s %d %s (%d given)' % (
  835. f_name, 'at most' if defaults else 'exactly', num_args,
  836. 'arguments' if num_args > 1 else 'argument', num_total))
  837. elif num_args == 0 and num_total:
  838. if varkw:
  839. if num_pos:
  840. # XXX: We should use num_pos, but Python also uses num_total:
  841. raise TypeError('%s() takes exactly 0 arguments '
  842. '(%d given)' % (f_name, num_total))
  843. else:
  844. raise TypeError('%s() takes no argument (%d given)' %
  845. (f_name, num_total))
  846. for arg in args:
  847. if isinstance(arg, str) and arg in named:
  848. if is_assigned(arg):
  849. raise TypeError("%s() got multiple values for keyword "
  850. "argument '%s'" % (f_name, arg))
  851. else:
  852. assign(arg, named.pop(arg))
  853. if defaults: # fill in any missing values with the defaults
  854. for arg, value in zip(args[-num_defaults:], defaults):
  855. if not is_assigned(arg):
  856. assign(arg, value)
  857. if varkw:
  858. assign(varkw, named)
  859. elif named:
  860. unexpected = next(iter(named))
  861. if isinstance(unexpected, unicode):
  862. unexpected = unexpected.encode(sys.getdefaultencoding(), 'replace')
  863. raise TypeError("%s() got an unexpected keyword argument '%s'" %
  864. (f_name, unexpected))
  865. unassigned = num_args - len([arg for arg in args if is_assigned(arg)])
  866. if unassigned:
  867. num_required = num_args - num_defaults
  868. raise TypeError('%s() takes %s %d %s (%d given)' % (
  869. f_name, 'at least' if defaults else 'exactly', num_required,
  870. 'arguments' if num_required > 1 else 'argument', num_total))
  871. return arg2value
  872. # -------------------------------------------------- stack frame extraction
  873. Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
  874. def getframeinfo(frame, context=1):
  875. """Get information about a frame or traceback object.
  876. A tuple of five things is returned: the filename, the line number of
  877. the current line, the function name, a list of lines of context from
  878. the source code, and the index of the current line within that list.
  879. The optional second argument specifies the number of lines of context
  880. to return, which are centered around the current line."""
  881. if istraceback(frame):
  882. lineno = frame.tb_lineno
  883. frame = frame.tb_frame
  884. else:
  885. lineno = frame.f_lineno
  886. if not isframe(frame):
  887. raise TypeError('{!r} is not a frame or traceback object'.format(frame))
  888. filename = getsourcefile(frame) or getfile(frame)
  889. if context > 0:
  890. start = lineno - 1 - context//2
  891. try:
  892. lines, lnum = findsource(frame)
  893. except IOError:
  894. lines = index = None
  895. else:
  896. start = max(start, 1)
  897. start = max(0, min(start, len(lines) - context))
  898. lines = lines[start:start+context]
  899. index = lineno - 1 - start
  900. else:
  901. lines = index = None
  902. return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
  903. def getlineno(frame):
  904. """Get the line number from a frame object, allowing for optimization."""
  905. # FrameType.f_lineno is now a descriptor that grovels co_lnotab
  906. return frame.f_lineno
  907. def getouterframes(frame, context=1):
  908. """Get a list of records for a frame and all higher (calling) frames.
  909. Each record contains a frame object, filename, line number, function
  910. name, a list of lines of context, and index within the context."""
  911. framelist = []
  912. while frame:
  913. framelist.append((frame,) + getframeinfo(frame, context))
  914. frame = frame.f_back
  915. return framelist
  916. def getinnerframes(tb, context=1):
  917. """Get a list of records for a traceback's frame and all lower frames.
  918. Each record contains a frame object, filename, line number, function
  919. name, a list of lines of context, and index within the context."""
  920. framelist = []
  921. while tb:
  922. framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
  923. tb = tb.tb_next
  924. return framelist
  925. if hasattr(sys, '_getframe'):
  926. currentframe = sys._getframe
  927. else:
  928. currentframe = lambda _=None: None
  929. def stack(context=1):
  930. """Return a list of records for the stack above the caller's frame."""
  931. return getouterframes(sys._getframe(1), context)
  932. def trace(context=1):
  933. """Return a list of records for the stack below the current exception."""
  934. return getinnerframes(sys.exc_info()[2], context)