[Pkg-mpd-commits] [python-mpd] 158/262: unicode: lower-level encoder/decoder to deal with non-ASCII characters
Simon McVittie
smcv at debian.org
Sun May 22 18:16:41 UTC 2016
This is an automated email from the git hooks/post-receive script.
smcv pushed a commit to branch upstream
in repository python-mpd.
commit 7ee6a7b0c418abf2628020314bdfcd4b96451c6b
Author: Jonathan Ballet <jon at multani.info>
Date: Wed Dec 12 17:41:16 2012 +0800
unicode: lower-level encoder/decoder to deal with non-ASCII characters
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?)
---
mpd.py | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/mpd.py b/mpd.py
index ecd004b..bd54cef 100644
--- a/mpd.py
+++ b/mpd.py
@@ -465,8 +465,16 @@ class MPDClient(object):
self._sock = self._connect_unix(host)
else:
self._sock = self._connect_tcp(host, port)
- self._rfile = self._sock.makefile("r")
- self._wfile = self._sock.makefile("w")
+
+ if IS_PYTHON2:
+ self._rfile = self._sock.makefile("r")
+ self._wfile = self._sock.makefile("w")
+ else:
+ # Force UTF-8 encoding, since this is dependant from the LC_CTYPE
+ # locale.
+ self._rfile = self._sock.makefile("r", encoding="utf-8")
+ self._wfile = self._sock.makefile("w", encoding="utf-8")
+
try:
self._hello()
except:
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mpd/python-mpd.git
More information about the Pkg-mpd-commits
mailing list