Karsten Hopp ae61a4
To: vim_dev@googlegroups.com
Karsten Hopp ae61a4
Subject: Patch 7.3.991
Karsten Hopp ae61a4
Fcc: outbox
Karsten Hopp ae61a4
From: Bram Moolenaar <Bram@moolenaar.net>
Karsten Hopp ae61a4
Mime-Version: 1.0
Karsten Hopp ae61a4
Content-Type: text/plain; charset=UTF-8
Karsten Hopp ae61a4
Content-Transfer-Encoding: 8bit
Karsten Hopp ae61a4
------------
Karsten Hopp ae61a4
Karsten Hopp ae61a4
Patch 7.3.991
Karsten Hopp ae61a4
Problem:    More can be shared by Python 2 and 3.
Karsten Hopp ae61a4
Solution:   Move more stuff to if_py_both. (ZyX)
Karsten Hopp ae61a4
Files:	    src/if_py_both.h, src/if_python3.c, src/if_python.c,
Karsten Hopp ae61a4
	    src/testdir/test87.ok
Karsten Hopp ae61a4
Karsten Hopp ae61a4
Karsten Hopp ae61a4
*** ../vim-7.3.990/src/if_py_both.h	2013-05-17 21:20:13.000000000 +0200
Karsten Hopp ae61a4
--- src/if_py_both.h	2013-05-21 18:07:46.000000000 +0200
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 30,35 ****
Karsten Hopp ae61a4
--- 30,38 ----
Karsten Hopp ae61a4
  #define INVALID_WINDOW_VALUE ((win_T *)(-1))
Karsten Hopp ae61a4
  #define INVALID_TABPAGE_VALUE ((tabpage_T *)(-1))
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
+ typedef void (*rangeinitializer)(void *);
Karsten Hopp ae61a4
+ typedef void (*runner)(const char *, void *, PyGILState_STATE *);
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
  static int ConvertFromPyObject(PyObject *, typval_T *);
Karsten Hopp ae61a4
  static int _ConvertFromPyObject(PyObject *, typval_T *, PyObject *);
Karsten Hopp ae61a4
  static PyObject *WindowNew(win_T *, tabpage_T *);
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 39,44 ****
Karsten Hopp ae61a4
--- 42,49 ----
Karsten Hopp ae61a4
  static PyInt RangeStart;
Karsten Hopp ae61a4
  static PyInt RangeEnd;
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
+ static PyObject *globals;
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
  /*
Karsten Hopp ae61a4
   * obtain a lock on the Vim data structures
Karsten Hopp ae61a4
   */
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 1296,1302 ****
Karsten Hopp ae61a4
      FunctionObject	*this = (FunctionObject *) (self);
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      func_unref(this->name);
Karsten Hopp ae61a4
!     PyMem_Del(this->name);
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      DESTRUCTOR_FINISH(self);
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
--- 1301,1307 ----
Karsten Hopp ae61a4
      FunctionObject	*this = (FunctionObject *) (self);
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      func_unref(this->name);
Karsten Hopp ae61a4
!     PyMem_Free(this->name);
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      DESTRUCTOR_FINISH(self);
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 3432,3437 ****
Karsten Hopp ae61a4
--- 3437,3562 ----
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      static void
Karsten Hopp ae61a4
+ init_range_cmd(exarg_T *eap)
Karsten Hopp ae61a4
+ {
Karsten Hopp ae61a4
+     RangeStart = eap->line1;
Karsten Hopp ae61a4
+     RangeEnd = eap->line2;
Karsten Hopp ae61a4
+ }
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+     static void
Karsten Hopp ae61a4
+ init_range_eval(typval_T *rettv UNUSED)
Karsten Hopp ae61a4
+ {
Karsten Hopp ae61a4
+     RangeStart = (PyInt) curwin->w_cursor.lnum;
Karsten Hopp ae61a4
+     RangeEnd = RangeStart;
Karsten Hopp ae61a4
+ }
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+     static void
Karsten Hopp ae61a4
+ run_cmd(const char *cmd, void *arg UNUSED, PyGILState_STATE *pygilstate UNUSED)
Karsten Hopp ae61a4
+ {
Karsten Hopp ae61a4
+     PyRun_SimpleString((char *) cmd);
Karsten Hopp ae61a4
+ }
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+ static const char	*code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
Karsten Hopp ae61a4
+ static int		code_hdr_len = 30;
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+     static void
Karsten Hopp ae61a4
+ run_do(const char *cmd, void *arg UNUSED, PyGILState_STATE *pygilstate)
Karsten Hopp ae61a4
+ {
Karsten Hopp ae61a4
+     PyInt	lnum;
Karsten Hopp ae61a4
+     size_t	len;
Karsten Hopp ae61a4
+     char	*code;
Karsten Hopp ae61a4
+     int		status;
Karsten Hopp ae61a4
+     PyObject	*pyfunc, *pymain;
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+     if (u_save(RangeStart - 1, RangeEnd + 1) != OK)
Karsten Hopp ae61a4
+     {
Karsten Hopp ae61a4
+ 	EMSG(_("cannot save undo information"));
Karsten Hopp ae61a4
+ 	return;
Karsten Hopp ae61a4
+     }
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+     len = code_hdr_len + STRLEN(cmd);
Karsten Hopp ae61a4
+     code = PyMem_New(char, len + 1);
Karsten Hopp ae61a4
+     memcpy(code, code_hdr, code_hdr_len);
Karsten Hopp ae61a4
+     STRCPY(code + code_hdr_len, cmd);
Karsten Hopp ae61a4
+     status = PyRun_SimpleString(code);
Karsten Hopp ae61a4
+     PyMem_Free(code);
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+     if (status)
Karsten Hopp ae61a4
+     {
Karsten Hopp ae61a4
+ 	EMSG(_("failed to run the code"));
Karsten Hopp ae61a4
+ 	return;
Karsten Hopp ae61a4
+     }
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+     status = 0;
Karsten Hopp ae61a4
+     pymain = PyImport_AddModule("__main__");
Karsten Hopp ae61a4
+     pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Karsten Hopp ae61a4
+     PyGILState_Release(*pygilstate);
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+     for (lnum = RangeStart; lnum <= RangeEnd; ++lnum)
Karsten Hopp ae61a4
+     {
Karsten Hopp ae61a4
+ 	PyObject	*line, *linenr, *ret;
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+ 	*pygilstate = PyGILState_Ensure();
Karsten Hopp ae61a4
+ 	if (!(line = GetBufferLine(curbuf, lnum)))
Karsten Hopp ae61a4
+ 	    goto err;
Karsten Hopp ae61a4
+ 	if (!(linenr = PyInt_FromLong((long) lnum)))
Karsten Hopp ae61a4
+ 	{
Karsten Hopp ae61a4
+ 	    Py_DECREF(line);
Karsten Hopp ae61a4
+ 	    goto err;
Karsten Hopp ae61a4
+ 	}
Karsten Hopp ae61a4
+ 	ret = PyObject_CallFunctionObjArgs(pyfunc, line, linenr, NULL);
Karsten Hopp ae61a4
+ 	Py_DECREF(line);
Karsten Hopp ae61a4
+ 	Py_DECREF(linenr);
Karsten Hopp ae61a4
+ 	if (!ret)
Karsten Hopp ae61a4
+ 	    goto err;
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+ 	if (ret != Py_None)
Karsten Hopp ae61a4
+ 	    if (SetBufferLine(curbuf, lnum, ret, NULL) == FAIL)
Karsten Hopp ae61a4
+ 		goto err;
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+ 	Py_XDECREF(ret);
Karsten Hopp ae61a4
+ 	PythonIO_Flush();
Karsten Hopp ae61a4
+ 	PyGILState_Release(*pygilstate);
Karsten Hopp ae61a4
+     }
Karsten Hopp ae61a4
+     goto out;
Karsten Hopp ae61a4
+ err:
Karsten Hopp ae61a4
+     *pygilstate = PyGILState_Ensure();
Karsten Hopp ae61a4
+     PyErr_PrintEx(0);
Karsten Hopp ae61a4
+     PythonIO_Flush();
Karsten Hopp ae61a4
+     status = 1;
Karsten Hopp ae61a4
+ out:
Karsten Hopp ae61a4
+     if (!status)
Karsten Hopp ae61a4
+ 	*pygilstate = PyGILState_Ensure();
Karsten Hopp ae61a4
+     Py_DECREF(pyfunc);
Karsten Hopp ae61a4
+     PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
Karsten Hopp ae61a4
+     if (status)
Karsten Hopp ae61a4
+ 	return;
Karsten Hopp ae61a4
+     check_cursor();
Karsten Hopp ae61a4
+     update_curbuf(NOT_VALID);
Karsten Hopp ae61a4
+ }
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+     static void
Karsten Hopp ae61a4
+ run_eval(const char *cmd, typval_T *rettv, PyGILState_STATE *pygilstate UNUSED)
Karsten Hopp ae61a4
+ {
Karsten Hopp ae61a4
+     PyObject	*r;
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+     r = PyRun_String((char *) cmd, Py_eval_input, globals, globals);
Karsten Hopp ae61a4
+     if (r == NULL)
Karsten Hopp ae61a4
+     {
Karsten Hopp ae61a4
+ 	if (PyErr_Occurred() && !msg_silent)
Karsten Hopp ae61a4
+ 	    PyErr_PrintEx(0);
Karsten Hopp ae61a4
+ 	EMSG(_("E858: Eval did not return a valid python object"));
Karsten Hopp ae61a4
+     }
Karsten Hopp ae61a4
+     else
Karsten Hopp ae61a4
+     {
Karsten Hopp ae61a4
+ 	if (ConvertFromPyObject(r, rettv) == -1)
Karsten Hopp ae61a4
+ 	    EMSG(_("E859: Failed to convert returned python object to vim value"));
Karsten Hopp ae61a4
+ 	Py_DECREF(r);
Karsten Hopp ae61a4
+     }
Karsten Hopp ae61a4
+     PyErr_Clear();
Karsten Hopp ae61a4
+ }
Karsten Hopp ae61a4
+ 
Karsten Hopp ae61a4
+     static void
Karsten Hopp ae61a4
  set_ref_in_py(const int copyID)
Karsten Hopp ae61a4
  {
Karsten Hopp ae61a4
      pylinkedlist_T	*cur;
Karsten Hopp ae61a4
*** ../vim-7.3.990/src/if_python3.c	2013-05-17 16:39:59.000000000 +0200
Karsten Hopp ae61a4
--- src/if_python3.c	2013-05-21 18:07:39.000000000 +0200
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 703,710 ****
Karsten Hopp ae61a4
   * Internal function prototypes.
Karsten Hopp ae61a4
   */
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
- static PyObject *globals;
Karsten Hopp ae61a4
- 
Karsten Hopp ae61a4
  static int PythonIO_Init(void);
Karsten Hopp ae61a4
  static PyObject *Py3Init_vim(void);
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
--- 703,708 ----
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 827,833 ****
Karsten Hopp ae61a4
   * External interface
Karsten Hopp ae61a4
   */
Karsten Hopp ae61a4
      static void
Karsten Hopp ae61a4
! DoPy3Command(exarg_T *eap, const char *cmd, typval_T *rettv)
Karsten Hopp ae61a4
  {
Karsten Hopp ae61a4
  #if defined(MACOS) && !defined(MACOS_X_UNIX)
Karsten Hopp ae61a4
      GrafPtr		oldPort;
Karsten Hopp ae61a4
--- 825,831 ----
Karsten Hopp ae61a4
   * External interface
Karsten Hopp ae61a4
   */
Karsten Hopp ae61a4
      static void
Karsten Hopp ae61a4
! DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Karsten Hopp ae61a4
  {
Karsten Hopp ae61a4
  #if defined(MACOS) && !defined(MACOS_X_UNIX)
Karsten Hopp ae61a4
      GrafPtr		oldPort;
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 848,863 ****
Karsten Hopp ae61a4
      if (Python3_Init())
Karsten Hopp ae61a4
  	goto theend;
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
!     if (rettv == NULL)
Karsten Hopp ae61a4
!     {
Karsten Hopp ae61a4
! 	RangeStart = eap->line1;
Karsten Hopp ae61a4
! 	RangeEnd = eap->line2;
Karsten Hopp ae61a4
!     }
Karsten Hopp ae61a4
!     else
Karsten Hopp ae61a4
!     {
Karsten Hopp ae61a4
! 	RangeStart = (PyInt) curwin->w_cursor.lnum;
Karsten Hopp ae61a4
! 	RangeEnd = RangeStart;
Karsten Hopp ae61a4
!     }
Karsten Hopp ae61a4
      Python_Release_Vim();	    /* leave vim */
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
  #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Karsten Hopp ae61a4
--- 846,853 ----
Karsten Hopp ae61a4
      if (Python3_Init())
Karsten Hopp ae61a4
  	goto theend;
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
!     init_range(arg);
Karsten Hopp ae61a4
! 
Karsten Hopp ae61a4
      Python_Release_Vim();	    /* leave vim */
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
  #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 881,908 ****
Karsten Hopp ae61a4
  					(char *)ENC_OPT, CODEC_ERROR_HANDLER);
Karsten Hopp ae61a4
      cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Karsten Hopp ae61a4
      Py_XDECREF(cmdstr);
Karsten Hopp ae61a4
-     if (rettv == NULL)
Karsten Hopp ae61a4
- 	PyRun_SimpleString(PyBytes_AsString(cmdbytes));
Karsten Hopp ae61a4
-     else
Karsten Hopp ae61a4
-     {
Karsten Hopp ae61a4
- 	PyObject	*r;
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
! 	r = PyRun_String(PyBytes_AsString(cmdbytes), Py_eval_input,
Karsten Hopp ae61a4
! 			 globals, globals);
Karsten Hopp ae61a4
! 	if (r == NULL)
Karsten Hopp ae61a4
! 	{
Karsten Hopp ae61a4
! 	    if (PyErr_Occurred() && !msg_silent)
Karsten Hopp ae61a4
! 		PyErr_PrintEx(0);
Karsten Hopp ae61a4
! 	    EMSG(_("E860: Eval did not return a valid python 3 object"));
Karsten Hopp ae61a4
! 	}
Karsten Hopp ae61a4
! 	else
Karsten Hopp ae61a4
! 	{
Karsten Hopp ae61a4
! 	    if (ConvertFromPyObject(r, rettv) == -1)
Karsten Hopp ae61a4
! 		EMSG(_("E861: Failed to convert returned python 3 object to vim value"));
Karsten Hopp ae61a4
! 	    Py_DECREF(r);
Karsten Hopp ae61a4
! 	}
Karsten Hopp ae61a4
! 	PyErr_Clear();
Karsten Hopp ae61a4
!     }
Karsten Hopp ae61a4
      Py_XDECREF(cmdbytes);
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      PyGILState_Release(pygilstate);
Karsten Hopp ae61a4
--- 871,878 ----
Karsten Hopp ae61a4
  					(char *)ENC_OPT, CODEC_ERROR_HANDLER);
Karsten Hopp ae61a4
      cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", CODEC_ERROR_HANDLER);
Karsten Hopp ae61a4
      Py_XDECREF(cmdstr);
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
!     run(PyBytes_AsString(cmdbytes), arg, &pygilstate);
Karsten Hopp ae61a4
      Py_XDECREF(cmdbytes);
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      PyGILState_Release(pygilstate);
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 936,945 ****
Karsten Hopp ae61a4
      script = script_get(eap, eap->arg);
Karsten Hopp ae61a4
      if (!eap->skip)
Karsten Hopp ae61a4
      {
Karsten Hopp ae61a4
! 	if (script == NULL)
Karsten Hopp ae61a4
! 	    DoPy3Command(eap, (char *)eap->arg, NULL);
Karsten Hopp ae61a4
! 	else
Karsten Hopp ae61a4
! 	    DoPy3Command(eap, (char *)script, NULL);
Karsten Hopp ae61a4
      }
Karsten Hopp ae61a4
      vim_free(script);
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
--- 906,915 ----
Karsten Hopp ae61a4
      script = script_get(eap, eap->arg);
Karsten Hopp ae61a4
      if (!eap->skip)
Karsten Hopp ae61a4
      {
Karsten Hopp ae61a4
! 	DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
Karsten Hopp ae61a4
! 		(rangeinitializer) init_range_cmd,
Karsten Hopp ae61a4
! 		(runner) run_cmd,
Karsten Hopp ae61a4
! 		(void *) eap);
Karsten Hopp ae61a4
      }
Karsten Hopp ae61a4
      vim_free(script);
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 1000,1100 ****
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      /* Execute the file */
Karsten Hopp ae61a4
!     DoPy3Command(eap, buffer, NULL);
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      void
Karsten Hopp ae61a4
  ex_py3do(exarg_T *eap)
Karsten Hopp ae61a4
  {
Karsten Hopp ae61a4
!     linenr_T		i;
Karsten Hopp ae61a4
!     const char		*code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
Karsten Hopp ae61a4
!     const char		*s = (const char *) eap->arg;
Karsten Hopp ae61a4
!     size_t		len;
Karsten Hopp ae61a4
!     char		*code;
Karsten Hopp ae61a4
!     int			status;
Karsten Hopp ae61a4
!     PyObject		*pyfunc, *pymain;
Karsten Hopp ae61a4
!     PyGILState_STATE	pygilstate;
Karsten Hopp ae61a4
! 
Karsten Hopp ae61a4
!     if (Python3_Init())
Karsten Hopp ae61a4
! 	goto theend;
Karsten Hopp ae61a4
! 
Karsten Hopp ae61a4
!     if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
Karsten Hopp ae61a4
!     {
Karsten Hopp ae61a4
! 	EMSG(_("cannot save undo information"));
Karsten Hopp ae61a4
! 	return;
Karsten Hopp ae61a4
!     }
Karsten Hopp ae61a4
!     len = strlen(code_hdr) + strlen(s);
Karsten Hopp ae61a4
!     code = malloc(len + 1);
Karsten Hopp ae61a4
!     STRCPY(code, code_hdr);
Karsten Hopp ae61a4
!     STRNCAT(code, s, len + 1);
Karsten Hopp ae61a4
!     pygilstate = PyGILState_Ensure();
Karsten Hopp ae61a4
!     status = PyRun_SimpleString(code);
Karsten Hopp ae61a4
!     vim_free(code);
Karsten Hopp ae61a4
!     if (status)
Karsten Hopp ae61a4
!     {
Karsten Hopp ae61a4
! 	EMSG(_("failed to run the code"));
Karsten Hopp ae61a4
! 	return;
Karsten Hopp ae61a4
!     }
Karsten Hopp ae61a4
!     status = 0; /* good */
Karsten Hopp ae61a4
!     pymain = PyImport_AddModule("__main__");
Karsten Hopp ae61a4
!     pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Karsten Hopp ae61a4
!     PyGILState_Release(pygilstate);
Karsten Hopp ae61a4
! 
Karsten Hopp ae61a4
!     for (i = eap->line1; i <= eap->line2; i++)
Karsten Hopp ae61a4
!     {
Karsten Hopp ae61a4
! 	const char *line;
Karsten Hopp ae61a4
! 	PyObject *pyline, *pylinenr, *pyret, *pybytes;
Karsten Hopp ae61a4
! 
Karsten Hopp ae61a4
! 	line = (char *)ml_get(i);
Karsten Hopp ae61a4
! 	pygilstate = PyGILState_Ensure();
Karsten Hopp ae61a4
! 	pyline = PyUnicode_Decode(line, strlen(line),
Karsten Hopp ae61a4
! 		(char *)ENC_OPT, CODEC_ERROR_HANDLER);
Karsten Hopp ae61a4
! 	pylinenr = PyLong_FromLong(i);
Karsten Hopp ae61a4
! 	pyret = PyObject_CallFunctionObjArgs(pyfunc, pyline, pylinenr, NULL);
Karsten Hopp ae61a4
! 	Py_DECREF(pyline);
Karsten Hopp ae61a4
! 	Py_DECREF(pylinenr);
Karsten Hopp ae61a4
! 	if (!pyret)
Karsten Hopp ae61a4
! 	{
Karsten Hopp ae61a4
! 	    PyErr_PrintEx(0);
Karsten Hopp ae61a4
! 	    PythonIO_Flush();
Karsten Hopp ae61a4
! 	    status = 1;
Karsten Hopp ae61a4
! 	    goto out;
Karsten Hopp ae61a4
! 	}
Karsten Hopp ae61a4
! 
Karsten Hopp ae61a4
! 	if (pyret && pyret != Py_None)
Karsten Hopp ae61a4
! 	{
Karsten Hopp ae61a4
! 	    if (!PyUnicode_Check(pyret))
Karsten Hopp ae61a4
! 	    {
Karsten Hopp ae61a4
! 		EMSG(_("E863: return value must be an instance of str"));
Karsten Hopp ae61a4
! 		Py_XDECREF(pyret);
Karsten Hopp ae61a4
! 		status = 1;
Karsten Hopp ae61a4
! 		goto out;
Karsten Hopp ae61a4
! 	    }
Karsten Hopp ae61a4
! 	    pybytes = PyUnicode_AsEncodedString(pyret,
Karsten Hopp ae61a4
! 		    (char *)ENC_OPT, CODEC_ERROR_HANDLER);
Karsten Hopp ae61a4
! 	    ml_replace(i, (char_u *) PyBytes_AsString(pybytes), 1);
Karsten Hopp ae61a4
! 	    Py_DECREF(pybytes);
Karsten Hopp ae61a4
! 	    changed();
Karsten Hopp ae61a4
! #ifdef SYNTAX_HL
Karsten Hopp ae61a4
! 	    syn_changed(i); /* recompute syntax hl. for this line */
Karsten Hopp ae61a4
! #endif
Karsten Hopp ae61a4
! 	}
Karsten Hopp ae61a4
! 	Py_XDECREF(pyret);
Karsten Hopp ae61a4
! 	PythonIO_Flush();
Karsten Hopp ae61a4
! 	PyGILState_Release(pygilstate);
Karsten Hopp ae61a4
!     }
Karsten Hopp ae61a4
!     pygilstate = PyGILState_Ensure();
Karsten Hopp ae61a4
! out:
Karsten Hopp ae61a4
!     Py_DECREF(pyfunc);
Karsten Hopp ae61a4
!     PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
Karsten Hopp ae61a4
!     PyGILState_Release(pygilstate);
Karsten Hopp ae61a4
!     if (status)
Karsten Hopp ae61a4
! 	return;
Karsten Hopp ae61a4
!     check_cursor();
Karsten Hopp ae61a4
!     update_curbuf(NOT_VALID);
Karsten Hopp ae61a4
! 
Karsten Hopp ae61a4
! theend:
Karsten Hopp ae61a4
!     return;
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
  /******************************************************
Karsten Hopp ae61a4
--- 970,988 ----
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      /* Execute the file */
Karsten Hopp ae61a4
!     DoPyCommand(buffer,
Karsten Hopp ae61a4
! 	    (rangeinitializer) init_range_cmd,
Karsten Hopp ae61a4
! 	    (runner) run_cmd,
Karsten Hopp ae61a4
! 	    (void *) eap);
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      void
Karsten Hopp ae61a4
  ex_py3do(exarg_T *eap)
Karsten Hopp ae61a4
  {
Karsten Hopp ae61a4
!     DoPyCommand((char *)eap->arg,
Karsten Hopp ae61a4
! 	    (rangeinitializer)init_range_cmd,
Karsten Hopp ae61a4
! 	    (runner)run_do,
Karsten Hopp ae61a4
! 	    (void *)eap);
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
  /******************************************************
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 1790,1796 ****
Karsten Hopp ae61a4
      void
Karsten Hopp ae61a4
  do_py3eval (char_u *str, typval_T *rettv)
Karsten Hopp ae61a4
  {
Karsten Hopp ae61a4
!     DoPy3Command(NULL, (char *) str, rettv);
Karsten Hopp ae61a4
      switch(rettv->v_type)
Karsten Hopp ae61a4
      {
Karsten Hopp ae61a4
  	case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
Karsten Hopp ae61a4
--- 1678,1687 ----
Karsten Hopp ae61a4
      void
Karsten Hopp ae61a4
  do_py3eval (char_u *str, typval_T *rettv)
Karsten Hopp ae61a4
  {
Karsten Hopp ae61a4
!     DoPyCommand((char *) str,
Karsten Hopp ae61a4
! 	    (rangeinitializer) init_range_eval,
Karsten Hopp ae61a4
! 	    (runner) run_eval,
Karsten Hopp ae61a4
! 	    (void *) rettv);
Karsten Hopp ae61a4
      switch(rettv->v_type)
Karsten Hopp ae61a4
      {
Karsten Hopp ae61a4
  	case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
Karsten Hopp ae61a4
*** ../vim-7.3.990/src/if_python.c	2013-05-17 16:39:59.000000000 +0200
Karsten Hopp ae61a4
--- src/if_python.c	2013-05-21 18:07:11.000000000 +0200
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 659,666 ****
Karsten Hopp ae61a4
   * Internal function prototypes.
Karsten Hopp ae61a4
   */
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
- static PyObject *globals;
Karsten Hopp ae61a4
- 
Karsten Hopp ae61a4
  static void PythonIO_Flush(void);
Karsten Hopp ae61a4
  static int PythonIO_Init(void);
Karsten Hopp ae61a4
  static int PythonMod_Init(void);
Karsten Hopp ae61a4
--- 659,664 ----
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 828,834 ****
Karsten Hopp ae61a4
   * External interface
Karsten Hopp ae61a4
   */
Karsten Hopp ae61a4
      static void
Karsten Hopp ae61a4
! DoPythonCommand(exarg_T *eap, const char *cmd, typval_T *rettv)
Karsten Hopp ae61a4
  {
Karsten Hopp ae61a4
  #ifndef PY_CAN_RECURSE
Karsten Hopp ae61a4
      static int		recursive = 0;
Karsten Hopp ae61a4
--- 826,832 ----
Karsten Hopp ae61a4
   * External interface
Karsten Hopp ae61a4
   */
Karsten Hopp ae61a4
      static void
Karsten Hopp ae61a4
! DoPyCommand(const char *cmd, rangeinitializer init_range, runner run, void *arg)
Karsten Hopp ae61a4
  {
Karsten Hopp ae61a4
  #ifndef PY_CAN_RECURSE
Karsten Hopp ae61a4
      static int		recursive = 0;
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 861,876 ****
Karsten Hopp ae61a4
      if (Python_Init())
Karsten Hopp ae61a4
  	goto theend;
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
!     if (rettv == NULL)
Karsten Hopp ae61a4
!     {
Karsten Hopp ae61a4
! 	RangeStart = eap->line1;
Karsten Hopp ae61a4
! 	RangeEnd = eap->line2;
Karsten Hopp ae61a4
!     }
Karsten Hopp ae61a4
!     else
Karsten Hopp ae61a4
!     {
Karsten Hopp ae61a4
! 	RangeStart = (PyInt) curwin->w_cursor.lnum;
Karsten Hopp ae61a4
! 	RangeEnd = RangeStart;
Karsten Hopp ae61a4
!     }
Karsten Hopp ae61a4
      Python_Release_Vim();	    /* leave vim */
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
  #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Karsten Hopp ae61a4
--- 859,866 ----
Karsten Hopp ae61a4
      if (Python_Init())
Karsten Hopp ae61a4
  	goto theend;
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
!     init_range(arg);
Karsten Hopp ae61a4
! 
Karsten Hopp ae61a4
      Python_Release_Vim();	    /* leave vim */
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
  #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 892,918 ****
Karsten Hopp ae61a4
      Python_RestoreThread();	    /* enter python */
Karsten Hopp ae61a4
  #endif
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
!     if (rettv == NULL)
Karsten Hopp ae61a4
! 	PyRun_SimpleString((char *)(cmd));
Karsten Hopp ae61a4
!     else
Karsten Hopp ae61a4
!     {
Karsten Hopp ae61a4
! 	PyObject	*r;
Karsten Hopp ae61a4
! 
Karsten Hopp ae61a4
! 	r = PyRun_String((char *)(cmd), Py_eval_input, globals, globals);
Karsten Hopp ae61a4
! 	if (r == NULL)
Karsten Hopp ae61a4
! 	{
Karsten Hopp ae61a4
! 	    if (PyErr_Occurred() && !msg_silent)
Karsten Hopp ae61a4
! 		PyErr_PrintEx(0);
Karsten Hopp ae61a4
! 	    EMSG(_("E858: Eval did not return a valid python object"));
Karsten Hopp ae61a4
! 	}
Karsten Hopp ae61a4
! 	else
Karsten Hopp ae61a4
! 	{
Karsten Hopp ae61a4
! 	    if (ConvertFromPyObject(r, rettv) == -1)
Karsten Hopp ae61a4
! 		EMSG(_("E859: Failed to convert returned python object to vim value"));
Karsten Hopp ae61a4
! 	    Py_DECREF(r);
Karsten Hopp ae61a4
! 	}
Karsten Hopp ae61a4
! 	PyErr_Clear();
Karsten Hopp ae61a4
!     }
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
  #ifdef PY_CAN_RECURSE
Karsten Hopp ae61a4
      PyGILState_Release(pygilstate);
Karsten Hopp ae61a4
--- 882,888 ----
Karsten Hopp ae61a4
      Python_RestoreThread();	    /* enter python */
Karsten Hopp ae61a4
  #endif
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
!     run((char *) cmd, arg, &pygilstate);
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
  #ifdef PY_CAN_RECURSE
Karsten Hopp ae61a4
      PyGILState_Release(pygilstate);
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 952,961 ****
Karsten Hopp ae61a4
      script = script_get(eap, eap->arg);
Karsten Hopp ae61a4
      if (!eap->skip)
Karsten Hopp ae61a4
      {
Karsten Hopp ae61a4
! 	if (script == NULL)
Karsten Hopp ae61a4
! 	    DoPythonCommand(eap, (char *)eap->arg, NULL);
Karsten Hopp ae61a4
! 	else
Karsten Hopp ae61a4
! 	    DoPythonCommand(eap, (char *)script, NULL);
Karsten Hopp ae61a4
      }
Karsten Hopp ae61a4
      vim_free(script);
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
--- 922,931 ----
Karsten Hopp ae61a4
      script = script_get(eap, eap->arg);
Karsten Hopp ae61a4
      if (!eap->skip)
Karsten Hopp ae61a4
      {
Karsten Hopp ae61a4
! 	DoPyCommand(script == NULL ? (char *) eap->arg : (char *) script,
Karsten Hopp ae61a4
! 		(rangeinitializer) init_range_cmd,
Karsten Hopp ae61a4
! 		(runner) run_cmd,
Karsten Hopp ae61a4
! 		(void *) eap);
Karsten Hopp ae61a4
      }
Karsten Hopp ae61a4
      vim_free(script);
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 1001,1094 ****
Karsten Hopp ae61a4
      *p++ = '\0';
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      /* Execute the file */
Karsten Hopp ae61a4
!     DoPythonCommand(eap, buffer, NULL);
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      void
Karsten Hopp ae61a4
  ex_pydo(exarg_T *eap)
Karsten Hopp ae61a4
  {
Karsten Hopp ae61a4
!     linenr_T		i;
Karsten Hopp ae61a4
!     const char		*code_hdr = "def " DOPY_FUNC "(line, linenr):\n ";
Karsten Hopp ae61a4
!     const char		*s = (const char *) eap->arg;
Karsten Hopp ae61a4
!     size_t		len;
Karsten Hopp ae61a4
!     char		*code;
Karsten Hopp ae61a4
!     int			status;
Karsten Hopp ae61a4
!     PyObject		*pyfunc, *pymain;
Karsten Hopp ae61a4
!     PyGILState_STATE	pygilstate;
Karsten Hopp ae61a4
! 
Karsten Hopp ae61a4
!     if (Python_Init())
Karsten Hopp ae61a4
!         return;
Karsten Hopp ae61a4
! 
Karsten Hopp ae61a4
!     if (u_save(eap->line1 - 1, eap->line2 + 1) != OK)
Karsten Hopp ae61a4
!     {
Karsten Hopp ae61a4
! 	EMSG(_("cannot save undo information"));
Karsten Hopp ae61a4
! 	return;
Karsten Hopp ae61a4
!     }
Karsten Hopp ae61a4
!     len = strlen(code_hdr) + strlen(s);
Karsten Hopp ae61a4
!     code = malloc(len + 1);
Karsten Hopp ae61a4
!     STRCPY(code, code_hdr);
Karsten Hopp ae61a4
!     STRNCAT(code, s, len + 1);
Karsten Hopp ae61a4
!     pygilstate = PyGILState_Ensure();
Karsten Hopp ae61a4
!     status = PyRun_SimpleString(code);
Karsten Hopp ae61a4
!     vim_free(code);
Karsten Hopp ae61a4
!     if (status)
Karsten Hopp ae61a4
!     {
Karsten Hopp ae61a4
! 	EMSG(_("failed to run the code"));
Karsten Hopp ae61a4
! 	return;
Karsten Hopp ae61a4
!     }
Karsten Hopp ae61a4
!     status = 0; /* good */
Karsten Hopp ae61a4
!     pymain = PyImport_AddModule("__main__");
Karsten Hopp ae61a4
!     pyfunc = PyObject_GetAttrString(pymain, DOPY_FUNC);
Karsten Hopp ae61a4
!     PyGILState_Release(pygilstate);
Karsten Hopp ae61a4
! 
Karsten Hopp ae61a4
!     for (i = eap->line1; i <= eap->line2; i++)
Karsten Hopp ae61a4
!     {
Karsten Hopp ae61a4
! 	const char *line;
Karsten Hopp ae61a4
! 	PyObject *pyline, *pylinenr, *pyret;
Karsten Hopp ae61a4
! 
Karsten Hopp ae61a4
! 	line = (char *)ml_get(i);
Karsten Hopp ae61a4
! 	pygilstate = PyGILState_Ensure();
Karsten Hopp ae61a4
! 	pyline = PyString_FromStringAndSize(line, strlen(line));
Karsten Hopp ae61a4
! 	pylinenr = PyLong_FromLong(i);
Karsten Hopp ae61a4
! 	pyret = PyObject_CallFunctionObjArgs(pyfunc, pyline, pylinenr, NULL);
Karsten Hopp ae61a4
! 	Py_DECREF(pyline);
Karsten Hopp ae61a4
! 	Py_DECREF(pylinenr);
Karsten Hopp ae61a4
! 	if (!pyret)
Karsten Hopp ae61a4
! 	{
Karsten Hopp ae61a4
! 	    PyErr_PrintEx(0);
Karsten Hopp ae61a4
! 	    PythonIO_Flush();
Karsten Hopp ae61a4
! 	    status = 1;
Karsten Hopp ae61a4
! 	    goto out;
Karsten Hopp ae61a4
! 	}
Karsten Hopp ae61a4
! 
Karsten Hopp ae61a4
! 	if (pyret && pyret != Py_None)
Karsten Hopp ae61a4
! 	{
Karsten Hopp ae61a4
! 	    if (!PyString_Check(pyret))
Karsten Hopp ae61a4
! 	    {
Karsten Hopp ae61a4
! 		EMSG(_("E863: return value must be an instance of str"));
Karsten Hopp ae61a4
! 		Py_XDECREF(pyret);
Karsten Hopp ae61a4
! 		status = 1;
Karsten Hopp ae61a4
! 		goto out;
Karsten Hopp ae61a4
! 	    }
Karsten Hopp ae61a4
! 	    ml_replace(i, (char_u *) PyString_AsString(pyret), 1);
Karsten Hopp ae61a4
! 	    changed();
Karsten Hopp ae61a4
! #ifdef SYNTAX_HL
Karsten Hopp ae61a4
! 	    syn_changed(i); /* recompute syntax hl. for this line */
Karsten Hopp ae61a4
! #endif
Karsten Hopp ae61a4
! 	}
Karsten Hopp ae61a4
! 	Py_XDECREF(pyret);
Karsten Hopp ae61a4
! 	PythonIO_Flush();
Karsten Hopp ae61a4
! 	PyGILState_Release(pygilstate);
Karsten Hopp ae61a4
!     }
Karsten Hopp ae61a4
!     pygilstate = PyGILState_Ensure();
Karsten Hopp ae61a4
! out:
Karsten Hopp ae61a4
!     Py_DECREF(pyfunc);
Karsten Hopp ae61a4
!     PyObject_SetAttrString(pymain, DOPY_FUNC, NULL);
Karsten Hopp ae61a4
!     PyGILState_Release(pygilstate);
Karsten Hopp ae61a4
!     if (status)
Karsten Hopp ae61a4
! 	return;
Karsten Hopp ae61a4
!     check_cursor();
Karsten Hopp ae61a4
!     update_curbuf(NOT_VALID);
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
  /******************************************************
Karsten Hopp ae61a4
--- 971,989 ----
Karsten Hopp ae61a4
      *p++ = '\0';
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      /* Execute the file */
Karsten Hopp ae61a4
!     DoPyCommand(buffer,
Karsten Hopp ae61a4
! 	    (rangeinitializer) init_range_cmd,
Karsten Hopp ae61a4
! 	    (runner) run_cmd,
Karsten Hopp ae61a4
! 	    (void *) eap);
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
      void
Karsten Hopp ae61a4
  ex_pydo(exarg_T *eap)
Karsten Hopp ae61a4
  {
Karsten Hopp ae61a4
!     DoPyCommand((char *)eap->arg,
Karsten Hopp ae61a4
! 	    (rangeinitializer) init_range_cmd,
Karsten Hopp ae61a4
! 	    (runner)run_do,
Karsten Hopp ae61a4
! 	    (void *)eap);
Karsten Hopp ae61a4
  }
Karsten Hopp ae61a4
  
Karsten Hopp ae61a4
  /******************************************************
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 1525,1531 ****
Karsten Hopp ae61a4
      void
Karsten Hopp ae61a4
  do_pyeval (char_u *str, typval_T *rettv)
Karsten Hopp ae61a4
  {
Karsten Hopp ae61a4
!     DoPythonCommand(NULL, (char *) str, rettv);
Karsten Hopp ae61a4
      switch(rettv->v_type)
Karsten Hopp ae61a4
      {
Karsten Hopp ae61a4
  	case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
Karsten Hopp ae61a4
--- 1420,1429 ----
Karsten Hopp ae61a4
      void
Karsten Hopp ae61a4
  do_pyeval (char_u *str, typval_T *rettv)
Karsten Hopp ae61a4
  {
Karsten Hopp ae61a4
!     DoPyCommand((char *) str,
Karsten Hopp ae61a4
! 	    (rangeinitializer) init_range_eval,
Karsten Hopp ae61a4
! 	    (runner) run_eval,
Karsten Hopp ae61a4
! 	    (void *) rettv);
Karsten Hopp ae61a4
      switch(rettv->v_type)
Karsten Hopp ae61a4
      {
Karsten Hopp ae61a4
  	case VAR_DICT: ++rettv->vval.v_dict->dv_refcount; break;
Karsten Hopp ae61a4
*** ../vim-7.3.990/src/testdir/test87.ok	2013-05-17 16:18:27.000000000 +0200
Karsten Hopp ae61a4
--- src/testdir/test87.ok	2013-05-21 17:59:56.000000000 +0200
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 59,68 ****
Karsten Hopp ae61a4
  ['c', 1]
Karsten Hopp ae61a4
  ['d', ['e']]
Karsten Hopp ae61a4
  0.0
Karsten Hopp ae61a4
! "\0":	Vim(let):E861:
Karsten Hopp ae61a4
! {"\0": 1}:	Vim(let):E861:
Karsten Hopp ae61a4
  undefined_name:	Vim(let):Trace
Karsten Hopp ae61a4
! vim:	Vim(let):E861:
Karsten Hopp ae61a4
  [1]
Karsten Hopp ae61a4
  [1, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 1]
Karsten Hopp ae61a4
  Abc
Karsten Hopp ae61a4
--- 59,68 ----
Karsten Hopp ae61a4
  ['c', 1]
Karsten Hopp ae61a4
  ['d', ['e']]
Karsten Hopp ae61a4
  0.0
Karsten Hopp ae61a4
! "\0":	Vim(let):E859:
Karsten Hopp ae61a4
! {"\0": 1}:	Vim(let):E859:
Karsten Hopp ae61a4
  undefined_name:	Vim(let):Trace
Karsten Hopp ae61a4
! vim:	Vim(let):E859:
Karsten Hopp ae61a4
  [1]
Karsten Hopp ae61a4
  [1, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 1]
Karsten Hopp ae61a4
  Abc
Karsten Hopp ae61a4
*** ../vim-7.3.990/src/version.c	2013-05-21 16:28:05.000000000 +0200
Karsten Hopp ae61a4
--- src/version.c	2013-05-21 18:19:20.000000000 +0200
Karsten Hopp ae61a4
***************
Karsten Hopp ae61a4
*** 730,731 ****
Karsten Hopp ae61a4
--- 730,733 ----
Karsten Hopp ae61a4
  {   /* Add new patch number below this line */
Karsten Hopp ae61a4
+ /**/
Karsten Hopp ae61a4
+     991,
Karsten Hopp ae61a4
  /**/
Karsten Hopp ae61a4
Karsten Hopp ae61a4
-- 
Karsten Hopp ae61a4
Mynd you, m00se bites Kan be pretty nasti ...
Karsten Hopp ae61a4
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
Karsten Hopp ae61a4
Karsten Hopp ae61a4
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
Karsten Hopp ae61a4
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
Karsten Hopp ae61a4
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
Karsten Hopp ae61a4
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///