diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2019-08-26 17:38:16 -0700 |
---|---|---|
committer | Jon Dufresne <jon.dufresne@gmail.com> | 2019-09-25 19:17:59 -0700 |
commit | 7f1293f7100d47824c12281238ce42aeef69167c (patch) | |
tree | 97f8ab3d7c341e63057dbdd44ecb1aa41d3dec49 /numpy/core/arrayprint.py | |
parent | aa3625f86302513fb7ca7288298e7cbe59d62fdd (diff) | |
download | numpy-7f1293f7100d47824c12281238ce42aeef69167c.tar.gz |
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
Diffstat (limited to 'numpy/core/arrayprint.py')
-rw-r--r-- | numpy/core/arrayprint.py | 8 |
1 files changed, 6 insertions, 2 deletions
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, ' ', "") |