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 all 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
12 changes: 9 additions & 3 deletions starlette/datastructures.py
Expand Up @@ -455,11 +455,17 @@ async def read(self, size: int = -1) -> bytes:
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)

async def tell(self) -> int:
if self._in_memory:
return self.file.tell()
else:
return await run_in_threadpool(self.file.tell)

async def close(self) -> None:
if self._in_memory:
Expand Down
14 changes: 13 additions & 1 deletion tests/test_datastructures.py
@@ -1,4 +1,5 @@
import io
import os

Choose a reason for hiding this comment

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

Is there any reason to use os' constants instead of io's?


import pytest

Expand Down Expand Up @@ -220,9 +221,20 @@ class BigUploadFile(UploadFile):
@pytest.mark.anyio
async def test_upload_file():
big_file = BigUploadFile("big-file")
await big_file.write(b"big-data" * 512)
await big_file.write(b"big-data")
assert (await big_file.tell()) == 8
await big_file.seek(0)
assert (await big_file.tell()) == 0
await big_file.seek(0, os.SEEK_END)
assert (await big_file.tell()) == 8
await big_file.write(b"big-data" * 511)
await big_file.write(b"big-data")
pos = await big_file.tell()
assert pos > 512 * len(b"big-data")
await big_file.seek(-10, os.SEEK_CUR)
assert (await big_file.tell()) == pos - 10
await big_file.seek(0)
assert (await big_file.tell()) == 0
assert await big_file.read(1024) == b"big-data" * 128
await big_file.close()

Expand Down