Skip to content

Commit 83d8aa7

Browse files
committed
updated for version 7.3.1226
Problem: Python: duplicate code. Solution: Share code between OutputWrite() and OutputWritelines(). (ZyX)
1 parent bd9087f commit 83d8aa7

File tree

4 files changed

+26
-30
lines changed

4 files changed

+26
-30
lines changed

src/if_py_both.h

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,15 @@ writer(writefn fn, char_u *str, PyInt n)
281281
}
282282
}
283283

284-
static PyObject *
285-
OutputWrite(OutputObject *self, PyObject *args)
284+
static int
285+
write_output(OutputObject *self, PyObject *string)
286286
{
287-
Py_ssize_t len = 0;
288-
char *str = NULL;
289-
int error = self->error;
287+
Py_ssize_t len = 0;
288+
char *str = NULL;
289+
int error = self->error;
290290

291-
if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
292-
return NULL;
291+
if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
292+
return -1;
293293

294294
Py_BEGIN_ALLOW_THREADS
295295
Python_Lock_Vim();
@@ -298,44 +298,37 @@ OutputWrite(OutputObject *self, PyObject *args)
298298
Py_END_ALLOW_THREADS
299299
PyMem_Free(str);
300300

301+
return 0;
302+
}
303+
304+
static PyObject *
305+
OutputWrite(OutputObject *self, PyObject *string)
306+
{
307+
if (write_output(self, string))
308+
return NULL;
309+
301310
Py_INCREF(Py_None);
302311
return Py_None;
303312
}
304313

305314
static PyObject *
306-
OutputWritelines(OutputObject *self, PyObject *args)
315+
OutputWritelines(OutputObject *self, PyObject *seq)
307316
{
308-
PyObject *seq;
309317
PyObject *iterator;
310318
PyObject *item;
311-
int error = self->error;
312-
313-
if (!PyArg_ParseTuple(args, "O", &seq))
314-
return NULL;
315319

316320
if (!(iterator = PyObject_GetIter(seq)))
317321
return NULL;
318322

319323
while ((item = PyIter_Next(iterator)))
320324
{
321-
char *str = NULL;
322-
PyInt len;
323-
324-
if (!PyArg_Parse(item, "et#", ENC_OPT, &str, &len))
325+
if (write_output(self, item))
325326
{
326-
PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
327327
Py_DECREF(iterator);
328328
Py_DECREF(item);
329329
return NULL;
330330
}
331331
Py_DECREF(item);
332-
333-
Py_BEGIN_ALLOW_THREADS
334-
Python_Lock_Vim();
335-
writer((writefn)(error ? emsg : msg), (char_u *)str, len);
336-
Python_Release_Vim();
337-
Py_END_ALLOW_THREADS
338-
PyMem_Free(str);
339332
}
340333

341334
Py_DECREF(iterator);
@@ -360,8 +353,8 @@ OutputFlush(PyObject *self UNUSED)
360353

361354
static struct PyMethodDef OutputMethods[] = {
362355
/* name, function, calling, doc */
363-
{"write", (PyCFunction)OutputWrite, METH_VARARGS, ""},
364-
{"writelines", (PyCFunction)OutputWritelines, METH_VARARGS, ""},
356+
{"write", (PyCFunction)OutputWrite, METH_O, ""},
357+
{"writelines", (PyCFunction)OutputWritelines, METH_O, ""},
365358
{"flush", (PyCFunction)OutputFlush, METH_NOARGS, ""},
366359
{"__dir__", (PyCFunction)OutputDir, METH_NOARGS, ""},
367360
{ NULL, NULL, 0, NULL}
@@ -3009,7 +3002,8 @@ TabListItem(PyObject *self UNUSED, PyInt n)
30093002
return NULL;
30103003
}
30113004

3012-
/* Window object
3005+
/*
3006+
* Window object
30133007
*/
30143008

30153009
typedef struct

src/testdir/test86.ok

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ sys.stdout.attr = None:AttributeError:('invalid attribute',)
444444
sys.stdout.write(None):TypeError:('coercing to Unicode: need string or buffer, NoneType found',)
445445
>> OutputWriteLines
446446
sys.stdout.writelines(None):TypeError:("'NoneType' object is not iterable",)
447-
sys.stdout.writelines([1]):TypeError:('writelines() requires list of strings',)
447+
sys.stdout.writelines([1]):TypeError:('coercing to Unicode: need string or buffer, int found',)
448448
> VimCommand
449449
vim.command(1):TypeError:('must be string, not int',)
450450
> VimToPython

src/testdir/test87.ok

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ sys.stdout.attr = None:(<class 'AttributeError'>, AttributeError('invalid attrib
433433
sys.stdout.write(None):(<class 'TypeError'>, TypeError("Can't convert 'NoneType' object to str implicitly",))
434434
>> OutputWriteLines
435435
sys.stdout.writelines(None):(<class 'TypeError'>, TypeError("'NoneType' object is not iterable",))
436-
sys.stdout.writelines([1]):(<class 'TypeError'>, TypeError('writelines() requires list of strings',))
436+
sys.stdout.writelines([1]):(<class 'TypeError'>, TypeError("Can't convert 'int' object to str implicitly",))
437437
>>> Testing *Iter* using sys.stdout.writelines(%s)
438438
sys.stdout.writelines(FailingIter()):(<class 'NotImplementedError'>, NotImplementedError())
439439
sys.stdout.writelines(FailingIterNext()):(<class 'NotImplementedError'>, NotImplementedError())

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

0 commit comments

Comments
 (0)