Skip to content

Commit d9a5c69

Browse files
committed
updated for version 7.3.1227
Problem: Inconsistent string conversion. Solution: Use 'encoding' instead of utf-8. Use METH_O in place of METH_VARARGS where appropriate. (ZyX)
1 parent 6e60fc4 commit d9a5c69

File tree

4 files changed

+72
-58
lines changed

4 files changed

+72
-58
lines changed

src/if_py_both.h

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -466,21 +466,20 @@ VimCheckInterrupt(void)
466466
*/
467467

468468
static PyObject *
469-
VimCommand(PyObject *self UNUSED, PyObject *args)
469+
VimCommand(PyObject *self UNUSED, PyObject *string)
470470
{
471-
char *cmd;
472-
PyObject *result;
471+
char_u *cmd;
472+
PyObject *result;
473+
PyObject *todecref;
473474

474-
if (!PyArg_ParseTuple(args, "s", &cmd))
475+
if (!(cmd = StringToChars(string, &todecref)))
475476
return NULL;
476477

477-
PyErr_Clear();
478-
479478
Py_BEGIN_ALLOW_THREADS
480479
Python_Lock_Vim();
481480

482481
VimTryStart();
483-
do_cmdline_cmd((char_u *)cmd);
482+
do_cmdline_cmd(cmd);
484483
update_screen(VALID);
485484

486485
Python_Release_Vim();
@@ -491,8 +490,8 @@ VimCommand(PyObject *self UNUSED, PyObject *args)
491490
else
492491
result = Py_None;
493492

494-
495493
Py_XINCREF(result);
494+
Py_XDECREF(todecref);
496495
return result;
497496
}
498497

@@ -641,21 +640,28 @@ VimToPython(typval_T *our_tv, int depth, PyObject *lookup_dict)
641640
static PyObject *
642641
VimEval(PyObject *self UNUSED, PyObject *args)
643642
{
644-
char *expr;
643+
char_u *expr;
645644
typval_T *our_tv;
645+
PyObject *string;
646+
PyObject *todecref;
646647
PyObject *result;
647-
PyObject *lookup_dict;
648+
PyObject *lookup_dict;
649+
650+
if (!PyArg_ParseTuple(args, "O", &string))
651+
return NULL;
648652

649-
if (!PyArg_ParseTuple(args, "s", &expr))
653+
if (!(expr = StringToChars(string, &todecref)))
650654
return NULL;
651655

652656
Py_BEGIN_ALLOW_THREADS
653657
Python_Lock_Vim();
654658
VimTryStart();
655-
our_tv = eval_expr((char_u *)expr, NULL);
659+
our_tv = eval_expr(expr, NULL);
656660
Python_Release_Vim();
657661
Py_END_ALLOW_THREADS
658662

663+
Py_XDECREF(todecref);
664+
659665
if (VimTryEnd())
660666
return NULL;
661667

@@ -688,22 +694,25 @@ VimEval(PyObject *self UNUSED, PyObject *args)
688694
static PyObject *ConvertToPyObject(typval_T *);
689695

690696
static PyObject *
691-
VimEvalPy(PyObject *self UNUSED, PyObject *args)
697+
VimEvalPy(PyObject *self UNUSED, PyObject *string)
692698
{
693-
char *expr;
694699
typval_T *our_tv;
695700
PyObject *result;
701+
char_u *expr;
702+
PyObject *todecref;
696703

697-
if (!PyArg_ParseTuple(args, "s", &expr))
704+
if (!(expr = StringToChars(string, &todecref)))
698705
return NULL;
699706

700707
Py_BEGIN_ALLOW_THREADS
701708
Python_Lock_Vim();
702709
VimTryStart();
703-
our_tv = eval_expr((char_u *)expr, NULL);
710+
our_tv = eval_expr(expr, NULL);
704711
Python_Release_Vim();
705712
Py_END_ALLOW_THREADS
706713

714+
Py_XDECREF(todecref);
715+
707716
if (VimTryEnd())
708717
return NULL;
709718

@@ -724,20 +733,24 @@ VimEvalPy(PyObject *self UNUSED, PyObject *args)
724733
}
725734

726735
static PyObject *
727-
VimStrwidth(PyObject *self UNUSED, PyObject *args)
736+
VimStrwidth(PyObject *self UNUSED, PyObject *string)
728737
{
729-
char *expr;
738+
char_u *str;
739+
PyObject *todecref;
740+
int result;
730741

731-
if (!PyArg_ParseTuple(args, "s", &expr))
742+
if (!(str = StringToChars(string, &todecref)))
732743
return NULL;
733744

734-
return PyLong_FromLong(
735745
#ifdef FEAT_MBYTE
736-
mb_string2cells((char_u *)expr, (int)STRLEN(expr))
746+
result = mb_string2cells(str, (int)STRLEN(str));
737747
#else
738-
STRLEN(expr)
748+
result = STRLEN(str);
739749
#endif
740-
);
750+
751+
Py_XDECREF(todecref);
752+
753+
return PyLong_FromLong(result);
741754
}
742755

743756
static PyObject *
@@ -840,13 +853,11 @@ map_rtp_callback(char_u *path, void *_data)
840853
}
841854

842855
static PyObject *
843-
VimForeachRTP(PyObject *self UNUSED, PyObject *args)
856+
VimForeachRTP(PyObject *self UNUSED, PyObject *callable)
844857
{
845858
map_rtp_data data;
846859

847-
if (!PyArg_ParseTuple(args, "O", &data.callable))
848-
return NULL;
849-
860+
data.callable = callable;
850861
data.result = NULL;
851862

852863
do_in_runtimepath(NULL, FALSE, &map_rtp_callback, &data);
@@ -1099,13 +1110,13 @@ VimPathHook(PyObject *self UNUSED, PyObject *args)
10991110

11001111
static struct PyMethodDef VimMethods[] = {
11011112
/* name, function, calling, documentation */
1102-
{"command", VimCommand, METH_VARARGS, "Execute a Vim ex-mode command" },
1113+
{"command", VimCommand, METH_O, "Execute a Vim ex-mode command" },
11031114
{"eval", VimEval, METH_VARARGS, "Evaluate an expression using Vim evaluator" },
1104-
{"bindeval", VimEvalPy, METH_VARARGS, "Like eval(), but returns objects attached to vim ones"},
1105-
{"strwidth", VimStrwidth, METH_VARARGS, "Screen string width, counts <Tab> as having width 1"},
1115+
{"bindeval", VimEvalPy, METH_O, "Like eval(), but returns objects attached to vim ones"},
1116+
{"strwidth", VimStrwidth, METH_O, "Screen string width, counts <Tab> as having width 1"},
11061117
{"chdir", (PyCFunction)VimChdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
11071118
{"fchdir", (PyCFunction)VimFchdir, METH_VARARGS|METH_KEYWORDS, "Change directory"},
1108-
{"foreach_rtp", VimForeachRTP, METH_VARARGS, "Call given callable for each path in &rtp"},
1119+
{"foreach_rtp", VimForeachRTP, METH_O, "Call given callable for each path in &rtp"},
11091120
{"find_module", FinderFindModule, METH_VARARGS, "Internal use only, returns loader object for any input it receives"},
11101121
{"path_hook", VimPathHook, METH_VARARGS, "Hook function to install in sys.path_hooks"},
11111122
{"_get_paths", (PyCFunction)Vim_GetPaths, METH_NOARGS, "Get &rtp-based additions to sys.path"},
@@ -1733,7 +1744,7 @@ DictionaryUpdate(DictionaryObject *self, PyObject *args, PyObject *kwargs)
17331744
{
17341745
PyObject *object;
17351746

1736-
if (!PyArg_Parse(args, "(O)", &object))
1747+
if (!PyArg_ParseTuple(args, "O", &object))
17371748
return NULL;
17381749

17391750
if (PyObject_HasAttrString(object, "keys"))
@@ -1877,13 +1888,8 @@ DictionaryPopItem(DictionaryObject *self)
18771888
}
18781889

18791890
static PyObject *
1880-
DictionaryHasKey(DictionaryObject *self, PyObject *args)
1891+
DictionaryHasKey(DictionaryObject *self, PyObject *keyObject)
18811892
{
1882-
PyObject *keyObject;
1883-
1884-
if (!PyArg_ParseTuple(args, "O", &keyObject))
1885-
return NULL;
1886-
18871893
return _DictionaryItem(self, keyObject, DICT_FLAG_RETURN_BOOL);
18881894
}
18891895

@@ -1914,7 +1920,7 @@ static struct PyMethodDef DictionaryMethods[] = {
19141920
{"get", (PyCFunction)DictionaryGet, METH_VARARGS, ""},
19151921
{"pop", (PyCFunction)DictionaryPop, METH_VARARGS, ""},
19161922
{"popitem", (PyCFunction)DictionaryPopItem, METH_NOARGS, ""},
1917-
{"has_key", (PyCFunction)DictionaryHasKey, METH_VARARGS, ""},
1923+
{"has_key", (PyCFunction)DictionaryHasKey, METH_O, ""},
19181924
{"__dir__", (PyCFunction)DictionaryDir, METH_NOARGS, ""},
19191925
{ NULL, NULL, 0, NULL}
19201926
};
@@ -2434,11 +2440,13 @@ FunctionConstructor(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
24342440
return NULL;
24352441
}
24362442

2437-
if (!PyArg_ParseTuple(args, "s", &name))
2443+
if (!PyArg_ParseTuple(args, "et", "ascii", &name))
24382444
return NULL;
24392445

24402446
self = FunctionNew(subtype, name);
24412447

2448+
PyMem_Free(name);
2449+
24422450
return self;
24432451
}
24442452

@@ -4383,27 +4391,31 @@ BufferAppend(BufferObject *self, PyObject *args)
43834391
}
43844392

43854393
static PyObject *
4386-
BufferMark(BufferObject *self, PyObject *args)
4394+
BufferMark(BufferObject *self, PyObject *pmarkObject)
43874395
{
43884396
pos_T *posp;
4389-
char *pmark;
4390-
char mark;
4397+
char_u *pmark;
4398+
char_u mark;
43914399
buf_T *savebuf;
4400+
PyObject *todecref;
43924401

43934402
if (CheckBuffer(self))
43944403
return NULL;
43954404

4396-
if (!PyArg_ParseTuple(args, "s", &pmark))
4405+
if (!(pmark = StringToChars(pmarkObject, &todecref)))
43974406
return NULL;
43984407

4399-
if (STRLEN(pmark) != 1)
4408+
if (pmark[0] == '\0' || pmark[1] != '\0')
44004409
{
44014410
PyErr_SetString(PyExc_ValueError,
44024411
_("mark name must be a single character"));
44034412
return NULL;
44044413
}
44054414

44064415
mark = *pmark;
4416+
4417+
Py_XDECREF(todecref);
4418+
44074419
VimTryStart();
44084420
switch_buffer(&savebuf, self->buf);
44094421
posp = getmark(mark, FALSE);
@@ -4461,7 +4473,7 @@ BufferRepr(BufferObject *self)
44614473
static struct PyMethodDef BufferMethods[] = {
44624474
/* name, function, calling, documentation */
44634475
{"append", (PyCFunction)BufferAppend, METH_VARARGS, "Append data to Vim buffer" },
4464-
{"mark", (PyCFunction)BufferMark, METH_VARARGS, "Return (row,col) representing position of named mark" },
4476+
{"mark", (PyCFunction)BufferMark, METH_O, "Return (row,col) representing position of named mark" },
44654477
{"range", (PyCFunction)BufferRange, METH_VARARGS, "Return a range object which represents the part of the given buffer between line numbers s and e" },
44664478
{"__dir__", (PyCFunction)BufferDir, METH_NOARGS, ""},
44674479
{ NULL, NULL, 0, NULL}

src/testdir/test86.ok

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,14 +446,14 @@ sys.stdout.write(None):TypeError:('coercing to Unicode: need string or buffer, N
446446
sys.stdout.writelines(None):TypeError:("'NoneType' object is not iterable",)
447447
sys.stdout.writelines([1]):TypeError:('coercing to Unicode: need string or buffer, int found',)
448448
> VimCommand
449-
vim.command(1):TypeError:('must be string, not int',)
449+
vim.command(1):TypeError:('object must be string',)
450450
> VimToPython
451451
> VimEval
452-
vim.eval(1):TypeError:('must be string, not int',)
452+
vim.eval(1):TypeError:('object must be string',)
453453
> VimEvalPy
454-
vim.bindeval(1):TypeError:('must be string, not int',)
454+
vim.bindeval(1):TypeError:('object must be string',)
455455
> VimStrwidth
456-
vim.strwidth(1):TypeError:('must be string, not int',)
456+
vim.strwidth(1):TypeError:('object must be string',)
457457
> Dictionary
458458
>> DictionaryConstructor
459459
vim.Dictionary("abc"):ValueError:('expected sequence element of size 2',)
@@ -683,7 +683,7 @@ d.update((("a", FailingMappingKey()),)):NotImplementedError:()
683683
>> DictionaryPopItem
684684
d.popitem(1, 2):TypeError:('popitem() takes no arguments (2 given)',)
685685
>> DictionaryHasKey
686-
d.has_key():TypeError:('function takes exactly 1 argument (0 given)',)
686+
d.has_key():TypeError:('has_key() takes exactly one argument (0 given)',)
687687
> List
688688
>> ListConstructor
689689
vim.List(1, 2):TypeError:('function takes at most 1 argument (2 given)',)
@@ -1065,7 +1065,7 @@ vim.current.buffer.xxx:AttributeError:('xxx',)
10651065
vim.current.buffer.name = True:TypeError:('object must be string',)
10661066
vim.current.buffer.xxx = True:AttributeError:('xxx',)
10671067
>> BufferMark
1068-
vim.current.buffer.mark(0):TypeError:('must be string, not int',)
1068+
vim.current.buffer.mark(0):TypeError:('object must be string',)
10691069
vim.current.buffer.mark("abc"):ValueError:('mark name must be a single character',)
10701070
vim.current.buffer.mark("!"):error:('invalid mark name',)
10711071
>> BufferRange

src/testdir/test87.ok

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,14 @@ sys.stdout.writelines(FailingIter()):(<class 'NotImplementedError'>, NotImplemen
439439
sys.stdout.writelines(FailingIterNext()):(<class 'NotImplementedError'>, NotImplementedError())
440440
<<< Finished
441441
> VimCommand
442-
vim.command(1):(<class 'TypeError'>, TypeError('must be str, not int',))
442+
vim.command(1):(<class 'TypeError'>, TypeError('object must be string',))
443443
> VimToPython
444444
> VimEval
445-
vim.eval(1):(<class 'TypeError'>, TypeError('must be str, not int',))
445+
vim.eval(1):(<class 'TypeError'>, TypeError('object must be string',))
446446
> VimEvalPy
447-
vim.bindeval(1):(<class 'TypeError'>, TypeError('must be str, not int',))
447+
vim.bindeval(1):(<class 'TypeError'>, TypeError('object must be string',))
448448
> VimStrwidth
449-
vim.strwidth(1):(<class 'TypeError'>, TypeError('must be str, not int',))
449+
vim.strwidth(1):(<class 'TypeError'>, TypeError('object must be string',))
450450
> Dictionary
451451
>> DictionaryConstructor
452452
vim.Dictionary("abc"):(<class 'ValueError'>, ValueError('expected sequence element of size 2',))
@@ -680,7 +680,7 @@ d.update((("a", FailingMappingKey()),)):(<class 'NotImplementedError'>, NotImple
680680
>> DictionaryPopItem
681681
d.popitem(1, 2):(<class 'TypeError'>, TypeError('popitem() takes no arguments (2 given)',))
682682
>> DictionaryHasKey
683-
d.has_key():(<class 'TypeError'>, TypeError('function takes exactly 1 argument (0 given)',))
683+
d.has_key():(<class 'TypeError'>, TypeError('has_key() takes exactly one argument (0 given)',))
684684
> List
685685
>> ListConstructor
686686
vim.List(1, 2):(<class 'TypeError'>, TypeError('function takes at most 1 argument (2 given)',))
@@ -1074,7 +1074,7 @@ vim.current.buffer.xxx:(<class 'AttributeError'>, AttributeError("'vim.buffer' o
10741074
vim.current.buffer.name = True:(<class 'TypeError'>, TypeError('object must be string',))
10751075
vim.current.buffer.xxx = True:(<class 'AttributeError'>, AttributeError('xxx',))
10761076
>> BufferMark
1077-
vim.current.buffer.mark(0):(<class 'TypeError'>, TypeError('must be str, not int',))
1077+
vim.current.buffer.mark(0):(<class 'TypeError'>, TypeError('object must be string',))
10781078
vim.current.buffer.mark("abc"):(<class 'ValueError'>, ValueError('mark name must be a single character',))
10791079
vim.current.buffer.mark("!"):(<class 'vim.error'>, error('invalid mark name',))
10801080
>> BufferRange

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+
1227,
731733
/**/
732734
1226,
733735
/**/

0 commit comments

Comments
 (0)