@@ -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)
9292StringToChars (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 *
33863386StringToLine (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 ;
0 commit comments