8000 Override sig normalize by jaimefrio · Pull Request #5677 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Override sig normalize #5677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions doc/source/reference/ufuncs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand All @@ -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
Expand All @@ -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*

Expand Down
9 changes: 8 additions & 1 deletion numpy/core/src/private/ufunc_override.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
16 changes: 16 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
0