[pypy-svn] r17396 - in pypy/dist/pypy/interpreter/pyparser: . test

pedronis at codespeak.net pedronis at codespeak.net
Thu Sep 8 23:19:06 CEST 2005


Author: pedronis
Date: Thu Sep  8 23:19:04 2005
New Revision: 17396

Modified:
   pypy/dist/pypy/interpreter/pyparser/astbuilder.py
   pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py
Log:
use parsestring.parsestr in astbuilder, missing encoding support

(disabled temporary eval_string)

activate previously failing tests



Modified: pypy/dist/pypy/interpreter/pyparser/astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/astbuilder.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/astbuilder.py	Thu Sep  8 23:19:04 2005
@@ -8,6 +8,7 @@
 import pypy.interpreter.pyparser.pysymbol as sym
 import pypy.interpreter.pyparser.pytoken as tok
 from pypy.interpreter.pyparser.error import SyntaxError
+from pypy.interpreter.pyparser.parsestring import parsestr
 
 DEBUG_MODE = 0
 
@@ -346,34 +347,34 @@
     atoms.reverse()
     return atoms
     
-def eval_string(value):
-    """temporary implementation
-
-    FIXME: need to be finished (check compile.c (parsestr) and
-    stringobject.c (PyString_DecodeEscape()) for complete implementation)
-    """
-    # return eval(value)
-    if len(value) == 2:
-        return ''
-    result = ''
-    length = len(value)
-    quotetype = value[0]
-    index = 1
-    while index < length and value[index] == quotetype:
-        index += 1
-    if index == 6:
-        # empty strings like """""" or ''''''
-        return ''
-    # XXX: is it RPYTHON to do this value[index:-index]
-    chars = [char for char in value[index:len(value)-index]]
-    result = ''.join(chars)
-    result = result.replace('\\\\', '\\')
-    d = {'\\b' : '\b', '\\f' : '\f', '\\t' : '\t', '\\n' : '\n',
-         '\\r' : '\r', '\\v' : '\v', '\\a' : '\a',
-         }
-    for escaped, value in d.items():
-        result = result.replace(escaped, value)
-    return result
+#def eval_string(value):
+#    """temporary implementation
+#
+#    FIXME: need to be finished (check compile.c (parsestr) and
+#    stringobject.c (PyString_DecodeEscape()) for complete implementation)
+#    """
+#    # return eval(value)
+#    if len(value) == 2:
+#        return ''
+#    result = ''
+#    length = len(value)
+#    quotetype = value[0]
+#    index = 1
+#    while index < length and value[index] == quotetype:
+#        index += 1
+#    if index == 6:
+#        # empty strings like """""" or ''''''
+#        return ''
+#    # XXX: is it RPYTHON to do this value[index:-index]
+#    chars = [char for char in value[index:len(value)-index]]
+#    result = ''.join(chars)
+#    result = result.replace('\\\\', '\\')
+#    d = {'\\b' : '\b', '\\f' : '\f', '\\t' : '\t', '\\n' : '\n',
+#         '\\r' : '\r', '\\v' : '\v', '\\a' : '\a',
+#         }
+#    for escaped, value in d.items():
+#        result = result.replace(escaped, value)
+#    return result
 
 
 ## misc utilities, especially for power: rule
@@ -490,10 +491,19 @@
         elif top.name == tok.STRING:
             # need to concatenate strings in atoms
             s = ''
-            for token in atoms:
+            if len(atoms) == 1:
+                token = atoms[0]
                 assert isinstance(token, TokenObject)
-                s += eval_string(token.get_value())
-            builder.push(ast.Const(builder.wrap_string(s)))
+                builder.push(ast.Const(parsestr(builder.space, None, token.get_value()))) # XXX encoding
+            else:
+                space = builder.space
+                empty = space.wrap('')
+                accum = []
+                for token in atoms:
+                    assert isinstance(token, TokenObject)
+                    accum.append(parsestr(builder.space, None, token.get_value())) # XXX encoding
+                w_s = space.call_method(empty, 'join', space.newlist(accum))
+                builder.push(ast.Const(w_s))
         elif top.name == tok.BACKQUOTE:
             builder.push(ast.Backquote(atoms[1]))
         else:

Modified: pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py	Thu Sep  8 23:19:04 2005
@@ -35,6 +35,7 @@
     if not isinstance(left,stable_ast.Node) or not isinstance(right,ast_ast.Node):
         return left==right
     if left.__class__.__name__ != right.__class__.__name__:
+        print "Node type mismatch:", left, right
         return False    
     if isinstance(left,stable_ast.Function) and isinstance(right,ast_ast.Function):
         left_nodes = list(left.getChildren())
@@ -56,20 +57,24 @@
             return False
     elif isinstance(left,stable_ast.Const):
         if isinstance(right,ast_ast.Const):
-            return left.value == right.value
+            r = left.value == right.value
         elif isinstance(right,ast_ast.NoneConst):
-            return left.value == None
+            r = left.value == None
         elif isinstance(right, ast_ast.NumberConst):
-            return left.value == right.number_value
+            r = left.value == right.number_value
         elif isinstance(right, ast_ast.StringConst):
-            return left.value == right.string_value
+            r = left.value == right.string_value
         else:
             print "Not const type %s" % repr(right)
             return False
+        if not r:
+            print "Constant mismatch:", left, right
+        return True
     else:
         left_nodes = left.getChildren()
         right_nodes = right.getChildren()
     if len(left_nodes)!=len(right_nodes):
+        print "Number of children mismatch:", left, right 
         return False
     for i,j in zip(left_nodes,right_nodes):
         if not nodes_equal(i,j):
@@ -513,6 +518,7 @@
     'x = 5 ',
     '''"""Docstring""";print 1''',
     '''"Docstring"''',
+    '''"Docstring" "\\x00"''',
     ]
 ]
 
@@ -546,9 +552,15 @@
     def type(self, obj):
         return type(obj)
 
+    def newlist(self, lst):
+        return list(lst)
+
     def newtuple(self, lst):
         return tuple(lst)
     
+    def call_method(self, obj, meth, *args):
+        return getattr(obj, meth)(*args)
+
 def ast_parse_expr(expr, target='single'):
     target = TARGET_DICT[target]
     builder = AstBuilder(space=FakeSpace())
@@ -628,7 +640,7 @@
         yield check_expression, source, 'exec'
 
 def test_libstuff():
-    py.test.skip("failing, need to investigate")
+    #py.test.skip("failing, need to investigate")
     for snippet_name in LIBSTUFF:
         filepath = os.path.join(os.path.dirname(__file__), '../../../lib', snippet_name)
         source = file(filepath).read()
@@ -651,13 +663,13 @@
 
 
 def test_eval_string():
-    from pypy.interpreter.pyparser.astbuilder import eval_string
     test = ['""', "''", '""""""', "''''''", "''' '''", '""" """', '"foo"',
             "'foo'", '"""\n"""', '"\\ "', '"\\n"',
-            # '"\""',
+            '"\\""',
+            '"\\x00"',
             ]
     for data in test:
-        assert eval_string(data) == eval(data)
+        yield check_expression, data, 'eval'
 
 def test_single_inputs():
     for family in SINGLE_INPUTS:



More information about the Pypy-commit mailing list