-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Remove NotImplemented handling from the ufunc machinery (almost) #5864
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
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bc41aa5
MAINT: move the special case for string comparison before the regular…
njsmith 069f0ff
MAINT: move the special case for void comparison before the regular case
njsmith c3c819a
MAINT: add __array_priority__ special case to masked array binary ops
njsmith acd37c8
MAINT: Remove NotImplemented handling from ufuncs (almost)
njsmith 6270100
MAINT: give more details on assert_deprecation failures
njsmith d3e763c
MAINT: make the deprecation warnings a little more distinctive
njsmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1295,6 +1295,34 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) | |
PyObject *obj_self = (PyObject *)self; | ||
PyObject *result = NULL; | ||
|
||
/* Special case for string arrays (which don't and currently can't have | ||
* ufunc loops defined, so there's no point in trying). | ||
*/ | ||
if (PyArray_ISSTRING(self)) { | ||
array_other = (PyArrayObject *)PyArray_FromObject(other, | ||
NPY_NOTYPE, 0, 0); | ||
if (array_other == NULL) { | ||
PyErr_Clear(); | ||
/* Never mind, carry on, see what happens */ | ||
} | ||
else if (!PyArray_ISSTRING(array_other)) { | ||
Py_DECREF(array_other); | ||
/* Never mind, carry on, see what happens */ | ||
} | ||
else { | ||
return _strings_richcompare(self, array_other, cmp_op, 0); | ||
} | ||
/* If we reach this point, it means that we are not comparing | ||
* string-to-string. It's possible that this will still work out, | ||
* e.g. if the other array is an object array, then both will be cast | ||
* to object or something? I don't know how that works actually, but | ||
* it does, b/c this works: | ||
* l = ["a", "b"] | ||
* assert np.array(l, dtype="S1") == np.array(l, dtype="O") | ||
* So we fall through and see what happens. | ||
*/ | ||
} | ||
|
||
switch (cmp_op) { | ||
case Py_LT: | ||
if (needs_right_binop_forward(obj_self, other, "__gt__", 0) && | ||
|
@@ -1324,16 +1352,6 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) | |
Py_INCREF(Py_False); | ||
return Py_False; | ||
} | ||
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; | ||
} | ||
result = PyArray_GenericBinaryFunction(self, | ||
(PyObject *)other, | ||
n_ops.equal); | ||
if (result && result != Py_NotImplemented) | ||
break; | ||
|
||
/* | ||
* The ufunc does not support void/structured types, so these | ||
|
@@ -1351,6 +1369,11 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) | |
*/ | ||
if (array_other == NULL) { | ||
PyErr_Clear(); | ||
if (DEPRECATE( | ||
"elementwise == comparison failed and returning scalar " | ||
"instead; this will raise an error in the future.") < 0) { | ||
return NULL; | ||
} | ||
Py_INCREF(Py_NotImplemented); | ||
return Py_NotImplemented; | ||
} | ||
|
@@ -1359,18 +1382,31 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) | |
PyArray_DESCR(array_other), | ||
NPY_EQUIV_CASTING); | ||
if (_res == 0) { | ||
Py_DECREF(result); | ||
Py_DECREF(array_other); | ||
if (DEPRECATE_FUTUREWARNING( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But that won't work here. Hmm.... |
||
"elementwise == comparison failed and returning scalar " | ||
"instead; this will raise an error or perform " | ||
"elementwise comparison in the future.") < 0) { | ||
return NULL; | ||
} | ||
Py_INCREF(Py_False); | ||
return Py_False; | ||
} | ||
else { | ||
Py_DECREF(result); | ||
result = _void_compare(self, array_other, cmp_op); | ||
} | ||
Py_DECREF(array_other); | ||
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; | ||
} | ||
result = PyArray_GenericBinaryFunction(self, | ||
(PyObject *)other, | ||
n_ops.equal); | ||
/* | ||
* If the comparison results in NULL, then the | ||
* two array objects can not be compared together; | ||
|
@@ -1382,8 +1418,8 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) | |
* is not possible. | ||
*/ | ||
PyErr_Clear(); | ||
if (DEPRECATE("elementwise comparison failed; " | ||
"this will raise the error in the future.") < 0) { | ||
if (DEPRECATE("elementwise == comparison failed; " | ||
"this will raise an error in the future.") < 0) { | ||
return NULL; | ||
} | ||
|
||
|
@@ -1400,15 +1436,6 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) | |
Py_INCREF(Py_True); | ||
return Py_True; | ||
} | ||
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; | ||
} | ||
result = PyArray_GenericBinaryFunction(self, (PyObject *)other, | ||
n_ops.not_equal); | ||
if (result && result != Py_NotImplemented) | ||
break; | ||
|
||
/* | ||
* The ufunc does not support void/structured types, so these | ||
|
@@ -1426,6 +1453,11 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) | |
*/ | ||
if (array_other == NULL) { | ||
PyErr_Clear(); | ||
if (DEPRECATE( | ||
"elementwise != comparison failed and returning scalar " | ||
"instead; this will raise an error in the future.") < 0) { | ||
return NULL; | ||
} | ||
Py_INCREF(Py_NotImplemented); | ||
return Py_NotImplemented; | ||
} | ||
|
@@ -1434,27 +1466,38 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) | |
PyArray_DESCR(array_other), | ||
NPY_EQUIV_CASTING); | ||
if (_res == 0) { | ||
Py_DECREF(result); | ||
Py_DECREF(array_other); | ||
if (DEPRECATE_FUTUREWARNING( | ||
"elementwise != comparison failed and returning scalar " | ||
"instead; this will raise an error or perform " | ||
"elementwise comparison in the future.") < 0) { | ||
return NULL; | ||
} | ||
Py_INCREF(Py_True); | ||
return Py_True; | ||
} | ||
else { | ||
Py_DECREF(result); | ||
result = _void_compare(self, array_other, cmp_op); | ||
Py_DECREF(array_other); | ||
} | ||
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; | ||
} | ||
result = PyArray_GenericBinaryFunction(self, (PyObject *)other, | ||
n_ops.not_equal); | ||
if (result == NULL) { | ||
/* | ||
* Comparisons should raise errors when element-wise comparison | ||
* is not possible. | ||
*/ | ||
PyErr_Clear(); | ||
if (DEPRECATE("elementwise comparison failed; " | ||
"this will raise the error in the future.") < 0) { | ||
if (DEPRECATE("elementwise != comparison failed; " | ||
"this will raise an error in the future.") < 0) { | ||
return NULL; | ||
} | ||
|
||
|
@@ -1484,24 +1527,6 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) | |
result = Py_NotImplemented; | ||
Py_INCREF(result); | ||
} | ||
if (result == Py_NotImplemented) { | ||
/* Try to handle string comparisons */ | ||
if (PyArray_TYPE(self) == NPY_OBJECT) { | ||
return result; | ||
} | ||
array_other = (PyArrayObject *)PyArray_FromObject(other, | ||
NPY_NOTYPE, 0, 0); | ||
if (array_other == NULL) { | ||
PyErr_Clear(); | ||
return result; | ||
} | ||
if (PyArray_ISSTRING(self) && PyArray_ISSTRING(array_other)) { | ||
Py_DECREF(result); | ||
result = _strings_richcompare(self, (PyArrayObject *) | ||
array_other, cmp_op, 0); | ||
} | ||
Py_DECREF(array_other); | ||
} | ||
return result; | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be clearer if the text lines up as an argument to
DEPRECATE(
OK, let's not worry about it. It will go away eventually anyway.