Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions fsspec/implementations/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ def _open(
if mode in ["rb", "ab", "r+b", "a+b"]:
if path in self.store:
f = self.store[path]
if self._intrans and "a" in mode:
# append to a copy so the store is unchanged until commit
f = MemoryFile(self, path, f.getvalue())
f.created = self.store[path].created
if "a" in mode:
# position at the end of file
f.seek(0, 2)
Expand Down
20 changes: 20 additions & 0 deletions fsspec/implementations/tests/test_memory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import pickle
from contextlib import nullcontext
from datetime import datetime, timezone
from pathlib import PurePosixPath, PureWindowsPath

Expand Down Expand Up @@ -396,6 +397,25 @@ def test_append_creates_missing_file(m, mode):
assert m.cat(filename) == b"data"


@pytest.mark.parametrize("global_store", [False, True])
@pytest.mark.parametrize("mode", ["ab", "a+b"])
@pytest.mark.parametrize("rollback", [False, True])
def test_transaction_append_existing_file(m, global_store, mode, rollback):
fs = filesystem("memory", global_store=global_store, skip_instance_cache=True)
fs.pipe_file("file", b"original")

with pytest.raises(RuntimeError) if rollback else nullcontext():
with fs.transaction:
with fs.open("file", mode) as f:
f.write(b"-appended")
assert fs.cat_file("file") == b"original"
if rollback:
raise RuntimeError("discard transaction")

expected = b"original" if rollback else b"original-appended"
assert fs.cat_file("file") == expected


def test_moves(m):
m.touch("source.txt")
m.mv("source.txt", "target.txt")
Expand Down
Loading