Fix translation of pywintypes.error on py3

This commit is contained in:
Kovid Goyal 2019-12-05 14:01:41 +05:30
parent 11fa71adf6
commit 4a7d673585
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 16 additions and 9 deletions

View File

@ -14,6 +14,7 @@ from calibre.constants import (
__appname__, fcntl, filesystem_encoding, islinux, isosx, iswindows, plugins
)
from calibre.utils.monotonic import monotonic
from calibre.utils.shared_file import raise_winerror
speedup = plugins['speedup'][0]
if iswindows:
@ -61,7 +62,7 @@ def windows_open(path):
None, # No template file
)
except pywintypes.error as err:
raise WindowsError(err[0], err[2], path)
raise_winerror(err)
fd = msvcrt.open_osfhandle(h.Detach(), 0)
return os.fdopen(fd, 'r+b')

View File

@ -70,12 +70,14 @@ if iswindows:
os.O_CREAT | os.O_TRUNC : CREATE_ALWAYS
}
def raise_winerror(pywinerr):
def raise_winerror(pywinerr, path=None):
reraise(
WindowsError,
WindowsError(pywinerr.winerror,
(pywinerr.funcname or '') + ': ' + (pywinerr.strerror or '')),
sys.exc_info()[2])
WindowsError, WindowsError(
pywinerr.winerror,
(pywinerr.funcname or '') + ': ' + (pywinerr.strerror or ''), path
),
sys.exc_info()[2]
)
def os_open(path, flags, mode=0o777, share_flags=FILE_SHARE_VALID_FLAGS):
'''
@ -114,7 +116,7 @@ if iswindows:
h = win32file.CreateFileW(
path, access_flags, share_flags, None, create_flags, attrib_flags, None)
except pywintypes.error as e:
raise_winerror(e)
raise_winerror(e, path)
ans = msvcrt.open_osfhandle(h.Detach(), flags | os.O_NOINHERIT)
return ans

View File

@ -191,6 +191,10 @@ def find_tests():
return unittest.defaultTestLoader.loadTestsFromTestCase(IPCLockTest)
def run_tests():
from calibre.utils.run_tests import run_tests
run_tests(find_tests)
if __name__ == '__main__':
suite = find_tests()
unittest.TextTestRunner(verbosity=4).run(suite)
run_tests()