Skip to content

Commit 96b199c

Browse files
committed
Work around for astropy#6460
1 parent fc2316b commit 96b199c

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
@@ -1309,7 +1309,7 @@ def test_read_chunks_input_types():
13091309
fpath = 't/test5.dat'
13101310
t1 = ascii.read(fpath, header_start=1, data_start=3)
13111311

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

‎astropy/io/ascii/ui.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import copy
1717
import time
1818
import warnings
19+
import contextlib
1920

2021
from . import core
2122
from . import basic
@@ -663,13 +664,25 @@ def _read_in_chunks_generator(table, chunk_size, **kwargs):
663664

664665
header = ''
665666

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

672-
with get_readable_fileobj(table, encoding=kwargs.get('encoding')) as fh:
685+
with fileobj_context(table, encoding=kwargs.get('encoding')) as fh:
673686
fh_index = 0
674687

675688
while True:

0 commit comments

Comments
 (0)