Skip to content

Commit

Permalink
Merge pull request numpy#14368 from jdufresne/byteswarning
Browse files Browse the repository at this point in the history
MAINT: Avoid BytesWarning in PyArray_DescrConverter()
  • Loading branch information
seberg authored Oct 15, 2019
2 parents c7f532e + 7f1293f commit b89caf6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
8 changes: 6 additions & 2 deletions numpy/core/arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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, ' ', "")

Expand Down
17 changes: 11 additions & 6 deletions numpy/core/src/multiarray/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '=';

Expand Down Expand Up @@ -1664,16 +1663,22 @@ PyArray_DescrConverter(PyObject *obj, PyArray_Descr **at)
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 */
Expand Down
1 change: 1 addition & 0 deletions numpy/f2py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def compile(source,
output = ''
else:
status = 0
output = output.decode()
if verbose:
print(output)
finally:
Expand Down
2 changes: 1 addition & 1 deletion numpy/lib/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1871,7 +1871,7 @@ def test_inconsistent_dtype(self):
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)
Expand Down
2 changes: 1 addition & 1 deletion tools/travis-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b89caf6

Please sign in to comment.