From ae0966a384eec507591dff4c62341c98b59bb108 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 2 May 2014 11:15:38 +0200 Subject: [PATCH] Fix encoding issues on windows. locale.getpreferredencoding(False) returns cp1252 on Windows, which is unable to parse our nice unicode arrows. Therefore, we always need to specify the encoding when using open(). Also we exclude configdata.py from pep257 on Windows because it doesn't detect the encoding correctly... Bug is submitted: https://github.com/GreenSteam/pep257/issues/70 --- qutebrowser/browser/curcommand.py | 4 ++-- qutebrowser/config/_iniparsers.py | 4 ++-- qutebrowser/config/_lineparser.py | 4 ++-- qutebrowser/config/config.py | 2 +- scripts/run_checks.py | 7 ++++++- setup.py | 2 +- 6 files changed, 14 insertions(+), 9 deletions(-) diff --git a/qutebrowser/browser/curcommand.py b/qutebrowser/browser/curcommand.py index 0e2193e68..f68885bc4 100644 --- a/qutebrowser/browser/curcommand.py +++ b/qutebrowser/browser/curcommand.py @@ -394,7 +394,7 @@ class CurCommandDispatcher(QObject): oshandle, filename = mkstemp(text=True) text = elem.evaluateJavaScript('this.value') if text: - with open(filename, 'w') as f: + with open(filename, 'w', encoding='utf-8') as f: f.write(text) proc = QProcess(self) proc.finished.connect(partial(self.on_editor_closed, elem, oshandle, @@ -431,7 +431,7 @@ class CurCommandDispatcher(QObject): if elem.isNull(): message.error("Element vanished while editing!") return - with open(filename, 'r') as f: + with open(filename, 'r', encoding='utf-8') as f: text = ''.join(f.readlines()) text = webelem.javascript_escape(text) logging.debug("Read back: {}".format(text)) diff --git a/qutebrowser/config/_iniparsers.py b/qutebrowser/config/_iniparsers.py index e8ddd7385..5ec99f04f 100644 --- a/qutebrowser/config/_iniparsers.py +++ b/qutebrowser/config/_iniparsers.py @@ -46,7 +46,7 @@ class ReadConfigParser(ConfigParser): if not os.path.isfile(self._configfile): return logging.debug("Reading config from {}".format(self._configfile)) - self.read(self._configfile) + self.read(self._configfile, encoding='utf-8') class ReadWriteConfigParser(ReadConfigParser): @@ -58,5 +58,5 @@ class ReadWriteConfigParser(ReadConfigParser): if not os.path.exists(self._configdir): os.makedirs(self._configdir, 0o755) logging.debug("Saving config to {}".format(self._configfile)) - with open(self._configfile, 'w') as f: + with open(self._configfile, 'w', encoding='utf-8') as f: self.write(f) diff --git a/qutebrowser/config/_lineparser.py b/qutebrowser/config/_lineparser.py index 7f5f10a98..3675d171c 100644 --- a/qutebrowser/config/_lineparser.py +++ b/qutebrowser/config/_lineparser.py @@ -53,7 +53,7 @@ class LineConfigParser: def read(self, filename): """Read the data from a file.""" - with open(filename, 'r') as f: + with open(filename, 'r', encoding='utf-8') as f: self.data = [line.rstrip('\n') for line in f.readlines()] def write(self, fp, limit=-1): @@ -82,7 +82,7 @@ class LineConfigParser: if not os.path.exists(self._configdir): os.makedirs(self._configdir, 0o755) logging.debug("Saving config to {}".format(self._configfile)) - with open(self._configfile, 'w') as f: + with open(self._configfile, 'w', encoding='utf-8') as f: self.write(f, limit) @pyqtSlot(str, str) diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index 33c500a02..c48d47430 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -374,7 +374,7 @@ class ConfigManager(QObject): if not os.path.exists(self._configdir): os.makedirs(self._configdir, 0o755) logging.debug("Saving config to {}".format(self._configfile)) - with open(self._configfile, 'w') as f: + with open(self._configfile, 'w', encoding='utf-8') as f: f.write(str(self)) def dump_userconfig(self): diff --git a/scripts/run_checks.py b/scripts/run_checks.py index 51887e2cc..f969b9962 100755 --- a/scripts/run_checks.py +++ b/scripts/run_checks.py @@ -52,6 +52,7 @@ options = { ], }, 'exclude': ['appdirs.py'], + 'exclude_pep257': [], 'other': { 'pylint': ['--output-format=colorized', '--reports=no', '--rcfile=.pylintrc'], @@ -59,6 +60,10 @@ options = { }, } +if os.name == 'nt': + # pep257 uses cp1252 by default on Windows, which can't handle the unicode + # arrows in configdata.py + options['exclude_pep257'].append('configdata.py') def run(name, args=None): """Run a checker via distutils with optional args. @@ -180,7 +185,7 @@ def _get_args(checker): pass try: args += ['--match=(?!{}).*\.py'.format('|'.join( - options['exclude']))] + options['exclude'] + options['exclude_pep257']))] except KeyError: pass try: diff --git a/setup.py b/setup.py index 9e29332da..0c4b81818 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ from setuptools import setup, find_packages import qutebrowser def read_file(name): - with open(name) as f: + with open(name, encoding='utf-8') as f: return f.read() setup(