diff options
-rw-r--r-- | numpy/core/src/multiarray/einsum.c.src | 1 | ||||
-rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 42 | ||||
-rw-r--r-- | numpy/core/src/multiarray/new_iterator.c.src | 55 | ||||
-rw-r--r-- | numpy/core/src/multiarray/new_iterator_pywrap.c | 79 | ||||
-rw-r--r-- | numpy/core/src/umath/ufunc_object.c | 56 | ||||
-rw-r--r-- | numpy/core/tests/test_new_iterator.py | 7 |
6 files changed, 183 insertions, 57 deletions
diff --git a/numpy/core/src/multiarray/einsum.c.src b/numpy/core/src/multiarray/einsum.c.src index 7f7abcd3f..f5c91b170 100644 --- a/numpy/core/src/multiarray/einsum.c.src +++ b/numpy/core/src/multiarray/einsum.c.src @@ -15,6 +15,7 @@ #define _MULTIARRAYMODULE #include <numpy/ndarrayobject.h> #include <numpy/halffloat.h> +#include <numpy/npy_3kcompat.h> #include <ctype.h> diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 695a17a30..bbc3e8f23 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -552,7 +552,7 @@ PyArray_CanCoerceScalar(int thistype, int neededtype, * so 1 is returned, but * _npy_scalar_kinds_table[uint]==POSINT < NEGINT, * so 0 is returned, as required. - * + * */ neededscalar = _npy_scalar_kinds_table[neededtype]; if (neededscalar >= scalar) { @@ -1799,7 +1799,18 @@ array_count_nonzero(PyObject *NPY_UNUSED(self), PyObject *args) Py_DECREF(array); +#if defined(NPY_PY3K) + return (count == -1) ? NULL : PyLong_FromSsize_t(count); +#elif PY_VERSION_HEX >= 0x02050000 return (count == -1) ? NULL : PyInt_FromSsize_t(count); +#else + if ((npy_intp)((long)count) == count) { + return (count == -1) ? NULL : PyInt_FromLong(count); + } + else { + return (count == -1) ? NULL : PyLong_FromVoidPtr((void*)count); + } +#endif } static PyObject * @@ -1953,6 +1964,9 @@ array_einsum(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) PyArrayObject *out = NULL; PyArray_Descr *dtype = NULL; PyObject *ret = NULL; + PyObject *subscripts_str; + PyObject *str_obj = NULL; + PyObject *str_key_obj = NULL; nop = PyTuple_GET_SIZE(args) - 1; if (nop <= 0) { @@ -1967,8 +1981,18 @@ array_einsum(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) } /* Get the subscripts string */ - subscripts = PyString_AsString(PyTuple_GET_ITEM(args, 0)); + subscripts_str = PyTuple_GET_ITEM(args, 0); + if (PyUnicode_Check(subscripts_str)) { + str_obj = PyUnicode_AsASCIIString(subscripts_str); + if (str_obj == NULL) { + return NULL; + } + subscripts_str = str_obj; + } + + subscripts = PyBytes_AsString(subscripts_str); if (subscripts == NULL) { + Py_XDECREF(str_obj); return NULL; } @@ -1998,7 +2022,17 @@ array_einsum(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) PyObject *key, *value; Py_ssize_t pos = 0; while (PyDict_Next(kwds, &pos, &key, &value)) { - char *str = PyString_AsString(key); + char *str = NULL; + +#if defined(NPY_PY3K) + Py_XDECREF(str_key_obj); + str_key_obj = PyUnicode_AsASCIIString(key); + if (str_key_obj != NULL) { + key = str_key_obj; + } +#endif + + str = PyBytes_AsString(key); if (str == NULL) { PyErr_Clear(); @@ -2054,6 +2088,8 @@ finish: Py_XDECREF(op[i]); } Py_XDECREF(dtype); + Py_XDECREF(str_obj); + Py_XDECREF(str_key_obj); return ret; } diff --git a/numpy/core/src/multiarray/new_iterator.c.src b/numpy/core/src/multiarray/new_iterator.c.src index 8c8a1b897..ba7c687de 100644 --- a/numpy/core/src/multiarray/new_iterator.c.src +++ b/numpy/core/src/multiarray/new_iterator.c.src @@ -13,6 +13,7 @@ #define _MULTIARRAYMODULE #include <numpy/ndarrayobject.h> +#include <numpy/npy_3kcompat.h> #include "convert_datatype.h" #include "lowlevel_strided_loops.h" @@ -3101,10 +3102,10 @@ npyiter_shape_string(npy_intp n, npy_intp *vals, char *ending) } if (i == n) { - return PyString_FromFormat("()%s", ending); + return PyUString_FromFormat("()%s", ending); } else { - ret = PyString_FromFormat("(%zd", vals[i++]); + ret = PyUString_FromFormat("(%zd", vals[i++]); if (ret == NULL) { return NULL; } @@ -3112,24 +3113,24 @@ npyiter_shape_string(npy_intp n, npy_intp *vals, char *ending) for (; i < n; ++i) { if (vals[i] < 0) { - tmp = PyString_FromString(",newaxis"); + tmp = PyUString_FromString(",newaxis"); } else { - tmp = PyString_FromFormat(",%zd", vals[i]); + tmp = PyUString_FromFormat(",%zd", vals[i]); } if (tmp == NULL) { Py_DECREF(ret); return NULL; } - - PyString_ConcatAndDel(&ret, tmp); + + PyUString_ConcatAndDel(&ret, tmp); if (ret == NULL) { return NULL; } } - tmp = PyString_FromFormat(")%s", ending); - PyString_ConcatAndDel(&ret, tmp); + tmp = PyUString_FromFormat(")%s", ending); + PyUString_ConcatAndDel(&ret, tmp); return ret; } @@ -3458,8 +3459,8 @@ broadcast_error: { char *tmpstr; if (op_axes == NULL) { - errmsg = PyString_FromString("operands could not be broadcast " - "together with shapes "); + errmsg = PyUString_FromString("operands could not be broadcast " + "together with shapes "); for (iiter = 0; iiter < niter; ++iiter) { if (op[iiter] != NULL) { tmp = npyiter_shape_string(PyArray_NDIM(op[iiter]), @@ -3468,7 +3469,7 @@ broadcast_error: { if (tmp == NULL) { return 0; } - PyString_ConcatAndDel(&errmsg, tmp); + PyUString_ConcatAndDel(&errmsg, tmp); if (errmsg == NULL) { return 0; } @@ -3477,9 +3478,9 @@ broadcast_error: { PyErr_SetObject(PyExc_ValueError, errmsg); } else { - errmsg = PyString_FromString("operands could not be broadcast " - "together with remapped shapes " - "[original->remapped]: "); + errmsg = PyUString_FromString("operands could not be broadcast " + "together with remapped shapes " + "[original->remapped]: "); for (iiter = 0; iiter < niter; ++iiter) { if (op[iiter] != NULL) { npy_intp *axes = op_axes[iiter]; @@ -3491,7 +3492,7 @@ broadcast_error: { if (tmp == NULL) { return 0; } - PyString_ConcatAndDel(&errmsg, tmp); + PyUString_ConcatAndDel(&errmsg, tmp); if (errmsg == NULL) { return 0; } @@ -3511,7 +3512,7 @@ broadcast_error: { if (tmp == NULL) { return 0; } - PyString_ConcatAndDel(&errmsg, tmp); + PyUString_ConcatAndDel(&errmsg, tmp); if (errmsg == NULL) { return 0; } @@ -3530,12 +3531,12 @@ operand_different_than_broadcast: { /* Start of error message */ if (op_flags[iiter]&NPY_ITER_READONLY) { - errmsg = PyString_FromString("non-broadcastable operand " - "with shape "); + errmsg = PyUString_FromString("non-broadcastable operand " + "with shape "); } else { - errmsg = PyString_FromString("non-broadcastable output " - "operand with shape "); + errmsg = PyUString_FromString("non-broadcastable output " + "operand with shape "); } if (errmsg == NULL) { return 0; @@ -3547,7 +3548,7 @@ operand_different_than_broadcast: { if (tmp == NULL) { return 0; } - PyString_ConcatAndDel(&errmsg, tmp); + PyUString_ConcatAndDel(&errmsg, tmp); if (errmsg == NULL) { return 0; } @@ -3566,11 +3567,11 @@ operand_different_than_broadcast: { } } - tmp = PyString_FromString(" [remapped to "); + tmp = PyUString_FromString(" [remapped to "); if (tmp == NULL) { return 0; } - PyString_ConcatAndDel(&errmsg, tmp); + PyUString_ConcatAndDel(&errmsg, tmp); if (errmsg == NULL) { return 0; } @@ -3579,17 +3580,17 @@ operand_different_than_broadcast: { if (tmp == NULL) { return 0; } - PyString_ConcatAndDel(&errmsg, tmp); + PyUString_ConcatAndDel(&errmsg, tmp); if (errmsg == NULL) { return 0; } } - tmp = PyString_FromString(" doesn't match the broadcast shape "); + tmp = PyUString_FromString(" doesn't match the broadcast shape "); if (tmp == NULL) { return 0; } - PyString_ConcatAndDel(&errmsg, tmp); + PyUString_ConcatAndDel(&errmsg, tmp); if (errmsg == NULL) { return 0; } @@ -3607,7 +3608,7 @@ operand_different_than_broadcast: { if (tmp == NULL) { return 0; } - PyString_ConcatAndDel(&errmsg, tmp); + PyUString_ConcatAndDel(&errmsg, tmp); if (errmsg == NULL) { return 0; } diff --git a/numpy/core/src/multiarray/new_iterator_pywrap.c b/numpy/core/src/multiarray/new_iterator_pywrap.c index 948ff0be6..3a41353f0 100644 --- a/numpy/core/src/multiarray/new_iterator_pywrap.c +++ b/numpy/core/src/multiarray/new_iterator_pywrap.c @@ -12,6 +12,7 @@ #define _MULTIARRAYMODULE #include <numpy/ndarrayobject.h> +#include <numpy/npy_3kcompat.h> #include "npy_config.h" @@ -113,7 +114,20 @@ NpyIter_GlobalFlagsConverter(PyObject *flags_in, npy_uint32 *flags) if (f == NULL) { return 0; } - if (PyString_AsStringAndSize(f, &str, &length) == -1) { + + if (PyUnicode_Check(f)) { + /* accept unicode input */ + PyObject *f_str; + f_str = PyUnicode_AsASCIIString(f); + if (f_str == NULL) { + Py_DECREF(f); + return 0; + } + Py_DECREF(f); + f = f_str; + } + + if (PyBytes_AsStringAndSize(f, &str, &length) == -1) { Py_DECREF(f); return 0; } @@ -203,8 +217,21 @@ npyiter_order_converter(PyObject *order_in, NPY_ORDER *order) { char *str = NULL; Py_ssize_t length = 0; - - if (PyString_AsStringAndSize(order_in, &str, &length) == -1) { + + if (PyUnicode_Check(order_in)) { + /* accept unicode input */ + PyObject *str_obj; + int ret; + str_obj = PyUnicode_AsASCIIString(order_in); + if (str_obj == NULL) { + return 0; + } + ret = npyiter_order_converter(str_obj, order); + Py_DECREF(str_obj); + return ret; + } + + if (PyBytes_AsStringAndSize(order_in, &str, &length) == -1) { return 0; } @@ -224,7 +251,7 @@ npyiter_order_converter(PyObject *order_in, NPY_ORDER *order) } PyErr_SetString(PyExc_ValueError, - "order must be one of 'C', 'F', 'A', or 'K'"); + "order must be one of 'C', 'F', 'A', or 'K'"); return 0; } @@ -237,10 +264,23 @@ PyArray_CastingConverter(PyObject *obj, NPY_CASTING *casting) { char *str = NULL; Py_ssize_t length = 0; - - if (PyString_AsStringAndSize(obj, &str, &length) == -1) { + + if (PyUnicode_Check(obj)) { + PyObject *str_obj; + int ret; + str_obj = PyUnicode_AsASCIIString(obj); + if (str_obj == NULL) { + return 0; + } + ret = PyArray_CastingConverter(str_obj, casting); + Py_DECREF(str_obj); + return ret; + } + + if (PyBytes_AsStringAndSize(obj, &str, &length) == -1) { return 0; } + if (length >= 2) switch (str[2]) { case 0: if (strcmp(str, "no") == 0) { @@ -278,7 +318,6 @@ PyArray_CastingConverter(PyObject *obj, NPY_CASTING *casting) "casting must be one of 'no', 'equiv', 'safe', " "'same_kind', or 'unsafe'"); return 0; - } static int @@ -307,7 +346,19 @@ NpyIter_OpFlagsConverter(PyObject *op_flags_in, return 0; } - if (PyString_AsStringAndSize(f, &str, &length) == -1) { + if (PyUnicode_Check(f)) { + /* accept unicode input */ + PyObject *f_str; + f_str = PyUnicode_AsASCIIString(f); + if (f_str == NULL) { + Py_DECREF(f); + return 0; + } + Py_DECREF(f); + f = f_str; + } + + if (PyBytes_AsStringAndSize(f, &str, &length) == -1) { Py_DECREF(f); PyErr_SetString(PyExc_ValueError, "op_flags must be a tuple or array of per-op flag-tuples"); @@ -412,7 +463,7 @@ npyiter_convert_op_flags_array(PyObject *op_flags_in, return 0; } /* If the first item is a string, try as one set of flags */ - if (iiter == 0 && (PyString_Check(f) || PyUnicode_Check(f))) { + if (iiter == 0 && (PyBytes_Check(f) || PyUnicode_Check(f))) { Py_DECREF(f); goto try_single_flags; } @@ -1114,7 +1165,7 @@ npyiter_dealloc(NewNpyArrayIterObject *self) Py_XDECREF(self->nested_child); self->nested_child = NULL; } - self->ob_type->tp_free((PyObject*)self); + Py_TYPE(self)->tp_free((PyObject*)self); } static int @@ -1706,7 +1757,11 @@ static PyObject *npyiter_iterrange_get(NewNpyArrayIterObject *self) static int npyiter_iterrange_set(NewNpyArrayIterObject *self, PyObject *value) { +#if PY_VERSION_HEX >= 0x02050000 npy_intp istart = 0, iend = 0; +#else + long istart = 0, iend = 0; +#endif if (self->iter == NULL) { PyErr_SetString(PyExc_ValueError, @@ -1720,7 +1775,11 @@ static int npyiter_iterrange_set(NewNpyArrayIterObject *self, PyObject *value) return -1; } +#if PY_VERSION_HEX >= 0x02050000 if (!PyArg_ParseTuple(value, "nn", &istart, &iend)) { +#else + if (!PyArg_ParseTuple(value, "ll", &istart, &iend)) { +#endif return -1; } diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 08f569502..9b6705707 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -704,6 +704,7 @@ static int get_ufunc_arguments(PyUFuncObject *self, { npy_intp i, nargs, nin = self->nin; PyObject *obj, *context; + PyObject *str_key_obj = NULL; char *ufunc_name; int any_flexible = 0, any_object = 0; @@ -797,10 +798,18 @@ static int get_ufunc_arguments(PyUFuncObject *self, Py_ssize_t length = 0; char *str = NULL; int bad_arg = 1; - - if (PyString_AsStringAndSize(key, &str, &length) == -1) { + +#if defined(NPY_PY3K) + Py_XDECREF(str_key_obj); + str_key_obj = PyUnicode_AsASCIIString(key); + if (str_key_obj != NULL) { + key = str_key_obj; + } +#endif + + if (PyBytes_AsStringAndSize(key, &str, &length) == -1) { PyErr_SetString(PyExc_TypeError, "invalid keyword argument"); - return -1; + goto fail; } switch (str[0]) { @@ -808,7 +817,7 @@ static int get_ufunc_arguments(PyUFuncObject *self, /* Provides a policy for allowed casting */ if (strncmp(str,"casting",7) == 0) { if (!PyArray_CastingConverter(value, out_casting)) { - return -1; + goto fail; } bad_arg = 0; } @@ -830,14 +839,14 @@ static int get_ufunc_arguments(PyUFuncObject *self, PyErr_SetString(PyExc_ValueError, "cannot specify 'out' as both a " "positional and keyword argument"); - return -1; + goto fail; } if (PyArray_Check(value)) { if (!PyArray_ISWRITEABLE(value)) { PyErr_SetString(PyExc_ValueError, "return array is not writeable"); - return -1; + goto fail; } Py_INCREF(value); out_op[nin] = (PyArrayObject *)value; @@ -846,14 +855,14 @@ static int get_ufunc_arguments(PyUFuncObject *self, PyErr_SetString(PyExc_TypeError, "return arrays must be " "of ArrayType"); - return -1; + goto fail; } bad_arg = 0; } /* Allows the default output layout to be overridden */ else if (strncmp(str,"order",5) == 0) { if (!PyArray_OrderConverter(value, out_order)) { - return -1; + goto fail; } bad_arg = 0; } @@ -864,7 +873,7 @@ static int get_ufunc_arguments(PyUFuncObject *self, if (*out_typetup != NULL) { PyErr_SetString(PyExc_RuntimeError, "cannot specify both 'sig' and 'dtype'"); - return -1; + goto fail; } *out_typetup = value; Py_INCREF(value); @@ -877,13 +886,13 @@ static int get_ufunc_arguments(PyUFuncObject *self, /* Allow this parameter to be None */ PyArray_Descr *dtype; if (!PyArray_DescrConverter2(value, &dtype)) { - return -1; + goto fail; } if (dtype != NULL) { if (*out_typetup != NULL) { PyErr_SetString(PyExc_RuntimeError, "cannot specify both 'sig' and 'dtype'"); - return -1; + goto fail; } *out_typetup = Py_BuildValue("(N)", dtype); } @@ -894,14 +903,19 @@ static int get_ufunc_arguments(PyUFuncObject *self, if (bad_arg) { char *format = "'%s' is an invalid keyword to ufunc '%s'"; PyErr_Format(PyExc_TypeError, format, str, ufunc_name); - return -1; + goto fail; } } } *out_any_object = any_object; + Py_XDECREF(str_key_obj); return 0; + +fail: + Py_XDECREF(str_key_obj); + return -1; } static const char * @@ -1447,11 +1461,21 @@ find_specified_ufunc_inner_loop(PyUFuncObject *self, n_specified = n; } - else if (PyString_Check(type_tup)) { + else if (PyBytes_Check(type_tup) || PyUnicode_Check(type_tup)) { Py_ssize_t length; char *str; + PyObject *str_obj = NULL; + + if (PyUnicode_Check(type_tup)) { + str_obj = PyUnicode_AsASCIIString(type_tup); + if (str_obj == NULL) { + return -1; + } + type_tup = str_obj; + } - if (!PyString_AsStringAndSize(type_tup, &str, &length) < 0) { + if (!PyBytes_AsStringAndSize(type_tup, &str, &length) < 0) { + Py_XDECREF(str_obj); return -1; } if (length != 1 && (length != niter + 2 || @@ -1463,6 +1487,7 @@ find_specified_ufunc_inner_loop(PyUFuncObject *self, "and %d after the -> sign", self->name ? self->name : "(unknown)", self->nin, self->nout); + Py_XDECREF(str_obj); return -1; } if (length == 1) { @@ -1470,6 +1495,7 @@ find_specified_ufunc_inner_loop(PyUFuncObject *self, n_specified = 1; dtype = PyArray_DescrFromType(str[0]); if (dtype == NULL) { + Py_XDECREF(str_obj); return -1; } NPY_UF_DBG_PRINTF("signature character '%c', type num %d\n", @@ -1486,6 +1512,7 @@ find_specified_ufunc_inner_loop(PyUFuncObject *self, dtype = PyArray_DescrFromType(str[istr]); if (dtype == NULL) { + Py_XDECREF(str_obj); return -1; } NPY_UF_DBG_PRINTF("signature character '%c', type num %d\n", @@ -1494,6 +1521,7 @@ find_specified_ufunc_inner_loop(PyUFuncObject *self, Py_DECREF(dtype); } } + Py_XDECREF(str_obj); } /* If the ufunc has userloops, search for them. */ diff --git a/numpy/core/tests/test_new_iterator.py b/numpy/core/tests/test_new_iterator.py index ad51862c9..04218ed3f 100644 --- a/numpy/core/tests/test_new_iterator.py +++ b/numpy/core/tests/test_new_iterator.py @@ -1,5 +1,6 @@ import numpy as np -from numpy import array, arange, newiter +from numpy import array, arange, newiter, all +from numpy.compat import asbytes from numpy.testing import * import sys, warnings @@ -1897,12 +1898,12 @@ def test_iter_buffering_badwriteback(): def test_iter_buffering_string(): # Safe casting disallows shrinking strings - a = np.array(['abc', 'a', 'abcd'], dtype=np.str) + a = np.array(['abc', 'a', 'abcd'], dtype=np.bytes_) assert_equal(a.dtype, np.dtype('S4')); assert_raises(TypeError,newiter,a,['buffered'],['readonly'], op_dtypes='S2') i = newiter(a, ['buffered'], ['readonly'], op_dtypes='S6') - assert_equal(i[0], 'abc') + assert_equal(i[0], asbytes('abc')) assert_equal(i[0].dtype, np.dtype('S6')) a = np.array(['abc', 'a', 'abcd'], dtype=np.unicode) |