Klaus Demo bjoern / 0491fe6
More filewrapper fixes Jonas Haag 2 years ago
3 changed file(s) with 21 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
00 #include "filewrapper.h"
11 #include "py2py3.h"
2
3 int FileWrapper_GetFd(PyObject *self)
4 {
5 return ((FileWrapper*)self)->fd;
6 }
7
8 void FileWrapper_Done(PyObject *self)
9 {
10 PyFile_DecUseCount((PyFileObject*)((FileWrapper*)self)->file);
11 Py_DECREF(((FileWrapper*)self)->file);
12 }
213
314 static PyObject*
415 FileWrapper_New(PyTypeObject* cls, PyObject* args, PyObject* kwargs)
3748 return PyObject_GetIter(((FileWrapper*)self)->file);
3849 }
3950
40 static void
41 FileWrapper_dealloc(PyObject* self)
42 {
43 PyFile_DecUseCount((PyFileObject*)((FileWrapper*)self)->file);
44 Py_DECREF(((FileWrapper*)self)->file);
45 PyObject_FREE(self);
46 }
47
4851 PyTypeObject FileWrapper_Type = {
4952 PyVarObject_HEAD_INIT(NULL, 0)
5053 "FileWrapper", /* tp_name (__name__) */
51 sizeof(FileWrapper), /* tp_basicsize */
52 0, /* tp_itemsize */
53 (destructor)FileWrapper_dealloc, /* tp_dealloc */
54 sizeof(FileWrapper) /* tp_basicsize */
5455 };
5556
5657 void _init_filewrapper(void)
0 #include <sys/types.h> /* for off_t */
01 #include <Python.h> /* for Py_ssize_t */
12
23 Py_ssize_t portable_sendfile(int out_fd, int in_fd, off_t offset);
400400 {
401401 Py_ssize_t bytes_sent = portable_sendfile(
402402 request->client_fd,
403 ((FileWrapper*)request->iterable)->fd,
403 FileWrapper_GetFd(request->iterable),
404404 request->current_chunk_p
405405 );
406406 switch(bytes_sent) {
407407 case -1:
408 return handle_nonzero_errno(request);
408 if (handle_nonzero_errno(request)) {
409 return true;
410 } else {
411 FileWrapper_Done(request->iterable);
412 return false;
413 }
409414 case 0:
415 FileWrapper_Done(request->iterable);
410416 return false;
411417 default:
412418 request->current_chunk_p += bytes_sent;