295
295
/******************************************************************/
297
Allocate a memory block for a pep3118 format string, copy prefix (if
298
non-null) and suffix into it. Returns NULL on failure, with the error
299
indicator set. If called with a suffix of NULL the error indicator must
303
alloc_format_string(const char *prefix, const char *suffix)
308
if (suffix == NULL) {
309
assert(PyErr_Occurred());
312
len = strlen(suffix);
314
len += strlen(prefix);
315
result = PyMem_Malloc(len + 1);
319
strcpy(result, prefix);
322
strcat(result, suffix);
297
327
StructType_Type - a meta type/class. Creating a new class using this one as
298
328
__metaclass__ will call the contructor StructUnionType_new. It replaces the
299
329
tp_dict member with a new instance of StgDict, and initializes the C
908
StgDictObject *itemdict = PyType_stgdict(proto);
910
stgdict->format = alloc_format_string("&", itemdict->format);
911
if (stgdict->format == NULL) {
912
Py_DECREF((PyObject *)stgdict);
877
917
/* create the new instance (which is a class,
878
918
since we are a metatype!) */
879
919
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
1325
assert(itemdict->format);
1326
if (itemdict->format[0] == '(') {
1327
sprintf(buf, "(%ld,", length);
1328
stgdict->format = alloc_format_string(buf, itemdict->format+1);
1330
sprintf(buf, "(%ld)", length);
1331
stgdict->format = alloc_format_string(buf, itemdict->format);
1333
if (stgdict->format == NULL) {
1334
Py_DECREF((PyObject *)stgdict);
1337
stgdict->ndim = itemdict->ndim + 1;
1338
stgdict->shape = PyMem_Malloc(sizeof(Py_ssize_t *) * stgdict->ndim);
1339
if (stgdict->shape == NULL) {
1340
Py_DECREF((PyObject *)stgdict);
1343
stgdict->shape[0] = length;
1344
memmove(&stgdict->shape[1], itemdict->shape,
1345
sizeof(Py_ssize_t) * (stgdict->ndim - 1));
1284
1347
itemsize = itemdict->size;
1285
1348
if (length * itemsize < 0) {
1286
1349
PyErr_SetString(PyExc_OverflowError,
1780
1845
proto = PyObject_GetAttrString((PyObject *)result, "_type_"); /* new ref */
1782
|| !PyString_Check(proto)
1783
|| 1 != strlen(PyString_AS_STRING(proto))
1784
|| !strchr(SIMPLE_TYPE_CHARS, PyString_AS_STRING(proto)[0])) {
1847
PyErr_SetString(PyExc_AttributeError,
1848
"class must define a '_type_' attribute");
1854
if (PyString_Check(proto)) {
1855
proto_str = PyBytes_AS_STRING(proto);
1856
proto_len = PyBytes_GET_SIZE(proto);
1858
PyErr_SetString(PyExc_TypeError,
1859
"class must define a '_type_' string attribute");
1862
if (proto_len != 1) {
1863
PyErr_SetString(PyExc_ValueError,
1864
"class must define a '_type_' attribute "
1865
"which must be a string of length 1");
1868
if (!strchr(SIMPLE_TYPE_CHARS, *proto_str)) {
1785
1869
PyErr_Format(PyExc_AttributeError,
1786
1870
"class must define a '_type_' attribute which must be\n"
1787
1871
"a single character string containing one of '%s'.",
1788
1872
SIMPLE_TYPE_CHARS);
1793
1875
fmt = getentry(PyString_AS_STRING(proto));
1794
1876
if (fmt == NULL) {
1810
1892
stgdict->size = fmt->pffi_type->size;
1811
1893
stgdict->setfunc = fmt->setfunc;
1812
1894
stgdict->getfunc = fmt->getfunc;
1895
#ifdef WORDS_BIGENDIAN
1896
stgdict->format = alloc_format_string(">", proto_str);
1898
stgdict->format = alloc_format_string("<", proto_str);
1900
if (stgdict->format == NULL) {
1902
Py_DECREF((PyObject *)stgdict);
1814
1906
stgdict->paramfunc = SimpleType_paramfunc;
1895
1987
if (type == &SimpleType_Type && fmt->setfunc_swapped && fmt->getfunc_swapped) {
1896
1988
PyObject *swapped = CreateSwappedType(type, args, kwds,
1990
StgDictObject *sw_dict;
1898
1991
if (swapped == NULL) {
1899
1992
Py_DECREF(result);
1995
sw_dict = PyType_stgdict(swapped);
1902
1996
#ifdef WORDS_BIGENDIAN
1903
1997
PyObject_SetAttrString((PyObject *)result, "__ctype_le__", swapped);
1904
1998
PyObject_SetAttrString((PyObject *)result, "__ctype_be__", (PyObject *)result);
1905
1999
PyObject_SetAttrString(swapped, "__ctype_be__", (PyObject *)result);
1906
2000
PyObject_SetAttrString(swapped, "__ctype_le__", swapped);
2001
/* We are creating the type for the OTHER endian */
2002
sw_dict->format = alloc_format_string("<", stgdict->format+1);
1908
2004
PyObject_SetAttrString((PyObject *)result, "__ctype_be__", swapped);
1909
2005
PyObject_SetAttrString((PyObject *)result, "__ctype_le__", (PyObject *)result);
1910
2006
PyObject_SetAttrString(swapped, "__ctype_le__", (PyObject *)result);
1911
2007
PyObject_SetAttrString(swapped, "__ctype_be__", swapped);
2008
/* We are creating the type for the OTHER endian */
2009
sw_dict->format = alloc_format_string(">", stgdict->format+1);
1913
2011
Py_DECREF(swapped);
2012
if (PyErr_Occurred()) {
1916
2018
return (PyObject *)result;
2168
2270
stgdict->paramfunc = CFuncPtrType_paramfunc;
2271
/* We do NOT expose the function signature in the format string. It
2272
is impossible, generally, because the only requirement for the
2273
argtypes items is that they have a .from_param method - we do not
2274
know the types of the arguments (although, in practice, most
2275
argtypes would be a ctypes type).
2277
stgdict->format = alloc_format_string(NULL, "X{}");
2169
2278
stgdict->flags |= TYPEFLAG_ISPOINTER;
2171
2280
/* create the new instance (which is a class,
2498
static int CData_NewGetBuffer(PyObject *_self, Py_buffer *view, int flags)
2500
CDataObject *self = (CDataObject *)_self;
2501
StgDictObject *dict = PyObject_stgdict(_self);
2504
if (view == NULL) return 0;
2506
view->buf = self->b_ptr;
2507
view->len = self->b_size;
2509
/* use default format character if not set */
2510
view->format = dict->format ? dict->format : "B";
2511
view->ndim = dict->ndim;
2512
view->shape = dict->shape;
2513
view->itemsize = self->b_size;
2514
for (i = 0; i < view->ndim; ++i) {
2515
view->itemsize /= dict->shape[i];
2517
view->strides = NULL;
2518
view->suboffsets = NULL;
2519
view->internal = NULL;
2523
static Py_ssize_t CData_GetSegcount(PyObject *_self, Py_ssize_t *lenp)
2389
2530
static Py_ssize_t CData_GetBuffer(PyObject *_self, Py_ssize_t seg, void **pptr)
2391
2532
CDataObject *self = (CDataObject *)_self;
2397
2538
return self->b_size;
2400
static Py_ssize_t CData_GetSegcount(PyObject *_self, Py_ssize_t *lenp)
2407
2541
static PyBufferProcs CData_as_buffer = {
2542
(readbufferproc)CData_GetBuffer,
2543
(writebufferproc)CData_GetBuffer,
2544
(segcountproc)CData_GetSegcount,
2545
(charbufferproc)NULL,
2546
(getbufferproc)CData_NewGetBuffer,
2547
(releasebufferproc)NULL,
2497
2633
0, /* tp_getattro */
2498
2634
0, /* tp_setattro */
2499
2635
&CData_as_buffer, /* tp_as_buffer */
2500
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2636
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE, /* tp_flags */
2501
2637
"XXX to be provided", /* tp_doc */
2502
2638
(traverseproc)CData_traverse, /* tp_traverse */
2503
2639
(inquiry)CData_clear, /* tp_clear */
3924
Pointer_nonzero(CDataObject *self)
3926
return *(void **)self->b_ptr != NULL;
3929
static PyNumberMethods Pointer_as_number = {
3931
0, /* nb_subtract */
3932
0, /* nb_multiply */
3934
0, /* nb_remainder */
3937
0, /* nb_negative */
3938
0, /* nb_positive */
3939
0, /* nb_absolute */
3940
(inquiry)Pointer_nonzero, /* nb_nonzero */
3787
3943
PyTypeObject CFuncPtr_Type = {
3788
3944
PyVarObject_HEAD_INIT(NULL, 0)
3789
3945
"_ctypes.CFuncPtr",
3804
3960
0, /* tp_getattro */
3805
3961
0, /* tp_setattro */
3806
3962
&CData_as_buffer, /* tp_as_buffer */
3807
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3963
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE, /* tp_flags */
3808
3964
"Function Pointer", /* tp_doc */
3809
3965
(traverseproc)CFuncPtr_traverse, /* tp_traverse */
3810
3966
(inquiry)CFuncPtr_clear, /* tp_clear */
3947
4103
0, /* tp_getattro */
3948
4104
0, /* tp_setattro */
3949
4105
&CData_as_buffer, /* tp_as_buffer */
3950
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4106
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE, /* tp_flags */
3951
4107
"Structure base class", /* tp_doc */
3952
4108
(traverseproc)CData_traverse, /* tp_traverse */
3953
4109
(inquiry)CData_clear, /* tp_clear */
3989
4145
0, /* tp_getattro */
3990
4146
0, /* tp_setattro */
3991
4147
&CData_as_buffer, /* tp_as_buffer */
3992
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4148
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE, /* tp_flags */
3993
4149
"Union base class", /* tp_doc */
3994
4150
(traverseproc)CData_traverse, /* tp_traverse */
3995
4151
(inquiry)CData_clear, /* tp_clear */
4386
4542
0, /* tp_getattro */
4387
4543
0, /* tp_setattro */
4388
4544
&CData_as_buffer, /* tp_as_buffer */
4389
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4545
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE, /* tp_flags */
4390
4546
"XXX to be provided", /* tp_doc */
4391
4547
(traverseproc)CData_traverse, /* tp_traverse */
4392
4548
(inquiry)CData_clear, /* tp_clear */
4623
4779
0, /* tp_getattro */
4624
4780
0, /* tp_setattro */
4625
4781
&CData_as_buffer, /* tp_as_buffer */
4626
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4782
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE, /* tp_flags */
4627
4783
"XXX to be provided", /* tp_doc */
4628
4784
(traverseproc)CData_traverse, /* tp_traverse */
4629
4785
(inquiry)CData_clear, /* tp_clear */
5003
5159
Pointer_subscript,
5007
Pointer_nonzero(CDataObject *self)
5009
return *(void **)self->b_ptr != NULL;
5012
static PyNumberMethods Pointer_as_number = {
5014
0, /* nb_subtract */
5015
0, /* nb_multiply */
5017
0, /* nb_remainder */
5020
0, /* nb_negative */
5021
0, /* nb_positive */
5022
0, /* nb_absolute */
5023
(inquiry)Pointer_nonzero, /* nb_nonzero */
5026
5162
PyTypeObject Pointer_Type = {
5027
5163
PyVarObject_HEAD_INIT(NULL, 0)
5028
5164
"_ctypes._Pointer",
5043
5179
0, /* tp_getattro */
5044
5180
0, /* tp_setattro */
5045
5181
&CData_as_buffer, /* tp_as_buffer */
5046
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
5182
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE, /* tp_flags */
5047
5183
"XXX to be provided", /* tp_doc */
5048
5184
(traverseproc)CData_traverse, /* tp_traverse */
5049
5185
(inquiry)CData_clear, /* tp_clear */
5394
5530
PyModule_AddObject(m, "FUNCFLAG_STDCALL", PyInt_FromLong(FUNCFLAG_STDCALL));
5396
5532
PyModule_AddObject(m, "FUNCFLAG_CDECL", PyInt_FromLong(FUNCFLAG_CDECL));
5533
PyModule_AddObject(m, "FUNCFLAG_USE_ERRNO", PyInt_FromLong(FUNCFLAG_USE_ERRNO));
5534
PyModule_AddObject(m, "FUNCFLAG_USE_LASTERROR", PyInt_FromLong(FUNCFLAG_USE_LASTERROR));
5397
5535
PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyInt_FromLong(FUNCFLAG_PYTHONAPI));
5398
5536
PyModule_AddStringConstant(m, "__version__", "1.1.0");