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

Add Python 3 support #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add Python 3 support #10

wants to merge 2 commits into from

Conversation

jonyesno
Copy link

@jonyesno jonyesno commented Aug 4, 2020

This MR:

  • encodes/decodes strings to/from bytes when talking to the HAProxy socket
  • tweaks the tryexcept syntax

both of which make the plugin work under Python 3.

This addresses the following logged exceptions:

Unhandled python exception in importing module: SyntaxError: invalid syntax
Unhandled python exception in read callback: TypeError: a bytes-like object is required, not 'str'
Unhandled python exception in read callback: TypeError: must be str, not bytes

Disclaimer: my Python skills are very limited, but this fixed it for me.

Python 3 distinguishes between Unicode `str`ings and byte strings.
The socket is expecting bytes, and we've dealing with `str`ings
internally so we need to convert in each direction.

This fixes the following problems:

```
Unhandled python exception in read callback: TypeError: a bytes-like object is required, not 'str'
Unhandled python exception in read callback: TypeError: must be str, not bytes
```
result = ''
buf = ''
buf = s.recv(RECV_SIZE)
while buf:
result += buf
result += buf.decode()
Copy link

@graingert graingert Nov 30, 2021

Choose a reason for hiding this comment

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

you need to use an incremental decoder, or decode the whole value at once in return result.decode()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants