-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
/
cfield.c
1686 lines (1488 loc) · 42.4 KB
/
cfield.c
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif
#include "Python.h"
// windows.h must be included before pycore internal headers
#ifdef MS_WIN32
# include <windows.h>
#endif
#include "pycore_bitutils.h" // _Py_bswap32()
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include <ffi.h>
#include "ctypes.h"
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
# include "../_complex.h" // complex
#endif
#define CTYPES_CFIELD_CAPSULE_NAME_PYMEM "_ctypes/cfield.c pymem"
/*[clinic input]
module _ctypes
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=476a19c49b31a75c]*/
#include "clinic/cfield.c.h"
static void pymem_destructor(PyObject *ptr)
{
void *p = PyCapsule_GetPointer(ptr, CTYPES_CFIELD_CAPSULE_NAME_PYMEM);
if (p) {
PyMem_Free(p);
}
}
/******************************************************************/
/*
PyCField_Type
*/
/*[clinic input]
class _ctypes.CField "PyObject *" "PyObject"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=602817ea3ffc709c]*/
static inline
Py_ssize_t NUM_BITS(Py_ssize_t bitsize);
static inline
Py_ssize_t LOW_BIT(Py_ssize_t offset);
/*[clinic input]
@classmethod
_ctypes.CField.__new__ as PyCField_new
name: object(subclass_of='&PyUnicode_Type')
type as proto: object
size: Py_ssize_t
offset: Py_ssize_t
index: Py_ssize_t
bit_size as bit_size_obj: object = None
[clinic start generated code]*/
static PyObject *
PyCField_new_impl(PyTypeObject *type, PyObject *name, PyObject *proto,
Py_ssize_t size, Py_ssize_t offset, Py_ssize_t index,
PyObject *bit_size_obj)
/*[clinic end generated code: output=43649ef9157c5f58 input=3d813f56373c4caa]*/
{
CFieldObject* self = NULL;
if (size < 0) {
PyErr_Format(PyExc_ValueError,
"size of field %R must not be negative, got %zd",
name, size);
goto error;
}
// assert: no overflow;
if ((unsigned long long int) size
>= (1ULL << (8*sizeof(Py_ssize_t)-1)) / 8) {
PyErr_Format(PyExc_ValueError,
"size of field %R is too big: %zd", name, size);
goto error;
}
PyTypeObject *tp = type;
ctypes_state *st = get_module_state_by_class(tp);
self = (CFieldObject *)tp->tp_alloc(tp, 0);
if (!self) {
return NULL;
}
if (PyUnicode_CheckExact(name)) {
self->name = Py_NewRef(name);
} else {
self->name = PyObject_Str(name);
if (!self->name) {
goto error;
}
}
StgInfo *info;
if (PyStgInfo_FromType(st, proto, &info) < 0) {
goto error;
}
if (info == NULL) {
PyErr_Format(PyExc_TypeError,
"type of field %R must be a C type", self->name);
goto error;
}
Py_ssize_t bit_size = NUM_BITS(size);
if (bit_size) {
assert(bit_size > 0);
assert(bit_size <= info->size * 8);
switch(info->ffi_type_pointer.type) {
case FFI_TYPE_UINT8:
case FFI_TYPE_UINT16:
case FFI_TYPE_UINT32:
case FFI_TYPE_SINT64:
case FFI_TYPE_UINT64:
break;
case FFI_TYPE_SINT8:
case FFI_TYPE_SINT16:
case FFI_TYPE_SINT32:
if (info->getfunc != _ctypes_get_fielddesc("c")->getfunc
&& info->getfunc != _ctypes_get_fielddesc("u")->getfunc)
{
break;
}
_Py_FALLTHROUGH; /* else fall through */
default:
PyErr_Format(PyExc_TypeError,
"bit fields not allowed for type %s",
((PyTypeObject*)proto)->tp_name);
goto error;
}
}
self->proto = Py_NewRef(proto);
self->size = size;
self->offset = offset;
self->index = index;
/* Field descriptors for 'c_char * n' are be special cased to
return a Python string instead of an Array object instance...
*/
self->setfunc = NULL;
self->getfunc = NULL;
if (PyCArrayTypeObject_Check(st, proto)) {
StgInfo *ainfo;
if (PyStgInfo_FromType(st, proto, &ainfo) < 0) {
goto error;
}
if (ainfo && ainfo->proto) {
StgInfo *iinfo;
if (PyStgInfo_FromType(st, ainfo->proto, &iinfo) < 0) {
goto error;
}
if (!iinfo) {
PyErr_SetString(PyExc_TypeError,
"has no _stginfo_");
goto error;
}
if (iinfo->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
struct fielddesc *fd = _ctypes_get_fielddesc("s");
self->getfunc = fd->getfunc;
self->setfunc = fd->setfunc;
}
if (iinfo->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
struct fielddesc *fd = _ctypes_get_fielddesc("U");
self->getfunc = fd->getfunc;
self->setfunc = fd->setfunc;
}
}
}
return (PyObject *)self;
error:
Py_XDECREF(self);
return NULL;
}
static int
PyCField_set(CFieldObject *self, PyObject *inst, PyObject *value)
{
CDataObject *dst;
char *ptr;
ctypes_state *st = get_module_state_by_class(Py_TYPE(self));
if (!CDataObject_Check(st, inst)) {
PyErr_SetString(PyExc_TypeError,
"not a ctype instance");
return -1;
}
dst = (CDataObject *)inst;
ptr = dst->b_ptr + self->offset;
if (value == NULL) {
PyErr_SetString(PyExc_TypeError,
"can't delete attribute");
return -1;
}
return PyCData_set(st, inst, self->proto, self->setfunc, value,
self->index, self->size, ptr);
}
static PyObject *
PyCField_get(CFieldObject *self, PyObject *inst, PyTypeObject *type)
{
CDataObject *src;
if (inst == NULL) {
return Py_NewRef(self);
}
ctypes_state *st = get_module_state_by_class(Py_TYPE(self));
if (!CDataObject_Check(st, inst)) {
PyErr_SetString(PyExc_TypeError,
"not a ctype instance");
return NULL;
}
src = (CDataObject *)inst;
return PyCData_get(st, self->proto, self->getfunc, inst,
self->index, self->size, src->b_ptr + self->offset);
}
static PyObject *
PyCField_get_offset(PyObject *self, void *data)
{
return PyLong_FromSsize_t(((CFieldObject *)self)->offset);
}
static PyObject *
PyCField_get_size(PyObject *self, void *data)
{
return PyLong_FromSsize_t(((CFieldObject *)self)->size);
}
static PyGetSetDef PyCField_getset[] = {
{ "offset", PyCField_get_offset, NULL, PyDoc_STR("offset in bytes of this field") },
{ "size", PyCField_get_size, NULL, PyDoc_STR("size in bytes of this field") },
{ NULL, NULL, NULL, NULL },
};
static int
PyCField_traverse(CFieldObject *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
Py_VISIT(self->proto);
return 0;
}
static int
PyCField_clear(CFieldObject *self)
{
Py_CLEAR(self->proto);
return 0;
}
static void
PyCField_dealloc(PyObject *self)
{
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
CFieldObject *self_cf = (CFieldObject *)self;
(void)PyCField_clear(self_cf);
Py_CLEAR(self_cf->name);
Py_TYPE(self)->tp_free(self);
Py_DECREF(tp);
}
static PyObject *
PyCField_repr(CFieldObject *self)
{
PyObject *result;
Py_ssize_t bits = NUM_BITS(self->size);
Py_ssize_t size = LOW_BIT(self->size);
const char *name;
name = ((PyTypeObject *)self->proto)->tp_name;
if (bits)
result = PyUnicode_FromFormat(
"<Field type=%s, ofs=%zd:%zd, bits=%zd>",
name, self->offset, size, bits);
else
result = PyUnicode_FromFormat(
"<Field type=%s, ofs=%zd, size=%zd>",
name, self->offset, size);
return result;
}
static PyType_Slot cfield_slots[] = {
{Py_tp_new, PyCField_new},
{Py_tp_dealloc, PyCField_dealloc},
{Py_tp_repr, PyCField_repr},
{Py_tp_doc, (void *)PyDoc_STR("Structure/Union member")},
{Py_tp_traverse, PyCField_traverse},
{Py_tp_clear, PyCField_clear},
{Py_tp_getset, PyCField_getset},
{Py_tp_descr_get, PyCField_get},
{Py_tp_descr_set, PyCField_set},
{0, NULL},
};
PyType_Spec cfield_spec = {
.name = "_ctypes.CField",
.basicsize = sizeof(CFieldObject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
Py_TPFLAGS_IMMUTABLETYPE),
.slots = cfield_slots,
};
/******************************************************************/
/*
Accessor functions
*/
/* Derived from Modules/structmodule.c:
Helper routine to get a Python integer and raise the appropriate error
if it isn't one */
static int
get_long(PyObject *v, long *p)
{
long x = PyLong_AsUnsignedLongMask(v);
if (x == -1 && PyErr_Occurred())
return -1;
*p = x;
return 0;
}
/* Same, but handling unsigned long */
static int
get_ulong(PyObject *v, unsigned long *p)
{
unsigned long x = PyLong_AsUnsignedLongMask(v);
if (x == (unsigned long)-1 && PyErr_Occurred())
return -1;
*p = x;
return 0;
}
/* Same, but handling native long long. */
static int
get_longlong(PyObject *v, long long *p)
{
long long x = PyLong_AsUnsignedLongLongMask(v);
if (x == -1 && PyErr_Occurred())
return -1;
*p = x;
return 0;
}
/* Same, but handling native unsigned long long. */
static int
get_ulonglong(PyObject *v, unsigned long long *p)
{
unsigned long long x = PyLong_AsUnsignedLongLongMask(v);
if (x == (unsigned long long)-1 && PyErr_Occurred())
return -1;
*p = x;
return 0;
}
/*****************************************************************
* Integer fields, with bitfield support
*/
/* how to decode the size field, for integer get/set functions */
static inline
Py_ssize_t LOW_BIT(Py_ssize_t offset) {
return offset & 0xFFFF;
}
static inline
Py_ssize_t NUM_BITS(Py_ssize_t bitsize) {
return bitsize >> 16;
}
/* Doesn't work if NUM_BITS(size) == 0, but it never happens in SET() call. */
#define BIT_MASK(type, size) (((((type)1 << (NUM_BITS(size) - 1)) - 1) << 1) + 1)
/* This macro CHANGES the first parameter IN PLACE. For proper sign handling,
we must first shift left, then right.
*/
#define GET_BITFIELD(v, size) \
if (NUM_BITS(size)) { \
v <<= (sizeof(v)*8 - LOW_BIT(size) - NUM_BITS(size)); \
v >>= (sizeof(v)*8 - NUM_BITS(size)); \
}
/* This macro RETURNS the first parameter with the bit field CHANGED. */
#define SET(type, x, v, size) \
(NUM_BITS(size) ? \
( ( (type)x & ~(BIT_MASK(type, size) << LOW_BIT(size)) ) | ( ((type)v & BIT_MASK(type, size)) << LOW_BIT(size) ) ) \
: (type)v)
#if SIZEOF_SHORT == 2
# define SWAP_SHORT _Py_bswap16
#else
# error "unsupported short size"
#endif
#if SIZEOF_INT == 4
# define SWAP_INT _Py_bswap32
#else
# error "unsupported int size"
#endif
#if SIZEOF_LONG == 4
# define SWAP_LONG _Py_bswap32
#elif SIZEOF_LONG == 8
# define SWAP_LONG _Py_bswap64
#else
# error "unsupported long size"
#endif
#if SIZEOF_LONG_LONG == 8
# define SWAP_LONG_LONG _Py_bswap64
#else
# error "unsupported long long size"
#endif
/*****************************************************************
* The setter methods return an object which must be kept alive, to keep the
* data valid which has been stored in the memory block. The ctypes object
* instance inserts this object into its 'b_objects' list.
*
* For simple Python types like integers or characters, there is nothing that
* has to been kept alive, so Py_None is returned in these cases. But this
* makes inspecting the 'b_objects' list, which is accessible from Python for
* debugging, less useful.
*
* So, defining the _CTYPES_DEBUG_KEEP symbol returns the original value
* instead of Py_None.
*/
#ifdef _CTYPES_DEBUG_KEEP
#define _RET(x) Py_INCREF(x); return x
#else
#define _RET(X) Py_RETURN_NONE
#endif
/*****************************************************************
* integer accessor methods, supporting bit fields
*/
static PyObject *
b_set(void *ptr, PyObject *value, Py_ssize_t size)
{
long val;
if (get_long(value, &val) < 0)
return NULL;
*(signed char *)ptr = SET(signed char, *(signed char *)ptr, val, size);
_RET(value);
}
static PyObject *
b_get(void *ptr, Py_ssize_t size)
{
signed char val = *(signed char *)ptr;
GET_BITFIELD(val, size);
return PyLong_FromLong(val);
}
static PyObject *
B_set(void *ptr, PyObject *value, Py_ssize_t size)
{
unsigned long val;
if (get_ulong(value, &val) < 0)
return NULL;
*(unsigned char *)ptr = SET(unsigned char, *(unsigned char*)ptr, val, size);
_RET(value);
}
static PyObject *
B_get(void *ptr, Py_ssize_t size)
{
unsigned char val = *(unsigned char *)ptr;
GET_BITFIELD(val, size);
return PyLong_FromLong(val);
}
static PyObject *
h_set(void *ptr, PyObject *value, Py_ssize_t size)
{
long val;
short x;
if (get_long(value, &val) < 0)
return NULL;
memcpy(&x, ptr, sizeof(x));
x = SET(short, x, val, size);
memcpy(ptr, &x, sizeof(x));
_RET(value);
}
static PyObject *
h_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
long val;
short field;
if (get_long(value, &val) < 0) {
return NULL;
}
memcpy(&field, ptr, sizeof(field));
field = SWAP_SHORT(field);
field = SET(short, field, val, size);
field = SWAP_SHORT(field);
memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
h_get(void *ptr, Py_ssize_t size)
{
short val;
memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyLong_FromLong((long)val);
}
static PyObject *
h_get_sw(void *ptr, Py_ssize_t size)
{
short val;
memcpy(&val, ptr, sizeof(val));
val = SWAP_SHORT(val);
GET_BITFIELD(val, size);
return PyLong_FromLong(val);
}
static PyObject *
H_set(void *ptr, PyObject *value, Py_ssize_t size)
{
unsigned long val;
unsigned short x;
if (get_ulong(value, &val) < 0)
return NULL;
memcpy(&x, ptr, sizeof(x));
x = SET(unsigned short, x, val, size);
memcpy(ptr, &x, sizeof(x));
_RET(value);
}
static PyObject *
H_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
unsigned long val;
unsigned short field;
if (get_ulong(value, &val) < 0) {
return NULL;
}
memcpy(&field, ptr, sizeof(field));
field = SWAP_SHORT(field);
field = SET(unsigned short, field, val, size);
field = SWAP_SHORT(field);
memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
H_get(void *ptr, Py_ssize_t size)
{
unsigned short val;
memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyLong_FromLong(val);
}
static PyObject *
H_get_sw(void *ptr, Py_ssize_t size)
{
unsigned short val;
memcpy(&val, ptr, sizeof(val));
val = SWAP_SHORT(val);
GET_BITFIELD(val, size);
return PyLong_FromLong(val);
}
static PyObject *
i_set(void *ptr, PyObject *value, Py_ssize_t size)
{
long val;
int x;
if (get_long(value, &val) < 0)
return NULL;
memcpy(&x, ptr, sizeof(x));
x = SET(int, x, val, size);
memcpy(ptr, &x, sizeof(x));
_RET(value);
}
static PyObject *
i_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
long val;
int field;
if (get_long(value, &val) < 0) {
return NULL;
}
memcpy(&field, ptr, sizeof(field));
field = SWAP_INT(field);
field = SET(int, field, val, size);
field = SWAP_INT(field);
memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
i_get(void *ptr, Py_ssize_t size)
{
int val;
memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyLong_FromLong(val);
}
static PyObject *
i_get_sw(void *ptr, Py_ssize_t size)
{
int val;
memcpy(&val, ptr, sizeof(val));
val = SWAP_INT(val);
GET_BITFIELD(val, size);
return PyLong_FromLong(val);
}
#ifndef MS_WIN32
/* http://msdn.microsoft.com/en-us/library/cc237864.aspx */
#define VARIANT_FALSE 0x0000
#define VARIANT_TRUE 0xFFFF
#endif
/* short BOOL - VARIANT_BOOL */
static PyObject *
vBOOL_set(void *ptr, PyObject *value, Py_ssize_t size)
{
switch (PyObject_IsTrue(value)) {
case -1:
return NULL;
case 0:
*(short int *)ptr = VARIANT_FALSE;
_RET(value);
default:
*(short int *)ptr = VARIANT_TRUE;
_RET(value);
}
}
static PyObject *
vBOOL_get(void *ptr, Py_ssize_t size)
{
return PyBool_FromLong((long)*(short int *)ptr);
}
static PyObject *
bool_set(void *ptr, PyObject *value, Py_ssize_t size)
{
switch (PyObject_IsTrue(value)) {
case -1:
return NULL;
case 0:
*(_Bool *)ptr = 0;
_RET(value);
default:
*(_Bool *)ptr = 1;
_RET(value);
}
}
static PyObject *
bool_get(void *ptr, Py_ssize_t size)
{
return PyBool_FromLong((long)*(_Bool *)ptr);
}
static PyObject *
I_set(void *ptr, PyObject *value, Py_ssize_t size)
{
unsigned long val;
unsigned int x;
if (get_ulong(value, &val) < 0)
return NULL;
memcpy(&x, ptr, sizeof(x));
x = SET(unsigned int, x, val, size);
memcpy(ptr, &x, sizeof(x));
_RET(value);
}
static PyObject *
I_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
unsigned long val;
unsigned int field;
if (get_ulong(value, &val) < 0) {
return NULL;
}
memcpy(&field, ptr, sizeof(field));
field = SWAP_INT(field);
field = SET(unsigned int, field, (unsigned int)val, size);
field = SWAP_INT(field);
memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
I_get(void *ptr, Py_ssize_t size)
{
unsigned int val;
memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyLong_FromUnsignedLong(val);
}
static PyObject *
I_get_sw(void *ptr, Py_ssize_t size)
{
unsigned int val;
memcpy(&val, ptr, sizeof(val));
val = SWAP_INT(val);
GET_BITFIELD(val, size);
return PyLong_FromUnsignedLong(val);
}
static PyObject *
l_set(void *ptr, PyObject *value, Py_ssize_t size)
{
long val;
long x;
if (get_long(value, &val) < 0)
return NULL;
memcpy(&x, ptr, sizeof(x));
x = SET(long, x, val, size);
memcpy(ptr, &x, sizeof(x));
_RET(value);
}
static PyObject *
l_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
long val;
long field;
if (get_long(value, &val) < 0) {
return NULL;
}
memcpy(&field, ptr, sizeof(field));
field = SWAP_LONG(field);
field = SET(long, field, val, size);
field = SWAP_LONG(field);
memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
l_get(void *ptr, Py_ssize_t size)
{
long val;
memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyLong_FromLong(val);
}
static PyObject *
l_get_sw(void *ptr, Py_ssize_t size)
{
long val;
memcpy(&val, ptr, sizeof(val));
val = SWAP_LONG(val);
GET_BITFIELD(val, size);
return PyLong_FromLong(val);
}
static PyObject *
L_set(void *ptr, PyObject *value, Py_ssize_t size)
{
unsigned long val;
unsigned long x;
if (get_ulong(value, &val) < 0)
return NULL;
memcpy(&x, ptr, sizeof(x));
x = SET(unsigned long, x, val, size);
memcpy(ptr, &x, sizeof(x));
_RET(value);
}
static PyObject *
L_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
unsigned long val;
unsigned long field;
if (get_ulong(value, &val) < 0) {
return NULL;
}
memcpy(&field, ptr, sizeof(field));
field = SWAP_LONG(field);
field = SET(unsigned long, field, val, size);
field = SWAP_LONG(field);
memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
L_get(void *ptr, Py_ssize_t size)
{
unsigned long val;
memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyLong_FromUnsignedLong(val);
}
static PyObject *
L_get_sw(void *ptr, Py_ssize_t size)
{
unsigned long val;
memcpy(&val, ptr, sizeof(val));
val = SWAP_LONG(val);
GET_BITFIELD(val, size);
return PyLong_FromUnsignedLong(val);
}
static PyObject *
q_set(void *ptr, PyObject *value, Py_ssize_t size)
{
long long val;
long long x;
if (get_longlong(value, &val) < 0)
return NULL;
memcpy(&x, ptr, sizeof(x));
x = SET(long long, x, val, size);
memcpy(ptr, &x, sizeof(x));
_RET(value);
}
static PyObject *
q_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
long long val;
long long field;
if (get_longlong(value, &val) < 0) {
return NULL;
}
memcpy(&field, ptr, sizeof(field));
field = SWAP_LONG_LONG(field);
field = SET(long long, field, val, size);
field = SWAP_LONG_LONG(field);
memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
q_get(void *ptr, Py_ssize_t size)
{
long long val;
memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyLong_FromLongLong(val);
}
static PyObject *
q_get_sw(void *ptr, Py_ssize_t size)
{
long long val;
memcpy(&val, ptr, sizeof(val));
val = SWAP_LONG_LONG(val);
GET_BITFIELD(val, size);
return PyLong_FromLongLong(val);
}
static PyObject *
Q_set(void *ptr, PyObject *value, Py_ssize_t size)
{
unsigned long long val;
unsigned long long x;
if (get_ulonglong(value, &val) < 0)
return NULL;
memcpy(&x, ptr, sizeof(x));
x = SET(long long, x, val, size);
memcpy(ptr, &x, sizeof(x));
_RET(value);
}
static PyObject *
Q_set_sw(void *ptr, PyObject *value, Py_ssize_t size)
{
unsigned long long val;
unsigned long long field;
if (get_ulonglong(value, &val) < 0) {
return NULL;
}
memcpy(&field, ptr, sizeof(field));
field = SWAP_LONG_LONG(field);
field = SET(unsigned long long, field, val, size);
field = SWAP_LONG_LONG(field);
memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
Q_get(void *ptr, Py_ssize_t size)
{
unsigned long long val;
memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyLong_FromUnsignedLongLong(val);
}
static PyObject *
Q_get_sw(void *ptr, Py_ssize_t size)
{
unsigned long long val;
memcpy(&val, ptr, sizeof(val));
val = SWAP_LONG_LONG(val);
GET_BITFIELD(val, size);
return PyLong_FromUnsignedLongLong(val);
}
/*****************************************************************
* non-integer accessor methods, not supporting bit fields
*/
static PyObject *
g_set(void *ptr, PyObject *value, Py_ssize_t size)
{
long double x;
x = PyFloat_AsDouble(value);
if (x == -1 && PyErr_Occurred())
return NULL;
memcpy(ptr, &x, sizeof(long double));
_RET(value);
}
static PyObject *
g_get(void *ptr, Py_ssize_t size)
{
long double val;
memcpy(&val, ptr, sizeof(long double));
return PyFloat_FromDouble(val);
}
static PyObject *
d_set(void *ptr, PyObject *value, Py_ssize_t size)
{
double x;
x = PyFloat_AsDouble(value);
if (x == -1 && PyErr_Occurred())
return NULL;
memcpy(ptr, &x, sizeof(double));
_RET(value);
}
static PyObject *
d_get(void *ptr, Py_ssize_t size)
{
double val;
memcpy(&val, ptr, sizeof(val));
return PyFloat_FromDouble(val);
}
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
static PyObject *
C_set(void *ptr, PyObject *value, Py_ssize_t size)
{
Py_complex c = PyComplex_AsCComplex(value);
if (c.real == -1 && PyErr_Occurred()) {
return NULL;
}
double complex x = CMPLX(c.real, c.imag);
memcpy(ptr, &x, sizeof(x));
_RET(value);
}
static PyObject *
C_get(void *ptr, Py_ssize_t size)
{
double complex x;
memcpy(&x, ptr, sizeof(x));
return PyComplex_FromDoubles(creal(x), cimag(x));
}
static PyObject *
E_set(void *ptr, PyObject *value, Py_ssize_t size)
{