From 7f1293f7100d47824c12281238ce42aeef69167c Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Mon, 26 Aug 2019 17:38:16 -0700 Subject: MAINT: Avoid all BytesWarning A BytesWarning can be emitted when bytes are and strings are mismatched. Catching BytesWarning ensures a better boundary between str and bytes type. The test suite is now run with the -b flag to emit this warning. Fixes #9308 --- numpy/core/arrayprint.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'numpy/core/arrayprint.py') diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index b1310a737..a923f05ea 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -1470,7 +1470,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( @@ -1488,7 +1492,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, ' ', "") -- cgit v1.2.1