Author: Ronan Lamy <[email protected]>
Branch: numpy-docstrings
Changeset: r78074:116f25d26306
Date: 2015-06-13 02:15 +0100
http://bitbucket.org/pypy/pypy/changeset/116f25d26306/

Log:    Create set_docstring() helper, move add_docstring() implementation
        to app-level and clean up

diff --git a/pypy/module/micronumpy/__init__.py 
b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -2,7 +2,9 @@
 
 
 class MultiArrayModule(MixedModule):
-    appleveldefs = {'arange': 'app_numpy.arange'}
+    appleveldefs = {
+        'arange': 'app_numpy.arange',
+        'add_docstring': 'app_numpy.add_docstring'}
     interpleveldefs = {
         'ndarray': 'ndarray.W_NDimArray',
         'dtype': 'descriptor.W_Dtype',
@@ -30,7 +32,7 @@
         'typeinfo': 'descriptor.get_dtype_cache(space).w_typeinfo',
         'nditer': 'nditer.W_NDIter',
 
-        'add_docstring': 'support.descr_add_docstring',
+        'set_docstring': 'support.descr_set_docstring',
     }
     for c in ['MAXDIMS', 'CLIP', 'WRAP', 'RAISE']:
         interpleveldefs[c] = 'space.wrap(constants.%s)' % c
diff --git a/pypy/module/micronumpy/app_numpy.py 
b/pypy/module/micronumpy/app_numpy.py
--- a/pypy/module/micronumpy/app_numpy.py
+++ b/pypy/module/micronumpy/app_numpy.py
@@ -3,6 +3,7 @@
 import math
 
 import _numpypy
+from _numpypy.multiarray import set_docstring
 
 def arange(start, stop=None, step=1, dtype=None):
     '''arange([start], stop[, step], dtype=None)
@@ -22,3 +23,13 @@
         arr[j] = i
         i += step
     return arr
+
+
+def add_docstring(obj, docstring):
+    old_doc = getattr(obj, '__doc__', None)
+    if old_doc is not None:
+        raise RuntimeError("%s already has a docstring" % obj)
+    try:
+        set_docstring(obj, docstring)
+    except:
+        raise TypeError("Cannot set a docstring for %s" % obj)
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,7 +4,6 @@
 
 from pypy.interpreter.error import OperationError, oefmt
 from pypy.interpreter.gateway import unwrap_spec, appdef
-from pypy.interpreter.function import Method
 from pypy.interpreter.typedef import GetSetProperty
 from pypy.objspace.std.typeobject import W_TypeObject
 
@@ -188,17 +187,19 @@
             return
     _add_doc_w(space, w_obj, space.wrap(docstring))
 
-_add_doc_w = appdef("""add_docstring(obj, docstring):
+def descr_set_docstring(space, w_obj, w_docstring):
+    if isinstance(w_obj, W_TypeObject):
+        w_obj.w_doc = w_docstring
+        return
+    elif isinstance(w_obj, GetSetProperty):
+        w_obj.doc = w_docstring
+        return
+    app_set_docstring(space, w_obj, w_docstring)
+
+app_set_docstring = appdef("""app_set_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)
-        pass
     if isinstance(obj, types.MethodType):
-        add_docstring(obj.im_func, docstring)
-        return
-    try:
+        obj.im_func.__doc__ = docstring
+    else:
         obj.__doc__ = docstring
-    except:
-        raise TypeError("Cannot set a docstring for %s" % obj)
 """)
diff --git a/pypy/module/micronumpy/test/test_support_app.py 
b/pypy/module/micronumpy/test/test_support_app.py
--- a/pypy/module/micronumpy/test/test_support_app.py
+++ b/pypy/module/micronumpy/test/test_support_app.py
@@ -1,7 +1,16 @@
 """App-level tests for support.py"""
+import sys
+import py
+
 from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
+from pypy.conftest import option
 
 class AppTestSupport(BaseNumpyAppTest):
+    def setup_class(cls):
+        if option.runappdirect and '__pypy__' not in sys.builtin_module_names:
+            py.test.skip("pypy only test")
+        BaseNumpyAppTest.setup_class.im_func(cls)
+
     def test_add_docstring(self):
         import numpy as np
         foo = lambda: None
@@ -9,21 +18,31 @@
         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
+        doc = types.FunctionType.__doc__
+        try:
+            np.set_docstring(types.FunctionType, 'foo')
+            assert types.FunctionType.__doc__ == 'foo'
+        finally:
+            np.set_docstring(types.FunctionType, doc)
+
         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'
+        doc = int.bit_length.__doc__
+        try:
+            np.set_docstring(int.bit_length, 'foo')
+            assert int.bit_length.__doc__ == 'foo'
+        finally:
+            np.set_docstring(int.bit_length, doc)
 
     def test_property_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(np.flatiter.base, 'foo')
-        assert np.flatiter.base.__doc__ == 'foo'
+        doc = np.flatiter.base.__doc__
+        try:
+            np.set_docstring(np.flatiter.base, 'foo')
+            assert np.flatiter.base.__doc__ == 'foo'
+        finally:
+            np.set_docstring(np.flatiter.base, doc)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to