-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-4669 - Update Async GridFS APIs for Motor Compatibility #1821
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
00f569b
PYTHON-4669 - Update More APIs for Motor Compatibility
NoahStapp 54d333a
Motor compat changes
NoahStapp 397ea6b
Cleanup
NoahStapp c36d5d5
AsyncGridOut fixes
NoahStapp 33867e5
Merge branch 'master' into PYTHON-4669
NoahStapp 66e104c
WIP
NoahStapp c313064
Add async test_grid_file
NoahStapp 911c487
Fix pre-3.9 async imports
NoahStapp f9930b1
Whoops
NoahStapp a9cf616
Fix helpers
NoahStapp d1e0ed0
Fixes
NoahStapp 4847621
Address review
NoahStapp f0293d5
Address review
NoahStapp 4498f65
Fixes
NoahStapp 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 |
---|---|---|
|
@@ -1194,19 +1194,9 @@ def __setattr__(self, name: str, value: Any) -> None: | |
) | ||
|
||
async def set(self, name: str, value: Any) -> None: | ||
# For properties of this instance like _buffer, or descriptors set on | ||
# the class like filename, use regular __setattr__ | ||
if name in self.__dict__ or name in self.__class__.__dict__: | ||
object.__setattr__(self, name, value) | ||
else: | ||
# All other attributes are part of the document in db.fs.files. | ||
# Store them to be sent to server on close() or if closed, send | ||
# them now. | ||
self._file[name] = value | ||
if self._closed: | ||
await self._coll.files.update_one( | ||
{"_id": self._file["_id"]}, {"$set": {name: value}} | ||
) | ||
self._file[name] = value | ||
if self._closed: | ||
await self._coll.files.update_one({"_id": self._file["_id"]}, {"$set": {name: value}}) | ||
|
||
async def _flush_data(self, data: Any, force: bool = False) -> None: | ||
"""Flush `data` to a chunk.""" | ||
|
@@ -1400,7 +1390,11 @@ async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> Any: | |
return False | ||
|
||
|
||
class AsyncGridOut(io.IOBase): | ||
GRIDOUT_BASE_CLASS = io.IOBase if _IS_SYNC else object # type: Any | ||
|
||
|
||
class AsyncGridOut(GRIDOUT_BASE_CLASS): # type: ignore | ||
|
||
"""Class to read data out of GridFS.""" | ||
|
||
def __init__( | ||
|
@@ -1460,6 +1454,8 @@ def __init__( | |
self._position = 0 | ||
self._file = file_document | ||
self._session = session | ||
if not _IS_SYNC: | ||
self.closed = False | ||
|
||
_id: Any = _a_grid_out_property("_id", "The ``'_id'`` value for this file.") | ||
filename: str = _a_grid_out_property("filename", "Name of this file.") | ||
|
@@ -1486,16 +1482,17 @@ def __init__( | |
_file: Any | ||
_chunk_iter: Any | ||
|
||
async def __anext__(self) -> bytes: | ||
return super().__next__() | ||
if not _IS_SYNC: | ||
closed: bool | ||
|
||
def __next__(self) -> bytes: # noqa: F811, RUF100 | ||
if _IS_SYNC: | ||
return super().__next__() | ||
else: | ||
raise TypeError( | ||
"AsyncGridOut does not support synchronous iteration. Use `async for` instead" | ||
) | ||
async def __anext__(self) -> bytes: | ||
line = await self.readline() | ||
if line: | ||
return line | ||
raise StopAsyncIteration() | ||
|
||
async def to_list(self) -> list[bytes]: | ||
return [x async for x in self] # noqa: C416, RUF100 | ||
|
||
async def open(self) -> None: | ||
if not self._file: | ||
|
@@ -1616,18 +1613,37 @@ async def read(self, size: int = -1) -> bytes: | |
""" | ||
return await self._read_size_or_line(size=size) | ||
|
||
async def readline(self, size: int = -1) -> bytes: # type: ignore[override] | ||
async def readline(self, size: int = -1) -> bytes: | ||
"""Read one line or up to `size` bytes from the file. | ||
|
||
:param size: the maximum number of bytes to read | ||
""" | ||
return await self._read_size_or_line(size=size, line=True) | ||
|
||
async def readlines(self, size: int = -1) -> list[bytes]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be async only too since we intentionally get it for free from IOBase in the sync version. |
||
"""Read one line or up to `size` bytes from the file. | ||
|
||
:param size: the maximum number of bytes to read | ||
""" | ||
await self.open() | ||
lines = [] | ||
remainder = int(self.length) - self._position | ||
bytes_read = 0 | ||
while remainder > 0: | ||
line = await self._read_size_or_line(line=True) | ||
bytes_read += len(line) | ||
lines.append(line) | ||
remainder = int(self.length) - self._position | ||
if 0 < size < bytes_read: | ||
break | ||
|
||
return lines | ||
|
||
def tell(self) -> int: | ||
"""Return the current position of this file.""" | ||
return self._position | ||
|
||
async def seek(self, pos: int, whence: int = _SEEK_SET) -> int: # type: ignore[override] | ||
async def seek(self, pos: int, whence: int = _SEEK_SET) -> int: | ||
"""Set the current position of this file. | ||
|
||
:param pos: the position (or offset if using relative | ||
|
@@ -1690,12 +1706,15 @@ def __aiter__(self) -> AsyncGridOut: | |
""" | ||
return self | ||
|
||
async def close(self) -> None: # type: ignore[override] | ||
async def close(self) -> None: | ||
"""Make GridOut more generically file-like.""" | ||
if self._chunk_iter: | ||
await self._chunk_iter.close() | ||
self._chunk_iter = None | ||
super().close() | ||
if _IS_SYNC: | ||
super().close() | ||
else: | ||
self.closed = True | ||
|
||
def write(self, value: Any) -> NoReturn: | ||
raise io.UnsupportedOperation("write") | ||
|
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
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
Oops, something went wrong.
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.
Rather than having
__setattr__
always raise an error in async, we should allow it as long as the file is not closed and only raise an error if the file is closed: