From 17dd4732ae4807d73ce6f780fbafb5c46cf0999d Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 20 Aug 2014 20:33:14 +0200 Subject: [PATCH] Add explicit encoding to open() calls. --- qutebrowser/app.py | 4 ++-- qutebrowser/commands/userscripts.py | 5 +++-- qutebrowser/utils/misc.py | 5 +++-- qutebrowser/utils/version.py | 2 +- scripts/setupcommon.py | 5 +++-- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/qutebrowser/app.py b/qutebrowser/app.py index f68660227..d9f880d8f 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -276,7 +276,7 @@ class Application(QApplication): 'crash.log') # First check if an old logfile exists. if os.path.exists(logname): - with open(logname, 'r') as f: + with open(logname, 'r', encoding='ascii') as f: data = f.read() if data: # Crashlog exists and has data in it, so something crashed @@ -309,7 +309,7 @@ class Application(QApplication): """Start a new logfile and redirect faulthandler to it.""" logname = os.path.join(get_standard_dir(QStandardPaths.DataLocation), 'crash.log') - self._crashlogfile = open(logname, 'w') + self._crashlogfile = open(logname, 'w', encoding='ascii') faulthandler.enable(self._crashlogfile) if (hasattr(faulthandler, 'register') and hasattr(signal, 'SIGUSR1')): diff --git a/qutebrowser/commands/userscripts.py b/qutebrowser/commands/userscripts.py index b4bea0e77..193bdf498 100644 --- a/qutebrowser/commands/userscripts.py +++ b/qutebrowser/commands/userscripts.py @@ -78,7 +78,8 @@ class _BlockingFIFOReader(QObject): # We also use os.open and os.fdopen rather than built-in open so we can # add O_NONBLOCK. fd = os.open(self.filepath, os.O_RDWR | - os.O_NONBLOCK) # pylint: disable=no-member + os.O_NONBLOCK, + encoding='utf-8') # pylint: disable=no-member self.fifo = os.fdopen(fd, 'r') while True: logger.debug("thread loop") @@ -279,7 +280,7 @@ class _WindowsUserscriptRunner(_BaseUserscriptRunner): got_cmd: Emitted for every command in the file. """ logger.debug("proc finished") - with open(self.filepath, 'r') as f: + with open(self.filepath, 'r', encoding='utf-8') as f: for line in f: self.got_cmd.emit(line.rstrip()) self._cleanup() diff --git a/qutebrowser/utils/misc.py b/qutebrowser/utils/misc.py index b15049e0f..f9b71ca85 100644 --- a/qutebrowser/utils/misc.py +++ b/qutebrowser/utils/misc.py @@ -78,7 +78,7 @@ def read_file(filename): if hasattr(sys, 'frozen'): # cx_Freeze doesn't support pkg_resources :( fn = os.path.join(os.path.dirname(sys.executable), filename) - with open(fn, 'r', encoding='UTF-8') as f: + with open(fn, 'r', encoding='utf-8') as f: return f.read() else: return resource_string(qutebrowser.__name__, filename).decode('UTF-8') @@ -193,7 +193,8 @@ def actute_warning(): return except ValueError: pass - with open('/usr/share/X11/locale/en_US.UTF-8/Compose', 'r') as f: + with open('/usr/share/X11/locale/en_US.UTF-8/Compose', 'r', + encoding='utf-8') as f: for line in f: if '' in line: if sys.stdout is not None: diff --git a/qutebrowser/utils/version.py b/qutebrowser/utils/version.py index 05f399294..bfaa98b4c 100644 --- a/qutebrowser/utils/version.py +++ b/qutebrowser/utils/version.py @@ -128,7 +128,7 @@ def _release_info(): data = [] for fn in glob.glob("/etc/*-release"): try: - with open(fn, 'r') as f: + with open(fn, 'r', encoding='utf-8') as f: data.append((fn, ''.join(f.readlines()))) except IOError as e: logger.warning("Error while reading {}: {}: {}".format( diff --git a/scripts/setupcommon.py b/scripts/setupcommon.py index 047b1816c..65179dc15 100644 --- a/scripts/setupcommon.py +++ b/scripts/setupcommon.py @@ -35,7 +35,7 @@ BASEDIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), def read_file(name): """Get the string contained in the file named name.""" - with open(name, encoding='utf-8') as f: + with open(name, 'r', encoding='utf-8') as f: return f.read() @@ -87,7 +87,8 @@ def write_git_file(): gitstr = _git_str() if gitstr is None: gitstr = '' - with open(os.path.join(BASEDIR, 'qutebrowser', 'git-commit-id'), 'w') as f: + path = os.path.join(BASEDIR, 'qutebrowser', 'git-commit-id') + with open(path, 'w', encoding='utf-8') as f: f.write(gitstr)