Skip to content

Commit

Permalink
Merge pull request #1557 from braingram/fsspec_fileno
Browse files Browse the repository at this point in the history
test for valid fileno before creating RealFile
  • Loading branch information
braingram authored Jul 12, 2023
2 parents 6d9c4f3 + 2c1d0d1 commit 8ef92d3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The ASDF Standard is at v1.6.0
- Remove deprecated legacy extension API [#1464]
- Drop Python 3.8 support [#1556]
- Drop NumPy 1.20, 1.21 support [#1568]
- Fix issue opening files that don't support ``fileno`` [#1557]

2.15.0 (2023-03-28)
-------------------
Expand Down
10 changes: 10 additions & 0 deletions asdf/_tests/test_generic_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,11 @@ def test_fsspec(tmp_path):
r = gf.read(len(ref))
assert r == ref, (r, ref)

gf.seek(0)
arr = gf.read_into_array(len(ref))
for ai, i in zip(arr, ref):
assert ai == i


@pytest.mark.remote_data()
def test_fsspec_http(httpserver):
Expand All @@ -823,6 +828,11 @@ def test_fsspec_http(httpserver):
r = gf.read(len(ref))
assert r == ref, (r, ref)

gf.seek(0)
arr = gf.read_into_array(len(ref))
for ai, i in zip(arr, ref):
assert ai == i


@pytest.mark.skipif(
sys.platform.startswith("win"),
Expand Down
15 changes: 10 additions & 5 deletions asdf/generic_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,11 +1173,16 @@ def get_file(init, mode="r", uri=None, close=False):
if init.seekable():
init2 = init.raw if hasattr(init, "raw") else init

result = (
MemoryIO(init2, mode, uri=uri)
if hasattr(init2, "getvalue")
else RealFile(init2, mode, uri=uri, close=close)
)
if hasattr(init2, "getvalue"):
result = MemoryIO(init2, mode, uri=uri)
else:
# can we call 'fileno'? if not, we can't memmap so don't
# make a RealFile
try:
init2.fileno()
result = RealFile(init2, mode, uri=uri, close=close)
except io.UnsupportedOperation:
result = RandomAccessFile(init2, mode, uri=uri, close=close)

result._secondary_fd = init
return result
Expand Down

0 comments on commit 8ef92d3

Please sign in to comment.