Skip to content

Commit 3243442

Browse files
committed
Work around for astropy#6460
1 parent df9c5e6 commit 3243442

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎astropy/io/ascii/tests/test_read.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ def test_read_chunks_input_types():
12961296
fpath = 't/test5.dat'
12971297
t1 = ascii.read(fpath, header_start=1, data_start=3)
12981298

1299-
for fp in (fpath, open(fpath, 'rb'), open(fpath, 'rb').read()):
1299+
for fp in (fpath, open(fpath, 'r'), open(fpath, 'r').read()):
13001300
t_gen = ascii.read(fp, header_start=1, data_start=3,
13011301
fast_reader={'chunk_size': 400, 'chunk_generator': True})
13021302
ts = list(t_gen)

‎astropy/io/ascii/ui.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import copy
1616
import time
1717
import warnings
18+
import contextlib
1819

1920
from . import core
2021
from . import basic
@@ -661,13 +662,25 @@ def _read_in_chunks_generator(table, chunk_size, **kwargs):
661662

662663
header = ''
663664

665+
@contextlib.contextmanager
666+
def passthrough_fileobj(fileobj, encoding=None):
667+
"""Stub for get_readable_fileobj, which does not seem to work in Py3
668+
for input File-like object, see #6460"""
669+
yield fileobj
670+
664671
# Convert table-as-string to a File object. Finding a newline implies
665672
# that the string is not a filename.
666673
if (isinstance(table, six.string_types) and
667674
('\n' in table or '\r' in table)):
668675
table = six.moves.cStringIO(table)
676+
fileobj_context = passthrough_fileobj
677+
elif hasattr(table, 'read') and hasattr(table, 'seek'):
678+
fileobj_context = passthrough_fileobj
679+
else:
680+
# string filename or pathlib
681+
fileobj_context = get_readable_fileobj
669682

670-
with get_readable_fileobj(table, encoding=kwargs.get('encoding')) as fh:
683+
with fileobj_context(table, encoding=kwargs.get('encoding')) as fh:
671684
fh_index = 0
672685

673686
while True:

0 commit comments

Comments
 (0)