From ffd6ffef45d04ee0cea99365ce5a5f908451cab2 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Tue, 26 Jun 2018 15:38:01 +1200 Subject: [PATCH] Add force_encoding call to sanitize_filename. So that callers don't have to call both. --- qutebrowser/browser/downloads.py | 4 +--- qutebrowser/utils/utils.py | 6 ++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/qutebrowser/browser/downloads.py b/qutebrowser/browser/downloads.py index 421b8c08b..27408ffd6 100644 --- a/qutebrowser/browser/downloads.py +++ b/qutebrowser/browser/downloads.py @@ -135,11 +135,11 @@ def create_full_filename(basename, filename): The full absolute path, or None if filename creation was not possible. """ basename = utils.sanitize_filename(basename) + # Filename can be a full path so don't use sanitize_filename on it. # Remove chars which can't be encoded in the filename encoding. # See https://github.com/qutebrowser/qutebrowser/issues/427 encoding = sys.getfilesystemencoding() filename = utils.force_encoding(filename, encoding) - basename = utils.force_encoding(basename, encoding) if os.path.isabs(filename) and (os.path.isdir(filename) or filename.endswith(os.sep)): # We got an absolute directory from the user, so we save it under @@ -161,8 +161,6 @@ def get_filename_question(*, suggested_filename, url, parent=None): parent: The parent of the question (a QObject). """ suggested_filename = utils.sanitize_filename(suggested_filename) - encoding = sys.getfilesystemencoding() - suggested_filename = utils.force_encoding(suggested_filename, encoding) q = usertypes.Question(parent) q.title = "Save file to:" diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index 4554aef2e..8beb21279 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -499,6 +499,12 @@ def sanitize_filename(name, replacement='_'): """ if replacement is None: replacement = '' + + # Remove chars which can't be encoded in the filename encoding. + # See https://github.com/qutebrowser/qutebrowser/issues/427 + encoding = sys.getfilesystemencoding() + name = force_encoding(name, encoding) + # Bad characters taken from Windows, there are even fewer on Linux # See also # https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words