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

adim at codespeak.net adim at codespeak.net
Thu Aug 11 18:10:13 CEST 2005


Author: adim
Date: Thu Aug 11 18:10:10 2005
New Revision: 15969

Added:
   pypy/dist/pypy/interpreter/pyparser/test/samples/snippet_decorators.py
Modified:
   pypy/dist/pypy/interpreter/pyparser/astbuilder.py
   pypy/dist/pypy/interpreter/pyparser/test/test_astbuilder.py
Log:
implemented decorators

Modified: pypy/dist/pypy/interpreter/pyparser/astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/astbuilder.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/astbuilder.py	Thu Aug 11 18:10:10 2005
@@ -346,7 +346,12 @@
     
     and returns an ast node : ast.Getattr(Getattr(Name('a'), 'b'), 'c' ...)
     """
-    result = tokens[0]
+    token = tokens[0]
+    # XXX HACK for when parse_attraccess is called from build_decorator
+    if isinstance(token, TokenObject):
+        result = ast.Name(token.value)
+    else:
+        result = token
     index = 1
     while index < len(tokens):
         token = tokens[index]
@@ -800,10 +805,35 @@
         builder.push(ast.List(nodes))
     
 
+def build_decorator(builder, nb):
+    """decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE"""
+    L = get_atoms(builder, nb)
+    print "***** decorator", L
+    nodes = []
+    # remove '@', '(' and ')' from L and use parse_attraccess
+    for token in L[1:]:
+        if isinstance(token, TokenObject) and \
+               token.name in (tok.LPAR, tok.RPAR, tok.NEWLINE):
+            # skip those ones
+            continue
+        else:
+            nodes.append(token)
+    obj = parse_attraccess(nodes)
+    builder.push(obj)
+
 def build_funcdef(builder, nb):
     """funcdef: [decorators] 'def' NAME parameters ':' suite
     """
     L = get_atoms(builder, nb)
+    index = 0
+    decorators = []
+    decorator_node = None
+    while not (isinstance(L[index], TokenObject) and L[index].value == 'def'):
+        decorators.append(L[index])
+        index += 1
+    if decorators:
+        decorator_node = ast.Decorators(decorators)
+    L = L[index:]
     funcname = L[1]
     arglist = []
     index = 3
@@ -818,7 +848,7 @@
     code = L[-1]
     doc = get_docstring(code)
     # FIXME: decorators and docstring !
-    builder.push(ast.Function(None, funcname, names, default, flags, doc, code))
+    builder.push(ast.Function(decorator_node, funcname, names, default, flags, doc, code))
 
 
 def build_classdef(builder, nb):
@@ -1178,6 +1208,7 @@
     sym.raise_stmt : build_raise_stmt,
     sym.try_stmt : build_try_stmt,
     sym.exprlist : build_exprlist,
+    sym.decorator : build_decorator,
     # sym.fplist : build_fplist,
     }
 

Added: pypy/dist/pypy/interpreter/pyparser/test/samples/snippet_decorators.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/interpreter/pyparser/test/samples/snippet_decorators.py	Thu Aug 11 18:10:10 2005
@@ -0,0 +1,18 @@
+# Function(Decorators([Name('foo')]), 'f', ['a', 'b'], [], 0, None, Stmt([Pass()]))
+ at foo
+def f(a, b):
+    pass
+
+ at accepts(int, (int,float))
+ at returns((int,float))
+def func(arg1, arg2):
+    return arg1 * arg2
+
+
+## Stmt([Function(Decorators([CallFunc(Getattr(Getattr(Name('mod1'), 'mod2'), 'accepts'), [Name('int'), Tuple([Name('int'), Name('float')])], None, None),
+##                            CallFunc(Getattr(Getattr(Name('mod1'), 'mod2'), 'returns'), [Tuple([Name('int'), Name('float')])], None, None)]),
+##                'func', ['arg1', 'arg2'], [], 0, None, Stmt([Return(Mul((Name('arg1'), Name('arg2'))))]))])
+ at mod1.mod2.accepts(int, (int,float))
+ at mod1.mod2.returns((int,float))
+def func(arg1, arg2):
+    return arg1 * arg2

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 Aug 11 18:10:10 2005
@@ -437,6 +437,7 @@
     'snippet_slice.py',
     'snippet_whitespaces.py',
     'snippet_samples.py',
+    'snippet_decorators.py',
     ]
 
 def test_snippets():



More information about the Pypy-commit mailing list