GrampsLocale: Separate self.encoding from sys.stdout.encoding

Because python standard library functions encode to
locale.getpreferredencoding or sys.getdefaultencoding, not to
sys.stdout.encoding.

svn: r21753
This commit is contained in:
John Ralls 2013-03-26 19:16:49 +00:00
parent e142003601
commit f432853dc4

View File

@ -211,18 +211,24 @@ class GrampsLocale(object):
locale.setlocale(locale.LC_MONETARY, self.currency)
except locale.Error:
pass
#Next, we need to know what is the encoding from the native environment:
self.encoding = sys.stdout.encoding or sys.getdefaultencoding()
#Next, we need to know what is the encoding from the native
#environment. This is used by python standard library funcions which
#localize their output, e.g. time.strftime():
self.encoding = locale.getpreferredencoding() or sys.getdefaultencoding()
#Ensure that output is encoded correctly to stdout and stderr. This is
#much less cumbersome and error-prone than encoding individual outputs
#and better handles the differences between Python 2 and Python 3:
_encoding = sys.stdout.encoding or sys.getdefaultencoding()
if sys.version_info[0] < 3:
sys.stdout = codecs.getwriter(self.encoding)(sys.stdout, 'backslashreplace')
sys.stderr = codecs.getwriter(self.encoding)(sys.stderr, 'backslashreplace')
sys.stdout = codecs.getwriter(_encoding)(sys.stdout,
'backslashreplace')
sys.stderr = codecs.getwriter(_encoding)(sys.stderr,
'backslashreplace')
else:
sys.stdout = codecs.getwriter(self.encoding)(sys.stdout.detach(), 'backslashreplace')
sys.stderr = codecs.getwriter(self.encoding)(sys.stderr.detach(), 'backslashreplace')
sys.stdout = codecs.getwriter(_encoding)(sys.stdout.detach(),
'backslashreplace')
sys.stderr = codecs.getwriter(_encoding)(sys.stderr.detach(),
'backslashreplace')
#GtkBuilder depends on reading Glade files as UTF-8 and crashes if it