diff options
author | Jaime Fernandez <jaime.frio@gmail.com> | 2015-03-12 22:56:15 -0700 |
---|---|---|
committer | Jaime Fernandez <jaime.frio@gmail.com> | 2015-03-12 22:56:15 -0700 |
commit | 533f616b0ca7be6efa28a7ae58db9235b4348d10 (patch) | |
tree | 73863d646686f3f04ce7da1a76077d332dc42098 | |
parent | fd3b0fc2481cfe2aad960fe05baf11be46a0dd3f (diff) | |
download | numpy-533f616b0ca7be6efa28a7ae58db9235b4348d10.tar.gz |
ENH: normalize 'sig' to 'signature' in ufunc override
Closes #5674
-rw-r--r-- | numpy/core/src/private/ufunc_override.h | 9 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 16 |
2 files changed, 24 insertions, 1 deletions
diff --git a/numpy/core/src/private/ufunc_override.h b/numpy/core/src/private/ufunc_override.h index c47c46a66..c3f9f601e 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 0c13cff6a..93d0cb6b9 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -2313,6 +2313,22 @@ class TestBinop(object): 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): |