[pypy-svn] r68706 - in pypy/trunk/pypy/interpreter/astcompiler: . test

benjamin at codespeak.net benjamin at codespeak.net
Thu Oct 22 01:55:36 CEST 2009


Author: benjamin
Date: Thu Oct 22 01:55:35 2009
New Revision: 68706

Modified:
   pypy/trunk/pypy/interpreter/astcompiler/codegen.py
   pypy/trunk/pypy/interpreter/astcompiler/test/test_compiler.py
Log:
prevent a LOAD_CONST from being generated for function docstrings

Modified: pypy/trunk/pypy/interpreter/astcompiler/codegen.py
==============================================================================
--- pypy/trunk/pypy/interpreter/astcompiler/codegen.py	(original)
+++ pypy/trunk/pypy/interpreter/astcompiler/codegen.py	Thu Oct 22 01:55:35 2009
@@ -1264,9 +1264,11 @@
         assert isinstance(func, ast.FunctionDef)
         # If there's a docstring, store it as the first constant.
         if self.is_docstring(func.body[0]):
-            doc_string = func.body[0]
-            assert isinstance(doc_string, ast.Expr)
-            doc_string.value.walkabout(self)
+            doc_expr = func.body[0]
+            assert isinstance(doc_expr, ast.Expr)
+            doc_str = doc_expr.value
+            assert isinstance(doc_str, ast.Str)
+            self.add_const(doc_str.s)
             start = 1
         else:
             self.add_const(self.space.w_None)

Modified: pypy/trunk/pypy/interpreter/astcompiler/test/test_compiler.py
==============================================================================
--- pypy/trunk/pypy/interpreter/astcompiler/test/test_compiler.py	(original)
+++ pypy/trunk/pypy/interpreter/astcompiler/test/test_compiler.py	Thu Oct 22 01:55:35 2009
@@ -725,7 +725,20 @@
         yield self.st, test, "X.__name__", "X"
 
 
-class AppTestPrint:
+class AppTestCompiler:
+
+    def test_docstring_not_loaded(self):
+        import StringIO, dis, sys
+        ns = {}
+        exec "def f():\n    'hi'" in ns
+        f = ns["f"]
+        save = sys.stdout
+        sys.stdout = output = StringIO.StringIO()
+        try:
+            dis.dis(f)
+        finally:
+            sys.stdout = save
+        assert "0 ('hi')" not in output.getvalue()
 
     def test_print_to(self):
          exec """from StringIO import StringIO



More information about the Pypy-commit mailing list