Karsten Hopp 4451f1
To: vim_dev@googlegroups.com
Karsten Hopp 4451f1
Subject: Patch 7.3.937
Karsten Hopp 4451f1
Fcc: outbox
Karsten Hopp 4451f1
From: Bram Moolenaar <Bram@moolenaar.net>
Karsten Hopp 4451f1
Mime-Version: 1.0
Karsten Hopp 4451f1
Content-Type: text/plain; charset=UTF-8
Karsten Hopp 4451f1
Content-Transfer-Encoding: 8bit
Karsten Hopp 4451f1
------------
Karsten Hopp 4451f1
Karsten Hopp 4451f1
Patch 7.3.937
Karsten Hopp 4451f1
Problem:    More can be shared between Python 2 and 3.
Karsten Hopp 4451f1
Solution:   Move code to if_py_both.h. (ZyX)
Karsten Hopp 4451f1
Files:	    src/if_python.c, src/if_python3.c, src/if_py_both.h
Karsten Hopp 4451f1
Karsten Hopp 4451f1
Karsten Hopp 4451f1
*** ../vim-7.3.936/src/if_python.c	2013-05-06 04:21:35.000000000 +0200
Karsten Hopp 4451f1
--- src/if_python.c	2013-05-12 18:31:20.000000000 +0200
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 619,624 ****
Karsten Hopp 4451f1
--- 619,627 ----
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
  #define DESTRUCTOR_FINISH(self) Py_DECREF(self);
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
+ #define WIN_PYTHON_REF(win) win->w_python_ref
Karsten Hopp 4451f1
+ #define BUF_PYTHON_REF(buf) buf->b_python_ref
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
  static PyObject *OutputGetattr(PyObject *, char *);
Karsten Hopp 4451f1
  static PyObject *BufferGetattr(PyObject *, char *);
Karsten Hopp 4451f1
  static PyObject *WindowGetattr(PyObject *, char *);
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 1054,1095 ****
Karsten Hopp 4451f1
   */
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      static PyObject *
Karsten Hopp 4451f1
- BufferNew(buf_T *buf)
Karsten Hopp 4451f1
- {
Karsten Hopp 4451f1
-     /* We need to handle deletion of buffers underneath us.
Karsten Hopp 4451f1
-      * If we add a "b_python_ref" field to the buf_T structure,
Karsten Hopp 4451f1
-      * then we can get at it in buf_freeall() in vim. We then
Karsten Hopp 4451f1
-      * need to create only ONE Python object per buffer - if
Karsten Hopp 4451f1
-      * we try to create a second, just INCREF the existing one
Karsten Hopp 4451f1
-      * and return it. The (single) Python object referring to
Karsten Hopp 4451f1
-      * the buffer is stored in "b_python_ref".
Karsten Hopp 4451f1
-      * Question: what to do on a buf_freeall(). We'll probably
Karsten Hopp 4451f1
-      * have to either delete the Python object (DECREF it to
Karsten Hopp 4451f1
-      * zero - a bad idea, as it leaves dangling refs!) or
Karsten Hopp 4451f1
-      * set the buf_T * value to an invalid value (-1?), which
Karsten Hopp 4451f1
-      * means we need checks in all access functions... Bah.
Karsten Hopp 4451f1
-      */
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     BufferObject *self;
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     if (buf->b_python_ref != NULL)
Karsten Hopp 4451f1
-     {
Karsten Hopp 4451f1
- 	self = buf->b_python_ref;
Karsten Hopp 4451f1
- 	Py_INCREF(self);
Karsten Hopp 4451f1
-     }
Karsten Hopp 4451f1
-     else
Karsten Hopp 4451f1
-     {
Karsten Hopp 4451f1
- 	self = PyObject_NEW(BufferObject, &BufferType);
Karsten Hopp 4451f1
- 	if (self == NULL)
Karsten Hopp 4451f1
- 	    return NULL;
Karsten Hopp 4451f1
- 	self->buf = buf;
Karsten Hopp 4451f1
- 	buf->b_python_ref = self;
Karsten Hopp 4451f1
-     }
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     return (PyObject *)(self);
Karsten Hopp 4451f1
- }
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     static PyObject *
Karsten Hopp 4451f1
  BufferGetattr(PyObject *self, char *name)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
      PyObject *r;
Karsten Hopp 4451f1
--- 1057,1062 ----
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 1107,1136 ****
Karsten Hopp 4451f1
  /******************/
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      static PyInt
Karsten Hopp 4451f1
- BufferLength(PyObject *self)
Karsten Hopp 4451f1
- {
Karsten Hopp 4451f1
-     /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Karsten Hopp 4451f1
-     if (CheckBuffer((BufferObject *)(self)))
Karsten Hopp 4451f1
- 	return -1; /* ??? */
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     return (((BufferObject *)(self))->buf->b_ml.ml_line_count);
Karsten Hopp 4451f1
- }
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     static PyObject *
Karsten Hopp 4451f1
- BufferItem(PyObject *self, PyInt n)
Karsten Hopp 4451f1
- {
Karsten Hopp 4451f1
-     return RBItem((BufferObject *)(self), n, 1,
Karsten Hopp 4451f1
- 		  (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Karsten Hopp 4451f1
- }
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     static PyObject *
Karsten Hopp 4451f1
- BufferSlice(PyObject *self, PyInt lo, PyInt hi)
Karsten Hopp 4451f1
- {
Karsten Hopp 4451f1
-     return RBSlice((BufferObject *)(self), lo, hi, 1,
Karsten Hopp 4451f1
- 		   (int)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Karsten Hopp 4451f1
- }
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     static PyInt
Karsten Hopp 4451f1
  BufferAssItem(PyObject *self, PyInt n, PyObject *val)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
      return RBAsItem((BufferObject *)(self), n, val, 1,
Karsten Hopp 4451f1
--- 1074,1079 ----
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 1217,1256 ****
Karsten Hopp 4451f1
   */
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      static PyObject *
Karsten Hopp 4451f1
- WindowNew(win_T *win)
Karsten Hopp 4451f1
- {
Karsten Hopp 4451f1
-     /* We need to handle deletion of windows underneath us.
Karsten Hopp 4451f1
-      * If we add a "w_python_ref" field to the win_T structure,
Karsten Hopp 4451f1
-      * then we can get at it in win_free() in vim. We then
Karsten Hopp 4451f1
-      * need to create only ONE Python object per window - if
Karsten Hopp 4451f1
-      * we try to create a second, just INCREF the existing one
Karsten Hopp 4451f1
-      * and return it. The (single) Python object referring to
Karsten Hopp 4451f1
-      * the window is stored in "w_python_ref".
Karsten Hopp 4451f1
-      * On a win_free() we set the Python object's win_T* field
Karsten Hopp 4451f1
-      * to an invalid value. We trap all uses of a window
Karsten Hopp 4451f1
-      * object, and reject them if the win_T* field is invalid.
Karsten Hopp 4451f1
-      */
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     WindowObject *self;
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     if (win->w_python_ref)
Karsten Hopp 4451f1
-     {
Karsten Hopp 4451f1
- 	self = win->w_python_ref;
Karsten Hopp 4451f1
- 	Py_INCREF(self);
Karsten Hopp 4451f1
-     }
Karsten Hopp 4451f1
-     else
Karsten Hopp 4451f1
-     {
Karsten Hopp 4451f1
- 	self = PyObject_NEW(WindowObject, &WindowType);
Karsten Hopp 4451f1
- 	if (self == NULL)
Karsten Hopp 4451f1
- 	    return NULL;
Karsten Hopp 4451f1
- 	self->win = win;
Karsten Hopp 4451f1
- 	win->w_python_ref = self;
Karsten Hopp 4451f1
-     }
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     return (PyObject *)(self);
Karsten Hopp 4451f1
- }
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     static PyObject *
Karsten Hopp 4451f1
  WindowGetattr(PyObject *self, char *name)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
      PyObject *r;
Karsten Hopp 4451f1
--- 1160,1165 ----
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 1289,1299 ****
Karsten Hopp 4451f1
      void
Karsten Hopp 4451f1
  python_buffer_free(buf_T *buf)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
!     if (buf->b_python_ref != NULL)
Karsten Hopp 4451f1
      {
Karsten Hopp 4451f1
! 	BufferObject *bp = buf->b_python_ref;
Karsten Hopp 4451f1
  	bp->buf = INVALID_BUFFER_VALUE;
Karsten Hopp 4451f1
! 	buf->b_python_ref = NULL;
Karsten Hopp 4451f1
      }
Karsten Hopp 4451f1
  }
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
--- 1198,1208 ----
Karsten Hopp 4451f1
      void
Karsten Hopp 4451f1
  python_buffer_free(buf_T *buf)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
!     if (BUF_PYTHON_REF(buf) != NULL)
Karsten Hopp 4451f1
      {
Karsten Hopp 4451f1
! 	BufferObject *bp = BUF_PYTHON_REF(buf);
Karsten Hopp 4451f1
  	bp->buf = INVALID_BUFFER_VALUE;
Karsten Hopp 4451f1
! 	BUF_PYTHON_REF(buf) = NULL;
Karsten Hopp 4451f1
      }
Karsten Hopp 4451f1
  }
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 1301,1311 ****
Karsten Hopp 4451f1
      void
Karsten Hopp 4451f1
  python_window_free(win_T *win)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
!     if (win->w_python_ref != NULL)
Karsten Hopp 4451f1
      {
Karsten Hopp 4451f1
! 	WindowObject *wp = win->w_python_ref;
Karsten Hopp 4451f1
  	wp->win = INVALID_WINDOW_VALUE;
Karsten Hopp 4451f1
! 	win->w_python_ref = NULL;
Karsten Hopp 4451f1
      }
Karsten Hopp 4451f1
  }
Karsten Hopp 4451f1
  #endif
Karsten Hopp 4451f1
--- 1210,1220 ----
Karsten Hopp 4451f1
      void
Karsten Hopp 4451f1
  python_window_free(win_T *win)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
!     if (WIN_PYTHON_REF(win) != NULL)
Karsten Hopp 4451f1
      {
Karsten Hopp 4451f1
! 	WindowObject *wp = WIN_PYTHON_REF(win);
Karsten Hopp 4451f1
  	wp->win = INVALID_WINDOW_VALUE;
Karsten Hopp 4451f1
! 	WIN_PYTHON_REF(win) = NULL;
Karsten Hopp 4451f1
      }
Karsten Hopp 4451f1
  }
Karsten Hopp 4451f1
  #endif
Karsten Hopp 4451f1
*** ../vim-7.3.936/src/if_python3.c	2013-05-06 04:21:35.000000000 +0200
Karsten Hopp 4451f1
--- src/if_python3.c	2013-05-12 18:31:20.000000000 +0200
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 621,626 ****
Karsten Hopp 4451f1
--- 621,629 ----
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
  #define DESTRUCTOR_FINISH(self) Py_TYPE(self)->tp_free((PyObject*)self);
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
+ #define WIN_PYTHON_REF(win) win->w_python3_ref
Karsten Hopp 4451f1
+ #define BUF_PYTHON_REF(buf) buf->b_python3_ref
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
      static void
Karsten Hopp 4451f1
  call_PyObject_Free(void *p)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 1067,1112 ****
Karsten Hopp 4451f1
  };
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
! /* Buffer object - Definitions
Karsten Hopp 4451f1
   */
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      static PyObject *
Karsten Hopp 4451f1
- BufferNew(buf_T *buf)
Karsten Hopp 4451f1
- {
Karsten Hopp 4451f1
-     /* We need to handle deletion of buffers underneath us.
Karsten Hopp 4451f1
-      * If we add a "b_python3_ref" field to the buf_T structure,
Karsten Hopp 4451f1
-      * then we can get at it in buf_freeall() in vim. We then
Karsten Hopp 4451f1
-      * need to create only ONE Python object per buffer - if
Karsten Hopp 4451f1
-      * we try to create a second, just INCREF the existing one
Karsten Hopp 4451f1
-      * and return it. The (single) Python object referring to
Karsten Hopp 4451f1
-      * the buffer is stored in "b_python3_ref".
Karsten Hopp 4451f1
-      * Question: what to do on a buf_freeall(). We'll probably
Karsten Hopp 4451f1
-      * have to either delete the Python object (DECREF it to
Karsten Hopp 4451f1
-      * zero - a bad idea, as it leaves dangling refs!) or
Karsten Hopp 4451f1
-      * set the buf_T * value to an invalid value (-1?), which
Karsten Hopp 4451f1
-      * means we need checks in all access functions... Bah.
Karsten Hopp 4451f1
-      */
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     BufferObject *self;
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     if (buf->b_python3_ref != NULL)
Karsten Hopp 4451f1
-     {
Karsten Hopp 4451f1
- 	self = buf->b_python3_ref;
Karsten Hopp 4451f1
- 	Py_INCREF(self);
Karsten Hopp 4451f1
-     }
Karsten Hopp 4451f1
-     else
Karsten Hopp 4451f1
-     {
Karsten Hopp 4451f1
- 	self = PyObject_NEW(BufferObject, &BufferType);
Karsten Hopp 4451f1
- 	buf->b_python3_ref = self;
Karsten Hopp 4451f1
- 	if (self == NULL)
Karsten Hopp 4451f1
- 	    return NULL;
Karsten Hopp 4451f1
- 	self->buf = buf;
Karsten Hopp 4451f1
-     }
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     return (PyObject *)(self);
Karsten Hopp 4451f1
- }
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     static PyObject *
Karsten Hopp 4451f1
  BufferGetattro(PyObject *self, PyObject*nameobj)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
      PyObject *r;
Karsten Hopp 4451f1
--- 1070,1079 ----
Karsten Hopp 4451f1
  };
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
! /* Buffer object
Karsten Hopp 4451f1
   */
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      static PyObject *
Karsten Hopp 4451f1
  BufferGetattro(PyObject *self, PyObject*nameobj)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
      PyObject *r;
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 1132,1160 ****
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
  /******************/
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
-     static Py_ssize_t
Karsten Hopp 4451f1
- BufferLength(PyObject *self)
Karsten Hopp 4451f1
- {
Karsten Hopp 4451f1
-     if (CheckBuffer((BufferObject *)(self)))
Karsten Hopp 4451f1
- 	return -1;
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     return (Py_ssize_t)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
Karsten Hopp 4451f1
- }
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     static PyObject *
Karsten Hopp 4451f1
- BufferItem(PyObject *self, Py_ssize_t n)
Karsten Hopp 4451f1
- {
Karsten Hopp 4451f1
-     return RBItem((BufferObject *)(self), n, 1,
Karsten Hopp 4451f1
- 	       (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Karsten Hopp 4451f1
- }
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     static PyObject *
Karsten Hopp 4451f1
- BufferSlice(PyObject *self, Py_ssize_t lo, Py_ssize_t hi)
Karsten Hopp 4451f1
- {
Karsten Hopp 4451f1
-     return RBSlice((BufferObject *)(self), lo, hi, 1,
Karsten Hopp 4451f1
- 	       (Py_ssize_t)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Karsten Hopp 4451f1
- }
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
      static PyObject *
Karsten Hopp 4451f1
  BufferSubscript(PyObject *self, PyObject* idx)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
--- 1099,1104 ----
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 1342,1381 ****
Karsten Hopp 4451f1
   */
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      static PyObject *
Karsten Hopp 4451f1
- WindowNew(win_T *win)
Karsten Hopp 4451f1
- {
Karsten Hopp 4451f1
-     /* We need to handle deletion of windows underneath us.
Karsten Hopp 4451f1
-      * If we add a "w_python3_ref" field to the win_T structure,
Karsten Hopp 4451f1
-      * then we can get at it in win_free() in vim. We then
Karsten Hopp 4451f1
-      * need to create only ONE Python object per window - if
Karsten Hopp 4451f1
-      * we try to create a second, just INCREF the existing one
Karsten Hopp 4451f1
-      * and return it. The (single) Python object referring to
Karsten Hopp 4451f1
-      * the window is stored in "w_python3_ref".
Karsten Hopp 4451f1
-      * On a win_free() we set the Python object's win_T* field
Karsten Hopp 4451f1
-      * to an invalid value. We trap all uses of a window
Karsten Hopp 4451f1
-      * object, and reject them if the win_T* field is invalid.
Karsten Hopp 4451f1
-      */
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     WindowObject *self;
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     if (win->w_python3_ref)
Karsten Hopp 4451f1
-     {
Karsten Hopp 4451f1
- 	self = win->w_python3_ref;
Karsten Hopp 4451f1
- 	Py_INCREF(self);
Karsten Hopp 4451f1
-     }
Karsten Hopp 4451f1
-     else
Karsten Hopp 4451f1
-     {
Karsten Hopp 4451f1
- 	self = PyObject_NEW(WindowObject, &WindowType);
Karsten Hopp 4451f1
- 	if (self == NULL)
Karsten Hopp 4451f1
- 	    return NULL;
Karsten Hopp 4451f1
- 	self->win = win;
Karsten Hopp 4451f1
- 	win->w_python3_ref = self;
Karsten Hopp 4451f1
-     }
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     return (PyObject *)(self);
Karsten Hopp 4451f1
- }
Karsten Hopp 4451f1
- 
Karsten Hopp 4451f1
-     static PyObject *
Karsten Hopp 4451f1
  WindowGetattro(PyObject *self, PyObject *nameobj)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
      PyObject *r;
Karsten Hopp 4451f1
--- 1286,1291 ----
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 1575,1585 ****
Karsten Hopp 4451f1
      void
Karsten Hopp 4451f1
  python3_buffer_free(buf_T *buf)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
!     if (buf->b_python3_ref != NULL)
Karsten Hopp 4451f1
      {
Karsten Hopp 4451f1
! 	BufferObject *bp = buf->b_python3_ref;
Karsten Hopp 4451f1
  	bp->buf = INVALID_BUFFER_VALUE;
Karsten Hopp 4451f1
! 	buf->b_python3_ref = NULL;
Karsten Hopp 4451f1
      }
Karsten Hopp 4451f1
  }
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
--- 1485,1495 ----
Karsten Hopp 4451f1
      void
Karsten Hopp 4451f1
  python3_buffer_free(buf_T *buf)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
!     if (BUF_PYTHON_REF(buf) != NULL)
Karsten Hopp 4451f1
      {
Karsten Hopp 4451f1
! 	BufferObject *bp = BUF_PYTHON_REF(buf);
Karsten Hopp 4451f1
  	bp->buf = INVALID_BUFFER_VALUE;
Karsten Hopp 4451f1
! 	BUF_PYTHON_REF(buf) = NULL;
Karsten Hopp 4451f1
      }
Karsten Hopp 4451f1
  }
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 1587,1597 ****
Karsten Hopp 4451f1
      void
Karsten Hopp 4451f1
  python3_window_free(win_T *win)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
!     if (win->w_python3_ref != NULL)
Karsten Hopp 4451f1
      {
Karsten Hopp 4451f1
! 	WindowObject *wp = win->w_python3_ref;
Karsten Hopp 4451f1
  	wp->win = INVALID_WINDOW_VALUE;
Karsten Hopp 4451f1
! 	win->w_python3_ref = NULL;
Karsten Hopp 4451f1
      }
Karsten Hopp 4451f1
  }
Karsten Hopp 4451f1
  #endif
Karsten Hopp 4451f1
--- 1497,1507 ----
Karsten Hopp 4451f1
      void
Karsten Hopp 4451f1
  python3_window_free(win_T *win)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
!     if (WIN_PYTHON_REF(win) != NULL)
Karsten Hopp 4451f1
      {
Karsten Hopp 4451f1
! 	WindowObject *wp = WIN_PYTHON_REF(win);
Karsten Hopp 4451f1
  	wp->win = INVALID_WINDOW_VALUE;
Karsten Hopp 4451f1
! 	WIN_PYTHON_REF(win) = NULL;
Karsten Hopp 4451f1
      }
Karsten Hopp 4451f1
  }
Karsten Hopp 4451f1
  #endif
Karsten Hopp 4451f1
*** ../vim-7.3.936/src/if_py_both.h	2013-05-06 06:26:10.000000000 +0200
Karsten Hopp 4451f1
--- src/if_py_both.h	2013-05-12 18:31:20.000000000 +0200
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 1782,1792 ****
Karsten Hopp 4451f1
--- 1782,1832 ----
Karsten Hopp 4451f1
      return 0;
Karsten Hopp 4451f1
  }
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
+ /* Window object
Karsten Hopp 4451f1
+  */
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
  static int WindowSetattr(PyObject *, char *, PyObject *);
Karsten Hopp 4451f1
  static PyObject *WindowRepr(PyObject *);
Karsten Hopp 4451f1
  static PyTypeObject WindowType;
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      static PyObject *
Karsten Hopp 4451f1
+ WindowNew(win_T *win)
Karsten Hopp 4451f1
+ {
Karsten Hopp 4451f1
+     /* We need to handle deletion of windows underneath us.
Karsten Hopp 4451f1
+      * If we add a "w_python*_ref" field to the win_T structure,
Karsten Hopp 4451f1
+      * then we can get at it in win_free() in vim. We then
Karsten Hopp 4451f1
+      * need to create only ONE Python object per window - if
Karsten Hopp 4451f1
+      * we try to create a second, just INCREF the existing one
Karsten Hopp 4451f1
+      * and return it. The (single) Python object referring to
Karsten Hopp 4451f1
+      * the window is stored in "w_python*_ref".
Karsten Hopp 4451f1
+      * On a win_free() we set the Python object's win_T* field
Karsten Hopp 4451f1
+      * to an invalid value. We trap all uses of a window
Karsten Hopp 4451f1
+      * object, and reject them if the win_T* field is invalid.
Karsten Hopp 4451f1
+      *
Karsten Hopp 4451f1
+      * Python2 and Python3 get different fields and different objects: 
Karsten Hopp 4451f1
+      * w_python_ref and w_python3_ref fields respectively.
Karsten Hopp 4451f1
+      */
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
+     WindowObject *self;
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
+     if (WIN_PYTHON_REF(win))
Karsten Hopp 4451f1
+     {
Karsten Hopp 4451f1
+ 	self = WIN_PYTHON_REF(win);
Karsten Hopp 4451f1
+ 	Py_INCREF(self);
Karsten Hopp 4451f1
+     }
Karsten Hopp 4451f1
+     else
Karsten Hopp 4451f1
+     {
Karsten Hopp 4451f1
+ 	self = PyObject_NEW(WindowObject, &WindowType);
Karsten Hopp 4451f1
+ 	if (self == NULL)
Karsten Hopp 4451f1
+ 	    return NULL;
Karsten Hopp 4451f1
+ 	self->win = win;
Karsten Hopp 4451f1
+ 	WIN_PYTHON_REF(win) = self;
Karsten Hopp 4451f1
+     }
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
+     return (PyObject *)(self);
Karsten Hopp 4451f1
+ }
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
+     static PyObject *
Karsten Hopp 4451f1
  WindowAttr(WindowObject *this, char *name)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
      if (strcmp(name, "buffer") == 0)
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 1809,1815 ****
Karsten Hopp 4451f1
  	return OptionsNew(SREQ_WIN, this->win, (checkfun) CheckWindow,
Karsten Hopp 4451f1
  			(PyObject *) this);
Karsten Hopp 4451f1
      else if (strcmp(name,"__members__") == 0)
Karsten Hopp 4451f1
! 	return Py_BuildValue("[sssss]", "buffer", "cursor", "height", "vars",
Karsten Hopp 4451f1
  		"options");
Karsten Hopp 4451f1
      else
Karsten Hopp 4451f1
  	return NULL;
Karsten Hopp 4451f1
--- 1849,1855 ----
Karsten Hopp 4451f1
  	return OptionsNew(SREQ_WIN, this->win, (checkfun) CheckWindow,
Karsten Hopp 4451f1
  			(PyObject *) this);
Karsten Hopp 4451f1
      else if (strcmp(name,"__members__") == 0)
Karsten Hopp 4451f1
! 	return Py_BuildValue("[ssssss]", "buffer", "cursor", "height", "vars",
Karsten Hopp 4451f1
  		"options");
Karsten Hopp 4451f1
      else
Karsten Hopp 4451f1
  	return NULL;
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 1821,1831 ****
Karsten Hopp 4451f1
      WindowObject *this = (WindowObject *)(self);
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      if (this->win && this->win != INVALID_WINDOW_VALUE)
Karsten Hopp 4451f1
! #if PY_MAJOR_VERSION >= 3
Karsten Hopp 4451f1
! 	this->win->w_python3_ref = NULL;
Karsten Hopp 4451f1
! #else
Karsten Hopp 4451f1
! 	this->win->w_python_ref = NULL;
Karsten Hopp 4451f1
! #endif
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      DESTRUCTOR_FINISH(self);
Karsten Hopp 4451f1
  }
Karsten Hopp 4451f1
--- 1861,1867 ----
Karsten Hopp 4451f1
      WindowObject *this = (WindowObject *)(self);
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      if (this->win && this->win != INVALID_WINDOW_VALUE)
Karsten Hopp 4451f1
! 	WIN_PYTHON_REF(this->win) = NULL;
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      DESTRUCTOR_FINISH(self);
Karsten Hopp 4451f1
  }
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 2756,2771 ****
Karsten Hopp 4451f1
      BufferObject *this = (BufferObject *)(self);
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Karsten Hopp 4451f1
! #if PY_MAJOR_VERSION >= 3
Karsten Hopp 4451f1
! 	this->buf->b_python3_ref = NULL;
Karsten Hopp 4451f1
! #else
Karsten Hopp 4451f1
! 	this->buf->b_python_ref = NULL;
Karsten Hopp 4451f1
! #endif
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      DESTRUCTOR_FINISH(self);
Karsten Hopp 4451f1
  }
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      static PyObject *
Karsten Hopp 4451f1
  BufferAttr(BufferObject *this, char *name)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
      if (strcmp(name, "name") == 0)
Karsten Hopp 4451f1
--- 2792,2842 ----
Karsten Hopp 4451f1
      BufferObject *this = (BufferObject *)(self);
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      if (this->buf && this->buf != INVALID_BUFFER_VALUE)
Karsten Hopp 4451f1
! 	BUF_PYTHON_REF(this->buf) = NULL;
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      DESTRUCTOR_FINISH(self);
Karsten Hopp 4451f1
  }
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
      static PyObject *
Karsten Hopp 4451f1
+ BufferNew(buf_T *buf)
Karsten Hopp 4451f1
+ {
Karsten Hopp 4451f1
+     /* We need to handle deletion of buffers underneath us.
Karsten Hopp 4451f1
+      * If we add a "b_python*_ref" field to the buf_T structure,
Karsten Hopp 4451f1
+      * then we can get at it in buf_freeall() in vim. We then
Karsten Hopp 4451f1
+      * need to create only ONE Python object per buffer - if
Karsten Hopp 4451f1
+      * we try to create a second, just INCREF the existing one
Karsten Hopp 4451f1
+      * and return it. The (single) Python object referring to
Karsten Hopp 4451f1
+      * the buffer is stored in "b_python*_ref".
Karsten Hopp 4451f1
+      * Question: what to do on a buf_freeall(). We'll probably
Karsten Hopp 4451f1
+      * have to either delete the Python object (DECREF it to
Karsten Hopp 4451f1
+      * zero - a bad idea, as it leaves dangling refs!) or
Karsten Hopp 4451f1
+      * set the buf_T * value to an invalid value (-1?), which
Karsten Hopp 4451f1
+      * means we need checks in all access functions... Bah.
Karsten Hopp 4451f1
+      *
Karsten Hopp 4451f1
+      * Python2 and Python3 get different fields and different objects: 
Karsten Hopp 4451f1
+      * b_python_ref and b_python3_ref fields respectively.
Karsten Hopp 4451f1
+      */
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
+     BufferObject *self;
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
+     if (BUF_PYTHON_REF(buf) != NULL)
Karsten Hopp 4451f1
+     {
Karsten Hopp 4451f1
+ 	self = BUF_PYTHON_REF(buf);
Karsten Hopp 4451f1
+ 	Py_INCREF(self);
Karsten Hopp 4451f1
+     }
Karsten Hopp 4451f1
+     else
Karsten Hopp 4451f1
+     {
Karsten Hopp 4451f1
+ 	self = PyObject_NEW(BufferObject, &BufferType);
Karsten Hopp 4451f1
+ 	if (self == NULL)
Karsten Hopp 4451f1
+ 	    return NULL;
Karsten Hopp 4451f1
+ 	self->buf = buf;
Karsten Hopp 4451f1
+ 	BUF_PYTHON_REF(buf) = self;
Karsten Hopp 4451f1
+     }
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
+     return (PyObject *)(self);
Karsten Hopp 4451f1
+ }
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
+     static PyObject *
Karsten Hopp 4451f1
  BufferAttr(BufferObject *this, char *name)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
      if (strcmp(name, "name") == 0)
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 2783,2788 ****
Karsten Hopp 4451f1
--- 2854,2883 ----
Karsten Hopp 4451f1
  	return NULL;
Karsten Hopp 4451f1
  }
Karsten Hopp 4451f1
  
Karsten Hopp 4451f1
+     static PyInt
Karsten Hopp 4451f1
+ BufferLength(PyObject *self)
Karsten Hopp 4451f1
+ {
Karsten Hopp 4451f1
+     /* HOW DO WE SIGNAL AN ERROR FROM THIS FUNCTION? */
Karsten Hopp 4451f1
+     if (CheckBuffer((BufferObject *)(self)))
Karsten Hopp 4451f1
+ 	return -1; /* ??? */
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
+     return (PyInt)(((BufferObject *)(self))->buf->b_ml.ml_line_count);
Karsten Hopp 4451f1
+ }
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
+     static PyObject *
Karsten Hopp 4451f1
+ BufferItem(PyObject *self, PyInt n)
Karsten Hopp 4451f1
+ {
Karsten Hopp 4451f1
+     return RBItem((BufferObject *)(self), n, 1,
Karsten Hopp 4451f1
+ 		  (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Karsten Hopp 4451f1
+ }
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
+     static PyObject *
Karsten Hopp 4451f1
+ BufferSlice(PyObject *self, PyInt lo, PyInt hi)
Karsten Hopp 4451f1
+ {
Karsten Hopp 4451f1
+     return RBSlice((BufferObject *)(self), lo, hi, 1,
Karsten Hopp 4451f1
+ 		   (PyInt)((BufferObject *)(self))->buf->b_ml.ml_line_count);
Karsten Hopp 4451f1
+ }
Karsten Hopp 4451f1
+ 
Karsten Hopp 4451f1
      static PyObject *
Karsten Hopp 4451f1
  BufferAppend(PyObject *self, PyObject *args)
Karsten Hopp 4451f1
  {
Karsten Hopp 4451f1
*** ../vim-7.3.936/src/version.c	2013-05-12 14:10:41.000000000 +0200
Karsten Hopp 4451f1
--- src/version.c	2013-05-12 18:44:01.000000000 +0200
Karsten Hopp 4451f1
***************
Karsten Hopp 4451f1
*** 730,731 ****
Karsten Hopp 4451f1
--- 730,733 ----
Karsten Hopp 4451f1
  {   /* Add new patch number below this line */
Karsten Hopp 4451f1
+ /**/
Karsten Hopp 4451f1
+     937,
Karsten Hopp 4451f1
  /**/
Karsten Hopp 4451f1
Karsten Hopp 4451f1
-- 
Karsten Hopp 4451f1
ARTHUR:  Well, I can't just call you `Man'.
Karsten Hopp 4451f1
DENNIS:  Well, you could say `Dennis'.
Karsten Hopp 4451f1
ARTHUR:  Well, I didn't know you were called `Dennis.'
Karsten Hopp 4451f1
DENNIS:  Well, you didn't bother to find out, did you?
Karsten Hopp 4451f1
                                  The Quest for the Holy Grail (Monty Python)
Karsten Hopp 4451f1
Karsten Hopp 4451f1
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
Karsten Hopp 4451f1
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
Karsten Hopp 4451f1
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
Karsten Hopp 4451f1
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///