To: vim-dev@vim.org Subject: Patch 6.2.297 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit ------------ Patch 6.2.297 (after 6.2.232) Problem: Cannot invoke Python commands recursively. Solution: With Python 2.3 and later use the available mechanisms to invoke Python recursively. (Matthew Mueller) Files: src/if_python.c *** ../vim-6.2.296/src/if_python.c Tue Feb 3 19:59:05 2004 --- src/if_python.c Sat Feb 28 21:11:15 2004 *************** *** 363,395 **** typedef PyObject PyThreadState; #endif /* Python 1.4 */ static PyThreadState* saved_python_thread = NULL; ! /* suspend a thread of the python interpreter ! - other threads are allowed to run */ ! static void Python_SaveThread(void) { saved_python_thread = PyEval_SaveThread(); } ! /* restore a thread of the python interpreter ! - waits for other threads to block */ ! static void Python_RestoreThread(void) { ! PyEval_RestoreThread( saved_python_thread ); saved_python_thread = NULL; } ! /* obtain a lock on the Vim data structures */ ! static void Python_Lock_Vim(void) { } ! /* release a lock on the Vim data structures */ ! static void Python_Release_Vim(void) { } --- 363,404 ---- typedef PyObject PyThreadState; #endif /* Python 1.4 */ + #if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x020300F0 + /* Python 2.3: can invoke ":python" recursively. */ + # define PY_CAN_RECURSE + #else static PyThreadState* saved_python_thread = NULL; ! /* ! * Suspend a thread of the Python interpreter, other threads are allowed to ! * run. ! */ static void Python_SaveThread(void) { saved_python_thread = PyEval_SaveThread(); } ! /* ! * Restore a thread of the Python interpreter, waits for other threads to ! * block. ! */ static void Python_RestoreThread(void) { ! PyEval_RestoreThread(saved_python_thread); saved_python_thread = NULL; } + #endif ! /* ! * obtain a lock on the Vim data structures ! */ static void Python_Lock_Vim(void) { } ! /* ! * release a lock on the Vim data structures ! */ static void Python_Release_Vim(void) { } *************** *** 433,440 **** --- 442,451 ---- if (PythonMod_Init()) goto fail; + #ifndef PY_CAN_RECURSE /* the first python thread is vim's */ Python_SaveThread(); + #endif initialised = 1; } *************** *** 457,476 **** static void DoPythonCommand(exarg_T *eap, const char *cmd) { ! static int recursive = 0; if (recursive) { EMSG(_("E659: Cannot invoke Python recursively")); return; } ++recursive; #if defined(MACOS) && !defined(MACOS_X_UNIX) ! GrafPtr oldPort; ! GetPort (&oldPort); /* Check if the Python library is available */ ! if ( (Ptr) PyMac_Initialize == (Ptr) kUnresolvedCFragSymbolAddress) goto theend; #endif if (Python_Init()) --- 468,495 ---- static void DoPythonCommand(exarg_T *eap, const char *cmd) { ! #ifdef PY_CAN_RECURSE ! PyGILState_STATE pygilstate; ! #else ! static int recursive = 0; ! #endif ! #if defined(MACOS) && !defined(MACOS_X_UNIX) ! GrafPtr oldPort; ! #endif + #ifndef PY_CAN_RECURSE if (recursive) { EMSG(_("E659: Cannot invoke Python recursively")); return; } ++recursive; + #endif #if defined(MACOS) && !defined(MACOS_X_UNIX) ! GetPort(&oldPort); /* Check if the Python library is available */ ! if ((Ptr)PyMac_Initialize == (Ptr)kUnresolvedCFragSymbolAddress) goto theend; #endif if (Python_Init()) *************** *** 479,495 **** RangeStart = eap->line1; RangeEnd = eap->line2; Python_Release_Vim(); /* leave vim */ Python_RestoreThread(); /* enter python */ PyRun_SimpleString((char *)(cmd)); Python_SaveThread(); /* leave python */ Python_Lock_Vim(); /* enter vim */ PythonIO_Flush(); #if defined(MACOS) && !defined(MACOS_X_UNIX) ! SetPort (oldPort); #endif theend: ! --recursive; } /* --- 498,529 ---- RangeStart = eap->line1; RangeEnd = eap->line2; Python_Release_Vim(); /* leave vim */ + + #ifdef PY_CAN_RECURSE + pygilstate = PyGILState_Ensure(); + #else Python_RestoreThread(); /* enter python */ + #endif + PyRun_SimpleString((char *)(cmd)); + + #ifdef PY_CAN_RECURSE + PyGILState_Release(pygilstate); + #else Python_SaveThread(); /* leave python */ + #endif + Python_Lock_Vim(); /* enter vim */ PythonIO_Flush(); #if defined(MACOS) && !defined(MACOS_X_UNIX) ! SetPort(oldPort); #endif theend: ! #ifndef PY_CAN_RECURSE ! --recursive ! #endif ! return; /* keeps lint happy */ } /* *** ../vim-6.2.296/src/version.c Sun Feb 29 14:48:02 2004 --- src/version.c Sun Feb 29 14:52:02 2004 *************** *** 639,640 **** --- 639,642 ---- { /* Add new patch number below this line */ + /**/ + 297, /**/ -- I have to exercise early in the morning before my brain figures out what I'm doing. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ Project leader for A-A-P -- http://www.A-A-P.org /// \\\ Buy at Amazon and help AIDS victims -- http://ICCF.nl/click1.html ///