Skip to content

Commit

Permalink
MAINT: Removed deprecated NumPy C api from sparse (scipy#14387)
Browse files Browse the repository at this point in the history
  • Loading branch information
Smit-create committed Aug 27, 2021
1 parent 7da2474 commit 23b919b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
4 changes: 2 additions & 2 deletions scipy/sparse/setup.py
Expand Up @@ -6,7 +6,7 @@
def configuration(parent_package='',top_path=None):
from numpy.distutils.misc_util import Configuration
from scipy._build_utils.compiler_helper import set_cxx_flags_hook

from scipy._build_utils import numpy_nodepr_api
config = Configuration('sparse',parent_package,top_path)

config.add_data_dir('tests')
Expand Down Expand Up @@ -43,7 +43,7 @@ def get_sparsetools_sources(ext, build_dir):
'util.h']
depends = [os.path.join('sparsetools', hdr) for hdr in depends],
sparsetools = config.add_extension('_sparsetools',
define_macros=[('__STDC_FORMAT_MACROS', 1)],
define_macros=[('__STDC_FORMAT_MACROS', 1)] + numpy_nodepr_api['define_macros'],
depends=depends,
include_dirs=['sparsetools'],
sources=[os.path.join('sparsetools', 'sparsetools.cxx'),
Expand Down
25 changes: 12 additions & 13 deletions scipy/sparse/sparsetools/sparsetools.cxx
Expand Up @@ -136,7 +136,6 @@ call_thunk(char ret_spec, const char *spec, thunk_t *thunk, PyObject *args)
int n_supported_typenums;
int cur_typenum;
PyObject *arg;
PyArray_Descr *dtype;

if (j >= MAX_ARGS) {
PyErr_SetString(PyExc_ValueError,
Expand Down Expand Up @@ -215,9 +214,9 @@ call_thunk(char ret_spec, const char *spec, thunk_t *thunk, PyObject *args)
}

/* Find a compatible supported data type */
dtype = PyArray_DESCR(arg_arrays[j]);

for (k = 0; k < n_supported_typenums; ++k) {
if (PyArray_CanCastSafely(dtype->type_num, supported_typenums[k]) &&
if (PyArray_CanCastSafely(PyArray_TYPE((PyArrayObject *)arg_arrays[j]), supported_typenums[k]) &&
(cur_typenum == -1 || PyArray_CanCastSafely(cur_typenum, supported_typenums[k])))
{
cur_typenum = supported_typenums[k];
Expand Down Expand Up @@ -311,10 +310,10 @@ call_thunk(char ret_spec, const char *spec, thunk_t *thunk, PyObject *args)

/* Cast if necessary */
arg = arg_arrays[j];
if (PyArray_EquivTypenums(PyArray_DESCR(arg)->type_num, cur_typenum)) {
if (PyArray_EquivTypenums(PyArray_TYPE((PyArrayObject *)arg), cur_typenum)) {
/* No cast needed. */
}
else if (!is_output[j] || PyArray_CanCastSafely(cur_typenum, PyArray_DESCR(arg)->type_num)) {
else if (!is_output[j] || PyArray_CanCastSafely(cur_typenum, PyArray_TYPE((PyArrayObject *)arg))) {
/* Cast needed. Output arrays require safe cast back. */
arg_arrays[j] = c_array_from_object(arg, cur_typenum, is_output[j]);
Py_DECREF(arg);
Expand All @@ -331,11 +330,11 @@ call_thunk(char ret_spec, const char *spec, thunk_t *thunk, PyObject *args)
}

/* Grab value */
arg_list[j] = PyArray_DATA(arg_arrays[j]);
arg_list[j] = PyArray_DATA((PyArrayObject *)arg_arrays[j]);

/* Find maximum array size */
if (PyArray_SIZE(arg_arrays[j]) > max_array_size) {
max_array_size = PyArray_SIZE(arg_arrays[j]);
if (PyArray_SIZE((PyArrayObject *)arg_arrays[j]) > max_array_size) {
max_array_size = PyArray_SIZE((PyArrayObject *)arg_arrays[j]);
}
}

Expand Down Expand Up @@ -502,7 +501,7 @@ static PyObject *array_from_std_vector_and_free(int typenum, void *p)
npy_intp length = v->size(); \
PyObject *obj = PyArray_SimpleNew(1, &length, typenum); \
if (length > 0) { \
memcpy(PyArray_DATA(obj), &((*v)[0]), \
memcpy(PyArray_DATA((PyArrayObject *)obj), &((*v)[0]), \
sizeof(ctype)*length); \
} \
delete v; \
Expand All @@ -522,17 +521,17 @@ static PyObject *c_array_from_object(PyObject *obj, int typenum, int is_output)
{
if (!is_output) {
if (typenum == -1) {
return PyArray_FROM_OF(obj, NPY_C_CONTIGUOUS|NPY_NOTSWAPPED);
return PyArray_FROM_OF(obj, NPY_ARRAY_C_CONTIGUOUS|NPY_ARRAY_NOTSWAPPED);
}
else {
return PyArray_FROM_OTF(obj, typenum, NPY_C_CONTIGUOUS|NPY_NOTSWAPPED);
return PyArray_FROM_OTF(obj, typenum, NPY_ARRAY_C_CONTIGUOUS|NPY_ARRAY_NOTSWAPPED);
}
}
else {
#ifdef HAVE_WRITEBACKIFCOPY
int flags = NPY_C_CONTIGUOUS|NPY_WRITEABLE|NPY_ARRAY_WRITEBACKIFCOPY|NPY_NOTSWAPPED;
int flags = NPY_ARRAY_C_CONTIGUOUS|NPY_ARRAY_WRITEABLE|NPY_ARRAY_WRITEBACKIFCOPY|NPY_ARRAY_NOTSWAPPED;
#else
int flags = NPY_C_CONTIGUOUS|NPY_WRITEABLE|NPY_UPDATEIFCOPY|NPY_NOTSWAPPED;
int flags = NPY_ARRAY_C_CONTIGUOUS|NPY_ARRAY_WRITEABLE|NPY_UPDATEIFCOPY|NPY_ARRAY_NOTSWAPPED;
#endif
if (typenum == -1) {
return PyArray_FROM_OF(obj, flags);
Expand Down

0 comments on commit 23b919b

Please sign in to comment.