summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2020-09-03 14:07:31 -0500
committerGitHub <noreply@github.com>2020-09-03 14:07:31 -0500
commit1462f7741da034d913540bb5eff08ee695cd5f9f (patch)
tree4aba456566ff94ec07274467f45f39179e071c3a
parent99bcf99750d7da499bf3887e6acf89f025e0b9f2 (diff)
parent58d48d7bf7b59e33d168dcd6f375a5708b42012c (diff)
downloadnumpy-1462f7741da034d913540bb5eff08ee695cd5f9f.tar.gz
Merge pull request #17145 from charris/cleanup-pystring_asstring-usage
MAINT, BUG: Replace uses of PyString_AsString.
-rw-r--r--numpy/core/src/multiarray/ctors.c9
-rw-r--r--numpy/core/src/umath/umathmodule.c9
-rw-r--r--numpy/core/tests/test_regression.py10
3 files changed, 21 insertions, 7 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index ef446cd90..094cd55a9 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -1977,10 +1977,10 @@ PyArray_FromInterface(PyObject *origin)
"(data pointer integer, read-only flag)");
goto fail;
}
+
dataptr = PyTuple_GET_ITEM(attr, 0);
- if (PyString_Check(dataptr)) {
- res = sscanf(PyString_AsString(dataptr),
- "%p", (void **)&data);
+ if (PyBytes_Check(dataptr)) {
+ res = sscanf(PyBytes_AsString(dataptr), "%p", (void **)&data);
if (res < 1) {
PyErr_SetString(PyExc_TypeError,
"__array_interface__ data string cannot be converted");
@@ -1989,6 +1989,9 @@ PyArray_FromInterface(PyObject *origin)
}
else if (PyLong_Check(dataptr)) {
data = PyLong_AsVoidPtr(dataptr);
+ if (data == NULL && PyErr_Occurred()) {
+ goto fail;
+ }
}
else {
PyErr_SetString(PyExc_TypeError,
diff --git a/numpy/core/src/umath/umathmodule.c b/numpy/core/src/umath/umathmodule.c
index ba7ac1706..474db0245 100644
--- a/numpy/core/src/umath/umathmodule.c
+++ b/numpy/core/src/umath/umathmodule.c
@@ -75,7 +75,8 @@ ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) {
int nin, nout, i, nargs;
PyUFunc_PyFuncData *fdata;
PyUFuncObject *self;
- char *fname, *str, *types, *doc;
+ const char *fname = NULL;
+ char *str, *types, *doc;
Py_ssize_t fname_len = -1;
void * ptr, **data;
int offset[2];
@@ -95,12 +96,12 @@ ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *kwds) {
pyname = PyObject_GetAttrString(function, "__name__");
if (pyname) {
- (void) PyString_AsStringAndSize(pyname, &fname, &fname_len);
+ fname = PyUnicode_AsUTF8AndSize(pyname, &fname_len);
}
- if (PyErr_Occurred()) {
+ if (fname == NULL) {
+ PyErr_Clear();
fname = "?";
fname_len = 1;
- PyErr_Clear();
}
/*
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
index a97198076..2e731d4fa 100644
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -2514,3 +2514,13 @@ class TestRegression:
b[...] = 1
assert b.strides[0] > int32_max * b.dtype.itemsize
assert np.dot(b, b) == 2.0
+
+ def test_frompyfunc_name(self):
+ # name conversion was failing for python 3 strings
+ # resulting in the default '?' name. Also test utf-8
+ # encoding using non-ascii name.
+ def cassé(x):
+ return x
+
+ f = np.frompyfunc(cassé, 1, 1)
+ assert str(f) == "<ufunc 'cassé (vectorized)'>"