diff options
author | Pauli Virtanen <pav@iki.fi> | 2009-02-14 22:09:26 +0000 |
---|---|---|
committer | Pauli Virtanen <pav@iki.fi> | 2009-02-14 22:09:26 +0000 |
commit | 9902bd1969a738ddec9e01d019d98142dda59f3f (patch) | |
tree | 7aa8e05daec4391ba5eccc20876cf5391a6ae72a /numpy/lib | |
parent | 33d79edf8e06fd69b26a9331d612caadb7b55672 (diff) | |
download | numpy-9902bd1969a738ddec9e01d019d98142dda59f3f.tar.gz |
More add_newdocs entries, and make add_newdoc capable of adding docs also to normal Python objects.
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/function_base.py | 47 | ||||
-rw-r--r-- | numpy/lib/index_tricks.py | 2 | ||||
-rw-r--r-- | numpy/lib/src/_compiled_base.c | 51 |
3 files changed, 33 insertions, 67 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 269a97721..283e3faff 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -1073,53 +1073,6 @@ def diff(a, n=1, axis=-1): else: return a[slice1]-a[slice2] -try: - add_docstring(digitize, -r"""digitize(x,bins) - -Return the index of the bin to which each value of x belongs. - -Each index i returned is such that bins[i-1] <= x < bins[i] if -bins is monotonically increasing, or bins [i-1] > x >= bins[i] if -bins is monotonically decreasing. - -Beyond the bounds of the bins 0 or len(bins) is returned as appropriate. - -""") -except RuntimeError: - pass - -try: - add_docstring(bincount, -r"""bincount(x,weights=None) - -Return the number of occurrences of each value in x. - -x must be a list of non-negative integers. The output, b[i], -represents the number of times that i is found in x. If weights -is specified, every occurrence of i at a position p contributes -weights[p] instead of 1. - -See also: histogram, digitize, unique. - -""") -except RuntimeError: - pass - -try: - add_docstring(add_docstring, -r"""docstring(obj, docstring) - -Add a docstring to a built-in obj if possible. -If the obj already has a docstring raise a RuntimeError -If this routine does not know how to add a docstring to the object -raise a TypeError - -""") -except RuntimeError: - pass - - def interp(x, xp, fp, left=None, right=None): """ One-dimensional linear interpolation. diff --git a/numpy/lib/index_tricks.py b/numpy/lib/index_tricks.py index 3021635dc..fcd3909af 100644 --- a/numpy/lib/index_tricks.py +++ b/numpy/lib/index_tricks.py @@ -212,6 +212,8 @@ class nd_grid(object): mgrid = nd_grid(sparse=False) ogrid = nd_grid(sparse=True) +mgrid.__doc__ = None # set in numpy.add_newdocs +ogrid.__doc__ = None # set in numpy.add_newdocs class AxisConcatenator(object): """Translates slice objects to concatenation along an axis. diff --git a/numpy/lib/src/_compiled_base.c b/numpy/lib/src/_compiled_base.c index ddab9f851..d6982bc9c 100644 --- a/numpy/lib/src/_compiled_base.c +++ b/numpy/lib/src/_compiled_base.c @@ -494,34 +494,45 @@ arr_add_docstring(PyObject *NPY_UNUSED(dummy), PyObject *args) #define _TESTDOC1(typebase) (obj->ob_type == &Py##typebase##_Type) #define _TESTDOC2(typebase) (obj->ob_type == Py##typebase##_TypePtr) -#define _ADDDOC(typebase, doc, name) { \ +#define _ADDDOC(typebase, doc, name) do { \ Py##typebase##Object *new = (Py##typebase##Object *)obj; \ if (!(doc)) { \ doc = docstr; \ } \ else { \ - PyErr_Format(PyExc_RuntimeError, \ - "%s method %s",name, msg); \ + PyErr_Format(PyExc_RuntimeError, "%s method %s", name, msg); \ return NULL; \ } \ - } + } while (0) + + if (_TESTDOC1(CFunction)) + _ADDDOC(CFunction, new->m_ml->ml_doc, new->m_ml->ml_name); + else if (_TESTDOC1(Type)) + _ADDDOC(Type, new->tp_doc, new->tp_name); + else if (_TESTDOC2(MemberDescr)) + _ADDDOC(MemberDescr, new->d_member->doc, new->d_member->name); + else if (_TESTDOC2(GetSetDescr)) + _ADDDOC(GetSetDescr, new->d_getset->doc, new->d_getset->name); + else if (_TESTDOC2(MethodDescr)) + _ADDDOC(MethodDescr, new->d_method->ml_doc, new->d_method->ml_name); + else { + PyObject *doc_attr; + + doc_attr = PyObject_GetAttrString(obj, "__doc__"); + if (doc_attr != NULL && doc_attr != Py_None) { + PyErr_Format(PyExc_RuntimeError, "object %s", msg); + return NULL; + } + Py_XDECREF(doc_attr); - if _TESTDOC1(CFunction) - _ADDDOC(CFunction, new->m_ml->ml_doc, new->m_ml->ml_name) - else if _TESTDOC1(Type) - _ADDDOC(Type, new->tp_doc, new->tp_name) - else if _TESTDOC2(MemberDescr) - _ADDDOC(MemberDescr, new->d_member->doc, new->d_member->name) - else if _TESTDOC2(GetSetDescr) - _ADDDOC(GetSetDescr, new->d_getset->doc, new->d_getset->name) - else if _TESTDOC2(MethodDescr) - _ADDDOC(MethodDescr, new->d_method->ml_doc, - new->d_method->ml_name) - else { - PyErr_SetString(PyExc_TypeError, - "Cannot set a docstring for that object"); - return NULL; - } + if (PyObject_SetAttrString(obj, "__doc__", str) < 0) { + PyErr_SetString(PyExc_TypeError, + "Cannot set a docstring for that object"); + return NULL; + } + Py_INCREF(Py_None); + return Py_None; + } #undef _TESTDOC1 #undef _TESTDOC2 |