diff -r d2571e334983 src/if_py_both.h
--- a/src/if_py_both.h	Tue Mar 19 18:31:49 2013 +0100
+++ b/src/if_py_both.h	Mon Mar 25 10:46:42 2013 +0900
@@ -1408,7 +1408,14 @@
 	}
     }
 
+    Py_BEGIN_ALLOW_THREADS
+    Python_Lock_Vim();
+
     error = func_call(name, &args, selfdict, &rettv);
+
+    Python_Release_Vim();
+    Py_END_ALLOW_THREADS
+
     if (error != OK)
     {
 	result = NULL;
diff -r d2571e334983 src/if_python.c
--- a/src/if_python.c	Tue Mar 19 18:31:49 2013 +0100
+++ b/src/if_python.c	Mon Mar 25 10:46:42 2013 +0900
@@ -647,11 +647,8 @@
 typedef PyObject PyThreadState;
 #endif
 
-#ifdef PY_CAN_RECURSE
-static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
-#else
+#ifndef PY_CAN_RECURSE
 static PyThreadState *saved_python_thread = NULL;
-#endif
 
 /*
  * Suspend a thread of the Python interpreter, other threads are allowed to
@@ -660,11 +657,7 @@
     static void
 Python_SaveThread(void)
 {
-#ifdef PY_CAN_RECURSE
-    PyGILState_Release(pygilstate);
-#else
     saved_python_thread = PyEval_SaveThread();
-#endif
 }
 
 /*
@@ -674,13 +667,10 @@
     static void
 Python_RestoreThread(void)
 {
-#ifdef PY_CAN_RECURSE
-    pygilstate = PyGILState_Ensure();
-#else
     PyEval_RestoreThread(saved_python_thread);
     saved_python_thread = NULL;
+}
 #endif
-}
 
     void
 python_end()
@@ -696,14 +686,22 @@
 #ifdef DYNAMIC_PYTHON
     if (hinstPython && Py_IsInitialized())
     {
+# ifdef PY_CAN_RECURSE
+	PyGILState_Ensure();
+# else
 	Python_RestoreThread();	    /* enter python */
+# endif
 	Py_Finalize();
     }
     end_dynamic_python();
 #else
     if (Py_IsInitialized())
     {
+# ifdef PY_CAN_RECURSE
+	PyGILState_Ensure();
+# else
 	Python_RestoreThread();	    /* enter python */
+# endif
 	Py_Finalize();
     }
 #endif
@@ -805,6 +803,9 @@
 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
     char		*saved_locale;
 #endif
+#ifdef PY_CAN_RECURSE
+    PyGILState_STATE	pygilstate;
+#endif
 
 #ifndef PY_CAN_RECURSE
     if (recursive)
@@ -849,7 +850,11 @@
     }
 #endif
 
+#ifdef PY_CAN_RECURSE
+    pygilstate = PyGILState_Ensure();
+#else
     Python_RestoreThread();	    /* enter python */
+#endif
 
     if (rettv == NULL)
 	PyRun_SimpleString((char *)(cmd));
@@ -873,7 +878,11 @@
 	PyErr_Clear();
     }
 
+#ifdef PY_CAN_RECURSE
+    PyGILState_Release(pygilstate);
+#else
     Python_SaveThread();	    /* leave python */
+#endif
 
 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
     if (saved_locale != NULL)
diff -r d2571e334983 src/if_python3.c
--- a/src/if_python3.c	Tue Mar 19 18:31:49 2013 +0100
+++ b/src/if_python3.c	Mon Mar 25 10:46:42 2013 +0900
@@ -666,8 +666,6 @@
  * 1. Python interpreter main program.
  */
 
-static PyGILState_STATE pygilstate = PyGILState_UNLOCKED;
-
     void
 python3_end()
 {
@@ -685,7 +683,7 @@
     if (Py_IsInitialized())
     {
 	// acquire lock before finalizing
-	pygilstate = PyGILState_Ensure();
+	PyGILState_Ensure();
 
 	Py_Finalize();
     }
@@ -793,6 +791,7 @@
 #endif
     PyObject		*cmdstr;
     PyObject		*cmdbytes;
+    PyGILState_STATE	pygilstate;
 
 #if defined(MACOS) && !defined(MACOS_X_UNIX)
     GetPort(&oldPort);
