-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: Use anyio.SpooledTemporaryFile in UploadFile #2925
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
Open
11kkw
wants to merge
10
commits into
Kludex:main
Choose a base branch
from
11kkw:feature/async-spooled-tempfile
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−180
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1eb9655
fix: Use anyio.SpooledTemporaryFile in UploadFile for proper async ha…
11kkw 873e2af
Refactor multipart parsing to use AsyncExitStack for safe file cleanu…
11kkw c87a49a
Use AsyncExitStack for safe file cleanup on exception
11kkw 73b68cd
Use AsyncExitStack to cleanup temp files on any error
11kkw 5bbb404
Merge branch 'master' into feature/async-spooled-tempfile
11kkw 0756e42
Merge branch 'master' into feature/async-spooled-tempfile
Kludex 43df948
Merge branch 'main' into feature/async-spooled-tempfile
11kkw 72e2145
tests(formparsers): remove rollover thread test and clean up imports
11kkw d3b0439
types: add AsyncFileIO protocol with minimal async file methods
11kkw 979840b
tests: add coverage for AsyncFileIO protocol
11kkw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from typing import Protocol, runtime_checkable | ||
|
|
||
|
|
||
| @runtime_checkable # pragma: no cover | ||
| class AsyncFileIO(Protocol): | ||
| async def read(self, size: int = -1) -> bytes: ... | ||
| async def write(self, data: bytes) -> int: ... | ||
| async def seek(self, offset: int, whence: int = os.SEEK_SET) -> int: ... | ||
| async def aclose(self) -> None: ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
| from starlette._fileio import AsyncFileIO | ||
|
|
||
|
|
||
| class GoodAsyncFile: | ||
| async def read(self, size: int = -1) -> bytes: | ||
| return b"ok" | ||
|
|
||
| async def write(self, data: bytes) -> int: | ||
| return len(data) | ||
|
|
||
| async def seek(self, offset: int, whence: int = os.SEEK_SET) -> int: | ||
| return offset | ||
|
|
||
| async def aclose(self) -> None: | ||
| return None | ||
|
|
||
|
|
||
| class BadAsyncFile_MissingSeek: | ||
| async def read(self, size: int = -1) -> bytes: | ||
| return b"ok" | ||
|
|
||
| async def write(self, data: bytes) -> int: | ||
| return len(data) | ||
|
|
||
| async def aclose(self) -> None: | ||
| return None | ||
|
|
||
|
|
||
| def test_async_fileio_runtime_check_positive() -> None: | ||
| obj = GoodAsyncFile() | ||
| assert isinstance(obj, AsyncFileIO) | ||
| assert issubclass(GoodAsyncFile, AsyncFileIO) | ||
|
|
||
|
|
||
| def test_async_fileio_runtime_check_negative() -> None: | ||
| obj = BadAsyncFile_MissingSeek() | ||
| assert not isinstance(obj, AsyncFileIO) | ||
| assert not issubclass(BadAsyncFile_MissingSeek, AsyncFileIO) | ||
|
|
||
|
|
||
| def test_async_fileio_runtime_check_unrelated_type() -> None: | ||
| assert not isinstance(123, AsyncFileIO) | ||
|
|
||
|
|
||
| def test_async_fileio_runtime_check_typeerror() -> None: | ||
| with pytest.raises(TypeError): | ||
| issubclass(123, AsyncFileIO) # type: ignore[arg-type] | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_goodasyncfile_methods_execute() -> None: | ||
| f = GoodAsyncFile() | ||
| assert await f.read() == b"ok" | ||
| assert await f.write(b"abc") == 3 | ||
| assert await f.seek(5) == 5 | ||
| await f.aclose() | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_badasyncfile_methods_execute() -> None: | ||
| f = BadAsyncFile_MissingSeek() | ||
| assert await f.read() == b"ok" | ||
| assert await f.write(b"abc") == 3 | ||
| await f.aclose() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why did this change?
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 changed it to
BaseExceptionassuming it would help ensure cleanup even for cases likeCancelledErrororKeyboardInterrupt, beyond justMultiPartException.But perhaps I misunderstood — please let me know if I’m thinking about this the wrong way!
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.
CancelledError being the most likely one