summaryrefslogtreecommitdiff
path: root/lib/python
diff options
context:
space:
mode:
authorNils Philippsen <nils@redhat.com>2011-10-26 17:31:36 +0200
committerNils Philippsen <nils@redhat.com>2011-10-27 12:13:59 +0200
commitb5a5011f31d6062cd00ee6b02ddf356d691e67e6 (patch)
treeecba035288b90ad14166f0c1b900674445b14c7e /lib/python
parent213bc568ed4afe101ac18b4847caa1e0f25886cf (diff)
python: implement and use utf8 stream writer for stdout, stderr
The C-side (glib) really wants stuff to be encoded in UTF-8.
Diffstat (limited to 'lib/python')
-rw-r--r--lib/python/packagekit/backend.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/python/packagekit/backend.py b/lib/python/packagekit/backend.py
index 1f0c9858a..859a98ae0 100644
--- a/lib/python/packagekit/backend.py
+++ b/lib/python/packagekit/backend.py
@@ -38,8 +38,49 @@ def _to_unicode(txt, encoding='utf-8'):
txt = unicode(txt, encoding, errors='replace')
return txt
+def _to_utf8(txt, errors='replace'):
+ '''convert practically anything to a utf-8-encoded byte string'''
+
+ # convert to unicode object
+ if isinstance(txt, str):
+ txt = txt.decode('utf-8', errors=errors)
+ if not isinstance(txt, basestring):
+ # try to convert non-string objects like exceptions
+ try:
+ # if txt.__unicode__() exists, or txt.__str__() returns ASCII
+ txt = unicode(txt)
+ except UnicodeDecodeError:
+ # if txt.__str__() exists
+ txt = str(txt).decode('utf-8', errors=errors)
+ except:
+ # no __str__(), __unicode__() methods, use representation
+ txt = unicode(repr(txt))
+
+ # return encoded as UTF-8
+ return txt.encode('utf-8', errors=errors)
+
# Classes
+class _UTF8Writer(codecs.StreamWriter):
+
+ encoding = 'utf-8'
+
+ def __init__(self, stream, errors='replace'):
+ codecs.StreamWriter.__init__(self, stream, errors)
+
+ def encode(self, inp, errors='strict'):
+ try:
+ l = len(inp)
+ except TypeError:
+ try:
+ l = len(unicode(inp))
+ except:
+ try:
+ l = len(str(inp))
+ except:
+ l = 1
+ return (_to_utf8(inp, errors=errors), l)
+
class PkError(Exception):
def __init__(self, code, details):
self.code = code
@@ -50,6 +91,10 @@ class PkError(Exception):
class PackageKitBaseBackend:
def __init__(self, cmds):
+ # Make sys.stdout/stderr cope with UTF-8
+ sys.stdout = _UTF8Writer(sys.stdout)
+ sys.stderr = _UTF8Writer(sys.stderr)
+
# Setup a custom exception handler
installExceptionHandler(self)
self.cmds = cmds