Skip to content
Open
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
22 changes: 22 additions & 0 deletions koyeb/sandbox/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2537,6 +2537,7 @@ def __init__(
self.mode = mode
self.encoding = encoding
self._closed = False
self._written: Optional[Union[str, bytes]] = None

def read(self) -> Union[str, bytes]:
"""Read file content synchronously"""
Expand Down Expand Up @@ -2569,7 +2570,17 @@ def write(self, content: Union[str, bytes]) -> None:
except SandboxFileNotFoundError:
pass

accumulate = "w" in self.mode and "a" not in self.mode
if accumulate:
# write_file replaces the whole file, so accumulate across writes.
if self._written is not None:
if isinstance(self._written, bytes) != isinstance(content, bytes):
raise TypeError("Cannot mix bytes and str content in write mode")
content = self._written + content

self.filesystem.write_file(self.path, content, encoding=self.encoding)
if accumulate:
self._written = content

def close(self) -> None:
"""Close the file"""
Expand Down Expand Up @@ -2597,6 +2608,7 @@ def __init__(
self.mode = mode
self.encoding = encoding
self._closed = False
self._written: Optional[Union[str, bytes]] = None

async def read(self) -> Union[str, bytes]:
"""Read file content asynchronously"""
Expand Down Expand Up @@ -2631,7 +2643,17 @@ async def write(self, content: Union[str, bytes]) -> None:
except SandboxFileNotFoundError:
pass

accumulate = "w" in self.mode and "a" not in self.mode
if accumulate:
# write_file replaces the whole file, so accumulate across writes.
if self._written is not None:
if isinstance(self._written, bytes) != isinstance(content, bytes):
raise TypeError("Cannot mix bytes and str content in write mode")
content = self._written + content

await self.filesystem.write_file(self.path, content, encoding=self.encoding)
if accumulate:
self._written = content

def close(self) -> None:
"""Close the file"""
Expand Down
41 changes: 41 additions & 0 deletions koyeb/sandbox/test_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,25 @@ def test_read_write_modes(self):
handle.write("data")
self.assertEqual(client.calls[0][0], "write_file")

def test_write_mode_accumulates_multiple_writes(self):
client = _FakeSyncClient()
with _fs(client) as fs:
with fs.open("/b", "w") as handle:
handle.write("first\n")
handle.write("second\n")
self.assertEqual(client.calls[-1], ("write_file", "/b", "first\nsecond\n"))

def test_write_mode_retry_after_failed_write_does_not_duplicate(self):
client = _FakeSyncClient()
with _fs(client) as fs:
with fs.open("/b", "w") as handle:
client.replies["write_file"] = RuntimeError("boom")
with self.assertRaises(Exception):
handle.write("first")
del client.replies["write_file"]
handle.write("first")
self.assertEqual(client.calls[-1], ("write_file", "/b", "first"))

def test_wrong_mode_raises(self):
with _fs(_FakeSyncClient()) as fs:
with self.assertRaises(ValueError):
Expand All @@ -357,6 +376,28 @@ def test_closed_handle_raises(self):
handle.read()


class TestAsyncSandboxFileIO(unittest.IsolatedAsyncioTestCase):
async def test_write_mode_accumulates_multiple_writes(self):
client = _FakeAsyncClient()
with _async_fs(client) as fs:
async with fs.open("/b", "w") as handle:
await handle.write("first\n")
await handle.write("second\n")
self.assertEqual(client.calls[-1], ("write_file", "/b", "first\nsecond\n"))


async def test_write_mode_retry_after_failed_write_does_not_duplicate(self):
client = _FakeAsyncClient()
with _async_fs(client) as fs:
async with fs.open("/b", "w") as handle:
client.replies["write_file"] = RuntimeError("boom")
with self.assertRaises(Exception):
await handle.write("first")
del client.replies["write_file"]
await handle.write("first")
self.assertEqual(client.calls[-1], ("write_file", "/b", "first"))


class TestAsyncReplyInterpretation(unittest.TestCase):
"""Async twin classifies through the same taxonomy."""

Expand Down