From 2c45e59900bc83648272ea44219d55ef21f22739 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Thu, 8 Nov 2018 22:04:11 +0000 Subject: [PATCH] Fix UnicodeDecodeErrors with files containing non-ascii chars The main issue here is a user trying to start deluge and the XDG `user-dirs.dirs` file contains dir names with non-ascii chars causing a UnicodeDecodeError when comparing with unicode chars since Py2 default encoding is ascii. The solution is to use io.open as used elsewhere in code with encoding set to utf8. Applied to all usage of open in common. --- deluge/common.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/deluge/common.py b/deluge/common.py index a0ffebaf3..f0b519d7e 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -26,7 +26,7 @@ import tarfile import time from contextlib import closing from datetime import datetime -from io import BytesIO +from io import BytesIO, open import pkg_resources @@ -147,7 +147,8 @@ def get_default_download_dir(): from xdg.BaseDirectory import xdg_config_home try: - with open(os.path.join(xdg_config_home, 'user-dirs.dirs'), 'r') as _file: + user_dirs_path = os.path.join(xdg_config_home, 'user-dirs.dirs') + with open(user_dirs_path, 'r', encoding='utf8') as _file: for line in _file: if not line.startswith('#') and line.startswith('XDG_DOWNLOAD_DIR'): download_dir = os.path.expandvars( @@ -1141,7 +1142,7 @@ def create_auth_file(): auth_file = deluge.configmanager.get_config_dir('auth') # Check for auth file and create if necessary if not os.path.exists(auth_file): - with open(auth_file, 'w') as _file: + with open(auth_file, 'w', encoding='utf8') as _file: _file.flush() os.fsync(_file.fileno()) # Change the permissions on the file so only this user can read/write it @@ -1157,7 +1158,7 @@ def create_localclient_account(append=False): if not os.path.exists(auth_file): create_auth_file() - with open(auth_file, 'a' if append else 'w') as _file: + with open(auth_file, 'a' if append else 'w', encoding='utf8') as _file: _file.write( ':'.join( [ @@ -1186,7 +1187,7 @@ def get_localhost_auth(): create_localclient_account() - with open(auth_file) as auth: + with open(auth_file, encoding='utf8') as auth: for line in auth: line = line.strip() if line.startswith('#') or not line: