Add optional align attribute to ctypes.Structure · monkeyman192/cpython@e3ad227 (original) (raw)

`@@ -379,6 +379,7 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct

`

379

379

`int bitofs;

`

380

380

`PyObject *tmp;

`

381

381

`int pack;

`

``

382

`+

int forced_alignment = 1;

`

382

383

`Py_ssize_t ffi_ofs;

`

383

384

`int big_endian;

`

384

385

`int arrays_seen = 0;

`

`@@ -419,6 +420,28 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct

`

419

420

`pack = 0;

`

420

421

` }

`

421

422

``

``

423

`+

if (PyObject_GetOptionalAttr(type, &_Py_ID(align), &tmp) < 0) {

`

``

424

`+

return -1;

`

``

425

`+

}

`

``

426

`+

if (tmp) {

`

``

427

`+

forced_alignment = PyLong_AsInt(tmp);

`

``

428

`+

Py_DECREF(tmp);

`

``

429

`+

if (forced_alignment < 0) {

`

``

430

`+

if (!PyErr_Occurred() ||

`

``

431

`+

PyErr_ExceptionMatches(PyExc_TypeError) ||

`

``

432

`+

PyErr_ExceptionMatches(PyExc_OverflowError))

`

``

433

`+

{

`

``

434

`+

PyErr_SetString(PyExc_ValueError,

`

``

435

`+

"align must be a non-negative integer");

`

``

436

`+

}

`

``

437

`+

return -1;

`

``

438

`+

}

`

``

439

`+

}

`

``

440

`+

else {

`

``

441

`` +

/* Setting _align_ = 0 amounts to using the default alignment */

``

``

442

`+

forced_alignment = 1;

`

``

443

`+

}

`

``

444

+

422

445

`len = PySequence_Size(fields);

`

423

446

`if (len == -1) {

`

424

447

`if (PyErr_ExceptionMatches(PyExc_TypeError)) {

`

`@@ -463,7 +486,7 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct

`

463

486

`size = offset = basedict->size;

`

464

487

`align = basedict->align;

`

465

488

`union_size = 0;

`

466

``

`-

total_align = align ? align : 1;

`

``

489

`+

total_align = align ? align : forced_alignment;

`

467

490

`stgdict->ffi_type_pointer.type = FFI_TYPE_STRUCT;

`

468

491

`stgdict->ffi_type_pointer.elements = PyMem_New(ffi_type *, basedict->length + len + 1);

`

469

492

`if (stgdict->ffi_type_pointer.elements == NULL) {

`

`@@ -483,7 +506,7 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct

`

483

506

`size = 0;

`

484

507

`align = 0;

`

485

508

`union_size = 0;

`

486

``

`-

total_align = 1;

`

``

509

`+

total_align = forced_alignment;

`

487

510

`stgdict->ffi_type_pointer.type = FFI_TYPE_STRUCT;

`

488

511

`stgdict->ffi_type_pointer.elements = PyMem_New(ffi_type *, len + 1);

`

489

512

`if (stgdict->ffi_type_pointer.elements == NULL) {

`