Skip to content

Commit

Permalink
Allow size= in open() (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
martindurant committed Sep 22, 2023
1 parent 315030e commit 7e05807
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion s3fs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ def _open(
fill_cache=None,
cache_type=None,
autocommit=True,
size=None,
requester_pays=None,
cache_options=None,
**kwargs,
Expand Down Expand Up @@ -680,6 +681,7 @@ def _open(
autocommit=autocommit,
requester_pays=requester_pays,
cache_options=cache_options,
size=size,
)

async def _lsdir(
Expand Down Expand Up @@ -2056,6 +2058,7 @@ def __init__(
cache_type="readahead",
requester_pays=False,
cache_options=None,
size=None,
):
bucket, key, path_version_id = s3.split_path(path)
if not key:
Expand Down Expand Up @@ -2094,6 +2097,7 @@ def __init__(
autocommit=autocommit,
cache_type=cache_type,
cache_options=cache_options,
size=size,
)
self.s3 = self.fs # compatibility

Expand Down Expand Up @@ -2133,7 +2137,7 @@ def __init__(
# Reflect head
self.s3_additional_kwargs.update(head)

if "r" in mode and "ETag" in self.details:
if "r" in mode and size is None and "ETag" in self.details:
self.req_kw["IfMatch"] = self.details["ETag"]

def _call_s3(self, method, *kwarglist, **kwargs):
Expand Down
12 changes: 12 additions & 0 deletions s3fs/tests/test_s3fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ def test_simple(s3):
assert out == data


def test_with_size(s3):
data = b"a" * (10 * 2**20)

with s3.open(a, "wb") as f:
f.write(data)

with s3.open(a, "rb", size=100) as f:
assert f.size == 100
out = f.read()
assert len(out) == 100


@pytest.mark.parametrize("default_cache_type", ["none", "bytes", "mmap", "readahead"])
def test_default_cache_type(s3, default_cache_type):
data = b"a" * (10 * 2**20)
Expand Down

0 comments on commit 7e05807

Please sign in to comment.