From 9365e5b18a98820d7bb5b7b79569ac7038094a9f Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Mon, 18 Sep 2017 20:14:17 +0200 Subject: [PATCH] [core] Set default encoding to utf-8 Have another go at all my fun utf-8 issues. This *should* fix #180 And hopefully still fixes #176 --- bumblebee-status | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/bumblebee-status b/bumblebee-status index 41e8636..e3fe4ca 100755 --- a/bumblebee-status +++ b/bumblebee-status @@ -10,27 +10,12 @@ import bumblebee.output import bumblebee.input import bumblebee.modules.error -# taken from -# https://stackoverflow.com/a/42146082 -# seems an excellent solution to me -class SmartStdout: - def __init__(self, encoding=None, org_stdout=None): - if org_stdout is None: - org_stdout = getattr(sys.stdout, 'org_stdout', sys.stdout) - self.org_stdout = org_stdout - self.encoding = encoding or getattr(org_stdout, 'encoding', None) or 'utf-8' +try: + reload(sys) + sys.setdefaultencoding('UTF8') +except Exception: + pass - def write(self, s): - self.org_stdout.write(s.encode(self.encoding, 'backslashreplace')) - - def __getattr__(self, name): - return getattr(self.org_stdout, name) - -def set_defaultencoding_globally(encoding='utf-8'): - assert sys.getdefaultencoding() in ('ascii', 'mbcs', encoding) - import imp - _sys_org = imp.load_dynamic('_sys_org', 'sys') - _sys_org.setdefaultencoding(encoding) def main(): config = bumblebee.config.Config(sys.argv[1:]) @@ -85,9 +70,6 @@ def main(): time.sleep(1) if __name__ == "__main__": - if sys.stdout.isatty(): - sys.stdout = sys.stderr = SmartStdout() - set_defaultencoding_globally('utf-8') main() # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4