8000 BUG: Make ndarray inplace operators forward calls when needed. by charris · Pull Request #9021 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Make ndarray inplace operators forward calls when needed. #9021

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 1 commit into from
Apr 29, 2017
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
110 changes: 72 additions & 38 deletions numpy/core/src/multiarray/number.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,38 @@

NPY_NO_EXPORT NumericOps n_ops; /* NB: static objects initialized to zero */

/*
* Forward declarations. Might want to move functions around instead
*/
static PyObject *
array_inplace_add(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_subtract(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_multiply(PyArrayObject *m1, PyObject *m2);
#if !defined(NPY_PY3K)
static PyObject *
array_inplace_divide(PyArrayObject *m1, PyObject *m2);
#endif
static PyObject *
array_inplace_true_divide(PyArrayObject *m1, PyObject *m2);
static PyObject *
8000 array_inplace_floor_divide(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_bitwise_and(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_bitwise_or(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_bitwise_xor(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_left_shift(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_right_shift(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_remainder(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_power(PyArrayObject *a1, PyObject *o2, PyObject *NPY_UNUSED(modulo));

/*
* Dictionary can contain any of the numeric operations, by name.
* Those not present will not be changed
Expand Down Expand Up @@ -255,31 +287,6 @@ PyArray_GenericInplaceUnaryFunction(PyArrayObject *m1, PyObject *op)
return PyObject_CallFunctionObjArgs(op, m1, m1, NULL);
}

static PyObject *
array_inplace_add(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_subtract(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_multiply(PyArrayObject *m1, PyObject *m2);
#if !defined(NPY_PY3K)
static PyObject *
array_inplace_divide(PyArrayObject *m1, PyObject *m2);
#endif
static PyObject *
array_inplace_true_divide(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_floor_divide(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_bitwise_and(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_bitwise_or(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_bitwise_xor(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_left_shift(PyArrayObject *m1, PyObject *m2);
static PyObject *
array_inplace_right_shift(PyArrayObject *m1, PyObject *m2);

static PyObject *
array_add(PyArrayObject *m1, PyObject *m2)
{
Expand Down Expand Up @@ -628,32 +635,42 @@ array_bitwise_xor(PyArrayObject *m1, PyObject *m2)
static PyObject *
array_inplace_add(PyArrayObject *m1, PyObject *m2)
{
INPLACE_GIVE_UP_IF_NEEDED(
m1, m2, nb_inplace_add, array_inplace_add);
return PyArray_GenericInplaceBinaryFunction(m1, m2, n_ops.add);
}

static PyObject *
array_inplace_subtract(PyArrayObject *m1, PyObject *m2)
{
INPLACE_GIVE_UP_IF_NEEDED(
m1, m2, nb_inplace_subtract, array_inplace_subtract);
return PyArray_GenericInplaceBinaryFunction(m1, m2, n_ops.subtract);
}

static PyObject *
array_inplace_multiply(PyArrayObject *m1, PyObject *m2)
{
INPLACE_GIVE_UP_IF_NEEDED(
m1, m2, nb_inplace_multiply, array_inplace_multiply);
return PyArray_GenericInplaceBinaryFunction(m1, m2, n_ops.multiply);
}

#if !defined(NPY_PY3K)
static PyObject *
array_inplace_divide(PyArrayObject *m1, PyObject *m2)
{
INPLACE_GIVE_UP_IF_NEEDED(
m1, m2, nb_inplace_divide, array_inplace_divide);
return PyArray_GenericInplaceBinaryFunction(m1, m2, n_ops.divide);
}
#endif

static PyObject *
array_inplace_remainder(PyArrayObject *m1, PyObject *m2)
{
INPLACE_GIVE_UP_IF_NEEDED(
m1, m2, nb_inplace_remainder, array_inplace_remainder);
return PyArray_GenericInplaceBinaryFunction(m1, m2, n_ops.remainder);
}

Expand All @@ -662,6 +679,9 @@ array_inplace_power(PyArrayObject *a1, PyObject *o2, PyObject *NPY_UNUSED(modulo
{
/* modulo is ignored! */
PyObject *value;

INPLACE_GIVE_UP_IF_NEEDED(
a1, o2, nb_inplace_power, array_inplace_power);
value = fast_scalar_power(a1, o2, 1);
if (!value) {
value = PyArray_GenericInplaceBinaryFunction(a1, o2, n_ops.power);
Expand All @@ -672,30 +692,40 @@ array_inplace_power(PyArrayObject *a1, PyObject *o2, PyObject *NPY_UNUSED(modulo
static PyObject *
array_inplace_left_shift(PyArrayObject *m1, PyObject *m2)
{
INPLACE_GIVE_UP_IF_NEEDED(
m1, m2, nb_inplace_lshift, array_inplace_left_shift);
return PyArray_GenericInplaceBinaryFunction(m1, m2, n_ops.left_shift);
}

static PyObject *
array_inplace_right_shift(PyArrayObject *m1, PyObject *m2)
{
INPLACE_GIVE_UP_IF_NEEDED(
m1, m2, nb_inplace_rshift, array_inplace_right_shift);
return PyArray_GenericInplaceBinaryFunction(m1, m2, n_ops.right_shift);
}

static PyObject *
array_inplace_bitwise_and(PyArrayObject *m1, PyObject *m2)
{
INPLACE_GIVE_UP_IF_NEEDED(
m1, m2, nb_inplace_and, array_inplace_bitwise_and);
return PyArray_GenericInplaceBinaryFunction(m1, m2, n_ops.bitwise_and);
}

static PyObject *
array_inplace_bitwise_or(PyArrayObject *m1, PyObject *m2)
{
INPLACE_GIVE_UP_IF_NEEDED(
m1, m2, nb_inplace_or, array_inplace_bitwise_or);
return PyArray_GenericInplaceBinaryFunction(m1, m2, n_ops.bitwise_or);
}

static PyObject *
array_inplace_bitwise_xor(PyArrayObject *m1, PyObject *m2)
{
INPLACE_GIVE_UP_IF_NEEDED(
m1, m2, nb_inplace_xor, array_inplace_bitwise_xor);
return PyArray_GenericInplaceBinaryFunction(m1, m2, n_ops.bitwise_xor);
}

Expand Down Expand Up @@ -728,13 +758,17 @@ array_true_divide(PyArrayObject *m1, PyObject *m2)
static PyObject *
array_inplace_floor_divide(PyArrayObject *m1, PyObject *m2)
{
INPLACE_GIVE_UP_IF_NEEDED(
m1, m2, nb_inplace_floor_divide, array_inplace_floor_divide);
return PyArray_GenericInplaceBinaryFunction(m1, m2,
n_ops.floor_divide);
}

static PyObject *
array_inplace_true_divide(PyArrayObject *m1, PyObject *m2)
{
INPLACE_GIVE_UP_IF_NEEDED(
m1, m2, nb_inplace_true_divide, array_inplace_true_divide);
return PyArray_GenericInplaceBinaryFunction(m1, m2,
n_ops.true_divide);
}
Expand All @@ -754,8 +788,8 @@ _array_nonzero(PyArrayObject *mp)
}
else {
PyErr_SetString(PyExc_ValueError,
"The truth value of an array " \
"with more than one element is ambiguous. " \
"The truth value of an array "
"with more than one element is ambiguous. "
"Use a.any() or a.all()");
return -1;
}
Expand Down Expand Up @@ -1060,19 +1094,19 @@ NPY_NO_EXPORT PyNumberMethods array_as_number = {
* This code adds augmented assignment functionality
* that was made available in Python 2.0
*/
(binaryfunc)array_inplace_add, /*inplace_add*/
(binaryfunc)array_inplace_subtract, /*inplace_subtract*/
(binaryfunc)array_inplace_multiply, /*inplace_multiply*/
(binaryfunc)array_inplace_add, /*nb_inplace_add*/
(binaryfunc)array_inplace_subtract, /*nb_inplace_subtract*/
(binaryfunc)array_inplace_multiply, /*nb_inplace_multiply*/
#if !defined(NPY_PY3K)
(binaryfunc)array_inplace_divide, /*inplace_divide*/
(binaryfunc)array_inplace_divide, /*nb_inplace_divide*/
#endif
(binaryfunc)array_inplace_remainder, /*inplace_remainder*/
(ternaryfunc)array_inplace_power, /*inplace_power*/
(binaryfunc)array_inplace_left_shift, /*inplace_lshift*/
(binaryfunc)array_inplace_right_shift, /*inplace_rshift*/
(binaryfunc)array_inplace_bitwise_and, /*inplace_and*/
(binaryfunc)array_inplace_bitwise_xor, /*inplace_xor*/
(binaryfunc)array_inplace_bitwise_or, /*inplace_or*/
(binaryfunc)array_inplace_remainder, /*nb_inplace_remainder*/
(ternaryfunc)array_inplace_power, /*nb_inplace_power*/
(binaryfunc)array_inplace_left_shift, /*nb_inplace_lshift*/
(binaryfunc)array_inplace_right_shift, /*nb_inplace_rshift*/
(binaryfunc)array_inplace_bitwise_and, /*nb_inplace_and*/
(binaryfunc)array_inplace_bitwise_xor, /*nb_inplace_xor*/
(binaryfunc)array_inplace_bitwise_or, /*nb_inplace_or*/

(binaryfunc)array_floor_divide, /*nb_floor_divide*/
(binaryfunc)array_true_divide, /*nb_true_divide*/
Expand Down
39 changes: 28 additions & 11 deletions numpy/core/src/private/binop_override.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,22 @@
* where setting a special-method name to None is a signal that that method
* cannot be used.
*
* So for 1.13, we are going to try the following rules. a.__add__(b) will
* be implemented as follows:
* So for 1.13, we are going to try the following rules.
*
* For binops like a.__add__(b):
* - If b does not define __array_ufunc__, apply the legacy rule:
* - If not isinstance(b, a.__class__), and b.__array_priority__ is higher
* than a.__array_priority__, return NotImplemented
* - If b does define __array_ufunc__ but it is None, return NotImplemented
* - Otherwise, call the corresponding ufunc.
*
* For reversed operations like b.__radd__(a), and for in-place operations
* like a.__iadd__(b), we:
* - Call the corresponding ufunc
* For in-place operations like a.__iadd__(b)
* - If b does not define __array_ufunc__, apply the legacy rule:
* - If not isinstance(b, a.__class__), and b.__array_priority__ is higher
* than a.__array_priority__, return NotImplemented
* - Otherwise, call the corresponding ufunc.
*
* For reversed operations like b.__radd__(a) we call the corresponding ufunc.
*
* Rationale for __radd__: This is because by the time the reversed operation
* is called, there are only two possibilities: The first possibility is that
Expand All @@ -77,16 +82,19 @@
* above, because if __iadd__ returns NotImplemented then Python will silently
* convert the operation into an out-of-place operation, i.e. 'a += b' will
* silently become 'a = a + b'. We don't want to allow this for arrays,
* because it will create unexpected memory allocations, break views,
* etc.
* because it will create unexpected memory allocations, break views, etc.
* However, backwards compatibility requires that we follow the rules of
* __array_priority__ for arrays that define it. For classes that use the new
* __array_ufunc__ mechanism we simply defer to the ufunc. That has the effect
* that when the other array has__array_ufunc = None a TypeError will be raised.
*
* In the future we might change these rules further. For example, we plan to
* eventually deprecate __array_priority__ in cases where __array_ufunc__ is
* not present.
*/

static int
binop_override_forward_binop_should_defer(PyObject *self, PyObject *other)
binop_should_defer(PyObject *self, PyObject *other, int inplace)
{
/*
* This function assumes that self.__binop__(other) is underway and
Expand Down Expand Up @@ -123,7 +131,7 @@ binop_override_forward_binop_should_defer(PyObject *self, PyObject *other)
*/
attr = PyArray_GetAttrString_SuppressException(other, "__array_ufunc__");
if (attr) {
defer = (attr == Py_None);
defer = !inplace && (attr == Py_None);
Py_DECREF(attr);
return defer;
}
Expand Down Expand Up @@ -171,7 +179,16 @@ binop_override_forward_binop_should_defer(PyObject *self, PyObject *other)
#define BINOP_GIVE_UP_IF_NEEDED(m1, m2, slot_expr, test_func) \
do { \
if (BINOP_IS_FORWARD(m1, m2, slot_expr, test_func) && \
binop_override_forward_binop_should_defer((PyObject*)m1, (PyObject*)m2)) { \
binop_should_defer((PyObject*)m1, (PyObject*)m2, 0)) { \
Py_INCREF(Py_NotImplemented); \
return Py_NotImplemented; \
} \
} while (0)

#define INPLACE_GIVE_UP_IF_NEEDED(m1, m2, slot_expr, test_func) \
do { \
if (BINOP_IS_FORWARD(m1, m2, slot_expr, test_func) && \
binop_should_defer((PyObject*)m1, (PyObject*)m2, 1)) { \
Py_INCREF(Py_NotImplemented); \
return Py_NotImplemented; \
} \
Expand All @@ -187,7 +204,7 @@ binop_override_forward_binop_should_defer(PyObject *self, PyObject *other)
*/
#define RICHCMP_GIVE_UP_IF_NEEDED(m1, m2) \
do { \
if (binop_override_forward_binop_should_defer((PyObject*)m1, (PyObject*)m2)) { \
if (binop_should_defer((PyObject*)m1, (PyObject*)m2, 0)) { \
Py_INCREF(Py_NotImplemented); \
return Py_NotImplemented; \
} \
Expand Down
Loading
0