From: <pj...@us...> - 2008-10-26 21:13:54
|
Revision: 5513 http://jython.svn.sourceforge.net/jython/?rev=5513&view=rev Author: pjenvey Date: 2008-10-26 21:13:51 +0000 (Sun, 26 Oct 2008) Log Message: ----------- workaround Jython ASTisms Modified Paths: -------------- trunk/jython/Lib/ast.py trunk/jython/Lib/test/test_ast.py Modified: trunk/jython/Lib/ast.py =================================================================== --- trunk/jython/Lib/ast.py 2008-10-26 19:55:08 UTC (rev 5512) +++ trunk/jython/Lib/ast.py 2008-10-26 21:13:51 UTC (rev 5513) @@ -25,10 +25,35 @@ :copyright: Copyright 2008 by Armin Ronacher. :license: Python License. """ +import sys from _ast import * from _ast import __version__ +if sys.platform.startswith('java'): + import array + ast_list = array.ArrayType + + def get_class_name(t): + result = t.__class__.__name__ + if result in ("expr_contextType", + "boolopType", + "unaryopType", + "cmpopType", + "operatorType"): + result = str(t) + if result == "AugLoad": + result = "Load" + elif result == "AugStore": + result = "Store" + elif result.endswith("Type"): + result = result[:-4] + return result +else: + ast_list = list + get_class_name = lambda node: node.__class__.__name__ + + def parse(expr, filename='<unknown>', mode='exec'): """ Parse an expression into an AST node. @@ -80,7 +105,7 @@ def _format(node): if isinstance(node, AST): fields = [(a, _format(b)) for a, b in iter_fields(node)] - rv = '%s(%s' % (node.__class__.__name__, ', '.join( + rv = '%s(%s' % (get_class_name(node), ', '.join( ('%s=%s' % field for field in fields) if annotate_fields else (b for a, b in fields) @@ -90,11 +115,11 @@ rv += ', '.join('%s=%s' % (a, _format(getattr(node, a))) for a in node._attributes) return rv + ')' - elif isinstance(node, list): + elif isinstance(node, ast_list): return '[%s]' % ', '.join(_format(x) for x in node) return repr(node) if not isinstance(node, AST): - raise TypeError('expected AST, got %r' % node.__class__.__name__) + raise TypeError('expected AST, got %r' % get_class_name(node)) return _format(node) @@ -168,7 +193,7 @@ for name, field in iter_fields(node): if isinstance(field, AST): yield field - elif isinstance(field, list): + elif isinstance(field, ast_list): for item in field: if isinstance(item, AST): yield item @@ -181,7 +206,7 @@ will be raised. """ if not isinstance(node, (FunctionDef, ClassDef, Module)): - raise TypeError("%r can't have docstrings" % node.__class__.__name__) + raise TypeError("%r can't have docstrings" % get_class_name(node)) if node.body and isinstance(node.body[0], Expr) and \ isinstance(node.body[0].value, Str): if clean: @@ -226,14 +251,14 @@ def visit(self, node): """Visit a node.""" - method = 'visit_' + node.__class__.__name__ + method = 'visit_' + get_class_name(node) visitor = getattr(self, method, self.generic_visit) return visitor(node) def generic_visit(self, node): """Called if no explicit visitor function exists for a node.""" for field, value in iter_fields(node): - if isinstance(value, list): + if isinstance(value, ast_list): for item in value: if isinstance(item, AST): self.visit(item) @@ -280,7 +305,7 @@ def generic_visit(self, node): for field, old_value in iter_fields(node): old_value = getattr(node, field, None) - if isinstance(old_value, list): + if isinstance(old_value, ast_list): new_values = [] for value in old_value: if isinstance(value, AST): Modified: trunk/jython/Lib/test/test_ast.py =================================================================== --- trunk/jython/Lib/test/test_ast.py 2008-10-26 19:55:08 UTC (rev 5512) +++ trunk/jython/Lib/test/test_ast.py 2008-10-26 21:13:51 UTC (rev 5513) @@ -1,6 +1,6 @@ #Taken and modified from CPython's release25-maint branch, revision 62446. import sys,os, itertools -import _ast +import ast def get_class_name(t): result = t.__class__.__name__ @@ -146,11 +146,11 @@ def test_order(ast_node, parent_pos): - if (not isinstance(ast_node, _ast.AST) + if (not isinstance(ast_node, ast.AST) or not hasattr(ast_node, '_fields') or ast_node._fields == None): return - if isinstance(ast_node, (_ast.expr, _ast.stmt, _ast.excepthandler)): + if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)): node_pos = (ast_node.lineno, ast_node.col_offset) assert node_pos >= parent_pos, (node_pos, parent_pos) parent_pos = (ast_node.lineno, ast_node.col_offset) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |