mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-08-11 09:13:57 -04:00
Get rid of code using the CRT on windows via ctypes as with the Universal CRT there is no way to load it
This commit is contained in:
parent
a5f9faaa5d
commit
8211335683
@ -274,12 +274,6 @@ def test_openssl():
|
|||||||
raise ValueError('Mozilla CA certs not loaded')
|
raise ValueError('Mozilla CA certs not loaded')
|
||||||
fprint('SSL OK!')
|
fprint('SSL OK!')
|
||||||
|
|
||||||
def test_crt():
|
|
||||||
from ctypes.util import find_msvcrt
|
|
||||||
if not find_msvcrt():
|
|
||||||
raise ValueError('find_msvcrt() failed')
|
|
||||||
fprint('CRT OK!')
|
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
if iswindows:
|
if iswindows:
|
||||||
test_dlls()
|
test_dlls()
|
||||||
@ -311,7 +305,6 @@ def test():
|
|||||||
test_wpd()
|
test_wpd()
|
||||||
test_winutil()
|
test_winutil()
|
||||||
test_file_dialog_helper()
|
test_file_dialog_helper()
|
||||||
test_crt()
|
|
||||||
else:
|
else:
|
||||||
test_terminal()
|
test_terminal()
|
||||||
if isosx:
|
if isosx:
|
||||||
|
@ -104,15 +104,13 @@ class Detect(object):
|
|||||||
import msvcrt
|
import msvcrt
|
||||||
self.msvcrt = msvcrt
|
self.msvcrt = msvcrt
|
||||||
self.file_handle = msvcrt.get_osfhandle(self.stream.fileno())
|
self.file_handle = msvcrt.get_osfhandle(self.stream.fileno())
|
||||||
from ctypes import windll, wintypes, byref, c_wchar_p, c_size_t, POINTER, WinDLL
|
from ctypes import windll, wintypes, byref, POINTER, WinDLL
|
||||||
mode = wintypes.DWORD(0)
|
mode = wintypes.DWORD(0)
|
||||||
f = windll.kernel32.GetConsoleMode
|
f = windll.kernel32.GetConsoleMode
|
||||||
f.argtypes, f.restype = [wintypes.HANDLE, POINTER(wintypes.DWORD)], wintypes.BOOL
|
f.argtypes, f.restype = [wintypes.HANDLE, POINTER(wintypes.DWORD)], wintypes.BOOL
|
||||||
if f(self.file_handle, byref(mode)):
|
if f(self.file_handle, byref(mode)):
|
||||||
# Stream is a console
|
# Stream is a console
|
||||||
self.set_console = windll.kernel32.SetConsoleTextAttribute
|
self.set_console = windll.kernel32.SetConsoleTextAttribute
|
||||||
self.wcslen = crt().wcslen
|
|
||||||
self.wcslen.argtypes, self.wcslen.restype = [c_wchar_p], c_size_t
|
|
||||||
self.write_console = WinDLL('kernel32', use_last_error=True).WriteConsoleW
|
self.write_console = WinDLL('kernel32', use_last_error=True).WriteConsoleW
|
||||||
self.write_console.argtypes = [wintypes.HANDLE, wintypes.c_wchar_p, wintypes.DWORD, POINTER(wintypes.DWORD), wintypes.LPVOID]
|
self.write_console.argtypes = [wintypes.HANDLE, wintypes.c_wchar_p, wintypes.DWORD, POINTER(wintypes.DWORD), wintypes.LPVOID]
|
||||||
self.write_console.restype = wintypes.BOOL
|
self.write_console.restype = wintypes.BOOL
|
||||||
@ -128,8 +126,9 @@ class Detect(object):
|
|||||||
chunk = len(text)
|
chunk = len(text)
|
||||||
while text:
|
while text:
|
||||||
t, text = text[:chunk], text[chunk:]
|
t, text = text[:chunk], text[chunk:]
|
||||||
|
t = t.partition('\0')[0]
|
||||||
wt = c_wchar_p(t)
|
wt = c_wchar_p(t)
|
||||||
if not self.write_console(self.file_handle, wt, self.wcslen(wt), byref(written), None):
|
if not self.write_console(self.file_handle, wt, len(t), byref(written), None):
|
||||||
# Older versions of windows can fail to write large strings
|
# Older versions of windows can fail to write large strings
|
||||||
# to console with WriteConsoleW (seen it happen on Win XP)
|
# to console with WriteConsoleW (seen it happen on Win XP)
|
||||||
import ctypes, winerror
|
import ctypes, winerror
|
||||||
@ -153,16 +152,6 @@ class Detect(object):
|
|||||||
if not ignore_errors:
|
if not ignore_errors:
|
||||||
raise ctypes.WinError(err)
|
raise ctypes.WinError(err)
|
||||||
|
|
||||||
_crt = None
|
|
||||||
def crt():
|
|
||||||
# We use the C runtime bundled with the calibre windows build
|
|
||||||
global _crt
|
|
||||||
if _crt is None:
|
|
||||||
import ctypes
|
|
||||||
from ctypes.util import find_msvcrt
|
|
||||||
_crt = ctypes.CDLL(find_msvcrt())
|
|
||||||
return _crt
|
|
||||||
|
|
||||||
class ColoredStream(Detect):
|
class ColoredStream(Detect):
|
||||||
|
|
||||||
def __init__(self, stream=None, fg=None, bg=None, bold=False):
|
def __init__(self, stream=None, fg=None, bg=None, bold=False):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user