Author: Ronan Lamy <[email protected]>
Branch: numpy-docstrings
Changeset: r78062:caa48835941b
Date: 2015-06-12 17:48 +0100
http://bitbucket.org/pypy/pypy/changeset/caa48835941b/

Log:    Start implementing np.add_docstring

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
@@ -29,6 +29,8 @@
         'set_string_function': 'appbridge.set_string_function',
         'typeinfo': 'descriptor.get_dtype_cache(space).w_typeinfo',
         'nditer': 'nditer.W_NDIter',
+
+        'add_docstring': 'support.descr_add_docstring',
     }
     for c in ['MAXDIMS', 'CLIP', 'WRAP', 'RAISE']:
         interpleveldefs[c] = 'space.wrap(constants.%s)' % c
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
@@ -1,8 +1,9 @@
-from pypy.interpreter.error import OperationError, oefmt
 from rpython.rlib import jit
 from rpython.rlib.rarithmetic import ovfcheck
 from rpython.rtyper.lltypesystem import rffi, lltype
 
+from pypy.interpreter.error import OperationError, oefmt
+from pypy.interpreter.gateway import unwrap_spec, appdef
 
 def issequence_w(space, w_obj):
     from pypy.module.micronumpy.base import W_NDimArray
@@ -172,3 +173,17 @@
     elif req_order == 'A':
         return proto_order
 
+@unwrap_spec(docstring=str)
+def descr_add_docstring(space, w_obj, docstring):
+    return _add_doc_w(space, w_obj, space.wrap(docstring))
+
+_add_doc_w = appdef("""add_docstring(obj, docstring):
+    old_doc = getattr(obj, '__doc__', None)
+    if old_doc is not None:
+        raise RuntimeError("object already has a docstring")
+    try:
+        obj.__doc__ = docstring
+    except:
+        if not isinstance(obj, type) and callable(obj):
+            raise TypeError("Cannot set a docstring for %s" % obj)
+""")
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to