Mercurial > vim
comparison src/if_py_both.h @ 4966:620d9b59d4ed v7.3.1228
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)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sun, 23 Jun 2013 13:11:18 +0200 |
parents | 5cee875f3096 |
children | b6e693e1f946 |
comparison
equal
deleted
inserted
replaced
4965:cc65e359c1ee | 4966:620d9b59d4ed |
---|---|
16 #if PY_VERSION_HEX < 0x02050000 | 16 #if PY_VERSION_HEX < 0x02050000 |
17 typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */ | 17 typedef int Py_ssize_t; /* Python 2.4 and earlier don't have this type. */ |
18 #endif | 18 #endif |
19 | 19 |
20 #ifdef FEAT_MBYTE | 20 #ifdef FEAT_MBYTE |
21 # define ENC_OPT p_enc | 21 # define ENC_OPT ((char *)p_enc) |
22 #else | 22 #else |
23 # define ENC_OPT "latin1" | 23 # define ENC_OPT "latin1" |
24 #endif | 24 #endif |
25 #define DOPY_FUNC "_vim_pydo" | 25 #define DOPY_FUNC "_vim_pydo" |
26 | 26 |
90 */ | 90 */ |
91 static char_u * | 91 static char_u * |
92 StringToChars(PyObject *object, PyObject **todecref) | 92 StringToChars(PyObject *object, PyObject **todecref) |
93 { | 93 { |
94 char_u *p; | 94 char_u *p; |
95 PyObject *bytes = NULL; | |
96 | 95 |
97 if (PyBytes_Check(object)) | 96 if (PyBytes_Check(object)) |
98 { | 97 { |
99 | 98 |
100 if (PyString_AsStringAndSize(object, (char **) &p, NULL) == -1) | 99 if (PyBytes_AsStringAndSize(object, (char **) &p, NULL) == -1 |
100 || p == NULL) | |
101 return NULL; | 101 return NULL; |
102 if (p == NULL) | 102 |
103 *todecref = NULL; | |
104 } | |
105 else if (PyUnicode_Check(object)) | |
106 { | |
107 PyObject *bytes; | |
108 | |
109 if (!(bytes = PyUnicode_AsEncodedString(object, ENC_OPT, NULL))) | |
103 return NULL; | 110 return NULL; |
104 | 111 |
105 *todecref = NULL; | 112 if(PyBytes_AsStringAndSize(bytes, (char **) &p, NULL) == -1 |
106 } | 113 || p == NULL) |
107 else if (PyUnicode_Check(object)) | 114 { |
108 { | 115 Py_DECREF(bytes); |
109 bytes = PyUnicode_AsEncodedString(object, (char *)ENC_OPT, NULL); | |
110 if (bytes == NULL) | |
111 return NULL; | 116 return NULL; |
112 | 117 } |
113 if(PyString_AsStringAndSize(bytes, (char **) &p, NULL) == -1) | |
114 return NULL; | |
115 if (p == NULL) | |
116 return NULL; | |
117 | 118 |
118 *todecref = bytes; | 119 *todecref = bytes; |
119 } | 120 } |
120 else | 121 else |
121 { | 122 { |
131 { | 132 { |
132 PyObject *string; | 133 PyObject *string; |
133 | 134 |
134 if (!(string = PyString_FromString(s))) | 135 if (!(string = PyString_FromString(s))) |
135 return -1; | 136 return -1; |
137 | |
136 if (PyList_Append(list, string)) | 138 if (PyList_Append(list, string)) |
137 { | 139 { |
138 Py_DECREF(string); | 140 Py_DECREF(string); |
139 return -1; | 141 return -1; |
140 } | 142 } |
532 return result; | 534 return result; |
533 } | 535 } |
534 } | 536 } |
535 | 537 |
536 if (our_tv->v_type == VAR_STRING) | 538 if (our_tv->v_type == VAR_STRING) |
537 { | |
538 result = PyString_FromString(our_tv->vval.v_string == NULL | 539 result = PyString_FromString(our_tv->vval.v_string == NULL |
539 ? "" : (char *)our_tv->vval.v_string); | 540 ? "" : (char *)our_tv->vval.v_string); |
540 } | |
541 else if (our_tv->v_type == VAR_NUMBER) | 541 else if (our_tv->v_type == VAR_NUMBER) |
542 { | 542 { |
543 char buf[NUMBUFLEN]; | 543 char buf[NUMBUFLEN]; |
544 | 544 |
545 /* For backwards compatibility numbers are stored as strings. */ | 545 /* For backwards compatibility numbers are stored as strings. */ |
3383 * On errors, the Python exception data is set, and NULL is returned. | 3383 * On errors, the Python exception data is set, and NULL is returned. |
3384 */ | 3384 */ |
3385 static char * | 3385 static char * |
3386 StringToLine(PyObject *obj) | 3386 StringToLine(PyObject *obj) |
3387 { | 3387 { |
3388 const char *str; | 3388 char *str; |
3389 char *save; | 3389 char *save; |
3390 PyObject *bytes; | 3390 PyObject *bytes = NULL; |
3391 PyInt len; | 3391 Py_ssize_t len; |
3392 PyInt i; | 3392 PyInt i; |
3393 char *p; | 3393 char *p; |
3394 | 3394 |
3395 if (obj == NULL || !PyString_Check(obj)) | 3395 if (PyBytes_Check(obj)) |
3396 { | 3396 { |
3397 PyErr_BadArgument(); | 3397 if (PyBytes_AsStringAndSize(obj, &str, &len) == -1 |
3398 return NULL; | 3398 || str == NULL) |
3399 } | 3399 return NULL; |
3400 | 3400 } |
3401 bytes = PyString_AsBytes(obj); /* for Python 2 this does nothing */ | 3401 else if (PyUnicode_Check(obj)) |
3402 str = PyString_AsString(bytes); | 3402 { |
3403 len = PyString_Size(bytes); | 3403 if (!(bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL))) |
3404 return NULL; | |
3405 | |
3406 if(PyBytes_AsStringAndSize(bytes, &str, &len) == -1 | |
3407 || str == NULL) | |
3408 { | |
3409 Py_DECREF(bytes); | |
3410 return NULL; | |
3411 } | |
3412 } | |
3404 | 3413 |
3405 /* | 3414 /* |
3406 * Error checking: String must not contain newlines, as we | 3415 * Error checking: String must not contain newlines, as we |
3407 * are replacing a single line, and we must replace it with | 3416 * are replacing a single line, and we must replace it with |
3408 * a single line. | 3417 * a single line. |
3437 else | 3446 else |
3438 save[i] = str[i]; | 3447 save[i] = str[i]; |
3439 } | 3448 } |
3440 | 3449 |
3441 save[i] = '\0'; | 3450 save[i] = '\0'; |
3442 PyString_FreeBytes(bytes); /* Python 2 does nothing here */ | 3451 Py_XDECREF(bytes); /* Python 2 does nothing here */ |
3443 | 3452 |
3444 return save; | 3453 return save; |
3445 } | 3454 } |
3446 | 3455 |
3447 /* Get a line from the specified buffer. The line number is | 3456 /* Get a line from the specified buffer. The line number is |
3566 if (len_change) | 3575 if (len_change) |
3567 *len_change = -1; | 3576 *len_change = -1; |
3568 | 3577 |
3569 return OK; | 3578 return OK; |
3570 } | 3579 } |
3571 else if (PyString_Check(line)) | 3580 else if (PyBytes_Check(line) || PyUnicode_Check(line)) |
3572 { | 3581 { |
3573 char *save = StringToLine(line); | 3582 char *save = StringToLine(line); |
3574 buf_T *savebuf; | 3583 buf_T *savebuf; |
3575 | 3584 |
3576 if (save == NULL) | 3585 if (save == NULL) |
3577 return FAIL; | 3586 return FAIL; |
3578 | 3587 |
3579 VimTryStart(); | 3588 VimTryStart(); |
3819 InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) | 3828 InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) |
3820 { | 3829 { |
3821 /* First of all, we check the type of the supplied Python object. | 3830 /* First of all, we check the type of the supplied Python object. |
3822 * It must be a string or a list, or the call is in error. | 3831 * It must be a string or a list, or the call is in error. |
3823 */ | 3832 */ |
3824 if (PyString_Check(lines)) | 3833 if (PyBytes_Check(lines) || PyUnicode_Check(lines)) |
3825 { | 3834 { |
3826 char *str = StringToLine(lines); | 3835 char *str = StringToLine(lines); |
3827 buf_T *savebuf; | 3836 buf_T *savebuf; |
3828 | 3837 |
3829 if (str == NULL) | 3838 if (str == NULL) |
5252 } | 5261 } |
5253 else if (PyBytes_Check(obj)) | 5262 else if (PyBytes_Check(obj)) |
5254 { | 5263 { |
5255 char_u *result; | 5264 char_u *result; |
5256 | 5265 |
5257 if (PyString_AsStringAndSize(obj, (char **) &result, NULL) == -1) | 5266 if (PyBytes_AsStringAndSize(obj, (char **) &result, NULL) == -1) |
5258 return -1; | 5267 return -1; |
5259 if (result == NULL) | 5268 if (result == NULL) |
5260 return -1; | 5269 return -1; |
5261 | 5270 |
5262 if (set_string_copy(result, tv) == -1) | 5271 if (set_string_copy(result, tv) == -1) |
5267 else if (PyUnicode_Check(obj)) | 5276 else if (PyUnicode_Check(obj)) |
5268 { | 5277 { |
5269 PyObject *bytes; | 5278 PyObject *bytes; |
5270 char_u *result; | 5279 char_u *result; |
5271 | 5280 |
5272 bytes = PyUnicode_AsEncodedString(obj, (char *)ENC_OPT, NULL); | 5281 bytes = PyUnicode_AsEncodedString(obj, ENC_OPT, NULL); |
5273 if (bytes == NULL) | 5282 if (bytes == NULL) |
5274 return -1; | 5283 return -1; |
5275 | 5284 |
5276 if(PyString_AsStringAndSize(bytes, (char **) &result, NULL) == -1) | 5285 if(PyBytes_AsStringAndSize(bytes, (char **) &result, NULL) == -1) |
5277 return -1; | 5286 return -1; |
5278 if (result == NULL) | 5287 if (result == NULL) |
5279 return -1; | 5288 return -1; |
5280 | 5289 |
5281 if (set_string_copy(result, tv)) | 5290 if (set_string_copy(result, tv)) |