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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make pycanberra work with both Python 2 and 3 #2

Merged
merged 2 commits into from May 15, 2013
Merged

Make pycanberra work with both Python 2 and 3 #2

merged 2 commits into from May 15, 2013

Conversation

bochecha
Copy link
Contributor

@bochecha bochecha commented Dec 2, 2012

The first commit is just clean up, it removes something that already wasn't needed in Python 2. However, from unneeded in Python 2, it becomes problematic in Python 3, so it should be dropped.

The second commit makes pycanberra work on Python 3, using the six library to keep it compatible with Python 2.

The issue is on strings defined as "foo" which were byte strings in Python 2 but became unicode strings in Python 3.

The underlying C libcanberra doesn't accept unicode strings, so they all must be byte strings.

However, defining them with b"foo" would break them on Python 2, so I used six.b("foo") instead.

If the dependency on six is an issue, then we could just copy-paste the code for six.b in pycanberra. (but code duplication is bad, so I preferred using the module as a dependency in the first submission 馃槈)

On Python 2, it is imported automatically, so there really isn't any
need to import it, it's classes can be used directly:
    $ python2
    >>> e = Exception()
    >>> import exceptions
    >>> isinstance(e, exceptions.Exception)
    True

Also, it doesn't exist on Python 3, so removing it will make the port
easier.
@pbor
Copy link

pbor commented Dec 5, 2012

I think patch 2 and 3 should be simplified: canberra properties should be plain strings without doing the "six" conversion there. The "six" conversion should be done on the fly when calling the function for all the arguments

@bochecha
Copy link
Contributor Author

bochecha commented Dec 5, 2012

@pbor

Agreed, I just dropped the second patch then, as the third one already implemented what you asked.

We need to pass byte strings to libcanberra, which is default with
Python 2 when defining a string as "foo".

However, on Python 3, the same definition gives a unicode string, which
libcanberra can't handle.

To avoid forcing applications to change the way they called pycanberra
on Python 2, pycanberra now quickly ensures that all strings it handles
are encoded to bytes, if they weren't already.
@bochecha
Copy link
Contributor Author

bochecha commented Jan 2, 2013

Patch updated to drop the dependency on six, we really need very few of it.

totdb added a commit that referenced this pull request May 15, 2013
Make pycanberra work with both Python 2 and 3
@totdb totdb merged commit 88c53cd into totdb:master May 15, 2013
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.

None yet

3 participants