Karsten Hopp 951116
To: vim_dev@googlegroups.com
Karsten Hopp 951116
Subject: Patch 7.3.1228
Karsten Hopp 951116
Fcc: outbox
Karsten Hopp 951116
From: Bram Moolenaar <Bram@moolenaar.net>
Karsten Hopp 951116
Mime-Version: 1.0
Karsten Hopp 951116
Content-Type: text/plain; charset=UTF-8
Karsten Hopp 951116
Content-Transfer-Encoding: 8bit
Karsten Hopp 951116
------------
Karsten Hopp 951116
Karsten Hopp 951116
Patch 7.3.1228
Karsten Hopp 951116
Problem:    Python: various inconsistencies and problems.
Karsten Hopp 951116
Solution:   StringToLine now supports both bytes() and unicode() objects.
Karsten Hopp 951116
	    Make function names consistant.  Fix memory leak fixed in
Karsten Hopp 951116
	    StringToLine. (ZyX)
Karsten Hopp 951116
Files:	    src/if_py_both.h, src/if_python3.c, src/if_python.c
Karsten Hopp 951116
Karsten Hopp 951116
Karsten Hopp 951116
*** ../vim-7.3.1227/src/if_py_both.h	2013-06-23 13:00:40.000000000 +0200
Karsten Hopp 951116
--- src/if_py_both.h	2013-06-23 13:08:18.000000000 +0200
Karsten Hopp 951116
***************
Karsten Hopp 951116
*** 18,24 ****
Karsten Hopp 951116
  #endif
Karsten Hopp 951116
  
Karsten Hopp 951116
  #ifdef FEAT_MBYTE
Karsten Hopp 951116
! # define ENC_OPT p_enc
Karsten Hopp 951116
  #else
Karsten Hopp 951116
  # define ENC_OPT "latin1"
Karsten Hopp 951116
  #endif
Karsten Hopp 951116
--- 18,24 ----
Karsten Hopp 951116
  #endif
Karsten Hopp 951116
  
Karsten Hopp 951116
  #ifdef FEAT_MBYTE
Karsten Hopp 951116
! # define ENC_OPT ((char *)p_enc)
Karsten Hopp 951116
  #else
Karsten Hopp 951116
  # define ENC_OPT "latin1"
Karsten Hopp 951116
  #endif
Karsten Hopp 951116
***************
Karsten Hopp 951116
*** 92,119 ****
Karsten Hopp 951116
  StringToChars(PyObject *object, PyObject **todecref)
Karsten Hopp 951116
  {
Karsten Hopp 951116
      char_u	*p;
Karsten Hopp 951116
-     PyObject	*bytes = NULL;
Karsten Hopp 951116
  
Karsten Hopp 951116
      if (PyBytes_Check(object))
Karsten Hopp 951116
      {
Karsten Hopp 951116
  
Karsten Hopp 951116
! 	if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
Karsten Hopp 951116
! 	    return NULL;
Karsten Hopp 951116
! 	if (p == NULL)
Karsten Hopp 951116
  	    return NULL;
Karsten Hopp 951116
  
Karsten Hopp 951116
  	*todecref = NULL;
Karsten Hopp 951116
      }
Karsten Hopp 951116
      else if (PyUnicode_Check(object))
Karsten Hopp 951116
      {
Karsten Hopp 951116
! 	bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
Karsten Hopp 951116
! 	if (bytes == NULL)
Karsten Hopp 951116
! 	    return NULL;
Karsten Hopp 951116
  
Karsten Hopp 951116
! 	if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
Karsten Hopp 951116
  	    return NULL;
Karsten Hopp 951116
! 	if (p == NULL)
Karsten Hopp 951116
  	    return NULL;
Karsten Hopp 951116
  
Karsten Hopp 951116
  	*todecref = bytes;
Karsten Hopp 951116
      }
Karsten Hopp 951116
--- 92,120 ----
Karsten Hopp 951116
  StringToChars(PyObject *object, PyObject **todecref)
Karsten Hopp 951116
  {
Karsten Hopp 951116
      char_u	*p;
Karsten Hopp 951116
  
Karsten Hopp 951116
      if (PyBytes_Check(object))
Karsten Hopp 951116
      {
Karsten Hopp 951116
  
Karsten Hopp 951116
! 	if (PyBytes_AsStringAndSize(object, (char **) &p, NULL) == -1
Karsten Hopp 951116
! 		|| p == NULL)
Karsten Hopp 951116
  	    return NULL;
Karsten Hopp 951116
  
Karsten Hopp 951116
  	*todecref = NULL;
Karsten Hopp 951116
      }
Karsten Hopp 951116
      else if (PyUnicode_Check(object))
Karsten Hopp 951116
      {
Karsten Hopp 951116
! 	PyObject	*bytes;
Karsten Hopp 951116
  
Karsten Hopp 951116
! 	if (!(bytes = PyUnicode_AsEncodedString(object, ENC_OPT, NULL)))
Karsten Hopp 951116
  	    return NULL;
Karsten Hopp 951116
! 
Karsten Hopp 951116
! 	if(PyBytes_AsStringAndSize(bytes, (char **) &p, NULL) == -1
Karsten Hopp 951116
! 		|| p == NULL)
Karsten Hopp 951116
! 	{
Karsten Hopp 951116
! 	    Py_DECREF(bytes);
Karsten Hopp 951116
  	    return NULL;
Karsten Hopp 951116
+ 	}
Karsten Hopp 951116
  
Karsten Hopp 951116
  	*todecref = bytes;
Karsten Hopp 951116
      }
Karsten Hopp 951116
***************
Karsten Hopp 951116
*** 133,138 ****
Karsten Hopp 951116
--- 134,140 ----
Karsten Hopp 951116
  
Karsten Hopp 951116
      if (!(string = PyString_FromString(s)))
Karsten Hopp 951116
  	return -1;
Karsten Hopp 951116
+ 
Karsten Hopp 951116
      if (PyList_Append(list, string))
Karsten Hopp 951116
      {
Karsten Hopp 951116
  	Py_DECREF(string);
Karsten Hopp 951116
***************
Karsten Hopp 951116
*** 534,543 ****
Karsten Hopp 951116
      }
Karsten Hopp 951116
  
Karsten Hopp 951116
      if (our_tv->v_type == VAR_STRING)
Karsten Hopp 951116
-     {
Karsten Hopp 951116
  	result = PyString_FromString(our_tv->vval.v_string == NULL
Karsten Hopp 951116
  					? "" : (char *)our_tv->vval.v_string);
Karsten Hopp 951116
-     }
Karsten Hopp 951116
      else if (our_tv->v_type == VAR_NUMBER)
Karsten Hopp 951116
      {
Karsten Hopp 951116
  	char buf[NUMBUFLEN];
Karsten Hopp 951116
--- 536,543 ----
Karsten Hopp 951116
***************
Karsten Hopp 951116
*** 3385,3406 ****
Karsten Hopp 951116
      static char *
Karsten Hopp 951116
  StringToLine(PyObject *obj)
Karsten Hopp 951116
  {
Karsten Hopp 951116
!     const char *str;
Karsten Hopp 951116
!     char *save;
Karsten Hopp 951116
!     PyObject *bytes;
Karsten Hopp 951116
!     PyInt len;
Karsten Hopp 951116
!     PyInt i;
Karsten Hopp 951116
!     char *p;
Karsten Hopp 951116
  
Karsten Hopp 951116
!     if (obj == NULL || !PyString_Check(obj))
Karsten Hopp 951116
      {
Karsten Hopp 951116
! 	PyErr_BadArgument();
Karsten Hopp 951116
! 	return NULL;
Karsten Hopp 951116
      }
Karsten Hopp 951116
  
Karsten Hopp 951116
!     bytes = PyString_AsBytes(obj);  /* for Python 2 this does nothing */
Karsten Hopp 951116
!     str = PyString_AsString(bytes);
Karsten Hopp 951116
!     len = PyString_Size(bytes);
Karsten Hopp 951116
  
Karsten Hopp 951116
      /*
Karsten Hopp 951116
       * Error checking: String must not contain newlines, as we
Karsten Hopp 951116
--- 3385,3415 ----
Karsten Hopp 951116
      static char *
Karsten Hopp 951116
  StringToLine(PyObject *obj)
Karsten Hopp 951116
  {
Karsten Hopp 951116
!     char	*str;
Karsten Hopp 951116
!     char	*save;
Karsten Hopp 951116
!     PyObject	*bytes = NULL;
Karsten Hopp 951116
!     Py_ssize_t	len;
Karsten Hopp 951116
!     PyInt	i;
Karsten Hopp 951116
!     char	*p;
Karsten Hopp 951116
  
Karsten Hopp 951116
!     if (PyBytes_Check(obj))
Karsten Hopp 951116
      {
Karsten Hopp 951116
! 	if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
Karsten Hopp 951116
! 		|| str == NULL)
Karsten Hopp 951116
! 	    return NULL;
Karsten Hopp 951116
      }
Karsten Hopp 951116
+     else if (PyUnicode_Check(obj))
Karsten Hopp 951116
+     {
Karsten Hopp 951116
+ 	if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
Karsten Hopp 951116
+ 	    return NULL;
Karsten Hopp 951116
  
Karsten Hopp 951116
! 	if(PyBytes_AsStringAndSize(bytes, &str, &len) == -1
Karsten Hopp 951116
! 		|| str == NULL)
Karsten Hopp 951116
! 	{
Karsten Hopp 951116
! 	    Py_DECREF(bytes);
Karsten Hopp 951116
! 	    return NULL;
Karsten Hopp 951116
! 	}
Karsten Hopp 951116
!     }
Karsten Hopp 951116
  
Karsten Hopp 951116
      /*
Karsten Hopp 951116
       * Error checking: String must not contain newlines, as we
Karsten Hopp 951116
***************
Karsten Hopp 951116
*** 3439,3445 ****
Karsten Hopp 951116
      }
Karsten Hopp 951116
  
Karsten Hopp 951116
      save[i] = '\0';
Karsten Hopp 951116
!     PyString_FreeBytes(bytes);  /* Python 2 does nothing here */
Karsten Hopp 951116
  
Karsten Hopp 951116
      return save;
Karsten Hopp 951116
  }
Karsten Hopp 951116
--- 3448,3454 ----
Karsten Hopp 951116
      }
Karsten Hopp 951116
  
Karsten Hopp 951116
      save[i] = '\0';
Karsten Hopp 951116
!     Py_XDECREF(bytes);  /* Python 2 does nothing here */
Karsten Hopp 951116
  
Karsten Hopp 951116
      return save;
Karsten Hopp 951116
  }
Karsten Hopp 951116
***************
Karsten Hopp 951116
*** 3568,3577 ****
Karsten Hopp 951116
  
Karsten Hopp 951116
  	return OK;
Karsten Hopp 951116
      }
Karsten Hopp 951116
!     else if (PyString_Check(line))
Karsten Hopp 951116
      {
Karsten Hopp 951116
! 	char *save = StringToLine(line);
Karsten Hopp 951116
! 	buf_T *savebuf;
Karsten Hopp 951116
  
Karsten Hopp 951116
  	if (save == NULL)
Karsten Hopp 951116
  	    return FAIL;
Karsten Hopp 951116
--- 3577,3586 ----
Karsten Hopp 951116
  
Karsten Hopp 951116
  	return OK;
Karsten Hopp 951116
      }
Karsten Hopp 951116
!     else if (PyBytes_Check(line) || PyUnicode_Check(line))
Karsten Hopp 951116
      {
Karsten Hopp 951116
! 	char	*save = StringToLine(line);
Karsten Hopp 951116
! 	buf_T	*savebuf;
Karsten Hopp 951116
  
Karsten Hopp 951116
  	if (save == NULL)
Karsten Hopp 951116
  	    return FAIL;
Karsten Hopp 951116
***************
Karsten Hopp 951116
*** 3821,3827 ****
Karsten Hopp 951116
      /* First of all, we check the type of the supplied Python object.
Karsten Hopp 951116
       * It must be a string or a list, or the call is in error.
Karsten Hopp 951116
       */
Karsten Hopp 951116
!     if (PyString_Check(lines))
Karsten Hopp 951116
      {
Karsten Hopp 951116
  	char	*str = StringToLine(lines);
Karsten Hopp 951116
  	buf_T	*savebuf;
Karsten Hopp 951116
--- 3830,3836 ----
Karsten Hopp 951116
      /* First of all, we check the type of the supplied Python object.
Karsten Hopp 951116
       * It must be a string or a list, or the call is in error.
Karsten Hopp 951116
       */
Karsten Hopp 951116
!     if (PyBytes_Check(lines) || PyUnicode_Check(lines))
Karsten Hopp 951116
      {
Karsten Hopp 951116
  	char	*str = StringToLine(lines);
Karsten Hopp 951116
  	buf_T	*savebuf;
Karsten Hopp 951116
***************
Karsten Hopp 951116
*** 5254,5260 ****
Karsten Hopp 951116
      {
Karsten Hopp 951116
  	char_u	*result;
Karsten Hopp 951116
  
Karsten Hopp 951116
! 	if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
Karsten Hopp 951116
  	    return -1;
Karsten Hopp 951116
  	if (result == NULL)
Karsten Hopp 951116
  	    return -1;
Karsten Hopp 951116
--- 5263,5269 ----
Karsten Hopp 951116
      {
Karsten Hopp 951116
  	char_u	*result;
Karsten Hopp 951116
  
Karsten Hopp 951116
! 	if (PyBytes_AsStringAndSize(obj, (char **) &result, NULL) == -1)
Karsten Hopp 951116
  	    return -1;
Karsten Hopp 951116
  	if (result == NULL)
Karsten Hopp 951116
  	    return -1;
Karsten Hopp 951116
***************
Karsten Hopp 951116
*** 5269,5279 ****
Karsten Hopp 951116
  	PyObject	*bytes;
Karsten Hopp 951116
  	char_u	*result;
Karsten Hopp 951116
  
Karsten Hopp 951116
! 	bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
Karsten Hopp 951116
  	if (bytes == NULL)
Karsten Hopp 951116
  	    return -1;
Karsten Hopp 951116
  
Karsten Hopp 951116
! 	if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
Karsten Hopp 951116
  	    return -1;
Karsten Hopp 951116
  	if (result == NULL)
Karsten Hopp 951116
  	    return -1;
Karsten Hopp 951116
--- 5278,5288 ----
Karsten Hopp 951116
  	PyObject	*bytes;
Karsten Hopp 951116
  	char_u	*result;
Karsten Hopp 951116
  
Karsten Hopp 951116
! 	bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
Karsten Hopp 951116
  	if (bytes == NULL)
Karsten Hopp 951116
  	    return -1;
Karsten Hopp 951116
  
Karsten Hopp 951116
! 	if(PyBytes_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
Karsten Hopp 951116
  	    return -1;
Karsten Hopp 951116
  	if (result == NULL)
Karsten Hopp 951116
  	    return -1;
Karsten Hopp 951116
*** ../vim-7.3.1227/src/if_python3.c	2013-06-13 20:57:44.000000000 +0200
Karsten Hopp 951116
--- src/if_python3.c	2013-06-23 13:08:18.000000000 +0200
Karsten Hopp 951116
***************
Karsten Hopp 951116
*** 84,96 ****
Karsten Hopp 951116
  
Karsten Hopp 951116
  #define PyInt Py_ssize_t
Karsten Hopp 951116
  #define PyString_Check(obj) PyUnicode_Check(obj)
Karsten Hopp 951116
- #define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, CODEC_ERROR_HANDLER)
Karsten Hopp 951116
- #define PyString_FreeBytes(obj) Py_XDECREF(bytes)
Karsten Hopp 951116
- #define PyString_AsString(obj) PyBytes_AsString(obj)
Karsten Hopp 951116
- #define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
Karsten Hopp 951116
  #define PyString_FromString(repr) PyUnicode_FromString(repr)
Karsten Hopp 951116
  #define PyString_FromFormat PyUnicode_FromFormat
Karsten Hopp 951116
- #define PyString_AsStringAndSize(obj, buffer, len) PyBytes_AsStringAndSize(obj, buffer, len)
Karsten Hopp 951116
  #define PyInt_Check(obj) PyLong_Check(obj)
Karsten Hopp 951116
  #define PyInt_FromLong(i) PyLong_FromLong(i)
Karsten Hopp 951116
  #define PyInt_AsLong(obj) PyLong_AsLong(obj)
Karsten Hopp 951116
--- 84,91 ----
Karsten Hopp 951116
***************
Karsten Hopp 951116
*** 357,363 ****
Karsten Hopp 951116
  # endif
Karsten Hopp 951116
  static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
Karsten Hopp 951116
  static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Karsten Hopp 951116
! static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length);
Karsten Hopp 951116
  static PyObject* (*py3_PyBytes_FromString)(char *str);
Karsten Hopp 951116
  static PyObject* (*py3_PyFloat_FromDouble)(double num);
Karsten Hopp 951116
  static double (*py3_PyFloat_AsDouble)(PyObject *);
Karsten Hopp 951116
--- 352,358 ----
Karsten Hopp 951116
  # endif
Karsten Hopp 951116
  static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
Karsten Hopp 951116
  static char* (*py3_PyBytes_AsString)(PyObject *bytes);
Karsten Hopp 951116
! static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
Karsten Hopp 951116
  static PyObject* (*py3_PyBytes_FromString)(char *str);
Karsten Hopp 951116
  static PyObject* (*py3_PyFloat_FromDouble)(double num);
Karsten Hopp 951116
  static double (*py3_PyFloat_AsDouble)(PyObject *);
Karsten Hopp 951116
*** ../vim-7.3.1227/src/if_python.c	2013-06-12 14:40:58.000000000 +0200
Karsten Hopp 951116
--- src/if_python.c	2013-06-23 13:08:18.000000000 +0200
Karsten Hopp 951116
***************
Karsten Hopp 951116
*** 68,79 ****
Karsten Hopp 951116
  #undef main /* Defined in python.h - aargh */
Karsten Hopp 951116
  #undef HAVE_FCNTL_H /* Clash with os_win32.h */
Karsten Hopp 951116
  
Karsten Hopp 951116
! #define PyBytes_FromString PyString_FromString
Karsten Hopp 951116
! #define PyBytes_Check PyString_Check
Karsten Hopp 951116
! 
Karsten Hopp 951116
! /* No-op conversion functions, use with care! */
Karsten Hopp 951116
! #define PyString_AsBytes(obj) (obj)
Karsten Hopp 951116
! #define PyString_FreeBytes(obj)
Karsten Hopp 951116
  
Karsten Hopp 951116
  #if !defined(FEAT_PYTHON) && defined(PROTO)
Karsten Hopp 951116
  /* Use this to be able to generate prototypes without python being used. */
Karsten Hopp 951116
--- 68,76 ----
Karsten Hopp 951116
  #undef main /* Defined in python.h - aargh */
Karsten Hopp 951116
  #undef HAVE_FCNTL_H /* Clash with os_win32.h */
Karsten Hopp 951116
  
Karsten Hopp 951116
! #define PyBytes_FromString      PyString_FromString
Karsten Hopp 951116
! #define PyBytes_Check           PyString_Check
Karsten Hopp 951116
! #define PyBytes_AsStringAndSize PyString_AsStringAndSize
Karsten Hopp 951116
  
Karsten Hopp 951116
  #if !defined(FEAT_PYTHON) && defined(PROTO)
Karsten Hopp 951116
  /* Use this to be able to generate prototypes without python being used. */
Karsten Hopp 951116
*** ../vim-7.3.1227/src/version.c	2013-06-23 13:00:40.000000000 +0200
Karsten Hopp 951116
--- src/version.c	2013-06-23 13:07:40.000000000 +0200
Karsten Hopp 951116
***************
Karsten Hopp 951116
*** 730,731 ****
Karsten Hopp 951116
--- 730,733 ----
Karsten Hopp 951116
  {   /* Add new patch number below this line */
Karsten Hopp 951116
+ /**/
Karsten Hopp 951116
+     1228,
Karsten Hopp 951116
  /**/
Karsten Hopp 951116
Karsten Hopp 951116
-- 
Karsten Hopp 951116
       In war we're tough and able.
Karsten Hopp 951116
       Quite indefatigable
Karsten Hopp 951116
       Between our quests
Karsten Hopp 951116
       We sequin vests
Karsten Hopp 951116
       And impersonate Clark Gable
Karsten Hopp 951116
       It's a busy life in Camelot.
Karsten Hopp 951116
       I have to push the pram a lot.
Karsten Hopp 951116
                 "Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
Karsten Hopp 951116
Karsten Hopp 951116
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
Karsten Hopp 951116
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
Karsten Hopp 951116
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
Karsten Hopp 951116
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///