Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add whence parameter to UploadFile.seek() #1084

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions starlette/datastructures.py
Expand Up @@ -449,11 +449,14 @@ async def read(self, size: int = -1) -> typing.Union[bytes, str]:
return self.file.read(size)
return await run_in_threadpool(self.file.read, size)

async def seek(self, offset: int) -> None:
async def seek(self, offset: int, whence: int = 0) -> None:
if self._in_memory:
self.file.seek(offset)
self.file.seek(offset, whence)
else:
await run_in_threadpool(self.file.seek, offset)
await run_in_threadpool(self.file.seek, offset, whence)

def tell(self) -> int:
return self.file.tell()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this one run_in_threadpool if not in memory?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe technically this should just be reading the position of the file descriptor from memory, but there's nothing that guarantees that this doesn't block, and aiofiles itself marks the method as needing to be run in an executor: https://github.com/Tinche/aiofiles/blob/v0.6.0/aiofiles/threadpool/binary.py#L48


async def close(self) -> None:
if self._in_memory:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_datastructures.py
Expand Up @@ -220,9 +220,15 @@ class BigUploadFile(UploadFile):
@pytest.mark.asyncio
async def test_upload_file():
big_file = BigUploadFile("big-file")
await big_file.seek(0)
await big_file.write(b"big-data" * 512)
await big_file.write(b"big-data")
pos = big_file.tell()
assert pos > 512 * len(b"big-data")
await big_file.seek(-10, 1)
assert big_file.tell() == pos - 10
await big_file.seek(0)
assert big_file.tell() == 0
assert await big_file.read(1024) == b"big-data" * 128
await big_file.close()

Expand Down