# HG changeset patch
# User ZyX <[email protected]>
# Date 1371223647 -14400
#      Fri Jun 14 19:27:27 2013 +0400
# Branch python-fixes
# Node ID b9d4dfa09951d4fb75972df34802675edf24a17e
# Parent  c40e953fe93696087c7f0a4e2b7fb25e2d2ad60c
More consistency changes:

- StringToLine now supports both bytes() and unicode() objects
- In if_py_both.h PyBytes_* points to bytes()-manipulating functions,
                  PyString_* points to bytes() (python 2) or unicode()
                                    manipulating functions
                  PyUnicode_* points to unicode() manipulating functions
  It may not be so in if_python*.c.

* Memory leak fixed in StringToLine in case conversion bytes to char* failed.
* Both PyBytes_AsStringAndSize failure checks were squashed.
* ENC_OPT now always has char* type.

(note: bytes() is str() on python 2; unicode() is str() on python 3)

diff -r c40e953fe936 -r b9d4dfa09951 src/if_py_both.h
--- a/src/if_py_both.h  Fri Jun 14 19:14:52 2013 +0400
+++ b/src/if_py_both.h  Fri Jun 14 19:27:27 2013 +0400
@@ -18,7 +18,7 @@
 #endif
 
 #ifdef FEAT_MBYTE
-# define ENC_OPT p_enc
+# define ENC_OPT ((char *)p_enc)
 #else
 # define ENC_OPT "latin1"
 #endif
@@ -92,28 +92,29 @@
 StringToChars(PyObject *object, PyObject **todecref)
 {
     char_u     *p;
-    PyObject   *bytes = NULL;
 
     if (PyBytes_Check(object))
     {
 
-       if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
+       if (PyBytes_AsStringAndSize(object, (char **) &p, NULL) == -1
+               || p == NULL)
            return NULL;
-       if (p == NULL)
+
+       *todecref = NULL;
+    }
+    else if (PyUnicode_Check(object))
+    {
+       PyObject        *bytes;
+
+       if (!(bytes = PyUnicode_AsEncodedString(object, ENC_OPT, NULL)))
            return NULL;
 
-       *todecref = NULL;
-    }
-    else if (PyUnicode_Check(object))
-    {
-       bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
-       if (bytes == NULL)
+       if(PyBytes_AsStringAndSize(bytes, (char **) &p, NULL) == -1
+               || p == NULL)
+       {
+           Py_DECREF(bytes);
            return NULL;
-
-       if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
-           return NULL;
-       if (p == NULL)
-           return NULL;
+       }
 
        *todecref = bytes;
     }
@@ -133,6 +134,7 @@
 
     if (!(string = PyString_FromString(s)))
        return -1;
+
     if (PyList_Append(list, string))
     {
        Py_DECREF(string);
@@ -535,10 +537,8 @@
     }
 
     if (our_tv->v_type == VAR_STRING)
-    {
        result = PyString_FromString(our_tv->vval.v_string == NULL
                                        ? "" : (char *)our_tv->vval.v_string);
-    }
     else if (our_tv->v_type == VAR_NUMBER)
     {
        char buf[NUMBUFLEN];
@@ -3385,22 +3385,31 @@
     static char *
 StringToLine(PyObject *obj)
 {
-    const char *str;
-    char *save;
-    PyObject *bytes;
-    PyInt len;
-    PyInt i;
-    char *p;
-
-    if (obj == NULL || !PyString_Check(obj))
-    {
-       PyErr_BadArgument();
-       return NULL;
-    }
-
-    bytes = PyString_AsBytes(obj);  /* for Python 2 this does nothing */
-    str = PyString_AsString(bytes);
-    len = PyString_Size(bytes);
+    char       *str;
+    char       *save;
+    PyObject   *bytes = NULL;
+    Py_ssize_t len;
+    PyInt      i;
+    char       *p;
+
+    if (PyBytes_Check(obj))
+    {
+       if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
+               || str == NULL)
+           return NULL;
+    }
+    else if (PyUnicode_Check(obj))
+    {
+       if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
+           return NULL;
+
+       if(PyBytes_AsStringAndSize(bytes, &str, &len) == -1
+               || str == NULL)
+       {
+           Py_DECREF(bytes);
+           return NULL;
+       }
+    }
 
     /*
      * Error checking: String must not contain newlines, as we
@@ -3439,7 +3448,7 @@
     }
 
     save[i] = '\0';
-    PyString_FreeBytes(bytes);  /* Python 2 does nothing here */
+    Py_XDECREF(bytes);  /* Python 2 does nothing here */
 
     return save;
 }
@@ -3568,10 +3577,10 @@
 
        return OK;
     }
-    else if (PyString_Check(line))
-    {
-       char *save = StringToLine(line);
-       buf_T *savebuf;
+    else if (PyBytes_Check(line) || PyUnicode_Check(line))
+    {
+       char    *save = StringToLine(line);
+       buf_T   *savebuf;
 
        if (save == NULL)
            return FAIL;
@@ -3821,7 +3830,7 @@
     /* First of all, we check the type of the supplied Python object.
      * It must be a string or a list, or the call is in error.
      */
-    if (PyString_Check(lines))
+    if (PyBytes_Check(lines) || PyUnicode_Check(lines))
     {
        char    *str = StringToLine(lines);
        buf_T   *savebuf;
@@ -5254,7 +5263,7 @@
     {
        char_u  *result;
 
-       if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
+       if (PyBytes_AsStringAndSize(obj, (char **) &result, NULL) == -1)
            return -1;
        if (result == NULL)
            return -1;
@@ -5269,11 +5278,11 @@
        PyObject        *bytes;
        char_u  *result;
 
-       bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
+       bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
        if (bytes == NULL)
            return -1;
 
-       if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
+       if(PyBytes_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
            return -1;
        if (result == NULL)
            return -1;
diff -r c40e953fe936 -r b9d4dfa09951 src/if_python.c
--- a/src/if_python.c   Fri Jun 14 19:14:52 2013 +0400
+++ b/src/if_python.c   Fri Jun 14 19:27:27 2013 +0400
@@ -68,12 +68,9 @@
 #undef main /* Defined in python.h - aargh */
 #undef HAVE_FCNTL_H /* Clash with os_win32.h */
 
-#define PyBytes_FromString PyString_FromString
-#define PyBytes_Check PyString_Check
-
-/* No-op conversion functions, use with care! */
-#define PyString_AsBytes(obj) (obj)
-#define PyString_FreeBytes(obj)
+#define PyBytes_FromString      PyString_FromString
+#define PyBytes_Check           PyString_Check
+#define PyBytes_AsStringAndSize PyString_AsStringAndSize
 
 #if !defined(FEAT_PYTHON) && defined(PROTO)
 /* Use this to be able to generate prototypes without python being used. */
diff -r c40e953fe936 -r b9d4dfa09951 src/if_python3.c
--- a/src/if_python3.c  Fri Jun 14 19:14:52 2013 +0400
+++ b/src/if_python3.c  Fri Jun 14 19:27:27 2013 +0400
@@ -84,13 +84,8 @@
 
 #define PyInt Py_ssize_t
 #define PyString_Check(obj) PyUnicode_Check(obj)
-#define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, 
CODEC_ERROR_HANDLER)
-#define PyString_FreeBytes(obj) Py_XDECREF(bytes)
-#define PyString_AsString(obj) PyBytes_AsString(obj)
-#define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
 #define PyString_FromString(repr) PyUnicode_FromString(repr)
 #define PyString_FromFormat PyUnicode_FromFormat
-#define PyString_AsStringAndSize(obj, buffer, len) 
PyBytes_AsStringAndSize(obj, buffer, len)
 #define PyInt_Check(obj) PyLong_Check(obj)
 #define PyInt_FromLong(i) PyLong_FromLong(i)
 #define PyInt_AsLong(obj) PyLong_AsLong(obj)
@@ -357,7 +352,7 @@
 # endif
 static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const 
char* encoding, const char* errors);
 static char* (*py3_PyBytes_AsString)(PyObject *bytes);
-static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int 
*length);
+static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, 
Py_ssize_t *length);
 static PyObject* (*py3_PyBytes_FromString)(char *str);
 static PyObject* (*py3_PyFloat_FromDouble)(double num);
 static double (*py3_PyFloat_AsDouble)(PyObject *);

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


diff -crN vim.c40e953fe936/src/if_py_both.h vim.b9d4dfa09951/src/if_py_both.h
*** vim.c40e953fe936/src/if_py_both.h	2013-06-16 19:24:53.069237853 +0400
--- vim.b9d4dfa09951/src/if_py_both.h	2013-06-16 19:24:53.083237718 +0400
***************
*** 18,24 ****
  #endif
  
  #ifdef FEAT_MBYTE
! # define ENC_OPT p_enc
  #else
  # define ENC_OPT "latin1"
  #endif
--- 18,24 ----
  #endif
  
  #ifdef FEAT_MBYTE
! # define ENC_OPT ((char *)p_enc)
  #else
  # define ENC_OPT "latin1"
  #endif
***************
*** 92,119 ****
  StringToChars(PyObject *object, PyObject **todecref)
  {
      char_u	*p;
-     PyObject	*bytes = NULL;
  
      if (PyBytes_Check(object))
      {
  
! 	if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
! 	    return NULL;
! 	if (p == NULL)
  	    return NULL;
  
  	*todecref = NULL;
      }
      else if (PyUnicode_Check(object))
      {
! 	bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
! 	if (bytes == NULL)
! 	    return NULL;
  
! 	if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
  	    return NULL;
! 	if (p == NULL)
  	    return NULL;
  
  	*todecref = bytes;
      }
--- 92,120 ----
  StringToChars(PyObject *object, PyObject **todecref)
  {
      char_u	*p;
  
      if (PyBytes_Check(object))
      {
  
! 	if (PyBytes_AsStringAndSize(object, (char **) &p, NULL) == -1
! 		|| p == NULL)
  	    return NULL;
  
  	*todecref = NULL;
      }
      else if (PyUnicode_Check(object))
      {
! 	PyObject	*bytes;
  
! 	if (!(bytes = PyUnicode_AsEncodedString(object, ENC_OPT, NULL)))
  	    return NULL;
! 
! 	if(PyBytes_AsStringAndSize(bytes, (char **) &p, NULL) == -1
! 		|| p == NULL)
! 	{
! 	    Py_DECREF(bytes);
  	    return NULL;
+ 	}
  
  	*todecref = bytes;
      }
***************
*** 133,138 ****
--- 134,140 ----
  
      if (!(string = PyString_FromString(s)))
  	return -1;
+ 
      if (PyList_Append(list, string))
      {
  	Py_DECREF(string);
***************
*** 535,544 ****
      }
  
      if (our_tv->v_type == VAR_STRING)
-     {
  	result = PyString_FromString(our_tv->vval.v_string == NULL
  					? "" : (char *)our_tv->vval.v_string);
-     }
      else if (our_tv->v_type == VAR_NUMBER)
      {
  	char buf[NUMBUFLEN];
--- 537,544 ----
***************
*** 3385,3406 ****
      static char *
  StringToLine(PyObject *obj)
  {
!     const char *str;
!     char *save;
!     PyObject *bytes;
!     PyInt len;
!     PyInt i;
!     char *p;
  
!     if (obj == NULL || !PyString_Check(obj))
      {
! 	PyErr_BadArgument();
! 	return NULL;
      }
  
!     bytes = PyString_AsBytes(obj);  /* for Python 2 this does nothing */
!     str = PyString_AsString(bytes);
!     len = PyString_Size(bytes);
  
      /*
       * Error checking: String must not contain newlines, as we
--- 3385,3415 ----
      static char *
  StringToLine(PyObject *obj)
  {
!     char	*str;
!     char	*save;
!     PyObject	*bytes = NULL;
!     Py_ssize_t	len;
!     PyInt	i;
!     char	*p;
  
!     if (PyBytes_Check(obj))
      {
! 	if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
! 		|| str == NULL)
! 	    return NULL;
      }
+     else if (PyUnicode_Check(obj))
+     {
+ 	if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
+ 	    return NULL;
  
! 	if(PyBytes_AsStringAndSize(bytes, &str, &len) == -1
! 		|| str == NULL)
! 	{
! 	    Py_DECREF(bytes);
! 	    return NULL;
! 	}
!     }
  
      /*
       * Error checking: String must not contain newlines, as we
***************
*** 3439,3445 ****
      }
  
      save[i] = '\0';
!     PyString_FreeBytes(bytes);  /* Python 2 does nothing here */
  
      return save;
  }
--- 3448,3454 ----
      }
  
      save[i] = '\0';
!     Py_XDECREF(bytes);  /* Python 2 does nothing here */
  
      return save;
  }
***************
*** 3568,3577 ****
  
  	return OK;
      }
!     else if (PyString_Check(line))
      {
! 	char *save = StringToLine(line);
! 	buf_T *savebuf;
  
  	if (save == NULL)
  	    return FAIL;
--- 3577,3586 ----
  
  	return OK;
      }
!     else if (PyBytes_Check(line) || PyUnicode_Check(line))
      {
! 	char	*save = StringToLine(line);
! 	buf_T	*savebuf;
  
  	if (save == NULL)
  	    return FAIL;
***************
*** 3821,3827 ****
      /* First of all, we check the type of the supplied Python object.
       * It must be a string or a list, or the call is in error.
       */
!     if (PyString_Check(lines))
      {
  	char	*str = StringToLine(lines);
  	buf_T	*savebuf;
--- 3830,3836 ----
      /* First of all, we check the type of the supplied Python object.
       * It must be a string or a list, or the call is in error.
       */
!     if (PyBytes_Check(lines) || PyUnicode_Check(lines))
      {
  	char	*str = StringToLine(lines);
  	buf_T	*savebuf;
***************
*** 5254,5260 ****
      {
  	char_u	*result;
  
! 	if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
  	    return -1;
  	if (result == NULL)
  	    return -1;
--- 5263,5269 ----
      {
  	char_u	*result;
  
! 	if (PyBytes_AsStringAndSize(obj, (char **) &result, NULL) == -1)
  	    return -1;
  	if (result == NULL)
  	    return -1;
***************
*** 5269,5279 ****
  	PyObject	*bytes;
  	char_u	*result;
  
! 	bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
  	if (bytes == NULL)
  	    return -1;
  
! 	if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
  	    return -1;
  	if (result == NULL)
  	    return -1;
--- 5278,5288 ----
  	PyObject	*bytes;
  	char_u	*result;
  
! 	bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
  	if (bytes == NULL)
  	    return -1;
  
! 	if(PyBytes_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
  	    return -1;
  	if (result == NULL)
  	    return -1;
diff -crN vim.c40e953fe936/src/if_python3.c vim.b9d4dfa09951/src/if_python3.c
*** vim.c40e953fe936/src/if_python3.c	2013-06-16 19:24:53.062237920 +0400
--- vim.b9d4dfa09951/src/if_python3.c	2013-06-16 19:24:53.077237778 +0400
***************
*** 84,96 ****
  
  #define PyInt Py_ssize_t
  #define PyString_Check(obj) PyUnicode_Check(obj)
- #define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, CODEC_ERROR_HANDLER)
- #define PyString_FreeBytes(obj) Py_XDECREF(bytes)
- #define PyString_AsString(obj) PyBytes_AsString(obj)
- #define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
  #define PyString_FromString(repr) PyUnicode_FromString(repr)
  #define PyString_FromFormat PyUnicode_FromFormat
- #define PyString_AsStringAndSize(obj, buffer, len) PyBytes_AsStringAndSize(obj, buffer, len)
  #define PyInt_Check(obj) PyLong_Check(obj)
  #define PyInt_FromLong(i) PyLong_FromLong(i)
  #define PyInt_AsLong(obj) PyLong_AsLong(obj)
--- 84,91 ----
***************
*** 357,363 ****
  # endif
  static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
  static char* (*py3_PyBytes_AsString)(PyObject *bytes);
! static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length);
  static PyObject* (*py3_PyBytes_FromString)(char *str);
  static PyObject* (*py3_PyFloat_FromDouble)(double num);
  static double (*py3_PyFloat_AsDouble)(PyObject *);
--- 352,358 ----
  # endif
  static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
  static char* (*py3_PyBytes_AsString)(PyObject *bytes);
! static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
  static PyObject* (*py3_PyBytes_FromString)(char *str);
  static PyObject* (*py3_PyFloat_FromDouble)(double num);
  static double (*py3_PyFloat_AsDouble)(PyObject *);
diff -crN vim.c40e953fe936/src/if_python.c vim.b9d4dfa09951/src/if_python.c
*** vim.c40e953fe936/src/if_python.c	2013-06-16 19:24:53.073237816 +0400
--- vim.b9d4dfa09951/src/if_python.c	2013-06-16 19:24:53.087237682 +0400
***************
*** 68,79 ****
  #undef main /* Defined in python.h - aargh */
  #undef HAVE_FCNTL_H /* Clash with os_win32.h */
  
! #define PyBytes_FromString PyString_FromString
! #define PyBytes_Check PyString_Check
! 
! /* No-op conversion functions, use with care! */
! #define PyString_AsBytes(obj) (obj)
! #define PyString_FreeBytes(obj)
  
  #if !defined(FEAT_PYTHON) && defined(PROTO)
  /* Use this to be able to generate prototypes without python being used. */
--- 68,76 ----
  #undef main /* Defined in python.h - aargh */
  #undef HAVE_FCNTL_H /* Clash with os_win32.h */
  
! #define PyBytes_FromString      PyString_FromString
! #define PyBytes_Check           PyString_Check
! #define PyBytes_AsStringAndSize PyString_AsStringAndSize
  
  #if !defined(FEAT_PYTHON) && defined(PROTO)
  /* Use this to be able to generate prototypes without python being used. */

Raspunde prin e-mail lui