summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/core/arrayprint.py25
-rw-r--r--numpy/core/tests/test_arrayprint.py27
2 files changed, 43 insertions, 9 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py
index 01db605f6..0f3e5ea5c 100644
--- a/numpy/core/arrayprint.py
+++ b/numpy/core/arrayprint.py
@@ -710,20 +710,27 @@ def _extendLine_pretty(s, line, word, line_width, next_line_prefix, legacy):
"""
Extends line with nicely formatted (possibly multi-line) string ``word``.
"""
- if legacy == '1.13':
+ words = word.splitlines()
+ if len(words) == 1 or legacy == '1.13':
return _extendLine(s, line, word, line_width, next_line_prefix, legacy)
- words = word.splitlines()
line_length = len(line)
- s, line = _extendLine(
- s, line, words[0], line_width, next_line_prefix, legacy)
+ max_word_length = max(len(word) for word in words)
+ if line_length + max_word_length > line_width and \
+ len(line) > len(next_line_prefix):
+ s += line.rstrip() + '\n'
+ line = next_line_prefix + words[0]
+ indent = next_line_prefix
+ else:
+ line += words[0]
+ indent = line_length*' '
+
for word in words[1::]:
s += line.rstrip() + '\n'
- if line_length + len(word) > line_width \
- and line_length > len(next_line_prefix):
- line = next_line_prefix + word
- else:
- line = line_length*' ' + word
+ line = indent + word
+
+ suffix_length = max_word_length-len(words[-1])
+ line += suffix_length*' '
return s, line
diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py
index 64ef5b14e..a2703d81b 100644
--- a/numpy/core/tests/test_arrayprint.py
+++ b/numpy/core/tests/test_arrayprint.py
@@ -426,6 +426,33 @@ class TestArray2String:
' Line 2, None]], dtype=object)'
)
+ class MultiLineLong:
+ def __repr__(self):
+ return "Line 1\nLooooooooooongestLine2\nLongerLine 3"
+
+ a = np.array([[None, MultiLineLong()], [MultiLineLong(), None]])
+ assert_equal(
+ repr(a),
+ 'array([[None, Line 1\n'
+ ' LooooooooooongestLine2\n'
+ ' LongerLine 3 ],\n'
+ ' [Line 1\n'
+ ' LooooooooooongestLine2\n'
+ ' LongerLine 3 , None]], dtype=object)'
+ )
+ assert_equal(
+ np.array_repr(a, 20),
+ 'array([[None,\n'
+ ' Line 1\n'
+ ' LooooooooooongestLine2\n'
+ ' LongerLine 3 ],\n'
+ ' [Line 1\n'
+ ' LooooooooooongestLine2\n'
+ ' LongerLine 3 ,\n'
+ ' None]],\n'
+ ' dtype=object)'
+ )
+
def test_nested_array_repr(self):
a = np.empty((2, 2), dtype=object)
a[0, 0] = np.eye(2)