Skip to content
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

Use disable_decoding in async read_response with hiredis parser. #3042

Merged
merged 1 commit into from
Dec 3, 2023
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
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* Fix async `read_response` to use `disable_decoding`.
* Add 'aclose()' methods to async classes, deprecate async close().
* Fix #2831, add auto_close_connection_pool=True arg to asyncio.Redis.from_url()
* Fix incorrect redis.asyncio.Cluster type hint for `retry_on_error`
Expand Down
10 changes: 8 additions & 2 deletions redis/_parsers/hiredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,16 @@ async def read_response(
if not self._connected:
raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR) from None

response = self._reader.gets()
if disable_decoding:
response = self._reader.gets(False)
else:
response = self._reader.gets()
Comment on lines -201 to +204
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not response=self._reader.gets(not disable_decoding)
or

enable_decoding = not disable decoding
response=self._reader.gets(enable_decoding)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a while so I don't exactly remember, especially that there wasn't any sophisticated reason.
I think I wanted to make it consistent with the sync version + I had a problem to get to the gets definition, and didn't want to take much more time digging deeper.

Copy link
Contributor

@kristjanvalur kristjanvalur Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, well, I had a look at the source for gets https://github.com/redis/hiredis-py/blob/861d51001b35d2b2162bd1152ce997824db674e8/src/reader.c#L367

 self->shouldDecode = 1;
    if (!PyArg_ParseTuple(args, "|i", &self->shouldDecode)) {
        return NULL;
    }

Basically, a shouldDecode argument can just be a Python bool.
so a should_decode = not disable_decoding would have been simpler and more readable.

Anyway, just came across this, don't worry.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing that out, and linking the code! 😄

while response is False:
await self.read_from_socket()
response = self._reader.gets()
if disable_decoding:
response = self._reader.gets(False)
else:
response = self._reader.gets()

# if the response is a ConnectionError or the response is a list and
# the first item is a ConnectionError, raise it as something bad
Expand Down
Loading