From 533f616b0ca7be6efa28a7ae58db9235b4348d10 Mon Sep 17 00:00:00 2001 From: Jaime Fernandez Date: Thu, 12 Mar 2015 22:56:15 -0700 Subject: [PATCH 1/2] ENH: normalize 'sig' to 'signature' in ufunc override Closes #5674 --- numpy/core/src/private/ufunc_override.h | 9 ++++++++- numpy/core/tests/test_multiarray.py | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/numpy/core/src/private/ufunc_override.h b/numpy/core/src/private/ufunc_override.h index c47c46a66c9b..c3f9f601e0ff 100644 --- a/numpy/core/src/private/ufunc_override.h +++ b/numpy/core/src/private/ufunc_override.h @@ -13,7 +13,14 @@ normalize___call___args(PyUFuncObject *ufunc, PyObject *args, { /* ufunc.__call__(*args, **kwds) */ int nargs = PyTuple_GET_SIZE(args); - PyObject *obj; + PyObject *obj = PyDict_GetItemString(*normal_kwds, "sig"); + + /* ufuncs accept 'sig' or 'signature' normalize to 'signature' */ + if (obj != NULL) { + Py_INCREF(obj); + PyDict_SetItemString(*normal_kwds, "signature", obj); + PyDict_DelItemString(*normal_kwds, "sig"); + } *normal_args = PyTuple_GetSlice(args, 0, nin); diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 0c13cff6a02a..93d0cb6b9999 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -2313,6 +2313,22 @@ def __numpy_ufunc__(self, ufunc, method, i, inputs, **kw): assert_equal(obj2.sum(), 42) assert_(isinstance(obj2, SomeClass2)) + def test_ufunc_override_normalize_signature(self): + # gh-5674 + class SomeClass(object): + def __numpy_ufunc__(self, ufunc, method, i, inputs, **kw): + return kw + + a = SomeClass() + kw = np.add(a, [1]) + assert_('sig' not in kw and 'signature' not in kw) + kw = np.add(a, [1], sig='ii->i') + assert_('sig' not in kw and 'signature' in kw) + assert_equal(kw['signature'], 'ii->i') + kw = np.add(a, [1], signature='ii->i') + assert_('sig' not in kw and 'signature' in kw) + assert_equal(kw['signature'], 'ii->i') + class TestCAPI(TestCase): def test_IsPythonScalar(self): From cb2405f6c82863c0f2263a077f0010584b36bbb3 Mon Sep 17 00:00:00 2001 From: Jaime Fernandez Date: Thu, 12 Mar 2015 23:05:50 -0700 Subject: [PATCH 2/2] DOC: 'signature' preferred over 'sig' as ufunc keyword argument --- doc/source/reference/ufuncs.rst | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/doc/source/reference/ufuncs.rst b/doc/source/reference/ufuncs.rst index 3d5176638e71..a975346125d0 100644 --- a/doc/source/reference/ufuncs.rst +++ b/doc/source/reference/ufuncs.rst @@ -350,7 +350,8 @@ advanced usage and will not typically be used. .. versionadded:: 1.6 - Overrides the dtype of the calculation and output arrays. Similar to *sig*. + Overrides the dtype of the calculation and output arrays. Similar to + *signature*. *subok* @@ -359,7 +360,7 @@ advanced usage and will not typically be used. Defaults to true. If set to false, the output will always be a strict array, not a subtype. -*sig* or *signature* +*signature* Either a data-type, a tuple of data-types, or a special signature string indicating the input and output types of a ufunc. This argument @@ -370,7 +371,9 @@ advanced usage and will not typically be used. available and searching for a loop with data-types to which all inputs can be cast safely. This keyword argument lets you bypass that search and choose a particular loop. A list of available signatures is - provided by the **types** attribute of the ufunc object. + provided by the **types** attribute of the ufunc object. For backwards + compatibility this argument can also be provided as *sig*, although + the long form is preferred. *extobj*