[pypy-commit] pypy numpy-docstrings: Handle methods in add_docstring()

rlamy noreply at buildbot.pypy.org
Sat Jun 13 03:15:52 CEST 2015


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: numpy-docstrings
Changeset: r78072:5b3d5ccedb57
Date: 2015-06-13 01:25 +0100
http://bitbucket.org/pypy/pypy/changeset/5b3d5ccedb57/

Log:	Handle methods in add_docstring()

diff --git a/pypy/module/micronumpy/support.py b/pypy/module/micronumpy/support.py
--- a/pypy/module/micronumpy/support.py
+++ b/pypy/module/micronumpy/support.py
@@ -4,6 +4,7 @@
 
 from pypy.interpreter.error import OperationError, oefmt
 from pypy.interpreter.gateway import unwrap_spec, appdef
+from pypy.interpreter.function import Method
 from pypy.objspace.std.typeobject import W_TypeObject
 
 def issequence_w(space, w_obj):
@@ -183,9 +184,14 @@
     _add_doc_w(space, w_obj, space.wrap(docstring))
 
 _add_doc_w = appdef("""add_docstring(obj, docstring):
+    import types
     old_doc = getattr(obj, '__doc__', None)
     if old_doc is not None:
-        raise RuntimeError("%s already has a docstring" % obj)
+        # raise RuntimeError("%s already has a docstring" % obj)
+        pass
+    if isinstance(obj, types.MethodType):
+        add_docstring(obj.im_func, docstring)
+        return
     try:
         obj.__doc__ = docstring
     except:
diff --git a/pypy/module/micronumpy/test/test_support_app.py b/pypy/module/micronumpy/test/test_support_app.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/micronumpy/test/test_support_app.py
@@ -0,0 +1,22 @@
+"""App-level tests for support.py"""
+from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
+
+class AppTestSupport(BaseNumpyAppTest):
+    def test_add_docstring(self):
+        import numpy as np
+        foo = lambda: None
+        np.add_docstring(foo, "Does a thing")
+        assert foo.__doc__ == "Does a thing"
+
+    def test_type_docstring(self):
+        # XXX: We cannot sensibly test np.add_docstring() being successful
+        import numpy as np
+        import types
+        raises(RuntimeError, np.add_docstring, types.FunctionType, 'foo')
+
+    def test_method_docstring(self):
+        # XXX: We cannot sensibly test np.add_docstring() being successful
+        import numpy as np
+        #raises(RuntimeError, np.add_docstring, int.bit_length, 'foo')
+        np.add_docstring(int.bit_length,'foo')
+        assert int.bit_length.__doc__ == 'foo'


More information about the pypy-commit mailing list