summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapi/buffer.c
blob: aff9a477eff57ec224b0523a1f54e66ed8e69199 (plain)
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
/* Test PEP 688 - Buffers */

#include "parts.h"

#include "structmember.h"           // PyMemberDef
#include <stddef.h>                 // offsetof

typedef struct {
    PyObject_HEAD
    PyObject *obj;
    Py_ssize_t references;
} testBufObject;

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(testBufObject *self, visitproc visit, void *arg)
{
    Py_VISIT(self->obj);
    return 0;
}

static int
testbuf_clear(testBufObject *self)
{
    Py_CLEAR(self->obj);
    return 0;
}

static void
testbuf_dealloc(testBufObject *self)
{
    PyObject_GC_UnTrack(self);
    Py_XDECREF(self->obj);
    Py_TYPE(self)->tp_free((PyObject *) self);
}

static int
testbuf_getbuf(testBufObject *self, Py_buffer *view, int flags)
{
    int buf = PyObject_GetBuffer(self->obj, view, flags);
    Py_SETREF(view->obj, Py_NewRef(self));
    self->references++;
    return buf;
}

static void
testbuf_releasebuf(testBufObject *self, Py_buffer *view)
{
    self->references--;
    assert(self->references >= 0);
}

static PyBufferProcs testbuf_as_buffer = {
    .bf_getbuffer = (getbufferproc) testbuf_getbuf,
    .bf_releasebuffer = (releasebufferproc) testbuf_releasebuf,
};

static struct PyMemberDef testbuf_members[] = {
    {"references", T_PYSSIZET, offsetof(testBufObject, references), 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 = (destructor) testbuf_dealloc,
    .tp_traverse = (traverseproc) testbuf_traverse,
    .tp_clear = (inquiry) 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;
}