forked from jonashaag/bjoern
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilewrapper.c
60 lines (49 loc) · 1.57 KB
/
filewrapper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "filewrapper.h"
static PyObject*
FileWrapper_New(PyTypeObject* cls, PyObject* args, PyObject* kwargs)
{
PyObject* file;
unsigned int ignored_blocksize;
if(!PyArg_ParseTuple(args, "O|I:FileWrapper", &file, &ignored_blocksize))
return NULL;
//if(!PyFile_Check(file)) {
// TYPE_ERROR("FileWrapper argument", "file", file);
// return NULL;
//}
Py_INCREF(file);
PyFile_IncUseCount((PyFileObject*)file);
FileWrapper* wrapper = PyObject_NEW(FileWrapper, cls);
wrapper->file = file;
return (PyObject*)wrapper;
}
static PyObject*
FileWrapper_GetAttrO(PyObject* self, PyObject* name)
{
return PyObject_GetAttr(((FileWrapper*)self)->file, name);
}
static PyObject*
FileWrapper_Iter(PyObject* self)
{
return PyObject_GetIter(((FileWrapper*)self)->file);
}
static void
FileWrapper_dealloc(PyObject* self)
{
PyFile_DecUseCount((PyFileObject*)((FileWrapper*)self)->file);
Py_DECREF(((FileWrapper*)self)->file);
PyObject_FREE(self);
}
PyTypeObject FileWrapper_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"FileWrapper", /* tp_name (__name__) */
sizeof(FileWrapper), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)FileWrapper_dealloc, /* tp_dealloc */
};
void _init_filewrapper(void)
{
FileWrapper_Type.tp_new = FileWrapper_New;
FileWrapper_Type.tp_iter = FileWrapper_Iter;
FileWrapper_Type.tp_getattro = FileWrapper_GetAttrO;
FileWrapper_Type.tp_flags |= Py_TPFLAGS_DEFAULT;
}