[Python-checkins] r53576 - in python/branches/release25-maint: Lib/compiler/pycodegen.py Lib/compiler/transformer.py Lib/test/test_compiler.py Misc/NEWS

georg.brandl python-checkins at python.org
Sat Jan 27 18:43:07 CET 2007


Author: georg.brandl
Date: Sat Jan 27 18:43:07 2007
New Revision: 53576

Modified:
   python/branches/release25-maint/Lib/compiler/pycodegen.py
   python/branches/release25-maint/Lib/compiler/transformer.py
   python/branches/release25-maint/Lib/test/test_compiler.py
   python/branches/release25-maint/Misc/NEWS
Log:
Patch #1638243: the compiler package is now able to correctly compile
a with statement; previously, executing code containing a with statement
compiled by the compiler package crashed the interpreter.
 (backport from rev. 53575)

Modified: python/branches/release25-maint/Lib/compiler/pycodegen.py
==============================================================================
--- python/branches/release25-maint/Lib/compiler/pycodegen.py	(original)
+++ python/branches/release25-maint/Lib/compiler/pycodegen.py	Sat Jan 27 18:43:07 2007
@@ -851,6 +851,8 @@
         self.emit('LOAD_CONST', None)
         self.nextBlock(final)
         self.setups.push((END_FINALLY, final))
+        self._implicitNameOp('LOAD', exitvar)
+        self._implicitNameOp('DELETE', exitvar)
         self.emit('WITH_CLEANUP')
         self.emit('END_FINALLY')
         self.setups.pop()

Modified: python/branches/release25-maint/Lib/compiler/transformer.py
==============================================================================
--- python/branches/release25-maint/Lib/compiler/transformer.py	(original)
+++ python/branches/release25-maint/Lib/compiler/transformer.py	Sat Jan 27 18:43:07 2007
@@ -960,7 +960,7 @@
         if nodelist[2][0] == token.COLON:
             var = None
         else:
-            var = self.com_node(nodelist[2])
+            var = self.com_assign(nodelist[2][2], OP_ASSIGN)
         return With(expr, var, body, lineno=nodelist[0][2])
 
     def com_with_var(self, nodelist):

Modified: python/branches/release25-maint/Lib/test/test_compiler.py
==============================================================================
--- python/branches/release25-maint/Lib/test/test_compiler.py	(original)
+++ python/branches/release25-maint/Lib/test/test_compiler.py	Sat Jan 27 18:43:07 2007
@@ -7,6 +7,12 @@
 # How much time in seconds can pass before we print a 'Still working' message.
 _PRINT_WORKING_MSG_INTERVAL = 5 * 60
 
+class TrivialContext(object):
+    def __enter__(self):
+        return self
+    def __exit__(self, *exc_info):
+        pass
+
 class CompilerTest(unittest.TestCase):
 
     def testCompileLibrary(self):
@@ -123,6 +129,31 @@
                              'eval')
         self.assertEquals(eval(c), [(0, 3), (1, 3), (2, 3)])
 
+    def testWith(self):
+        # SF bug 1638243
+        c = compiler.compile('from __future__ import with_statement\n'
+                             'def f():\n'
+                             '    with TrivialContext():\n'
+                             '        return 1\n'
+                             'result = f()',
+                             '<string>',
+                             'exec' )
+        dct = {'TrivialContext': TrivialContext}
+        exec c in dct
+        self.assertEquals(dct.get('result'), 1)
+
+    def testWithAss(self):
+        c = compiler.compile('from __future__ import with_statement\n'
+                             'def f():\n'
+                             '    with TrivialContext() as tc:\n'
+                             '        return 1\n'
+                             'result = f()',
+                             '<string>',
+                             'exec' )
+        dct = {'TrivialContext': TrivialContext}
+        exec c in dct
+        self.assertEquals(dct.get('result'), 1)
+
 
 NOLINENO = (compiler.ast.Module, compiler.ast.Stmt, compiler.ast.Discard)
 

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Sat Jan 27 18:43:07 2007
@@ -154,6 +154,10 @@
 Library
 -------
 
+- Patch #1638243: the compiler package is now able to correctly compile
+  a with statement; previously, executing code containing a with statement
+  compiled by the compiler package crashed the interpreter.
+
 - Bug #1643943: Fix %U handling for time.strptime.
 
 - Patch #1643874: memory leak in ctypes fixed.


More information about the Python-checkins mailing list