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

ac at codespeak.net ac at codespeak.net
Tue Sep 20 11:55:48 CEST 2005


Author: ac
Date: Tue Sep 20 11:55:48 2005
New Revision: 17682

Modified:
   pypy/dist/pypy/interpreter/astcompiler/pycodegen.py
   pypy/dist/pypy/interpreter/test/test_compiler.py
Log:
Detect manipulation of None and __debug__

Modified: pypy/dist/pypy/interpreter/astcompiler/pycodegen.py
==============================================================================
--- pypy/dist/pypy/interpreter/astcompiler/pycodegen.py	(original)
+++ pypy/dist/pypy/interpreter/astcompiler/pycodegen.py	Tue Sep 20 11:55:48 2005
@@ -214,8 +214,9 @@
         raise RuntimeError, "should be implemented by subclasses"
 
     # Next five methods handle name access
-
     def storeName(self, name):
+        if name in ('None', '__debug__'):
+            raise SyntaxError('assignment to %s is not allowed' % name)
         self._nameOp('STORE', name)
 
     def loadName(self, name):
@@ -228,6 +229,8 @@
         self._nameOp('LOAD', name)
 
     def delName(self, name):
+        if name in ('None', '__debug__'):
+            raise SyntaxError('deleting %s is not allowed' % name)
         scope = self.scope.check_name(name)
         if scope == SC_CELL:
             raise SyntaxError("can not delete variable '%s' "
@@ -895,8 +898,12 @@
     def visitAssAttr(self, node):
         node.expr.accept( self )
         if node.flags == 'OP_ASSIGN':
+            if node.attrname  == 'None':
+                raise SyntaxError('assignment to None is not allowed')
             self.emitop('STORE_ATTR', self.mangle(node.attrname))
         elif node.flags == 'OP_DELETE':
+            if node.attrname == 'None':
+                raise SyntaxError('deleting None is not allowed')
             self.emitop('DELETE_ATTR', self.mangle(node.attrname))
         else:
             assert False, "visitAssAttr unexpected flags: %s" % node.flags            
@@ -1234,6 +1241,8 @@
                     if name in argnames:
                         raise SyntaxError("duplicate argument '%s' in function definition" % arg.name)
                     argnames[name] = 1
+        if 'None' in argnames:
+            raise SyntaxError('assignment to None is not allowed')
 
         args, hasTupleArg = generateArgList(func.argnames)
 
@@ -1279,7 +1288,7 @@
         
         for elt in tup.nodes:
             if isinstance(elt, ast.AssName):
-                self._nameOp('STORE', elt.name)
+                self.storeName(elt.name)
             elif isinstance(elt, ast.AssTuple):
                 self.unpackSequence( elt )
             else:

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	Tue Sep 20 11:55:48 2005
@@ -5,7 +5,6 @@
 from pypy.interpreter.pycode import PyCode
 from pypy.interpreter.error import OperationError
 
-
 class BaseTestCompiler:
     def setup_method(self, method):
         self.compiler = self.space.createcompiler()
@@ -200,6 +199,33 @@
         ex = e.value
         ex.normalize_exception(self.space)
         assert ex.match(self.space, self.space.w_SyntaxError)
+        
+    def test_debug_assignment(self):
+        code = '__debug__ = 1'
+        e = py.test.raises(OperationError, self.compiler.compile, code, '', 'single', 0)
+        ex = e.value
+        ex.normalize_exception(self.space)
+        assert ex.match(self.space, self.space.w_SyntaxError)
+
+    def test_none_assignment(self):
+        stmts = [
+            'None = 0',
+            'None += 0',
+            '__builtins__.None = 0',
+            'def None(): pass',
+            'class None: pass',
+            '(a, None) = 0, 0',
+            'for None in range(10): pass',
+            'def f(None): pass',
+        ]
+        for stmt in stmts:
+            stmt += '\n'
+            for kind in 'single', 'exec':
+                e = py.test.raises(OperationError, self.compiler.compile, stmt,
+                               '', kind, 0)
+                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