From bb9bdf7e5b5cb0d3458242c5b8ba6134efb282a4 Mon Sep 17 00:00:00 2001 From: Miro Hron?ok Date: Fri, 8 Jun 2018 18:49:42 +0200 Subject: Don't raise StopIteration from generator, return instead See https://www.python.org/dev/peps/pep-0479/ --- paste/util/killthread.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 paste/util/killthread.py (limited to 'paste/util/killthread.py') diff --git a/paste/util/killthread.py b/paste/util/killthread.py new file mode 100644 index 0000000..4df4f42 --- /dev/null +++ b/paste/util/killthread.py @@ -0,0 +1,30 @@ +""" +Kill a thread, from http://sebulba.wikispaces.com/recipe+thread2 +""" +import six +try: + import ctypes +except ImportError: + raise ImportError( + "You cannot use paste.util.killthread without ctypes installed") +if not hasattr(ctypes, 'pythonapi'): + raise ImportError( + "You cannot use paste.util.killthread without ctypes.pythonapi") + +def async_raise(tid, exctype): + """raises the exception, performs cleanup if needed. + + tid is the value given by thread.get_ident() (an integer). + Raise SystemExit to kill a thread.""" + if not isinstance(exctype, (six.class_types, type)): + raise TypeError("Only types can be raised (not instances)") + if not isinstance(tid, int): + raise TypeError("tid must be an integer") + res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(exctype)) + if res == 0: + raise ValueError("invalid thread id") + elif res != 1: + # """if it returns a number greater than one, you're in trouble, + # and you should call it again with exc=NULL to revert the effect""" + ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), 0) + raise SystemError("PyThreadState_SetAsyncExc failed") -- cgit v1.2.1