diff Python/ceval.c @ 29643:fa97293533a9 legacy-trunk

Add PyThreadState_SetAsyncExc(long, PyObject *). A new API (only accessible from C) to interrupt a thread by sending it an exception. This is not always effective, but might help some people. Requested by Just van Rossum and Alex Martelli. It is intentional that you have to write your own C extension to call it from Python. Docs will have to wait.
author Guido van Rossum <guido@python.org>
date Sat, 28 Jun 2003 21:53:52 +0000
parents 2f731c57cdc6
children 96ecaab4cba6
line wrap: on
line diff
--- a/Python/ceval.c	Sat Jun 28 20:10:26 2003 +0000
+++ b/Python/ceval.c	Sat Jun 28 21:53:52 2003 +0000
@@ -290,7 +290,7 @@
 
 extern int _PyThread_Started; /* Flag for Py_Exit */
 
-static PyThread_type_lock interpreter_lock = 0;
+static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
 static long main_thread = 0;
 
 void
@@ -773,6 +773,11 @@
 		   Py_MakePendingCalls() above. */
 
 		if (--_Py_Ticker < 0) {
+                        if (*next_instr == SETUP_FINALLY) {
+                                /* Make the last opcode before
+                                   a try: finally: block uninterruptable. */
+                                goto fast_next_opcode;
+                        }
 			_Py_Ticker = _Py_CheckInterval;
 			tstate->tick_counter++;
 			if (things_to_do) {
@@ -805,6 +810,17 @@
 				PyThread_acquire_lock(interpreter_lock, 1);
 				if (PyThreadState_Swap(tstate) != NULL)
 					Py_FatalError("ceval: orphan tstate");
+
+				/* Check for thread interrupts */
+
+				if (tstate->async_exc != NULL) {
+					x = tstate->async_exc;
+					tstate->async_exc = NULL;
+					PyErr_SetNone(x);
+					Py_DECREF(x);
+					why = WHY_EXCEPTION;
+					goto on_error;
+				}
 			}
 #endif
 		}