mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
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:
parent
466e0d4a13
commit
ad3156a58f
@ -7,7 +7,7 @@ __docformat__ = 'restructuredtext en'
|
|||||||
Perform various initialization tasks.
|
Perform various initialization tasks.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import locale, sys
|
import locale, sys, os
|
||||||
|
|
||||||
# Default translation is NOOP
|
# Default translation is NOOP
|
||||||
from polyglot.builtins import builtins, is_py3, unicode_type
|
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):
|
if len(sys.argv) > 1 and not isinstance(sys.argv[1], unicode_type):
|
||||||
sys.argv[1:] = winutil.argv()[1-len(sys.argv):]
|
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
|
# Ensure that all temp files/dirs are created under a calibre tmp dir
|
||||||
from calibre.ptempfile import base_dir
|
from calibre.ptempfile import base_dir
|
||||||
try:
|
try:
|
||||||
|
@ -12,7 +12,7 @@ from math import ceil
|
|||||||
|
|
||||||
from calibre import force_unicode, isbytestring, prints, sanitize_file_name
|
from calibre import force_unicode, isbytestring, prints, sanitize_file_name
|
||||||
from calibre.constants import (
|
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 calibre.utils.localization import get_udc
|
||||||
from polyglot.builtins import iteritems, itervalues, unicode_type, range
|
from polyglot.builtins import iteritems, itervalues, unicode_type, range
|
||||||
@ -545,20 +545,7 @@ def remove_dir_if_empty(path, ignore_metadata_caches=False):
|
|||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
if iswindows and not ispy3:
|
expanduser = os.path.expanduser
|
||||||
# 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
|
|
||||||
|
|
||||||
|
|
||||||
def format_permissions(st_mode):
|
def format_permissions(st_mode):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user