From 0da7f4fb26b25e5ed1d35dbce43e2e8bc4ca494a Mon Sep 17 00:00:00 2001 From: Antti Kajander Date: Sat, 18 Nov 2023 02:02:38 +0200 Subject: [PATCH] Use ulong for thread id on Python >= 3.7 As per https://docs.python.org/3/c-api/init.html#c.PyThreadState_SetAsyncExc --- src/stopit/threadstop.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/stopit/threadstop.py b/src/stopit/threadstop.py index 37ecb92..a991750 100644 --- a/src/stopit/threadstop.py +++ b/src/stopit/threadstop.py @@ -9,10 +9,16 @@ """ import ctypes +import sys import threading from .utils import TimeoutException, BaseTimeout, base_timeoutable +if sys.version_info < (3, 7): + tid_ctype = ctypes.c_long +else: + tid_ctype = ctypes.c_ulong + def async_raise(target_tid, exception): """Raises an asynchronous exception in another thread. @@ -24,13 +30,13 @@ def async_raise(target_tid, exception): """ # Ensuring and releasing GIL are useless since we're not in C # gil_state = ctypes.pythonapi.PyGILState_Ensure() - ret = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(target_tid), + ret = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid_ctype(target_tid), ctypes.py_object(exception)) # ctypes.pythonapi.PyGILState_Release(gil_state) if ret == 0: raise ValueError("Invalid thread ID {}".format(target_tid)) elif ret > 1: - ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(target_tid), None) + ctypes.pythonapi.PyThreadState_SetAsyncExc(tid_ctype(target_tid), None) raise SystemError("PyThreadState_SetAsyncExc failed")