8000 BUG/ENH: Switch to simplified __numpy_ufunc__/binop interaction by njsmith · Pull Request #6001 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG/ENH: Switch to simplified __numpy_ufunc__/binop interaction #6001

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

Closed
wants to merge 3 commits into from
Closed
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
45 changes: 10 additions & 35 deletions numpy/core/src/multiarray/arrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ maintainer email: oliphant.travis@ieee.org
#include "array_assign.h"
#include "alloc.h"

#include "binop_override.h"

/*NUMPY_API
Compute the size of an array (in number of items)
*/
Expand Down Expand Up @@ -1326,23 +1328,12 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)

switch (cmp_op) {
case Py_LT:
if (needs_right_binop_forward(obj_self, other, "__gt__", 0) &&
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
/* See discussion in number.c */
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
result = PyArray_GenericBinaryFunction(self, other,
n_ops.less);
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
result = PyArray_GenericBinaryFunction(self, other, n_ops.less);
break;
case Py_LE:
if (needs_right_binop_forward(obj_self, other, "__ge__", 0) &&
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
result = PyArray_GenericBinaryFunction(self, other,
n_ops.less_equal);
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
result = PyArray_GenericBinaryFunction(self, other, n_ops.less_equal);
break;
case Py_EQ:
if (other == Py_None) {
Expand Down Expand Up @@ -1403,11 +1394,7 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
return result;
}

if (needs_right_binop_forward(obj_self, other, "__eq__", 0) &&
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
result = PyArray_GenericBinaryFunction(self,
(PyObject *)other,
n_ops.equal);
Expand Down Expand Up @@ -1491,11 +1478,7 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
return result;
}

if (needs_right_binop_forward(obj_self, other, "__ne__", 0) &&
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
result = PyArray_GenericBinaryFunction(self, (PyObject *)other,
n_ops.not_equal);
if (result == NULL) {
Expand All @@ -1515,20 +1498,12 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op)
}
break;
case Py_GT:
if (needs_right_binop_forward(obj_self, other, "__lt__", 0) &&
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
result = PyArray_GenericBinaryFunction(self, other,
n_ops.greater);
break;
case Py_GE:
if (needs_right_binop_forward(obj_self, other, "__le__", 0) &&
Py_TYPE(obj_self)->tp_richcompare != Py_TYPE(other)->tp_richcompare) {
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
}
RICHCMP_GIVE_UP_IF_NEEDED(obj_self, other);
result = PyArray_GenericBinaryFunction(self, other,
n_ops.greater_equal);
break;
Expand Down
57 changes: 2 additions & 55 deletions numpy/core/src/multiarray/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "common.h"
#include "buffer.h"

#include "get_attr_string.h"

/*
* The casting to use for implicit assignment operations resulting from
* in-place operations (like +=) and out= arguments. (Notice that this
Expand All @@ -29,61 +31,6 @@
* warning (that people's code will be broken in a future release.)
*/

/*
* PyArray_GetAttrString_SuppressException:
*
* Stripped down version of PyObject_GetAttrString,
* avoids lookups for None, tuple, and List objects,
* and doesn't create a PyErr since this code ignores it.
*
* This can be much faster then PyObject_GetAttrString where
* exceptions are not used by caller.
*
* 'obj' is the object to search for attribute.
*
* 'name' is the attribute to search for.
*
* Returns attribute value on success, 0 on failure.
*/
PyObject *
PyArray_GetAttrString_SuppressException(PyObject *obj, char *name)
{
PyTypeObject *tp = Py_TYPE(obj);
PyObject *res = (PyObject *)NULL;

/* We do not need to check for special attributes on trivial types */
if (_is_basic_python_type(obj)) {
return NULL;
}

/* Attribute referenced by (char *)name */
if (tp->tp_getattr != NULL) {
res = (*tp->tp_getattr)(obj, name);
if (res == NULL) {
PyErr_Clear();
}
}
/* Attribute referenced by (PyObject *)name */
else if (tp->tp_getattro != NULL) {
#if defined(NPY_PY3K)
PyObject *w = PyUnicode_InternFromString(name);
#else
PyObject *w = PyString_InternFromString(name);
#endif
if (w == NULL) {
return (PyObject *)NULL;
}
res = (*tp->tp_getattro)(obj, w);
Py_DECREF(w);
if (res == NULL) {
PyErr_Clear();
}
}
return res;
}



NPY_NO_EXPORT NPY_CASTING NPY_DEFAULT_ASSIGN_CASTING = NPY_SAME_KIND_CASTING;


Expand Down
31 changes: 0 additions & 31 deletions numpy/core/src/multiarray/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ NPY_NO_EXPORT int
PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims,
PyArray_Descr **out_dtype, int string_status);

NPY_NO_EXPORT PyObject *
PyArray_GetAttrString_SuppressException(PyObject *v, char *name);

/*
* Returns NULL without setting an exception if no scalar is matched, a
* new dtype reference otherwise.
Expand Down Expand Up @@ -188,34 +185,6 @@ npy_memchr(char * haystack, char needle,
return p;
}

static NPY_INLINE int
_is_basic_python_type(PyObject * obj)
{
if (obj == Py_None ||
PyBool_Check(obj) ||
/* Basic number types */
#if !defined(NPY_PY3K)
PyInt_CheckExact(obj) ||
PyString_CheckExact(obj) ||
#endif
PyLong_CheckExact(obj) ||
PyFloat_CheckExact(obj) ||
PyComplex_CheckExact(obj) ||
/* Basic sequence types */
PyList_CheckExact(obj) ||
PyTuple_CheckExact(obj) ||
PyDict_CheckExact(obj) ||
PyAnySet_CheckExact(obj) ||
PyUnicode_CheckExact(obj) ||
PyBytes_CheckExact(obj) ||
PySlice_Check(obj)) {

return 1;
}

return 0;
}

/*
* Convert NumPy stride to BLAS stride. Returns 0 if conversion cannot be done
* (BLAS won't handle negative or zero strides the way we want).
Expand Down
2 changes: 2 additions & 0 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "alloc.h"
#include <assert.h>

#include "get_attr_string.h"

/*
* Reading from a file or a string.
*
Expand Down
2 changes: 2 additions & 0 deletions numpy/core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 5FCD +61,8 @@ NPY_NO_EXPORT int NPY_NUMUSERTYPES = 0;
#include "templ_common.h" /* for npy_mul_with_overflow_intp */
#include "compiled_base.h"

#include "get_attr_string.h"

/* Only here for API compatibility */
NPY_NO_EXPORT PyTypeObject PyBigArray_Type;

Expand Down
Loading
0