diff --git a/src/calibre/gui2/tts/windows.py b/src/calibre/gui2/tts/windows.py index befe545f9e..8ed3076fb6 100644 --- a/src/calibre/gui2/tts/windows.py +++ b/src/calibre/gui2/tts/windows.py @@ -18,17 +18,19 @@ class Client: self.current_callback = None def __del__(self): - self.sp_voice.shutdown_event_loop() - self.events_thread.join(5) - self.sp_voice = None + if self.sp_voice is not None: + self.sp_voice.shutdown_event_loop() + self.events_thread.join(5) + self.sp_voice = None shutdown = __del__ def wait_for_events(self): while True: if self.sp_voice.wait_for_event() is False: break - if self.current_callback is not None: - self.current_callback() + c = self.current_callback + if c is not None: + c() def get_events(self): from calibre_extensions.winsapi import SPEI_TTS_BOOKMARK, SPEI_START_INPUT_STREAM, SPEI_END_INPUT_STREAM diff --git a/src/calibre/utils/windows/winsapi.cpp b/src/calibre/utils/windows/winsapi.cpp index b7efd088ff..e76039a391 100644 --- a/src/calibre/utils/windows/winsapi.cpp +++ b/src/calibre/utils/windows/winsapi.cpp @@ -340,7 +340,7 @@ Voice_shutdown_event_loop(Voice *self, PyObject *args) { } static PyObject* -get_events(Voice *self, PyObject *args) { +Voice_get_events(Voice *self, PyObject *args) { HRESULT hr; const ULONG asz = 32; ULONG num_events; @@ -383,9 +383,9 @@ get_events(Voice *self, PyObject *args) { } static PyObject* -Voice_wait_for_event(Voice *self, PyObject *callback) { - if (!PyCallable_Check(callback)) { PyErr_SetString(PyExc_TypeError, "callback object is not callable"); return NULL; } +Voice_wait_for_event(Voice *self, PyObject *args) { const HANDLE handles[2] = {self->shutdown_events_thread, self->events_available}; + DWORD ev; Py_BEGIN_ALLOW_THREADS; ev = WaitForMultipleObjects(2, handles, true, INFINITE); Py_END_ALLOW_THREADS; @@ -420,7 +420,7 @@ static PyMethodDef Voice_methods[] = { M(set_current_sound_output, METH_VARARGS), M(shutdown_event_loop, METH_NOARGS), - M(wait_for_event, METH_O), + M(wait_for_event, METH_NOARGS), M(get_events, METH_NOARGS), {NULL, NULL, 0, NULL} };