Skip to content
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

Fixed a bug that caused newlines on Windows to be \r\r\n in ASCII tables #8659

Merged
merged 2 commits into from May 3, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -360,6 +360,9 @@ astropy.io.ascii
method. This can make writing ECSV significantly faster if the data do not
actually have any masked values. [#8447]

- Fixed a bug that caused newlines to be incorrect when writing out ASCII tables
on Windows (they were ``\r\r\n`` instead of ``\r\n``). [#8659]

astropy.io.misc
^^^^^^^^^^^^^^^

Expand Down
5 changes: 4 additions & 1 deletion astropy/io/ascii/cparser.pyx
Expand Up @@ -1020,7 +1020,10 @@ cdef class FastWriter:
opened_file = False

if not hasattr(output, 'write'): # output is a filename
output = open(output, 'w')
# NOTE: we need to specify newline='', otherwise the default
# behavior is for Python to translate \r\n (which we write because
# of os.linesep) into \r\r\n. Specifying newline='' disables any
output = open(output, 'w', newline='')
opened_file = True # remember to close file afterwards
writer = core.CsvWriter(output,
delimiter=self.delimiter,
Expand Down
18 changes: 18 additions & 0 deletions astropy/io/ascii/tests/test_write.py
Expand Up @@ -750,3 +750,21 @@ def test_roundtrip_masked(fmt_name_class):
for col, col2 in zip(t.itercols(), t2.itercols()):
assert col.dtype.kind == col2.dtype.kind
assert np.all(col == col2)


@pytest.mark.parametrize("fast_writer", [True, False])
def test_write_newlines(fast_writer, tmpdir):

# Regression test for https://github.com/astropy/astropy/issues/5126
# On windows, when writing to a filename (not e.g. StringIO), newlines were
# \r\r\n instead of \r\n.

filename = tmpdir.join('test').strpath

t = table.Table([['a', 'b', 'c']], names=['col'])
ascii.write(t, filename, fast_writer=fast_writer)

with open(filename, 'r', newline='') as f:
content = f.read()

assert content == os.linesep.join(['col', 'a', 'b', 'c']) + os.linesep
6 changes: 5 additions & 1 deletion astropy/io/ascii/ui.py
Expand Up @@ -798,7 +798,11 @@ def write(table, output=None, format=None, Writer=None, fast_writer=True, *,
# Write the lines to output
outstr = os.linesep.join(lines)
if not hasattr(output, 'write'):
output = open(output, 'w')
# NOTE: we need to specify newline='', otherwise the default
# behavior is for Python to translate \r\n (which we write because
# of os.linesep) into \r\r\n. Specifying newline='' disables any
# auto-translation.
output = open(output, 'w', newline='')
output.write(outstr)
output.write(os.linesep)
output.close()
Expand Down