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

ac at codespeak.net ac at codespeak.net
Mon Sep 19 18:02:30 CEST 2005


Author: ac
Date: Mon Sep 19 18:02:30 2005
New Revision: 17664

Modified:
   pypy/dist/pypy/interpreter/pycompiler.py
   pypy/dist/pypy/interpreter/pyparser/astbuilder.py
   pypy/dist/pypy/interpreter/test/test_compiler.py
Log:
Detect non-default arguments after default arguments.

Modified: pypy/dist/pypy/interpreter/pycompiler.py
==============================================================================
--- pypy/dist/pypy/interpreter/pycompiler.py	(original)
+++ pypy/dist/pypy/interpreter/pycompiler.py	Mon Sep 19 18:02:30 2005
@@ -413,6 +413,9 @@
         except ParseError, e:
             raise OperationError(space.w_SyntaxError,
                                  e.wrap_info(space, filename))
+        except SyntaxError, e:
+            raise OperationError(space.w_SyntaxError,
+                                 e.wrap_info(space, filename))
 
         try:
             astcompiler.misc.set_filename(filename, ast_tree)

Modified: pypy/dist/pypy/interpreter/pyparser/astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/astbuilder.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/astbuilder.py	Mon Sep 19 18:02:30 2005
@@ -148,12 +148,15 @@
     defaults = []
     names = []
     flags = 0
+    first_with_default = -1
     while index < l:
         cur_token = tokens[index]
         index += 1
         if not isinstance(cur_token, TokenObject):
             # XXX: think of another way to write this test
             defaults.append(cur_token)
+            if first_with_default == -1:
+                first_with_default = len(names) - 1
         elif cur_token.name == tok.COMMA:
             # We could skip test COMMA by incrementing index cleverly
             # but we might do some experiment on the grammar at some point
@@ -198,6 +201,15 @@
         elif cur_token.name == tok.NAME:
             val = cur_token.get_value()
             names.append( ast.AssName( val, consts.OP_ASSIGN ) )
+
+    if first_with_default != -1:
+        num_expected_with_default = len(names) - first_with_default
+        if flags & consts.CO_VARKEYWORDS:
+            num_expected_with_default -= 1
+        if flags & consts.CO_VARARGS:
+            num_expected_with_default -= 1
+        if len(defaults) != num_expected_with_default:
+            raise SyntaxError('non-default argument follows default argument')
     return names, defaults, flags
 
 

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 18:02:30 2005
@@ -194,7 +194,12 @@
             ex.normalize_exception(self.space)
             assert ex.match(self.space, self.space.w_SyntaxError)
 
-
+    def test_argument_order(self):
+        code = 'def f(a=1, (b, c)): pass'
+        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):



More information about the Pypy-commit mailing list