diff --git a/starlette/datastructures.py b/starlette/datastructures.py index 8dd5d8489..37457b52a 100644 --- a/starlette/datastructures.py +++ b/starlette/datastructures.py @@ -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() diff --git a/tests/test_datastructures.py b/tests/test_datastructures.py index bd356c236..23ce4edef 100644 --- a/tests/test_datastructures.py +++ b/tests/test_datastructures.py @@ -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()