From d5b93c52be4edb685eeded5f04d07fb2262efab8 Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Tue, 17 Feb 2015 16:36:52 -0500 Subject: [PATCH] Moved stderr and stdin handlers to only be installed for cli application; allows Gramps libraries to be used in other REPLs --- gramps/gen/utils/grampslocale.py | 18 ------------------ gramps/grampsapp.py | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/gramps/gen/utils/grampslocale.py b/gramps/gen/utils/grampslocale.py index 83c094d1f..aac7f1b05 100644 --- a/gramps/gen/utils/grampslocale.py +++ b/gramps/gen/utils/grampslocale.py @@ -423,24 +423,6 @@ class GrampsLocale(object): self.encoding = (locale.getpreferredencoding() or sys.getdefaultencoding()) LOG.debug("Setting encoding to %s", self.encoding) -#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: - try: - _encoding = sys.stdout.encoding or sys.getdefaultencoding() - except: - _encoding = "UTF-8" - if sys.version_info[0] < 3: - sys.stdout = codecs.getwriter(_encoding)(sys.stdout, - 'backslashreplace') - sys.stderr = codecs.getwriter(_encoding)(sys.stderr, - 'backslashreplace') - else: - sys.stdout = codecs.getwriter(_encoding)(sys.stdout.detach(), - 'backslashreplace') - sys.stderr = codecs.getwriter(_encoding)(sys.stderr.detach(), - 'backslashreplace') - # Make sure that self.lang and self.language are reflected # back into the environment for Gtk to use when its diff --git a/gramps/grampsapp.py b/gramps/grampsapp.py index cc5cf9502..40134b1b0 100644 --- a/gramps/grampsapp.py +++ b/gramps/grampsapp.py @@ -434,6 +434,25 @@ def run(): startgtkloop(error, argpars) else: #CLI use of GRAMPS + + #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: + try: + _encoding = sys.stdout.encoding or sys.getdefaultencoding() + except: + _encoding = "UTF-8" + if sys.version_info[0] < 3: + sys.stdout = codecs.getwriter(_encoding)(sys.stdout, + 'backslashreplace') + sys.stderr = codecs.getwriter(_encoding)(sys.stderr, + 'backslashreplace') + else: + sys.stdout = codecs.getwriter(_encoding)(sys.stdout.detach(), + 'backslashreplace') + sys.stderr = codecs.getwriter(_encoding)(sys.stderr.detach(), + 'backslashreplace') argpars.print_help() argpars.print_usage() from .cli.grampscli import startcli