Commit 25c928b6 authored by Christos Stavrakakis's avatar Christos Stavrakakis

Fix UnicodeError when writing to sys.stdout

Due to a bug the was fixed in Python-2.7, in Python-2.6 unicode strings
that are written to sys.stdout (using sys.stdout.write) are not
converted to bytestrings using the file.encoding. Instead
sys.stdout.encoding is set to None, and the default encoding is used. We
solve this by creating a wrapper to stdout, and encoding everything
either with the file encoding, or with the `locale` prefered encoding.
parent bc880ee2
......@@ -59,6 +59,7 @@ from synnefo.util.version import get_component_version
from synnefo.lib.dictconfig import dictConfig
import sys
import locale
import os
import imp
......@@ -310,12 +311,23 @@ def configure_logging():
log.warning("SNF_MANAGE_LOGGING_SETUP setting missing.")
class EncodedStdOut(object):
def __init__(self):
self.encoding = sys.__stdout__.encoding or locale.getpreferredencoding()
def write(self, string):
if isinstance(string, unicode):
string = string.encode(self.encoding)
def main():
# no need to run setup_environ
# we already know our project
sys.stdout = EncodedStdOut()
mu = SynnefoManagementUtility(sys.argv)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment