mirror of
https://github.com/python/cpython.git
synced 2025-08-11 12:29:34 +00:00

svn+ssh://pythondev@svn.python.org/python/trunk ................ r67654 | georg.brandl | 2008-12-07 16:42:09 -0600 (Sun, 07 Dec 2008) | 2 lines #4457: rewrite __import__() documentation. ................ r67676 | benjamin.peterson | 2008-12-08 20:03:03 -0600 (Mon, 08 Dec 2008) | 1 line specify how things are copied ................ r67677 | benjamin.peterson | 2008-12-08 20:05:11 -0600 (Mon, 08 Dec 2008) | 1 line revert unrelated change to installer script ................ r67681 | jeremy.hylton | 2008-12-09 15:03:10 -0600 (Tue, 09 Dec 2008) | 2 lines Add simple unittests for Request ................ r67692 | amaury.forgeotdarc | 2008-12-10 18:03:42 -0600 (Wed, 10 Dec 2008) | 2 lines #1030250: correctly pass the dry_run option to the mkpath() function. ................ r67725 | benjamin.peterson | 2008-12-12 22:02:20 -0600 (Fri, 12 Dec 2008) | 1 line fix incorrect example ................ r67746 | antoine.pitrou | 2008-12-13 17:12:30 -0600 (Sat, 13 Dec 2008) | 3 lines Issue #4163: Use unicode-friendly word splitting in the textwrap functions when given an unicode string. ................ r67748 | benjamin.peterson | 2008-12-13 19:46:11 -0600 (Sat, 13 Dec 2008) | 1 line remove has_key usage ................ r67761 | benjamin.peterson | 2008-12-14 11:26:04 -0600 (Sun, 14 Dec 2008) | 1 line fix missing bracket ................ r67784 | georg.brandl | 2008-12-15 02:33:58 -0600 (Mon, 15 Dec 2008) | 2 lines #4446: document "platforms" argument for setup(). ................ r67785 | georg.brandl | 2008-12-15 02:36:11 -0600 (Mon, 15 Dec 2008) | 2 lines #4611: fix typo. ................ r67787 | georg.brandl | 2008-12-15 02:58:59 -0600 (Mon, 15 Dec 2008) | 2 lines #4578: fix has_key() usage in compiler package. ................ r67788 | georg.brandl | 2008-12-15 03:07:39 -0600 (Mon, 15 Dec 2008) | 2 lines #4568: remove limitation in varargs callback example. ................ r67802 | amaury.forgeotdarc | 2008-12-15 16:29:14 -0600 (Mon, 15 Dec 2008) | 4 lines #3632: the "pyo" macro from gdbinit can now run when the GIL is released. Patch by haypo. ................ r67832 | antoine.pitrou | 2008-12-17 16:46:54 -0600 (Wed, 17 Dec 2008) | 4 lines Issue #2467: gc.DEBUG_STATS reports invalid elapsed times. Patch by Neil Schemenauer, very slightly modified. ................ r67848 | benjamin.peterson | 2008-12-18 20:28:56 -0600 (Thu, 18 Dec 2008) | 1 line fix typo ................ r67849 | benjamin.peterson | 2008-12-18 20:31:35 -0600 (Thu, 18 Dec 2008) | 1 line _call_method -> _callmethod and _get_value to _getvalue ................ r67859 | amaury.forgeotdarc | 2008-12-19 16:56:48 -0600 (Fri, 19 Dec 2008) | 4 lines #4700: crtlicense.txt is displayed by the license() command and should be kept ascii-only. Will port to 3.0 ................ r67862 | benjamin.peterson | 2008-12-19 20:48:02 -0600 (Fri, 19 Dec 2008) | 1 line copy sentence from docstring ................ r67863 | benjamin.peterson | 2008-12-19 20:51:26 -0600 (Fri, 19 Dec 2008) | 1 line add headings ................ r67864 | benjamin.peterson | 2008-12-19 20:57:19 -0600 (Fri, 19 Dec 2008) | 1 line beef up docstring ................ r67880 | benjamin.peterson | 2008-12-20 16:49:24 -0600 (Sat, 20 Dec 2008) | 1 line remove redundant sentence ................ r67882 | benjamin.peterson | 2008-12-20 16:59:49 -0600 (Sat, 20 Dec 2008) | 1 line add some recent releases to the list ................ r67885 | benjamin.peterson | 2008-12-20 17:48:54 -0600 (Sat, 20 Dec 2008) | 1 line silence annoying DeprecationWarning ................ r67889 | benjamin.peterson | 2008-12-20 19:04:32 -0600 (Sat, 20 Dec 2008) | 1 line sphinx.web is long gone ................ r67890 | benjamin.peterson | 2008-12-20 19:12:26 -0600 (Sat, 20 Dec 2008) | 1 line update readme ................ r67891 | benjamin.peterson | 2008-12-20 19:14:47 -0600 (Sat, 20 Dec 2008) | 1 line there are way too many places which need to have the current version added ................ r67892 | benjamin.peterson | 2008-12-20 19:29:32 -0600 (Sat, 20 Dec 2008) | 9 lines Merged revisions 67809 via svnmerge from svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3 ........ r67809 | benjamin.peterson | 2008-12-15 21:54:45 -0600 (Mon, 15 Dec 2008) | 1 line fix logic error ........ ................ r67895 | neal.norwitz | 2008-12-21 08:28:32 -0600 (Sun, 21 Dec 2008) | 2 lines Add Tarek for work on distutils. ................
113 lines
3.8 KiB
Python
113 lines
3.8 KiB
Python
from compiler import ast
|
|
|
|
# XXX should probably rename ASTVisitor to ASTWalker
|
|
# XXX can it be made even more generic?
|
|
|
|
class ASTVisitor:
|
|
"""Performs a depth-first walk of the AST
|
|
|
|
The ASTVisitor will walk the AST, performing either a preorder or
|
|
postorder traversal depending on which method is called.
|
|
|
|
methods:
|
|
preorder(tree, visitor)
|
|
postorder(tree, visitor)
|
|
tree: an instance of ast.Node
|
|
visitor: an instance with visitXXX methods
|
|
|
|
The ASTVisitor is responsible for walking over the tree in the
|
|
correct order. For each node, it checks the visitor argument for
|
|
a method named 'visitNodeType' where NodeType is the name of the
|
|
node's class, e.g. Class. If the method exists, it is called
|
|
with the node as its sole argument.
|
|
|
|
The visitor method for a particular node type can control how
|
|
child nodes are visited during a preorder walk. (It can't control
|
|
the order during a postorder walk, because it is called _after_
|
|
the walk has occurred.) The ASTVisitor modifies the visitor
|
|
argument by adding a visit method to the visitor; this method can
|
|
be used to visit a child node of arbitrary type.
|
|
"""
|
|
|
|
VERBOSE = 0
|
|
|
|
def __init__(self):
|
|
self.node = None
|
|
self._cache = {}
|
|
|
|
def default(self, node, *args):
|
|
for child in node.getChildNodes():
|
|
self.dispatch(child, *args)
|
|
|
|
def dispatch(self, node, *args):
|
|
self.node = node
|
|
klass = node.__class__
|
|
meth = self._cache.get(klass, None)
|
|
if meth is None:
|
|
className = klass.__name__
|
|
meth = getattr(self.visitor, 'visit' + className, self.default)
|
|
self._cache[klass] = meth
|
|
## if self.VERBOSE > 0:
|
|
## className = klass.__name__
|
|
## if self.VERBOSE == 1:
|
|
## if meth == 0:
|
|
## print "dispatch", className
|
|
## else:
|
|
## print "dispatch", className, (meth and meth.__name__ or '')
|
|
return meth(node, *args)
|
|
|
|
def preorder(self, tree, visitor, *args):
|
|
"""Do preorder walk of tree using visitor"""
|
|
self.visitor = visitor
|
|
visitor.visit = self.dispatch
|
|
self.dispatch(tree, *args) # XXX *args make sense?
|
|
|
|
class ExampleASTVisitor(ASTVisitor):
|
|
"""Prints examples of the nodes that aren't visited
|
|
|
|
This visitor-driver is only useful for development, when it's
|
|
helpful to develop a visitor incrementally, and get feedback on what
|
|
you still have to do.
|
|
"""
|
|
examples = {}
|
|
|
|
def dispatch(self, node, *args):
|
|
self.node = node
|
|
meth = self._cache.get(node.__class__, None)
|
|
className = node.__class__.__name__
|
|
if meth is None:
|
|
meth = getattr(self.visitor, 'visit' + className, 0)
|
|
self._cache[node.__class__] = meth
|
|
if self.VERBOSE > 1:
|
|
print "dispatch", className, (meth and meth.__name__ or '')
|
|
if meth:
|
|
meth(node, *args)
|
|
elif self.VERBOSE > 0:
|
|
klass = node.__class__
|
|
if klass not in self.examples:
|
|
self.examples[klass] = klass
|
|
print
|
|
print self.visitor
|
|
print klass
|
|
for attr in dir(node):
|
|
if attr[0] != '_':
|
|
print "\t", "%-12.12s" % attr, getattr(node, attr)
|
|
print
|
|
return self.default(node, *args)
|
|
|
|
# XXX this is an API change
|
|
|
|
_walker = ASTVisitor
|
|
def walk(tree, visitor, walker=None, verbose=None):
|
|
if walker is None:
|
|
walker = _walker()
|
|
if verbose is not None:
|
|
walker.VERBOSE = verbose
|
|
walker.preorder(tree, visitor)
|
|
return walker.visitor
|
|
|
|
def dumpNode(node):
|
|
print node.__class__
|
|
for attr in dir(node):
|
|
if attr[0] != '_':
|
|
print "\t", "%-10.10s" % attr, getattr(node, attr)
|