changeset 4619:90beab957ba9 v7.3.1057

updated for version 7.3.1057 Problem: Python: not enough compatibilty. Solution: Python patch 16: Make OutputWritelines support any sequence object (ZyX) Note: tests fail
author Bram Moolenaar <bram@vim.org>
date Thu, 30 May 2013 12:26:58 +0200
parents 0b378d6158fd
children 980dfdc910b3
files src/if_py_both.h src/testdir/test86.in src/testdir/test86.ok src/testdir/test87.in src/testdir/test87.ok src/version.c
diffstat 6 files changed, 55 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/if_py_both.h
+++ b/src/if_py_both.h
@@ -312,36 +312,30 @@ OutputWrite(OutputObject *self, PyObject
     static PyObject *
 OutputWritelines(OutputObject *self, PyObject *args)
 {
-    PyInt n;
-    PyInt i;
-    PyObject *list;
+    PyObject	*seq;
+    PyObject	*iterator;
+    PyObject	*item;
     int error = self->error;
 
-    if (!PyArg_ParseTuple(args, "O", &list))
+    if (!PyArg_ParseTuple(args, "O", &seq))
 	return NULL;
-    Py_INCREF(list);
-
-    if (!PyList_Check(list))
+
+    if (!(iterator = PyObject_GetIter(seq)))
+	return NULL;
+
+    while ((item = PyIter_Next(iterator)))
     {
-	PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
-	Py_DECREF(list);
-	return NULL;
-    }
-
-    n = PyList_Size(list);
-
-    for (i = 0; i < n; ++i)
-    {
-	PyObject *line = PyList_GetItem(list, i);
 	char *str = NULL;
 	PyInt len;
 
-	if (!PyArg_Parse(line, "et#", ENC_OPT, &str, &len))
+	if (!PyArg_Parse(item, "et#", ENC_OPT, &str, &len))
 	{
 	    PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
-	    Py_DECREF(list);
+	    Py_DECREF(iterator);
+	    Py_DECREF(item);
 	    return NULL;
 	}
+	Py_DECREF(item);
 
 	Py_BEGIN_ALLOW_THREADS
 	Python_Lock_Vim();
@@ -351,7 +345,12 @@ OutputWritelines(OutputObject *self, PyO
 	PyMem_Free(str);
     }
 
-    Py_DECREF(list);
+    Py_DECREF(iterator);
+
+    /* Iterator may have finished due to an exception */
+    if (PyErr_Occurred())
+	return NULL;
+
     Py_INCREF(Py_None);
     return Py_None;
 }
--- a/src/testdir/test86.in
+++ b/src/testdir/test86.in
@@ -709,6 +709,16 @@ del name
 del o
 EOF
 :"
+:"
+:" Test stdout/stderr
+:redir => messages
+:py sys.stdout.write('abc') ; sys.stdout.write('def')
+:py sys.stderr.write('abc') ; sys.stderr.write('def')
+:py sys.stdout.writelines(iter('abc'))
+:py sys.stderr.writelines(iter('abc'))
+:redir END
+:$put =string(substitute(messages, '\d\+', '', 'g'))
+:"
 :" Test exceptions
 :fun Exe(e)
 :   execute a:e
--- a/src/testdir/test86.ok
+++ b/src/testdir/test86.ok
@@ -391,6 +391,13 @@ dictionary:__dir__,__members__,keys,lock
 list:__dir__,__members__,extend,locked
 function:__call__,__dir__,__members__,softspace
 output:__dir__,__members__,flush,softspace,write,writelines
+'
+abcdef
+line  :
+abcdef
+abc
+line  :
+abc'
 (<class 'vim.error'>, error('abc',))
 (<class 'vim.error'>, error('def',))
 (<class 'vim.error'>, error('ghi',))
--- a/src/testdir/test87.in
+++ b/src/testdir/test87.in
@@ -687,6 +687,16 @@ del name
 del o
 EOF
 :"
+:"
+:" Test stdout/stderr
+:redir => messages
+:py sys.stdout.write('abc') ; sys.stdout.write('def')
+:py sys.stderr.write('abc') ; sys.stderr.write('def')
+:py sys.stdout.writelines(iter('abc'))
+:py sys.stderr.writelines(iter('abc'))
+:redir END
+:$put =string(substitute(messages, '\d\+', '', 'g'))
+:"
 :" Test exceptions
 :fun Exe(e)
 :   execute a:e
--- a/src/testdir/test87.ok
+++ b/src/testdir/test87.ok
@@ -380,6 +380,13 @@ dictionary:__dir__,keys,locked,scope
 list:__dir__,extend,locked
 function:__call__,__dir__,softspace
 output:__dir__,flush,softspace,write,writelines
+'
+abcdef
+line  :
+abcdef
+abc
+line  :
+abc'
 (<class 'vim.error'>, error('abc',))
 (<class 'vim.error'>, error('def',))
 (<class 'vim.error'>, error('ghi',))
--- a/src/version.c
+++ b/src/version.c
@@ -729,6 +729,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1057,
+/**/
     1056,
 /**/
     1055,