[pypy-svn] r16508 - in pypy/dist/pypy/interpreter: stablecompiler test

arigo at codespeak.net arigo at codespeak.net
Thu Aug 25 17:33:46 CEST 2005


Author: arigo
Date: Thu Aug 25 17:33:45 2005
New Revision: 16508

Modified:
   pypy/dist/pypy/interpreter/stablecompiler/pycodegen.py
   pypy/dist/pypy/interpreter/stablecompiler/transformer.py
   pypy/dist/pypy/interpreter/test/test_compiler.py
Log:
* fixed handling of docstrings in stablecompiler, step 1: don't skip the
  whole first line of a function if there is a docstring (because it can be
  part of a ';'-separated statements-on-a-single-line).

  Instead, to avoid to have the docstring in the bytecode as a
  LOAD_CONST/POP_TOP, we generally skip generating code for any
  statement that is just a single constant.

  There is a remaining failure shown by INPROGRESS_test_toplevel_docstring().

* re-enabled test_getcodeflags(), which checks something that has
  been fixed one or two days ago already.



Modified: pypy/dist/pypy/interpreter/stablecompiler/pycodegen.py
==============================================================================
--- pypy/dist/pypy/interpreter/stablecompiler/pycodegen.py	(original)
+++ pypy/dist/pypy/interpreter/stablecompiler/pycodegen.py	Thu Aug 25 17:33:45 2005
@@ -827,6 +827,11 @@
     # misc
 
     def visitDiscard(self, node):
+        # Important: this function is overridden in InteractiveCodeGenerator,
+        # which also has the effect that the following test only occurs in
+        # non-'single' modes.
+        if isinstance(node.expr, ast.Const):
+            return    # skip LOAD_CONST/POP_TOP pairs (for e.g. docstrings)
         self.set_lineno(node)
         self.visit(node.expr)
         self.emit('POP_TOP')

Modified: pypy/dist/pypy/interpreter/stablecompiler/transformer.py
==============================================================================
--- pypy/dist/pypy/interpreter/stablecompiler/transformer.py	(original)
+++ pypy/dist/pypy/interpreter/stablecompiler/transformer.py	Thu Aug 25 17:33:45 2005
@@ -185,12 +185,8 @@
 
     def file_input(self, nodelist):
         doc = self.get_docstring(nodelist, symbol.file_input)
-        if doc is not None:
-            i = 1
-        else:
-            i = 0
         stmts = []
-        for node in nodelist[i:]:
+        for node in nodelist:
             if node[0] != token.ENDMARKER and node[0] != token.NEWLINE:
                 self.com_append_stmt(stmts, node)
         return Module(doc, Stmt(stmts))

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	Thu Aug 25 17:33:45 2005
@@ -44,7 +44,6 @@
                        'if 1:\n  x x', '?', 'exec', 0)
 
     def test_getcodeflags(self):
-        py.test.skip("flags don't work correctly when using the compiler package")
         code = self.compiler.compile('from __future__ import division\n',
                                      '<hello>', 'exec', 0)
         flags = self.compiler.getcodeflags(code)
@@ -117,6 +116,32 @@
         ex = e.value 
         assert ex.match(self.space, self.space.w_SyntaxError)
 
+    def INPROGRESS_test_toplevel_docstring(self):
+        space = self.space
+        import pdb; pdb.set_trace()
+        code = self.compiler.compile('"spam"; "bar"; x=5', '<hello>', 'exec', 0)
+        assert space.eq_w(code.co_consts_w[0], space.wrap("spam"))
+        w_locals = space.newdict([])
+        code.exec_code(space, space.newdict([]), w_locals)
+        w_x = space.getitem(w_locals, space.wrap('x'))
+        assert space.eq_w(w_x, space.wrap(5))
+        #
+        code = self.compiler.compile('"spam"; "bar"; x=5',
+                                     '<hello>', 'single', 0)
+        w_locals = space.newdict([])
+        code.exec_code(space, space.newdict([]), w_locals)
+        w_x = space.getitem(w_locals, space.wrap('x'))
+        assert space.eq_w(w_x, space.wrap(5))
+
+    def test_barestringstmts_disappear(self):
+        space = self.space
+        code = self.compiler.compile('"a"\n"b"\n"c"\n', '<hello>', 'exec', 0)
+        for w_const in code.co_consts_w:
+            # "a" should show up as a docstring, but "b" and "c" should not
+            assert not space.eq_w(w_const, space.wrap("b"))
+            assert not space.eq_w(w_const, space.wrap("c"))
+
+
 class TestECCompiler(BaseTestCompiler):
     def setup_method(self, method):
         self.compiler = self.space.getexecutioncontext().compiler



More information about the Pypy-commit mailing list