Skip to content

Commit

Permalink
Add UploadFile.tell() parameter
Browse files Browse the repository at this point in the history
Also improve tests for seek(), testing with and without the whence
parameter.
  • Loading branch information
dvarrazzo committed Nov 1, 2020
1 parent 7098fed commit 3a62e71
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions starlette/datastructures.py
Expand Up @@ -455,6 +455,9 @@ async def seek(self, offset: int, whence: int = 0) -> None:
else:
await run_in_threadpool(self.file.seek, offset, whence)

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

async def close(self) -> None:
if self._in_memory:
self.file.close()
Expand Down
8 changes: 7 additions & 1 deletion 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")
await big_file.seek(0, 0)
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

0 comments on commit 3a62e71

Please sign in to comment.