[pypy-svn] r38704 - in pypy/dist/pypy/interpreter: . astcompiler pyparser test

arigo at codespeak.net arigo at codespeak.net
Tue Feb 13 15:49:01 CET 2007


Author: arigo
Date: Tue Feb 13 15:48:57 2007
New Revision: 38704

Modified:
   pypy/dist/pypy/interpreter/astcompiler/pycodegen.py
   pypy/dist/pypy/interpreter/eval.py
   pypy/dist/pypy/interpreter/function.py
   pypy/dist/pypy/interpreter/gateway.py
   pypy/dist/pypy/interpreter/pycode.py
   pypy/dist/pypy/interpreter/pyparser/astbuilder.py
   pypy/dist/pypy/interpreter/test/test_compiler.py
   pypy/dist/pypy/interpreter/test/test_function.py
   pypy/dist/pypy/interpreter/typedef.py
Log:
* Support for unicode docstrings.
* Make sure the docstring of a module is in co_consts[0].


Modified: pypy/dist/pypy/interpreter/astcompiler/pycodegen.py
==============================================================================
--- pypy/dist/pypy/interpreter/astcompiler/pycodegen.py	(original)
+++ pypy/dist/pypy/interpreter/astcompiler/pycodegen.py	Tue Feb 13 15:48:57 2007
@@ -302,6 +302,7 @@
         self.scope = node.scope
         self.emitop_int('SET_LINENO', 0)
         if not space.is_w(node.w_doc, space.w_None):
+            self.setDocstring(node.w_doc)
             self.set_lineno(node)
             self.emitop_obj('LOAD_CONST', node.w_doc)
             self.storeName('__doc__', node.lineno)

Modified: pypy/dist/pypy/interpreter/eval.py
==============================================================================
--- pypy/dist/pypy/interpreter/eval.py	(original)
+++ pypy/dist/pypy/interpreter/eval.py	Tue Feb 13 15:48:57 2007
@@ -45,8 +45,8 @@
             argcount += 1
         return argcount
 
-    def getdocstring(self):
-        return None
+    def getdocstring(self, space):
+        return space.w_None
 
     def funcrun(self, func, args):
         frame = func.space.createframe(self, func.w_func_globals,

Modified: pypy/dist/pypy/interpreter/function.py
==============================================================================
--- pypy/dist/pypy/interpreter/function.py	(original)
+++ pypy/dist/pypy/interpreter/function.py	Tue Feb 13 15:48:57 2007
@@ -19,7 +19,7 @@
     def __init__(self, space, code, w_globals=None, defs_w=[], closure=None, forcename=None):
         self.space = space
         self.name = forcename or code.co_name
-        self.w_doc = None   # lazily read and wrapped from code.getdocstring()
+        self.w_doc = None   # lazily read from code.getdocstring()
         self.code = code       # Code instance
         self.w_func_globals = w_globals  # the globals dictionary
         self.closure   = closure    # normally, list of Cell instances or None
@@ -232,7 +232,7 @@
 
     def fget_func_doc(space, self):
         if self.w_doc is None:
-            self.w_doc = space.wrap(self.code.getdocstring())
+            self.w_doc = self.code.getdocstring(space)
         return self.w_doc
 
     def fset_func_doc(space, self, w_doc):

Modified: pypy/dist/pypy/interpreter/gateway.py
==============================================================================
--- pypy/dist/pypy/interpreter/gateway.py	(original)
+++ pypy/dist/pypy/interpreter/gateway.py	Tue Feb 13 15:48:57 2007
@@ -441,8 +441,8 @@
     def signature(self):
         return self.sig
 
-    def getdocstring(self):
-        return self.docstring
+    def getdocstring(self, space):
+        return space.wrap(self.docstring)
 
     def funcrun(self, func, args):
         space = func.space

Modified: pypy/dist/pypy/interpreter/pycode.py
==============================================================================
--- pypy/dist/pypy/interpreter/pycode.py	(original)
+++ pypy/dist/pypy/interpreter/pycode.py	Tue Feb 13 15:48:57 2007
@@ -228,15 +228,11 @@
     def getvarnames(self):
         return self.co_varnames
 
-    def getdocstring(self):
+    def getdocstring(self, space):
         if self.co_consts_w:   # it is probably never empty
-            const0_w = self.co_consts_w[0]
-            if const0_w is self.space.w_None:
-                return None
-            else:
-                return self.space.str_w(const0_w)
+            return self.co_consts_w[0]
         else:
-            return None
+            return space.w_None
 
     def getjoinpoints(self):
         """Compute the bytecode positions that are potential join points

Modified: pypy/dist/pypy/interpreter/pyparser/astbuilder.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyparser/astbuilder.py	(original)
+++ pypy/dist/pypy/interpreter/pyparser/astbuilder.py	Tue Feb 13 15:48:57 2007
@@ -352,7 +352,7 @@
         first_child = stmt.nodes[0]
         if isinstance(first_child, ast.Discard):
             expr = first_child.expr
-            if builder.is_string_const(expr):
+            if builder.is_basestring_const(expr):
                 # This *is* a docstring, remove it from stmt list
                 assert isinstance(expr, ast.Const)
                 del stmt.nodes[0]
@@ -1798,11 +1798,11 @@
             f = space.builtin.get('float')
             return space.call_function(f, space.wrap(value))
 
-    def is_string_const(self, expr):
+    def is_basestring_const(self, expr):
         if not isinstance(expr,ast.Const):
             return False
         space = self.space
-        return space.is_true(space.isinstance(expr.value,space.w_str))
+        return space.is_true(space.isinstance(expr.value,space.w_basestring))
 
     def wrap_string(self, obj):
         if self.space:

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	Tue Feb 13 15:48:57 2007
@@ -185,6 +185,12 @@
         ex.normalize_exception(self.space)
         assert ex.match(self.space, self.space.w_UnicodeError)
 
+    def test_unicode_docstring(self):
+        space = self.space
+        code = self.compiler.compile('u"hello"\n', '<hello>', 'exec', 0)
+        assert space.eq_w(code.co_consts_w[0], space.wrap("hello"))
+        assert space.is_w(space.type(code.co_consts_w[0]), space.w_unicode)
+
     def test_argument_handling(self):
         for expr in 'lambda a,a:0', 'lambda a,a=1:0', 'lambda a=1,a=1:0':
             e = py.test.raises(OperationError, self.eval_string, expr)

Modified: pypy/dist/pypy/interpreter/test/test_function.py
==============================================================================
--- pypy/dist/pypy/interpreter/test/test_function.py	(original)
+++ pypy/dist/pypy/interpreter/test/test_function.py	Tue Feb 13 15:48:57 2007
@@ -169,6 +169,12 @@
         raises(TypeError, len, s, some_unknown_keyword=s)
         raises(TypeError, len, s, s, some_unknown_keyword=s)
 
+    def test_unicode_docstring(self):
+        def f():
+            u"hi"
+        assert f.__doc__ == u"hi"
+        assert type(f.__doc__) is unicode
+
 class AppTestMethod: 
     def test_get(self):
         def func(self): return self

Modified: pypy/dist/pypy/interpreter/typedef.py
==============================================================================
--- pypy/dist/pypy/interpreter/typedef.py	(original)
+++ pypy/dist/pypy/interpreter/typedef.py	Tue Feb 13 15:48:57 2007
@@ -523,7 +523,7 @@
     return space.wrap(flags)
 
 def fget_co_consts(space, code): # unwrapping through unwrap_spec
-    w_docstring = space.wrap(code.getdocstring())
+    w_docstring = code.getdocstring(space)
     return space.newtuple([w_docstring])
 
 weakref_descr = GetSetProperty(descr_get_weakref)



More information about the Pypy-commit mailing list