[pypy-commit] pypy default: Issue #1963 fix

arigo noreply at buildbot.pypy.org
Sat Jan 17 14:37:19 CET 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r75411:abdf46d950ba
Date: 2015-01-17 14:37 +0100
http://bitbucket.org/pypy/pypy/changeset/abdf46d950ba/

Log:	Issue #1963 fix

diff --git a/pypy/interpreter/astcompiler/optimize.py b/pypy/interpreter/astcompiler/optimize.py
--- a/pypy/interpreter/astcompiler/optimize.py
+++ b/pypy/interpreter/astcompiler/optimize.py
@@ -254,11 +254,15 @@
         return rep
 
     def visit_Name(self, name):
-        # Turn loading None into a constant lookup.  Eventaully, we can do this
-        # for True and False, too.
+        # Turn loading None into a constant lookup.  We cannot do this
+        # for True and False, because rebinding them is allowed (2.7).
         if name.id == "None":
-            assert name.ctx == ast.Load
-            return ast.Const(self.space.w_None, name.lineno, name.col_offset)
+            # The compiler refuses to parse "None = ...", but "del None"
+            # is allowed (if pointless).  Check anyway: custom asts that
+            # correspond to "None = ..." can be made by hand.
+            if name.ctx == ast.Load:
+                return ast.Const(self.space.w_None, name.lineno,
+                                 name.col_offset)
         return name
 
     def visit_Tuple(self, tup):
diff --git a/pypy/interpreter/test/test_compiler.py b/pypy/interpreter/test/test_compiler.py
--- a/pypy/interpreter/test/test_compiler.py
+++ b/pypy/interpreter/test/test_compiler.py
@@ -654,6 +654,18 @@
         assert ex.match(space, space.w_SyntaxError)
         assert 'hello_world' in space.str_w(space.str(ex.get_w_value(space)))
 
+    def test_del_None(self):
+        snippet = '''if 1:
+            try:
+                del None
+            except NameError:
+                pass
+        '''
+        code = self.compiler.compile(snippet, '<tmp>', 'exec', 0)
+        space = self.space
+        w_d = space.newdict()
+        space.exec_(code, w_d, w_d)
+
 
 class TestPythonAstCompiler_25_grammar(BaseTestCompiler):
     def setup_method(self, method):


More information about the pypy-commit mailing list