diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2019-10-15 09:44:15 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-15 09:44:15 -0700 |
commit | b89caf6dd1341032fbffbe0888097a018037af00 (patch) | |
tree | 46056076194f4608eaaea0877f5ab0ab1b6ca0db | |
parent | c7f532e9acd1a2e2c7bd49f6355959511281c134 (diff) | |
parent | 7f1293f7100d47824c12281238ce42aeef69167c (diff) | |
download | numpy-b89caf6dd1341032fbffbe0888097a018037af00.tar.gz |
Merge pull request #14368 from jdufresne/byteswarning
MAINT: Avoid BytesWarning in PyArray_DescrConverter()
-rw-r--r-- | numpy/core/arrayprint.py | 8 | ||||
-rw-r--r-- | numpy/core/src/multiarray/descriptor.c | 17 | ||||
-rw-r--r-- | numpy/f2py/__init__.py | 1 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 2 | ||||
-rwxr-xr-x | tools/travis-test.sh | 2 | ||||
-rw-r--r-- | tox.ini | 2 |
6 files changed, 21 insertions, 11 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index 233d139fd..8a7626d9d 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -1479,7 +1479,11 @@ def array_repr(arr, max_line_width=None, precision=None, suppress_small=None): arr, max_line_width, precision, suppress_small) -_guarded_str = _recursive_guard()(str) +@_recursive_guard() +def _guarded_repr_or_str(v): + if isinstance(v, bytes): + return repr(v) + return str(v) def _array_str_implementation( @@ -1497,7 +1501,7 @@ def _array_str_implementation( # obtain a scalar and call str on it, avoiding problems for subclasses # for which indexing with () returns a 0d instead of a scalar by using # ndarray's getindex. Also guard against recursive 0d object arrays. - return _guarded_str(np.ndarray.__getitem__(a, ())) + return _guarded_repr_or_str(np.ndarray.__getitem__(a, ())) return array2string(a, max_line_width, precision, suppress_small, ' ', "") diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index 734255a9d..23d140cf6 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -1385,7 +1385,6 @@ NPY_NO_EXPORT int PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at) { int check_num = NPY_NOTYPE + 10; - PyObject *item; int elsize = 0; char endian = '='; @@ -1664,16 +1663,22 @@ finish: PyErr_Clear(); /* Now check to see if the object is registered in typeDict */ if (typeDict != NULL) { - item = PyDict_GetItem(typeDict, obj); + PyObject *item = NULL; #if defined(NPY_PY3K) - if (!item && PyBytes_Check(obj)) { + if (PyBytes_Check(obj)) { PyObject *tmp; tmp = PyUnicode_FromEncodedObject(obj, "ascii", "strict"); - if (tmp != NULL) { - item = PyDict_GetItem(typeDict, tmp); - Py_DECREF(tmp); + if (tmp == NULL) { + goto fail; } + item = PyDict_GetItem(typeDict, tmp); + Py_DECREF(tmp); + } + else { + item = PyDict_GetItem(typeDict, obj); } +#else + item = PyDict_GetItem(typeDict, obj); #endif if (item) { /* Check for a deprecated Numeric-style typecode */ diff --git a/numpy/f2py/__init__.py b/numpy/f2py/__init__.py index d146739bb..42e3632fd 100644 --- a/numpy/f2py/__init__.py +++ b/numpy/f2py/__init__.py @@ -109,6 +109,7 @@ def compile(source, output = '' else: status = 0 + output = output.decode() if verbose: print(output) finally: diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 6ee17c830..1181fe986 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -1871,7 +1871,7 @@ M 33 21.99 data = ["1, 1, 1, 1, -1.1"] * 50 mdata = TextIO("\n".join(data)) - converters = {4: lambda x: "(%s)" % x} + converters = {4: lambda x: "(%s)" % x.decode()} kwargs = dict(delimiter=",", converters=converters, dtype=[(_, int) for _ in 'abcde'],) assert_raises(ValueError, np.genfromtxt, mdata, **kwargs) diff --git a/tools/travis-test.sh b/tools/travis-test.sh index 6baa55817..6094f0ee6 100755 --- a/tools/travis-test.sh +++ b/tools/travis-test.sh @@ -88,7 +88,7 @@ run_test() if [ -n "$RUN_FULL_TESTS" ]; then export PYTHONWARNINGS="ignore::DeprecationWarning:virtualenv" - $PYTHON ../runtests.py -n -v --durations 10 --mode=full $COVERAGE_FLAG + $PYTHON -b ../runtests.py -n -v --durations 10 --mode=full $COVERAGE_FLAG else # disable --durations temporarily, pytest currently aborts # when that is used with python3.6-dbg @@ -32,7 +32,7 @@ envlist = [testenv] deps= -Ur{toxinidir}/test_requirements.txt changedir={envdir} -commands={envpython} {toxinidir}/runtests.py --mode=full {posargs:} +commands={envpython} -b {toxinidir}/runtests.py --mode=full {posargs:} [testenv:py37-not-relaxed-strides] basepython=python3.7 |