Skip to content

Remove redundant branch in GridFS #2064

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 5 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions gridfs/asynchronous/grid_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,11 +1301,8 @@ async def write(self, data: Any) -> None:
raise ValueError("cannot write to a closed file")

try:
if isinstance(data, AsyncGridOut):
read = data.read
else:
# file-like
read = data.read
# file-like
read = data.read
except AttributeError:
# string
if not isinstance(data, (str, bytes)):
Expand All @@ -1317,7 +1314,7 @@ async def write(self, data: Any) -> None:
raise TypeError(
"must specify an encoding for file in order to write str"
) from None
read = io.BytesIO(data).read # type: ignore[assignment]
read = io.BytesIO(data).read

if inspect.iscoroutinefunction(read):
await self._write_async(read)
Expand All @@ -1331,15 +1328,15 @@ async def write(self, data: Any) -> None:
except BaseException:
await self.abort()
raise
self._buffer.write(to_write) # type: ignore
if len(to_write) < space: # type: ignore
self._buffer.write(to_write)
if len(to_write) < space:
return # EOF or incomplete
await self._flush_buffer()
to_write = read(self.chunk_size)
while to_write and len(to_write) == self.chunk_size: # type: ignore
while to_write and len(to_write) == self.chunk_size:
await self._flush_data(to_write)
to_write = read(self.chunk_size)
self._buffer.write(to_write) # type: ignore
self._buffer.write(to_write)

async def _write_async(self, read: Any) -> None:
if self._buffer.tell() > 0:
Expand Down
17 changes: 7 additions & 10 deletions gridfs/synchronous/grid_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -1291,11 +1291,8 @@ def write(self, data: Any) -> None:
raise ValueError("cannot write to a closed file")

try:
if isinstance(data, GridOut):
read = data.read
else:
# file-like
read = data.read
# file-like
read = data.read
except AttributeError:
# string
if not isinstance(data, (str, bytes)):
Expand All @@ -1307,7 +1304,7 @@ def write(self, data: Any) -> None:
raise TypeError(
"must specify an encoding for file in order to write str"
) from None
read = io.BytesIO(data).read # type: ignore[assignment]
read = io.BytesIO(data).read

if inspect.iscoroutinefunction(read):
self._write_async(read)
Expand All @@ -1321,15 +1318,15 @@ def write(self, data: Any) -> None:
except BaseException:
self.abort()
raise
self._buffer.write(to_write) # type: ignore
if len(to_write) < space: # type: ignore
self._buffer.write(to_write)
if len(to_write) < space:
return # EOF or incomplete
self._flush_buffer()
to_write = read(self.chunk_size)
while to_write and len(to_write) == self.chunk_size: # type: ignore
while to_write and len(to_write) == self.chunk_size:
self._flush_data(to_write)
to_write = read(self.chunk_size)
self._buffer.write(to_write) # type: ignore
self._buffer.write(to_write)

def _write_async(self, read: Any) -> None:
if self._buffer.tell() > 0:
Expand Down
Loading