# HG changeset patch
# User ZyX <[email protected]>
# Date 1371218395 -14400
#      Fri Jun 14 17:59:55 2013 +0400
# Branch python-fixes
# Node ID d0320d1f7ff29076f01fb39e431446f5c98e8ba2
# Parent  8991454c00246d1561474090cfbbb8cda7c88dd5
Use same code both for OutputWrite and OutputWritelines

diff -r 8991454c0024 -r d0320d1f7ff2 src/if_py_both.h
--- a/src/if_py_both.h  Sun Jun 16 16:57:46 2013 +0200
+++ b/src/if_py_both.h  Fri Jun 14 17:59:55 2013 +0400
@@ -281,15 +281,15 @@
     }
 }
 
-    static PyObject *
-OutputWrite(OutputObject *self, PyObject *args)
-{
-    Py_ssize_t len = 0;
-    char *str = NULL;
-    int error = self->error;
-
-    if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
-       return NULL;
+    static int
+write_output(OutputObject *self, PyObject *string)
+{
+    Py_ssize_t len = 0;
+    char       *str = NULL;
+    int                error = self->error;
+
+    if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
+       return -1;
 
     Py_BEGIN_ALLOW_THREADS
     Python_Lock_Vim();
@@ -298,44 +298,38 @@
     Py_END_ALLOW_THREADS
     PyMem_Free(str);
 
+    return 0;
+}
+
+    static PyObject *
+OutputWrite(OutputObject *self, PyObject *string)
+{
+    if (write_output(self, string))
+       return NULL;
+
     Py_INCREF(Py_None);
     return Py_None;
 }
 
     static PyObject *
-OutputWritelines(OutputObject *self, PyObject *args)
-{
-    PyObject   *seq;
+OutputWritelines(OutputObject *self, PyObject *seq)
+{
     PyObject   *iterator;
     PyObject   *item;
     int error = self->error;
 
-    if (!PyArg_ParseTuple(args, "O", &seq))
-       return NULL;
-
     if (!(iterator = PyObject_GetIter(seq)))
        return NULL;
 
     while ((item = PyIter_Next(iterator)))
     {
-       char *str = NULL;
-       PyInt len;
-
-       if (!PyArg_Parse(item, "et#", ENC_OPT, &str, &len))
+       if (write_output(self, item))
        {
-           PyErr_SetString(PyExc_TypeError, _("writelines() requires list of 
strings"));
            Py_DECREF(iterator);
            Py_DECREF(item);
            return NULL;
        }
        Py_DECREF(item);
-
-       Py_BEGIN_ALLOW_THREADS
-       Python_Lock_Vim();
-       writer((writefn)(error ? emsg : msg), (char_u *)str, len);
-       Python_Release_Vim();
-       Py_END_ALLOW_THREADS
-       PyMem_Free(str);
     }
 
     Py_DECREF(iterator);
@@ -360,8 +354,8 @@
 
 static struct PyMethodDef OutputMethods[] = {
     /* name,       function,                           calling,        doc */
-    {"write",      (PyCFunction)OutputWrite,           METH_VARARGS,   ""},
-    {"writelines",  (PyCFunction)OutputWritelines,     METH_VARARGS,   ""},
+    {"write",      (PyCFunction)OutputWrite,           METH_O,         ""},
+    {"writelines",  (PyCFunction)OutputWritelines,     METH_O,         ""},
     {"flush",      (PyCFunction)OutputFlush,           METH_NOARGS,    ""},
     {"__dir__",            (PyCFunction)OutputDir,             METH_NOARGS,    
""},
     { NULL,        NULL,                               0,              NULL}
diff -r 8991454c0024 -r d0320d1f7ff2 src/testdir/test86.ok
--- a/src/testdir/test86.ok     Sun Jun 16 16:57:46 2013 +0200
+++ b/src/testdir/test86.ok     Fri Jun 14 17:59:55 2013 +0400
@@ -444,7 +444,7 @@
 sys.stdout.write(None):TypeError:('coercing to Unicode: need string or buffer, 
NoneType found',)
 >> OutputWriteLines
 sys.stdout.writelines(None):TypeError:("'NoneType' object is not iterable",)
-sys.stdout.writelines([1]):TypeError:('writelines() requires list of strings',)
+sys.stdout.writelines([1]):TypeError:('coercing to Unicode: need string or 
buffer, int found',)
 > VimCommand
 vim.command(1):TypeError:('must be string, not int',)
 > VimToPython
diff -r 8991454c0024 -r d0320d1f7ff2 src/testdir/test87.ok
--- a/src/testdir/test87.ok     Sun Jun 16 16:57:46 2013 +0200
+++ b/src/testdir/test87.ok     Fri Jun 14 17:59:55 2013 +0400
@@ -433,7 +433,7 @@
 sys.stdout.write(None):(<class 'TypeError'>, TypeError("Can't convert 
'NoneType' object to str implicitly",))
 >> OutputWriteLines
 sys.stdout.writelines(None):(<class 'TypeError'>, TypeError("'NoneType' object 
is not iterable",))
-sys.stdout.writelines([1]):(<class 'TypeError'>, TypeError('writelines() 
requires list of strings',))
+sys.stdout.writelines([1]):(<class 'TypeError'>, TypeError("Can't convert 
'int' object to str implicitly",))
 >>> Testing *Iter* using sys.stdout.writelines(%s)
 sys.stdout.writelines(FailingIter()):(<class 'NotImplementedError'>, 
NotImplementedError())
 sys.stdout.writelines(FailingIterNext()):(<class 'NotImplementedError'>, 
NotImplementedError())

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


diff -crN vim.8991454c0024/src/if_py_both.h vim.d0320d1f7ff2/src/if_py_both.h
*** vim.8991454c0024/src/if_py_both.h	2013-06-16 19:21:59.548903375 +0400
--- vim.d0320d1f7ff2/src/if_py_both.h	2013-06-16 19:21:59.578903087 +0400
***************
*** 281,295 ****
      }
  }
  
!     static PyObject *
! OutputWrite(OutputObject *self, PyObject *args)
  {
!     Py_ssize_t len = 0;
!     char *str = NULL;
!     int error = self->error;
  
!     if (!PyArg_ParseTuple(args, "et#", ENC_OPT, &str, &len))
! 	return NULL;
  
      Py_BEGIN_ALLOW_THREADS
      Python_Lock_Vim();
--- 281,295 ----
      }
  }
  
!     static int
! write_output(OutputObject *self, PyObject *string)
  {
!     Py_ssize_t	len = 0;
!     char	*str = NULL;
!     int		error = self->error;
  
!     if (!PyArg_Parse(string, "et#", ENC_OPT, &str, &len))
! 	return -1;
  
      Py_BEGIN_ALLOW_THREADS
      Python_Lock_Vim();
***************
*** 298,341 ****
      Py_END_ALLOW_THREADS
      PyMem_Free(str);
  
      Py_INCREF(Py_None);
      return Py_None;
  }
  
      static PyObject *
! OutputWritelines(OutputObject *self, PyObject *args)
  {
-     PyObject	*seq;
      PyObject	*iterator;
      PyObject	*item;
      int error = self->error;
  
-     if (!PyArg_ParseTuple(args, "O", &seq))
- 	return NULL;
- 
      if (!(iterator = PyObject_GetIter(seq)))
  	return NULL;
  
      while ((item = PyIter_Next(iterator)))
      {
! 	char *str = NULL;
! 	PyInt len;
! 
! 	if (!PyArg_Parse(item, "et#", ENC_OPT, &str, &len))
  	{
- 	    PyErr_SetString(PyExc_TypeError, _("writelines() requires list of strings"));
  	    Py_DECREF(iterator);
  	    Py_DECREF(item);
  	    return NULL;
  	}
  	Py_DECREF(item);
- 
- 	Py_BEGIN_ALLOW_THREADS
- 	Python_Lock_Vim();
- 	writer((writefn)(error ? emsg : msg), (char_u *)str, len);
- 	Python_Release_Vim();
- 	Py_END_ALLOW_THREADS
- 	PyMem_Free(str);
      }
  
      Py_DECREF(iterator);
--- 298,335 ----
      Py_END_ALLOW_THREADS
      PyMem_Free(str);
  
+     return 0;
+ }
+ 
+     static PyObject *
+ OutputWrite(OutputObject *self, PyObject *string)
+ {
+     if (write_output(self, string))
+ 	return NULL;
+ 
      Py_INCREF(Py_None);
      return Py_None;
  }
  
      static PyObject *
! OutputWritelines(OutputObject *self, PyObject *seq)
  {
      PyObject	*iterator;
      PyObject	*item;
      int error = self->error;
  
      if (!(iterator = PyObject_GetIter(seq)))
  	return NULL;
  
      while ((item = PyIter_Next(iterator)))
      {
! 	if (write_output(self, item))
  	{
  	    Py_DECREF(iterator);
  	    Py_DECREF(item);
  	    return NULL;
  	}
  	Py_DECREF(item);
      }
  
      Py_DECREF(iterator);
***************
*** 360,367 ****
  
  static struct PyMethodDef OutputMethods[] = {
      /* name,	    function,				calling,	doc */
!     {"write",	    (PyCFunction)OutputWrite,		METH_VARARGS,	""},
!     {"writelines",  (PyCFunction)OutputWritelines,	METH_VARARGS,	""},
      {"flush",	    (PyCFunction)OutputFlush,		METH_NOARGS,	""},
      {"__dir__",	    (PyCFunction)OutputDir,		METH_NOARGS,	""},
      { NULL,	    NULL,				0,		NULL}
--- 354,361 ----
  
  static struct PyMethodDef OutputMethods[] = {
      /* name,	    function,				calling,	doc */
!     {"write",	    (PyCFunction)OutputWrite,		METH_O,		""},
!     {"writelines",  (PyCFunction)OutputWritelines,	METH_O,		""},
      {"flush",	    (PyCFunction)OutputFlush,		METH_NOARGS,	""},
      {"__dir__",	    (PyCFunction)OutputDir,		METH_NOARGS,	""},
      { NULL,	    NULL,				0,		NULL}
diff -crN vim.8991454c0024/src/testdir/test86.ok vim.d0320d1f7ff2/src/testdir/test86.ok
*** vim.8991454c0024/src/testdir/test86.ok	2013-06-16 19:21:59.538903471 +0400
--- vim.d0320d1f7ff2/src/testdir/test86.ok	2013-06-16 19:21:59.568903183 +0400
***************
*** 444,450 ****
  sys.stdout.write(None):TypeError:('coercing to Unicode: need string or buffer, NoneType found',)
  >> OutputWriteLines
  sys.stdout.writelines(None):TypeError:("'NoneType' object is not iterable",)
! sys.stdout.writelines([1]):TypeError:('writelines() requires list of strings',)
  > VimCommand
  vim.command(1):TypeError:('must be string, not int',)
  > VimToPython
--- 444,450 ----
  sys.stdout.write(None):TypeError:('coercing to Unicode: need string or buffer, NoneType found',)
  >> OutputWriteLines
  sys.stdout.writelines(None):TypeError:("'NoneType' object is not iterable",)
! sys.stdout.writelines([1]):TypeError:('coercing to Unicode: need string or buffer, int found',)
  > VimCommand
  vim.command(1):TypeError:('must be string, not int',)
  > VimToPython
diff -crN vim.8991454c0024/src/testdir/test87.ok vim.d0320d1f7ff2/src/testdir/test87.ok
*** vim.8991454c0024/src/testdir/test87.ok	2013-06-16 19:21:59.540903452 +0400
--- vim.d0320d1f7ff2/src/testdir/test87.ok	2013-06-16 19:21:59.570903164 +0400
***************
*** 433,439 ****
  sys.stdout.write(None):(<class 'TypeError'>, TypeError("Can't convert 'NoneType' object to str implicitly",))
  >> OutputWriteLines
  sys.stdout.writelines(None):(<class 'TypeError'>, TypeError("'NoneType' object is not iterable",))
! sys.stdout.writelines([1]):(<class 'TypeError'>, TypeError('writelines() requires list of strings',))
  >>> Testing *Iter* using sys.stdout.writelines(%s)
  sys.stdout.writelines(FailingIter()):(<class 'NotImplementedError'>, NotImplementedError())
  sys.stdout.writelines(FailingIterNext()):(<class 'NotImplementedError'>, NotImplementedError())
--- 433,439 ----
  sys.stdout.write(None):(<class 'TypeError'>, TypeError("Can't convert 'NoneType' object to str implicitly",))
  >> OutputWriteLines
  sys.stdout.writelines(None):(<class 'TypeError'>, TypeError("'NoneType' object is not iterable",))
! sys.stdout.writelines([1]):(<class 'TypeError'>, TypeError("Can't convert 'int' object to str implicitly",))
  >>> Testing *Iter* using sys.stdout.writelines(%s)
  sys.stdout.writelines(FailingIter()):(<class 'NotImplementedError'>, NotImplementedError())
  sys.stdout.writelines(FailingIterNext()):(<class 'NotImplementedError'>, NotImplementedError())

Raspunde prin e-mail lui