[Python-checkins] r57019 - python/branches/alex-py3k/Modules/_bytesiomodule.c python/branches/alex-py3k/Modules/_stringiomodule.c

alexandre.vassalotti python-checkins at python.org
Tue Aug 14 16:25:53 CEST 2007


Author: alexandre.vassalotti
Date: Tue Aug 14 16:25:52 2007
New Revision: 57019
Modified:
 python/branches/alex-py3k/Modules/_bytesiomodule.c
 python/branches/alex-py3k/Modules/_stringiomodule.c
Log:
Remove duplicate code in writelines() by using the write() method.
Modified: python/branches/alex-py3k/Modules/_bytesiomodule.c
==============================================================================
--- python/branches/alex-py3k/Modules/_bytesiomodule.c	(original)
+++ python/branches/alex-py3k/Modules/_bytesiomodule.c	Tue Aug 14 16:25:52 2007
@@ -415,8 +415,7 @@
 bytesio_writelines(BytesIOObject *self, PyObject *v)
 {
 PyObject *it, *item;
- const char *bytes;
- Py_ssize_t len;
+ PyObject *ret;
 
 if (self->buf == NULL)
 return err_closed();
@@ -426,17 +425,11 @@
 return NULL;
 
 while ((item = PyIter_Next(it)) != NULL) {
- if (PyObject_AsCharBuffer(item, &bytes, &len) < 0) {
- Py_DECREF(it);
- Py_DECREF(item);
+ ret = bytesio_write(self, item);
+ if (ret == NULL)
 return NULL;
- }
+ Py_DECREF(ret);
 Py_DECREF(item);
-
- if (write_bytes(self, bytes, len) == -1) {
- Py_DECREF(it);
- return NULL;
- }
 }
 Py_DECREF(it);
 
Modified: python/branches/alex-py3k/Modules/_stringiomodule.c
==============================================================================
--- python/branches/alex-py3k/Modules/_stringiomodule.c	(original)
+++ python/branches/alex-py3k/Modules/_stringiomodule.c	Tue Aug 14 16:25:52 2007
@@ -363,30 +363,36 @@
 }
 
 static PyObject *
-stringio_write(StringIOObject *self, PyObject *args)
+stringio_write(StringIOObject *self, PyObject *obj)
 {
- const Py_UNICODE *ustr;
- Py_ssize_t len, n;
+ const Py_UNICODE *str;
+ Py_ssize_t size, n;
 
 if (self->buf == NULL)
 return err_closed();
 
- if (!PyArg_ParseTuple(args,
- "u#;write() may only be called on"
- " unicode strings", &ustr, &n))
+ if (PyUnicode_Check(obj)) {
+ str = PyUnicode_AsUnicode(obj);
+ size = PyUnicode_GetSize(obj);
+ }
+ else {
+ PyErr_Format(PyExc_TypeError, "expected a string, got %s instead",
+ Py_Type(obj)->tp_name);
 return NULL;
+ }
 
- len = write_str(self, ustr, n);
- if (len == -1)
+ n = write_str(self, str, size);
+ if (n == -1)
 return NULL;
 
- return PyInt_FromSsize_t(len);
+ return PyInt_FromSsize_t(n);
 }
 
 static PyObject *
 stringio_writelines(StringIOObject *self, PyObject *v)
 {
 PyObject *it, *item;
+ PyObject *ret;
 
 if (self->buf == NULL)
 return err_closed();
@@ -396,22 +402,11 @@
 return NULL;
 
 while ((item = PyIter_Next(it)) != NULL) {
- Py_ssize_t n;
- Py_UNICODE *ustr;
- if ((ustr = PyUnicode_AsUnicode(item)) == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "Need a list of unicode objects");
- Py_DECREF(it);
- Py_DECREF(item);
+ ret = stringio_write(self, item);
+ if (ret == NULL)
 return NULL;
- }
- n = PyUnicode_GetSize(item);
+ Py_DECREF(ret);
 Py_DECREF(item);
-
- if (write_str(self, ustr, n) == -1) {
- Py_DECREF(it);
- return NULL;
- }
 }
 Py_DECREF(it);
 
@@ -593,7 +588,7 @@
 StringIO_close_doc},
 {"seek", (PyCFunction) stringio_seek, METH_VARARGS,
 StringIO_seek_doc},
- {"write", (PyCFunction) stringio_write, METH_VARARGS,
+ {"write", (PyCFunction) stringio_write, METH_O,
 StringIO_write_doc},
 {"writelines", (PyCFunction) stringio_writelines, METH_O,
 StringIO_writelines_doc},


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /