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

Python 2 and 3, str and bytes compatibility #38

Open
slezica opened this issue Mar 11, 2016 · 4 comments
Open

Python 2 and 3, str and bytes compatibility #38

slezica opened this issue Mar 11, 2016 · 4 comments

Comments

@slezica
Copy link

slezica commented Mar 11, 2016

To make the library run under both Python versions with the same code, we need to address the str vs bytes problem.

In Python 2, bytes is an alias for str. In Python 3, bytes is a separate class. These two, despite having the same name, behave completely different: they take different arguments to construct, they return different types when indexed and str and repr don't do the same.

Possible solutions:

  • Use the bytes class in future.builtins, from pip install future
  • Use the six bridging library (undesireable, it cannot be exposed to the API user)
@federicobond
Copy link
Collaborator

When porting to Python 3 I used bytearray to get numbers when indexing bytes. It's not super clean, but it has the advantage of not cluttering the package with unnecessary dependencies.

@slezica
Copy link
Author

slezica commented Mar 13, 2016

We could create a blob type (avoiding bytes altogether), either by extending bytearray or imitating the python-future bytes implementation.

What do you think?

@federicobond
Copy link
Collaborator

Could our Buffer class fill this role? I haven't looked into it much, but it makes sense to use a single type of object for handling binary data across the codebase.

@slezica
Copy link
Author

slezica commented Mar 14, 2016

Buffer is mutable, itself a thin wrapper around bytearray

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

No branches or pull requests

2 participants