Skip to content

Commit c865121

Browse files
authored
Allow python SDKs to write any instance of IOBase files (#640)
- [x] update `write` method in sync & async python SDKs to allow `io.BufferedReader` files - [x] update tests
2 parents 184f33c + 5a208eb commit c865121

5 files changed

Lines changed: 45 additions & 6 deletions

File tree

.changeset/clean-chicken-sort.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@e2b/python-sdk': patch
3+
---
4+
5+
Allow python SDKs to write any instance of IOBase files

packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import httpcore
22
import httpx
3-
from io import TextIOBase
3+
from io import IOBase
44
from packaging.version import Version
55
from typing import AsyncIterator, IO, List, Literal, Optional, overload, Union
66
from e2b.sandbox.filesystem.filesystem import WriteEntry
@@ -206,7 +206,7 @@ async def write(
206206
file_path, file_data = file['path'], file['data']
207207
if isinstance(file_data, str) or isinstance(file_data, bytes):
208208
httpx_files.append(('file', (file_path, file_data)))
209-
elif isinstance(file_data, TextIOBase):
209+
elif isinstance(file_data,IOBase):
210210
httpx_files.append(('file', (file_path, file_data.read())))
211211
else:
212212
raise ValueError(f"Unsupported data type for file {file_path}")

packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from io import TextIOBase
1+
from io import IOBase
22
from typing import IO, Iterator, List, Literal, Optional, overload, Union
33
from e2b.sandbox.filesystem.filesystem import WriteEntry
44

@@ -202,7 +202,7 @@ def write(
202202
file_path, file_data = file['path'], file['data']
203203
if isinstance(file_data, str) or isinstance(file_data, bytes):
204204
httpx_files.append(('file', (file_path, file_data)))
205-
elif isinstance(file_data, TextIOBase):
205+
elif isinstance(file_data, IOBase):
206206
httpx_files.append(('file', (file_path, file_data.read())))
207207
else:
208208
raise ValueError(f"Unsupported data type for file {file_path}")

packages/python-sdk/tests/async/sandbox_async/files/test_write.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import io
2+
13
from e2b import AsyncSandbox
24
from e2b.sandbox_async.filesystem.filesystem import EntryInfo
35

4-
async def test_write_file(async_sandbox: AsyncSandbox):
6+
async def test_write_text_file(async_sandbox: AsyncSandbox):
57
filename = "test_write.txt"
68
content = "This is a test file."
79

@@ -20,6 +22,21 @@ async def test_write_file(async_sandbox: AsyncSandbox):
2022
read_content = await async_sandbox.files.read(filename)
2123
assert read_content == content
2224

25+
async def test_write_binary_file(async_sandbox: AsyncSandbox):
26+
filename = "test_write.txt"
27+
text = "This is a test binary file."
28+
# equivalent to `open("path/to/local/file", "rb")`
29+
content = io.BytesIO(text.encode('utf-8'))
30+
31+
info = await async_sandbox.files.write(filename, content)
32+
assert info.path == f"/home/user/{filename}"
33+
34+
exists = await async_sandbox.files.exists(filename)
35+
assert exists
36+
37+
read_content = await async_sandbox.files.read(filename)
38+
assert read_content == text
39+
2340
async def test_write_multiple_files(async_sandbox: AsyncSandbox):
2441
# Attempt to write with empty files array
2542
empty_info = await async_sandbox.files.write([])

packages/python-sdk/tests/sync/sandbox_sync/files/test_write.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import io
2+
13
from e2b.sandbox.filesystem.filesystem import EntryInfo
24

3-
def test_write_file(sandbox):
5+
def test_write_text_file(sandbox):
46
filename = "test_write.txt"
57
content = "This is a test file."
68

@@ -19,6 +21,21 @@ def test_write_file(sandbox):
1921
read_content = sandbox.files.read(filename)
2022
assert read_content == content
2123

24+
def test_write_binary_file(sandbox):
25+
filename = "test_write.txt"
26+
text = "This is a test binary file."
27+
# equivalent to `open("path/to/local/file", "rb")`
28+
content = io.BytesIO(text.encode('utf-8'))
29+
30+
info = sandbox.files.write(filename, content)
31+
assert info.path == f"/home/user/{filename}"
32+
33+
exists = sandbox.files.exists(filename)
34+
assert exists
35+
36+
read_content = sandbox.files.read(filename)
37+
assert read_content == text
38+
2239
def test_write_multiple_files(sandbox):
2340
# Attempt to write with empty files array
2441
empty_info = sandbox.files.write([])

0 commit comments

Comments
 (0)