19
19
def __init__(self):
20
20
# In the C class, "_x_attr" is not accessible from Python code
21
21
self._x_attr = {}
22
+ self._x_buffer = bytesarray(10)
23
+ self._x_exports = 0
22
24
23
25
def __getattr__(self, name):
24
26
return self._x_attr[name]
29
31
def __delattr__(self, name):
30
32
del self._x_attr[name]
31
33
34
+ @property
35
+ def x_exports(self):
36
+ return self._x_exports
37
+
32
38
def demo(o, /):
33
39
if isinstance(o, str):
34
40
return o
57
63
#define Py_LIMITED_API 0x030b0000
58
64
59
65
#include "Python.h"
66
+ #include <string.h>
67
+
68
+ #define BUFSIZE 10
60
69
61
70
// Module state
62
71
typedef struct {
@@ -70,7 +79,9 @@ typedef struct {
70
79
// Instance state
71
80
typedef struct {
72
81
PyObject_HEAD
73
- PyObject * x_attr ; /* Attributes dictionary */
82
+ PyObject * x_attr ; /* Attributes dictionary */
83
+ char x_buffer [BUFSIZE ]; /* buffer for Py_buffer */
84
+ Py_ssize_t x_exports ; /* how many buffer are exported */
74
85
} XxoObject ;
75
86
76
87
// XXX: no good way to do this yet
@@ -89,6 +100,8 @@ newXxoObject(PyObject *module)
89
100
return NULL ;
90
101
}
91
102
self -> x_attr = NULL ;
103
+ memset (self -> x_buffer , 0 , BUFSIZE );
104
+ self -> x_exports = 0 ;
92
105
return self ;
93
106
}
94
107
@@ -212,11 +225,43 @@ static PyMethodDef Xxo_methods[] = {
212
225
{NULL , NULL } /* sentinel */
213
226
};
214
227
228
+ /* Xxo buffer interface */
229
+
230
+ static int
231
+ Xxo_getbuffer (XxoObject * self , Py_buffer * view , int flags )
232
+ {
233
+ int res = PyBuffer_FillInfo (view , (PyObject * )self ,
234
+ (void * )self -> x_buffer , BUFSIZE ,
235
+ 0 , flags );
236
+ if (res == 0 ) {
237
+ self -> x_exports ++ ;
238
+ }
239
+ return res ;
240
+ }
241
+
242
+ static void
243
+ Xxo_releasebuffer (XxoObject * self , Py_buffer * view )
244
+ {
245
+ self -> x_exports -- ;
246
+ }
247
+
248
+ static PyObject *
249
+ Xxo_get_x_exports (XxoObject * self , void * c )
250
+ {
251
+ return PyLong_FromSsize_t (self -> x_exports );
252
+ }
253
+
215
254
/* Xxo type definition */
216
255
217
256
PyDoc_STRVAR (Xxo_doc ,
218
257
"A class that explicitly stores attributes in an internal dict" );
219
258
259
+ static PyGetSetDef Xxo_getsetlist [] = {
260
+ {"x_exports" , (getter ) Xxo_get_x_exports , NULL , NULL },
261
+ {NULL },
262
+ };
263
+
264
+
220
265
static PyType_Slot Xxo_Type_slots [] = {
221
266
{Py_tp_doc , (char * )Xxo_doc },
222
267
{Py_tp_traverse , Xxo_traverse },
@@ -226,6 +271,9 @@ static PyType_Slot Xxo_Type_slots[] = {
226
271
{Py_tp_getattro , Xxo_getattro },
227
272
{Py_tp_setattro , Xxo_setattro },
228
273
{Py_tp_methods , Xxo_methods },
274
+ {Py_bf_getbuffer , Xxo_getbuffer },
275
+ {Py_bf_releasebuffer , Xxo_releasebuffer },
276
+ {Py_tp_getset , Xxo_getsetlist },
229
277
{0 , 0 }, /* sentinel */
230
278
};
231
279
0 commit comments