Klaus Demo ~jonashaag/bjoern / 824b209
Implement FileWrapper.close(). Fixes #171 Jonas Haag 10 months ago
4 changed file(s) with 20 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
55 int FileWrapper_GetFd(PyObject *self)
66 {
77 return FW_self->fd;
8 }
9
10 void FileWrapper_Done(PyObject *self)
11 {
12 if (FW_self->fd != -1) {
13 PyFile_DecUseCount((PyFileObject*)FW_self->file);
14 }
158 }
169
1710 static PyObject*
6659 PyObject_FREE(self);
6760 }
6861
62 PyObject* FileWrapper_close(PyObject* self)
63 {
64 if (PyObject_HasAttr(FW_self->file, _close)) {
65 return PyObject_CallMethodObjArgs(FW_self->file, _close, NULL);
66 } else {
67 Py_RETURN_NONE;
68 }
69 if (FW_self->fd != -1) {
70 PyFile_DecUseCount((PyFileObject*)FW_self->file);
71 }
72 }
73
74 static PyMethodDef FileWrapper_methods[] = {
75 {"close", (PyCFunction) FileWrapper_close, METH_NOARGS, NULL},
76 {NULL} /* Sentinel */
77 };
78
6979 PyTypeObject FileWrapper_Type = {
7080 PyVarObject_HEAD_INIT(NULL, 0)
7181 "FileWrapper", /* tp_name (__name__) */
8090 FileWrapper_Type.tp_iter = FileWrapper_Iter;
8191 FileWrapper_Type.tp_iternext = FileWrapper_IterNext;
8292 FileWrapper_Type.tp_flags |= Py_TPFLAGS_DEFAULT;
93 FileWrapper_Type.tp_methods = FileWrapper_methods;
8394 }
1212
1313 void _init_filewrapper(void);
1414 int FileWrapper_GetFd(PyObject *self);
15 void FileWrapper_Done(PyObject *self);
4949 void Request_clean(Request* request)
5050 {
5151 if(request->iterable) {
52 /* Call 'iterable.close()' if available */
52 /* Call 'iterable.close()' if available. This will also call into FileWrapper_close()
53 * if 'iterable' is a FileWrapper object. */
5354 PyObject* close_method = PyObject_GetAttr(request->iterable, _close);
5455 if(close_method == NULL) {
5556 if(PyErr_ExceptionMatches(PyExc_AttributeError))
499499 if (handle_nonzero_errno(request)) {
500500 return true;
501501 } else {
502 FileWrapper_Done(request->iterable);
503502 return false;
504503 }
505504 case 0:
506 FileWrapper_Done(request->iterable);
507505 return false;
508506 default:
509507 request->current_chunk_p += bytes_sent;