-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
MNT: replace _PyDict_GetItemStringWithError with PyDict_GetItemStringRef #26282
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
Changes from all commits
407689c
64ffcd1
5d67699
90c8049
26f7421
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
#include "numpy/npy_math.h" | ||
|
||
#include "npy_config.h" | ||
|
||
#include "npy_pycompat.h" | ||
#include "npy_ctypes.h" | ||
|
||
#include "multiarraymodule.h" | ||
|
@@ -2180,10 +2180,10 @@ PyArray_FromInterface(PyObject *origin) | |
} | ||
|
||
/* Get type string from interface specification */ | ||
attr = _PyDict_GetItemStringWithError(iface, "typestr"); | ||
if (attr == NULL) { | ||
int result = PyDict_GetItemStringRef(iface, "typestr", &attr); | ||
if (result <= 0) { | ||
Py_DECREF(iface); | ||
if (!PyErr_Occurred()) { | ||
if (result == 0) { | ||
PyErr_SetString(PyExc_ValueError, | ||
"Missing __array_interface__ typestr"); | ||
} | ||
|
@@ -2207,43 +2207,47 @@ PyArray_FromInterface(PyObject *origin) | |
* the 'descr' attribute. | ||
*/ | ||
if (dtype->type_num == NPY_VOID) { | ||
PyObject *descr = _PyDict_GetItemStringWithError(iface, "descr"); | ||
if (descr == NULL && PyErr_Occurred()) { | ||
PyObject *descr = NULL; | ||
result = PyDict_GetItemStringRef(iface, "descr", &descr); | ||
if (result == -1) { | ||
goto fail; | ||
} | ||
PyArray_Descr *new_dtype = NULL; | ||
if (descr != NULL) { | ||
if (result == 1) { | ||
int is_default = _is_default_descr(descr, attr); | ||
if (is_default < 0) { | ||
Py_DECREF(descr); | ||
goto fail; | ||
} | ||
if (!is_default) { | ||
if (PyArray_DescrConverter2(descr, &new_dtype) != NPY_SUCCEED) { | ||
Py_DECREF(descr); | ||
goto fail; | ||
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. Weird how we use |
||
} | ||
if (new_dtype != NULL) { | ||
Py_DECREF(dtype); | ||
dtype = new_dtype; | ||
Py_SETREF(dtype, new_dtype); | ||
} | ||
} | ||
|
||
} | ||
|
||
Py_DECREF(descr); | ||
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. @ngoldbaum this is at the wrong indentation level. It's too late here, and I am not sure where to put a test though. 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. Oops, thanks for catching. I unfortunately can't help with a test, I was doing a mechanical refactor here. 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. We must have some 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. My initial reading of your message was that it was a head's up and an ask for help. I'm happy to fix this but if, in the future, you want me to look at something, it would help me if you could say that explicitly in your initial message to me. 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. I don't think the indentation level is wrong, doesn't it just need to be an xdecref? I also don't immediately see how to write a test for this, the existing 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. Should have just created an issue. I wanted to look at it quickly, but realized that adding a test was not quite as quick as I had hoped, and hoped you might have a look. |
||
} | ||
Py_CLEAR(attr); | ||
|
||
/* Get shape tuple from interface specification */ | ||
attr = _PyDict_GetItemStringWithError(iface, "shape"); | ||
if (attr == NULL) { | ||
if (PyErr_Occurred()) { | ||
return NULL; | ||
} | ||
result = PyDict_GetItemStringRef(iface, "shape", &attr); | ||
if (result < 0) { | ||
return NULL; | ||
} | ||
if (result == 0) { | ||
/* Shape must be specified when 'data' is specified */ | ||
PyObject *data = _PyDict_GetItemStringWithError(iface, "data"); | ||
if (data == NULL && PyErr_Occurred()) { | ||
int result = PyDict_ContainsString(iface, "data"); | ||
if (result < 0) { | ||
Py_DECREF(attr); | ||
return NULL; | ||
} | ||
else if (data != NULL) { | ||
else if (result == 1) { | ||
Py_DECREF(iface); | ||
Py_DECREF(attr); | ||
PyErr_SetString(PyExc_ValueError, | ||
"Missing __array_interface__ shape"); | ||
return NULL; | ||
|
@@ -2271,10 +2275,11 @@ PyArray_FromInterface(PyObject *origin) | |
} | ||
} | ||
} | ||
Py_CLEAR(attr); | ||
|
||
/* Get data buffer from interface specification */ | ||
attr = _PyDict_GetItemStringWithError(iface, "data"); | ||
if (attr == NULL && PyErr_Occurred()){ | ||
result = PyDict_GetItemStringRef(iface, "data", &attr); | ||
if (result == -1){ | ||
return NULL; | ||
} | ||
|
||
|
@@ -2337,20 +2342,24 @@ PyArray_FromInterface(PyObject *origin) | |
PyBuffer_Release(&view); | ||
|
||
/* Get offset number from interface specification */ | ||
attr = _PyDict_GetItemStringWithError(iface, "offset"); | ||
if (attr == NULL && PyErr_Occurred()) { | ||
PyObject *offset = NULL; | ||
result = PyDict_GetItemStringRef(iface, "offset", &offset); | ||
if (result == -1) { | ||
goto fail; | ||
} | ||
else if (attr) { | ||
npy_longlong num = PyLong_AsLongLong(attr); | ||
else if (result == 1) { | ||
npy_longlong num = PyLong_AsLongLong(offset); | ||
if (error_converting(num)) { | ||
PyErr_SetString(PyExc_TypeError, | ||
"__array_interface__ offset must be an integer"); | ||
Py_DECREF(offset); | ||
goto fail; | ||
} | ||
data += num; | ||
Py_DECREF(offset); | ||
} | ||
} | ||
Py_CLEAR(attr); | ||
|
||
ret = (PyArrayObject *)PyArray_NewFromDescrAndBase( | ||
&PyArray_Type, dtype, | ||
|
@@ -2376,11 +2385,11 @@ PyArray_FromInterface(PyObject *origin) | |
goto fail; | ||
} | ||
} | ||
attr = _PyDict_GetItemStringWithError(iface, "strides"); | ||
if (attr == NULL && PyErr_Occurred()){ | ||
result = PyDict_GetItemStringRef(iface, "strides", &attr); | ||
if (result == -1){ | ||
return NULL; | ||
} | ||
if (attr != NULL && attr != Py_None) { | ||
if (result == 1 && attr != Py_None) { | ||
if (!PyTuple_Check(attr)) { | ||
PyErr_SetString(PyExc_TypeError, | ||
"strides must be a tuple"); | ||
|
@@ -2404,12 +2413,14 @@ PyArray_FromInterface(PyObject *origin) | |
if (n) { | ||
memcpy(PyArray_STRIDES(ret), strides, n*sizeof(npy_intp)); | ||
} | ||
Py_DECREF(attr); | ||
} | ||
PyArray_UpdateFlags(ret, NPY_ARRAY_UPDATE_ALL); | ||
Py_DECREF(iface); | ||
return (PyObject *)ret; | ||
|
||
fail: | ||
Py_XDECREF(attr); | ||
Py_XDECREF(dtype); | ||
Py_XDECREF(iface); | ||
return NULL; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
#include <stddef.h> | ||
|
||
#include "npy_config.h" | ||
|
||
#include "npy_pycompat.h" | ||
#include "npy_argparse.h" | ||
|
||
#include "numpy/arrayobject.h" | ||
|
@@ -142,8 +142,10 @@ PyUFunc_clearfperr() | |
NPY_NO_EXPORT int | ||
set_matmul_flags(PyObject *d) | ||
{ | ||
PyObject *matmul = _PyDict_GetItemStringWithError(d, "matmul"); | ||
if (matmul == NULL) { | ||
PyObject *matmul = NULL; | ||
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. Does this need an init? If it does, might make sense to mention that in the Python docs, @vstinner, even if not a big deal. (I.e. that it effectively uses But this would be a silly nitpick here to actually remove it. 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. You can check the doc: https://docs.python.org/dev/c-api/dict.html#c.PyDict_GetItemRef 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. Right, the docs are fine. It isn't like EDIT: Sorry, right... it lists that 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. I tend to initialize variables in C code even if I don't strictly need to. Probably a knee-jerk response from experiences of reading from an uninitialized variable causing heisenbugs. |
||
int result = PyDict_GetItemStringRef(d, "matmul", &matmul); | ||
if (result <= 0) { | ||
// caller sets an error if one isn't already set | ||
return -1; | ||
} | ||
/* | ||
|
@@ -162,6 +164,7 @@ set_matmul_flags(PyObject *d) | |
NPY_ITER_UPDATEIFCOPY | | ||
NPY_UFUNC_DEFAULT_OUTPUT_FLAGS) & | ||
~NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE; | ||
Py_DECREF(matmul); | ||
return 0; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.