Skip to content

Commit 20ee113

Browse files
committed
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)
1 parent 33896b6 commit 20ee113

File tree

4 files changed

+47
-44
lines changed

4 files changed

+47
-44
lines changed

src/if_py_both.h

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */
1818
#endif
1919

2020
#ifdef FEAT_MBYTE
21-
# define ENC_OPT p_enc
21+
# define ENC_OPT ((char *)p_enc)
2222
#else
2323
# define ENC_OPT "latin1"
2424
#endif
@@ -92,28 +92,29 @@ Python_Release_Vim(void)
9292
StringToChars(PyObject *object, PyObject **todecref)
9393
{
9494
char_u *p;
95-
PyObject *bytes = NULL;
9695

9796
if (PyBytes_Check(object))
9897
{
9998

100-
if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1)
101-
return NULL;
102-
if (p == NULL)
99+
if (PyBytes_AsStringAndSize(object, (char **) &p, NULL) == -1
100+
|| p == NULL)
103101
return NULL;
104102

105103
*todecref = NULL;
106104
}
107105
else if (PyUnicode_Check(object))
108106
{
109-
bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL);
110-
if (bytes == NULL)
111-
return NULL;
107+
PyObject *bytes;
112108

113-
if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1)
109+
if (!(bytes = PyUnicode_AsEncodedString(object, ENC_OPT, NULL)))
114110
return NULL;
115-
if (p == NULL)
111+
112+
if(PyBytes_AsStringAndSize(bytes, (char **) &p, NULL) == -1
113+
|| p == NULL)
114+
{
115+
Py_DECREF(bytes);
116116
return NULL;
117+
}
117118

118119
*todecref = bytes;
119120
}
@@ -133,6 +134,7 @@ add_string(PyObject *list, char *s)
133134

134135
if (!(string = PyString_FromString(s)))
135136
return -1;
137+
136138
if (PyList_Append(list, string))
137139
{
138140
Py_DECREF(string);
@@ -534,10 +536,8 @@ VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
534536
}
535537

536538
if (our_tv->v_type == VAR_STRING)
537-
{
538539
result = PyString_FromString(our_tv->vval.v_string == NULL
539540
? "" : (char *)our_tv->vval.v_string);
540-
}
541541
else if (our_tv->v_type == VAR_NUMBER)
542542
{
543543
char buf[NUMBUFLEN];
@@ -3385,22 +3385,31 @@ WinListItem(WinListObject *self, PyInt n)
33853385
static char *
33863386
StringToLine(PyObject *obj)
33873387
{
3388-
const char *str;
3389-
char *save;
3390-
PyObject *bytes;
3391-
PyInt len;
3392-
PyInt i;
3393-
char *p;
3388+
char *str;
3389+
char *save;
3390+
PyObject *bytes = NULL;
3391+
Py_ssize_t len;
3392+
PyInt i;
3393+
char *p;
33943394

3395-
if (obj == NULL || !PyString_Check(obj))
3395+
if (PyBytes_Check(obj))
33963396
{
3397-
PyErr_BadArgument();
3398-
return NULL;
3397+
if (PyBytes_AsStringAndSize(obj, &str, &len) == -1
3398+
|| str == NULL)
3399+
return NULL;
33993400
}
3401+
else if (PyUnicode_Check(obj))
3402+
{
3403+
if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL)))
3404+
return NULL;
34003405

3401-
bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */
3402-
str = PyString_AsString(bytes);
3403-
len = PyString_Size(bytes);
3406+
if(PyBytes_AsStringAndSize(bytes, &str, &len) == -1
3407+
|| str == NULL)
3408+
{
3409+
Py_DECREF(bytes);
3410+
return NULL;
3411+
}
3412+
}
34043413

34053414
/*
34063415
* Error checking: String must not contain newlines, as we
@@ -3439,7 +3448,7 @@ StringToLine(PyObject *obj)
34393448
}
34403449

34413450
save[i] = '\0';
3442-
PyString_FreeBytes(bytes); /* Python 2 does nothing here */
3451+
Py_XDECREF(bytes); /* Python 2 does nothing here */
34433452

34443453
return save;
34453454
}
@@ -3568,10 +3577,10 @@ SetBufferLine(buf_T *buf, PyInt n, PyObject *line, PyInt *len_change)
35683577

35693578
return OK;
35703579
}
3571-
else if (PyString_Check(line))
3580+
else if (PyBytes_Check(line) || PyUnicode_Check(line))
35723581
{
3573-
char *save = StringToLine(line);
3574-
buf_T *savebuf;
3582+
char *save = StringToLine(line);
3583+
buf_T *savebuf;
35753584

35763585
if (save == NULL)
35773586
return FAIL;
@@ -3821,7 +3830,7 @@ InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change)
38213830
/* First of all, we check the type of the supplied Python object.
38223831
* It must be a string or a list, or the call is in error.
38233832
*/
3824-
if (PyString_Check(lines))
3833+
if (PyBytes_Check(lines) || PyUnicode_Check(lines))
38253834
{
38263835
char *str = StringToLine(lines);
38273836
buf_T *savebuf;
@@ -5254,7 +5263,7 @@ _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
52545263
{
52555264
char_u *result;
52565265

5257-
if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1)
5266+
if (PyBytes_AsStringAndSize(obj, (char **) &result, NULL) == -1)
52585267
return -1;
52595268
if (result == NULL)
52605269
return -1;
@@ -5269,11 +5278,11 @@ _ConvertFromPyObject(PyObject *obj, typval_T *tv, PyObject *lookup_dict)
52695278
PyObject *bytes;
52705279
char_u *result;
52715280

5272-
bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL);
5281+
bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL);
52735282
if (bytes == NULL)
52745283
return -1;
52755284

5276-
if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
5285+
if(PyBytes_AsStringAndSize(bytes, (char **) &result, NULL) == -1)
52775286
return -1;
52785287
if (result == NULL)
52795288
return -1;

src/if_python.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,9 @@
6868
#undef main /* Defined in python.h - aargh */
6969
#undef HAVE_FCNTL_H /* Clash with os_win32.h */
7070

71-
#define PyBytes_FromString PyString_FromString
72-
#define PyBytes_Check PyString_Check
73-
74-
/* No-op conversion functions, use with care! */
75-
#define PyString_AsBytes(obj) (obj)
76-
#define PyString_FreeBytes(obj)
71+
#define PyBytes_FromString PyString_FromString
72+
#define PyBytes_Check PyString_Check
73+
#define PyBytes_AsStringAndSize PyString_AsStringAndSize
7774

7875
#if !defined(FEAT_PYTHON) && defined(PROTO)
7976
/* Use this to be able to generate prototypes without python being used. */

src/if_python3.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,8 @@
8484

8585
#define PyInt Py_ssize_t
8686
#define PyString_Check(obj) PyUnicode_Check(obj)
87-
#define PyString_AsBytes(obj) PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, CODEC_ERROR_HANDLER)
88-
#define PyString_FreeBytes(obj) Py_XDECREF(bytes)
89-
#define PyString_AsString(obj) PyBytes_AsString(obj)
90-
#define PyString_Size(obj) PyBytes_GET_SIZE(bytes)
9187
#define PyString_FromString(repr) PyUnicode_FromString(repr)
9288
#define PyString_FromFormat PyUnicode_FromFormat
93-
#define PyString_AsStringAndSize(obj, buffer, len) PyBytes_AsStringAndSize(obj, buffer, len)
9489
#define PyInt_Check(obj) PyLong_Check(obj)
9590
#define PyInt_FromLong(i) PyLong_FromLong(i)
9691
#define PyInt_AsLong(obj) PyLong_AsLong(obj)
@@ -357,7 +352,7 @@ static char* (*py3__PyUnicode_AsString)(PyObject *unicode);
357352
# endif
358353
static PyObject* (*py3_PyUnicode_AsEncodedString)(PyObject *unicode, const char* encoding, const char* errors);
359354
static char* (*py3_PyBytes_AsString)(PyObject *bytes);
360-
static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, int *length);
355+
static int (*py3_PyBytes_AsStringAndSize)(PyObject *bytes, char **buffer, Py_ssize_t *length);
361356
static PyObject* (*py3_PyBytes_FromString)(char *str);
362357
static PyObject* (*py3_PyFloat_FromDouble)(double num);
363358
static double (*py3_PyFloat_AsDouble)(PyObject *);

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,8 @@ static char *(features[]) =
728728

729729
static int included_patches[] =
730730
{ /* Add new patch number below this line */
731+
/**/
732+
1228,
731733
/**/
732734
1227,
733735
/**/

0 commit comments

Comments
 (0)