Fix a regression caused by py3 porting that is preventing calibre from starting up on windows machines with non UTF-8 locales. Fixes #1836360 [no RUN ver 3.45.0 or 3.45.1](https://bugs.launchpad.net/calibre/+bug/1836360)

os.path.expanduser is broken in python2. When passed a unicode object it
concatenates it to a bytestring, which will lead to UnicodeDecodeError
if the bytestring happens to not be in the default encoding, ususally
UTF-8
This commit is contained in:
Kovid Goyal 2019-07-12 20:10:15 +05:30
parent 466e0d4a13
commit ad3156a58f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 22 additions and 17 deletions

View File

@ -7,7 +7,7 @@ __docformat__ = 'restructuredtext en'
Perform various initialization tasks.
'''
import locale, sys
import locale, sys, os
# Default translation is NOOP
from polyglot.builtins import builtins, is_py3, unicode_type
@ -79,7 +79,25 @@ if not _run_once:
if len(sys.argv) > 1 and not isinstance(sys.argv[1], unicode_type):
sys.argv[1:] = winutil.argv()[1-len(sys.argv):]
#
if not ispy3:
# Python2's expanduser is broken for non-ASCII usernames
# and unicode paths
def expanduser(path):
if isinstance(path, bytes):
path = path.decode('mbcs')
if path[:1] != '~':
return path
i, n = 1, len(path)
while i < n and path[i] not in '/\\':
i += 1
userhome = winutil.special_folder_path(winutil.CSIDL_PROFILE)
if i != 1: # ~user
userhome = os.path.join(os.path.dirname(userhome), path[1:i])
return userhome + path[i:]
os.path.expanduser = expanduser
# Ensure that all temp files/dirs are created under a calibre tmp dir
from calibre.ptempfile import base_dir
try:

View File

@ -12,7 +12,7 @@ from math import ceil
from calibre import force_unicode, isbytestring, prints, sanitize_file_name
from calibre.constants import (
filesystem_encoding, iswindows, plugins, preferred_encoding, isosx, ispy3
filesystem_encoding, iswindows, plugins, preferred_encoding, isosx
)
from calibre.utils.localization import get_udc
from polyglot.builtins import iteritems, itervalues, unicode_type, range
@ -545,19 +545,6 @@ def remove_dir_if_empty(path, ignore_metadata_caches=False):
raise
if iswindows and not ispy3:
# Python's expanduser is broken for non-ASCII usernames
def expanduser(path):
if isinstance(path, bytes):
path = path.decode(filesystem_encoding)
if path[:1] != '~':
return path
i, n = 1, len(path)
while i < n and path[i] not in '/\\':
i += 1
userhome = plugins['winutil'][0].special_folder_path(plugins['winutil'][0].CSIDL_PROFILE)
return userhome + path[i:]
else:
expanduser = os.path.expanduser