Skip to content

Commit fd5bf44

Browse files
committed
Wrap PyGILState_Ensure/PyGILState_Release in try-finally
1 parent cd03749 commit fd5bf44

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

ahkpy/Python.ahk

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -269,19 +269,21 @@ HandleCtrlEvent(signal) {
269269

270270
CheckSignals() {
271271
gstate := PyGILState_Ensure()
272-
err := PyErr_CheckSignals()
273-
if (err == 0) {
274-
PyGILState_Release(gstate)
275-
return
276-
}
277-
; Python's signal handler raised an exception.
278-
PyExc_KeyboardInterrupt := CachedProcAddress("PyExc_KeyboardInterrupt", "PtrP")
279-
if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
272+
try {
273+
err := PyErr_CheckSignals()
274+
if (err == 0) {
275+
return
276+
}
277+
; Python's signal handler raised an exception.
278+
PyExc_KeyboardInterrupt := CachedProcAddress("PyExc_KeyboardInterrupt", "PtrP")
279+
if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
280+
PyErr_Print()
281+
ExitApp, %STATUS_CONTROL_C_EXIT%
282+
}
280283
PyErr_Print()
281-
ExitApp, %STATUS_CONTROL_C_EXIT%
284+
} finally {
285+
PyGILState_Release(gstate)
282286
}
283-
PyErr_Print()
284-
PyGILState_Release(gstate)
285287
}
286288

287289
AHKCall(self, args) {
@@ -432,9 +434,12 @@ class WrappedPythonCallable {
432434
__Delete() {
433435
WRAPPED_PYTHON_CALLABLE.Delete(this.pyFunc)
434436
gstate := PyGILState_Ensure()
435-
Py_DecRef(this.pyFunc)
436-
Py_DecRef(this.ctx)
437-
PyGILState_Release(gstate)
437+
try {
438+
Py_DecRef(this.pyFunc)
439+
Py_DecRef(this.ctx)
440+
} finally {
441+
PyGILState_Release(gstate)
442+
}
438443
}
439444
}
440445

0 commit comments

Comments
 (0)