summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2011-04-19 08:00:34 +0000
committerGiampaolo Rodola <g.rodola@gmail.com>2011-04-19 08:00:34 +0000
commite27b39205f94702f73f1280b6773a9070e27e0da (patch)
tree24e51c6c379e2dd3a0699240f021686d30f9cdac
parentf52b41433eaf24b8c54c95dd101d72643ba75ed7 (diff)
downloadpysendfile-e27b39205f94702f73f1280b6773a9070e27e0da.tar.gz
Add support for offset = None on Linux, meaning the offset is automatically updated by sendfile() call
-rw-r--r--sendfilemodule.c24
-rw-r--r--test/test_sendfile.py13
2 files changed, 27 insertions, 10 deletions
diff --git a/sendfilemodule.c b/sendfilemodule.c
index e3c9af5..d34a06b 100644
--- a/sendfilemodule.c
+++ b/sendfilemodule.c
@@ -266,8 +266,6 @@ method_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
&tail_len, &flags)) {
return NULL;
}
- if (!_parse_off_t(offobj, &offset))
- return NULL;
if (head_len != 0 || tail_len != 0) {
int cork = 1;
@@ -303,9 +301,19 @@ method_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
}
// send file
- Py_BEGIN_ALLOW_THREADS;
- sent_f = sendfile(out_fd, in_fd, &offset, nbytes);
- Py_END_ALLOW_THREADS;
+ if (offobj == Py_None) {
+ Py_BEGIN_ALLOW_THREADS;
+ sent_f = sendfile(out_fd, in_fd, NULL, nbytes);
+ Py_END_ALLOW_THREADS;
+ }
+ else {
+ if (!_parse_off_t(offobj, &offset))
+ return NULL;
+ Py_BEGIN_ALLOW_THREADS;
+ sent_f = sendfile(out_fd, in_fd, &offset, nbytes);
+ Py_END_ALLOW_THREADS;
+ }
+
if (sent_f == -1)
return PyErr_SetFromErrno(PyExc_OSError);
@@ -327,11 +335,7 @@ method_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
goto done;
done:
-#if defined(HAVE_LARGEFILE_SUPPORT)
- return Py_BuildValue("L", sent_h + sent_f + sent_t);
-#else
- return Py_BuildValue("l", sent_h + sent_f + sent_t);
-#endif
+ return Py_BuildValue("I", sent_h + sent_f + sent_t);
}
/* --- end Linux --- */
diff --git a/test/test_sendfile.py b/test/test_sendfile.py
index 30fce09..b687398 100644
--- a/test/test_sendfile.py
+++ b/test/test_sendfile.py
@@ -356,6 +356,19 @@ class TestSendfile(unittest.TestCase):
data_sent = self.server.handler_instance.get_data()
self.assertEqual(data_sent, data)
+ if "linux" in sys.platform:
+ def test_offset_none(self):
+ # on Linux offset == None is supposed to update file offset
+ while 1:
+ sent = sendfile_wrapper(self.sockno, self.fileno, None, 4096)
+ if sent == 0:
+ break
+ self.client.close()
+ self.server.wait()
+ data = self.server.handler_instance.get_data()
+ self.assertEqual(hash(data), hash(DATA))
+
+
class RepeatedTimer: