[issue11351] (test_os) os.sendfile() error

Steffen Daode Nurpmeso report at bugs.python.org
Mon Feb 28 14:15:57 CET 2011


Steffen Daode Nurpmeso <sdaoden at googlemail.com> added the comment:

Did you know that 'sbytes' is not adjusted to match possibly 
existent headers and trailers in posixmodule.c? 
This however is required according to Mac OS X 'man 2 sendfile'.
I'll attach a simple, naive patch for a very intelligent and 
outstanding operating system.

----------
Added file: http://bugs.python.org/file20943/posixmodule.naive-apple-patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11351>
_______________________________________
-------------- next part --------------
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -5867,9 +5867,10 @@
 
 #ifdef HAVE_SENDFILE
 #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
-static int
+static off_t
 iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
 {
+    off_t ret = 0;
     int i, j;
     *iov = PyMem_New(struct iovec, cnt);
     if (*iov == NULL) {
@@ -5880,7 +5881,7 @@
     if (*buf == NULL) {
         PyMem_Del(*iov);
         PyErr_NoMemory();
-        return 0;
+        return ret;
     }
 
     for (i = 0; i < cnt; i++) {
@@ -5889,14 +5890,18 @@
             PyMem_Del(*iov);
             for (j = 0; j < i; j++) {
                 PyBuffer_Release(&(*buf)[j]);
-           }
+            }
             PyMem_Del(*buf);
-            return 0;
+            ret ^= ret;
+            return ret;
         }
         (*iov)[i].iov_base = (*buf)[i].buf;
-        (*iov)[i].iov_len = (*buf)[i].len;
-    }
-    return 1;
+        do {    off_t x = (*buf)[i].len;
+                (*iov)[i].iov_len = x;
+                ret += x;
+        } while (0);
+    }
+    return ret;
 }
 
 static void
@@ -5954,10 +5959,15 @@
                 "sendfile() headers must be a sequence or None");
             return NULL;
         } else {
+            off_t i = 0; /* Uninitialized warning */
             sf.hdr_cnt = PySequence_Size(headers);
-            if (sf.hdr_cnt > 0 && !iov_setup(&(sf.headers), &hbuf,
-                    headers, sf.hdr_cnt, PyBUF_SIMPLE))
+            if (sf.hdr_cnt > 0 &&
+                !(i = iov_setup(&(sf.headers), &hbuf,
+                                headers, sf.hdr_cnt, PyBUF_SIMPLE)))
                 return NULL;
+#ifdef __APPLE__
+            sbytes += i;
+#endif
         }
     }
     if (trailers != NULL) {
@@ -5966,10 +5976,15 @@
                 "sendfile() trailers must be a sequence or None");
             return NULL;
         } else {
+            off_t i = 0; /* Uninitialized warning */
             sf.trl_cnt = PySequence_Size(trailers);
-            if (sf.trl_cnt > 0 && !iov_setup(&(sf.trailers), &tbuf,
-                    trailers, sf.trl_cnt, PyBUF_SIMPLE))
+            if (sf.trl_cnt > 0 &&
+                !(i = iov_setup(&(sf.trailers), &tbuf,
+                                trailers, sf.trl_cnt, PyBUF_SIMPLE)))
                 return NULL;
+#ifdef __APPLE__
+            sbytes += i;
+#endif
         }
     }
 


More information about the Python-bugs-list mailing list