Karsten Hopp 084270
To: vim-dev@vim.org
Karsten Hopp 084270
Subject: Patch 7.3.007
Karsten Hopp 084270
Fcc: outbox
Karsten Hopp 084270
From: Bram Moolenaar <Bram@moolenaar.net>
Karsten Hopp 084270
Mime-Version: 1.0
Karsten Hopp 084270
Content-Type: text/plain; charset=UTF-8
Karsten Hopp 084270
Content-Transfer-Encoding: 8bit
Karsten Hopp 084270
------------
Karsten Hopp 084270
Karsten Hopp 084270
Patch 7.3.007
Karsten Hopp 084270
Problem:    Python code defines global "buffer".  Re-implements a grow-array.
Karsten Hopp 084270
Solution:   Use a grow-array instead of coding the same functionality.  Handle
Karsten Hopp 084270
	    out-of-memory situation properly.
Karsten Hopp 084270
Files:	    src/if_py_both.h
Karsten Hopp 084270
Karsten Hopp 084270
Karsten Hopp 084270
*** ../vim-7.3.006/src/if_py_both.h	2010-08-15 21:57:27.000000000 +0200
Karsten Hopp 084270
--- src/if_py_both.h	2010-09-21 16:00:54.000000000 +0200
Karsten Hopp 084270
***************
Karsten Hopp 084270
*** 34,39 ****
Karsten Hopp 084270
--- 34,40 ----
Karsten Hopp 084270
  static PyObject *OutputWrite(PyObject *, PyObject *);
Karsten Hopp 084270
  static PyObject *OutputWritelines(PyObject *, PyObject *);
Karsten Hopp 084270
  
Karsten Hopp 084270
+ /* Function to write a line, points to either msg() or emsg(). */
Karsten Hopp 084270
  typedef void (*writefn)(char_u *);
Karsten Hopp 084270
  static void writer(writefn fn, char_u *str, PyInt n);
Karsten Hopp 084270
  
Karsten Hopp 084270
***************
Karsten Hopp 084270
*** 122,173 ****
Karsten Hopp 084270
      return Py_None;
Karsten Hopp 084270
  }
Karsten Hopp 084270
  
Karsten Hopp 084270
! static char_u *buffer = NULL;
Karsten Hopp 084270
! static PyInt buffer_len = 0;
Karsten Hopp 084270
! static PyInt buffer_size = 0;
Karsten Hopp 084270
! 
Karsten Hopp 084270
  static writefn old_fn = NULL;
Karsten Hopp 084270
  
Karsten Hopp 084270
      static void
Karsten Hopp 084270
- buffer_ensure(PyInt n)
Karsten Hopp 084270
- {
Karsten Hopp 084270
-     PyInt new_size;
Karsten Hopp 084270
-     char_u *new_buffer;
Karsten Hopp 084270
- 
Karsten Hopp 084270
-     if (n < buffer_size)
Karsten Hopp 084270
- 	return;
Karsten Hopp 084270
- 
Karsten Hopp 084270
-     new_size = buffer_size;
Karsten Hopp 084270
-     while (new_size < n)
Karsten Hopp 084270
- 	new_size += 80;
Karsten Hopp 084270
- 
Karsten Hopp 084270
-     if (new_size != buffer_size)
Karsten Hopp 084270
-     {
Karsten Hopp 084270
- 	new_buffer = alloc((unsigned)new_size);
Karsten Hopp 084270
- 	if (new_buffer == NULL)
Karsten Hopp 084270
- 	    return;
Karsten Hopp 084270
- 
Karsten Hopp 084270
- 	if (buffer)
Karsten Hopp 084270
- 	{
Karsten Hopp 084270
- 	    memcpy(new_buffer, buffer, buffer_len);
Karsten Hopp 084270
- 	    vim_free(buffer);
Karsten Hopp 084270
- 	}
Karsten Hopp 084270
- 
Karsten Hopp 084270
- 	buffer = new_buffer;
Karsten Hopp 084270
- 	buffer_size = new_size;
Karsten Hopp 084270
-     }
Karsten Hopp 084270
- }
Karsten Hopp 084270
- 
Karsten Hopp 084270
-     static void
Karsten Hopp 084270
  PythonIO_Flush(void)
Karsten Hopp 084270
  {
Karsten Hopp 084270
!     if (old_fn && buffer_len)
Karsten Hopp 084270
      {
Karsten Hopp 084270
! 	buffer[buffer_len] = 0;
Karsten Hopp 084270
! 	old_fn(buffer);
Karsten Hopp 084270
      }
Karsten Hopp 084270
! 
Karsten Hopp 084270
!     buffer_len = 0;
Karsten Hopp 084270
  }
Karsten Hopp 084270
  
Karsten Hopp 084270
      static void
Karsten Hopp 084270
--- 123,141 ----
Karsten Hopp 084270
      return Py_None;
Karsten Hopp 084270
  }
Karsten Hopp 084270
  
Karsten Hopp 084270
! /* Buffer IO, we write one whole line at a time. */
Karsten Hopp 084270
! static garray_T io_ga = {0, 0, 1, 80, NULL};
Karsten Hopp 084270
  static writefn old_fn = NULL;
Karsten Hopp 084270
  
Karsten Hopp 084270
      static void
Karsten Hopp 084270
  PythonIO_Flush(void)
Karsten Hopp 084270
  {
Karsten Hopp 084270
!     if (old_fn != NULL && io_ga.ga_len > 0)
Karsten Hopp 084270
      {
Karsten Hopp 084270
! 	((char_u *)io_ga.ga_data)[io_ga.ga_len] = NUL;
Karsten Hopp 084270
! 	old_fn((char_u *)io_ga.ga_data);
Karsten Hopp 084270
      }
Karsten Hopp 084270
!     io_ga.ga_len = 0;
Karsten Hopp 084270
  }
Karsten Hopp 084270
  
Karsten Hopp 084270
      static void
Karsten Hopp 084270
***************
Karsten Hopp 084270
*** 175,204 ****
Karsten Hopp 084270
  {
Karsten Hopp 084270
      char_u *ptr;
Karsten Hopp 084270
  
Karsten Hopp 084270
!     if (fn != old_fn && old_fn != NULL)
Karsten Hopp 084270
  	PythonIO_Flush();
Karsten Hopp 084270
- 
Karsten Hopp 084270
      old_fn = fn;
Karsten Hopp 084270
  
Karsten Hopp 084270
      while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
Karsten Hopp 084270
      {
Karsten Hopp 084270
  	PyInt len = ptr - str;
Karsten Hopp 084270
  
Karsten Hopp 084270
! 	buffer_ensure(buffer_len + len + 1);
Karsten Hopp 084270
  
Karsten Hopp 084270
! 	memcpy(buffer + buffer_len, str, len);
Karsten Hopp 084270
! 	buffer_len += len;
Karsten Hopp 084270
! 	buffer[buffer_len] = 0;
Karsten Hopp 084270
! 	fn(buffer);
Karsten Hopp 084270
  	str = ptr + 1;
Karsten Hopp 084270
  	n -= len + 1;
Karsten Hopp 084270
! 	buffer_len = 0;
Karsten Hopp 084270
      }
Karsten Hopp 084270
  
Karsten Hopp 084270
!     /* Put the remaining text into the buffer for later printing */
Karsten Hopp 084270
!     buffer_ensure(buffer_len + n + 1);
Karsten Hopp 084270
!     memcpy(buffer + buffer_len, str, n);
Karsten Hopp 084270
!     buffer_len += n;
Karsten Hopp 084270
  }
Karsten Hopp 084270
  
Karsten Hopp 084270
  /***************/
Karsten Hopp 084270
--- 143,176 ----
Karsten Hopp 084270
  {
Karsten Hopp 084270
      char_u *ptr;
Karsten Hopp 084270
  
Karsten Hopp 084270
!     /* Flush when switching output function. */
Karsten Hopp 084270
!     if (fn != old_fn)
Karsten Hopp 084270
  	PythonIO_Flush();
Karsten Hopp 084270
      old_fn = fn;
Karsten Hopp 084270
  
Karsten Hopp 084270
+     /* Write each NL separated line.  Text after the last NL is kept for
Karsten Hopp 084270
+      * writing later. */
Karsten Hopp 084270
      while (n > 0 && (ptr = memchr(str, '\n', n)) != NULL)
Karsten Hopp 084270
      {
Karsten Hopp 084270
  	PyInt len = ptr - str;
Karsten Hopp 084270
  
Karsten Hopp 084270
! 	if (ga_grow(&io_ga, len + 1) == FAIL)
Karsten Hopp 084270
! 	    break;
Karsten Hopp 084270
  
Karsten Hopp 084270
! 	mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)len);
Karsten Hopp 084270
! 	((char *)io_ga.ga_data)[io_ga.ga_len + len] = NUL;
Karsten Hopp 084270
! 	fn((char_u *)io_ga.ga_data);
Karsten Hopp 084270
  	str = ptr + 1;
Karsten Hopp 084270
  	n -= len + 1;
Karsten Hopp 084270
! 	io_ga.ga_len = 0;
Karsten Hopp 084270
      }
Karsten Hopp 084270
  
Karsten Hopp 084270
!     /* Put the remaining text into io_ga for later printing. */
Karsten Hopp 084270
!     if (n > 0 && ga_grow(&io_ga, n + 1) == OK)
Karsten Hopp 084270
!     {
Karsten Hopp 084270
! 	mch_memmove(((char *)io_ga.ga_data) + io_ga.ga_len, str, (size_t)n);
Karsten Hopp 084270
! 	io_ga.ga_len += n;
Karsten Hopp 084270
!     }
Karsten Hopp 084270
  }
Karsten Hopp 084270
  
Karsten Hopp 084270
  /***************/
Karsten Hopp 084270
*** ../vim-7.3.006/src/version.c	2010-09-18 13:36:41.000000000 +0200
Karsten Hopp 084270
--- src/version.c	2010-09-21 16:49:13.000000000 +0200
Karsten Hopp 084270
***************
Karsten Hopp 084270
*** 716,717 ****
Karsten Hopp 084270
--- 716,719 ----
Karsten Hopp 084270
  {   /* Add new patch number below this line */
Karsten Hopp 084270
+ /**/
Karsten Hopp 084270
+     7,
Karsten Hopp 084270
  /**/
Karsten Hopp 084270
Karsten Hopp 084270
-- 
Karsten Hopp 084270
hundred-and-one symptoms of being an internet addict:
Karsten Hopp 084270
180. You maintain more than six e-mail addresses.
Karsten Hopp 084270
Karsten Hopp 084270
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
Karsten Hopp 084270
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
Karsten Hopp 084270
\\\        download, build and distribute -- http://www.A-A-P.org        ///
Karsten Hopp 084270
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///