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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/*
* SIP-TQt library code.
*
* Copyright (c) 2010 Riverbank Computing Limited <info@riverbankcomputing.com>
*
* This file is part of SIP-TQt.
*
* This copy of SIP-TQt is licensed for use under the terms of the SIP License
* Agreement. See the file LICENSE for more details.
*
* This copy of SIP-TQt may also used under the terms of the GNU General Public
* License v2 or v3 as published by the Free Software Foundation which can be
* found in the files LICENSE-GPL2 and LICENSE-GPL3 included in this package.
*
* SIP-TQt is supplied WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <Python.h>
#include <stddef.h>
#include "sip-tqt.h"
#include "sipint.h"
/* The object data structure. */
typedef struct {
PyObject_HEAD
void *voidptr;
SIP_SSIZE_T size;
int rw;
} sipVoidPtrObject;
/* The structure used to hold the results of a voidptr conversion. */
struct vp_values {
void *voidptr;
SIP_SSIZE_T size;
int rw;
};
static PyObject *make_voidptr(void *voidptr, SIP_SSIZE_T size, int rw);
static int vp_convertor(PyObject *arg, struct vp_values *vp);
/*
* Implement __new__ for the type.
*/
static PyObject *sipVoidPtr_new(PyTypeObject *subtype, PyObject *args,
PyObject *kw)
{
static char *kwlist[] = {"address", "size", "writeable", NULL};
struct vp_values vp_conversion;
SIP_SSIZE_T size = -1;
int rw = -1;
PyObject *obj;
if (!PyArg_ParseTupleAndKeywords(args, kw,
"O&|ni:voidptr",
kwlist, vp_convertor, &vp_conversion, &size, &rw))
return NULL;
/* Use the explicit size if one was given. */
if (size >= 0)
vp_conversion.size = size;
/* Use the explicit writeable flag if one was given. */
if (rw >= 0)
vp_conversion.rw = rw;
/* Create the instance. */
if ((obj = subtype->tp_alloc(subtype, 0)) == NULL)
return NULL;
/* Save the values. */
((sipVoidPtrObject *)obj)->voidptr = vp_conversion.voidptr;
((sipVoidPtrObject *)obj)->size = vp_conversion.size;
((sipVoidPtrObject *)obj)->rw = vp_conversion.rw;
return obj;
}
/*
* The read buffer implementation for Python v3.
*/
static int sipVoidPtr_getbuffer(PyObject *self, Py_buffer *buf, int flags)
{
sipVoidPtrObject *v = (sipVoidPtrObject *)self;
return PyBuffer_FillInfo(buf, self, v->voidptr, v->size, !v->rw, flags);
}
/*
* Implement int() for the type.
*/
static PyObject *sipVoidPtr_int(sipVoidPtrObject *v)
{
return PyLong_FromVoidPtr(v->voidptr);
}
/*
* Implement ascapsule() for the type.
*/
static PyObject *sipVoidPtr_ascapsule(sipVoidPtrObject *v, PyObject *arg)
{
return PyCapsule_New(v->voidptr, NULL, NULL);
}
/*
* Implement asstring() for the type.
*/
static PyObject *sipVoidPtr_asstring(sipVoidPtrObject *v, PyObject *args,
PyObject *kw)
{
static char *kwlist[] = {"size", NULL};
SIP_SSIZE_T size = -1;
if (!PyArg_ParseTupleAndKeywords(args, kw,
"|n:asstring",
kwlist, &size))
return NULL;
/* Use the current size if one wasn't explicitly given. */
if (size < 0)
size = v->size;
if (size < 0)
{
PyErr_SetString(PyExc_ValueError,
"a size must be given or the sip_tqt.voidptr must have a size");
return NULL;
}
return PyBytes_FromStringAndSize(v->voidptr, size);
}
/*
* Implement getsize() for the type.
*/
static PyObject *sipVoidPtr_getsize(sipVoidPtrObject *v, PyObject *arg)
{
return PyLong_FromSsize_t(v->size);
}
/*
* Implement setsize() for the type.
*/
static PyObject *sipVoidPtr_setsize(sipVoidPtrObject *v, PyObject *arg)
{
SIP_SSIZE_T size;
size = PyLong_AsSsize_t(arg);
if (PyErr_Occurred())
return NULL;
v->size = size;
Py_INCREF(Py_None);
return Py_None;
}
/*
* Implement getwriteable() for the type.
*/
static PyObject *sipVoidPtr_getwriteable(sipVoidPtrObject *v, PyObject *arg)
{
return PyBool_FromLong(v->rw);
}
/*
* Implement setwriteable() for the type.
*/
static PyObject *sipVoidPtr_setwriteable(sipVoidPtrObject *v, PyObject *arg)
{
int rw;
rw = (int)PyLong_AsLong(arg);
if (PyErr_Occurred())
return NULL;
v->rw = rw;
Py_INCREF(Py_None);
return Py_None;
}
/* The methods data structure. */
static PyMethodDef sipVoidPtr_Methods[] = {
{"ascapsule", (PyCFunction)sipVoidPtr_ascapsule, METH_NOARGS, NULL},
{"asstring", (PyCFunction)sipVoidPtr_asstring, METH_VARARGS|METH_KEYWORDS, NULL},
{"getsize", (PyCFunction)sipVoidPtr_getsize, METH_NOARGS, NULL},
{"setsize", (PyCFunction)sipVoidPtr_setsize, METH_O, NULL},
{"getwriteable", (PyCFunction)sipVoidPtr_getwriteable, METH_NOARGS, NULL},
{"setwriteable", (PyCFunction)sipVoidPtr_setwriteable, METH_O, NULL},
{NULL}
};
/* The number methods data structure. */
static PyNumberMethods sipVoidPtr_NumberMethods = {
0, /* nb_add */
0, /* nb_subtract */
0, /* nb_multiply */
0, /* nb_remainder */
0, /* nb_divmod */
0, /* nb_power */
0, /* nb_negative */
0, /* nb_positive */
0, /* nb_absolute */
0, /* nb_bool (Python v3), nb_nonzero (Python v2) */
0, /* nb_invert */
0, /* nb_lshift */
0, /* nb_rshift */
0, /* nb_and */
0, /* nb_xor */
0, /* nb_or */
(unaryfunc)sipVoidPtr_int, /* nb_int */
0, /* nb_reserved */
0, /* nb_float */
0, /* nb_inplace_add */
0, /* nb_inplace_subtract */
0, /* nb_inplace_multiply */
0, /* nb_inplace_remainder */
0, /* nb_inplace_power */
0, /* nb_inplace_lshift */
0, /* nb_inplace_rshift */
0, /* nb_inplace_and */
0, /* nb_inplace_xor */
0, /* nb_inplace_or */
0, /* nb_floor_divide */
0, /* nb_true_divide */
0, /* nb_inplace_floor_divide */
0, /* nb_inplace_true_divide */
0 /* nb_index */
};
/* The buffer methods data structure. */
static PyBufferProcs sipVoidPtr_BufferProcs = {
sipVoidPtr_getbuffer,
NULL,
};
/* The type data structure. */
PyTypeObject sipVoidPtr_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"sip_tqt.voidptr", /* tp_name */
sizeof (sipVoidPtrObject), /* tp_basicsize */
0, /* tp_itemsize */
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved (Python v3), tp_compare (Python v2) */
0, /* tp_repr */
&sipVoidPtr_NumberMethods, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
&sipVoidPtr_BufferProcs, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
sipVoidPtr_Methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
sipVoidPtr_new, /* tp_new */
};
/*
* A convenience function to convert a C/C++ void pointer from a Python object.
*/
void *sip_api_convert_to_void_ptr(PyObject *obj)
{
if (obj == NULL)
{
PyErr_SetString(PyExc_TypeError, "sip_tqt.voidptr is NULL");
return NULL;
}
if (obj == Py_None)
return NULL;
if (PyObject_TypeCheck(obj, &sipVoidPtr_Type))
return ((sipVoidPtrObject *)obj)->voidptr;
if (PyCapsule_CheckExact(obj))
return PyCapsule_GetPointer(obj, NULL);
return PyLong_AsVoidPtr(obj);
}
/*
* Convert a C/C++ void pointer to a sip_tqt.voidptr object.
*/
PyObject *sip_api_convert_from_void_ptr(void *val)
{
return make_voidptr(val, -1, TRUE);
}
/*
* Convert a C/C++ void pointer to a sip_tqt.voidptr object.
*/
PyObject *sip_api_convert_from_const_void_ptr(const void *val)
{
return make_voidptr((void *)val, -1, FALSE);
}
/*
* Convert a sized C/C++ void pointer to a sip_tqt.voidptr object.
*/
PyObject *sip_api_convert_from_void_ptr_and_size(void *val, SIP_SSIZE_T size)
{
return make_voidptr(val, size, TRUE);
}
/*
* Convert a sized C/C++ const void pointer to a sip_tqt.voidptr object.
*/
PyObject *sip_api_convert_from_const_void_ptr_and_size(const void *val,
SIP_SSIZE_T size)
{
return make_voidptr((void *)val, size, FALSE);
}
/*
* Do the work of converting a void pointer.
*/
static PyObject *make_voidptr(void *voidptr, SIP_SSIZE_T size, int rw)
{
sipVoidPtrObject *self;
if (voidptr == NULL)
{
Py_INCREF(Py_None);
return Py_None;
}
if ((self = PyObject_NEW(sipVoidPtrObject, &sipVoidPtr_Type)) == NULL)
return NULL;
self->voidptr = voidptr;
self->size = size;
self->rw = rw;
return (PyObject *)self;
}
/*
* Convert a Python object to the values needed to create a voidptr.
*/
static int vp_convertor(PyObject *arg, struct vp_values *vp)
{
void *ptr;
SIP_SSIZE_T size = -1;
int rw = TRUE;
if (arg == Py_None)
ptr = NULL;
else if (PyCapsule_CheckExact(arg))
ptr = PyCapsule_GetPointer(arg, NULL);
else if (PyObject_TypeCheck(arg, &sipVoidPtr_Type))
{
ptr = ((sipVoidPtrObject *)arg)->voidptr;
size = ((sipVoidPtrObject *)arg)->size;
rw = ((sipVoidPtrObject *)arg)->rw;
}
else
{
ptr = PyLong_AsVoidPtr(arg);
if (PyErr_Occurred())
{
PyErr_SetString(PyExc_TypeError, "a single integer, CObject, None or another voidptr is required");
return 0;
}
}
vp->voidptr = ptr;
vp->size = size;
vp->rw = rw;
return 1;
}
|