[pypy-svn] r50431 - in pypy/branch/simpler-pyassem/pypy/interpreter/astcompiler: . test

arigo at codespeak.net arigo at codespeak.net
Mon Jan 7 18:56:45 CET 2008


Author: arigo
Date: Mon Jan  7 18:56:45 2008
New Revision: 50431

Modified:
   pypy/branch/simpler-pyassem/pypy/interpreter/astcompiler/pyassem.py
   pypy/branch/simpler-pyassem/pypy/interpreter/astcompiler/pycodegen.py
   pypy/branch/simpler-pyassem/pypy/interpreter/astcompiler/test/test_compiler.py
Log:
Docstrings.


Modified: pypy/branch/simpler-pyassem/pypy/interpreter/astcompiler/pyassem.py
==============================================================================
--- pypy/branch/simpler-pyassem/pypy/interpreter/astcompiler/pyassem.py	(original)
+++ pypy/branch/simpler-pyassem/pypy/interpreter/astcompiler/pyassem.py	Mon Jan  7 18:56:45 2008
@@ -19,7 +19,7 @@
             argnames = []
         self.name = name
         self.filename = filename
-        self.docstring = space.w_None
+        self.w_docstring = space.w_None
         self.argcount = len(argnames)
         self.klass = klass
         self.flags = 0
@@ -46,8 +46,8 @@
         # Pending label targets to fix: [(label, index-in-co_code-to-fix, abs)]
         self.pending_label_fixes = []
 
-    def setDocstring(self, doc):
-        self.docstring = doc
+    def setDocstring(self, w_docstring):
+        self.w_docstring = w_docstring
 
     def setFlag(self, flag):
         self.flags = self.flags | flag
@@ -97,6 +97,12 @@
 
     def _lookupConst(self, w_obj, w_dict):
         space = self.space
+        # insert the docstring first, if necessary
+        if not space.is_true(w_dict):
+            w_obj_type = space.type(self.w_docstring)
+            w_key = space.newtuple([self.w_docstring, w_obj_type])
+            space.setitem(w_dict, w_key, space.wrap(0))
+        # normal logic follows
         w_obj_type = space.type(w_obj)
         w_key = space.newtuple([w_obj, w_obj_type])
         try:
@@ -350,6 +356,10 @@
     def getConsts(self):
         """Return a tuple for the const slot of the code object
         """
+        # sanity-check
+        index = self._lookupConst(self.w_docstring, self.w_consts)
+        if index != 0:
+            raise InternalCompilerError("setDocstring() called too late")
         space = self.space
         keys_w = space.unpackiterable(self.w_consts)
         l_w = [None] * len(keys_w)

Modified: pypy/branch/simpler-pyassem/pypy/interpreter/astcompiler/pycodegen.py
==============================================================================
--- pypy/branch/simpler-pyassem/pypy/interpreter/astcompiler/pycodegen.py	(original)
+++ pypy/branch/simpler-pyassem/pypy/interpreter/astcompiler/pycodegen.py	Mon Jan  7 18:56:45 2008
@@ -313,8 +313,6 @@
     def visitFunction(self, node):
         self._visitFuncOrLambda(node, isLambda=0)
         space = self.space
-        if not space.is_w(node.w_doc, space.w_None):
-            self.setDocstring(node.w_doc)
         self.storeName(node.name, node.lineno)
 
     def visitLambda(self, node):
@@ -1416,7 +1414,7 @@
         CodeGenerator.__init__(self, space, graph)
         self.optimized = 1
 
-        if not isLambda and not space.is_w(func.w_doc, space.w_None):
+        if not isLambda:
             self.setDocstring(func.w_doc)
 
         if func.varargs:
@@ -1495,8 +1493,7 @@
 
         CodeGenerator.__init__(self, space, graph)
         self.graph.setFlag(CO_NEWLOCALS)
-        if not space.is_w(klass.w_doc, space.w_None):
-            self.setDocstring(klass.w_doc)
+        self.setDocstring(klass.w_doc)
 
     def get_module(self):
         return self.module

Modified: pypy/branch/simpler-pyassem/pypy/interpreter/astcompiler/test/test_compiler.py
==============================================================================
--- pypy/branch/simpler-pyassem/pypy/interpreter/astcompiler/test/test_compiler.py	(original)
+++ pypy/branch/simpler-pyassem/pypy/interpreter/astcompiler/test/test_compiler.py	Mon Jan  7 18:56:45 2008
@@ -325,6 +325,11 @@
                     """doc"""; print 1
                     a=1
              ''',                            "doc"),
+            ('''
+                class Foo(object): pass
+                foo = Foo()
+                exec "'moduledoc'" in foo.__dict__
+             ''',                            "moduledoc"),
             ]:
             yield self.simple_test, source, "foo.__doc__", expected
 



More information about the Pypy-commit mailing list