-
-
Notifications
You must be signed in to change notification settings - Fork 959
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
Conversation
Also improve tests for seek(), testing with and without the whence parameter.
7a7d2e3
to
3a62e71
Compare
starlette/datastructures.py
Outdated
await run_in_threadpool(self.file.seek, offset, whence) | ||
|
||
def tell(self) -> int: | ||
return self.file.tell() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
Adding the Given the questions about |
@JayH5 You are right that, 5 minutes later, I stumbled in the problem of async def is_zipfile(cls, f: UploadFile):
"""A poor man's async version of zipfile.is_zipfile()."""
head = await f.read(4)
await f.seek(0)
return head == b"PK\x03\x04" do you prefer to just close this MR or would you prefer me to make |
I've made |
IMO, there's nothing wrong with this, but the change needs a use case to justify its addition. If there isn't a clear one I think we can close this. |
I can see a clear use case for this. Implementing it allows for a method to get the uploaded file size. Based on django's approach (https://github.com/django/django/blob/master/django/core/files/base.py#L32) I'm doing the following:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it would be nice to update the seek
description, under UploadFile, at docs/requests.md
, as well.
@@ -1,4 +1,5 @@ | |||
import io | |||
import os |
There was a problem hiding this comment.
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?
Let's close this off. The original motivation was to use it with We've also got an alternate ticket to consider making the |
The second parameter of the
seek()
method is missing. See https://docs.python.org/3/library/io.html#io.IOBase.seekThis makes UploadFile not compatible with e.g.
zipfile.is_zipfile()
: