Dont use win32com for expanduser

This commit is contained in:
Kovid Goyal 2019-06-11 16:04:47 +05:30
parent 57572d977a
commit 529033ff78
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 8 additions and 4 deletions

View File

@ -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 filesystem_encoding, iswindows, plugins, preferred_encoding, isosx, ispy3
) )
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,7 +545,7 @@ def remove_dir_if_empty(path, ignore_metadata_caches=False):
raise raise
if iswindows: if iswindows and not ispy3:
# Python's expanduser is broken for non-ASCII usernames # Python's expanduser is broken for non-ASCII usernames
def expanduser(path): def expanduser(path):
if isinstance(path, bytes): if isinstance(path, bytes):
@ -555,8 +555,7 @@ if iswindows:
i, n = 1, len(path) i, n = 1, len(path)
while i < n and path[i] not in '/\\': while i < n and path[i] not in '/\\':
i += 1 i += 1
from win32com.shell import shell, shellcon userhome = plugins['winutil'][0].special_folder_path(plugins['winutil'][0].CSIDL_PROFILE)
userhome = shell.SHGetFolderPath(0, shellcon.CSIDL_PROFILE, None, 0)
return userhome + path[i:] return userhome + path[i:]
else: else:
expanduser = os.path.expanduser expanduser = os.path.expanduser

View File

@ -27,6 +27,9 @@ class TestWinutil(unittest.TestCase):
self.assertIn('notepad.exe', self.winutil.file_association('.txt')) self.assertIn('notepad.exe', self.winutil.file_association('.txt'))
self.assertIsNone(self.winutil.file_association('.mkjsfks')) self.assertIsNone(self.winutil.file_association('.mkjsfks'))
def test_special_folder_path(self):
self.assertEqual(os.path.expanduser('~'), self.winutil.special_folder_path(self.winutil.CSIDL_PROFILE))
def find_tests(): def find_tests():
return unittest.defaultTestLoader.loadTestsFromTestCase(TestWinutil) return unittest.defaultTestLoader.loadTestsFromTestCase(TestWinutil)

View File

@ -71,3 +71,5 @@ file_association(PyObject *self, PyObject *args) {
if (!SUCCEEDED(hr)) Py_RETURN_NONE; if (!SUCCEEDED(hr)) Py_RETURN_NONE;
return Py_BuildValue("u#", buf, (int)sz); return Py_BuildValue("u#", buf, (int)sz);
} }
}