Mic92/python-mpd2 — pull request #15 by multani (2012-12-11 09:34:51+00:00)
Title: unicode: lower-level encoder/decoder to deal with non-ASCII characters

While trying to deal with multani/sonata#25, I got the problem described below.

Note that I'm not sure this is the correct way to deal with this and that I didn't write test (yet, since I can't get the current test running successfully on both Python 2 & 3 in a reliable way).

On python3, socket.socket().makefile() returns a file-like object which
converts bytes to string (which is cool), using the system encoding as
default encoding/decoding method (which is not so cool).
So, if our MPD library contains non-ASCII characters and your locale is C,
_rfile.readline() will fail since it will try to decode the bytes to Unicode
using an ASCII decoder.

This patch enforces the communication with MPD to UTF-8 on Python 3, and
try to deal with it if use_unicode is True with Python 2.

Note that wrapping the file-like sockets with codecs.EncodedFile() would be
better, but it gets stuck while reading lines (incompatibility when wrapping
sockets?)