0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

updated for version 7.3.1228

Problem:    Python: various inconsistencies and problems.
Solution:   StringToLine now supports both bytes() and unicode() objects.
            Make function names consistant.  Fix memory leak fixed in
            StringToLine. (ZyX)
This commit is contained in:
Bram Moolenaar 2013-06-23 13:11:18 +02:00
parent 389a1793f4
commit 808c2bc8bf
4 changed files with 47 additions and 44 deletions

View File

@ -18,7 +18,7 @@ typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
#endif #endif
#ifdef FEAT_MBYTE #ifdef FEAT_MBYTE
# define ENC_OPT p_enc # define ENC_OPT ((char *)p_enc)
#else #else
# define ENC_OPT "latin1" # define ENC_OPT "latin1"
#endif #endif
@ -92,28 +92,29 @@ Python_Release_Vim(void)
StringToChars(PyObject *object, PyObject **todecref) StringToChars(PyObject *object, PyObject **todecref)
{ {
char_u *p; char_u *p;
PyObject *bytes = NULL;
if (PyBytes_Check(object)) if (PyBytes_Check(object))
{ {
if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1) if (PyBytes_AsStringAndSize(object, (char **) &p, NULL) == -1
return NULL; || p == NULL)
if (p == NULL)
return NULL; return NULL;
*todecref = NULL; *todecref = NULL;
} }
else if (PyUnicode_Check(object)) else if (PyUnicode_Check(object))
{ {
bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL); PyObject *bytes;
if (bytes == NULL)
if (!(bytes = PyUnicode_AsEncodedString(object, ENC_OPT, NULL)))
return NULL; return NULL;
if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1) if(PyBytes_AsStringAndSize(bytes, (char **) &p, NULL) == -1
return NULL; || p == NULL)
if (p == NULL) {
Py_DECREF(bytes);
return NULL; return NULL;
}
*todecref = bytes; *todecref = bytes;
} }
@ -133,6 +134,7 @@ add_string(PyObject *list, char *s)
if (!(string = PyString_FromString(s))) if (!(string = PyString_FromString(s)))
return -1; return -1;
if (PyList_Append(list, string)) if (PyList_Append(list, string))
{ {
Py_DECREF(string); Py_DECREF(string);
@ -534,10 +536,8 @@ VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
} }
if (our_tv->v_type == VAR_STRING) if (our_tv->v_type == VAR_STRING)
{
result = PyString_FromString(our_tv->vval.v_string == NULL result = PyString_FromString(our_tv->vval.v_string == NULL
? "" : (char *)our_tv->vval.v_string); ? "" : (char *)our_tv->vval.v_string);
}
else if (our_tv->v_type == VAR_NUMBER) else if (our_tv->v_type == VAR_NUMBER)
{ {
char buf[NUMBUFLEN]; char buf[NUMBUFLEN];
@ -3385,22 +3385,31 @@ WinListItem(WinListObject *self, PyInt n)
static char * static char *
StringToLine(PyObject *obj) StringToLine(PyObject *obj)
{ {
const char *str; char *str;
char *save; char *save;
PyObject *bytes; PyObject *bytes = NULL;
PyInt len; Py_ssize_t len;
PyInt i; PyInt i;
char *p; char *p;
if (obj == NULL || !PyString_Check(obj)) if (PyBytes_Check(obj))
{ {
PyErr_BadArgument(); if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
return NULL; || str == NULL)
return NULL;
} }
else if (PyUnicode_Check(obj))
{
if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
return NULL;
bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */ if(PyBytes_AsStringAndSize(bytes, &str, &len) == -1
str = PyString_AsString(bytes); || str == NULL)
len = PyString_Size(bytes); {
Py_DECREF(bytes);
return NULL;
}
}
/* /*
* Error checking: String must not contain newlines, as we * Error checking: String must not contain newlines, as we
@ -3439,7 +3448,7 @@ StringToLine(PyObject *obj)
} }
save[i] = '\0'; save[i] = '\0';
PyString_FreeBytes(bytes); /* Python 2 does nothing here */ Py_XDECREF(bytes); /* Python 2 does nothing here */
return save; return save;
} }
@ -3568,10 +3577,10 @@ SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
return OK; return OK;
} }
else if (PyString_Check(line)) else if (PyBytes_Check(line) || PyUnicode_Check(line))
{ {
char *save = StringToLine(line); char *save = StringToLine(line);
buf_T *savebuf; buf_T *savebuf;
if (save == NULL) if (save == NULL)
return FAIL; return FAIL;
@ -3821,7 +3830,7 @@ InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
/* First of all, we check the type of the supplied Python object. /* 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. * 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); char *str = StringToLine(lines);
buf_T *savebuf; buf_T *savebuf;
@ -5254,7 +5263,7 @@ _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
{ {
char_u *result; char_u *result;
if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1) if (PyBytes_AsStringAndSize(obj, (char **) &result, NULL) == -1)
return -1; return -1;
if (result == NULL) if (result == NULL)
return -1; return -1;
@ -5269,11 +5278,11 @@ _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
PyObject *bytes; PyObject *bytes;
char_u *result; char_u *result;
bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL); bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
if (bytes == NULL) if (bytes == NULL)
return -1; return -1;
if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1) if(PyBytes_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
return -1; return -1;
if (result == NULL) if (result == NULL)
return -1; return -1;

View File

@ -68,12 +68,9 @@
#undef main /* Defined in python.h - aargh */ #undef main /* Defined in python.h - aargh */
#undef HAVE_FCNTL_H /* Clash with os_win32.h */ #undef HAVE_FCNTL_H /* Clash with os_win32.h */
#define PyBytes_FromString PyString_FromString #define PyBytes_FromString PyString_FromString
#define PyBytes_Check PyString_Check #define PyBytes_Check PyString_Check
#define PyBytes_AsStringAndSize PyString_AsStringAndSize
/* No-op conversion functions, use with care! */
#define PyString_AsBytes(obj) (obj)
#define PyString_FreeBytes(obj)
#if !defined(FEAT_PYTHON) && defined(PROTO) #if !defined(FEAT_PYTHON) && defined(PROTO)
/* Use this to be able to generate prototypes without python being used. */ /* Use this to be able to generate prototypes without python being used. */

View File

@ -84,13 +84,8 @@
#define PyInt Py_ssize_t #define PyInt Py_ssize_t
#define PyString_Check(obj) PyUnicode_Check(obj) #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_FromString(repr) PyUnicode_FromString(repr)
#define PyString_FromFormat PyUnicode_FromFormat #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_Check(obj) PyLong_Check(obj)
#define PyInt_FromLong(i) PyLong_FromLong(i) #define PyInt_FromLong(i) PyLong_FromLong(i)
#define PyInt_AsLong(obj) PyLong_AsLong(obj) #define PyInt_AsLong(obj) PyLong_AsLong(obj)
@ -357,7 +352,7 @@ static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
# endif # endif
static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors); static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
static char* (*py3_PyBytes_AsString)(PyObject *bytes); 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_PyBytes_FromString)(char *str);
static PyObject* (*py3_PyFloat_FromDouble)(double num); static PyObject* (*py3_PyFloat_FromDouble)(double num);
static double (*py3_PyFloat_AsDouble)(PyObject *); static double (*py3_PyFloat_AsDouble)(PyObject *);

View File

@ -728,6 +728,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
1228,
/**/ /**/
1227, 1227,
/**/ /**/