[pypy-svn] r4962 - in pypy/branch/src-newobjectmodel/pypy/interpreter: . test

arigo at codespeak.net arigo at codespeak.net
Sat Jun 5 14:55:37 CEST 2004


Author: arigo
Date: Sat Jun  5 14:55:37 2004
New Revision: 4962

Modified:
   pypy/branch/src-newobjectmodel/pypy/interpreter/function.py
   pypy/branch/src-newobjectmodel/pypy/interpreter/test/test_function.py
   pypy/branch/src-newobjectmodel/pypy/interpreter/typedef.py
Log:
Writable func_doc.


Modified: pypy/branch/src-newobjectmodel/pypy/interpreter/function.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/interpreter/function.py	(original)
+++ pypy/branch/src-newobjectmodel/pypy/interpreter/function.py	Sat Jun  5 14:55:37 2004
@@ -17,7 +17,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.doc = getattr(code, 'co_consts', (None,))[0]
+        self.w_doc = None   # lazily read and wrapped from code.co_consts[0]
         self.code = code       # Code instance
         self.w_func_globals = w_globals  # the globals dictionary
         self.closure   = closure    # normally, list of Cell instances or None
@@ -185,7 +185,22 @@
         values_w = self.defs_w
         if not values_w:
             return space.w_None
-        return space.newtuple(values_w) 
+        return space.newtuple(values_w)
+
+    def fget_func_doc(space, w_self):
+        self = space.unwrap(w_self)
+        if self.w_doc is None:
+            doc = getattr(self.code, 'co_consts', (None,))[0]
+            self.w_doc = space.wrap(doc)
+        return self.w_doc
+
+    def fset_func_doc(space, w_self, w_doc):
+        self = space.unwrap(w_self)
+        self.w_doc = w_doc
+
+    def fdel_func_doc(space, w_self):
+        self = space.unwrap(w_self)
+        self.w_doc = space.w_None
 
 class Method(Wrappable): 
     """A method is a function bound to a specific instance or class."""

Modified: pypy/branch/src-newobjectmodel/pypy/interpreter/test/test_function.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/interpreter/test/test_function.py	(original)
+++ pypy/branch/src-newobjectmodel/pypy/interpreter/test/test_function.py	Sat Jun  5 14:55:37 2004
@@ -30,6 +30,22 @@
         self.assert_(f.__dict__ is f.func_dict)
         self.assert_(hasattr(f, '__class__'))
 
+    def test_write_doc(self):
+        def f(): "hello"
+        self.assertEquals(f.__doc__, 'hello')
+        f.__doc__ = 'good bye'
+        self.assertEquals(f.__doc__, 'good bye')
+        del f.__doc__
+        self.assertEquals(f.__doc__, None)
+
+    def test_write_func_doc(self):
+        def f(): "hello"
+        self.assertEquals(f.func_doc, 'hello')
+        f.func_doc = 'good bye'
+        self.assertEquals(f.func_doc, 'good bye')
+        del f.func_doc
+        self.assertEquals(f.func_doc, None)
+
 class AppTestFunction(testit.AppTestCase):
     def test_simple_call(self):
         def func(arg1, arg2):

Modified: pypy/branch/src-newobjectmodel/pypy/interpreter/typedef.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/interpreter/typedef.py	(original)
+++ pypy/branch/src-newobjectmodel/pypy/interpreter/typedef.py	Sat Jun  5 14:55:37 2004
@@ -121,16 +121,20 @@
     __dict__ = attrproperty_w('w_dict'), 
     )
 
+getset_func_doc = GetSetProperty(Function.fget_func_doc,
+                                 Function.fset_func_doc,
+                                 Function.fdel_func_doc)
+
 Function.typedef = TypeDef("function",
     __call__ = interp2app(Function.descr_function_call.im_func),
     __get__ = interp2app(Function.descr_function_get.im_func),
     func_code = attrproperty('code'), 
-    func_doc = attrproperty('doc'), 
+    func_doc = getset_func_doc,
     func_name = attrproperty('name'), 
     func_dict = attrproperty_w('w_func_dict'), 
     func_defaults = GetSetProperty(Function.fget_func_defaults),
     func_globals = attrproperty_w('w_func_globals'),
-    __doc__ = attrproperty('doc'), 
+    __doc__ = getset_func_doc,
     __name__ = attrproperty('name'), 
     __dict__ = attrproperty_w('w_func_dict'), 
     # XXX func_closure, etc.pp



More information about the Pypy-commit mailing list