[Python-checkins] r68077 - sandbox/trunk/io-c/_textio.c

antoine.pitrou python-checkins at python.org
Tue Dec 30 19:47:22 CET 2008


Author: antoine.pitrou
Date: Tue Dec 30 19:47:20 2008
New Revision: 68077

Log:
Another patch by Amaury.




Modified:
   sandbox/trunk/io-c/_textio.c

Modified: sandbox/trunk/io-c/_textio.c
==============================================================================
--- sandbox/trunk/io-c/_textio.c	(original)
+++ sandbox/trunk/io-c/_textio.c	Tue Dec 30 19:47:20 2008
@@ -258,7 +258,7 @@
     if (!PyArg_Parse(state, "(OK)", &buffer, &flag))
         return NULL;
 
-    self->pendingcr = flag & 1;
+    self->pendingcr = (int) flag & 1;
     flag >>= 1;
 
     return PyObject_CallMethod(self->decoder, "setstate", "((OK))", buffer, flag);
@@ -652,6 +652,8 @@
     avail = (PyUnicode_GET_SIZE(self->decoded_chars)
              - self->decoded_chars_used);
 
+    assert(avail >= 0);
+
     if (n < 0 || n > avail)
         n = avail;
 
@@ -1011,7 +1013,7 @@
 TextIOWrapper_parseCookie(CookieStruct *cookie, PyObject *cookieObj)
 {
     char buffer[sizeof(CookieStruct)];
-    static int one = 1;
+    int one = 1;
     PyLongObject *cookieLong = (PyLongObject *)PyNumber_Long(cookieObj);
     if (cookieLong == NULL)
         return -1;
@@ -1055,7 +1057,7 @@
 TextIOWrapper_buildCookie(CookieStruct *cookie)
 {
     char buffer[sizeof(CookieStruct)];
-    static int one = 1;
+    int one = 1;
 
     if (!IS_LITTLE_ENDIAN) {
         *(Py_off_t*)(buffer +
@@ -1323,6 +1325,7 @@
                               "((yi))", "", cookie.dec_flags);
     if (res == NULL)
         goto fail;
+    Py_DECREF(res);
 
     /* Feed the decoder one byte at a time.  As we go, note the
      * nearest "safe start point" before the current location
@@ -1339,7 +1342,7 @@
         int dec_flags;
 
         PyObject *decoded = PyObject_CallMethod(
-            self->decoder, "decode", "y#", &input, 1);
+            self->decoder, "decode", "y#", input, 1);
         if (decoded == NULL)
             goto fail;
         assert (PyUnicode_Check(decoded));
@@ -1348,7 +1351,7 @@
 
         cookie.bytes_to_feed += 1;
 
-        state = PyObject_CallMethod(self->decoder, "getstate", "NULL");
+        state = PyObject_CallMethod(self->decoder, "getstate", NULL);
         if (state == NULL)
             goto fail;
         if (!PyArg_Parse(state, "(y#i)", &dec_buffer, &dec_buffer_len, &dec_flags)) {
@@ -1357,7 +1360,7 @@
         }
         Py_DECREF(state);
 
-        if (dec_buffer_len == 0) {
+        if (dec_buffer_len == 0 && chars_decoded <= chars_to_skip) {
             /* Decoder buffer is empty, so this is a safe start point. */
             cookie.start_pos += cookie.bytes_to_feed;
             chars_to_skip -= chars_decoded;
@@ -1367,8 +1370,9 @@
         }
         if (chars_decoded >= chars_to_skip)
             break;
+        input++;
     }
-    if (chars_decoded < chars_to_skip) {
+    if (input == input_end) {
         /* We didn't get enough decoded data; signal EOF to get more. */
         PyObject *decoded = PyObject_CallMethod(
             self->decoder, "decode", "yi", "", /* final = */ 1);


More information about the Python-checkins mailing list