[Python-checkins] r69044 - in python/branches/io-c/Modules: _iomodule.h _textio.c io.c

antoine.pitrou python-checkins at python.org
Wed Jan 28 01:51:07 CET 2009


Author: antoine.pitrou
Date: Wed Jan 28 01:51:07 2009
New Revision: 69044

Log:
Improve heuristic in IncrementalNewlineDecoder + some micro-optimizations



Modified:
   python/branches/io-c/Modules/_iomodule.h
   python/branches/io-c/Modules/_textio.c
   python/branches/io-c/Modules/io.c

Modified: python/branches/io-c/Modules/_iomodule.h
==============================================================================
--- python/branches/io-c/Modules/_iomodule.h	(original)
+++ python/branches/io-c/Modules/_iomodule.h	Wed Jan 28 01:51:07 2009
@@ -107,6 +107,7 @@
 extern PyObject *_PyIO_str_readable;
 extern PyObject *_PyIO_str_readinto;
 extern PyObject *_PyIO_str_readline;
+extern PyObject *_PyIO_str_reset;
 extern PyObject *_PyIO_str_seek;
 extern PyObject *_PyIO_str_seekable;
 extern PyObject *_PyIO_str_tell;

Modified: python/branches/io-c/Modules/_textio.c
==============================================================================
--- python/branches/io-c/Modules/_textio.c	(original)
+++ python/branches/io-c/Modules/_textio.c	Wed Jan 28 01:51:07 2009
@@ -283,7 +283,7 @@
         Py_UNICODE *in_str;
         Py_ssize_t len;
         int seennl = self->seennl;
-        int only_lf;
+        int only_lf = 0;
 
         in_str = PyUnicode_AS_UNICODE(output);
         len = PyUnicode_GET_SIZE(output);
@@ -294,8 +294,16 @@
         /* If, up to now, newlines are consistently \n, do a quick check
            for the \r *byte* with the libc's optimized memchr.
            */
-        only_lf = ((seennl == SEEN_LF)
-                && !memchr(in_str, '\r', len * sizeof(Py_UNICODE)));
+        if (seennl == SEEN_LF || seennl == 0) {
+            int has_cr, has_lf;
+            has_lf = (seennl == SEEN_LF) ||
+                    (memchr(in_str, '\n', len * sizeof(Py_UNICODE)) != NULL);
+            has_cr = (memchr(in_str, '\r', len * sizeof(Py_UNICODE)) != NULL);
+            if (has_lf && !has_cr) {
+                only_lf = 1;
+                seennl = SEEN_LF;
+            }
+        }
 
         if (!self->translate) {
             Py_UNICODE *s, *end;
@@ -453,7 +461,7 @@
 {
     self->seennl = 0;
     self->pendingcr = 0;
-    return PyObject_CallMethod(self->decoder, "reset", NULL);
+    return PyObject_CallMethodObjArgs(self->decoder, _PyIO_str_reset, NULL);
 }
 
 static PyObject *
@@ -1729,7 +1737,7 @@
        utf-16, that we are expecting a BOM).
     */
     if (cookie->start_pos == 0 && cookie->dec_flags == 0)
-        res = PyObject_CallMethod(self->decoder, "reset", NULL);
+        res = PyObject_CallMethodObjArgs(self->decoder, _PyIO_str_reset, NULL);
     else
         res = PyObject_CallMethod(self->decoder, "setstate",
                                   "((yi))", "", cookie->dec_flags);
@@ -1742,7 +1750,7 @@
 static PyObject *
 TextIOWrapper_seek(PyTextIOWrapperObject *self, PyObject *args)
 {
-    PyObject *cookieObj;
+    PyObject *cookieObj, *posobj;
     CookieStruct cookie;
     int whence = 0;
     static PyObject *zero = NULL;
@@ -1835,7 +1843,7 @@
         goto fail;
     }
 
-    res = PyObject_CallMethod((PyObject *)self, "flush", NULL);
+    res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL);
     if (res == NULL)
         goto fail;
     Py_DECREF(res);
@@ -1847,8 +1855,12 @@
         goto fail;
 
     /* Seek back to the safe start point. */
-    res = PyObject_CallMethod(self->buffer, "seek",
-                              "L", (PY_LONG_LONG)cookie.start_pos);
+    posobj = PyLong_FromOff_t(cookie.start_pos);
+    if (posobj == NULL)
+        goto fail;
+    res = PyObject_CallMethodObjArgs(self->buffer,
+                                     _PyIO_str_seek, posobj, NULL);
+    Py_DECREF(posobj);
     if (res == NULL)
         goto fail;
     Py_DECREF(res);

Modified: python/branches/io-c/Modules/io.c
==============================================================================
--- python/branches/io-c/Modules/io.c	(original)
+++ python/branches/io-c/Modules/io.c	Wed Jan 28 01:51:07 2009
@@ -38,6 +38,7 @@
 PyObject *_PyIO_str_readable;
 PyObject *_PyIO_str_readinto;
 PyObject *_PyIO_str_readline;
+PyObject *_PyIO_str_reset;
 PyObject *_PyIO_str_seek;
 PyObject *_PyIO_str_seekable;
 PyObject *_PyIO_str_tell;
@@ -722,6 +723,8 @@
         goto fail;
     if (!(_PyIO_str_readline = PyUnicode_InternFromString("readline")))
         goto fail;
+    if (!(_PyIO_str_reset = PyUnicode_InternFromString("reset")))
+        goto fail;
     if (!(_PyIO_str_seek = PyUnicode_InternFromString("seek")))
         goto fail;
     if (!(_PyIO_str_seekable = PyUnicode_InternFromString("seekable")))


More information about the Python-checkins mailing list