Skip to content

Commit 6943420

Browse files
authored
Use list form of fs.put() in LocalTempFile to avoid isdir() checks (#1920)
1 parent 7aa982a commit 6943420

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

fsspec/implementations/cached.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,9 @@ def discard(self):
984984
os.remove(self.fn)
985985

986986
def commit(self):
987-
self.fs.put(self.fn, self.path, **self.kwargs)
987+
# calling put() with list arguments avoids path expansion and additional operations
988+
# like isdir()
989+
self.fs.put([self.fn], [self.path], **self.kwargs)
988990
# we do not delete the local copy, it's still in the cache.
989991

990992
@property

fsspec/implementations/tests/test_cached.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,3 +1342,19 @@ def test_filecache_write(tmpdir, m):
13421342
def test_cache_protocol_is_preserved():
13431343
fs = fsspec.filesystem("filecache", target_protocol="file")
13441344
assert fs.protocol == "filecache"
1345+
1346+
1347+
@pytest.mark.parametrize("protocol", ["simplecache", "filecache"])
1348+
def test_local_temp_file_put_by_list2(protocol, mocker, tmp_path) -> None:
1349+
fs = fsspec.filesystem(protocol, target_protocol="memory")
1350+
1351+
spy_put = mocker.spy(fs.fs, "put")
1352+
spy_isdir = mocker.spy(fs.fs, "isdir")
1353+
1354+
with fs.open("memory://some/file.txt", mode="wb") as file:
1355+
file.write(b"hello")
1356+
1357+
# passed by list
1358+
spy_put.assert_called_once_with([file.name], ["/some/file.txt"])
1359+
# which avoids isdir() check
1360+
spy_isdir.assert_not_called()

fsspec/tests/test_caches.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,4 @@ def test_cache_kwargs(mocker):
289289
# We don't care about the first parameter, just retrieve its expected value.
290290
# It is a random location that cannot be predicted.
291291
# The important thing is the 'overwrite' kwarg
292-
fs.fs.put.assert_called_with(fs.fs.put.call_args[0][0], "/test", overwrite=True)
292+
fs.fs.put.assert_called_with(fs.fs.put.call_args[0][0], ["/test"], overwrite=True)

0 commit comments

Comments
 (0)