Python-2.7.3/Modules/_ctypes/cfield.c

Location Tool Test ID Function Issue
/builddir/build/BUILD/Python-2.7.3/Modules/_ctypes/cfield.c:50:29 gcc unused-but-set-variable PyCField_FromDesc variable 'length' set but not used
/builddir/build/BUILD/Python-2.7.3/Modules/_ctypes/cfield.c:50:29 gcc unused-but-set-variable PyCField_FromDesc variable 'length' set but not used
/builddir/build/BUILD/Python-2.7.3/Modules/_ctypes/cfield.c:104:5 clang-analyzer Value stored to 'length' is never read
/builddir/build/BUILD/Python-2.7.3/Modules/_ctypes/cfield.c:104:5 clang-analyzer Value stored to 'length' is never read
   1 /*****************************************************************
   2   This file should be kept compatible with Python 2.3, see PEP 291.
   3  *****************************************************************/
   4 
   5 #include "Python.h"
   6 
   7 #include <ffi.h>
   8 #ifdef MS_WIN32
   9 #include <windows.h>
  10 #endif
  11 #include "ctypes.h"
  12 
  13 
  14 #define CTYPES_CAPSULE_WCHAR_T "_ctypes/cfield.c wchar_t buffer from unicode"
  15 CTYPES_CAPSULE_INSTANTIATE_DESTRUCTOR(CTYPES_CAPSULE_WCHAR_T)
  16 
  17 
  18 /******************************************************************/
  19 /*
  20   PyCField_Type
  21 */
  22 static PyObject *
  23 PyCField_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  24 {
  25     CFieldObject *obj;
  26     obj = (CFieldObject *)type->tp_alloc(type, 0);
  27     return (PyObject *)obj;
  28 }
  29 
  30 /*
  31  * Expects the size, index and offset for the current field in *psize and
  32  * *poffset, stores the total size so far in *psize, the offset for the next
  33  * field in *poffset, the alignment requirements for the current field in
  34  * *palign, and returns a field desriptor for this field.
  35  */
  36 /*
  37  * bitfields extension:
  38  * bitsize != 0: this is a bit field.
  39  * pbitofs points to the current bit offset, this will be updated.
  40  * prev_desc points to the type of the previous bitfield, if any.
  41  */
  42 PyObject *
  43 PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
  44                 Py_ssize_t *pfield_size, int bitsize, int *pbitofs,
  45                 Py_ssize_t *psize, Py_ssize_t *poffset, Py_ssize_t *palign,
  46                 int pack, int big_endian)
  47 {
  48     CFieldObject *self;
  49     PyObject *proto;
  50     Py_ssize_t size, align, length;
variable 'length' set but not used
(emitted by gcc)
variable 'length' set but not used
(emitted by gcc)
51 SETFUNC setfunc = NULL; 52 GETFUNC getfunc = NULL; 53 StgDictObject *dict; 54 int fieldtype; 55 #define NO_BITFIELD 0 56 #define NEW_BITFIELD 1 57 #define CONT_BITFIELD 2 58 #define EXPAND_BITFIELD 3 59 60 self = (CFieldObject *)PyObject_CallObject((PyObject *)&PyCField_Type, 61 NULL); 62 if (self == NULL) 63 return NULL; 64 dict = PyType_stgdict(desc); 65 if (!dict) { 66 PyErr_SetString(PyExc_TypeError, 67 "has no _stginfo_"); 68 Py_DECREF(self); 69 return NULL; 70 } 71 if (bitsize /* this is a bitfield request */ 72 && *pfield_size /* we have a bitfield open */ 73 #ifdef MS_WIN32 74 /* MSVC, GCC with -mms-bitfields */ 75 && dict->size * 8 == *pfield_size 76 #else 77 /* GCC */ 78 && dict->size * 8 <= *pfield_size 79 #endif 80 && (*pbitofs + bitsize) <= *pfield_size) { 81 /* continue bit field */ 82 fieldtype = CONT_BITFIELD; 83 #ifndef MS_WIN32 84 } else if (bitsize /* this is a bitfield request */ 85 && *pfield_size /* we have a bitfield open */ 86 && dict->size * 8 >= *pfield_size 87 && (*pbitofs + bitsize) <= dict->size * 8) { 88 /* expand bit field */ 89 fieldtype = EXPAND_BITFIELD; 90 #endif 91 } else if (bitsize) { 92 /* start new bitfield */ 93 fieldtype = NEW_BITFIELD; 94 *pbitofs = 0; 95 *pfield_size = dict->size * 8; 96 } else { 97 /* not a bit field */ 98 fieldtype = NO_BITFIELD; 99 *pbitofs = 0; 100 *pfield_size = 0; 101 } 102 103 size = dict->size; 104 length = dict->length;
Value stored to 'length' is never read
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

Value stored to 'length' is never read
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

105 proto = desc; 106 107 /* Field descriptors for 'c_char * n' are be scpecial cased to 108 return a Python string instead of an Array object instance... 109 */ 110 if (PyCArrayTypeObject_Check(proto)) { 111 StgDictObject *adict = PyType_stgdict(proto); 112 StgDictObject *idict; 113 if (adict && adict->proto) { 114 idict = PyType_stgdict(adict->proto); 115 if (!idict) { 116 PyErr_SetString(PyExc_TypeError, 117 "has no _stginfo_"); 118 Py_DECREF(self); 119 return NULL; 120 } 121 if (idict->getfunc == _ctypes_get_fielddesc("c")->getfunc) { 122 struct fielddesc *fd = _ctypes_get_fielddesc("s"); 123 getfunc = fd->getfunc; 124 setfunc = fd->setfunc; 125 } 126 #ifdef CTYPES_UNICODE 127 if (idict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { 128 struct fielddesc *fd = _ctypes_get_fielddesc("U"); 129 getfunc = fd->getfunc; 130 setfunc = fd->setfunc; 131 } 132 #endif 133 } 134 } 135 136 self->setfunc = setfunc; 137 self->getfunc = getfunc; 138 self->index = index; 139 140 Py_INCREF(proto); 141 self->proto = proto; 142 143 switch (fieldtype) { 144 case NEW_BITFIELD: 145 if (big_endian) 146 self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize; 147 else 148 self->size = (bitsize << 16) + *pbitofs; 149 *pbitofs = bitsize; 150 /* fall through */ 151 case NO_BITFIELD: 152 if (pack) 153 align = min(pack, dict->align); 154 else 155 align = dict->align; 156 if (align && *poffset % align) { 157 Py_ssize_t delta = align - (*poffset % align); 158 *psize += delta; 159 *poffset += delta; 160 } 161 162 if (bitsize == 0) 163 self->size = size; 164 *psize += size; 165 166 self->offset = *poffset; 167 *poffset += size; 168 169 *palign = align; 170 break; 171 172 case EXPAND_BITFIELD: 173 *poffset += dict->size - *pfield_size/8; 174 *psize += dict->size - *pfield_size/8; 175 176 *pfield_size = dict->size * 8; 177 178 if (big_endian) 179 self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize; 180 else 181 self->size = (bitsize << 16) + *pbitofs; 182 183 self->offset = *poffset - size; /* poffset is already updated for the NEXT field */ 184 *pbitofs += bitsize; 185 break; 186 187 case CONT_BITFIELD: 188 if (big_endian) 189 self->size = (bitsize << 16) + *pfield_size - *pbitofs - bitsize; 190 else 191 self->size = (bitsize << 16) + *pbitofs; 192 193 self->offset = *poffset - size; /* poffset is already updated for the NEXT field */ 194 *pbitofs += bitsize; 195 break; 196 } 197 198 return (PyObject *)self; 199 } 200 201 static int 202 PyCField_set(CFieldObject *self, PyObject *inst, PyObject *value) 203 { 204 CDataObject *dst; 205 char *ptr; 206 assert(CDataObject_Check(inst)); 207 dst = (CDataObject *)inst; 208 ptr = dst->b_ptr + self->offset; 209 if (value == NULL) { 210 PyErr_SetString(PyExc_TypeError, 211 "can't delete attribute"); 212 return -1; 213 } 214 return PyCData_set(inst, self->proto, self->setfunc, value, 215 self->index, self->size, ptr); 216 } 217 218 static PyObject * 219 PyCField_get(CFieldObject *self, PyObject *inst, PyTypeObject *type) 220 { 221 CDataObject *src; 222 if (inst == NULL) { 223 Py_INCREF(self); 224 return (PyObject *)self; 225 } 226 assert(CDataObject_Check(inst)); 227 src = (CDataObject *)inst; 228 return PyCData_get(self->proto, self->getfunc, inst, 229 self->index, self->size, src->b_ptr + self->offset); 230 } 231 232 static PyObject * 233 PyCField_get_offset(PyObject *self, void *data) 234 { 235 return PyInt_FromSsize_t(((CFieldObject *)self)->offset); 236 } 237 238 static PyObject * 239 PyCField_get_size(PyObject *self, void *data) 240 { 241 return PyInt_FromSsize_t(((CFieldObject *)self)->size); 242 } 243 244 static PyGetSetDef PyCField_getset[] = { 245 { "offset", PyCField_get_offset, NULL, "offset in bytes of this field" }, 246 { "size", PyCField_get_size, NULL, "size in bytes of this field" }, 247 { NULL, NULL, NULL, NULL }, 248 }; 249 250 static int 251 PyCField_traverse(CFieldObject *self, visitproc visit, void *arg) 252 { 253 Py_VISIT(self->proto); 254 return 0; 255 } 256 257 static int 258 PyCField_clear(CFieldObject *self) 259 { 260 Py_CLEAR(self->proto); 261 return 0; 262 } 263 264 static void 265 PyCField_dealloc(PyObject *self) 266 { 267 PyCField_clear((CFieldObject *)self); 268 self->ob_type->tp_free((PyObject *)self); 269 } 270 271 static PyObject * 272 PyCField_repr(CFieldObject *self) 273 { 274 PyObject *result; 275 Py_ssize_t bits = self->size >> 16; 276 Py_ssize_t size = self->size & 0xFFFF; 277 const char *name; 278 279 name = ((PyTypeObject *)self->proto)->tp_name; 280 281 if (bits) 282 result = PyString_FromFormat( 283 #if (PY_VERSION_HEX < 0x02050000) 284 "<Field type=%s, ofs=%d:%d, bits=%d>", 285 #else 286 "<Field type=%s, ofs=%zd:%zd, bits=%zd>", 287 #endif 288 name, self->offset, size, bits); 289 else 290 result = PyString_FromFormat( 291 #if (PY_VERSION_HEX < 0x02050000) 292 "<Field type=%s, ofs=%d, size=%d>", 293 #else 294 "<Field type=%s, ofs=%zd, size=%zd>", 295 #endif 296 name, self->offset, size); 297 return result; 298 } 299 300 PyTypeObject PyCField_Type = { 301 PyVarObject_HEAD_INIT(NULL, 0) 302 "_ctypes.CField", /* tp_name */ 303 sizeof(CFieldObject), /* tp_basicsize */ 304 0, /* tp_itemsize */ 305 PyCField_dealloc, /* tp_dealloc */ 306 0, /* tp_print */ 307 0, /* tp_getattr */ 308 0, /* tp_setattr */ 309 0, /* tp_compare */ 310 (reprfunc)PyCField_repr, /* tp_repr */ 311 0, /* tp_as_number */ 312 0, /* tp_as_sequence */ 313 0, /* tp_as_mapping */ 314 0, /* tp_hash */ 315 0, /* tp_call */ 316 0, /* tp_str */ 317 0, /* tp_getattro */ 318 0, /* tp_setattro */ 319 0, /* tp_as_buffer */ 320 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ 321 "Structure/Union member", /* tp_doc */ 322 (traverseproc)PyCField_traverse, /* tp_traverse */ 323 (inquiry)PyCField_clear, /* tp_clear */ 324 0, /* tp_richcompare */ 325 0, /* tp_weaklistoffset */ 326 0, /* tp_iter */ 327 0, /* tp_iternext */ 328 0, /* tp_methods */ 329 0, /* tp_members */ 330 PyCField_getset, /* tp_getset */ 331 0, /* tp_base */ 332 0, /* tp_dict */ 333 (descrgetfunc)PyCField_get, /* tp_descr_get */ 334 (descrsetfunc)PyCField_set, /* tp_descr_set */ 335 0, /* tp_dictoffset */ 336 0, /* tp_init */ 337 0, /* tp_alloc */ 338 PyCField_new, /* tp_new */ 339 0, /* tp_free */ 340 }; 341 342 343 /******************************************************************/ 344 /* 345 Accessor functions 346 */ 347 348 /* Derived from Modules/structmodule.c: 349 Helper routine to get a Python integer and raise the appropriate error 350 if it isn't one */ 351 352 static int 353 get_long(PyObject *v, long *p) 354 { 355 long x; 356 if (PyFloat_Check(v)) { 357 PyErr_SetString(PyExc_TypeError, 358 "int expected instead of float"); 359 return -1; 360 } 361 x = PyInt_AsUnsignedLongMask(v); 362 if (x == -1 && PyErr_Occurred()) 363 return -1; 364 *p = x; 365 return 0; 366 } 367 368 /* Same, but handling unsigned long */ 369 370 static int 371 get_ulong(PyObject *v, unsigned long *p) 372 { 373 unsigned long x; 374 if (PyFloat_Check(v)) { 375 PyErr_SetString(PyExc_TypeError, 376 "int expected instead of float"); 377 return -1; 378 } 379 x = PyInt_AsUnsignedLongMask(v); 380 if (x == (unsigned long)-1 && PyErr_Occurred()) 381 return -1; 382 *p = x; 383 return 0; 384 } 385 386 #ifdef HAVE_LONG_LONG 387 388 /* Same, but handling native long long. */ 389 390 static int 391 get_longlong(PyObject *v, PY_LONG_LONG *p) 392 { 393 PY_LONG_LONG x; 394 if (PyFloat_Check(v)) { 395 PyErr_SetString(PyExc_TypeError, 396 "int expected instead of float"); 397 return -1; 398 } 399 x = PyInt_AsUnsignedLongLongMask(v); 400 if (x == -1 && PyErr_Occurred()) 401 return -1; 402 *p = x; 403 return 0; 404 } 405 406 /* Same, but handling native unsigned long long. */ 407 408 static int 409 get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p) 410 { 411 unsigned PY_LONG_LONG x; 412 if (PyFloat_Check(v)) { 413 PyErr_SetString(PyExc_TypeError, 414 "int expected instead of float"); 415 return -1; 416 } 417 x = PyInt_AsUnsignedLongLongMask(v); 418 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) 419 return -1; 420 *p = x; 421 return 0; 422 } 423 424 #endif 425 426 /***************************************************************** 427 * Integer fields, with bitfield support 428 */ 429 430 /* how to decode the size field, for integer get/set functions */ 431 #define LOW_BIT(x) ((x) & 0xFFFF) 432 #define NUM_BITS(x) ((x) >> 16) 433 434 /* This seems nore a compiler issue than a Windows/non-Windows one */ 435 #ifdef MS_WIN32 436 # define BIT_MASK(size) ((1 << NUM_BITS(size))-1) 437 #else 438 # define BIT_MASK(size) ((1LL << NUM_BITS(size))-1) 439 #endif 440 441 /* This macro CHANGES the first parameter IN PLACE. For proper sign handling, 442 we must first shift left, then right. 443 */ 444 #define GET_BITFIELD(v, size) \ 445 if (NUM_BITS(size)) { \ 446 v <<= (sizeof(v)*8 - LOW_BIT(size) - NUM_BITS(size)); \ 447 v >>= (sizeof(v)*8 - NUM_BITS(size)); \ 448 } 449 450 /* This macro RETURNS the first parameter with the bit field CHANGED. */ 451 #define SET(x, v, size) \ 452 (NUM_BITS(size) ? \ 453 ( ( x & ~(BIT_MASK(size) << LOW_BIT(size)) ) | ( (v & BIT_MASK(size)) << LOW_BIT(size) ) ) \ 454 : v) 455 456 /* byte swapping macros */ 457 #define SWAP_2(v) \ 458 ( ( (v >> 8) & 0x00FF) | \ 459 ( (v << 8) & 0xFF00) ) 460 461 #define SWAP_4(v) \ 462 ( ( (v & 0x000000FF) << 24 ) | \ 463 ( (v & 0x0000FF00) << 8 ) | \ 464 ( (v & 0x00FF0000) >> 8 ) | \ 465 ( ((v >> 24) & 0xFF)) ) 466 467 #ifdef _MSC_VER 468 #define SWAP_8(v) \ 469 ( ( (v & 0x00000000000000FFL) << 56 ) | \ 470 ( (v & 0x000000000000FF00L) << 40 ) | \ 471 ( (v & 0x0000000000FF0000L) << 24 ) | \ 472 ( (v & 0x00000000FF000000L) << 8 ) | \ 473 ( (v & 0x000000FF00000000L) >> 8 ) | \ 474 ( (v & 0x0000FF0000000000L) >> 24 ) | \ 475 ( (v & 0x00FF000000000000L) >> 40 ) | \ 476 ( ((v >> 56) & 0xFF)) ) 477 #else 478 #define SWAP_8(v) \ 479 ( ( (v & 0x00000000000000FFLL) << 56 ) | \ 480 ( (v & 0x000000000000FF00LL) << 40 ) | \ 481 ( (v & 0x0000000000FF0000LL) << 24 ) | \ 482 ( (v & 0x00000000FF000000LL) << 8 ) | \ 483 ( (v & 0x000000FF00000000LL) >> 8 ) | \ 484 ( (v & 0x0000FF0000000000LL) >> 24 ) | \ 485 ( (v & 0x00FF000000000000LL) >> 40 ) | \ 486 ( ((v >> 56) & 0xFF)) ) 487 #endif 488 489 #define SWAP_INT SWAP_4 490 491 #if SIZEOF_LONG == 4 492 # define SWAP_LONG SWAP_4 493 #elif SIZEOF_LONG == 8 494 # define SWAP_LONG SWAP_8 495 #endif 496 /***************************************************************** 497 * The setter methods return an object which must be kept alive, to keep the 498 * data valid which has been stored in the memory block. The ctypes object 499 * instance inserts this object into its 'b_objects' list. 500 * 501 * For simple Python types like integers or characters, there is nothing that 502 * has to been kept alive, so Py_None is returned in these cases. But this 503 * makes inspecting the 'b_objects' list, which is accessible from Python for 504 * debugging, less useful. 505 * 506 * So, defining the _CTYPES_DEBUG_KEEP symbol returns the original value 507 * instead of Py_None. 508 */ 509 510 #ifdef _CTYPES_DEBUG_KEEP 511 #define _RET(x) Py_INCREF(x); return x 512 #else 513 #define _RET(X) Py_INCREF(Py_None); return Py_None 514 #endif 515 516 /***************************************************************** 517 * integer accessor methods, supporting bit fields 518 */ 519 520 static PyObject * 521 b_set(void *ptr, PyObject *value, Py_ssize_t size) 522 { 523 long val; 524 if (get_long(value, &val) < 0) 525 return NULL; 526 *(signed char *)ptr = (signed char)SET(*(signed char *)ptr, (signed char)val, size); 527 _RET(value); 528 } 529 530 531 static PyObject * 532 b_get(void *ptr, Py_ssize_t size) 533 { 534 signed char val = *(signed char *)ptr; 535 GET_BITFIELD(val, size); 536 return PyInt_FromLong(val); 537 } 538 539 static PyObject * 540 B_set(void *ptr, PyObject *value, Py_ssize_t size) 541 { 542 unsigned long val; 543 if (get_ulong(value, &val) < 0) 544 return NULL; 545 *(unsigned char *)ptr = (unsigned char)SET(*(unsigned char*)ptr, 546 (unsigned short)val, size); 547 _RET(value); 548 } 549 550 551 static PyObject * 552 B_get(void *ptr, Py_ssize_t size) 553 { 554 unsigned char val = *(unsigned char *)ptr; 555 GET_BITFIELD(val, size); 556 return PyInt_FromLong(val); 557 } 558 559 static PyObject * 560 h_set(void *ptr, PyObject *value, Py_ssize_t size) 561 { 562 long val; 563 short x; 564 if (get_long(value, &val) < 0) 565 return NULL; 566 memcpy(&x, ptr, sizeof(x)); 567 x = SET(x, (short)val, size); 568 memcpy(ptr, &x, sizeof(x)); 569 _RET(value); 570 } 571 572 573 static PyObject * 574 h_set_sw(void *ptr, PyObject *value, Py_ssize_t size) 575 { 576 long val; 577 short field; 578 if (get_long(value, &val) < 0) 579 return NULL; 580 memcpy(&field, ptr, sizeof(field)); 581 field = SWAP_2(field); 582 field = SET(field, (short)val, size); 583 field = SWAP_2(field); 584 memcpy(ptr, &field, sizeof(field)); 585 _RET(value); 586 } 587 588 static PyObject * 589 h_get(void *ptr, Py_ssize_t size) 590 { 591 short val; 592 memcpy(&val, ptr, sizeof(val)); 593 GET_BITFIELD(val, size); 594 return PyInt_FromLong((long)val); 595 } 596 597 static PyObject * 598 h_get_sw(void *ptr, Py_ssize_t size) 599 { 600 short val; 601 memcpy(&val, ptr, sizeof(val)); 602 val = SWAP_2(val); 603 GET_BITFIELD(val, size); 604 return PyInt_FromLong(val); 605 } 606 607 static PyObject * 608 H_set(void *ptr, PyObject *value, Py_ssize_t size) 609 { 610 unsigned long val; 611 unsigned short x; 612 if (get_ulong(value, &val) < 0) 613 return NULL; 614 memcpy(&x, ptr, sizeof(x)); 615 x = SET(x, (unsigned short)val, size); 616 memcpy(ptr, &x, sizeof(x)); 617 _RET(value); 618 } 619 620 static PyObject * 621 H_set_sw(void *ptr, PyObject *value, Py_ssize_t size) 622 { 623 unsigned long val; 624 unsigned short field; 625 if (get_ulong(value, &val) < 0) 626 return NULL; 627 memcpy(&field, ptr, sizeof(field)); 628 field = SWAP_2(field); 629 field = SET(field, (unsigned short)val, size); 630 field = SWAP_2(field); 631 memcpy(ptr, &field, sizeof(field)); 632 _RET(value); 633 } 634 635 636 static PyObject * 637 H_get(void *ptr, Py_ssize_t size) 638 { 639 unsigned short val; 640 memcpy(&val, ptr, sizeof(val)); 641 GET_BITFIELD(val, size); 642 return PyInt_FromLong(val); 643 } 644 645 static PyObject * 646 H_get_sw(void *ptr, Py_ssize_t size) 647 { 648 unsigned short val; 649 memcpy(&val, ptr, sizeof(val)); 650 val = SWAP_2(val); 651 GET_BITFIELD(val, size); 652 return PyInt_FromLong(val); 653 } 654 655 static PyObject * 656 i_set(void *ptr, PyObject *value, Py_ssize_t size) 657 { 658 long val; 659 int x; 660 if (get_long(value, &val) < 0) 661 return NULL; 662 memcpy(&x, ptr, sizeof(x)); 663 x = SET(x, (int)val, size); 664 memcpy(ptr, &x, sizeof(x)); 665 _RET(value); 666 } 667 668 static PyObject * 669 i_set_sw(void *ptr, PyObject *value, Py_ssize_t size) 670 { 671 long val; 672 int field; 673 if (get_long(value, &val) < 0) 674 return NULL; 675 memcpy(&field, ptr, sizeof(field)); 676 field = SWAP_INT(field); 677 field = SET(field, (int)val, size); 678 field = SWAP_INT(field); 679 memcpy(ptr, &field, sizeof(field)); 680 _RET(value); 681 } 682 683 684 static PyObject * 685 i_get(void *ptr, Py_ssize_t size) 686 { 687 int val; 688 memcpy(&val, ptr, sizeof(val)); 689 GET_BITFIELD(val, size); 690 return PyInt_FromLong(val); 691 } 692 693 static PyObject * 694 i_get_sw(void *ptr, Py_ssize_t size) 695 { 696 int val; 697 memcpy(&val, ptr, sizeof(val)); 698 val = SWAP_INT(val); 699 GET_BITFIELD(val, size); 700 return PyInt_FromLong(val); 701 } 702 703 #ifdef MS_WIN32 704 /* short BOOL - VARIANT_BOOL */ 705 static PyObject * 706 vBOOL_set(void *ptr, PyObject *value, Py_ssize_t size) 707 { 708 switch (PyObject_IsTrue(value)) { 709 case -1: 710 return NULL; 711 case 0: 712 *(short int *)ptr = VARIANT_FALSE; 713 _RET(value); 714 default: 715 *(short int *)ptr = VARIANT_TRUE; 716 _RET(value); 717 } 718 } 719 720 static PyObject * 721 vBOOL_get(void *ptr, Py_ssize_t size) 722 { 723 return PyBool_FromLong((long)*(short int *)ptr); 724 } 725 #endif 726 727 #ifdef HAVE_C99_BOOL 728 #define BOOL_TYPE _Bool 729 #else 730 #define BOOL_TYPE char 731 #undef SIZEOF__BOOL 732 #define SIZEOF__BOOL 1 733 #endif 734 735 static PyObject * 736 bool_set(void *ptr, PyObject *value, Py_ssize_t size) 737 { 738 switch (PyObject_IsTrue(value)) { 739 case -1: 740 return NULL; 741 case 0: 742 *(BOOL_TYPE *)ptr = 0; 743 _RET(value); 744 default: 745 *(BOOL_TYPE *)ptr = 1; 746 _RET(value); 747 } 748 } 749 750 static PyObject * 751 bool_get(void *ptr, Py_ssize_t size) 752 { 753 return PyBool_FromLong((long)*(BOOL_TYPE *)ptr); 754 } 755 756 static PyObject * 757 I_set(void *ptr, PyObject *value, Py_ssize_t size) 758 { 759 unsigned long val; 760 unsigned int x; 761 if (get_ulong(value, &val) < 0) 762 return NULL; 763 memcpy(&x, ptr, sizeof(x)); 764 x = SET(x, (unsigned int)val, size); 765 memcpy(ptr, &x, sizeof(x)); 766 _RET(value); 767 } 768 769 static PyObject * 770 I_set_sw(void *ptr, PyObject *value, Py_ssize_t size) 771 { 772 unsigned long val; 773 unsigned int field; 774 if (get_ulong(value, &val) < 0) 775 return NULL; 776 memcpy(&field, ptr, sizeof(field)); 777 field = (unsigned int)SET(field, (unsigned int)val, size); 778 field = SWAP_INT(field); 779 memcpy(ptr, &field, sizeof(field)); 780 _RET(value); 781 } 782 783 784 static PyObject * 785 I_get(void *ptr, Py_ssize_t size) 786 { 787 unsigned int val; 788 memcpy(&val, ptr, sizeof(val)); 789 GET_BITFIELD(val, size); 790 return PyLong_FromUnsignedLong(val); 791 } 792 793 static PyObject * 794 I_get_sw(void *ptr, Py_ssize_t size) 795 { 796 unsigned int val; 797 memcpy(&val, ptr, sizeof(val)); 798 val = SWAP_INT(val); 799 GET_BITFIELD(val, size); 800 return PyLong_FromUnsignedLong(val); 801 } 802 803 static PyObject * 804 l_set(void *ptr, PyObject *value, Py_ssize_t size) 805 { 806 long val; 807 long x; 808 if (get_long(value, &val) < 0) 809 return NULL; 810 memcpy(&x, ptr, sizeof(x)); 811 x = SET(x, val, size); 812 memcpy(ptr, &x, sizeof(x)); 813 _RET(value); 814 } 815 816 static PyObject * 817 l_set_sw(void *ptr, PyObject *value, Py_ssize_t size) 818 { 819 long val; 820 long field; 821 if (get_long(value, &val) < 0) 822 return NULL; 823 memcpy(&field, ptr, sizeof(field)); 824 field = SWAP_LONG(field); 825 field = (long)SET(field, val, size); 826 field = SWAP_LONG(field); 827 memcpy(ptr, &field, sizeof(field)); 828 _RET(value); 829 } 830 831 832 static PyObject * 833 l_get(void *ptr, Py_ssize_t size) 834 { 835 long val; 836 memcpy(&val, ptr, sizeof(val)); 837 GET_BITFIELD(val, size); 838 return PyInt_FromLong(val); 839 } 840 841 static PyObject * 842 l_get_sw(void *ptr, Py_ssize_t size) 843 { 844 long val; 845 memcpy(&val, ptr, sizeof(val)); 846 val = SWAP_LONG(val); 847 GET_BITFIELD(val, size); 848 return PyInt_FromLong(val); 849 } 850 851 static PyObject * 852 L_set(void *ptr, PyObject *value, Py_ssize_t size) 853 { 854 unsigned long val; 855 unsigned long x; 856 if (get_ulong(value, &val) < 0) 857 return NULL; 858 memcpy(&x, ptr, sizeof(x)); 859 x = SET(x, val, size); 860 memcpy(ptr, &x, sizeof(x)); 861 _RET(value); 862 } 863 864 static PyObject * 865 L_set_sw(void *ptr, PyObject *value, Py_ssize_t size) 866 { 867 unsigned long val; 868 unsigned long field; 869 if (get_ulong(value, &val) < 0) 870 return NULL; 871 memcpy(&field, ptr, sizeof(field)); 872 field = SWAP_LONG(field); 873 field = (unsigned long)SET(field, val, size); 874 field = SWAP_LONG(field); 875 memcpy(ptr, &field, sizeof(field)); 876 _RET(value); 877 } 878 879 880 static PyObject * 881 L_get(void *ptr, Py_ssize_t size) 882 { 883 unsigned long val; 884 memcpy(&val, ptr, sizeof(val)); 885 GET_BITFIELD(val, size); 886 return PyLong_FromUnsignedLong(val); 887 } 888 889 static PyObject * 890 L_get_sw(void *ptr, Py_ssize_t size) 891 { 892 unsigned long val; 893 memcpy(&val, ptr, sizeof(val)); 894 val = SWAP_LONG(val); 895 GET_BITFIELD(val, size); 896 return PyLong_FromUnsignedLong(val); 897 } 898 899 #ifdef HAVE_LONG_LONG 900 static PyObject * 901 q_set(void *ptr, PyObject *value, Py_ssize_t size) 902 { 903 PY_LONG_LONG val; 904 PY_LONG_LONG x; 905 if (get_longlong(value, &val) < 0) 906 return NULL; 907 memcpy(&x, ptr, sizeof(x)); 908 x = SET(x, val, size); 909 memcpy(ptr, &x, sizeof(x)); 910 _RET(value); 911 } 912 913 static PyObject * 914 q_set_sw(void *ptr, PyObject *value, Py_ssize_t size) 915 { 916 PY_LONG_LONG val; 917 PY_LONG_LONG field; 918 if (get_longlong(value, &val) < 0) 919 return NULL; 920 memcpy(&field, ptr, sizeof(field)); 921 field = SWAP_8(field); 922 field = (PY_LONG_LONG)SET(field, val, size); 923 field = SWAP_8(field); 924 memcpy(ptr, &field, sizeof(field)); 925 _RET(value); 926 } 927 928 static PyObject * 929 q_get(void *ptr, Py_ssize_t size) 930 { 931 PY_LONG_LONG val; 932 memcpy(&val, ptr, sizeof(val)); 933 GET_BITFIELD(val, size); 934 return PyLong_FromLongLong(val); 935 } 936 937 static PyObject * 938 q_get_sw(void *ptr, Py_ssize_t size) 939 { 940 PY_LONG_LONG val; 941 memcpy(&val, ptr, sizeof(val)); 942 val = SWAP_8(val); 943 GET_BITFIELD(val, size); 944 return PyLong_FromLongLong(val); 945 } 946 947 static PyObject * 948 Q_set(void *ptr, PyObject *value, Py_ssize_t size) 949 { 950 unsigned PY_LONG_LONG val; 951 unsigned PY_LONG_LONG x; 952 if (get_ulonglong(value, &val) < 0) 953 return NULL; 954 memcpy(&x, ptr, sizeof(x)); 955 x = SET(x, val, size); 956 memcpy(ptr, &x, sizeof(x)); 957 _RET(value); 958 } 959 960 static PyObject * 961 Q_set_sw(void *ptr, PyObject *value, Py_ssize_t size) 962 { 963 unsigned PY_LONG_LONG val; 964 unsigned PY_LONG_LONG field; 965 if (get_ulonglong(value, &val) < 0) 966 return NULL; 967 memcpy(&field, ptr, sizeof(field)); 968 field = SWAP_8(field); 969 field = (unsigned PY_LONG_LONG)SET(field, val, size); 970 field = SWAP_8(field); 971 memcpy(ptr, &field, sizeof(field)); 972 _RET(value); 973 } 974 975 static PyObject * 976 Q_get(void *ptr, Py_ssize_t size) 977 { 978 unsigned PY_LONG_LONG val; 979 memcpy(&val, ptr, sizeof(val)); 980 GET_BITFIELD(val, size); 981 return PyLong_FromUnsignedLongLong(val); 982 } 983 984 static PyObject * 985 Q_get_sw(void *ptr, Py_ssize_t size) 986 { 987 unsigned PY_LONG_LONG val; 988 memcpy(&val, ptr, sizeof(val)); 989 val = SWAP_8(val); 990 GET_BITFIELD(val, size); 991 return PyLong_FromUnsignedLongLong(val); 992 } 993 #endif 994 995 /***************************************************************** 996 * non-integer accessor methods, not supporting bit fields 997 */ 998 999 1000 static PyObject * 1001 g_set(void *ptr, PyObject *value, Py_ssize_t size) 1002 { 1003 long double x; 1004 1005 x = PyFloat_AsDouble(value); 1006 if (x == -1 && PyErr_Occurred()) { 1007 PyErr_Format(PyExc_TypeError, 1008 " float expected instead of %s instance", 1009 value->ob_type->tp_name); 1010 return NULL; 1011 } 1012 memcpy(ptr, &x, sizeof(long double)); 1013 _RET(value); 1014 } 1015 1016 static PyObject * 1017 g_get(void *ptr, Py_ssize_t size) 1018 { 1019 long double val; 1020 memcpy(&val, ptr, sizeof(long double)); 1021 return PyFloat_FromDouble(val); 1022 } 1023 1024 static PyObject * 1025 d_set(void *ptr, PyObject *value, Py_ssize_t size) 1026 { 1027 double x; 1028 1029 x = PyFloat_AsDouble(value); 1030 if (x == -1 && PyErr_Occurred()) { 1031 PyErr_Format(PyExc_TypeError, 1032 " float expected instead of %s instance", 1033 value->ob_type->tp_name); 1034 return NULL; 1035 } 1036 memcpy(ptr, &x, sizeof(double)); 1037 _RET(value); 1038 } 1039 1040 static PyObject * 1041 d_get(void *ptr, Py_ssize_t size) 1042 { 1043 double val; 1044 memcpy(&val, ptr, sizeof(val)); 1045 return PyFloat_FromDouble(val); 1046 } 1047 1048 static PyObject * 1049 d_set_sw(void *ptr, PyObject *value, Py_ssize_t size) 1050 { 1051 double x; 1052 1053 x = PyFloat_AsDouble(value); 1054 if (x == -1 && PyErr_Occurred()) { 1055 PyErr_Format(PyExc_TypeError, 1056 " float expected instead of %s instance", 1057 value->ob_type->tp_name); 1058 return NULL; 1059 } 1060 #ifdef WORDS_BIGENDIAN 1061 if (_PyFloat_Pack8(x, (unsigned char *)ptr, 1)) 1062 return NULL; 1063 #else 1064 if (_PyFloat_Pack8(x, (unsigned char *)ptr, 0)) 1065 return NULL; 1066 #endif 1067 _RET(value); 1068 } 1069 1070 static PyObject * 1071 d_get_sw(void *ptr, Py_ssize_t size) 1072 { 1073 #ifdef WORDS_BIGENDIAN 1074 return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 1)); 1075 #else 1076 return PyFloat_FromDouble(_PyFloat_Unpack8(ptr, 0)); 1077 #endif 1078 } 1079 1080 static PyObject * 1081 f_set(void *ptr, PyObject *value, Py_ssize_t size) 1082 { 1083 float x; 1084 1085 x = (float)PyFloat_AsDouble(value); 1086 if (x == -1 && PyErr_Occurred()) { 1087 PyErr_Format(PyExc_TypeError, 1088 " float expected instead of %s instance", 1089 value->ob_type->tp_name); 1090 return NULL; 1091 } 1092 memcpy(ptr, &x, sizeof(x)); 1093 _RET(value); 1094 } 1095 1096 static PyObject * 1097 f_get(void *ptr, Py_ssize_t size) 1098 { 1099 float val; 1100 memcpy(&val, ptr, sizeof(val)); 1101 return PyFloat_FromDouble(val); 1102 } 1103 1104 static PyObject * 1105 f_set_sw(void *ptr, PyObject *value, Py_ssize_t size) 1106 { 1107 float x; 1108 1109 x = (float)PyFloat_AsDouble(value); 1110 if (x == -1 && PyErr_Occurred()) { 1111 PyErr_Format(PyExc_TypeError, 1112 " float expected instead of %s instance", 1113 value->ob_type->tp_name); 1114 return NULL; 1115 } 1116 #ifdef WORDS_BIGENDIAN 1117 if (_PyFloat_Pack4(x, (unsigned char *)ptr, 1)) 1118 return NULL; 1119 #else 1120 if (_PyFloat_Pack4(x, (unsigned char *)ptr, 0)) 1121 return NULL; 1122 #endif 1123 _RET(value); 1124 } 1125 1126 static PyObject * 1127 f_get_sw(void *ptr, Py_ssize_t size) 1128 { 1129 #ifdef WORDS_BIGENDIAN 1130 return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 1)); 1131 #else 1132 return PyFloat_FromDouble(_PyFloat_Unpack4(ptr, 0)); 1133 #endif 1134 } 1135 1136 /* 1137 py_object refcounts: 1138 1139 1. If we have a py_object instance, O_get must Py_INCREF the returned 1140 object, of course. If O_get is called from a function result, no py_object 1141 instance is created - so callproc.c::GetResult has to call Py_DECREF. 1142 1143 2. The memory block in py_object owns a refcount. So, py_object must call 1144 Py_DECREF on destruction. Maybe only when b_needsfree is non-zero. 1145 */ 1146 static PyObject * 1147 O_get(void *ptr, Py_ssize_t size) 1148 { 1149 PyObject *ob = *(PyObject **)ptr; 1150 if (ob == NULL) { 1151 if (!PyErr_Occurred()) 1152 /* Set an error if not yet set */ 1153 PyErr_SetString(PyExc_ValueError, 1154 "PyObject is NULL"); 1155 return NULL; 1156 } 1157 Py_INCREF(ob); 1158 return ob; 1159 } 1160 1161 static PyObject * 1162 O_set(void *ptr, PyObject *value, Py_ssize_t size) 1163 { 1164 /* Hm, does the memory block need it's own refcount or not? */ 1165 *(PyObject **)ptr = value; 1166 Py_INCREF(value); 1167 return value; 1168 } 1169 1170 1171 static PyObject * 1172 c_set(void *ptr, PyObject *value, Py_ssize_t size) 1173 { 1174 if (!PyString_Check(value) || (1 != PyString_Size(value))) { 1175 PyErr_Format(PyExc_TypeError, 1176 "one character string expected"); 1177 return NULL; 1178 } 1179 *(char *)ptr = PyString_AS_STRING(value)[0]; 1180 _RET(value); 1181 } 1182 1183 1184 static PyObject * 1185 c_get(void *ptr, Py_ssize_t size) 1186 { 1187 return PyString_FromStringAndSize((char *)ptr, 1); 1188 } 1189 1190 #ifdef CTYPES_UNICODE 1191 /* u - a single wchar_t character */ 1192 static PyObject * 1193 u_set(void *ptr, PyObject *value, Py_ssize_t size) 1194 { 1195 Py_ssize_t len; 1196 1197 if (PyString_Check(value)) { 1198 value = PyUnicode_FromEncodedObject(value, 1199 _ctypes_conversion_encoding, 1200 _ctypes_conversion_errors); 1201 if (!value) 1202 return NULL; 1203 } else if (!PyUnicode_Check(value)) { 1204 PyErr_Format(PyExc_TypeError, 1205 "unicode string expected instead of %s instance", 1206 value->ob_type->tp_name); 1207 return NULL; 1208 } else 1209 Py_INCREF(value); 1210 1211 len = PyUnicode_GET_SIZE(value); 1212 if (len != 1) { 1213 Py_DECREF(value); 1214 PyErr_SetString(PyExc_TypeError, 1215 "one character unicode string expected"); 1216 return NULL; 1217 } 1218 1219 *(wchar_t *)ptr = PyUnicode_AS_UNICODE(value)[0]; 1220 Py_DECREF(value); 1221 1222 _RET(value); 1223 } 1224 1225 1226 static PyObject * 1227 u_get(void *ptr, Py_ssize_t size) 1228 { 1229 return PyUnicode_FromWideChar((wchar_t *)ptr, 1); 1230 } 1231 1232 /* U - a unicode string */ 1233 static PyObject * 1234 U_get(void *ptr, Py_ssize_t size) 1235 { 1236 PyObject *result; 1237 Py_ssize_t len; 1238 Py_UNICODE *p; 1239 1240 size /= sizeof(wchar_t); /* we count character units here, not bytes */ 1241 1242 result = PyUnicode_FromWideChar((wchar_t *)ptr, size); 1243 if (!result) 1244 return NULL; 1245 /* We need 'result' to be able to count the characters with wcslen, 1246 since ptr may not be NUL terminated. If the length is smaller (if 1247 it was actually NUL terminated, we construct a new one and throw 1248 away the result. 1249 */ 1250 /* chop off at the first NUL character, if any. */ 1251 p = PyUnicode_AS_UNICODE(result); 1252 for (len = 0; len < size; ++len) 1253 if (!p[len]) 1254 break; 1255 1256 if (len < size) { 1257 PyObject *ob = PyUnicode_FromWideChar((wchar_t *)ptr, len); 1258 Py_DECREF(result); 1259 return ob; 1260 } 1261 return result; 1262 } 1263 1264 static PyObject * 1265 U_set(void *ptr, PyObject *value, Py_ssize_t length) 1266 { 1267 Py_ssize_t size; 1268 1269 /* It's easier to calculate in characters than in bytes */ 1270 length /= sizeof(wchar_t); 1271 1272 if (PyString_Check(value)) { 1273 value = PyUnicode_FromEncodedObject(value, 1274 _ctypes_conversion_encoding, 1275 _ctypes_conversion_errors); 1276 if (!value) 1277 return NULL; 1278 } else if (!PyUnicode_Check(value)) { 1279 PyErr_Format(PyExc_TypeError, 1280 "unicode string expected instead of %s instance", 1281 value->ob_type->tp_name); 1282 return NULL; 1283 } else 1284 Py_INCREF(value); 1285 size = PyUnicode_GET_SIZE(value); 1286 if (size > length) { 1287 PyErr_Format(PyExc_ValueError, 1288 #if (PY_VERSION_HEX < 0x02050000) 1289 "string too long (%d, maximum length %d)", 1290 #else 1291 "string too long (%zd, maximum length %zd)", 1292 #endif 1293 size, length); 1294 Py_DECREF(value); 1295 return NULL; 1296 } else if (size < length-1) 1297 /* copy terminating NUL character if there is space */ 1298 size += 1; 1299 PyUnicode_AsWideChar((PyUnicodeObject *)value, (wchar_t *)ptr, size); 1300 return value; 1301 } 1302 1303 #endif 1304 1305 static PyObject * 1306 s_get(void *ptr, Py_ssize_t size) 1307 { 1308 PyObject *result; 1309 size_t slen; 1310 1311 result = PyString_FromString((char *)ptr); 1312 if (!result) 1313 return NULL; 1314 /* chop off at the first NUL character, if any. 1315 * On error, result will be deallocated and set to NULL. 1316 */ 1317 slen = strlen(PyString_AS_STRING(result)); 1318 size = min(size, (Py_ssize_t)slen); 1319 if (result->ob_refcnt == 1) { 1320 /* shorten the result */ 1321 _PyString_Resize(&result, size); 1322 return result; 1323 } else 1324 /* cannot shorten the result */ 1325 return PyString_FromStringAndSize(ptr, size); 1326 } 1327 1328 static PyObject * 1329 s_set(void *ptr, PyObject *value, Py_ssize_t length) 1330 { 1331 char *data; 1332 Py_ssize_t size; 1333 1334 data = PyString_AsString(value); 1335 if (!data) 1336 return NULL; 1337 size = strlen(data); 1338 if (size < length) { 1339 /* This will copy the leading NUL character 1340 * if there is space for it. 1341 */ 1342 ++size; 1343 } else if (size > length) { 1344 PyErr_Format(PyExc_ValueError, 1345 #if (PY_VERSION_HEX < 0x02050000) 1346 "string too long (%d, maximum length %d)", 1347 #else 1348 "string too long (%zd, maximum length %zd)", 1349 #endif 1350 size, length); 1351 return NULL; 1352 } 1353 /* Also copy the terminating NUL character if there is space */ 1354 memcpy((char *)ptr, data, size); 1355 _RET(value); 1356 } 1357 1358 static PyObject * 1359 z_set(void *ptr, PyObject *value, Py_ssize_t size) 1360 { 1361 if (value == Py_None) { 1362 *(char **)ptr = NULL; 1363 Py_INCREF(value); 1364 return value; 1365 } 1366 if (PyString_Check(value)) { 1367 *(char **)ptr = PyString_AS_STRING(value); 1368 Py_INCREF(value); 1369 return value; 1370 } else if (PyUnicode_Check(value)) { 1371 PyObject *str = PyUnicode_AsEncodedString(value, 1372 _ctypes_conversion_encoding, 1373 _ctypes_conversion_errors); 1374 if (str == NULL) 1375 return NULL; 1376 *(char **)ptr = PyString_AS_STRING(str); 1377 return str; 1378 } else if (PyInt_Check(value) || PyLong_Check(value)) { 1379 #if SIZEOF_VOID_P == SIZEOF_LONG_LONG 1380 *(char **)ptr = (char *)PyInt_AsUnsignedLongLongMask(value); 1381 #else 1382 *(char **)ptr = (char *)PyInt_AsUnsignedLongMask(value); 1383 #endif 1384 _RET(value); 1385 } 1386 PyErr_Format(PyExc_TypeError, 1387 "string or integer address expected instead of %s instance", 1388 value->ob_type->tp_name); 1389 return NULL; 1390 } 1391 1392 static PyObject * 1393 z_get(void *ptr, Py_ssize_t size) 1394 { 1395 /* XXX What about invalid pointers ??? */ 1396 if (*(void **)ptr) { 1397 #if defined(MS_WIN32) && !defined(_WIN32_WCE) 1398 if (IsBadStringPtrA(*(char **)ptr, -1)) { 1399 PyErr_Format(PyExc_ValueError, 1400 "invalid string pointer %p", 1401 *(char **)ptr); 1402 return NULL; 1403 } 1404 #endif 1405 return PyString_FromString(*(char **)ptr); 1406 } else { 1407 Py_INCREF(Py_None); 1408 return Py_None; 1409 } 1410 } 1411 1412 #ifdef CTYPES_UNICODE 1413 static PyObject * 1414 Z_set(void *ptr, PyObject *value, Py_ssize_t size) 1415 { 1416 if (value == Py_None) { 1417 *(wchar_t **)ptr = NULL; 1418 Py_INCREF(value); 1419 return value; 1420 } 1421 if (PyString_Check(value)) { 1422 value = PyUnicode_FromEncodedObject(value, 1423 _ctypes_conversion_encoding, 1424 _ctypes_conversion_errors); 1425 if (!value) 1426 return NULL; 1427 } else if (PyInt_Check(value) || PyLong_Check(value)) { 1428 #if SIZEOF_VOID_P == SIZEOF_LONG_LONG 1429 *(wchar_t **)ptr = (wchar_t *)PyInt_AsUnsignedLongLongMask(value); 1430 #else 1431 *(wchar_t **)ptr = (wchar_t *)PyInt_AsUnsignedLongMask(value); 1432 #endif 1433 Py_INCREF(Py_None); 1434 return Py_None; 1435 } else if (!PyUnicode_Check(value)) { 1436 PyErr_Format(PyExc_TypeError, 1437 "unicode string or integer address expected instead of %s instance", 1438 value->ob_type->tp_name); 1439 return NULL; 1440 } else 1441 Py_INCREF(value); 1442 #ifdef HAVE_USABLE_WCHAR_T 1443 /* HAVE_USABLE_WCHAR_T means that Py_UNICODE and wchar_t is the same 1444 type. So we can copy directly. Hm, are unicode objects always NUL 1445 terminated in Python, internally? 1446 */ 1447 *(wchar_t **)ptr = PyUnicode_AS_UNICODE(value); 1448 return value; 1449 #else 1450 { 1451 /* We must create a wchar_t* buffer from the unicode object, 1452 and keep it alive */ 1453 PyObject *keep; 1454 wchar_t *buffer; 1455 1456 int size = PyUnicode_GET_SIZE(value); 1457 size += 1; /* terminating NUL */ 1458 size *= sizeof(wchar_t); 1459 buffer = (wchar_t *)PyMem_Malloc(size); 1460 if (!buffer) { 1461 Py_DECREF(value); 1462 return PyErr_NoMemory(); 1463 } 1464 memset(buffer, 0, size); 1465 keep = CAPSULE_NEW(buffer, CTYPES_CAPSULE_WCHAR_T); 1466 if (!keep) { 1467 Py_DECREF(value); 1468 PyMem_Free(buffer); 1469 return NULL; 1470 } 1471 *(wchar_t **)ptr = (wchar_t *)buffer; 1472 if (-1 == PyUnicode_AsWideChar((PyUnicodeObject *)value, 1473 buffer, PyUnicode_GET_SIZE(value))) { 1474 Py_DECREF(value); 1475 Py_DECREF(keep); 1476 return NULL; 1477 } 1478 Py_DECREF(value); 1479 return keep; 1480 } 1481 #endif 1482 } 1483 1484 static PyObject * 1485 Z_get(void *ptr, Py_ssize_t size) 1486 { 1487 wchar_t *p; 1488 p = *(wchar_t **)ptr; 1489 if (p) { 1490 #if defined(MS_WIN32) && !defined(_WIN32_WCE) 1491 if (IsBadStringPtrW(*(wchar_t **)ptr, -1)) { 1492 PyErr_Format(PyExc_ValueError, 1493 "invalid string pointer %p", 1494 *(wchar_t **)ptr); 1495 return NULL; 1496 } 1497 #endif 1498 return PyUnicode_FromWideChar(p, wcslen(p)); 1499 } else { 1500 Py_INCREF(Py_None); 1501 return Py_None; 1502 } 1503 } 1504 #endif 1505 1506 #ifdef MS_WIN32 1507 static PyObject * 1508 BSTR_set(void *ptr, PyObject *value, Py_ssize_t size) 1509 { 1510 BSTR bstr; 1511 1512 /* convert value into a PyUnicodeObject or NULL */ 1513 if (Py_None == value) { 1514 value = NULL; 1515 } else if (PyString_Check(value)) { 1516 value = PyUnicode_FromEncodedObject(value, 1517 _ctypes_conversion_encoding, 1518 _ctypes_conversion_errors); 1519 if (!value) 1520 return NULL; 1521 } else if (PyUnicode_Check(value)) { 1522 Py_INCREF(value); /* for the descref below */ 1523 } else { 1524 PyErr_Format(PyExc_TypeError, 1525 "unicode string expected instead of %s instance", 1526 value->ob_type->tp_name); 1527 return NULL; 1528 } 1529 1530 /* create a BSTR from value */ 1531 if (value) { 1532 Py_ssize_t size = PyUnicode_GET_SIZE(value); 1533 if ((unsigned) size != size) { 1534 PyErr_SetString(PyExc_ValueError, "String too long for BSTR"); 1535 return NULL; 1536 } 1537 bstr = SysAllocStringLen(PyUnicode_AS_UNICODE(value), 1538 (unsigned)size); 1539 Py_DECREF(value); 1540 } else 1541 bstr = NULL; 1542 1543 /* free the previous contents, if any */ 1544 if (*(BSTR *)ptr) 1545 SysFreeString(*(BSTR *)ptr); 1546 1547 /* and store it */ 1548 *(BSTR *)ptr = bstr; 1549 1550 /* We don't need to keep any other object */ 1551 _RET(value); 1552 } 1553 1554 1555 static PyObject * 1556 BSTR_get(void *ptr, Py_ssize_t size) 1557 { 1558 BSTR p; 1559 p = *(BSTR *)ptr; 1560 if (p) 1561 return PyUnicode_FromWideChar(p, SysStringLen(p)); 1562 else { 1563 /* Hm, it seems NULL pointer and zero length string are the 1564 same in BSTR, see Don Box, p 81 1565 */ 1566 Py_INCREF(Py_None); 1567 return Py_None; 1568 } 1569 } 1570 #endif 1571 1572 static PyObject * 1573 P_set(void *ptr, PyObject *value, Py_ssize_t size) 1574 { 1575 void *v; 1576 if (value == Py_None) { 1577 *(void **)ptr = NULL; 1578 _RET(value); 1579 } 1580 1581 if (!PyInt_Check(value) && !PyLong_Check(value)) { 1582 PyErr_SetString(PyExc_TypeError, 1583 "cannot be converted to pointer"); 1584 return NULL; 1585 } 1586 1587 #if SIZEOF_VOID_P <= SIZEOF_LONG 1588 v = (void *)PyInt_AsUnsignedLongMask(value); 1589 #else 1590 #ifndef HAVE_LONG_LONG 1591 # error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long" 1592 #elif SIZEOF_LONG_LONG < SIZEOF_VOID_P 1593 # error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)" 1594 #endif 1595 v = (void *)PyInt_AsUnsignedLongLongMask(value); 1596 #endif 1597 1598 if (PyErr_Occurred()) 1599 return NULL; 1600 1601 *(void **)ptr = v; 1602 _RET(value); 1603 } 1604 1605 static PyObject * 1606 P_get(void *ptr, Py_ssize_t size) 1607 { 1608 if (*(void **)ptr == NULL) { 1609 Py_INCREF(Py_None); 1610 return Py_None; 1611 } 1612 return PyLong_FromVoidPtr(*(void **)ptr); 1613 } 1614 1615 static struct fielddesc formattable[] = { 1616 { 's', s_set, s_get, &ffi_type_pointer}, 1617 { 'b', b_set, b_get, &ffi_type_schar}, 1618 { 'B', B_set, B_get, &ffi_type_uchar}, 1619 { 'c', c_set, c_get, &ffi_type_schar}, 1620 { 'd', d_set, d_get, &ffi_type_double, d_set_sw, d_get_sw}, 1621 { 'g', g_set, g_get, &ffi_type_longdouble}, 1622 { 'f', f_set, f_get, &ffi_type_float, f_set_sw, f_get_sw}, 1623 { 'h', h_set, h_get, &ffi_type_sshort, h_set_sw, h_get_sw}, 1624 { 'H', H_set, H_get, &ffi_type_ushort, H_set_sw, H_get_sw}, 1625 { 'i', i_set, i_get, &ffi_type_sint, i_set_sw, i_get_sw}, 1626 { 'I', I_set, I_get, &ffi_type_uint, I_set_sw, I_get_sw}, 1627 /* XXX Hm, sizeof(int) == sizeof(long) doesn't hold on every platform */ 1628 /* As soon as we can get rid of the type codes, this is no longer a problem */ 1629 #if SIZEOF_LONG == 4 1630 { 'l', l_set, l_get, &ffi_type_sint32, l_set_sw, l_get_sw}, 1631 { 'L', L_set, L_get, &ffi_type_uint32, L_set_sw, L_get_sw}, 1632 #elif SIZEOF_LONG == 8 1633 { 'l', l_set, l_get, &ffi_type_sint64, l_set_sw, l_get_sw}, 1634 { 'L', L_set, L_get, &ffi_type_uint64, L_set_sw, L_get_sw}, 1635 #else 1636 # error 1637 #endif 1638 #ifdef HAVE_LONG_LONG 1639 #if SIZEOF_LONG_LONG == 8 1640 { 'q', q_set, q_get, &ffi_type_sint64, q_set_sw, q_get_sw}, 1641 { 'Q', Q_set, Q_get, &ffi_type_uint64, Q_set_sw, Q_get_sw}, 1642 #else 1643 # error 1644 #endif 1645 #endif 1646 { 'P', P_set, P_get, &ffi_type_pointer}, 1647 { 'z', z_set, z_get, &ffi_type_pointer}, 1648 #ifdef CTYPES_UNICODE 1649 { 'u', u_set, u_get, NULL}, /* ffi_type set later */ 1650 { 'U', U_set, U_get, &ffi_type_pointer}, 1651 { 'Z', Z_set, Z_get, &ffi_type_pointer}, 1652 #endif 1653 #ifdef MS_WIN32 1654 { 'X', BSTR_set, BSTR_get, &ffi_type_pointer}, 1655 { 'v', vBOOL_set, vBOOL_get, &ffi_type_sshort}, 1656 #endif 1657 #if SIZEOF__BOOL == 1 1658 { '?', bool_set, bool_get, &ffi_type_uchar}, /* Also fallback for no native _Bool support */ 1659 #elif SIZEOF__BOOL == SIZEOF_SHORT 1660 { '?', bool_set, bool_get, &ffi_type_ushort}, 1661 #elif SIZEOF__BOOL == SIZEOF_INT 1662 { '?', bool_set, bool_get, &ffi_type_uint, I_set_sw, I_get_sw}, 1663 #elif SIZEOF__BOOL == SIZEOF_LONG 1664 { '?', bool_set, bool_get, &ffi_type_ulong, L_set_sw, L_get_sw}, 1665 #elif SIZEOF__BOOL == SIZEOF_LONG_LONG 1666 { '?', bool_set, bool_get, &ffi_type_ulong, Q_set_sw, Q_get_sw}, 1667 #endif /* SIZEOF__BOOL */ 1668 { 'O', O_set, O_get, &ffi_type_pointer}, 1669 { 0, NULL, NULL, NULL}, 1670 }; 1671 1672 /* 1673 Ideas: Implement VARIANT in this table, using 'V' code. 1674 Use '?' as code for BOOL. 1675 */ 1676 1677 struct fielddesc * 1678 _ctypes_get_fielddesc(char *fmt) 1679 { 1680 static int initialized = 0; 1681 struct fielddesc *table = formattable; 1682 1683 if (!initialized) { 1684 initialized = 1; 1685 #ifdef CTYPES_UNICODE 1686 if (sizeof(wchar_t) == sizeof(short)) 1687 _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sshort; 1688 else if (sizeof(wchar_t) == sizeof(int)) 1689 _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_sint; 1690 else if (sizeof(wchar_t) == sizeof(long)) 1691 _ctypes_get_fielddesc("u")->pffi_type = &ffi_type_slong; 1692 #endif 1693 } 1694 1695 for (; table->code; ++table) { 1696 if (table->code == fmt[0]) 1697 return table; 1698 } 1699 return NULL; 1700 } 1701 1702 typedef struct { char c; char x; } s_char; 1703 typedef struct { char c; short x; } s_short; 1704 typedef struct { char c; int x; } s_int; 1705 typedef struct { char c; long x; } s_long; 1706 typedef struct { char c; float x; } s_float; 1707 typedef struct { char c; double x; } s_double; 1708 typedef struct { char c; long double x; } s_long_double; 1709 typedef struct { char c; char *x; } s_char_p; 1710 typedef struct { char c; void *x; } s_void_p; 1711 1712 /* 1713 #define CHAR_ALIGN (sizeof(s_char) - sizeof(char)) 1714 #define SHORT_ALIGN (sizeof(s_short) - sizeof(short)) 1715 #define INT_ALIGN (sizeof(s_int) - sizeof(int)) 1716 #define LONG_ALIGN (sizeof(s_long) - sizeof(long)) 1717 */ 1718 #define FLOAT_ALIGN (sizeof(s_float) - sizeof(float)) 1719 #define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double)) 1720 #define LONGDOUBLE_ALIGN (sizeof(s_long_double) - sizeof(long double)) 1721 1722 /* #define CHAR_P_ALIGN (sizeof(s_char_p) - sizeof(char*)) */ 1723 #define VOID_P_ALIGN (sizeof(s_void_p) - sizeof(void*)) 1724 1725 /* 1726 #ifdef HAVE_USABLE_WCHAR_T 1727 typedef struct { char c; wchar_t x; } s_wchar; 1728 typedef struct { char c; wchar_t *x; } s_wchar_p; 1729 1730 #define WCHAR_ALIGN (sizeof(s_wchar) - sizeof(wchar_t)) 1731 #define WCHAR_P_ALIGN (sizeof(s_wchar_p) - sizeof(wchar_t*)) 1732 #endif 1733 */ 1734 1735 #ifdef HAVE_LONG_LONG 1736 typedef struct { char c; PY_LONG_LONG x; } s_long_long; 1737 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG)) 1738 #endif 1739 1740 /* from ffi.h: 1741 typedef struct _ffi_type 1742 { 1743 size_t size; 1744 unsigned short alignment; 1745 unsigned short type; 1746 struct _ffi_type **elements; 1747 } ffi_type; 1748 */ 1749 1750 /* align and size are bogus for void, but they must not be zero */ 1751 ffi_type ffi_type_void = { 1, 1, FFI_TYPE_VOID }; 1752 1753 ffi_type ffi_type_uint8 = { 1, 1, FFI_TYPE_UINT8 }; 1754 ffi_type ffi_type_sint8 = { 1, 1, FFI_TYPE_SINT8 }; 1755 1756 ffi_type ffi_type_uint16 = { 2, 2, FFI_TYPE_UINT16 }; 1757 ffi_type ffi_type_sint16 = { 2, 2, FFI_TYPE_SINT16 }; 1758 1759 ffi_type ffi_type_uint32 = { 4, 4, FFI_TYPE_UINT32 }; 1760 ffi_type ffi_type_sint32 = { 4, 4, FFI_TYPE_SINT32 }; 1761 1762 ffi_type ffi_type_uint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_UINT64 }; 1763 ffi_type ffi_type_sint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_SINT64 }; 1764 1765 ffi_type ffi_type_float = { sizeof(float), FLOAT_ALIGN, FFI_TYPE_FLOAT }; 1766 ffi_type ffi_type_double = { sizeof(double), DOUBLE_ALIGN, FFI_TYPE_DOUBLE }; 1767 1768 #ifdef ffi_type_longdouble 1769 #undef ffi_type_longdouble 1770 #endif 1771 /* This is already defined on OSX */ 1772 ffi_type ffi_type_longdouble = { sizeof(long double), LONGDOUBLE_ALIGN, 1773 FFI_TYPE_LONGDOUBLE }; 1774 1775 ffi_type ffi_type_pointer = { sizeof(void *), VOID_P_ALIGN, FFI_TYPE_POINTER }; 1776 1777 /*---------------- EOF ----------------*/