| 1 |
#include "Python.h" |
| 2 |
#include <ffi.h> |
| 3 |
#ifdef MS_WIN32 |
| 4 |
#include <windows.h> |
| 5 |
#include <malloc.h> |
| 6 |
#endif |
| 7 |
#include "ctypes.h" |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
static int |
| 20 |
PyCStgDict_init(StgDictObject *self, PyObject *args, PyObject *kwds) |
| 21 |
{ |
| 22 |
if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0) |
| 23 |
return -1; |
| 24 |
self->format = NULL; |
| 25 |
self->ndim = 0; |
| 26 |
self->shape = NULL; |
| 27 |
return 0; |
| 28 |
} |
| 29 |
|
| 30 |
static int |
| 31 |
PyCStgDict_clear(StgDictObject *self) |
| 32 |
{ |
| 33 |
Py_CLEAR(self->proto); |
| 34 |
Py_CLEAR(self->argtypes); |
| 35 |
Py_CLEAR(self->converters); |
| 36 |
Py_CLEAR(self->restype); |
| 37 |
Py_CLEAR(self->checker); |
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
static void |
| 42 |
PyCStgDict_dealloc(StgDictObject *self) |
| 43 |
{ |
| 44 |
PyCStgDict_clear(self); |
| 45 |
PyMem_Free(self->format); |
| 46 |
PyMem_Free(self->shape); |
| 47 |
PyMem_Free(self->ffi_type_pointer.elements); |
| 48 |
PyDict_Type.tp_dealloc((PyObject *)self); |
| 49 |
} |
| 50 |
|
| 51 |
int |
| 52 |
PyCStgDict_clone(StgDictObject *dst, StgDictObject *src) |
| 53 |
{ |
| 54 |
char *d, *s; |
| 55 |
Py_ssize_t size; |
| 56 |
|
| 57 |
PyCStgDict_clear(dst); |
| 58 |
PyMem_Free(dst->ffi_type_pointer.elements); |
| 59 |
PyMem_Free(dst->format); |
| 60 |
dst->format = NULL; |
| 61 |
PyMem_Free(dst->shape); |
| 62 |
dst->shape = NULL; |
| 63 |
dst->ffi_type_pointer.elements = NULL; |
| 64 |
|
| 65 |
d = (char *)dst; |
| 66 |
s = (char *)src; |
| 67 |
memcpy(d + sizeof(PyDictObject), |
| 68 |
s + sizeof(PyDictObject), |
| 69 |
sizeof(StgDictObject) - sizeof(PyDictObject)); |
| 70 |
|
| 71 |
Py_XINCREF(dst->proto); |
| 72 |
Py_XINCREF(dst->argtypes); |
| 73 |
Py_XINCREF(dst->converters); |
| 74 |
Py_XINCREF(dst->restype); |
| 75 |
Py_XINCREF(dst->checker); |
| 76 |
|
| 77 |
if (src->format) { |
| 78 |
dst->format = PyMem_Malloc(strlen(src->format) + 1); |
| 79 |
if (dst->format == NULL) { |
| 80 |
PyErr_NoMemory(); |
| 81 |
return -1; |
| 82 |
} |
| 83 |
strcpy(dst->format, src->format); |
| 84 |
} |
| 85 |
if (src->shape) { |
| 86 |
dst->shape = PyMem_Malloc(sizeof(Py_ssize_t) * src->ndim); |
| 87 |
if (dst->shape == NULL) { |
| 88 |
PyErr_NoMemory(); |
| 89 |
return -1; |
| 90 |
} |
| 91 |
memcpy(dst->shape, src->shape, |
| 92 |
sizeof(Py_ssize_t) * src->ndim); |
| 93 |
} |
| 94 |
|
| 95 |
if (src->ffi_type_pointer.elements == NULL) |
| 96 |
return 0; |
| 97 |
size = sizeof(ffi_type *) * (src->length + 1); |
| 98 |
dst->ffi_type_pointer.elements = PyMem_Malloc(size); |
| 99 |
if (dst->ffi_type_pointer.elements == NULL) { |
| 100 |
PyErr_NoMemory(); |
| 101 |
return -1; |
| 102 |
} |
| 103 |
memcpy(dst->ffi_type_pointer.elements, |
| 104 |
src->ffi_type_pointer.elements, |
| 105 |
size); |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
PyTypeObject PyCStgDict_Type = { |
| 110 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 111 |
"StgDict", |
| 112 |
sizeof(StgDictObject), |
| 113 |
0, |
| 114 |
(destructor)PyCStgDict_dealloc, |
| 115 |
0, |
| 116 |
0, |
| 117 |
0, |
| 118 |
0, |
| 119 |
0, |
| 120 |
0, |
| 121 |
0, |
| 122 |
0, |
| 123 |
0, |
| 124 |
0, |
| 125 |
0, |
| 126 |
0, |
| 127 |
0, |
| 128 |
0, |
| 129 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 130 |
0, |
| 131 |
0, |
| 132 |
0, |
| 133 |
0, |
| 134 |
0, |
| 135 |
0, |
| 136 |
0, |
| 137 |
0, |
| 138 |
0, |
| 139 |
0, |
| 140 |
0, |
| 141 |
0, |
| 142 |
0, |
| 143 |
0, |
| 144 |
0, |
| 145 |
(initproc)PyCStgDict_init, |
| 146 |
0, |
| 147 |
0, |
| 148 |
0, |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
StgDictObject * |
| 153 |
PyType_stgdict(PyObject *obj) |
| 154 |
{ |
| 155 |
PyTypeObject *type; |
| 156 |
|
| 157 |
if (!PyType_Check(obj)) |
| 158 |
return NULL; |
| 159 |
type = (PyTypeObject *)obj; |
| 160 |
if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS)) |
| 161 |
return NULL; |
| 162 |
if (!type->tp_dict || !PyCStgDict_CheckExact(type->tp_dict)) |
| 163 |
return NULL; |
| 164 |
return (StgDictObject *)type->tp_dict; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
StgDictObject * |
| 173 |
PyObject_stgdict(PyObject *self) |
| 174 |
{ |
| 175 |
PyTypeObject *type = self->ob_type; |
| 176 |
if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS)) |
| 177 |
return NULL; |
| 178 |
if (!type->tp_dict || !PyCStgDict_CheckExact(type->tp_dict)) |
| 179 |
return NULL; |
| 180 |
return (StgDictObject *)type->tp_dict; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
static int |
| 188 |
MakeFields(PyObject *type, CFieldObject *descr, |
| 189 |
Py_ssize_t index, Py_ssize_t offset) |
| 190 |
{ |
| 191 |
Py_ssize_t i; |
| 192 |
PyObject *fields; |
| 193 |
PyObject *fieldlist; |
| 194 |
|
| 195 |
fields = PyObject_GetAttrString(descr->proto, "_fields_"); |
| 196 |
if (fields == NULL) |
| 197 |
return -1; |
| 198 |
fieldlist = PySequence_Fast(fields, "_fields_ must be a sequence"); |
| 199 |
Py_DECREF(fields); |
| 200 |
if (fieldlist == NULL) |
| 201 |
return -1; |
| 202 |
|
| 203 |
for (i = 0; i < PySequence_Fast_GET_SIZE(fieldlist); ++i) { |
| 204 |
PyObject *pair = PySequence_Fast_GET_ITEM(fieldlist, i); |
| 205 |
PyObject *fname, *ftype, *bits; |
| 206 |
CFieldObject *fdescr; |
| 207 |
CFieldObject *new_descr; |
| 208 |
|
| 209 |
if (!PyArg_ParseTuple(pair, "OO|O", &fname, &ftype, &bits)) { |
| 210 |
Py_DECREF(fieldlist); |
| 211 |
return -1; |
| 212 |
} |
| 213 |
fdescr = (CFieldObject *)PyObject_GetAttr(descr->proto, fname); |
| 214 |
if (fdescr == NULL) { |
| 215 |
Py_DECREF(fieldlist); |
| 216 |
return -1; |
| 217 |
} |
| 218 |
if (Py_TYPE(fdescr) != &PyCField_Type) { |
| 219 |
PyErr_SetString(PyExc_TypeError, "unexpected type"); |
| 220 |
Py_DECREF(fdescr); |
| 221 |
Py_DECREF(fieldlist); |
| 222 |
return -1; |
| 223 |
} |
| 224 |
if (fdescr->anonymous) { |
| 225 |
int rc = MakeFields(type, fdescr, |
| 226 |
index + fdescr->index, |
| 227 |
offset + fdescr->offset); |
| 228 |
Py_DECREF(fdescr); |
| 229 |
if (rc == -1) { |
| 230 |
Py_DECREF(fieldlist); |
| 231 |
return -1; |
| 232 |
} |
| 233 |
continue; |
| 234 |
} |
| 235 |
new_descr = (CFieldObject *)PyObject_CallObject((PyObject *)&PyCField_Type, NULL); |
| 236 |
if (new_descr == NULL) { |
| 237 |
Py_DECREF(fdescr); |
| 238 |
Py_DECREF(fieldlist); |
| 239 |
return -1; |
| 240 |
} |
| 241 |
assert(Py_TYPE(new_descr) == &PyCField_Type); |
| 242 |
new_descr->size = fdescr->size; |
| 243 |
new_descr->offset = fdescr->offset + offset; |
| 244 |
new_descr->index = fdescr->index + index; |
| 245 |
new_descr->proto = fdescr->proto; |
| 246 |
Py_XINCREF(new_descr->proto); |
| 247 |
new_descr->getfunc = fdescr->getfunc; |
| 248 |
new_descr->setfunc = fdescr->setfunc; |
| 249 |
|
| 250 |
Py_DECREF(fdescr); |
| 251 |
|
| 252 |
if (-1 == PyObject_SetAttr(type, fname, (PyObject *)new_descr)) { |
| 253 |
Py_DECREF(fieldlist); |
| 254 |
Py_DECREF(new_descr); |
| 255 |
return -1; |
| 256 |
} |
| 257 |
Py_DECREF(new_descr); |
| 258 |
} |
| 259 |
Py_DECREF(fieldlist); |
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
static int |
| 266 |
MakeAnonFields(PyObject *type) |
| 267 |
{ |
| 268 |
PyObject *anon; |
| 269 |
PyObject *anon_names; |
| 270 |
Py_ssize_t i; |
| 271 |
|
| 272 |
anon = PyObject_GetAttrString(type, "_anonymous_"); |
| 273 |
if (anon == NULL) { |
| 274 |
PyErr_Clear(); |
| 275 |
return 0; |
| 276 |
} |
| 277 |
anon_names = PySequence_Fast(anon, "_anonymous_ must be a sequence"); |
| 278 |
Py_DECREF(anon); |
| 279 |
if (anon_names == NULL) |
| 280 |
return -1; |
| 281 |
|
| 282 |
for (i = 0; i < PySequence_Fast_GET_SIZE(anon_names); ++i) { |
| 283 |
PyObject *fname = PySequence_Fast_GET_ITEM(anon_names, i); |
| 284 |
CFieldObject *descr = (CFieldObject *)PyObject_GetAttr(type, fname); |
| 285 |
if (descr == NULL) { |
| 286 |
Py_DECREF(anon_names); |
| 287 |
return -1; |
| 288 |
} |
| 289 |
if (Py_TYPE(descr) != &PyCField_Type) { |
| 290 |
PyErr_Format(PyExc_AttributeError, |
| 291 |
"an item in _anonymous_ (index %zd) is not " |
| 292 |
"specified in _fields_", |
| 293 |
i); |
| 294 |
Py_DECREF(anon_names); |
| 295 |
Py_DECREF(descr); |
| 296 |
return -1; |
| 297 |
} |
| 298 |
descr->anonymous = 1; |
| 299 |
|
| 300 |
|
| 301 |
if (-1 == MakeFields(type, (CFieldObject *)descr, |
| 302 |
((CFieldObject *)descr)->index, |
| 303 |
((CFieldObject *)descr)->offset)) { |
| 304 |
Py_DECREF(descr); |
| 305 |
Py_DECREF(anon_names); |
| 306 |
return -1; |
| 307 |
} |
| 308 |
Py_DECREF(descr); |
| 309 |
} |
| 310 |
|
| 311 |
Py_DECREF(anon_names); |
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
int |
| 320 |
PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct) |
| 321 |
{ |
| 322 |
StgDictObject *stgdict, *basedict; |
| 323 |
Py_ssize_t len, offset, size, align, i; |
| 324 |
Py_ssize_t union_size, total_align; |
| 325 |
Py_ssize_t field_size = 0; |
| 326 |
int bitofs; |
| 327 |
PyObject *isPacked; |
| 328 |
int pack = 0; |
| 329 |
Py_ssize_t ffi_ofs; |
| 330 |
int big_endian; |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
int use_broken_old_ctypes_semantics; |
| 339 |
|
| 340 |
if (fields == NULL) |
| 341 |
return 0; |
| 342 |
|
| 343 |
#ifdef WORDS_BIGENDIAN |
| 344 |
big_endian = PyObject_HasAttrString(type, "_swappedbytes_") ? 0 : 1; |
| 345 |
#else |
| 346 |
big_endian = PyObject_HasAttrString(type, "_swappedbytes_") ? 1 : 0; |
| 347 |
#endif |
| 348 |
|
| 349 |
use_broken_old_ctypes_semantics = \ |
| 350 |
PyObject_HasAttrString(type, "_use_broken_old_ctypes_structure_semantics_"); |
| 351 |
|
| 352 |
isPacked = PyObject_GetAttrString(type, "_pack_"); |
| 353 |
if (isPacked) { |
| 354 |
pack = _PyInt_AsInt(isPacked); |
| 355 |
if (pack < 0 || PyErr_Occurred()) { |
| 356 |
Py_XDECREF(isPacked); |
| 357 |
PyErr_SetString(PyExc_ValueError, |
| 358 |
"_pack_ must be a non-negative integer"); |
| 359 |
return -1; |
| 360 |
} |
| 361 |
Py_DECREF(isPacked); |
| 362 |
} else |
| 363 |
PyErr_Clear(); |
| 364 |
|
| 365 |
len = PySequence_Length(fields); |
| 366 |
if (len == -1) { |
| 367 |
PyErr_SetString(PyExc_TypeError, |
| 368 |
"'_fields_' must be a sequence of pairs"); |
| 369 |
return -1; |
| 370 |
} |
| 371 |
|
| 372 |
stgdict = PyType_stgdict(type); |
| 373 |
if (!stgdict) |
| 374 |
return -1; |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
if (stgdict->flags & DICTFLAG_FINAL) { |
| 379 |
PyErr_SetString(PyExc_AttributeError, |
| 380 |
"_fields_ is final"); |
| 381 |
return -1; |
| 382 |
} |
| 383 |
|
| 384 |
if (stgdict->format) { |
| 385 |
PyMem_Free(stgdict->format); |
| 386 |
stgdict->format = NULL; |
| 387 |
} |
| 388 |
|
| 389 |
if (stgdict->ffi_type_pointer.elements) |
| 390 |
PyMem_Free(stgdict->ffi_type_pointer.elements); |
| 391 |
|
| 392 |
basedict = PyType_stgdict((PyObject *)((PyTypeObject *)type)->tp_base); |
| 393 |
if (basedict && !use_broken_old_ctypes_semantics) { |
| 394 |
size = offset = basedict->size; |
| 395 |
align = basedict->align; |
| 396 |
union_size = 0; |
| 397 |
total_align = align ? align : 1; |
| 398 |
stgdict->ffi_type_pointer.type = FFI_TYPE_STRUCT; |
| 399 |
stgdict->ffi_type_pointer.elements = PyMem_New(ffi_type *, basedict->length + len + 1); |
| 400 |
if (stgdict->ffi_type_pointer.elements == NULL) { |
| 401 |
PyErr_NoMemory(); |
| 402 |
return -1; |
| 403 |
} |
| 404 |
memset(stgdict->ffi_type_pointer.elements, 0, |
| 405 |
sizeof(ffi_type *) * (basedict->length + len + 1)); |
| 406 |
if (basedict->length > 0) { |
| 407 |
memcpy(stgdict->ffi_type_pointer.elements, |
| 408 |
basedict->ffi_type_pointer.elements, |
| 409 |
sizeof(ffi_type *) * (basedict->length)); |
| 410 |
} |
| 411 |
ffi_ofs = basedict->length; |
| 412 |
} else { |
| 413 |
offset = 0; |
| 414 |
size = 0; |
| 415 |
align = 0; |
| 416 |
union_size = 0; |
| 417 |
total_align = 1; |
| 418 |
stgdict->ffi_type_pointer.type = FFI_TYPE_STRUCT; |
| 419 |
stgdict->ffi_type_pointer.elements = PyMem_New(ffi_type *, len + 1); |
| 420 |
if (stgdict->ffi_type_pointer.elements == NULL) { |
| 421 |
PyErr_NoMemory(); |
| 422 |
return -1; |
| 423 |
} |
| 424 |
memset(stgdict->ffi_type_pointer.elements, 0, |
| 425 |
sizeof(ffi_type *) * (len + 1)); |
| 426 |
ffi_ofs = 0; |
| 427 |
} |
| 428 |
|
| 429 |
assert(stgdict->format == NULL); |
| 430 |
if (isStruct && !isPacked) { |
| 431 |
stgdict->format = _ctypes_alloc_format_string(NULL, "T{"); |
| 432 |
} else { |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
stgdict->format = _ctypes_alloc_format_string(NULL, "B"); |
| 437 |
} |
| 438 |
|
| 439 |
#define realdict ((PyObject *)&stgdict->dict) |
| 440 |
for (i = 0; i < len; ++i) { |
| 441 |
PyObject *name = NULL, *desc = NULL; |
| 442 |
PyObject *pair = PySequence_GetItem(fields, i); |
| 443 |
PyObject *prop; |
| 444 |
StgDictObject *dict; |
| 445 |
int bitsize = 0; |
| 446 |
|
| 447 |
if (!pair || !PyArg_ParseTuple(pair, "OO|i", &name, &desc, &bitsize)) { |
| 448 |
PyErr_SetString(PyExc_AttributeError, |
| 449 |
"'_fields_' must be a sequence of pairs"); |
| 450 |
Py_XDECREF(pair); |
| 451 |
return -1; |
| 452 |
} |
| 453 |
dict = PyType_stgdict(desc); |
| 454 |
if (dict == NULL) { |
| 455 |
Py_DECREF(pair); |
| 456 |
PyErr_Format(PyExc_TypeError, |
| 457 |
#if (PY_VERSION_HEX < 0x02050000) |
| 458 |
|
| 459 |
"second item in _fields_ tuple (index %d) must be a C type", |
| 460 |
#else |
| 461 |
"second item in _fields_ tuple (index %zd) must be a C type", |
| 462 |
#endif |
| 463 |
i); |
| 464 |
return -1; |
| 465 |
} |
| 466 |
stgdict->ffi_type_pointer.elements[ffi_ofs + i] = &dict->ffi_type_pointer; |
| 467 |
if (dict->flags & (TYPEFLAG_ISPOINTER | TYPEFLAG_HASPOINTER)) |
| 468 |
stgdict->flags |= TYPEFLAG_HASPOINTER; |
| 469 |
dict->flags |= DICTFLAG_FINAL; |
| 470 |
if (PyTuple_Size(pair) == 3) { |
| 471 |
switch(dict->ffi_type_pointer.type) { |
| 472 |
case FFI_TYPE_UINT8: |
| 473 |
case FFI_TYPE_UINT16: |
| 474 |
case FFI_TYPE_UINT32: |
| 475 |
case FFI_TYPE_SINT64: |
| 476 |
case FFI_TYPE_UINT64: |
| 477 |
break; |
| 478 |
|
| 479 |
case FFI_TYPE_SINT8: |
| 480 |
case FFI_TYPE_SINT16: |
| 481 |
case FFI_TYPE_SINT32: |
| 482 |
if (dict->getfunc != _ctypes_get_fielddesc("c")->getfunc |
| 483 |
#ifdef CTYPES_UNICODE |
| 484 |
&& dict->getfunc != _ctypes_get_fielddesc("u")->getfunc |
| 485 |
#endif |
| 486 |
) |
| 487 |
break; |
| 488 |
|
| 489 |
default: |
| 490 |
PyErr_Format(PyExc_TypeError, |
| 491 |
"bit fields not allowed for type %s", |
| 492 |
((PyTypeObject *)desc)->tp_name); |
| 493 |
Py_DECREF(pair); |
| 494 |
return -1; |
| 495 |
} |
| 496 |
if (bitsize <= 0 || bitsize > dict->size * 8) { |
| 497 |
PyErr_SetString(PyExc_ValueError, |
| 498 |
"number of bits invalid for bit field"); |
| 499 |
Py_DECREF(pair); |
| 500 |
return -1; |
| 501 |
} |
| 502 |
} else |
| 503 |
bitsize = 0; |
| 504 |
if (isStruct && !isPacked) { |
| 505 |
char *fieldfmt = dict->format ? dict->format : "B"; |
| 506 |
char *fieldname = PyString_AsString(name); |
| 507 |
char *ptr; |
| 508 |
Py_ssize_t len; |
| 509 |
char *buf; |
| 510 |
|
| 511 |
if (fieldname == NULL) |
| 512 |
{ |
| 513 |
PyErr_Format(PyExc_TypeError, |
| 514 |
"structure field name must be string not %s", |
| 515 |
name->ob_type->tp_name); |
| 516 |
|
| 517 |
Py_DECREF(pair); |
| 518 |
return -1; |
| 519 |
} |
| 520 |
|
| 521 |
len = strlen(fieldname) + strlen(fieldfmt); |
| 522 |
|
| 523 |
buf = PyMem_Malloc(len + 2 + 1); |
| 524 |
if (buf == NULL) { |
| 525 |
Py_DECREF(pair); |
| 526 |
PyErr_NoMemory(); |
| 527 |
return -1; |
| 528 |
} |
| 529 |
sprintf(buf, "%s:%s:", fieldfmt, fieldname); |
| 530 |
|
| 531 |
ptr = stgdict->format; |
| 532 |
if (dict->shape != NULL) { |
| 533 |
stgdict->format = _ctypes_alloc_format_string_with_shape( |
| 534 |
dict->ndim, dict->shape, stgdict->format, buf); |
| 535 |
} else { |
| 536 |
stgdict->format = _ctypes_alloc_format_string(stgdict->format, buf); |
| 537 |
} |
| 538 |
PyMem_Free(ptr); |
| 539 |
PyMem_Free(buf); |
| 540 |
|
| 541 |
if (stgdict->format == NULL) { |
| 542 |
Py_DECREF(pair); |
| 543 |
return -1; |
| 544 |
} |
| 545 |
} |
| 546 |
if (isStruct) { |
| 547 |
prop = PyCField_FromDesc(desc, i, |
| 548 |
&field_size, bitsize, &bitofs, |
| 549 |
&size, &offset, &align, |
| 550 |
pack, big_endian); |
| 551 |
} else { |
| 552 |
size = 0; |
| 553 |
offset = 0; |
| 554 |
align = 0; |
| 555 |
prop = PyCField_FromDesc(desc, i, |
| 556 |
&field_size, bitsize, &bitofs, |
| 557 |
&size, &offset, &align, |
| 558 |
pack, big_endian); |
| 559 |
union_size = max(size, union_size); |
| 560 |
} |
| 561 |
total_align = max(align, total_align); |
| 562 |
|
| 563 |
if (!prop) { |
| 564 |
Py_DECREF(pair); |
| 565 |
return -1; |
| 566 |
} |
| 567 |
if (-1 == PyObject_SetAttr(type, name, prop)) { |
| 568 |
Py_DECREF(prop); |
| 569 |
Py_DECREF(pair); |
| 570 |
return -1; |
| 571 |
} |
| 572 |
Py_DECREF(pair); |
| 573 |
Py_DECREF(prop); |
| 574 |
} |
| 575 |
#undef realdict |
| 576 |
|
| 577 |
if (isStruct && !isPacked) { |
| 578 |
char *ptr = stgdict->format; |
| 579 |
stgdict->format = _ctypes_alloc_format_string(stgdict->format, "}"); |
| 580 |
PyMem_Free(ptr); |
| 581 |
if (stgdict->format == NULL) |
| 582 |
return -1; |
| 583 |
} |
| 584 |
|
| 585 |
if (!isStruct) |
| 586 |
size = union_size; |
| 587 |
|
| 588 |
|
| 589 |
size = ((size + total_align - 1) / total_align) * total_align; |
| 590 |
|
| 591 |
stgdict->ffi_type_pointer.alignment = Py_SAFE_DOWNCAST(total_align, |
| 592 |
Py_ssize_t, |
| 593 |
unsigned short); |
| 594 |
stgdict->ffi_type_pointer.size = size; |
| 595 |
|
| 596 |
stgdict->size = size; |
| 597 |
stgdict->align = total_align; |
| 598 |
stgdict->length = len; |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
if (stgdict->flags & DICTFLAG_FINAL) { |
| 603 |
PyErr_SetString(PyExc_AttributeError, |
| 604 |
"Structure or union cannot contain itself"); |
| 605 |
return -1; |
| 606 |
} |
| 607 |
stgdict->flags |= DICTFLAG_FINAL; |
| 608 |
|
| 609 |
return MakeAnonFields(type); |
| 610 |
} |