Index: Source/Modules/python.cxx
===================================================================
--- Source/Modules/python.cxx	(revision 10831)
+++ Source/Modules/python.cxx	(working copy)
@@ -188,6 +188,7 @@
       if (bb) {
 	Append(f, bb);
       } else {
+	Append(f, "{ PyEval_AcquireLock(); PyEval_ReleaseLock(); } // Will deadlock if GIL already held by this thread.\n");
 	Append(f, "SWIG_PYTHON_THREAD_BEGIN_BLOCK;\n");
       }
     }
@@ -200,6 +201,7 @@
 	Append(f, eb);
       } else {
 	Append(f, "SWIG_PYTHON_THREAD_END_BLOCK;\n");
+	Append(f, "{ PyEval_AcquireLock(); PyEval_ReleaseLock(); } // Will deadlock if GIL already held by this thread.\n");
       }
     }
   }
@@ -1497,8 +1499,8 @@
     Wrapper_add_local(f, "argc", "int argc");
     Printf(tmp, "PyObject *argv[%d]", maxargs + 1);
     Wrapper_add_local(f, "argv", tmp);
-    if (allow_thread)
-      thread_begin_block(n, f->code);
+    if (allow_thread && !GetFlag(n, "feature:nothreadblock"))
+      Append(f->code, "(void)PyThreadState_Get(); // Aborts if GIL not held\n");
 
     if (!fastunpack) {
       Wrapper_add_local(f, "ii", "int ii");
@@ -1517,7 +1519,8 @@
 
     if (allow_thread) {
       String *ret = NewStringEmpty();
-      thread_end_block(n, ret);
+      if (allow_thread && !GetFlag(n, "feature:nothreadblock"))
+	Append(ret, "(void)PyThreadState_Get(); // Aborts if GIL not held\n");
       Append(ret, "return ");
       Replaceall(dispatch, "return ", ret);
       Delete(ret);
@@ -1525,8 +1528,8 @@
 
     Printv(f->code, dispatch, "\n", NIL);
 
-    if (allow_thread)
-      thread_end_block(n, f->code);
+    if (allow_thread && !GetFlag(n, "feature:nothreadblock"))
+      Append(f->code, "(void)PyThreadState_Get(); // Aborts if GIL not held\n");
 
     if (GetFlag(n, "feature:python:maybecall")) {
       Append(f->code, "fail:\n");
@@ -1634,8 +1637,8 @@
     kwargs = NewString("");
 
     int allow_thread = threads_enable(n);
-    if (allow_thread)
-      thread_begin_block(n, f->code);
+    if (allow_thread && !GetFlag(n, "feature:nothreadblock"))
+      Append(f->code, "(void)PyThreadState_Get(); // Aborts if GIL not held\n");
 
     Wrapper_add_local(f, "resultobj", "PyObject *resultobj = 0");
 
@@ -2066,8 +2069,8 @@
       }
     }
 
-    if (allow_thread)
-      thread_end_block(n, f->code);
+    if (allow_thread && !GetFlag(n, "feature:nothreadblock"))
+      Append(f->code, "(void)PyThreadState_Get(); // Aborts if GIL not held\n");
     Append(f->code, "    return resultobj;\n");
 
     /* Error handling code */
@@ -2076,8 +2079,8 @@
     if (need_cleanup) {
       Printv(f->code, cleanup, NIL);
     }
-    if (allow_thread)
-      thread_end_block(n, f->code);
+    if (allow_thread && !GetFlag(n, "feature:nothreadblock"))
+      Append(f->code, "(void)PyThreadState_Get(); // Aborts if GIL not held\n");
     Printv(f->code, ctab4, "return NULL;\n", NIL);
 
 
