5217895
To: vim_dev@googlegroups.com
5217895
Subject: Patch 7.3.220
5217895
Fcc: outbox
5217895
From: Bram Moolenaar <Bram@moolenaar.net>
5217895
Mime-Version: 1.0
5217895
Content-Type: text/plain; charset=UTF-8
5217895
Content-Transfer-Encoding: 8bit
5217895
------------
5217895
5217895
Patch 7.3.220
5217895
Problem:    Python 3: vim.error is a 'str' instead of an 'Exception' object,
5217895
            so 'except' or 'raise' it causes a 'SystemError' exception.
5217895
            Buffer objects do not support slice assignment.
5217895
            When exchanging text between Vim and Python, multibyte texts become
5217895
            gabage or cause Unicode Expceptions, etc.
5217895
            'py3file' tries to read in the file as Unicode, sometimes causes
5217895
            UnicodeDecodeException
5217895
Solution:   Fix the problems. (lilydjwg)
5217895
Files:      src/if_py_both.h, src/if_python.c, src/if_python3.c
5217895
    
5217895
5217895
*** ../mercurial/vim73/src/if_py_both.h	2011-03-22 15:47:18.000000000 +0100
5217895
--- src/if_py_both.h	2011-06-18 23:54:25.000000000 +0200
5217895
***************
5217895
*** 65,74 ****
5217895
  OutputWrite(PyObject *self, PyObject *args)
5217895
  {
5217895
      int len;
5217895
!     char *str;
5217895
      int error = ((OutputObject *)(self))->error;
5217895
  
5217895
!     if (!PyArg_ParseTuple(args, "s#", &str, &len))
5217895
  	return NULL;
5217895
  
5217895
      Py_BEGIN_ALLOW_THREADS
5217895
--- 65,74 ----
5217895
  OutputWrite(PyObject *self, PyObject *args)
5217895
  {
5217895
      int len;
5217895
!     char *str = NULL;
5217895
      int error = ((OutputObject *)(self))->error;
5217895
  
5217895
!     if (!PyArg_ParseTuple(args, "es#", p_enc, &str, &len))
5217895
  	return NULL;
5217895
  
5217895
      Py_BEGIN_ALLOW_THREADS
5217895
***************
5217895
*** 76,81 ****
5217895
--- 76,82 ----
5217895
      writer((writefn)(error ? emsg : msg), (char_u *)str, len);
5217895
      Python_Release_Vim();
5217895
      Py_END_ALLOW_THREADS
5217895
+     PyMem_Free(str);
5217895
  
5217895
      Py_INCREF(Py_None);
5217895
      return Py_None;
5217895
***************
5217895
*** 104,113 ****
5217895
      for (i = 0; i < n; ++i)
5217895
      {
5217895
  	PyObject *line = PyList_GetItem(list, i);
5217895
! 	char *str;
5217895
  	PyInt len;
5217895
  
5217895
! 	if (!PyArg_Parse(line, "s#", &str, &len)) {
5217895
  	    PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
5217895
  	    Py_DECREF(list);
5217895
  	    return NULL;
5217895
--- 105,114 ----
5217895
      for (i = 0; i < n; ++i)
5217895
      {
5217895
  	PyObject *line = PyList_GetItem(list, i);
5217895
! 	char *str = NULL;
5217895
  	PyInt len;
5217895
  
5217895
! 	if (!PyArg_Parse(line, "es#", p_enc, &str, &len)) {
5217895
  	    PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
5217895
  	    Py_DECREF(list);
5217895
  	    return NULL;
5217895
***************
5217895
*** 118,123 ****
5217895
--- 119,125 ----
5217895
  	writer((writefn)(error ? emsg : msg), (char_u *)str, len);
5217895
  	Python_Release_Vim();
5217895
  	Py_END_ALLOW_THREADS
5217895
+ 	PyMem_Free(str);
5217895
      }
5217895
  
5217895
      Py_DECREF(list);
5217895
***************
5217895
*** 681,686 ****
5217895
--- 683,689 ----
5217895
  {
5217895
      const char *str;
5217895
      char *save;
5217895
+     PyObject *bytes;
5217895
      PyInt len;
5217895
      PyInt i;
5217895
      char *p;
5217895
***************
5217895
*** 691,698 ****
5217895
  	return NULL;
5217895
      }
5217895
  
5217895
!     str = PyString_AsString(obj);
5217895
!     len = PyString_Size(obj);
5217895
  
5217895
      /*
5217895
       * Error checking: String must not contain newlines, as we
5217895
--- 694,702 ----
5217895
  	return NULL;
5217895
      }
5217895
  
5217895
!     bytes = PyString_AsBytes(obj);  /* for Python 2 this does nothing */
5217895
!     str = PyString_AsString(bytes);
5217895
!     len = PyString_Size(bytes);
5217895
  
5217895
      /*
5217895
       * Error checking: String must not contain newlines, as we
5217895
***************
5217895
*** 731,736 ****
5217895
--- 735,741 ----
5217895
      }
5217895
  
5217895
      save[i] = '\0';
5217895
+     PyString_FreeBytes(bytes);  /* Python 2 does nothing here */
5217895
  
5217895
      return save;
5217895
  }
5217895
***************
5217895
*** 817,823 ****
5217895
      invalidate_botline();
5217895
  }
5217895
  
5217895
! /* Replace a line in the specified buffer. The line number is
5217895
   * in Vim format (1-based). The replacement line is given as
5217895
   * a Python string object. The object is checked for validity
5217895
   * and correct format. Errors are returned as a value of FAIL.
5217895
--- 822,829 ----
5217895
      invalidate_botline();
5217895
  }
5217895
  
5217895
! /*
5217895
!  * Replace a line in the specified buffer. The line number is
5217895
   * in Vim format (1-based). The replacement line is given as
5217895
   * a Python string object. The object is checked for validity
5217895
   * and correct format. Errors are returned as a value of FAIL.
5217895
***************
5217895
*** 908,913 ****
5217895
--- 914,1106 ----
5217895
      }
5217895
  }
5217895
  
5217895
+ /* Replace a range of lines in the specified buffer. The line numbers are in
5217895
+  * Vim format (1-based). The range is from lo up to, but not including, hi.
5217895
+  * The replacement lines are given as a Python list of string objects. The
5217895
+  * list is checked for validity and correct format. Errors are returned as a
5217895
+  * value of FAIL.  The return value is OK on success.
5217895
+  * If OK is returned and len_change is not NULL, *len_change
5217895
+  * is set to the change in the buffer length.
5217895
+  */
5217895
+     static int
5217895
+ SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
5217895
+ {
5217895
+     /* First of all, we check the thpe of the supplied Python object.
5217895
+      * There are three cases:
5217895
+      *	  1. NULL, or None - this is a deletion.
5217895
+      *	  2. A list	   - this is a replacement.
5217895
+      *	  3. Anything else - this is an error.
5217895
+      */
5217895
+     if (list == Py_None || list == NULL)
5217895
+     {
5217895
+ 	PyInt	i;
5217895
+ 	PyInt	n = (int)(hi - lo);
5217895
+ 	buf_T	*savebuf = curbuf;
5217895
+ 
5217895
+ 	PyErr_Clear();
5217895
+ 	curbuf = buf;
5217895
+ 
5217895
+ 	if (u_savedel((linenr_T)lo, (long)n) == FAIL)
5217895
+ 	    PyErr_SetVim(_("cannot save undo information"));
5217895
+ 	else
5217895
+ 	{
5217895
+ 	    for (i = 0; i < n; ++i)
5217895
+ 	    {
5217895
+ 		if (ml_delete((linenr_T)lo, FALSE) == FAIL)
5217895
+ 		{
5217895
+ 		    PyErr_SetVim(_("cannot delete line"));
5217895
+ 		    break;
5217895
+ 		}
5217895
+ 	    }
5217895
+ 	    if (buf == curwin->w_buffer)
5217895
+ 		py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
5217895
+ 	    deleted_lines_mark((linenr_T)lo, (long)i);
5217895
+ 	}
5217895
+ 
5217895
+ 	curbuf = savebuf;
5217895
+ 
5217895
+ 	if (PyErr_Occurred() || VimErrorCheck())
5217895
+ 	    return FAIL;
5217895
+ 
5217895
+ 	if (len_change)
5217895
+ 	    *len_change = -n;
5217895
+ 
5217895
+ 	return OK;
5217895
+     }
5217895
+     else if (PyList_Check(list))
5217895
+     {
5217895
+ 	PyInt	i;
5217895
+ 	PyInt	new_len = PyList_Size(list);
5217895
+ 	PyInt	old_len = hi - lo;
5217895
+ 	PyInt	extra = 0;	/* lines added to text, can be negative */
5217895
+ 	char	**array;
5217895
+ 	buf_T	*savebuf;
5217895
+ 
5217895
+ 	if (new_len == 0)	/* avoid allocating zero bytes */
5217895
+ 	    array = NULL;
5217895
+ 	else
5217895
+ 	{
5217895
+ 	    array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
5217895
+ 	    if (array == NULL)
5217895
+ 	    {
5217895
+ 		PyErr_NoMemory();
5217895
+ 		return FAIL;
5217895
+ 	    }
5217895
+ 	}
5217895
+ 
5217895
+ 	for (i = 0; i < new_len; ++i)
5217895
+ 	{
5217895
+ 	    PyObject *line = PyList_GetItem(list, i);
5217895
+ 
5217895
+ 	    array[i] = StringToLine(line);
5217895
+ 	    if (array[i] == NULL)
5217895
+ 	    {
5217895
+ 		while (i)
5217895
+ 		    vim_free(array[--i]);
5217895
+ 		vim_free(array);
5217895
+ 		return FAIL;
5217895
+ 	    }
5217895
+ 	}
5217895
+ 
5217895
+ 	savebuf = curbuf;
5217895
+ 
5217895
+ 	PyErr_Clear();
5217895
+ 	curbuf = buf;
5217895
+ 
5217895
+ 	if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
5217895
+ 	    PyErr_SetVim(_("cannot save undo information"));
5217895
+ 
5217895
+ 	/* If the size of the range is reducing (ie, new_len < old_len) we
5217895
+ 	 * need to delete some old_len. We do this at the start, by
5217895
+ 	 * repeatedly deleting line "lo".
5217895
+ 	 */
5217895
+ 	if (!PyErr_Occurred())
5217895
+ 	{
5217895
+ 	    for (i = 0; i < old_len - new_len; ++i)
5217895
+ 		if (ml_delete((linenr_T)lo, FALSE) == FAIL)
5217895
+ 		{
5217895
+ 		    PyErr_SetVim(_("cannot delete line"));
5217895
+ 		    break;
5217895
+ 		}
5217895
+ 	    extra -= i;
5217895
+ 	}
5217895
+ 
5217895
+ 	/* For as long as possible, replace the existing old_len with the
5217895
+ 	 * new old_len. This is a more efficient operation, as it requires
5217895
+ 	 * less memory allocation and freeing.
5217895
+ 	 */
5217895
+ 	if (!PyErr_Occurred())
5217895
+ 	{
5217895
+ 	    for (i = 0; i < old_len && i < new_len; ++i)
5217895
+ 		if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
5217895
+ 								      == FAIL)
5217895
+ 		{
5217895
+ 		    PyErr_SetVim(_("cannot replace line"));
5217895
+ 		    break;
5217895
+ 		}
5217895
+ 	}
5217895
+ 	else
5217895
+ 	    i = 0;
5217895
+ 
5217895
+ 	/* Now we may need to insert the remaining new old_len. If we do, we
5217895
+ 	 * must free the strings as we finish with them (we can't pass the
5217895
+ 	 * responsibility to vim in this case).
5217895
+ 	 */
5217895
+ 	if (!PyErr_Occurred())
5217895
+ 	{
5217895
+ 	    while (i < new_len)
5217895
+ 	    {
5217895
+ 		if (ml_append((linenr_T)(lo + i - 1),
5217895
+ 					(char_u *)array[i], 0, FALSE) == FAIL)
5217895
+ 		{
5217895
+ 		    PyErr_SetVim(_("cannot insert line"));
5217895
+ 		    break;
5217895
+ 		}
5217895
+ 		vim_free(array[i]);
5217895
+ 		++i;
5217895
+ 		++extra;
5217895
+ 	    }
5217895
+ 	}
5217895
+ 
5217895
+ 	/* Free any left-over old_len, as a result of an error */
5217895
+ 	while (i < new_len)
5217895
+ 	{
5217895
+ 	    vim_free(array[i]);
5217895
+ 	    ++i;
5217895
+ 	}
5217895
+ 
5217895
+ 	/* Free the array of old_len. All of its contents have now
5217895
+ 	 * been dealt with (either freed, or the responsibility passed
5217895
+ 	 * to vim.
5217895
+ 	 */
5217895
+ 	vim_free(array);
5217895
+ 
5217895
+ 	/* Adjust marks. Invalidate any which lie in the
5217895
+ 	 * changed range, and move any in the remainder of the buffer.
5217895
+ 	 */
5217895
+ 	mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
5217895
+ 						  (long)MAXLNUM, (long)extra);
5217895
+ 	changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
5217895
+ 
5217895
+ 	if (buf == curwin->w_buffer)
5217895
+ 	    py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
5217895
+ 
5217895
+ 	curbuf = savebuf;
5217895
+ 
5217895
+ 	if (PyErr_Occurred() || VimErrorCheck())
5217895
+ 	    return FAIL;
5217895
+ 
5217895
+ 	if (len_change)
5217895
+ 	    *len_change = new_len - old_len;
5217895
+ 
5217895
+ 	return OK;
5217895
+     }
5217895
+     else
5217895
+     {
5217895
+ 	PyErr_BadArgument();
5217895
+ 	return FAIL;
5217895
+     }
5217895
+ }
5217895
  
5217895
  /* Insert a number of lines into the specified buffer after the specifed line.
5217895
   * The line number is in Vim format (1-based). The lines to be inserted are
5217895
***************
5217895
*** 1108,1113 ****
5217895
--- 1301,1340 ----
5217895
  	return -1;
5217895
  
5217895
      if (new_end)
5217895
+ 	*new_end = end + len_change;
5217895
+ 
5217895
+     return 0;
5217895
+ }
5217895
+ 
5217895
+     static PyInt
5217895
+ RBAsSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
5217895
+ {
5217895
+     PyInt size;
5217895
+     PyInt len_change;
5217895
+ 
5217895
+     /* Self must be a valid buffer */
5217895
+     if (CheckBuffer(self))
5217895
+ 	return -1;
5217895
+ 
5217895
+     /* Sort out the slice range */
5217895
+     size = end - start + 1;
5217895
+ 
5217895
+     if (lo < 0)
5217895
+ 	lo = 0;
5217895
+     else if (lo > size)
5217895
+ 	lo = size;
5217895
+     if (hi < 0)
5217895
+ 	hi = 0;
5217895
+     if (hi < lo)
5217895
+ 	hi = lo;
5217895
+     else if (hi > size)
5217895
+ 	hi = size;
5217895
+ 
5217895
+     if (SetBufferLineList(self->buf, lo + start, hi + start,
5217895
+ 						    val, &len_change) == FAIL)
5217895
+ 	return -1;
5217895
+ 
5217895
+     if (new_end)
5217895
  	*new_end = end + len_change;
5217895
  
5217895
      return 0;
5217895
*** ../mercurial/vim73/src/if_python.c	2011-03-26 18:32:00.000000000 +0100
5217895
--- src/if_python.c	2011-06-19 00:02:15.000000000 +0200
5217895
***************
5217895
*** 56,61 ****
5217895
--- 56,65 ----
5217895
  
5217895
  static void init_structs(void);
5217895
  
5217895
+ /* No-op conversion functions, use with care! */
5217895
+ #define PyString_AsBytes(obj) (obj)
5217895
+ #define PyString_FreeBytes(obj)
5217895
+ 
5217895
  #if !defined(FEAT_PYTHON) && defined(PROTO)
5217895
  /* Use this to be able to generate prototypes without python being used. */
5217895
  # define PyObject Py_ssize_t
5217895
***************
5217895
*** 129,134 ****
5217895
--- 133,139 ----
5217895
   */
5217895
  # define PyArg_Parse dll_PyArg_Parse
5217895
  # define PyArg_ParseTuple dll_PyArg_ParseTuple
5217895
+ # define PyMem_Free dll_PyMem_Free
5217895
  # define PyDict_SetItemString dll_PyDict_SetItemString
5217895
  # define PyErr_BadArgument dll_PyErr_BadArgument
5217895
  # define PyErr_Clear dll_PyErr_Clear
5217895
***************
5217895
*** 189,194 ****
5217895
--- 194,200 ----
5217895
   */
5217895
  static int(*dll_PyArg_Parse)(PyObject *, char *, ...);
5217895
  static int(*dll_PyArg_ParseTuple)(PyObject *, char *, ...);
5217895
+ static int(*dll_PyMem_Free)(void *);
5217895
  static int(*dll_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
5217895
  static int(*dll_PyErr_BadArgument)(void);
5217895
  static void(*dll_PyErr_Clear)(void);
5217895
***************
5217895
*** 271,276 ****
5217895
--- 277,283 ----
5217895
  {
5217895
      {"PyArg_Parse", (PYTHON_PROC*)&dll_PyArg_Parse},
5217895
      {"PyArg_ParseTuple", (PYTHON_PROC*)&dll_PyArg_ParseTuple},
5217895
+     {"PyMem_Free", (PYTHON_PROC*)&dll_PyMem_Free},
5217895
      {"PyDict_SetItemString", (PYTHON_PROC*)&dll_PyDict_SetItemString},
5217895
      {"PyErr_BadArgument", (PYTHON_PROC*)&dll_PyErr_BadArgument},
5217895
      {"PyErr_Clear", (PYTHON_PROC*)&dll_PyErr_Clear},
5217895
***************
5217895
*** 833,876 ****
5217895
  static PyObject *CurrentGetattr(PyObject *, char *);
5217895
  static int CurrentSetattr(PyObject *, char *, PyObject *);
5217895
  
5217895
- /* Common routines for buffers and line ranges
5217895
-  * -------------------------------------------
5217895
-  */
5217895
- 
5217895
-     static PyInt
5217895
- RBAssSlice(BufferObject *self, PyInt lo, PyInt hi, PyObject *val, PyInt start, PyInt end, PyInt *new_end)
5217895
- {
5217895
-     PyInt size;
5217895
-     PyInt len_change;
5217895
- 
5217895
-     /* Self must be a valid buffer */
5217895
-     if (CheckBuffer(self))
5217895
- 	return -1;
5217895
- 
5217895
-     /* Sort out the slice range */
5217895
-     size = end - start + 1;
5217895
- 
5217895
-     if (lo < 0)
5217895
- 	lo = 0;
5217895
-     else if (lo > size)
5217895
- 	lo = size;
5217895
-     if (hi < 0)
5217895
- 	hi = 0;
5217895
-     if (hi < lo)
5217895
- 	hi = lo;
5217895
-     else if (hi > size)
5217895
- 	hi = size;
5217895
- 
5217895
-     if (SetBufferLineList(self->buf, lo + start, hi + start,
5217895
- 						    val, &len_change) == FAIL)
5217895
- 	return -1;
5217895
- 
5217895
-     if (new_end)
5217895
- 	*new_end = end + len_change;
5217895
- 
5217895
-     return 0;
5217895
- }
5217895
- 
5217895
  static PySequenceMethods BufferAsSeq = {
5217895
      (PyInquiry)		BufferLength,	    /* sq_length,    len(x)   */
5217895
      (binaryfunc)	0, /* BufferConcat, */	     /* sq_concat,    x+y      */
5217895
--- 840,845 ----
5217895
***************
5217895
*** 1038,1044 ****
5217895
      static PyInt
5217895
  BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
5217895
  {
5217895
!     return RBAssSlice((BufferObject *)(self), lo, hi, val, 1,
5217895
  		      (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
5217895
  		      NULL);
5217895
  }
5217895
--- 1007,1013 ----
5217895
      static PyInt
5217895
  BufferAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
5217895
  {
5217895
!     return RBAsSlice((BufferObject *)(self), lo, hi, val, 1,
5217895
  		      (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
5217895
  		      NULL);
5217895
  }
5217895
***************
5217895
*** 1088,1094 ****
5217895
      static PyInt
5217895
  RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
5217895
  {
5217895
!     return RBAssSlice(((RangeObject *)(self))->buf, lo, hi, val,
5217895
  		      ((RangeObject *)(self))->start,
5217895
  		      ((RangeObject *)(self))->end,
5217895
  		      &((RangeObject *)(self))->end);
5217895
--- 1057,1063 ----
5217895
      static PyInt
5217895
  RangeAssSlice(PyObject *self, PyInt lo, PyInt hi, PyObject *val)
5217895
  {
5217895
!     return RBAsSlice(((RangeObject *)(self))->buf, lo, hi, val,
5217895
  		      ((RangeObject *)(self))->start,
5217895
  		      ((RangeObject *)(self))->end,
5217895
  		      &((RangeObject *)(self))->end);
5217895
***************
5217895
*** 1435,1628 ****
5217895
   * 4. Utility functions for handling the interface between Vim and Python.
5217895
   */
5217895
  
5217895
- /* Replace a range of lines in the specified buffer. The line numbers are in
5217895
-  * Vim format (1-based). The range is from lo up to, but not including, hi.
5217895
-  * The replacement lines are given as a Python list of string objects. The
5217895
-  * list is checked for validity and correct format. Errors are returned as a
5217895
-  * value of FAIL.  The return value is OK on success.
5217895
-  * If OK is returned and len_change is not NULL, *len_change
5217895
-  * is set to the change in the buffer length.
5217895
-  */
5217895
-     static int
5217895
- SetBufferLineList(buf_T *buf, PyInt lo, PyInt hi, PyObject *list, PyInt *len_change)
5217895
- {
5217895
-     /* First of all, we check the thpe of the supplied Python object.
5217895
-      * There are three cases:
5217895
-      *	  1. NULL, or None - this is a deletion.
5217895
-      *	  2. A list	   - this is a replacement.
5217895
-      *	  3. Anything else - this is an error.
5217895
-      */
5217895
-     if (list == Py_None || list == NULL)
5217895
-     {
5217895
- 	PyInt	i;
5217895
- 	PyInt	n = (int)(hi - lo);
5217895
- 	buf_T	*savebuf = curbuf;
5217895
- 
5217895
- 	PyErr_Clear();
5217895
- 	curbuf = buf;
5217895
- 
5217895
- 	if (u_savedel((linenr_T)lo, (long)n) == FAIL)
5217895
- 	    PyErr_SetVim(_("cannot save undo information"));
5217895
- 	else
5217895
- 	{
5217895
- 	    for (i = 0; i < n; ++i)
5217895
- 	    {
5217895
- 		if (ml_delete((linenr_T)lo, FALSE) == FAIL)
5217895
- 		{
5217895
- 		    PyErr_SetVim(_("cannot delete line"));
5217895
- 		    break;
5217895
- 		}
5217895
- 	    }
5217895
- 	    if (buf == curwin->w_buffer)
5217895
- 		py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)-n);
5217895
- 	    deleted_lines_mark((linenr_T)lo, (long)i);
5217895
- 	}
5217895
- 
5217895
- 	curbuf = savebuf;
5217895
- 
5217895
- 	if (PyErr_Occurred() || VimErrorCheck())
5217895
- 	    return FAIL;
5217895
- 
5217895
- 	if (len_change)
5217895
- 	    *len_change = -n;
5217895
- 
5217895
- 	return OK;
5217895
-     }
5217895
-     else if (PyList_Check(list))
5217895
-     {
5217895
- 	PyInt	i;
5217895
- 	PyInt	new_len = PyList_Size(list);
5217895
- 	PyInt	old_len = hi - lo;
5217895
- 	PyInt	extra = 0;	/* lines added to text, can be negative */
5217895
- 	char	**array;
5217895
- 	buf_T	*savebuf;
5217895
- 
5217895
- 	if (new_len == 0)	/* avoid allocating zero bytes */
5217895
- 	    array = NULL;
5217895
- 	else
5217895
- 	{
5217895
- 	    array = (char **)alloc((unsigned)(new_len * sizeof(char *)));
5217895
- 	    if (array == NULL)
5217895
- 	    {
5217895
- 		PyErr_NoMemory();
5217895
- 		return FAIL;
5217895
- 	    }
5217895
- 	}
5217895
- 
5217895
- 	for (i = 0; i < new_len; ++i)
5217895
- 	{
5217895
- 	    PyObject *line = PyList_GetItem(list, i);
5217895
- 
5217895
- 	    array[i] = StringToLine(line);
5217895
- 	    if (array[i] == NULL)
5217895
- 	    {
5217895
- 		while (i)
5217895
- 		    vim_free(array[--i]);
5217895
- 		vim_free(array);
5217895
- 		return FAIL;
5217895
- 	    }
5217895
- 	}
5217895
- 
5217895
- 	savebuf = curbuf;
5217895
- 
5217895
- 	PyErr_Clear();
5217895
- 	curbuf = buf;
5217895
- 
5217895
- 	if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
5217895
- 	    PyErr_SetVim(_("cannot save undo information"));
5217895
- 
5217895
- 	/* If the size of the range is reducing (ie, new_len < old_len) we
5217895
- 	 * need to delete some old_len. We do this at the start, by
5217895
- 	 * repeatedly deleting line "lo".
5217895
- 	 */
5217895
- 	if (!PyErr_Occurred())
5217895
- 	{
5217895
- 	    for (i = 0; i < old_len - new_len; ++i)
5217895
- 		if (ml_delete((linenr_T)lo, FALSE) == FAIL)
5217895
- 		{
5217895
- 		    PyErr_SetVim(_("cannot delete line"));
5217895
- 		    break;
5217895
- 		}
5217895
- 	    extra -= i;
5217895
- 	}
5217895
- 
5217895
- 	/* For as long as possible, replace the existing old_len with the
5217895
- 	 * new old_len. This is a more efficient operation, as it requires
5217895
- 	 * less memory allocation and freeing.
5217895
- 	 */
5217895
- 	if (!PyErr_Occurred())
5217895
- 	{
5217895
- 	    for (i = 0; i < old_len && i < new_len; ++i)
5217895
- 		if (ml_replace((linenr_T)(lo+i), (char_u *)array[i], FALSE)
5217895
- 								      == FAIL)
5217895
- 		{
5217895
- 		    PyErr_SetVim(_("cannot replace line"));
5217895
- 		    break;
5217895
- 		}
5217895
- 	}
5217895
- 	else
5217895
- 	    i = 0;
5217895
- 
5217895
- 	/* Now we may need to insert the remaining new old_len. If we do, we
5217895
- 	 * must free the strings as we finish with them (we can't pass the
5217895
- 	 * responsibility to vim in this case).
5217895
- 	 */
5217895
- 	if (!PyErr_Occurred())
5217895
- 	{
5217895
- 	    while (i < new_len)
5217895
- 	    {
5217895
- 		if (ml_append((linenr_T)(lo + i - 1),
5217895
- 					(char_u *)array[i], 0, FALSE) == FAIL)
5217895
- 		{
5217895
- 		    PyErr_SetVim(_("cannot insert line"));
5217895
- 		    break;
5217895
- 		}
5217895
- 		vim_free(array[i]);
5217895
- 		++i;
5217895
- 		++extra;
5217895
- 	    }
5217895
- 	}
5217895
- 
5217895
- 	/* Free any left-over old_len, as a result of an error */
5217895
- 	while (i < new_len)
5217895
- 	{
5217895
- 	    vim_free(array[i]);
5217895
- 	    ++i;
5217895
- 	}
5217895
- 
5217895
- 	/* Free the array of old_len. All of its contents have now
5217895
- 	 * been dealt with (either freed, or the responsibility passed
5217895
- 	 * to vim.
5217895
- 	 */
5217895
- 	vim_free(array);
5217895
- 
5217895
- 	/* Adjust marks. Invalidate any which lie in the
5217895
- 	 * changed range, and move any in the remainder of the buffer.
5217895
- 	 */
5217895
- 	mark_adjust((linenr_T)lo, (linenr_T)(hi - 1),
5217895
- 						  (long)MAXLNUM, (long)extra);
5217895
- 	changed_lines((linenr_T)lo, 0, (linenr_T)hi, (long)extra);
5217895
- 
5217895
- 	if (buf == curwin->w_buffer)
5217895
- 	    py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
5217895
- 
5217895
- 	curbuf = savebuf;
5217895
- 
5217895
- 	if (PyErr_Occurred() || VimErrorCheck())
5217895
- 	    return FAIL;
5217895
- 
5217895
- 	if (len_change)
5217895
- 	    *len_change = new_len - old_len;
5217895
- 
5217895
- 	return OK;
5217895
-     }
5217895
-     else
5217895
-     {
5217895
- 	PyErr_BadArgument();
5217895
- 	return FAIL;
5217895
-     }
5217895
- }
5217895
- 
5217895
  /* Convert a Vim line into a Python string.
5217895
   * All internal newlines are replaced by null characters.
5217895
   *
5217895
--- 1404,1409 ----
5217895
*** ../mercurial/vim73/src/if_python3.c	2011-06-12 21:37:06.000000000 +0200
5217895
--- src/if_python3.c	2011-06-19 00:10:42.000000000 +0200
5217895
***************
5217895
*** 70,77 ****
5217895
  
5217895
  #define PyInt Py_ssize_t
5217895
  #define PyString_Check(obj) PyUnicode_Check(obj)
5217895
! #define PyString_AsString(obj) _PyUnicode_AsString(obj)
5217895
! #define PyString_Size(obj) PyUnicode_GET_SIZE(obj)
5217895
  #define PyString_FromString(repr) PyUnicode_FromString(repr)
5217895
  
5217895
  #if defined(DYNAMIC_PYTHON3) || defined(PROTO)
5217895
--- 70,79 ----
5217895
  
5217895
  #define PyInt Py_ssize_t
5217895
  #define PyString_Check(obj) PyUnicode_Check(obj)
5217895
! #define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)p_enc, NULL);
5217895
! #define PyString_FreeBytes(obj) Py_XDECREF(bytes)
5217895
! #define PyString_AsString(obj) PyBytes_AsString(obj)
5217895
! #define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
5217895
  #define PyString_FromString(repr) PyUnicode_FromString(repr)
5217895
  
5217895
  #if defined(DYNAMIC_PYTHON3) || defined(PROTO)
5217895
***************
5217895
*** 99,104 ****
5217895
--- 101,107 ----
5217895
  # define PyArg_Parse py3_PyArg_Parse
5217895
  # undef PyArg_ParseTuple
5217895
  # define PyArg_ParseTuple py3_PyArg_ParseTuple
5217895
+ # define PyMem_Free py3_PyMem_Free
5217895
  # define PyDict_SetItemString py3_PyDict_SetItemString
5217895
  # define PyErr_BadArgument py3_PyErr_BadArgument
5217895
  # define PyErr_Clear py3_PyErr_Clear
5217895
***************
5217895
*** 140,147 ****
5217895
--- 143,155 ----
5217895
  # define PyModule_AddObject py3_PyModule_AddObject
5217895
  # define PyImport_AppendInittab py3_PyImport_AppendInittab
5217895
  # define _PyUnicode_AsString py3__PyUnicode_AsString
5217895
+ # undef PyUnicode_AsEncodedString
5217895
+ # define PyUnicode_AsEncodedString py3_PyUnicode_AsEncodedString
5217895
+ # undef PyBytes_AsString
5217895
+ # define PyBytes_AsString py3_PyBytes_AsString
5217895
  # define PyObject_GenericGetAttr py3_PyObject_GenericGetAttr
5217895
  # define PySlice_Type (*py3_PySlice_Type)
5217895
+ # define PyErr_NewException py3_PyErr_NewException
5217895
  # ifdef Py_DEBUG
5217895
  #  define _Py_NegativeRefcount py3__Py_NegativeRefcount
5217895
  #  define _Py_RefTotal (*py3__Py_RefTotal)
5217895
***************
5217895
*** 157,164 ****
5217895
  # define PyModule_Create2 py3_PyModule_Create2
5217895
  # undef PyUnicode_FromString
5217895
  # define PyUnicode_FromString py3_PyUnicode_FromString
5217895
! # undef PyUnicode_FromStringAndSize
5217895
! # define PyUnicode_FromStringAndSize py3_PyUnicode_FromStringAndSize
5217895
  
5217895
  # ifdef Py_DEBUG
5217895
  #  undef PyObject_NEW
5217895
--- 165,172 ----
5217895
  # define PyModule_Create2 py3_PyModule_Create2
5217895
  # undef PyUnicode_FromString
5217895
  # define PyUnicode_FromString py3_PyUnicode_FromString
5217895
! # undef PyUnicode_Decode
5217895
! # define PyUnicode_Decode py3_PyUnicode_Decode
5217895
  
5217895
  # ifdef Py_DEBUG
5217895
  #  undef PyObject_NEW
5217895
***************
5217895
*** 199,205 ****
5217895
  static int (*py3_PyType_Ready)(PyTypeObject *type);
5217895
  static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
5217895
  static PyObject* (*py3_PyUnicode_FromString)(const char *u);
5217895
! static PyObject* (*py3_PyUnicode_FromStringAndSize)(const char *u, Py_ssize_t size);
5217895
  static long (*py3_PyLong_AsLong)(PyObject *);
5217895
  static void (*py3_PyErr_SetNone)(PyObject *);
5217895
  static void (*py3_PyEval_InitThreads)(void);
5217895
--- 207,214 ----
5217895
  static int (*py3_PyType_Ready)(PyTypeObject *type);
5217895
  static int (*py3_PyDict_SetItemString)(PyObject *dp, char *key, PyObject *item);
5217895
  static PyObject* (*py3_PyUnicode_FromString)(const char *u);
5217895
! static PyObject* (*py3_PyUnicode_Decode)(const char *u, Py_ssize_t size,
5217895
! 	const char *encoding, const char *errors);
5217895
  static long (*py3_PyLong_AsLong)(PyObject *);
5217895
  static void (*py3_PyErr_SetNone)(PyObject *);
5217895
  static void (*py3_PyEval_InitThreads)(void);
5217895
***************
5217895
*** 207,212 ****
5217895
--- 216,222 ----
5217895
  static PyThreadState*(*py3_PyEval_SaveThread)(void);
5217895
  static int (*py3_PyArg_Parse)(PyObject *, char *, ...);
5217895
  static int (*py3_PyArg_ParseTuple)(PyObject *, char *, ...);
5217895
+ static int (*py3_PyMem_Free)(void *);
5217895
  static int (*py3_Py_IsInitialized)(void);
5217895
  static void (*py3_PyErr_Clear)(void);
5217895
  static PyObject*(*py3__PyObject_Init)(PyObject *, PyTypeObject *);
5217895
***************
5217895
*** 214,224 ****
5217895
--- 224,237 ----
5217895
  static int (*py3_PyModule_AddObject)(PyObject *m, const char *name, PyObject *o);
5217895
  static int (*py3_PyImport_AppendInittab)(const char *name, PyObject* (*initfunc)(void));
5217895
  static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
5217895
+ static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
5217895
+ static char* (*py3_PyBytes_AsString)(PyObject *bytes);
5217895
  static PyObject* (*py3_PyObject_GenericGetAttr)(PyObject *obj, PyObject *name);
5217895
  static PyObject* (*py3_PyModule_Create2)(struct PyModuleDef* module, int module_api_version);
5217895
  static PyObject* (*py3_PyType_GenericAlloc)(PyTypeObject *type, Py_ssize_t nitems);
5217895
  static PyObject* (*py3_PyType_GenericNew)(PyTypeObject *type, PyObject *args, PyObject *kwds);
5217895
  static PyTypeObject* py3_PySlice_Type;
5217895
+ static PyObject* (*py3_PyErr_NewException)(char *name, PyObject *base, PyObject *dict);
5217895
  # ifdef Py_DEBUG
5217895
      static void (*py3__Py_NegativeRefcount)(const char *fname, int lineno, PyObject *op);
5217895
      static Py_ssize_t* py3__Py_RefTotal;
5217895
***************
5217895
*** 259,264 ****
5217895
--- 272,278 ----
5217895
      {"Py_SetPythonHome", (PYTHON_PROC*)&py3_Py_SetPythonHome},
5217895
      {"Py_Initialize", (PYTHON_PROC*)&py3_Py_Initialize},
5217895
      {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
5217895
+     {"PyMem_Free", (PYTHON_PROC*)&py3_PyMem_Free},
5217895
      {"PyList_New", (PYTHON_PROC*)&py3_PyList_New},
5217895
      {"PyGILState_Ensure", (PYTHON_PROC*)&py3_PyGILState_Ensure},
5217895
      {"PyGILState_Release", (PYTHON_PROC*)&py3_PyGILState_Release},
5217895
***************
5217895
*** 289,295 ****
5217895
      {"PyEval_RestoreThread", (PYTHON_PROC*)&py3_PyEval_RestoreThread},
5217895
      {"PyEval_SaveThread", (PYTHON_PROC*)&py3_PyEval_SaveThread},
5217895
      {"PyArg_Parse", (PYTHON_PROC*)&py3_PyArg_Parse},
5217895
-     {"PyArg_ParseTuple", (PYTHON_PROC*)&py3_PyArg_ParseTuple},
5217895
      {"Py_IsInitialized", (PYTHON_PROC*)&py3_Py_IsInitialized},
5217895
      {"_Py_NoneStruct", (PYTHON_PROC*)&py3__Py_NoneStruct},
5217895
      {"PyErr_Clear", (PYTHON_PROC*)&py3_PyErr_Clear},
5217895
--- 303,308 ----
5217895
***************
5217895
*** 297,307 ****
5217895
--- 310,322 ----
5217895
      {"PyModule_AddObject", (PYTHON_PROC*)&py3_PyModule_AddObject},
5217895
      {"PyImport_AppendInittab", (PYTHON_PROC*)&py3_PyImport_AppendInittab},
5217895
      {"_PyUnicode_AsString", (PYTHON_PROC*)&py3__PyUnicode_AsString},
5217895
+     {"PyBytes_AsString", (PYTHON_PROC*)&py3_PyBytes_AsString},
5217895
      {"PyObject_GenericGetAttr", (PYTHON_PROC*)&py3_PyObject_GenericGetAttr},
5217895
      {"PyModule_Create2", (PYTHON_PROC*)&py3_PyModule_Create2},
5217895
      {"PyType_GenericAlloc", (PYTHON_PROC*)&py3_PyType_GenericAlloc},
5217895
      {"PyType_GenericNew", (PYTHON_PROC*)&py3_PyType_GenericNew},
5217895
      {"PySlice_Type", (PYTHON_PROC*)&py3_PySlice_Type},
5217895
+     {"PyErr_NewException", (PYTHON_PROC*)&py3_PyErr_NewException},
5217895
  # ifdef Py_DEBUG
5217895
      {"_Py_NegativeRefcount", (PYTHON_PROC*)&py3__Py_NegativeRefcount},
5217895
      {"_Py_RefTotal", (PYTHON_PROC*)&py3__Py_RefTotal},
5217895
***************
5217895
*** 337,343 ****
5217895
  py3_runtime_link_init(char *libname, int verbose)
5217895
  {
5217895
      int i;
5217895
!     void *ucs_from_string, *ucs_from_string_and_size;
5217895
  
5217895
  # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
5217895
      /* Can't have Python and Python3 loaded at the same time.
5217895
--- 352,358 ----
5217895
  py3_runtime_link_init(char *libname, int verbose)
5217895
  {
5217895
      int i;
5217895
!     void *ucs_from_string, *ucs_decode, *ucs_as_encoded_string;
5217895
  
5217895
  # if !(defined(PY_NO_RTLD_GLOBAL) && defined(PY3_NO_RTLD_GLOBAL)) && defined(UNIX) && defined(FEAT_PYTHON)
5217895
      /* Can't have Python and Python3 loaded at the same time.
5217895
***************
5217895
*** 377,395 ****
5217895
      /* Load unicode functions separately as only the ucs2 or the ucs4 functions
5217895
       * will be present in the library. */
5217895
      ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
5217895
!     ucs_from_string_and_size = symbol_from_dll(hinstPy3,
5217895
! 	    "PyUnicodeUCS2_FromStringAndSize");
5217895
!     if (!ucs_from_string || !ucs_from_string_and_size)
5217895
      {
5217895
  	ucs_from_string = symbol_from_dll(hinstPy3,
5217895
  		"PyUnicodeUCS4_FromString");
5217895
! 	ucs_from_string_and_size = symbol_from_dll(hinstPy3,
5217895
! 		"PyUnicodeUCS4_FromStringAndSize");
5217895
      }
5217895
!     if (ucs_from_string && ucs_from_string_and_size)
5217895
      {
5217895
  	py3_PyUnicode_FromString = ucs_from_string;
5217895
! 	py3_PyUnicode_FromStringAndSize = ucs_from_string_and_size;
5217895
      }
5217895
      else
5217895
      {
5217895
--- 392,415 ----
5217895
      /* Load unicode functions separately as only the ucs2 or the ucs4 functions
5217895
       * will be present in the library. */
5217895
      ucs_from_string = symbol_from_dll(hinstPy3, "PyUnicodeUCS2_FromString");
5217895
!     ucs_decode = symbol_from_dll(hinstPy3,
5217895
! 	    "PyUnicodeUCS2_Decode");
5217895
!     ucs_as_encoded_string = symbol_from_dll(hinstPy3,
5217895
! 	    "PyUnicodeUCS2_AsEncodedString");
5217895
!     if (!ucs_from_string || !ucs_decode || !ucs_as_encoded_string)
5217895
      {
5217895
  	ucs_from_string = symbol_from_dll(hinstPy3,
5217895
  		"PyUnicodeUCS4_FromString");
5217895
! 	ucs_decode = symbol_from_dll(hinstPy3,
5217895
! 		"PyUnicodeUCS4_Decode");
5217895
! 	ucs_as_encoded_string = symbol_from_dll(hinstPy3,
5217895
! 		"PyUnicodeUCS4_AsEncodedString");
5217895
      }
5217895
!     if (ucs_from_string && ucs_decode && ucs_as_encoded_string)
5217895
      {
5217895
  	py3_PyUnicode_FromString = ucs_from_string;
5217895
! 	py3_PyUnicode_Decode = ucs_decode;
5217895
! 	py3_PyUnicode_AsEncodedString = ucs_as_encoded_string;
5217895
      }
5217895
      else
5217895
      {
5217895
***************
5217895
*** 567,574 ****
5217895
  	/* Remove the element from sys.path that was added because of our
5217895
  	 * argv[0] value in Py3Init_vim().  Previously we used an empty
5217895
  	 * string, but dependinding on the OS we then get an empty entry or
5217895
! 	 * the current directory in sys.path. */
5217895
! 	PyRun_SimpleString("import sys; sys.path = list(filter(lambda x: x != '/must>not&exist', sys.path))");
5217895
  
5217895
  	// lock is created and acquired in PyEval_InitThreads() and thread
5217895
  	// state is created in Py_Initialize()
5217895
--- 587,597 ----
5217895
  	/* Remove the element from sys.path that was added because of our
5217895
  	 * argv[0] value in Py3Init_vim().  Previously we used an empty
5217895
  	 * string, but dependinding on the OS we then get an empty entry or
5217895
! 	 * the current directory in sys.path.
5217895
! 	 * Only after vim has been imported, the element does exist in
5217895
! 	 * sys.path.
5217895
! 	 */
5217895
! 	PyRun_SimpleString("import vim; import sys; sys.path = list(filter(lambda x: not x.endswith('must>not&exist'), sys.path))");
5217895
  
5217895
  	// lock is created and acquired in PyEval_InitThreads() and thread
5217895
  	// state is created in Py_Initialize()
5217895
***************
5217895
*** 605,610 ****
5217895
--- 628,635 ----
5217895
  #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
5217895
      char		*saved_locale;
5217895
  #endif
5217895
+     PyObject		*cmdstr;
5217895
+     PyObject		*cmdbytes;
5217895
  
5217895
  #if defined(MACOS) && !defined(MACOS_X_UNIX)
5217895
      GetPort(&oldPort);
5217895
***************
5217895
*** 634,640 ****
5217895
  
5217895
      pygilstate = PyGILState_Ensure();
5217895
  
5217895
!     PyRun_SimpleString((char *)(cmd));
5217895
  
5217895
      PyGILState_Release(pygilstate);
5217895
  
5217895
--- 659,671 ----
5217895
  
5217895
      pygilstate = PyGILState_Ensure();
5217895
  
5217895
!     /* PyRun_SimpleString expects a UTF-8 string. Wrong encoding may cause
5217895
!      * SyntaxError (unicode error). */
5217895
!     cmdstr = PyUnicode_Decode(cmd, strlen(cmd), (char *)p_enc, NULL);
5217895
!     cmdbytes = PyUnicode_AsEncodedString(cmdstr, "utf-8", NULL);
5217895
!     Py_XDECREF(cmdstr);
5217895
!     PyRun_SimpleString(PyBytes_AsString(cmdbytes));
5217895
!     Py_XDECREF(cmdbytes);
5217895
  
5217895
      PyGILState_Release(pygilstate);
5217895
  
5217895
***************
5217895
*** 693,699 ****
5217895
       * different options under Windows, meaning that stdio pointers aren't
5217895
       * compatible between the two. Yuk.
5217895
       *
5217895
!      * construct: exec(compile(open('a_filename').read(), 'a_filename', 'exec'))
5217895
       *
5217895
       * We need to escape any backslashes or single quotes in the file name, so that
5217895
       * Python won't mangle the file name.
5217895
--- 724,733 ----
5217895
       * different options under Windows, meaning that stdio pointers aren't
5217895
       * compatible between the two. Yuk.
5217895
       *
5217895
!      * construct: exec(compile(open('a_filename', 'rb').read(), 'a_filename', 'exec'))
5217895
!      *
5217895
!      * Using bytes so that Python can detect the source encoding as it normally
5217895
!      * does. The doc does not say "compile" accept bytes, though.
5217895
       *
5217895
       * We need to escape any backslashes or single quotes in the file name, so that
5217895
       * Python won't mangle the file name.
5217895
***************
5217895
*** 716,723 ****
5217895
  	    return;
5217895
  	if (i==0)
5217895
  	{
5217895
! 	    strcpy(p,"').read(),'");
5217895
! 	    p += 11;
5217895
  	}
5217895
  	else
5217895
  	{
5217895
--- 750,757 ----
5217895
  	    return;
5217895
  	if (i==0)
5217895
  	{
5217895
! 	    strcpy(p,"','rb').read(),'");
5217895
! 	    p += 16;
5217895
  	}
5217895
  	else
5217895
  	{
5217895
***************
5217895
*** 812,819 ****
5217895
  
5217895
  static Py_ssize_t BufferLength(PyObject *);
5217895
  static PyObject *BufferItem(PyObject *, Py_ssize_t);
5217895
- static Py_ssize_t BufferAsItem(PyObject *, Py_ssize_t, PyObject *);
5217895
  static PyObject* BufferSubscript(PyObject *self, PyObject* idx);
5217895
  
5217895
  
5217895
  /* Line range type - Implementation functions
5217895
--- 846,853 ----
5217895
  
5217895
  static Py_ssize_t BufferLength(PyObject *);
5217895
  static PyObject *BufferItem(PyObject *, Py_ssize_t);
5217895
  static PyObject* BufferSubscript(PyObject *self, PyObject* idx);
5217895
+ static Py_ssize_t BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val);
5217895
  
5217895
  
5217895
  /* Line range type - Implementation functions
5217895
***************
5217895
*** 835,841 ****
5217895
      (ssizeargfunc)	0,		    /* sq_repeat,    x*n      */
5217895
      (ssizeargfunc)	BufferItem,	    /* sq_item,      x[i]     */
5217895
      0,					    /* was_sq_slice,	 x[i:j]   */
5217895
!     (ssizeobjargproc)	BufferAsItem,	    /* sq_ass_item,  x[i]=v   */
5217895
      0,					    /* sq_ass_slice, x[i:j]=v */
5217895
      0,					    /* sq_contains */
5217895
      0,					    /* sq_inplace_concat */
5217895
--- 869,875 ----
5217895
      (ssizeargfunc)	0,		    /* sq_repeat,    x*n      */
5217895
      (ssizeargfunc)	BufferItem,	    /* sq_item,      x[i]     */
5217895
      0,					    /* was_sq_slice,	 x[i:j]   */
5217895
!     0,					    /* sq_ass_item,  x[i]=v   */
5217895
      0,					    /* sq_ass_slice, x[i:j]=v */
5217895
      0,					    /* sq_contains */
5217895
      0,					    /* sq_inplace_concat */
5217895
***************
5217895
*** 845,851 ****
5217895
  PyMappingMethods BufferAsMapping = {
5217895
      /* mp_length	*/ (lenfunc)BufferLength,
5217895
      /* mp_subscript     */ (binaryfunc)BufferSubscript,
5217895
!     /* mp_ass_subscript */ (objobjargproc)0,
5217895
  };
5217895
  
5217895
  
5217895
--- 879,885 ----
5217895
  PyMappingMethods BufferAsMapping = {
5217895
      /* mp_length	*/ (lenfunc)BufferLength,
5217895
      /* mp_subscript     */ (binaryfunc)BufferSubscript,
5217895
!     /* mp_ass_subscript */ (objobjargproc)BufferAsSubscript,
5217895
  };
5217895
  
5217895
  
5217895
***************
5217895
*** 897,902 ****
5217895
--- 931,938 ----
5217895
  
5217895
      if (this->buf && this->buf != INVALID_BUFFER_VALUE)
5217895
  	this->buf->b_python3_ref = NULL;
5217895
+ 
5217895
+     Py_TYPE(self)->tp_free((PyObject*)self);
5217895
  }
5217895
  
5217895
      static PyObject *
5217895
***************
5217895
*** 975,989 ****
5217895
  	       (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
5217895
  }
5217895
  
5217895
-     static Py_ssize_t
5217895
- BufferAsItem(PyObject *self, Py_ssize_t n, PyObject *val)
5217895
- {
5217895
-     return RBAsItem((BufferObject *)(self), n, val, 1,
5217895
- 		(Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
5217895
- 		NULL);
5217895
- }
5217895
- 
5217895
- 
5217895
      static PyObject *
5217895
  BufferSubscript(PyObject *self, PyObject* idx)
5217895
  {
5217895
--- 1011,1016 ----
5217895
***************
5217895
*** 999,1011 ****
5217895
  	      &step, &slicelen) < 0) {
5217895
  	    return NULL;
5217895
  	}
5217895
! 	return BufferSlice(self,start,stop+1);
5217895
      } else {
5217895
  	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
5217895
  	return NULL;
5217895
      }
5217895
  }
5217895
  
5217895
  static PySequenceMethods RangeAsSeq = {
5217895
      (lenfunc)		RangeLength,	 /* sq_length,	  len(x)   */
5217895
      (binaryfunc)	0,		 /* RangeConcat, sq_concat,  x+y   */
5217895
--- 1026,1064 ----
5217895
  	      &step, &slicelen) < 0) {
5217895
  	    return NULL;
5217895
  	}
5217895
! 	return BufferSlice(self,start,stop);
5217895
      } else {
5217895
  	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
5217895
  	return NULL;
5217895
      }
5217895
  }
5217895
  
5217895
+     static Py_ssize_t
5217895
+ BufferAsSubscript(PyObject *self, PyObject* idx, PyObject* val)
5217895
+ {
5217895
+     if (PyLong_Check(idx)) {
5217895
+ 	long n = PyLong_AsLong(idx);
5217895
+ 	return RBAsItem((BufferObject *)(self), n, val, 1,
5217895
+ 		    (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count,
5217895
+ 		    NULL);
5217895
+     } else if (PySlice_Check(idx)) {
5217895
+ 	Py_ssize_t start, stop, step, slicelen;
5217895
+ 
5217895
+ 	if (PySlice_GetIndicesEx((PySliceObject *)idx,
5217895
+ 	      (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count+1,
5217895
+ 	      &start, &stop,
5217895
+ 	      &step, &slicelen) < 0) {
5217895
+ 	    return -1;
5217895
+ 	}
5217895
+ 	return RBAsSlice((BufferObject *)(self), start, stop, val, 1,
5217895
+ 			  (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count,
5217895
+ 			  NULL);
5217895
+     } else {
5217895
+ 	PyErr_SetString(PyExc_IndexError, "Index must be int or slice");
5217895
+ 	return -1;
5217895
+     }
5217895
+ }
5217895
+ 
5217895
  static PySequenceMethods RangeAsSeq = {
5217895
      (lenfunc)		RangeLength,	 /* sq_length,	  len(x)   */
5217895
      (binaryfunc)	0,		 /* RangeConcat, sq_concat,  x+y   */
5217895
***************
5217895
*** 1032,1037 ****
5217895
--- 1085,1091 ----
5217895
  RangeDestructor(PyObject *self)
5217895
  {
5217895
      Py_DECREF(((RangeObject *)(self))->buf);
5217895
+     Py_TYPE(self)->tp_free((PyObject*)self);
5217895
  }
5217895
  
5217895
      static PyObject *
5217895
***************
5217895
*** 1159,1164 ****
5217895
--- 1213,1220 ----
5217895
  
5217895
      if (this->win && this->win != INVALID_WINDOW_VALUE)
5217895
  	this->win->w_python3_ref = NULL;
5217895
+ 
5217895
+     Py_TYPE(self)->tp_free((PyObject*)self);
5217895
  }
5217895
  
5217895
      static PyObject *
5217895
***************
5217895
*** 1350,1357 ****
5217895
      PySys_SetArgv(1, argv);
5217895
  
5217895
      mod = PyModule_Create(&vimmodule);
5217895
  
5217895
!     VimError = Py_BuildValue("s", "vim.error");
5217895
  
5217895
      PyModule_AddObject(mod, "error", VimError);
5217895
      Py_INCREF((PyObject *)(void *)&TheBufferList);
5217895
--- 1406,1416 ----
5217895
      PySys_SetArgv(1, argv);
5217895
  
5217895
      mod = PyModule_Create(&vimmodule);
5217895
+     if (mod == NULL)
5217895
+ 	return NULL;
5217895
  
5217895
!     VimError = PyErr_NewException("vim.error", NULL, NULL);
5217895
!     Py_INCREF(VimError);
5217895
  
5217895
      PyModule_AddObject(mod, "error", VimError);
5217895
      Py_INCREF((PyObject *)(void *)&TheBufferList);
5217895
***************
5217895
*** 1404,1410 ****
5217895
      }
5217895
      *p = '\0';
5217895
  
5217895
!     result = PyUnicode_FromStringAndSize(tmp, len);
5217895
  
5217895
      vim_free(tmp);
5217895
      return result;
5217895
--- 1463,1469 ----
5217895
      }
5217895
      *p = '\0';
5217895
  
5217895
!     result = PyUnicode_Decode(tmp, len, (char *)p_enc, NULL);
5217895
  
5217895
      vim_free(tmp);
5217895
      return result;
5217895
*** ../vim-7.3.219/src/version.c	2011-06-13 02:03:55.000000000 +0200
5217895
--- src/version.c	2011-06-19 00:25:38.000000000 +0200
5217895
***************
5217895
*** 711,712 ****
5217895
--- 711,714 ----
5217895
  {   /* Add new patch number below this line */
5217895
+ /**/
5217895
+     220,
5217895
  /**/
5217895
5217895
-- 
5217895
I'm in shape.  Round IS a shape.
5217895
5217895
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
5217895
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
5217895
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
5217895
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///