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

alexandre.vassalotti python-checkins at python.org
Fri Aug 17 23:00:08 CEST 2007


Author: alexandre.vassalotti
Date: Fri Aug 17 23:00:07 2007
New Revision: 57161

Modified:
   python/branches/alex-py3k/Modules/_stringiomodule.c
Log:
Add support for str8 objects.
Remove some trailing whitespace.
Change 'i' to 'n' in PyArg_ParseTuple() format string.
Add an arrestion in read().


Modified: python/branches/alex-py3k/Modules/_stringiomodule.c
==============================================================================
--- python/branches/alex-py3k/Modules/_stringiomodule.c	(original)
+++ python/branches/alex-py3k/Modules/_stringiomodule.c	Fri Aug 17 23:00:07 2007
@@ -138,7 +138,7 @@
                    PyUnicode_GetSize(value))) < 0) {
         return -1;  /* out of memory */
     }
-    /* Reset the position back to beginning-of-file, since 
+    /* Reset the position back to beginning-of-file, since
        write_str changed it. */
     self->pos = 0;
 
@@ -174,6 +174,7 @@
             n = 0;
     }
 
+    assert(self->buf != NULL);
     output = self->buf + self->pos;
     self->pos += n;
 
@@ -186,7 +187,7 @@
     Py_ssize_t n, size = -1;
     Py_UNICODE *output;
 
-    if (!PyArg_ParseTuple(args, "|i:readline", &size))
+    if (!PyArg_ParseTuple(args, "|n:readline", &size))
         return NULL;
 
     n = get_line(self, &output);
@@ -207,7 +208,7 @@
     PyObject *result, *line;
     Py_UNICODE *output;
 
-    if (!PyArg_ParseTuple(args, "|i:readlines", &size))
+    if (!PyArg_ParseTuple(args, "|n:readlines", &size))
         return NULL;
 
     result = PyList_New(0);
@@ -319,18 +320,28 @@
 {
     const Py_UNICODE *str;
     Py_ssize_t size, n;
+    PyObject *ustr = NULL;
 
     if (PyUnicode_Check(obj)) {
         str = PyUnicode_AsUnicode(obj);
         size = PyUnicode_GetSize(obj);
     }
+    /* Temporary condition for str8 objects. */
+    else if (PyString_Check(obj)) {
+        ustr = PyObject_Unicode(obj);
+        if (ustr == NULL)
+            return NULL;
+        str = PyUnicode_AsUnicode(ustr);
+        size = PyUnicode_GetSize(ustr);
+    }
     else {
-        PyErr_Format(PyExc_TypeError, "expected a string, got %s instead",
+        PyErr_Format(PyExc_TypeError, "string argument expected, got %s",
                      Py_Type(obj)->tp_name);
         return NULL;
     }
 
     n = write_str(self, str, size);
+    Py_XDECREF(ustr);
     if (n == -1)
         return NULL;
 
@@ -501,7 +512,7 @@
      generic_true_doc},
     {"seekable",   (PyCFunction)generic_true, METH_NOARGS,
      generic_true_doc},
-    {"writable",   (PyCFunction)generic_true, METH_NOARGS, 
+    {"writable",   (PyCFunction)generic_true, METH_NOARGS,
      generic_true_doc},
     {"flush",      (PyCFunction)stringio_flush, METH_NOARGS,
      StringIO_flush_doc},


More information about the Python-checkins mailing list