-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
/
Copy pathbuffer.c
111 lines (95 loc) · 2.48 KB
/
buffer.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* Test PEP 688 - Buffers */
#include "parts.h"
#include <stddef.h> // offsetof
typedef struct {
PyObject_HEAD
PyObject *obj;
Py_ssize_t references;
} testBufObject;
#define testBufObject_CAST(op) ((testBufObject *)(op))
static PyObject *
testbuf_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *obj = PyBytes_FromString("test");
if (obj == NULL) {
return NULL;
}
testBufObject *self = (testBufObject *)type->tp_alloc(type, 0);
if (self == NULL) {
Py_DECREF(obj);
return NULL;
}
self->obj = obj;
self->references = 0;
return (PyObject *)self;
}
static int
testbuf_traverse(PyObject *op, visitproc visit, void *arg)
{
testBufObject *self = testBufObject_CAST(op);
Py_VISIT(self->obj);
return 0;
}
static int
testbuf_clear(PyObject *op)
{
testBufObject *self = testBufObject_CAST(op);
Py_CLEAR(self->obj);
return 0;
}
static void
testbuf_dealloc(PyObject *op)
{
testBufObject *self = testBufObject_CAST(op);
PyObject_GC_UnTrack(self);
Py_XDECREF(self->obj);
Py_TYPE(self)->tp_free(self);
}
static int
testbuf_getbuf(PyObject *op, Py_buffer *view, int flags)
{
testBufObject *self = testBufObject_CAST(op);
int buf = PyObject_GetBuffer(self->obj, view, flags);
if (buf == 0) {
Py_SETREF(view->obj, Py_NewRef(self));
self->references++;
}
return buf;
}
static void
testbuf_releasebuf(PyObject *op, Py_buffer *Py_UNUSED(view))
{
testBufObject *self = testBufObject_CAST(op);
self->references--;
assert(self->references >= 0);
}
static PyBufferProcs testbuf_as_buffer = {
.bf_getbuffer = testbuf_getbuf,
.bf_releasebuffer = testbuf_releasebuf,
};
static struct PyMemberDef testbuf_members[] = {
{"references", Py_T_PYSSIZET, offsetof(testBufObject, references), Py_READONLY},
{NULL},
};
static PyTypeObject testBufType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "testBufType",
.tp_basicsize = sizeof(testBufObject),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
.tp_new = testbuf_new,
.tp_dealloc = testbuf_dealloc,
.tp_traverse = testbuf_traverse,
.tp_clear = testbuf_clear,
.tp_as_buffer = &testbuf_as_buffer,
.tp_members = testbuf_members
};
int
_PyTestCapi_Init_Buffer(PyObject *m) {
if (PyType_Ready(&testBufType) < 0) {
return -1;
}
if (PyModule_AddObjectRef(m, "testBuf", (PyObject *)&testBufType)) {
return -1;
}
return 0;
}