Skip to content

BUG: stray comma should be preserved for legacy printing #10120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions numpy/core/arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,8 @@ def _array2string(a, options, separator=' ', prefix=""):
next_line_prefix += " "*len(prefix)

lst = _formatArray(a, format_function, a.ndim, options['linewidth'],
next_line_prefix, separator,
options['edgeitems'], summary_insert)[:-1]
next_line_prefix, separator, options['edgeitems'],
summary_insert, options['legacy'])[:-1]
return lst


Expand Down Expand Up @@ -617,8 +617,8 @@ def _extendLine(s, line, word, max_line_len, next_line_prefix):
return s, line


def _formatArray(a, format_function, rank, max_line_len,
next_line_prefix, separator, edge_items, summary_insert):
def _formatArray(a, format_function, rank, max_line_len, next_line_prefix,
separator, edge_items, summary_insert, legacy):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe default to =False, just in case anyone is relying on these internals? Not that we have to support them, but it's easy to do so.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I want to gut this whole function to fix #9816, so let's leave this as is

"""formatArray is designed for two modes of operation:

1. Full output
Expand All @@ -633,6 +633,8 @@ def _formatArray(a, format_function, rank, max_line_len,
leading_items = edge_items
trailing_items = edge_items
summary_insert1 = summary_insert + separator
if legacy == '1.13':
summary_insert1 = summary_insert + ', '
else:
leading_items = 0
trailing_items = len(a)
Expand All @@ -646,7 +648,8 @@ def _formatArray(a, format_function, rank, max_line_len,
s, line = _extendLine(s, line, word, max_line_len, next_line_prefix)

if summary_insert1:
s, line = _extendLine(s, line, summary_insert1, max_line_len, next_line_prefix)
s, line = _extendLine(s, line, summary_insert1, max_line_len,
next_line_prefix)

for i in range(trailing_items, 1, -1):
word = format_function(a[-i]) + separator
Expand All @@ -664,7 +667,7 @@ def _formatArray(a, format_function, rank, max_line_len,
s += next_line_prefix
s += _formatArray(a[i], format_function, rank-1, max_line_len,
" " + next_line_prefix, separator, edge_items,
summary_insert)
summary_insert, legacy)
s = s.rstrip() + sep.rstrip() + '\n'*max(rank-1, 1)

if summary_insert1:
Expand All @@ -675,13 +678,13 @@ def _formatArray(a, format_function, rank, max_line_len,
s += next_line_prefix
s += _formatArray(a[-i], format_function, rank-1, max_line_len,
" " + next_line_prefix, separator, edge_items,
summary_insert)
summary_insert, legacy)
s = s.rstrip() + sep.rstrip() + '\n'*max(rank-1, 1)
if leading_items or trailing_items > 1:
s += next_line_prefix
s += _formatArray(a[-1], format_function, rank-1, max_line_len,
" " + next_line_prefix, separator, edge_items,
summary_insert).rstrip()+']\n'
summary_insert, legacy).rstrip()+']\n'
return s


Expand Down
7 changes: 7 additions & 0 deletions numpy/core/tests/test_arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,13 @@ def test_legacy_mode_scalars(self):
'1.1234567891234568')
assert_equal(str(np.complex128(complex(1, np.nan))), '(1+nanj)')

def test_legacy_stray_comma(self):
np.set_printoptions(legacy='1.13')
assert_equal(str(np.arange(10000)), '[ 0 1 2 ..., 9997 9998 9999]')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a try-finally, maybe? Or does this have cleanup?


np.set_printoptions(legacy=False)
assert_equal(str(np.arange(10000)), '[ 0 1 2 ... 9997 9998 9999]')

def test_dtype_linewidth_wrapping(self):
np.set_printoptions(linewidth=75)
assert_equal(repr(np.arange(10,20., dtype='f4')),
Expand Down
4 changes: 2 additions & 2 deletions numpy/ma/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,8 @@ def test_str_repr_legacy(self):
a[1:50] = np.ma.masked
assert_equal(
repr(a),
'masked_array(data = [0 -- -- ... 1997 1998 1999],\n'
' mask = [False True True ... False False False],\n'
'masked_array(data = [0 -- -- ..., 1997 1998 1999],\n'
' mask = [False True True ..., False False False],\n'
' fill_value = 999999)\n'
)
finally:
Expand Down