[pypy-svn] r17661 - in pypy/dist/pypy/interpreter: astcompiler test

ac at codespeak.net ac at codespeak.net
Mon Sep 19 16:19:11 CEST 2005


Author: ac
Date: Mon Sep 19 16:19:11 2005
New Revision: 17661

Modified:
   pypy/dist/pypy/interpreter/astcompiler/pycodegen.py
   pypy/dist/pypy/interpreter/test/test_compiler.py
Log:
Detect duplicate arguments

Modified: pypy/dist/pypy/interpreter/astcompiler/pycodegen.py
==============================================================================
--- pypy/dist/pypy/interpreter/astcompiler/pycodegen.py	(original)
+++ pypy/dist/pypy/interpreter/astcompiler/pycodegen.py	Mon Sep 19 16:19:11 2005
@@ -1212,7 +1212,7 @@
         # name.
         node.expr.accept( self )
         self.emit('PRINT_EXPR')
-
+        
 class AbstractFunctionCode(CodeGenerator):
     def __init__(self, space, func, isLambda, class_name, mod):
         self.class_name = class_name
@@ -1222,8 +1222,21 @@
         else:
             assert isinstance(func, ast.Function)
             name = func.name
+        # Find duplicated arguments.
+        argnames = {}
+        for arg in func.argnames:
+            if isinstance(arg, ast.AssName):
+                if arg.name in argnames:
+                    raise SyntaxError("duplicate argument '%s' in function definition" % arg.name)
+                argnames[arg.name] = 1
+            elif isinstance(arg, ast.AssTuple):
+                for name in arg.getArgNames():
+                    if name in argnames:
+                        raise SyntaxError("duplicate argument '%s' in function definition" % arg.name)
+                    argnames[name] = 1
 
         args, hasTupleArg = generateArgList(func.argnames)
+
         graph = pyassem.PyFlowGraph(space, name, func.filename, args,
                                     optimized=self.localsfullyknown,
                                     newlocals=1)

Modified: pypy/dist/pypy/interpreter/test/test_compiler.py
==============================================================================
--- pypy/dist/pypy/interpreter/test/test_compiler.py	(original)
+++ pypy/dist/pypy/interpreter/test/test_compiler.py	Mon Sep 19 16:19:11 2005
@@ -10,9 +10,9 @@
     def setup_method(self, method):
         self.compiler = self.space.createcompiler()
 
-    def eval_string(self, string):
+    def eval_string(self, string, kind='eval'):
         space = self.space
-        code = self.compiler.compile(string, '<>', 'eval', 0)
+        code = self.compiler.compile(string, '<>', kind, 0)
         return code.exec_code(space, space.newdict([]), space.newdict([]))
 
     def test_compile(self):
@@ -181,6 +181,21 @@
         ex.normalize_exception(self.space)
         assert ex.match(self.space, self.space.w_UnicodeError)
 
+    def test_argument_handling(self):
+        for expr in 'lambda a,a:0', 'lambda a,a=1:0', 'lambda a=1,a=1:0':
+            e = py.test.raises(OperationError, self.eval_string, expr)
+            ex = e.value
+            ex.normalize_exception(self.space)
+            assert ex.match(self.space, self.space.w_SyntaxError)
+
+        for code in 'def f(a, a): pass', 'def f(a = 0, a = 1): pass', 'def f(a): global a; a = 1':
+            e = py.test.raises(OperationError, self.eval_string, code, 'exec')
+            ex = e.value
+            ex.normalize_exception(self.space)
+            assert ex.match(self.space, self.space.w_SyntaxError)
+
+
+
 class TestECCompiler(BaseTestCompiler):
     def setup_method(self, method):
         self.compiler = self.space.getexecutioncontext().compiler



More information about the Pypy-commit mailing list