Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(28)

Delta Between Two Patch Sets: Modules/_ctypes/_ctypes.c

Issue 10744: ctypes arrays have incorrect buffer information (PEP-3118)
Left Patch Set: Created 5 years, 10 months ago
Right Patch Set: Created 5 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/ctypes/test/test_pep3118.py ('k') | Modules/_ctypes/ctypes.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /*****************************************************************
2 This file should be kept compatible with Python 2.3, see PEP 291.
3 *****************************************************************/
4
5
6 /* 1 /*
7 ToDo: 2 ToDo:
8 3
9 Get rid of the checker (and also the converters) field in PyCFuncPtrObject and 4 Get rid of the checker (and also the converters) field in PyCFuncPtrObject and
10 StgDictObject, and replace them by slot functions in StgDictObject. 5 StgDictObject, and replace them by slot functions in StgDictObject.
11 6
12 think about a buffer-like object (memory? bytes?) 7 think about a buffer-like object (memory? bytes?)
13 8
14 Should POINTER(c_char) and POINTER(c_wchar) have a .value property? 9 Should POINTER(c_char) and POINTER(c_wchar) have a .value property?
15 What about c_char and c_wchar arrays then? 10 What about c_char and c_wchar arrays then?
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 PyObject *PyExc_ArgError; 125 PyObject *PyExc_ArgError;
131 126
132 /* This dict maps ctypes types to POINTER types */ 127 /* This dict maps ctypes types to POINTER types */
133 PyObject *_ctypes_ptrtype_cache; 128 PyObject *_ctypes_ptrtype_cache;
134 129
135 static PyTypeObject Simple_Type; 130 static PyTypeObject Simple_Type;
136 131
137 /* a callable object used for unpickling */ 132 /* a callable object used for unpickling */
138 static PyObject *_unpickle; 133 static PyObject *_unpickle;
139 134
140 char *_ctypes_conversion_encoding = NULL; 135
141 char *_ctypes_conversion_errors = NULL;
142
143
144 /****************************************************************/
145
146 #if (PY_VERSION_HEX < 0x02040000)
147 /* Only in Python 2.4 and up */
148 static PyObject *
149 PyTuple_Pack(int n, ...)
150 {
151 int i;
152 PyObject *o;
153 PyObject *result;
154 PyObject **items;
155 va_list vargs;
156
157 va_start(vargs, n);
158 result = PyTuple_New(n);
159 if (result == NULL)
160 return NULL;
161 items = ((PyTupleObject *)result)->ob_item;
162 for (i = 0; i < n; i++) {
163 o = va_arg(vargs, PyObject *);
164 Py_INCREF(o);
165 items[i] = o;
166 }
167 va_end(vargs);
168 return result;
169 }
170 #endif
171 136
172 /****************************************************************/ 137 /****************************************************************/
173 138
174 typedef struct { 139 typedef struct {
175 PyObject_HEAD 140 PyObject_HEAD
176 PyObject *key; 141 PyObject *key;
177 PyObject *dict; 142 PyObject *dict;
178 } DictRemoverObject; 143 } DictRemoverObject;
179 144
180 static void 145 static void
181 _DictRemover_dealloc(PyObject *_self) 146 _DictRemover_dealloc(PyObject *myself)
182 { 147 {
183 DictRemoverObject *self = (DictRemoverObject *)_self; 148 DictRemoverObject *self = (DictRemoverObject *)myself;
184 Py_XDECREF(self->key); 149 Py_XDECREF(self->key);
185 Py_XDECREF(self->dict); 150 Py_XDECREF(self->dict);
186 Py_TYPE(self)->tp_free(_self); 151 Py_TYPE(self)->tp_free(myself);
187 } 152 }
188 153
189 static PyObject * 154 static PyObject *
190 _DictRemover_call(PyObject *_self, PyObject *args, PyObject *kw) 155 _DictRemover_call(PyObject *myself, PyObject *args, PyObject *kw)
191 { 156 {
192 DictRemoverObject *self = (DictRemoverObject *)_self; 157 DictRemoverObject *self = (DictRemoverObject *)myself;
193 if (self->key && self->dict) { 158 if (self->key && self->dict) {
194 if (-1 == PyDict_DelItem(self->dict, self->key)) 159 if (-1 == PyDict_DelItem(self->dict, self->key))
195 /* XXX Error context */ 160 /* XXX Error context */
196 PyErr_WriteUnraisable(Py_None); 161 PyErr_WriteUnraisable(Py_None);
197 Py_CLEAR(self->key); 162 Py_CLEAR(self->key);
198 Py_CLEAR(self->dict); 163 Py_CLEAR(self->dict);
199 } 164 }
200 Py_INCREF(Py_None); 165 Py_INCREF(Py_None);
201 return Py_None; 166 return Py_None;
202 } 167 }
203 168
204 static PyTypeObject DictRemover_Type = { 169 static PyTypeObject DictRemover_Type = {
205 PyVarObject_HEAD_INIT(NULL, 0) 170 PyVarObject_HEAD_INIT(NULL, 0)
206 "_ctypes.DictRemover", /* tp_name */ 171 "_ctypes.DictRemover", /* tp_name */
207 sizeof(DictRemoverObject), /* tp_basicsize */ 172 sizeof(DictRemoverObject), /* tp_basicsize */
208 0, /* tp_itemsize */ 173 0, /* tp_itemsize */
209 _DictRemover_dealloc, /* tp_dealloc */ 174 _DictRemover_dealloc, /* tp_dealloc */
210 0, /* tp_print */ 175 0, /* tp_print */
211 0, /* tp_getattr */ 176 0, /* tp_getattr */
212 0, /* tp_setattr */ 177 0, /* tp_setattr */
213 0, /* tp_compare */ 178 0, /* tp_reserved */
214 0, /* tp_repr */ 179 0, /* tp_repr */
215 0, /* tp_as_number */ 180 0, /* tp_as_number */
216 0, /* tp_as_sequence */ 181 0, /* tp_as_sequence */
217 0, /* tp_as_mapping */ 182 0, /* tp_as_mapping */
218 0, /* tp_hash */ 183 0, /* tp_hash */
219 _DictRemover_call, /* tp_call */ 184 _DictRemover_call, /* tp_call */
220 0, /* tp_str */ 185 0, /* tp_str */
221 0, /* tp_getattro */ 186 0, /* tp_getattro */
222 0, /* tp_setattro */ 187 0, /* tp_setattro */
223 0, /* tp_as_buffer */ 188 0, /* tp_as_buffer */
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
304 char *result; 269 char *result;
305 270
306 if (suffix == NULL) { 271 if (suffix == NULL) {
307 assert(PyErr_Occurred()); 272 assert(PyErr_Occurred());
308 return NULL; 273 return NULL;
309 } 274 }
310 len = strlen(suffix); 275 len = strlen(suffix);
311 if (prefix) 276 if (prefix)
312 len += strlen(prefix); 277 len += strlen(prefix);
313 result = PyMem_Malloc(len + 1); 278 result = PyMem_Malloc(len + 1);
314 if (result == NULL) 279 if (result == NULL) {
315 return NULL; 280 PyErr_NoMemory();
281 return NULL;
282 }
316 if (prefix) 283 if (prefix)
317 strcpy(result, prefix); 284 strcpy(result, prefix);
318 else 285 else
319 result[0] = '\0'; 286 result[0] = '\0';
320 strcat(result, suffix); 287 strcat(result, suffix);
321 return result; 288 return result;
322 } 289 }
323 290
324 /* 291 /*
325 Allocate a memory block for a pep3118 format string, adding 292 Allocate a memory block for a pep3118 format string, adding
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 return StructUnionType_new(type, args, kwds, 0); 436 return StructUnionType_new(type, args, kwds, 0);
470 } 437 }
471 438
472 static char from_address_doc[] = 439 static char from_address_doc[] =
473 "C.from_address(integer) -> C instance\naccess a C instance at the specified add ress"; 440 "C.from_address(integer) -> C instance\naccess a C instance at the specified add ress";
474 441
475 static PyObject * 442 static PyObject *
476 CDataType_from_address(PyObject *type, PyObject *value) 443 CDataType_from_address(PyObject *type, PyObject *value)
477 { 444 {
478 void *buf; 445 void *buf;
479 if (!PyInt_Check(value) && !PyLong_Check(value)) { 446 if (!PyLong_Check(value)) {
480 PyErr_SetString(PyExc_TypeError, 447 PyErr_SetString(PyExc_TypeError,
481 "integer expected"); 448 "integer expected");
482 return NULL; 449 return NULL;
483 } 450 }
484 buf = (void *)PyLong_AsVoidPtr(value); 451 buf = (void *)PyLong_AsVoidPtr(value);
485 if (PyErr_Occurred()) 452 if (PyErr_Occurred())
486 return NULL; 453 return NULL;
487 return PyCData_AtAddress(type, buf); 454 return PyCData_AtAddress(type, buf);
488 } 455 }
489 456
490 static char from_buffer_doc[] = 457 static char from_buffer_doc[] =
491 "C.from_buffer(object, offset=0) -> C instance\ncreate a C instance from a write able buffer"; 458 "C.from_buffer(object, offset=0) -> C instance\ncreate a C instance from a write able buffer";
492 459
493 static int 460 static int
494 KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep); 461 KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep);
495 462
496 static PyObject * 463 static PyObject *
497 CDataType_from_buffer(PyObject *type, PyObject *args) 464 CDataType_from_buffer(PyObject *type, PyObject *args)
498 { 465 {
499 void *buffer; 466 void *buffer;
500 Py_ssize_t buffer_len; 467 Py_ssize_t buffer_len;
501 Py_ssize_t offset = 0; 468 Py_ssize_t offset = 0;
502 PyObject *obj, *result; 469 PyObject *obj, *result;
503 StgDictObject *dict = PyType_stgdict(type); 470 StgDictObject *dict = PyType_stgdict(type);
504 assert (dict); 471 assert (dict);
505 472
506 if (!PyArg_ParseTuple(args, 473 if (!PyArg_ParseTuple(args, "O|n:from_buffer", &obj, &offset))
507 #if (PY_VERSION_HEX < 0x02050000)
508 "O|i:from_buffer",
509 #else
510 "O|n:from_buffer",
511 #endif
512 &obj, &offset))
513 return NULL; 474 return NULL;
514 475
515 if (-1 == PyObject_AsWriteBuffer(obj, &buffer, &buffer_len)) 476 if (-1 == PyObject_AsWriteBuffer(obj, &buffer, &buffer_len))
516 return NULL; 477 return NULL;
517 478
518 if (offset < 0) { 479 if (offset < 0) {
519 PyErr_SetString(PyExc_ValueError, 480 PyErr_SetString(PyExc_ValueError,
520 "offset cannot be negative"); 481 "offset cannot be negative");
521 return NULL; 482 return NULL;
522 } 483 }
523 if (dict->size > buffer_len - offset) { 484 if (dict->size > buffer_len - offset) {
524 PyErr_Format(PyExc_ValueError, 485 PyErr_Format(PyExc_ValueError,
525 #if (PY_VERSION_HEX < 0x02050000)
526 "Buffer size too small (%d instead of at least %d bytes)",
527 #else
528 "Buffer size too small (%zd instead of at least %zd bytes)" , 486 "Buffer size too small (%zd instead of at least %zd bytes)" ,
529 #endif
530 buffer_len, dict->size + offset); 487 buffer_len, dict->size + offset);
531 return NULL; 488 return NULL;
532 } 489 }
533 490
534 result = PyCData_AtAddress(type, (char *)buffer + offset); 491 result = PyCData_AtAddress(type, (char *)buffer + offset);
535 if (result == NULL) 492 if (result == NULL)
536 return NULL; 493 return NULL;
537 494
538 Py_INCREF(obj); 495 Py_INCREF(obj);
539 if (-1 == KeepRef((CDataObject *)result, -1, obj)) { 496 if (-1 == KeepRef((CDataObject *)result, -1, obj)) {
540 Py_DECREF(result);
541 return NULL; 497 return NULL;
542 } 498 }
543 return result; 499 return result;
544 } 500 }
545 501
546 static char from_buffer_copy_doc[] = 502 static char from_buffer_copy_doc[] =
547 "C.from_buffer_copy(object, offset=0) -> C instance\ncreate a C instance from a readable buffer"; 503 "C.from_buffer_copy(object, offset=0) -> C instance\ncreate a C instance from a readable buffer";
548 504
549 static PyObject * 505 static PyObject *
550 GenericPyCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds); 506 GenericPyCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
551 507
552 static PyObject * 508 static PyObject *
553 CDataType_from_buffer_copy(PyObject *type, PyObject *args) 509 CDataType_from_buffer_copy(PyObject *type, PyObject *args)
554 { 510 {
555 const void *buffer; 511 const void *buffer;
556 Py_ssize_t buffer_len; 512 Py_ssize_t buffer_len;
557 Py_ssize_t offset = 0; 513 Py_ssize_t offset = 0;
558 PyObject *obj, *result; 514 PyObject *obj, *result;
559 StgDictObject *dict = PyType_stgdict(type); 515 StgDictObject *dict = PyType_stgdict(type);
560 assert (dict); 516 assert (dict);
561 517
562 if (!PyArg_ParseTuple(args, 518 if (!PyArg_ParseTuple(args, "O|n:from_buffer", &obj, &offset))
563 #if (PY_VERSION_HEX < 0x02050000) 519 return NULL;
564 "O|i:from_buffer", 520
565 #else 521 if (-1 == PyObject_AsReadBuffer(obj, (const void**)&buffer, &buffer_len))
566 "O|n:from_buffer",
567 #endif
568 &obj, &offset))
569 return NULL;
570
571 if (-1 == PyObject_AsReadBuffer(obj, &buffer, &buffer_len))
572 return NULL; 522 return NULL;
573 523
574 if (offset < 0) { 524 if (offset < 0) {
575 PyErr_SetString(PyExc_ValueError, 525 PyErr_SetString(PyExc_ValueError,
576 "offset cannot be negative"); 526 "offset cannot be negative");
577 return NULL; 527 return NULL;
578 } 528 }
579 529
580 if (dict->size > buffer_len - offset) { 530 if (dict->size > buffer_len - offset) {
581 PyErr_Format(PyExc_ValueError, 531 PyErr_Format(PyExc_ValueError,
582 #if (PY_VERSION_HEX < 0x02050000)
583 "Buffer size too small (%d instead of at least %d bytes)",
584 #else
585 "Buffer size too small (%zd instead of at least %zd bytes)" , 532 "Buffer size too small (%zd instead of at least %zd bytes)" ,
586 #endif
587 buffer_len, dict->size + offset); 533 buffer_len, dict->size + offset);
588 return NULL; 534 return NULL;
589 } 535 }
590 536
591 result = GenericPyCData_new((PyTypeObject *)type, NULL, NULL); 537 result = GenericPyCData_new((PyTypeObject *)type, NULL, NULL);
592 if (result == NULL) 538 if (result == NULL)
593 return NULL; 539 return NULL;
594 memcpy(((CDataObject *)result)->b_ptr, 540 memcpy(((CDataObject *)result)->b_ptr,
595 (char *)buffer+offset, dict->size); 541 (char *)buffer+offset, dict->size);
596 return result; 542 return result;
(...skipping 10 matching lines...) Expand all
607 PyObject *obj; 553 PyObject *obj;
608 void *handle; 554 void *handle;
609 void *address; 555 void *address;
610 556
611 if (!PyArg_ParseTuple(args, "Os:in_dll", &dll, &name)) 557 if (!PyArg_ParseTuple(args, "Os:in_dll", &dll, &name))
612 return NULL; 558 return NULL;
613 559
614 obj = PyObject_GetAttrString(dll, "_handle"); 560 obj = PyObject_GetAttrString(dll, "_handle");
615 if (!obj) 561 if (!obj)
616 return NULL; 562 return NULL;
617 if (!PyInt_Check(obj) && !PyLong_Check(obj)) { 563 if (!PyLong_Check(obj)) {
618 PyErr_SetString(PyExc_TypeError, 564 PyErr_SetString(PyExc_TypeError,
619 "the _handle attribute of the second argument must be an integer"); 565 "the _handle attribute of the second argument must be an integer");
620 Py_DECREF(obj); 566 Py_DECREF(obj);
621 return NULL; 567 return NULL;
622 } 568 }
623 handle = (void *)PyLong_AsVoidPtr(obj); 569 handle = (void *)PyLong_AsVoidPtr(obj);
624 Py_DECREF(obj); 570 Py_DECREF(obj);
625 if (PyErr_Occurred()) { 571 if (PyErr_Occurred()) {
626 PyErr_SetString(PyExc_ValueError, 572 PyErr_SetString(PyExc_ValueError,
627 "could not convert the _handle attribute to a pointer"); 573 "could not convert the _handle attribute to a pointer");
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 { "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_ copy_doc, }, 658 { "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_ copy_doc, },
713 { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc }, 659 { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc },
714 { NULL, NULL }, 660 { NULL, NULL },
715 }; 661 };
716 662
717 static PyObject * 663 static PyObject *
718 CDataType_repeat(PyObject *self, Py_ssize_t length) 664 CDataType_repeat(PyObject *self, Py_ssize_t length)
719 { 665 {
720 if (length < 0) 666 if (length < 0)
721 return PyErr_Format(PyExc_ValueError, 667 return PyErr_Format(PyExc_ValueError,
722 #if (PY_VERSION_HEX < 0x02050000)
723 "Array length must be >= 0, not %d",
724 #else
725 "Array length must be >= 0, not %zd", 668 "Array length must be >= 0, not %zd",
726 #endif
727 length); 669 length);
728 return PyCArrayType_from_ctype(self, length); 670 return PyCArrayType_from_ctype(self, length);
729 } 671 }
730 672
731 static PySequenceMethods CDataType_as_sequence = { 673 static PySequenceMethods CDataType_as_sequence = {
732 0, /* inquiry sq_length; */ 674 0, /* inquiry sq_length; */
733 0, /* binaryfunc sq_concat; */ 675 0, /* binaryfunc sq_concat; */
734 CDataType_repeat, /* intargfunc sq_repeat; */ 676 CDataType_repeat, /* intargfunc sq_repeat; */
735 0, /* intargfunc sq_item; */ 677 0, /* intargfunc sq_item; */
736 0, /* intintargfunc sq_slice; */ 678 0, /* intintargfunc sq_slice; */
(...skipping 23 matching lines...) Expand all
760 return PyType_Type.tp_traverse((PyObject *)self, visit, arg); 702 return PyType_Type.tp_traverse((PyObject *)self, visit, arg);
761 } 703 }
762 704
763 static int 705 static int
764 PyCStructType_setattro(PyObject *self, PyObject *key, PyObject *value) 706 PyCStructType_setattro(PyObject *self, PyObject *key, PyObject *value)
765 { 707 {
766 /* XXX Should we disallow deleting _fields_? */ 708 /* XXX Should we disallow deleting _fields_? */
767 if (-1 == PyType_Type.tp_setattro(self, key, value)) 709 if (-1 == PyType_Type.tp_setattro(self, key, value))
768 return -1; 710 return -1;
769 711
770 if (value && PyString_Check(key) && 712 if (value && PyUnicode_Check(key) &&
771 0 == strcmp(PyString_AS_STRING(key), "_fields_")) 713 /* XXX struni _PyUnicode_AsString can fail (also in other places)! */
714 0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
772 return PyCStructUnionType_update_stgdict(self, value, 1); 715 return PyCStructUnionType_update_stgdict(self, value, 1);
773 return 0; 716 return 0;
774 } 717 }
775 718
776 719
777 static int 720 static int
778 UnionType_setattro(PyObject *self, PyObject *key, PyObject *value) 721 UnionType_setattro(PyObject *self, PyObject *key, PyObject *value)
779 { 722 {
780 /* XXX Should we disallow deleting _fields_? */ 723 /* XXX Should we disallow deleting _fields_? */
781 if (-1 == PyObject_GenericSetAttr(self, key, value)) 724 if (-1 == PyObject_GenericSetAttr(self, key, value))
782 return -1; 725 return -1;
783 726
784 if (PyString_Check(key) && 727 if (PyUnicode_Check(key) &&
785 0 == strcmp(PyString_AS_STRING(key), "_fields_")) 728 0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
786 return PyCStructUnionType_update_stgdict(self, value, 0); 729 return PyCStructUnionType_update_stgdict(self, value, 0);
787 return 0; 730 return 0;
788 } 731 }
789 732
790 733
791 PyTypeObject PyCStructType_Type = { 734 PyTypeObject PyCStructType_Type = {
792 PyVarObject_HEAD_INIT(NULL, 0) 735 PyVarObject_HEAD_INIT(NULL, 0)
793 "_ctypes.PyCStructType", /* tp_name */ 736 "_ctypes.PyCStructType", /* tp_name */
794 0, /* tp_basicsize */ 737 0, /* tp_basicsize */
795 0, /* tp_itemsize */ 738 0, /* tp_itemsize */
796 0, /* tp_dealloc */ 739 0, /* tp_dealloc */
797 0, /* tp_print */ 740 0, /* tp_print */
798 0, /* tp_getattr */ 741 0, /* tp_getattr */
799 0, /* tp_setattr */ 742 0, /* tp_setattr */
800 0, /* tp_compare */ 743 0, /* tp_reserved */
801 0, /* tp_repr */ 744 0, /* tp_repr */
802 0, /* tp_as_number */ 745 0, /* tp_as_number */
803 &CDataType_as_sequence, /* tp_as_sequence */ 746 &CDataType_as_sequence, /* tp_as_sequence */
804 0, /* tp_as_mapping */ 747 0, /* tp_as_mapping */
805 0, /* tp_hash */ 748 0, /* tp_hash */
806 0, /* tp_call */ 749 0, /* tp_call */
807 0, /* tp_str */ 750 0, /* tp_str */
808 0, /* tp_getattro */ 751 0, /* tp_getattro */
809 PyCStructType_setattro, /* tp_setattro */ 752 PyCStructType_setattro, /* tp_setattro */
810 0, /* tp_as_buffer */ 753 0, /* tp_as_buffer */
(...skipping 21 matching lines...) Expand all
832 775
833 static PyTypeObject UnionType_Type = { 776 static PyTypeObject UnionType_Type = {
834 PyVarObject_HEAD_INIT(NULL, 0) 777 PyVarObject_HEAD_INIT(NULL, 0)
835 "_ctypes.UnionType", /* tp_name */ 778 "_ctypes.UnionType", /* tp_name */
836 0, /* tp_basicsize */ 779 0, /* tp_basicsize */
837 0, /* tp_itemsize */ 780 0, /* tp_itemsize */
838 0, /* tp_dealloc */ 781 0, /* tp_dealloc */
839 0, /* tp_print */ 782 0, /* tp_print */
840 0, /* tp_getattr */ 783 0, /* tp_getattr */
841 0, /* tp_setattr */ 784 0, /* tp_setattr */
842 0, /* tp_compare */ 785 0, /* tp_reserved */
843 0, /* tp_repr */ 786 0, /* tp_repr */
844 0, /* tp_as_number */ 787 0, /* tp_as_number */
845 &CDataType_as_sequence, /* tp_as_sequence */ 788 &CDataType_as_sequence, /* tp_as_sequence */
846 0, /* tp_as_mapping */ 789 0, /* tp_as_mapping */
847 0, /* tp_hash */ 790 0, /* tp_hash */
848 0, /* tp_call */ 791 0, /* tp_call */
849 0, /* tp_str */ 792 0, /* tp_str */
850 0, /* tp_getattro */ 793 0, /* tp_getattro */
851 UnionType_setattro, /* tp_setattro */ 794 UnionType_setattro, /* tp_setattro */
852 0, /* tp_as_buffer */ 795 0, /* tp_as_buffer */
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
1012 if (-1 == PyCPointerType_SetProto(dict, type)) 955 if (-1 == PyCPointerType_SetProto(dict, type))
1013 return NULL; 956 return NULL;
1014 957
1015 if (-1 == PyDict_SetItemString((PyObject *)dict, "_type_", type)) 958 if (-1 == PyDict_SetItemString((PyObject *)dict, "_type_", type))
1016 return NULL; 959 return NULL;
1017 960
1018 Py_INCREF(Py_None); 961 Py_INCREF(Py_None);
1019 return Py_None; 962 return Py_None;
1020 } 963 }
1021 964
1022 staticforward PyObject *_byref(PyObject *); 965 static PyObject *_byref(PyObject *);
1023 966
1024 static PyObject * 967 static PyObject *
1025 PyCPointerType_from_param(PyObject *type, PyObject *value) 968 PyCPointerType_from_param(PyObject *type, PyObject *value)
1026 { 969 {
1027 StgDictObject *typedict; 970 StgDictObject *typedict;
1028 971
1029 if (value == Py_None) { 972 if (value == Py_None) {
1030 /* ConvParam will convert to a NULL pointer later */ 973 /* ConvParam will convert to a NULL pointer later */
1031 Py_INCREF(value); 974 Py_INCREF(value);
1032 return value; 975 return value;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1074 1017
1075 PyTypeObject PyCPointerType_Type = { 1018 PyTypeObject PyCPointerType_Type = {
1076 PyVarObject_HEAD_INIT(NULL, 0) 1019 PyVarObject_HEAD_INIT(NULL, 0)
1077 "_ctypes.PyCPointerType", /* tp_name */ 1020 "_ctypes.PyCPointerType", /* tp_name */
1078 0, /* tp_basicsize */ 1021 0, /* tp_basicsize */
1079 0, /* tp_itemsize */ 1022 0, /* tp_itemsize */
1080 0, /* tp_dealloc */ 1023 0, /* tp_dealloc */
1081 0, /* tp_print */ 1024 0, /* tp_print */
1082 0, /* tp_getattr */ 1025 0, /* tp_getattr */
1083 0, /* tp_setattr */ 1026 0, /* tp_setattr */
1084 0, /* tp_compare */ 1027 0, /* tp_reserved */
1085 0, /* tp_repr */ 1028 0, /* tp_repr */
1086 0, /* tp_as_number */ 1029 0, /* tp_as_number */
1087 &CDataType_as_sequence, /* tp_as_sequence */ 1030 &CDataType_as_sequence, /* tp_as_sequence */
1088 0, /* tp_as_mapping */ 1031 0, /* tp_as_mapping */
1089 0, /* tp_hash */ 1032 0, /* tp_hash */
1090 0, /* tp_call */ 1033 0, /* tp_call */
1091 0, /* tp_str */ 1034 0, /* tp_str */
1092 0, /* tp_getattro */ 1035 0, /* tp_getattro */
1093 0, /* tp_setattro */ 1036 0, /* tp_setattro */
1094 0, /* tp_as_buffer */ 1037 0, /* tp_as_buffer */
(...skipping 27 matching lines...) Expand all
1122 /* 1065 /*
1123 PyCArrayType_new ensures that the new Array subclass created has a _length_ 1066 PyCArrayType_new ensures that the new Array subclass created has a _length_
1124 attribute, and a _type_ attribute. 1067 attribute, and a _type_ attribute.
1125 */ 1068 */
1126 1069
1127 static int 1070 static int
1128 CharArray_set_raw(CDataObject *self, PyObject *value) 1071 CharArray_set_raw(CDataObject *self, PyObject *value)
1129 { 1072 {
1130 char *ptr; 1073 char *ptr;
1131 Py_ssize_t size; 1074 Py_ssize_t size;
1132 #if (PY_VERSION_HEX >= 0x02060000) 1075 Py_buffer view;
1133 Py_buffer view = { 0 }; 1076
1134 #endif 1077 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
1135 if (PyBuffer_Check(value)) { 1078 return -1;
1136 size = Py_TYPE(value)->tp_as_buffer->bf_getreadbuffer(value, 0, (void *) &ptr); 1079 size = view.len;
1137 if (size < 0) 1080 ptr = view.buf;
1138 goto fail;
1139 } else {
1140 #if (PY_VERSION_HEX >= 0x02060000)
1141 if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
1142 goto fail;
1143 size = view.len;
1144 ptr = view.buf;
1145 #else
1146 if (-1 == PyString_AsStringAndSize(value, &ptr, &size))
1147 goto fail;
1148 #endif
1149 }
1150 if (size > self->b_size) { 1081 if (size > self->b_size) {
1151 PyErr_SetString(PyExc_ValueError, 1082 PyErr_SetString(PyExc_ValueError,
1152 "string too long"); 1083 "string too long");
1153 goto fail; 1084 goto fail;
1154 } 1085 }
1155 1086
1156 memcpy(self->b_ptr, ptr, size); 1087 memcpy(self->b_ptr, ptr, size);
1157 1088
1158 #if (PY_VERSION_HEX >= 0x02060000)
1159 PyBuffer_Release(&view); 1089 PyBuffer_Release(&view);
1160 #endif
1161 return 0; 1090 return 0;
1162 fail: 1091 fail:
1163
1164 #if (PY_VERSION_HEX >= 0x02060000)
1165 PyBuffer_Release(&view); 1092 PyBuffer_Release(&view);
1166 #endif
1167 return -1; 1093 return -1;
1168 } 1094 }
1169 1095
1170 static PyObject * 1096 static PyObject *
1171 CharArray_get_raw(CDataObject *self) 1097 CharArray_get_raw(CDataObject *self)
1172 { 1098 {
1173 return PyString_FromStringAndSize(self->b_ptr, self->b_size); 1099 return PyBytes_FromStringAndSize(self->b_ptr, self->b_size);
1174 } 1100 }
1175 1101
1176 static PyObject * 1102 static PyObject *
1177 CharArray_get_value(CDataObject *self) 1103 CharArray_get_value(CDataObject *self)
1178 { 1104 {
1179 int i; 1105 int i;
1180 char *ptr = self->b_ptr; 1106 char *ptr = self->b_ptr;
1181 for (i = 0; i < self->b_size; ++i) 1107 for (i = 0; i < self->b_size; ++i)
1182 if (*ptr++ == '\0') 1108 if (*ptr++ == '\0')
1183 break; 1109 break;
1184 return PyString_FromStringAndSize(self->b_ptr, i); 1110 return PyBytes_FromStringAndSize(self->b_ptr, i);
1185 } 1111 }
1186 1112
1187 static int 1113 static int
1188 CharArray_set_value(CDataObject *self, PyObject *value) 1114 CharArray_set_value(CDataObject *self, PyObject *value)
1189 { 1115 {
1190 char *ptr; 1116 char *ptr;
1191 Py_ssize_t size; 1117 Py_ssize_t size;
1192 1118
1193 if (value == NULL) { 1119 if (value == NULL) {
1194 PyErr_SetString(PyExc_TypeError, 1120 PyErr_SetString(PyExc_TypeError,
1195 "can't delete attribute"); 1121 "can't delete attribute");
1196 return -1; 1122 return -1;
1197 } 1123 }
1198 1124
1199 if (PyUnicode_Check(value)) { 1125 if (!PyBytes_Check(value)) {
1200 value = PyUnicode_AsEncodedString(value,
1201 _ctypes_conversion_encoding,
1202 _ctypes_conversion_errors);
1203 if (!value)
1204 return -1;
1205 } else if (!PyString_Check(value)) {
1206 PyErr_Format(PyExc_TypeError, 1126 PyErr_Format(PyExc_TypeError,
1207 "string expected instead of %s instance", 1127 "bytes expected instead of %s instance",
1208 Py_TYPE(value)->tp_name); 1128 Py_TYPE(value)->tp_name);
1209 return -1; 1129 return -1;
1210 } else 1130 } else
1211 Py_INCREF(value); 1131 Py_INCREF(value);
1212 size = PyString_GET_SIZE(value); 1132 size = PyBytes_GET_SIZE(value);
1213 if (size > self->b_size) { 1133 if (size > self->b_size) {
1214 PyErr_SetString(PyExc_ValueError, 1134 PyErr_SetString(PyExc_ValueError,
1215 "string too long"); 1135 "string too long");
1216 Py_DECREF(value); 1136 Py_DECREF(value);
1217 return -1; 1137 return -1;
1218 } 1138 }
1219 1139
1220 ptr = PyString_AS_STRING(value); 1140 ptr = PyBytes_AS_STRING(value);
1221 memcpy(self->b_ptr, ptr, size); 1141 memcpy(self->b_ptr, ptr, size);
1222 if (size < self->b_size) 1142 if (size < self->b_size)
1223 self->b_ptr[size] = '\0'; 1143 self->b_ptr[size] = '\0';
1224 Py_DECREF(value); 1144 Py_DECREF(value);
1225 1145
1226 return 0; 1146 return 0;
1227 } 1147 }
1228 1148
1229 static PyGetSetDef CharArray_getsets[] = { 1149 static PyGetSetDef CharArray_getsets[] = {
1230 { "raw", (getter)CharArray_get_raw, (setter)CharArray_set_raw, 1150 { "raw", (getter)CharArray_get_raw, (setter)CharArray_set_raw,
(...skipping 12 matching lines...) Expand all
1243 for (i = 0; i < self->b_size/sizeof(wchar_t); ++i) 1163 for (i = 0; i < self->b_size/sizeof(wchar_t); ++i)
1244 if (*ptr++ == (wchar_t)0) 1164 if (*ptr++ == (wchar_t)0)
1245 break; 1165 break;
1246 return PyUnicode_FromWideChar((wchar_t *)self->b_ptr, i); 1166 return PyUnicode_FromWideChar((wchar_t *)self->b_ptr, i);
1247 } 1167 }
1248 1168
1249 static int 1169 static int
1250 WCharArray_set_value(CDataObject *self, PyObject *value) 1170 WCharArray_set_value(CDataObject *self, PyObject *value)
1251 { 1171 {
1252 Py_ssize_t result = 0; 1172 Py_ssize_t result = 0;
1173 Py_UNICODE *wstr;
1174 Py_ssize_t len;
1253 1175
1254 if (value == NULL) { 1176 if (value == NULL) {
1255 PyErr_SetString(PyExc_TypeError, 1177 PyErr_SetString(PyExc_TypeError,
1256 "can't delete attribute"); 1178 "can't delete attribute");
1257 return -1; 1179 return -1;
1258 } 1180 }
1259 if (PyString_Check(value)) { 1181 if (!PyUnicode_Check(value)) {
1260 value = PyUnicode_FromEncodedObject(value,
1261 _ctypes_conversion_encoding,
1262 _ctypes_conversion_errors);
1263 if (!value)
1264 return -1;
1265 } else if (!PyUnicode_Check(value)) {
1266 PyErr_Format(PyExc_TypeError, 1182 PyErr_Format(PyExc_TypeError,
1267 "unicode string expected instead of %s instance", 1183 "unicode string expected instead of %s instance",
1268 Py_TYPE(value)->tp_name); 1184 Py_TYPE(value)->tp_name);
1269 return -1; 1185 return -1;
1270 } else 1186 } else
1271 Py_INCREF(value); 1187 Py_INCREF(value);
1272 if ((unsigned)PyUnicode_GET_SIZE(value) > self->b_size/sizeof(wchar_t)) { 1188
1189 wstr = PyUnicode_AsUnicodeAndSize(value, &len);
1190 if (wstr == NULL)
1191 return -1;
1192 if ((unsigned)len > self->b_size/sizeof(wchar_t)) {
1273 PyErr_SetString(PyExc_ValueError, 1193 PyErr_SetString(PyExc_ValueError,
1274 "string too long"); 1194 "string too long");
1275 result = -1; 1195 result = -1;
1276 goto done; 1196 goto done;
1277 } 1197 }
1278 result = PyUnicode_AsWideChar((PyUnicodeObject *)value, 1198 result = PyUnicode_AsWideChar(value,
1279 (wchar_t *)self->b_ptr, 1199 (wchar_t *)self->b_ptr,
1280 self->b_size/sizeof(wchar_t)); 1200 self->b_size/sizeof(wchar_t));
1281 if (result >= 0 && (size_t)result < self->b_size/sizeof(wchar_t)) 1201 if (result >= 0 && (size_t)result < self->b_size/sizeof(wchar_t))
1282 ((wchar_t *)self->b_ptr)[result] = (wchar_t)0; 1202 ((wchar_t *)self->b_ptr)[result] = (wchar_t)0;
1283 done: 1203 done:
1284 Py_DECREF(value); 1204 Py_DECREF(value);
1285 1205
1286 return result >= 0 ? 0 : -1; 1206 return result >= 0 ? 0 : -1;
1287 } 1207 }
1288 1208
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
1363 p->obj = (PyObject *)self; 1283 p->obj = (PyObject *)self;
1364 return p; 1284 return p;
1365 } 1285 }
1366 1286
1367 static PyObject * 1287 static PyObject *
1368 PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 1288 PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1369 { 1289 {
1370 PyTypeObject *result; 1290 PyTypeObject *result;
1371 StgDictObject *stgdict; 1291 StgDictObject *stgdict;
1372 StgDictObject *itemdict; 1292 StgDictObject *itemdict;
1373 PyObject *proto; 1293 PyObject *length_attr, *type_attr;
1374 PyObject *typedict;
1375 long length; 1294 long length;
1376 1295 int overflow;
1377 Py_ssize_t itemsize, itemalign; 1296 Py_ssize_t itemsize, itemalign;
1378 1297
1379 typedict = PyTuple_GetItem(args, 2); 1298 /* create the new instance (which is a class,
1380 if (!typedict) 1299 since we are a metatype!) */
1381 return NULL; 1300 result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
1382 1301 if (result == NULL)
1383 proto = PyDict_GetItemString(typedict, "_length_"); /* Borrowed ref */ 1302 return NULL;
1384 if (!proto || !PyInt_Check(proto)) { 1303
1304 /* Initialize these variables to NULL so that we can simplify error
1305 handling by using Py_XDECREF. */
1306 stgdict = NULL;
1307 type_attr = NULL;
1308
1309 length_attr = PyObject_GetAttrString((PyObject *)result, "_length_");
1310 if (!length_attr || !PyLong_Check(length_attr)) {
1385 PyErr_SetString(PyExc_AttributeError, 1311 PyErr_SetString(PyExc_AttributeError,
1386 "class must define a '_length_' attribute, " 1312 "class must define a '_length_' attribute, "
1387 "which must be a positive integer"); 1313 "which must be a positive integer");
1388 return NULL; 1314 Py_XDECREF(length_attr);
1389 } 1315 goto error;
1390 length = PyInt_AS_LONG(proto); 1316 }
1391 1317 length = PyLong_AsLongAndOverflow(length_attr, &overflow);
1392 proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */ 1318 if (overflow) {
1393 if (!proto) { 1319 PyErr_SetString(PyExc_OverflowError,
1320 "The '_length_' attribute is too large");
1321 Py_DECREF(length_attr);
1322 goto error;
1323 }
1324 Py_DECREF(length_attr);
1325
1326 type_attr = PyObject_GetAttrString((PyObject *)result, "_type_");
1327 if (!type_attr) {
1394 PyErr_SetString(PyExc_AttributeError, 1328 PyErr_SetString(PyExc_AttributeError,
1395 "class must define a '_type_' attribute"); 1329 "class must define a '_type_' attribute");
1396 return NULL; 1330 goto error;
1397 } 1331 }
1398 1332
1399 stgdict = (StgDictObject *)PyObject_CallObject( 1333 stgdict = (StgDictObject *)PyObject_CallObject(
1400 (PyObject *)&PyCStgDict_Type, NULL); 1334 (PyObject *)&PyCStgDict_Type, NULL);
1401 if (!stgdict) 1335 if (!stgdict)
1402 return NULL; 1336 goto error;
1403 1337
1404 itemdict = PyType_stgdict(proto); 1338 itemdict = PyType_stgdict(type_attr);
1405 if (!itemdict) { 1339 if (!itemdict) {
1406 PyErr_SetString(PyExc_TypeError, 1340 PyErr_SetString(PyExc_TypeError,
1407 "_type_ must have storage info"); 1341 "_type_ must have storage info");
1408 Py_DECREF((PyObject *)stgdict); 1342 goto error;
1409 return NULL;
1410 } 1343 }
1411 1344
1412 assert(itemdict->format); 1345 assert(itemdict->format);
1413 stgdict->format = _ctypes_alloc_format_string(NULL, itemdict->format); 1346 stgdict->format = _ctypes_alloc_format_string(NULL, itemdict->format);
1414 if (stgdict->format == NULL) { 1347 if (stgdict->format == NULL)
1415 Py_DECREF((PyObject *)stgdict); 1348 goto error;
1416 return NULL;
1417 }
1418 stgdict->ndim = itemdict->ndim + 1; 1349 stgdict->ndim = itemdict->ndim + 1;
1419 stgdict->shape = PyMem_Malloc(sizeof(Py_ssize_t *) * stgdict->ndim); 1350 stgdict->shape = PyMem_Malloc(sizeof(Py_ssize_t) * stgdict->ndim);
1420 if (stgdict->shape == NULL) { 1351 if (stgdict->shape == NULL) {
1421 Py_DECREF((PyObject *)stgdict); 1352 PyErr_NoMemory();
1422 return NULL; 1353 goto error;
1423 } 1354 }
1424 stgdict->shape[0] = length; 1355 stgdict->shape[0] = length;
1425 memmove(&stgdict->shape[1], itemdict->shape, 1356 memmove(&stgdict->shape[1], itemdict->shape,
1426 sizeof(Py_ssize_t) * (stgdict->ndim - 1)); 1357 sizeof(Py_ssize_t) * (stgdict->ndim - 1));
1427 1358
1428 itemsize = itemdict->size; 1359 itemsize = itemdict->size;
1429 if (length * itemsize < 0) { 1360 if (length * itemsize < 0) {
1430 PyErr_SetString(PyExc_OverflowError, 1361 PyErr_SetString(PyExc_OverflowError,
1431 "array too large"); 1362 "array too large");
1432 return NULL; 1363 goto error;
1433 } 1364 }
1434 1365
1435 itemalign = itemdict->align; 1366 itemalign = itemdict->align;
1436 1367
1437 if (itemdict->flags & (TYPEFLAG_ISPOINTER | TYPEFLAG_HASPOINTER)) 1368 if (itemdict->flags & (TYPEFLAG_ISPOINTER | TYPEFLAG_HASPOINTER))
1438 stgdict->flags |= TYPEFLAG_HASPOINTER; 1369 stgdict->flags |= TYPEFLAG_HASPOINTER;
1439 1370
1440 stgdict->size = itemsize * length; 1371 stgdict->size = itemsize * length;
1441 stgdict->align = itemalign; 1372 stgdict->align = itemalign;
1442 stgdict->length = length; 1373 stgdict->length = length;
1443 Py_INCREF(proto); 1374 stgdict->proto = type_attr;
1444 stgdict->proto = proto;
1445 1375
1446 stgdict->paramfunc = &PyCArrayType_paramfunc; 1376 stgdict->paramfunc = &PyCArrayType_paramfunc;
1447 1377
1448 /* Arrays are passed as pointers to function calls. */ 1378 /* Arrays are passed as pointers to function calls. */
1449 stgdict->ffi_type_pointer = ffi_type_pointer; 1379 stgdict->ffi_type_pointer = ffi_type_pointer;
1450 1380
1451 /* create the new instance (which is a class,
1452 since we are a metatype!) */
1453 result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
1454 if (result == NULL)
1455 return NULL;
1456
1457 /* replace the class dict by our updated spam dict */ 1381 /* replace the class dict by our updated spam dict */
1458 if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) { 1382 if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict))
1459 Py_DECREF(result); 1383 goto error;
1460 Py_DECREF((PyObject *)stgdict);
1461 return NULL;
1462 }
1463 Py_DECREF(result->tp_dict); 1384 Py_DECREF(result->tp_dict);
1464 result->tp_dict = (PyObject *)stgdict; 1385 result->tp_dict = (PyObject *)stgdict; /* steal the reference */
1386 stgdict = NULL;
1465 1387
1466 /* Special case for character arrays. 1388 /* Special case for character arrays.
1467 A permanent annoyance: char arrays are also strings! 1389 A permanent annoyance: char arrays are also strings!
1468 */ 1390 */
1469 if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) { 1391 if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
1470 if (-1 == add_getset(result, CharArray_getsets)) 1392 if (-1 == add_getset(result, CharArray_getsets))
1471 return NULL; 1393 goto error;
1472 #ifdef CTYPES_UNICODE 1394 #ifdef CTYPES_UNICODE
1473 } else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { 1395 } else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
1474 if (-1 == add_getset(result, WCharArray_getsets)) 1396 if (-1 == add_getset(result, WCharArray_getsets))
1475 return NULL; 1397 goto error;
1476 #endif 1398 #endif
1477 } 1399 }
1478 1400
1479 return (PyObject *)result; 1401 return (PyObject *)result;
1402 error:
1403 Py_XDECREF((PyObject*)stgdict);
1404 Py_XDECREF(type_attr);
1405 Py_DECREF(result);
1406 return NULL;
1480 } 1407 }
1481 1408
1482 PyTypeObject PyCArrayType_Type = { 1409 PyTypeObject PyCArrayType_Type = {
1483 PyVarObject_HEAD_INIT(NULL, 0) 1410 PyVarObject_HEAD_INIT(NULL, 0)
1484 "_ctypes.PyCArrayType", /* tp_name */ 1411 "_ctypes.PyCArrayType", /* tp_name */
1485 0, /* tp_basicsize */ 1412 0, /* tp_basicsize */
1486 0, /* tp_itemsize */ 1413 0, /* tp_itemsize */
1487 0, /* tp_dealloc */ 1414 0, /* tp_dealloc */
1488 0, /* tp_print */ 1415 0, /* tp_print */
1489 0, /* tp_getattr */ 1416 0, /* tp_getattr */
1490 0, /* tp_setattr */ 1417 0, /* tp_setattr */
1491 0, /* tp_compare */ 1418 0, /* tp_reserved */
1492 0, /* tp_repr */ 1419 0, /* tp_repr */
1493 0, /* tp_as_number */ 1420 0, /* tp_as_number */
1494 &CDataType_as_sequence, /* tp_as_sequence */ 1421 &CDataType_as_sequence, /* tp_as_sequence */
1495 0, /* tp_as_mapping */ 1422 0, /* tp_as_mapping */
1496 0, /* tp_hash */ 1423 0, /* tp_hash */
1497 0, /* tp_call */ 1424 0, /* tp_call */
1498 0, /* tp_str */ 1425 0, /* tp_str */
1499 0, /* tp_getattro */ 1426 0, /* tp_getattro */
1500 0, /* tp_setattro */ 1427 0, /* tp_setattro */
1501 0, /* tp_as_buffer */ 1428 0, /* tp_as_buffer */
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1533 1460
1534 */ 1461 */
1535 1462
1536 static char *SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOv?g"; 1463 static char *SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOv?g";
1537 1464
1538 static PyObject * 1465 static PyObject *
1539 c_wchar_p_from_param(PyObject *type, PyObject *value) 1466 c_wchar_p_from_param(PyObject *type, PyObject *value)
1540 { 1467 {
1541 PyObject *as_parameter; 1468 PyObject *as_parameter;
1542 int res; 1469 int res;
1543 #if (PYTHON_API_VERSION < 1012)
1544 # error not supported
1545 #endif
1546 if (value == Py_None) { 1470 if (value == Py_None) {
1547 Py_INCREF(Py_None); 1471 Py_INCREF(Py_None);
1548 return Py_None; 1472 return Py_None;
1549 } 1473 }
1550 if (PyUnicode_Check(value) || PyString_Check(value)) { 1474 if (PyUnicode_Check(value) || PyBytes_Check(value)) {
1551 PyCArgObject *parg; 1475 PyCArgObject *parg;
1552 struct fielddesc *fd = _ctypes_get_fielddesc("Z"); 1476 struct fielddesc *fd = _ctypes_get_fielddesc("Z");
1553 1477
1554 parg = PyCArgObject_new(); 1478 parg = PyCArgObject_new();
1555 if (parg == NULL) 1479 if (parg == NULL)
1556 return NULL; 1480 return NULL;
1557 parg->pffi_type = &ffi_type_pointer; 1481 parg->pffi_type = &ffi_type_pointer;
1558 parg->tag = 'Z'; 1482 parg->tag = 'Z';
1559 parg->obj = fd->setfunc(&parg->value, value, 0); 1483 parg->obj = fd->setfunc(&parg->value, value, 0);
1560 if (parg->obj == NULL) { 1484 if (parg->obj == NULL) {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1601 PyErr_SetString(PyExc_TypeError, 1525 PyErr_SetString(PyExc_TypeError,
1602 "wrong type"); 1526 "wrong type");
1603 return NULL; 1527 return NULL;
1604 } 1528 }
1605 1529
1606 static PyObject * 1530 static PyObject *
1607 c_char_p_from_param(PyObject *type, PyObject *value) 1531 c_char_p_from_param(PyObject *type, PyObject *value)
1608 { 1532 {
1609 PyObject *as_parameter; 1533 PyObject *as_parameter;
1610 int res; 1534 int res;
1611 #if (PYTHON_API_VERSION < 1012)
1612 # error not supported
1613 #endif
1614 if (value == Py_None) { 1535 if (value == Py_None) {
1615 Py_INCREF(Py_None); 1536 Py_INCREF(Py_None);
1616 return Py_None; 1537 return Py_None;
1617 } 1538 }
1618 if (PyString_Check(value) || PyUnicode_Check(value)) { 1539 if (PyBytes_Check(value)) {
1619 PyCArgObject *parg; 1540 PyCArgObject *parg;
1620 struct fielddesc *fd = _ctypes_get_fielddesc("z"); 1541 struct fielddesc *fd = _ctypes_get_fielddesc("z");
1621 1542
1622 parg = PyCArgObject_new(); 1543 parg = PyCArgObject_new();
1623 if (parg == NULL) 1544 if (parg == NULL)
1624 return NULL; 1545 return NULL;
1625 parg->pffi_type = &ffi_type_pointer; 1546 parg->pffi_type = &ffi_type_pointer;
1626 parg->tag = 'z'; 1547 parg->tag = 'z';
1627 parg->obj = fd->setfunc(&parg->value, value, 0); 1548 parg->obj = fd->setfunc(&parg->value, value, 0);
1628 if (parg->obj == NULL) { 1549 if (parg->obj == NULL) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1670 "wrong type"); 1591 "wrong type");
1671 return NULL; 1592 return NULL;
1672 } 1593 }
1673 1594
1674 static PyObject * 1595 static PyObject *
1675 c_void_p_from_param(PyObject *type, PyObject *value) 1596 c_void_p_from_param(PyObject *type, PyObject *value)
1676 { 1597 {
1677 StgDictObject *stgd; 1598 StgDictObject *stgd;
1678 PyObject *as_parameter; 1599 PyObject *as_parameter;
1679 int res; 1600 int res;
1680 #if (PYTHON_API_VERSION < 1012)
1681 # error not supported
1682 #endif
1683 1601
1684 /* None */ 1602 /* None */
1685 if (value == Py_None) { 1603 if (value == Py_None) {
1686 Py_INCREF(Py_None); 1604 Py_INCREF(Py_None);
1687 return Py_None; 1605 return Py_None;
1688 } 1606 }
1689 /* Should probably allow buffer interface as well */ 1607 /* Should probably allow buffer interface as well */
1690 /* int, long */ 1608 /* int, long */
1691 if (PyInt_Check(value) || PyLong_Check(value)) { 1609 if (PyLong_Check(value)) {
1692 PyCArgObject *parg; 1610 PyCArgObject *parg;
1693 struct fielddesc *fd = _ctypes_get_fielddesc("P"); 1611 struct fielddesc *fd = _ctypes_get_fielddesc("P");
1694 1612
1695 parg = PyCArgObject_new(); 1613 parg = PyCArgObject_new();
1696 if (parg == NULL) 1614 if (parg == NULL)
1697 return NULL; 1615 return NULL;
1698 parg->pffi_type = &ffi_type_pointer; 1616 parg->pffi_type = &ffi_type_pointer;
1699 parg->tag = 'P'; 1617 parg->tag = 'P';
1700 parg->obj = fd->setfunc(&parg->value, value, 0); 1618 parg->obj = fd->setfunc(&parg->value, value, 0);
1701 if (parg->obj == NULL) { 1619 if (parg->obj == NULL) {
1702 Py_DECREF(parg); 1620 Py_DECREF(parg);
1703 return NULL; 1621 return NULL;
1704 } 1622 }
1705 return (PyObject *)parg; 1623 return (PyObject *)parg;
1706 } 1624 }
1625 /* XXX struni: remove later */
1707 /* string */ 1626 /* string */
1708 if (PyString_Check(value)) { 1627 if (PyBytes_Check(value)) {
1628 PyCArgObject *parg;
1629 struct fielddesc *fd = _ctypes_get_fielddesc("z");
1630
1631 parg = PyCArgObject_new();
1632 if (parg == NULL)
1633 return NULL;
1634 parg->pffi_type = &ffi_type_pointer;
1635 parg->tag = 'z';
1636 parg->obj = fd->setfunc(&parg->value, value, 0);
1637 if (parg->obj == NULL) {
1638 Py_DECREF(parg);
1639 return NULL;
1640 }
1641 return (PyObject *)parg;
1642 }
1643 /* bytes */
1644 if (PyByteArray_Check(value)) {
1709 PyCArgObject *parg; 1645 PyCArgObject *parg;
1710 struct fielddesc *fd = _ctypes_get_fielddesc("z"); 1646 struct fielddesc *fd = _ctypes_get_fielddesc("z");
1711 1647
1712 parg = PyCArgObject_new(); 1648 parg = PyCArgObject_new();
1713 if (parg == NULL) 1649 if (parg == NULL)
1714 return NULL; 1650 return NULL;
1715 parg->pffi_type = &ffi_type_pointer; 1651 parg->pffi_type = &ffi_type_pointer;
1716 parg->tag = 'z'; 1652 parg->tag = 'z';
1717 parg->obj = fd->setfunc(&parg->value, value, 0); 1653 parg->obj = fd->setfunc(&parg->value, value, 0);
1718 if (parg->obj == NULL) { 1654 if (parg->obj == NULL) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1772 return NULL; 1708 return NULL;
1773 parg->pffi_type = &ffi_type_pointer; 1709 parg->pffi_type = &ffi_type_pointer;
1774 parg->tag = 'P'; 1710 parg->tag = 'P';
1775 Py_INCREF(value); 1711 Py_INCREF(value);
1776 parg->value.p = *(void **)func->b_ptr; 1712 parg->value.p = *(void **)func->b_ptr;
1777 parg->obj = value; 1713 parg->obj = value;
1778 return (PyObject *)parg; 1714 return (PyObject *)parg;
1779 } 1715 }
1780 /* c_char_p, c_wchar_p */ 1716 /* c_char_p, c_wchar_p */
1781 stgd = PyObject_stgdict(value); 1717 stgd = PyObject_stgdict(value);
1782 if (stgd && CDataObject_Check(value) && stgd->proto && PyString_Check(stgd-> proto)) { 1718 if (stgd && CDataObject_Check(value) && stgd->proto && PyUnicode_Check(stgd- >proto)) {
1783 PyCArgObject *parg; 1719 PyCArgObject *parg;
1784 1720
1785 switch (PyString_AS_STRING(stgd->proto)[0]) { 1721 switch (_PyUnicode_AsString(stgd->proto)[0]) {
1786 case 'z': /* c_char_p */ 1722 case 'z': /* c_char_p */
1787 case 'Z': /* c_wchar_p */ 1723 case 'Z': /* c_wchar_p */
1788 parg = PyCArgObject_new(); 1724 parg = PyCArgObject_new();
1789 if (parg == NULL) 1725 if (parg == NULL)
1790 return NULL; 1726 return NULL;
1791 parg->pffi_type = &ffi_type_pointer; 1727 parg->pffi_type = &ffi_type_pointer;
1792 parg->tag = 'Z'; 1728 parg->tag = 'Z';
1793 Py_INCREF(value); 1729 Py_INCREF(value);
1794 parg->obj = value; 1730 parg->obj = value;
1795 /* Remember: b_ptr points to where the pointer is stored! */ 1731 /* Remember: b_ptr points to where the pointer is stored! */
1796 parg->value.p = *(void **)(((CDataObject *)value)->b_ptr); 1732 parg->value.p = *(void **)(((CDataObject *)value)->b_ptr);
1797 return (PyObject *)parg; 1733 return (PyObject *)parg;
1798 } 1734 }
1799 } 1735 }
1800 1736
1801 as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); 1737 as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
1802 if (as_parameter) { 1738 if (as_parameter) {
1803 value = c_void_p_from_param(type, as_parameter); 1739 value = c_void_p_from_param(type, as_parameter);
1804 Py_DECREF(as_parameter); 1740 Py_DECREF(as_parameter);
1805 return value; 1741 return value;
1806 } 1742 }
1807 /* XXX better message */ 1743 /* XXX better message */
1808 PyErr_SetString(PyExc_TypeError, 1744 PyErr_SetString(PyExc_TypeError,
1809 "wrong type"); 1745 "wrong type");
1810 return NULL; 1746 return NULL;
1811 } 1747 }
1812 #if (PYTHON_API_VERSION >= 1012)
1813 1748
1814 static PyMethodDef c_void_p_method = { "from_param", c_void_p_from_param, METH_O }; 1749 static PyMethodDef c_void_p_method = { "from_param", c_void_p_from_param, METH_O };
1815 static PyMethodDef c_char_p_method = { "from_param", c_char_p_from_param, METH_O }; 1750 static PyMethodDef c_char_p_method = { "from_param", c_char_p_from_param, METH_O };
1816 static PyMethodDef c_wchar_p_method = { "from_param", c_wchar_p_from_param, METH _O }; 1751 static PyMethodDef c_wchar_p_method = { "from_param", c_wchar_p_from_param, METH _O };
1817 1752
1818 #else
1819 #error
1820 static PyMethodDef c_void_p_method = { "from_param", c_void_p_from_param, METH_V ARARGS };
1821 static PyMethodDef c_char_p_method = { "from_param", c_char_p_from_param, METH_V ARARGS };
1822 static PyMethodDef c_wchar_p_method = { "from_param", c_wchar_p_from_param, METH _VARARGS };
1823
1824 #endif
1825
1826 static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject *kwds, 1753 static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject *kwds,
1827 PyObject *proto, struct fielddesc *fmt) 1754 PyObject *proto, struct fielddesc *fmt)
1828 { 1755 {
1829 PyTypeObject *result; 1756 PyTypeObject *result;
1830 StgDictObject *stgdict; 1757 StgDictObject *stgdict;
1831 PyObject *name = PyTuple_GET_ITEM(args, 0); 1758 PyObject *name = PyTuple_GET_ITEM(args, 0);
1759 PyObject *newname;
1832 PyObject *swapped_args; 1760 PyObject *swapped_args;
1833 static PyObject *suffix; 1761 static PyObject *suffix;
1834 Py_ssize_t i; 1762 Py_ssize_t i;
1835 1763
1836 swapped_args = PyTuple_New(PyTuple_GET_SIZE(args)); 1764 swapped_args = PyTuple_New(PyTuple_GET_SIZE(args));
1837 if (!swapped_args) 1765 if (!swapped_args)
1838 return NULL; 1766 return NULL;
1839 1767
1840 if (suffix == NULL) 1768 if (suffix == NULL)
1841 #ifdef WORDS_BIGENDIAN 1769 #ifdef WORDS_BIGENDIAN
1842 suffix = PyString_InternFromString("_le"); 1770 suffix = PyUnicode_InternFromString("_le");
1843 #else 1771 #else
1844 suffix = PyString_InternFromString("_be"); 1772 suffix = PyUnicode_InternFromString("_be");
1845 #endif 1773 #endif
1846 1774
1847 Py_INCREF(name); 1775 newname = PyUnicode_Concat(name, suffix);
1848 PyString_Concat(&name, suffix); 1776 if (newname == NULL) {
1849 if (name == NULL) 1777 return NULL;
1850 return NULL; 1778 }
1851 1779
1852 PyTuple_SET_ITEM(swapped_args, 0, name); 1780 PyTuple_SET_ITEM(swapped_args, 0, newname);
1853 for (i=1; i<PyTuple_GET_SIZE(args); ++i) { 1781 for (i=1; i<PyTuple_GET_SIZE(args); ++i) {
1854 PyObject *v = PyTuple_GET_ITEM(args, i); 1782 PyObject *v = PyTuple_GET_ITEM(args, i);
1855 Py_INCREF(v); 1783 Py_INCREF(v);
1856 PyTuple_SET_ITEM(swapped_args, i, v); 1784 PyTuple_SET_ITEM(swapped_args, i, v);
1857 } 1785 }
1858 1786
1859 /* create the new instance (which is a class, 1787 /* create the new instance (which is a class,
1860 since we are a metatype!) */ 1788 since we are a metatype!) */
1861 result = (PyTypeObject *)PyType_Type.tp_new(type, swapped_args, kwds); 1789 result = (PyTypeObject *)PyType_Type.tp_new(type, swapped_args, kwds);
1862 Py_DECREF(swapped_args); 1790 Py_DECREF(swapped_args);
(...skipping 30 matching lines...) Expand all
1893 static PyCArgObject * 1821 static PyCArgObject *
1894 PyCSimpleType_paramfunc(CDataObject *self) 1822 PyCSimpleType_paramfunc(CDataObject *self)
1895 { 1823 {
1896 StgDictObject *dict; 1824 StgDictObject *dict;
1897 char *fmt; 1825 char *fmt;
1898 PyCArgObject *parg; 1826 PyCArgObject *parg;
1899 struct fielddesc *fd; 1827 struct fielddesc *fd;
1900 1828
1901 dict = PyObject_stgdict((PyObject *)self); 1829 dict = PyObject_stgdict((PyObject *)self);
1902 assert(dict); /* Cannot be NULL for CDataObject instances */ 1830 assert(dict); /* Cannot be NULL for CDataObject instances */
1903 fmt = PyString_AsString(dict->proto); 1831 fmt = _PyUnicode_AsString(dict->proto);
1904 assert(fmt); 1832 assert(fmt);
1905 1833
1906 fd = _ctypes_get_fielddesc(fmt); 1834 fd = _ctypes_get_fielddesc(fmt);
1907 assert(fd); 1835 assert(fd);
1908 1836
1909 parg = PyCArgObject_new(); 1837 parg = PyCArgObject_new();
1910 if (parg == NULL) 1838 if (parg == NULL)
1911 return NULL; 1839 return NULL;
1912 1840
1913 parg->tag = fmt[0]; 1841 parg->tag = fmt[0];
(...skipping 23 matching lines...) Expand all
1937 1865
1938 proto = PyObject_GetAttrString((PyObject *)result, "_type_"); /* new ref */ 1866 proto = PyObject_GetAttrString((PyObject *)result, "_type_"); /* new ref */
1939 if (!proto) { 1867 if (!proto) {
1940 PyErr_SetString(PyExc_AttributeError, 1868 PyErr_SetString(PyExc_AttributeError,
1941 "class must define a '_type_' attribute"); 1869 "class must define a '_type_' attribute");
1942 error: 1870 error:
1943 Py_XDECREF(proto); 1871 Py_XDECREF(proto);
1944 Py_XDECREF(result); 1872 Py_XDECREF(result);
1945 return NULL; 1873 return NULL;
1946 } 1874 }
1947 if (PyString_Check(proto)) { 1875 if (PyUnicode_Check(proto)) {
1948 proto_str = PyString_AS_STRING(proto); 1876 proto_str = PyUnicode_AsUTF8AndSize(proto, &proto_len);
1949 proto_len = PyString_GET_SIZE(proto); 1877 if (!proto_str)
1878 goto error;
1950 } else { 1879 } else {
1951 PyErr_SetString(PyExc_TypeError, 1880 PyErr_SetString(PyExc_TypeError,
1952 "class must define a '_type_' string attribute"); 1881 "class must define a '_type_' string attribute");
1953 goto error; 1882 goto error;
1954 } 1883 }
1955 if (proto_len != 1) { 1884 if (proto_len != 1) {
1956 PyErr_SetString(PyExc_ValueError, 1885 PyErr_SetString(PyExc_ValueError,
1957 "class must define a '_type_' attribute " 1886 "class must define a '_type_' attribute "
1958 "which must be a string of length 1"); 1887 "which must be a string of length 1");
1959 goto error; 1888 goto error;
1960 } 1889 }
1961 if (!strchr(SIMPLE_TYPE_CHARS, *proto_str)) { 1890 if (!strchr(SIMPLE_TYPE_CHARS, *proto_str)) {
1962 PyErr_Format(PyExc_AttributeError, 1891 PyErr_Format(PyExc_AttributeError,
1963 "class must define a '_type_' attribute which must be\n" 1892 "class must define a '_type_' attribute which must be\n"
1964 "a single character string containing one of '%s'.", 1893 "a single character string containing one of '%s'.",
1965 SIMPLE_TYPE_CHARS); 1894 SIMPLE_TYPE_CHARS);
1966 goto error; 1895 goto error;
1967 } 1896 }
1968 fmt = _ctypes_get_fielddesc(PyString_AS_STRING(proto)); 1897 fmt = _ctypes_get_fielddesc(proto_str);
1969 if (fmt == NULL) { 1898 if (fmt == NULL) {
1970 PyErr_Format(PyExc_ValueError, 1899 PyErr_Format(PyExc_ValueError,
1971 "_type_ '%s' not supported", 1900 "_type_ '%s' not supported", proto_str);
1972 PyString_AS_STRING(proto));
1973 goto error; 1901 goto error;
1974 } 1902 }
1975 1903
1976 stgdict = (StgDictObject *)PyObject_CallObject( 1904 stgdict = (StgDictObject *)PyObject_CallObject(
1977 (PyObject *)&PyCStgDict_Type, NULL); 1905 (PyObject *)&PyCStgDict_Type, NULL);
1978 if (!stgdict) 1906 if (!stgdict)
1979 goto error; 1907 goto error;
1980 1908
1981 stgdict->ffi_type_pointer = *fmt->pffi_type; 1909 stgdict->ffi_type_pointer = *fmt->pffi_type;
1982 stgdict->align = fmt->pffi_type->alignment; 1910 stgdict->align = fmt->pffi_type->alignment;
(...skipping 30 matching lines...) Expand all
2013 Py_DECREF((PyObject *)stgdict); 1941 Py_DECREF((PyObject *)stgdict);
2014 return NULL; 1942 return NULL;
2015 } 1943 }
2016 Py_DECREF(result->tp_dict); 1944 Py_DECREF(result->tp_dict);
2017 result->tp_dict = (PyObject *)stgdict; 1945 result->tp_dict = (PyObject *)stgdict;
2018 1946
2019 /* Install from_param class methods in ctypes base classes. 1947 /* Install from_param class methods in ctypes base classes.
2020 Overrides the PyCSimpleType_from_param generic method. 1948 Overrides the PyCSimpleType_from_param generic method.
2021 */ 1949 */
2022 if (result->tp_base == &Simple_Type) { 1950 if (result->tp_base == &Simple_Type) {
2023 switch (PyString_AS_STRING(proto)[0]) { 1951 switch (*proto_str) {
2024 case 'z': /* c_char_p */ 1952 case 'z': /* c_char_p */
2025 ml = &c_char_p_method; 1953 ml = &c_char_p_method;
2026 stgdict->flags |= TYPEFLAG_ISPOINTER; 1954 stgdict->flags |= TYPEFLAG_ISPOINTER;
2027 break; 1955 break;
2028 case 'Z': /* c_wchar_p */ 1956 case 'Z': /* c_wchar_p */
2029 ml = &c_wchar_p_method; 1957 ml = &c_wchar_p_method;
2030 stgdict->flags |= TYPEFLAG_ISPOINTER; 1958 stgdict->flags |= TYPEFLAG_ISPOINTER;
2031 break; 1959 break;
2032 case 'P': /* c_void_p */ 1960 case 'P': /* c_void_p */
2033 ml = &c_void_p_method; 1961 ml = &c_void_p_method;
2034 stgdict->flags |= TYPEFLAG_ISPOINTER; 1962 stgdict->flags |= TYPEFLAG_ISPOINTER;
2035 break; 1963 break;
2036 case 's': 1964 case 's':
2037 case 'X': 1965 case 'X':
2038 case 'O': 1966 case 'O':
2039 ml = NULL; 1967 ml = NULL;
2040 stgdict->flags |= TYPEFLAG_ISPOINTER; 1968 stgdict->flags |= TYPEFLAG_ISPOINTER;
2041 break; 1969 break;
2042 default: 1970 default:
2043 ml = NULL; 1971 ml = NULL;
2044 break; 1972 break;
2045 } 1973 }
2046 1974
2047 if (ml) { 1975 if (ml) {
2048 #if (PYTHON_API_VERSION >= 1012)
2049 PyObject *meth; 1976 PyObject *meth;
2050 int x; 1977 int x;
2051 meth = PyDescr_NewClassMethod(result, ml); 1978 meth = PyDescr_NewClassMethod(result, ml);
2052 if (!meth) 1979 if (!meth)
2053 return NULL; 1980 return NULL;
2054 #else
2055 #error
2056 PyObject *meth, *func;
2057 int x;
2058 func = PyCFunction_New(ml, NULL);
2059 if (!func)
2060 return NULL;
2061 meth = PyObject_CallFunctionObjArgs(
2062 (PyObject *)&PyClassMethod_Type,
2063 func, NULL);
2064 Py_DECREF(func);
2065 if (!meth) {
2066 return NULL;
2067 }
2068 #endif
2069 x = PyDict_SetItemString(result->tp_dict, 1981 x = PyDict_SetItemString(result->tp_dict,
2070 ml->ml_name, 1982 ml->ml_name,
2071 meth); 1983 meth);
2072 Py_DECREF(meth); 1984 Py_DECREF(meth);
2073 if (x == -1) { 1985 if (x == -1) {
2074 Py_DECREF(result); 1986 Py_DECREF(result);
2075 return NULL; 1987 return NULL;
2076 } 1988 }
2077 } 1989 }
2078 } 1990 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2132 return NULL; 2044 return NULL;
2133 if (res) { 2045 if (res) {
2134 Py_INCREF(value); 2046 Py_INCREF(value);
2135 return value; 2047 return value;
2136 } 2048 }
2137 2049
2138 dict = PyType_stgdict(type); 2050 dict = PyType_stgdict(type);
2139 assert(dict); 2051 assert(dict);
2140 2052
2141 /* I think we can rely on this being a one-character string */ 2053 /* I think we can rely on this being a one-character string */
2142 fmt = PyString_AsString(dict->proto); 2054 fmt = _PyUnicode_AsString(dict->proto);
2143 assert(fmt); 2055 assert(fmt);
2144 2056
2145 fd = _ctypes_get_fielddesc(fmt); 2057 fd = _ctypes_get_fielddesc(fmt);
2146 assert(fd); 2058 assert(fd);
2147 2059
2148 parg = PyCArgObject_new(); 2060 parg = PyCArgObject_new();
2149 if (parg == NULL) 2061 if (parg == NULL)
2150 return NULL; 2062 return NULL;
2151 2063
2152 parg->tag = fmt[0]; 2064 parg->tag = fmt[0];
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
2184 2096
2185 PyTypeObject PyCSimpleType_Type = { 2097 PyTypeObject PyCSimpleType_Type = {
2186 PyVarObject_HEAD_INIT(NULL, 0) 2098 PyVarObject_HEAD_INIT(NULL, 0)
2187 "_ctypes.PyCSimpleType", /* tp_name */ 2099 "_ctypes.PyCSimpleType", /* tp_name */
2188 0, /* tp_basicsize */ 2100 0, /* tp_basicsize */
2189 0, /* tp_itemsize */ 2101 0, /* tp_itemsize */
2190 0, /* tp_dealloc */ 2102 0, /* tp_dealloc */
2191 0, /* tp_print */ 2103 0, /* tp_print */
2192 0, /* tp_getattr */ 2104 0, /* tp_getattr */
2193 0, /* tp_setattr */ 2105 0, /* tp_setattr */
2194 0, /* tp_compare */ 2106 0, /* tp_reserved */
2195 0, /* tp_repr */ 2107 0, /* tp_repr */
2196 0, /* tp_as_number */ 2108 0, /* tp_as_number */
2197 &CDataType_as_sequence, /* tp_as_sequence */ 2109 &CDataType_as_sequence, /* tp_as_sequence */
2198 0, /* tp_as_mapping */ 2110 0, /* tp_as_mapping */
2199 0, /* tp_hash */ 2111 0, /* tp_hash */
2200 0, /* tp_call */ 2112 0, /* tp_call */
2201 0, /* tp_str */ 2113 0, /* tp_str */
2202 0, /* tp_getattro */ 2114 0, /* tp_getattro */
2203 0, /* tp_setattro */ 2115 0, /* tp_setattro */
2204 0, /* tp_as_buffer */ 2116 0, /* tp_as_buffer */
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
2260 goto argtypes_error_1; 2172 goto argtypes_error_1;
2261 PyTuple_SET_ITEM(converters, i, cnv); 2173 PyTuple_SET_ITEM(converters, i, cnv);
2262 } 2174 }
2263 Py_DECREF(ob); 2175 Py_DECREF(ob);
2264 return converters; 2176 return converters;
2265 2177
2266 argtypes_error_1: 2178 argtypes_error_1:
2267 Py_XDECREF(converters); 2179 Py_XDECREF(converters);
2268 Py_DECREF(ob); 2180 Py_DECREF(ob);
2269 PyErr_Format(PyExc_TypeError, 2181 PyErr_Format(PyExc_TypeError,
2270 #if (PY_VERSION_HEX < 0x02050000)
2271 "item %d in _argtypes_ has no from_param method",
2272 #else
2273 "item %zd in _argtypes_ has no from_param method", 2182 "item %zd in _argtypes_ has no from_param method",
2274 #endif
2275 i+1); 2183 i+1);
2276 return NULL; 2184 return NULL;
2277 } 2185 }
2278 2186
2279 static int 2187 static int
2280 make_funcptrtype_dict(StgDictObject *stgdict) 2188 make_funcptrtype_dict(StgDictObject *stgdict)
2281 { 2189 {
2282 PyObject *ob; 2190 PyObject *ob;
2283 PyObject *converters = NULL; 2191 PyObject *converters = NULL;
2284 2192
2285 stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment; 2193 stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment;
2286 stgdict->length = 1; 2194 stgdict->length = 1;
2287 stgdict->size = sizeof(void *); 2195 stgdict->size = sizeof(void *);
2288 stgdict->setfunc = NULL; 2196 stgdict->setfunc = NULL;
2289 stgdict->getfunc = NULL; 2197 stgdict->getfunc = NULL;
2290 stgdict->ffi_type_pointer = ffi_type_pointer; 2198 stgdict->ffi_type_pointer = ffi_type_pointer;
2291 2199
2292 ob = PyDict_GetItemString((PyObject *)stgdict, "_flags_"); 2200 ob = PyDict_GetItemString((PyObject *)stgdict, "_flags_");
2293 if (!ob || !PyInt_Check(ob)) { 2201 if (!ob || !PyLong_Check(ob)) {
2294 PyErr_SetString(PyExc_TypeError, 2202 PyErr_SetString(PyExc_TypeError,
2295 "class must define _flags_ which must be an integer"); 2203 "class must define _flags_ which must be an integer");
2296 return -1; 2204 return -1;
2297 } 2205 }
2298 stgdict->flags = PyInt_AS_LONG(ob) | TYPEFLAG_ISPOINTER; 2206 stgdict->flags = PyLong_AS_LONG(ob) | TYPEFLAG_ISPOINTER;
2299 2207
2300 /* _argtypes_ is optional... */ 2208 /* _argtypes_ is optional... */
2301 ob = PyDict_GetItemString((PyObject *)stgdict, "_argtypes_"); 2209 ob = PyDict_GetItemString((PyObject *)stgdict, "_argtypes_");
2302 if (ob) { 2210 if (ob) {
2303 converters = converters_from_argtypes(ob); 2211 converters = converters_from_argtypes(ob);
2304 if (!converters) 2212 if (!converters)
2305 goto error; 2213 goto error;
2306 Py_INCREF(ob); 2214 Py_INCREF(ob);
2307 stgdict->argtypes = ob; 2215 stgdict->argtypes = ob;
2308 stgdict->converters = converters; 2216 stgdict->converters = converters;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
2370 return NULL; 2278 return NULL;
2371 2279
2372 stgdict->paramfunc = PyCFuncPtrType_paramfunc; 2280 stgdict->paramfunc = PyCFuncPtrType_paramfunc;
2373 /* We do NOT expose the function signature in the format string. It 2281 /* We do NOT expose the function signature in the format string. It
2374 is impossible, generally, because the only requirement for the 2282 is impossible, generally, because the only requirement for the
2375 argtypes items is that they have a .from_param method - we do not 2283 argtypes items is that they have a .from_param method - we do not
2376 know the types of the arguments (although, in practice, most 2284 know the types of the arguments (although, in practice, most
2377 argtypes would be a ctypes type). 2285 argtypes would be a ctypes type).
2378 */ 2286 */
2379 stgdict->format = _ctypes_alloc_format_string(NULL, "X{}"); 2287 stgdict->format = _ctypes_alloc_format_string(NULL, "X{}");
2288 if (stgdict->format == NULL) {
2289 Py_DECREF((PyObject *)stgdict);
2290 return NULL;
2291 }
2380 stgdict->flags |= TYPEFLAG_ISPOINTER; 2292 stgdict->flags |= TYPEFLAG_ISPOINTER;
2381 2293
2382 /* create the new instance (which is a class, 2294 /* create the new instance (which is a class,
2383 since we are a metatype!) */ 2295 since we are a metatype!) */
2384 result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds); 2296 result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
2385 if (result == NULL) { 2297 if (result == NULL) {
2386 Py_DECREF((PyObject *)stgdict); 2298 Py_DECREF((PyObject *)stgdict);
2387 return NULL; 2299 return NULL;
2388 } 2300 }
2389 2301
(...skipping 16 matching lines...) Expand all
2406 2318
2407 PyTypeObject PyCFuncPtrType_Type = { 2319 PyTypeObject PyCFuncPtrType_Type = {
2408 PyVarObject_HEAD_INIT(NULL, 0) 2320 PyVarObject_HEAD_INIT(NULL, 0)
2409 "_ctypes.PyCFuncPtrType", /* tp_name */ 2321 "_ctypes.PyCFuncPtrType", /* tp_name */
2410 0, /* tp_basicsize */ 2322 0, /* tp_basicsize */
2411 0, /* tp_itemsize */ 2323 0, /* tp_itemsize */
2412 0, /* tp_dealloc */ 2324 0, /* tp_dealloc */
2413 0, /* tp_print */ 2325 0, /* tp_print */
2414 0, /* tp_getattr */ 2326 0, /* tp_getattr */
2415 0, /* tp_setattr */ 2327 0, /* tp_setattr */
2416 0, /* tp_compare */ 2328 0, /* tp_reserved */
2417 0, /* tp_repr */ 2329 0, /* tp_repr */
2418 0, /* tp_as_number */ 2330 0, /* tp_as_number */
2419 &CDataType_as_sequence, /* tp_as_sequence */ 2331 &CDataType_as_sequence, /* tp_as_sequence */
2420 0, /* tp_as_mapping */ 2332 0, /* tp_as_mapping */
2421 0, /* tp_hash */ 2333 0, /* tp_hash */
2422 0, /* tp_call */ 2334 0, /* tp_call */
2423 0, /* tp_str */ 2335 0, /* tp_str */
2424 0, /* tp_getattro */ 2336 0, /* tp_getattro */
2425 0, /* tp_setattro */ 2337 0, /* tp_setattro */
2426 0, /* tp_as_buffer */ 2338 0, /* tp_as_buffer */
(...skipping 25 matching lines...) Expand all
2452 */ 2364 */
2453 2365
2454 static CDataObject * 2366 static CDataObject *
2455 PyCData_GetContainer(CDataObject *self) 2367 PyCData_GetContainer(CDataObject *self)
2456 { 2368 {
2457 while (self->b_base) 2369 while (self->b_base)
2458 self = self->b_base; 2370 self = self->b_base;
2459 if (self->b_objects == NULL) { 2371 if (self->b_objects == NULL) {
2460 if (self->b_length) { 2372 if (self->b_length) {
2461 self->b_objects = PyDict_New(); 2373 self->b_objects = PyDict_New();
2374 if (self->b_objects == NULL)
2375 return NULL;
2462 } else { 2376 } else {
2463 Py_INCREF(Py_None); 2377 Py_INCREF(Py_None);
2464 self->b_objects = Py_None; 2378 self->b_objects = Py_None;
2465 } 2379 }
2466 } 2380 }
2467 return self; 2381 return self;
2468 } 2382 }
2469 2383
2470 static PyObject * 2384 static PyObject *
2471 GetKeepedObjects(CDataObject *target) 2385 GetKeepedObjects(CDataObject *target)
2472 { 2386 {
2473 return PyCData_GetContainer(target)->b_objects; 2387 CDataObject *container;
2388 container = PyCData_GetContainer(target);
2389 if (container == NULL)
2390 return NULL;
2391 return container->b_objects;
2474 } 2392 }
2475 2393
2476 static PyObject * 2394 static PyObject *
2477 unique_key(CDataObject *target, Py_ssize_t index) 2395 unique_key(CDataObject *target, Py_ssize_t index)
2478 { 2396 {
2479 char string[256]; 2397 char string[256];
2480 char *cp = string; 2398 char *cp = string;
2481 size_t bytes_left; 2399 size_t bytes_left;
2482 2400
2483 assert(sizeof(string) - 1 > sizeof(Py_ssize_t) * 2); 2401 assert(sizeof(string) - 1 > sizeof(Py_ssize_t) * 2);
2484 #if (PY_VERSION_HEX < 0x02050000)
2485 cp += sprintf(cp, "%x", index);
2486 #else
2487 cp += sprintf(cp, "%x", Py_SAFE_DOWNCAST(index, Py_ssize_t, int)); 2402 cp += sprintf(cp, "%x", Py_SAFE_DOWNCAST(index, Py_ssize_t, int));
2488 #endif
2489 while (target->b_base) { 2403 while (target->b_base) {
2490 bytes_left = sizeof(string) - (cp - string) - 1; 2404 bytes_left = sizeof(string) - (cp - string) - 1;
2491 /* Hex format needs 2 characters per byte */ 2405 /* Hex format needs 2 characters per byte */
2492 if (bytes_left < sizeof(Py_ssize_t) * 2) { 2406 if (bytes_left < sizeof(Py_ssize_t) * 2) {
2493 PyErr_SetString(PyExc_ValueError, 2407 PyErr_SetString(PyExc_ValueError,
2494 "ctypes object structure too deep"); 2408 "ctypes object structure too deep");
2495 return NULL; 2409 return NULL;
2496 } 2410 }
2497 #if (PY_VERSION_HEX < 0x02050000)
2498 cp += sprintf(cp, ":%x", (int)target->b_index);
2499 #else
2500 cp += sprintf(cp, ":%x", Py_SAFE_DOWNCAST(target->b_index, Py_ssize_t, i nt)); 2411 cp += sprintf(cp, ":%x", Py_SAFE_DOWNCAST(target->b_index, Py_ssize_t, i nt));
2501 #endif
2502 target = target->b_base; 2412 target = target->b_base;
2503 } 2413 }
2504 return PyString_FromStringAndSize(string, cp-string); 2414 return PyUnicode_FromStringAndSize(string, cp-string);
2505 } 2415 }
2506 2416
2507 /* 2417 /*
2508 * Keep a reference to 'keep' in the 'target', at index 'index'. 2418 * Keep a reference to 'keep' in the 'target', at index 'index'.
2509 * 2419 *
2510 * If 'keep' is None, do nothing. 2420 * If 'keep' is None, do nothing.
2511 * 2421 *
2512 * Otherwise create a dictionary (if it does not yet exist) id the root 2422 * Otherwise create a dictionary (if it does not yet exist) id the root
2513 * objects 'b_objects' item, which will store the 'keep' object under a unique 2423 * objects 'b_objects' item, which will store the 'keep' object under a unique
2514 * key. 2424 * key.
(...skipping 13 matching lines...) Expand all
2528 int result; 2438 int result;
2529 CDataObject *ob; 2439 CDataObject *ob;
2530 PyObject *key; 2440 PyObject *key;
2531 2441
2532 /* Optimization: no need to store None */ 2442 /* Optimization: no need to store None */
2533 if (keep == Py_None) { 2443 if (keep == Py_None) {
2534 Py_DECREF(Py_None); 2444 Py_DECREF(Py_None);
2535 return 0; 2445 return 0;
2536 } 2446 }
2537 ob = PyCData_GetContainer(target); 2447 ob = PyCData_GetContainer(target);
2448 if (ob == NULL) {
2449 Py_DECREF(keep);
2450 return -1;
2451 }
2538 if (ob->b_objects == NULL || !PyDict_CheckExact(ob->b_objects)) { 2452 if (ob->b_objects == NULL || !PyDict_CheckExact(ob->b_objects)) {
2539 Py_XDECREF(ob->b_objects); 2453 Py_XDECREF(ob->b_objects);
2540 ob->b_objects = keep; /* refcount consumed */ 2454 ob->b_objects = keep; /* refcount consumed */
2541 return 0; 2455 return 0;
2542 } 2456 }
2543 key = unique_key(target, index); 2457 key = unique_key(target, index);
2544 if (key == NULL) { 2458 if (key == NULL) {
2545 Py_DECREF(keep); 2459 Py_DECREF(keep);
2546 return -1; 2460 return -1;
2547 } 2461 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
2588 "the base object" }, 2502 "the base object" },
2589 { "_b_needsfree_", T_INT, 2503 { "_b_needsfree_", T_INT,
2590 offsetof(CDataObject, b_needsfree), READONLY, 2504 offsetof(CDataObject, b_needsfree), READONLY,
2591 "whether the object owns the memory or not" }, 2505 "whether the object owns the memory or not" },
2592 { "_objects", T_OBJECT, 2506 { "_objects", T_OBJECT,
2593 offsetof(CDataObject, b_objects), READONLY, 2507 offsetof(CDataObject, b_objects), READONLY,
2594 "internal objects tree (NEVER CHANGE THIS OBJECT!)"}, 2508 "internal objects tree (NEVER CHANGE THIS OBJECT!)"},
2595 { NULL }, 2509 { NULL },
2596 }; 2510 };
2597 2511
2598 #if (PY_VERSION_HEX >= 0x02060000) 2512 static int PyCData_NewGetBuffer(PyObject *myself, Py_buffer *view, int flags)
2599 static int PyCData_NewGetBuffer(PyObject *_self, Py_buffer *view, int flags) 2513 {
2600 { 2514 CDataObject *self = (CDataObject *)myself;
2601 CDataObject *self = (CDataObject *)_self; 2515 StgDictObject *dict = PyObject_stgdict(myself);
2602 StgDictObject *dict = PyObject_stgdict(_self);
2603 Py_ssize_t i; 2516 Py_ssize_t i;
2604 2517
2605 if (view == NULL) return 0; 2518 if (view == NULL) return 0;
2606 2519
2607 view->buf = self->b_ptr; 2520 view->buf = self->b_ptr;
2608 view->obj = _self; 2521 view->obj = myself;
2609 Py_INCREF(_self); 2522 Py_INCREF(myself);
2610 view->len = self->b_size; 2523 view->len = self->b_size;
2611 view->readonly = 0; 2524 view->readonly = 0;
2612 /* use default format character if not set */ 2525 /* use default format character if not set */
2613 view->format = dict->format ? dict->format : "B"; 2526 view->format = dict->format ? dict->format : "B";
2614 view->ndim = dict->ndim; 2527 view->ndim = dict->ndim;
2615 view->shape = dict->shape; 2528 view->shape = dict->shape;
2616 view->itemsize = self->b_size; 2529 view->itemsize = self->b_size;
2617 if (view->itemsize) { 2530 if (view->itemsize) {
2618 for (i = 0; i < view->ndim; ++i) { 2531 for (i = 0; i < view->ndim; ++i) {
2619 view->itemsize /= dict->shape[i]; 2532 view->itemsize /= dict->shape[i];
2620 } 2533 }
2621 } 2534 }
2622 view->strides = NULL; 2535 view->strides = NULL;
2623 view->suboffsets = NULL; 2536 view->suboffsets = NULL;
2624 view->internal = NULL; 2537 view->internal = NULL;
2625 return 0; 2538 return 0;
2626 } 2539 }
2627 #endif
2628
2629 static Py_ssize_t PyCData_GetSegcount(PyObject *_self, Py_ssize_t *lenp)
2630 {
2631 if (lenp)
2632 *lenp = 1;
2633 return 1;
2634 }
2635
2636 static Py_ssize_t PyCData_GetBuffer(PyObject *_self, Py_ssize_t seg, void **pptr )
2637 {
2638 CDataObject *self = (CDataObject *)_self;
2639 if (seg != 0) {
2640 /* Hm. Must this set an exception? */
2641 return -1;
2642 }
2643 *pptr = self->b_ptr;
2644 return self->b_size;
2645 }
2646 2540
2647 static PyBufferProcs PyCData_as_buffer = { 2541 static PyBufferProcs PyCData_as_buffer = {
2648 (readbufferproc)PyCData_GetBuffer, 2542 PyCData_NewGetBuffer,
2649 (writebufferproc)PyCData_GetBuffer, 2543 NULL,
2650 (segcountproc)PyCData_GetSegcount,
2651 (charbufferproc)NULL,
2652 #if (PY_VERSION_HEX >= 0x02060000)
2653 (getbufferproc)PyCData_NewGetBuffer,
2654 (releasebufferproc)NULL,
2655 #endif
2656 }; 2544 };
2657 2545
2658 /* 2546 /*
2659 * CData objects are mutable, so they cannot be hashable! 2547 * CData objects are mutable, so they cannot be hashable!
2660 */ 2548 */
2661 static long 2549 static Py_hash_t
2662 PyCData_nohash(PyObject *self) 2550 PyCData_nohash(PyObject *self)
2663 { 2551 {
2664 PyErr_SetString(PyExc_TypeError, "unhashable type"); 2552 PyErr_SetString(PyExc_TypeError, "unhashable type");
2665 return -1; 2553 return -1;
2666 } 2554 }
2667 2555
2668 static PyObject * 2556 static PyObject *
2669 PyCData_reduce(PyObject *_self, PyObject *args) 2557 PyCData_reduce(PyObject *myself, PyObject *args)
2670 { 2558 {
2671 CDataObject *self = (CDataObject *)_self; 2559 CDataObject *self = (CDataObject *)myself;
2672 2560
2673 if (PyObject_stgdict(_self)->flags & (TYPEFLAG_ISPOINTER|TYPEFLAG_HASPOINTER )) { 2561 if (PyObject_stgdict(myself)->flags & (TYPEFLAG_ISPOINTER|TYPEFLAG_HASPOINTE R)) {
2674 PyErr_SetString(PyExc_ValueError, 2562 PyErr_SetString(PyExc_ValueError,
2675 "ctypes objects containing pointers cannot be pickled"); 2563 "ctypes objects containing pointers cannot be pickled");
2676 return NULL; 2564 return NULL;
2677 } 2565 }
2678 return Py_BuildValue("O(O(NN))", 2566 return Py_BuildValue("O(O(NN))",
2679 _unpickle, 2567 _unpickle,
2680 Py_TYPE(_self), 2568 Py_TYPE(myself),
2681 PyObject_GetAttrString(_self, "__dict__"), 2569 PyObject_GetAttrString(myself, "__dict__"),
2682 PyString_FromStringAndSize(self->b_ptr, self->b_size)); 2570 PyBytes_FromStringAndSize(self->b_ptr, self->b_size));
2683 } 2571 }
2684 2572
2685 static PyObject * 2573 static PyObject *
2686 PyCData_setstate(PyObject *_self, PyObject *args) 2574 PyCData_setstate(PyObject *myself, PyObject *args)
2687 { 2575 {
2688 void *data; 2576 void *data;
2689 Py_ssize_t len; 2577 Py_ssize_t len;
2690 int res; 2578 int res;
2691 PyObject *dict, *mydict; 2579 PyObject *dict, *mydict;
2692 CDataObject *self = (CDataObject *)_self; 2580 CDataObject *self = (CDataObject *)myself;
2693 if (!PyArg_ParseTuple(args, "Os#", &dict, &data, &len)) 2581 if (!PyArg_ParseTuple(args, "Os#", &dict, &data, &len))
2694 return NULL; 2582 return NULL;
2695 if (len > self->b_size) 2583 if (len > self->b_size)
2696 len = self->b_size; 2584 len = self->b_size;
2697 memmove(self->b_ptr, data, len); 2585 memmove(self->b_ptr, data, len);
2698 mydict = PyObject_GetAttrString(_self, "__dict__"); 2586 mydict = PyObject_GetAttrString(myself, "__dict__");
2699 res = PyDict_Update(mydict, dict); 2587 res = PyDict_Update(mydict, dict);
2700 Py_DECREF(mydict); 2588 Py_DECREF(mydict);
2701 if (res == -1) 2589 if (res == -1)
2702 return NULL; 2590 return NULL;
2703 Py_INCREF(Py_None); 2591 Py_INCREF(Py_None);
2704 return Py_None; 2592 return Py_None;
2705 } 2593 }
2706 2594
2707 /* 2595 /*
2708 * default __ctypes_from_outparam__ method returns self. 2596 * default __ctypes_from_outparam__ method returns self.
(...skipping 14 matching lines...) Expand all
2723 2611
2724 PyTypeObject PyCData_Type = { 2612 PyTypeObject PyCData_Type = {
2725 PyVarObject_HEAD_INIT(NULL, 0) 2613 PyVarObject_HEAD_INIT(NULL, 0)
2726 "_ctypes._CData", 2614 "_ctypes._CData",
2727 sizeof(CDataObject), /* tp_basicsize */ 2615 sizeof(CDataObject), /* tp_basicsize */
2728 0, /* tp_itemsize */ 2616 0, /* tp_itemsize */
2729 PyCData_dealloc, /* tp_dealloc */ 2617 PyCData_dealloc, /* tp_dealloc */
2730 0, /* tp_print */ 2618 0, /* tp_print */
2731 0, /* tp_getattr */ 2619 0, /* tp_getattr */
2732 0, /* tp_setattr */ 2620 0, /* tp_setattr */
2733 0, /* tp_compare */ 2621 0, /* tp_reserved */
2734 0, /* tp_repr */ 2622 0, /* tp_repr */
2735 0, /* tp_as_number */ 2623 0, /* tp_as_number */
2736 0, /* tp_as_sequence */ 2624 0, /* tp_as_sequence */
2737 0, /* tp_as_mapping */ 2625 0, /* tp_as_mapping */
2738 PyCData_nohash, /* tp_hash */ 2626 PyCData_nohash, /* tp_hash */
2739 0, /* tp_call */ 2627 0, /* tp_call */
2740 0, /* tp_str */ 2628 0, /* tp_str */
2741 0, /* tp_getattro */ 2629 0, /* tp_getattro */
2742 0, /* tp_setattro */ 2630 0, /* tp_setattro */
2743 &PyCData_as_buffer, /* tp_as_buffer */ 2631 &PyCData_as_buffer, /* tp_as_buffer */
2744 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE, /* tp_ flags */ 2632 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2745 "XXX to be provided", /* tp_doc */ 2633 "XXX to be provided", /* tp_doc */
2746 (traverseproc)PyCData_traverse, /* tp_traverse */ 2634 (traverseproc)PyCData_traverse, /* tp_traverse */
2747 (inquiry)PyCData_clear, /* tp_clear */ 2635 (inquiry)PyCData_clear, /* tp_clear */
2748 0, /* tp_richcompare */ 2636 0, /* tp_richcompare */
2749 0, /* tp_weaklistoffset */ 2637 0, /* tp_weaklistoffset */
2750 0, /* tp_iter */ 2638 0, /* tp_iter */
2751 0, /* tp_iternext */ 2639 0, /* tp_iternext */
2752 PyCData_methods, /* tp_methods */ 2640 PyCData_methods, /* tp_methods */
2753 PyCData_members, /* tp_members */ 2641 PyCData_members, /* tp_members */
2754 0, /* tp_getset */ 2642 0, /* tp_getset */
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
2814 cmem->b_size = dict->size; 2702 cmem->b_size = dict->size;
2815 if (base) { /* use base's buffer */ 2703 if (base) { /* use base's buffer */
2816 assert(CDataObject_Check(base)); 2704 assert(CDataObject_Check(base));
2817 cmem->b_ptr = adr; 2705 cmem->b_ptr = adr;
2818 cmem->b_needsfree = 0; 2706 cmem->b_needsfree = 0;
2819 Py_INCREF(base); 2707 Py_INCREF(base);
2820 cmem->b_base = (CDataObject *)base; 2708 cmem->b_base = (CDataObject *)base;
2821 cmem->b_index = index; 2709 cmem->b_index = index;
2822 } else { /* copy contents of adr */ 2710 } else { /* copy contents of adr */
2823 if (-1 == PyCData_MallocBuffer(cmem, dict)) { 2711 if (-1 == PyCData_MallocBuffer(cmem, dict)) {
2712 Py_DECREF(cmem);
2824 return NULL; 2713 return NULL;
2825 Py_DECREF(cmem);
2826 } 2714 }
2827 memcpy(cmem->b_ptr, adr, dict->size); 2715 memcpy(cmem->b_ptr, adr, dict->size);
2828 cmem->b_index = index; 2716 cmem->b_index = index;
2829 } 2717 }
2830 return (PyObject *)cmem; 2718 return (PyObject *)cmem;
2831 } 2719 }
2832 2720
2833 /* 2721 /*
2834 Box a memory block into a CData instance. 2722 Box a memory block into a CData instance.
2835 */ 2723 */
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
2940 return NULL; 2828 return NULL;
2941 if (err) { 2829 if (err) {
2942 memcpy(ptr, 2830 memcpy(ptr,
2943 src->b_ptr, 2831 src->b_ptr,
2944 size); 2832 size);
2945 2833
2946 if (PyCPointerTypeObject_Check(type)) 2834 if (PyCPointerTypeObject_Check(type))
2947 /* XXX */; 2835 /* XXX */;
2948 2836
2949 value = GetKeepedObjects(src); 2837 value = GetKeepedObjects(src);
2838 if (value == NULL)
2839 return NULL;
2840
2950 Py_INCREF(value); 2841 Py_INCREF(value);
2951 return value; 2842 return value;
2952 } 2843 }
2953 2844
2954 if (PyCPointerTypeObject_Check(type) 2845 if (PyCPointerTypeObject_Check(type)
2955 && ArrayObject_Check(value)) { 2846 && ArrayObject_Check(value)) {
2956 StgDictObject *p1, *p2; 2847 StgDictObject *p1, *p2;
2957 PyObject *keep; 2848 PyObject *keep;
2958 p1 = PyObject_stgdict(value); 2849 p1 = PyObject_stgdict(value);
2959 assert(p1); /* Cannot be NULL for array instances */ 2850 assert(p1); /* Cannot be NULL for array instances */
2960 p2 = PyType_stgdict(type); 2851 p2 = PyType_stgdict(type);
2961 assert(p2); /* Cannot be NULL for pointer types */ 2852 assert(p2); /* Cannot be NULL for pointer types */
2962 2853
2963 if (p1->proto != p2->proto) { 2854 if (p1->proto != p2->proto) {
2964 PyErr_Format(PyExc_TypeError, 2855 PyErr_Format(PyExc_TypeError,
2965 "incompatible types, %s instance instead of %s instance ", 2856 "incompatible types, %s instance instead of %s instance ",
2966 Py_TYPE(value)->tp_name, 2857 Py_TYPE(value)->tp_name,
2967 ((PyTypeObject *)type)->tp_name); 2858 ((PyTypeObject *)type)->tp_name);
2968 return NULL; 2859 return NULL;
2969 } 2860 }
2970 *(void **)ptr = src->b_ptr; 2861 *(void **)ptr = src->b_ptr;
2971 2862
2972 keep = GetKeepedObjects(src); 2863 keep = GetKeepedObjects(src);
2864 if (keep == NULL)
2865 return NULL;
2866
2973 /* 2867 /*
2974 We are assigning an array object to a field which represents 2868 We are assigning an array object to a field which represents
2975 a pointer. This has the same effect as converting an array 2869 a pointer. This has the same effect as converting an array
2976 into a pointer. So, again, we have to keep the whole object 2870 into a pointer. So, again, we have to keep the whole object
2977 pointed to (which is the array in this case) alive, and not 2871 pointed to (which is the array in this case) alive, and not
2978 only it's object list. So we create a tuple, containing 2872 only it's object list. So we create a tuple, containing
2979 b_objects list PLUS the array itself, and return that! 2873 b_objects list PLUS the array itself, and return that!
2980 */ 2874 */
2981 return PyTuple_Pack(2, keep, value); 2875 return PyTuple_Pack(2, keep, value);
2982 } 2876 }
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
3221 3115
3222 if (PyCPointerTypeObject_Check(arg)) 3116 if (PyCPointerTypeObject_Check(arg))
3223 return 1; 3117 return 1;
3224 3118
3225 if (PyCArrayTypeObject_Check(arg)) 3119 if (PyCArrayTypeObject_Check(arg))
3226 return 1; 3120 return 1;
3227 3121
3228 dict = PyType_stgdict(arg); 3122 dict = PyType_stgdict(arg);
3229 if (dict 3123 if (dict
3230 /* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */ 3124 /* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */
3231 && PyString_Check(dict->proto) 3125 && PyUnicode_Check(dict->proto)
3232 /* We only allow c_void_p, c_char_p and c_wchar_p as a simple output parameter t ype */ 3126 /* We only allow c_void_p, c_char_p and c_wchar_p as a simple output parameter t ype */
3233 && (strchr("PzZ", PyString_AS_STRING(dict->proto)[0]))) { 3127 && (strchr("PzZ", _PyUnicode_AsString(dict->proto)[0]))) {
3234 return 1; 3128 return 1;
3235 } 3129 }
3236 3130
3237 PyErr_Format(PyExc_TypeError, 3131 PyErr_Format(PyExc_TypeError,
3238 "'out' parameter %d must be a pointer type, not %s", 3132 "'out' parameter %d must be a pointer type, not %s",
3239 Py_SAFE_DOWNCAST(index, Py_ssize_t, int), 3133 Py_SAFE_DOWNCAST(index, Py_ssize_t, int),
3240 PyType_Check(arg) ? 3134 PyType_Check(arg) ?
3241 ((PyTypeObject *)arg)->tp_name : 3135 ((PyTypeObject *)arg)->tp_name :
3242 Py_TYPE(arg)->tp_name); 3136 Py_TYPE(arg)->tp_name);
3243 return 0; 3137 return 0;
(...skipping 26 matching lines...) Expand all
3270 "paramflags must have the same length as argtypes"); 3164 "paramflags must have the same length as argtypes");
3271 return 0; 3165 return 0;
3272 } 3166 }
3273 3167
3274 for (i = 0; i < len; ++i) { 3168 for (i = 0; i < len; ++i) {
3275 PyObject *item = PyTuple_GET_ITEM(paramflags, i); 3169 PyObject *item = PyTuple_GET_ITEM(paramflags, i);
3276 int flag; 3170 int flag;
3277 char *name; 3171 char *name;
3278 PyObject *defval; 3172 PyObject *defval;
3279 PyObject *typ; 3173 PyObject *typ;
3280 if (!PyArg_ParseTuple(item, "i|zO", &flag, &name, &defval)) { 3174 if (!PyArg_ParseTuple(item, "i|ZO", &flag, &name, &defval)) {
3281 PyErr_SetString(PyExc_TypeError, 3175 PyErr_SetString(PyExc_TypeError,
3282 "paramflags must be a sequence of (int [,string [,value]]) tu ples"); 3176 "paramflags must be a sequence of (int [,string [,value]]) tu ples");
3283 return 0; 3177 return 0;
3284 } 3178 }
3285 typ = PyTuple_GET_ITEM(argtypes, i); 3179 typ = PyTuple_GET_ITEM(argtypes, i);
3286 switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) { 3180 switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) {
3287 case 0: 3181 case 0:
3288 case PARAMFLAG_FIN: 3182 case PARAMFLAG_FIN:
3289 case PARAMFLAG_FIN | PARAMFLAG_FLCID: 3183 case PARAMFLAG_FIN | PARAMFLAG_FLCID:
3290 case PARAMFLAG_FIN | PARAMFLAG_FOUT: 3184 case PARAMFLAG_FIN | PARAMFLAG_FOUT:
3291 break; 3185 break;
3292 case PARAMFLAG_FOUT: 3186 case PARAMFLAG_FOUT:
3293 if (!_check_outarg_type(typ, i+1)) 3187 if (!_check_outarg_type(typ, i+1))
3294 return 0; 3188 return 0;
3295 break; 3189 break;
3296 default: 3190 default:
3297 PyErr_Format(PyExc_TypeError, 3191 PyErr_Format(PyExc_TypeError,
3298 "paramflag value %d not supported", 3192 "paramflag value %d not supported",
3299 flag); 3193 flag);
3300 return 0; 3194 return 0;
3301 } 3195 }
3302 } 3196 }
3303 return 1; 3197 return 1;
3304 } 3198 }
3305 3199
3306 static int 3200 static int
3307 _get_name(PyObject *obj, char **pname) 3201 _get_name(PyObject *obj, char **pname)
3308 { 3202 {
3309 #ifdef MS_WIN32 3203 #ifdef MS_WIN32
3310 if (PyInt_Check(obj) || PyLong_Check(obj)) { 3204 if (PyLong_Check(obj)) {
3311 /* We have to use MAKEINTRESOURCEA for Windows CE. 3205 /* We have to use MAKEINTRESOURCEA for Windows CE.
3312 Works on Windows as well, of course. 3206 Works on Windows as well, of course.
3313 */ 3207 */
3314 *pname = MAKEINTRESOURCEA(PyInt_AsUnsignedLongMask(obj) & 0xFFFF); 3208 *pname = MAKEINTRESOURCEA(PyLong_AsUnsignedLongMask(obj) & 0xFFFF);
3315 return 1; 3209 return 1;
3316 } 3210 }
3317 #endif 3211 #endif
3318 if (PyString_Check(obj) || PyUnicode_Check(obj)) { 3212 if (PyBytes_Check(obj)) {
3319 *pname = PyString_AsString(obj); 3213 *pname = PyBytes_AS_STRING(obj);
3214 return *pname ? 1 : 0;
3215 }
3216 if (PyUnicode_Check(obj)) {
3217 *pname = _PyUnicode_AsString(obj);
3320 return *pname ? 1 : 0; 3218 return *pname ? 1 : 0;
3321 } 3219 }
3322 PyErr_SetString(PyExc_TypeError, 3220 PyErr_SetString(PyExc_TypeError,
3323 "function name must be string or integer"); 3221 "function name must be string or integer");
3324 return 0; 3222 return 0;
3325 } 3223 }
3326 3224
3327 3225
3328 static PyObject * 3226 static PyObject *
3329 PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds) 3227 PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
(...skipping 20 matching lines...) Expand all
3350 if (!PyArg_ParseTuple(ftuple, "O&O", _get_name, &name, &dll)) { 3248 if (!PyArg_ParseTuple(ftuple, "O&O", _get_name, &name, &dll)) {
3351 Py_DECREF(ftuple); 3249 Py_DECREF(ftuple);
3352 return NULL; 3250 return NULL;
3353 } 3251 }
3354 3252
3355 obj = PyObject_GetAttrString(dll, "_handle"); 3253 obj = PyObject_GetAttrString(dll, "_handle");
3356 if (!obj) { 3254 if (!obj) {
3357 Py_DECREF(ftuple); 3255 Py_DECREF(ftuple);
3358 return NULL; 3256 return NULL;
3359 } 3257 }
3360 if (!PyInt_Check(obj) && !PyLong_Check(obj)) { 3258 if (!PyLong_Check(obj)) {
3361 PyErr_SetString(PyExc_TypeError, 3259 PyErr_SetString(PyExc_TypeError,
3362 "the _handle attribute of the second argument must be an integer"); 3260 "the _handle attribute of the second argument must be an integer");
3363 Py_DECREF(ftuple); 3261 Py_DECREF(ftuple);
3364 Py_DECREF(obj); 3262 Py_DECREF(obj);
3365 return NULL; 3263 return NULL;
3366 } 3264 }
3367 handle = (void *)PyLong_AsVoidPtr(obj); 3265 handle = (void *)PyLong_AsVoidPtr(obj);
3368 Py_DECREF(obj); 3266 Py_DECREF(obj);
3369 if (PyErr_Occurred()) { 3267 if (PyErr_Occurred()) {
3370 PyErr_SetString(PyExc_ValueError, 3268 PyErr_SetString(PyExc_ValueError,
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
3476 StgDictObject *dict; 3374 StgDictObject *dict;
3477 CThunkObject *thunk; 3375 CThunkObject *thunk;
3478 3376
3479 if (PyTuple_GET_SIZE(args) == 0) 3377 if (PyTuple_GET_SIZE(args) == 0)
3480 return GenericPyCData_new(type, args, kwds); 3378 return GenericPyCData_new(type, args, kwds);
3481 3379
3482 if (1 <= PyTuple_GET_SIZE(args) && PyTuple_Check(PyTuple_GET_ITEM(args, 0))) 3380 if (1 <= PyTuple_GET_SIZE(args) && PyTuple_Check(PyTuple_GET_ITEM(args, 0)))
3483 return PyCFuncPtr_FromDll(type, args, kwds); 3381 return PyCFuncPtr_FromDll(type, args, kwds);
3484 3382
3485 #ifdef MS_WIN32 3383 #ifdef MS_WIN32
3486 if (2 <= PyTuple_GET_SIZE(args) && PyInt_Check(PyTuple_GET_ITEM(args, 0))) 3384 if (2 <= PyTuple_GET_SIZE(args) && PyLong_Check(PyTuple_GET_ITEM(args, 0)))
3487 return PyCFuncPtr_FromVtblIndex(type, args, kwds); 3385 return PyCFuncPtr_FromVtblIndex(type, args, kwds);
3488 #endif 3386 #endif
3489 3387
3490 if (1 == PyTuple_GET_SIZE(args) 3388 if (1 == PyTuple_GET_SIZE(args)
3491 && (PyInt_Check(PyTuple_GET_ITEM(args, 0)) 3389 && (PyLong_Check(PyTuple_GET_ITEM(args, 0)))) {
3492 || PyLong_Check(PyTuple_GET_ITEM(args, 0)))) {
3493 CDataObject *ob; 3390 CDataObject *ob;
3494 void *ptr = PyLong_AsVoidPtr(PyTuple_GET_ITEM(args, 0)); 3391 void *ptr = PyLong_AsVoidPtr(PyTuple_GET_ITEM(args, 0));
3495 if (ptr == NULL && PyErr_Occurred()) 3392 if (ptr == NULL && PyErr_Occurred())
3496 return NULL; 3393 return NULL;
3497 ob = (CDataObject *)GenericPyCData_new(type, args, kwds); 3394 ob = (CDataObject *)GenericPyCData_new(type, args, kwds);
3498 if (ob == NULL) 3395 if (ob == NULL)
3499 return NULL; 3396 return NULL;
3500 *(void **)ob->b_ptr = ptr; 3397 *(void **)ob->b_ptr = ptr;
3501 return (PyObject *)ob; 3398 return (PyObject *)ob;
3502 } 3399 }
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
3579 } 3476 }
3580 3477
3581 parg->tag = 'P'; 3478 parg->tag = 'P';
3582 parg->pffi_type = &ffi_type_pointer; 3479 parg->pffi_type = &ffi_type_pointer;
3583 parg->obj = obj; 3480 parg->obj = obj;
3584 parg->value.p = ((CDataObject *)obj)->b_ptr; 3481 parg->value.p = ((CDataObject *)obj)->b_ptr;
3585 return (PyObject *)parg; 3482 return (PyObject *)parg;
3586 } 3483 }
3587 3484
3588 static PyObject * 3485 static PyObject *
3589 _get_arg(int *pindex, char *name, PyObject *defval, PyObject *inargs, PyObject * kwds) 3486 _get_arg(int *pindex, PyObject *name, PyObject *defval, PyObject *inargs, PyObje ct *kwds)
3590 { 3487 {
3591 PyObject *v; 3488 PyObject *v;
3592 3489
3593 if (*pindex < PyTuple_GET_SIZE(inargs)) { 3490 if (*pindex < PyTuple_GET_SIZE(inargs)) {
3594 v = PyTuple_GET_ITEM(inargs, *pindex); 3491 v = PyTuple_GET_ITEM(inargs, *pindex);
3595 ++*pindex; 3492 ++*pindex;
3596 Py_INCREF(v); 3493 Py_INCREF(v);
3597 return v; 3494 return v;
3598 } 3495 }
3599 if (kwds && (v = PyDict_GetItemString(kwds, name))) { 3496 if (kwds && name && (v = PyDict_GetItem(kwds, name))) {
3600 ++*pindex; 3497 ++*pindex;
3601 Py_INCREF(v); 3498 Py_INCREF(v);
3602 return v; 3499 return v;
3603 } 3500 }
3604 if (defval) { 3501 if (defval) {
3605 Py_INCREF(defval); 3502 Py_INCREF(defval);
3606 return defval; 3503 return defval;
3607 } 3504 }
3608 /* we can't currently emit a better error message */ 3505 /* we can't currently emit a better error message */
3609 if (name) 3506 if (name)
3610 PyErr_Format(PyExc_TypeError, 3507 PyErr_Format(PyExc_TypeError,
3611 "required argument '%s' missing", name); 3508 "required argument '%S' missing", name);
3612 else 3509 else
3613 PyErr_Format(PyExc_TypeError, 3510 PyErr_Format(PyExc_TypeError,
3614 "not enough arguments"); 3511 "not enough arguments");
3615 return NULL; 3512 return NULL;
3616 } 3513 }
3617 3514
3618 /* 3515 /*
3619 This function implements higher level functionality plus the ability to call 3516 This function implements higher level functionality plus the ability to call
3620 functions with keyword arguments by looking at parameter flags. parameter 3517 functions with keyword arguments by looking at parameter flags. parameter
3621 flags is a tuple of 1, 2 or 3-tuples. The first entry in each is an integer 3518 flags is a tuple of 1, 2 or 3-tuples. The first entry in each is an integer
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
3670 #ifdef MS_WIN32 3567 #ifdef MS_WIN32
3671 /* For a COM method, skip the first arg */ 3568 /* For a COM method, skip the first arg */
3672 if (self->index) { 3569 if (self->index) {
3673 inargs_index = 1; 3570 inargs_index = 1;
3674 } 3571 }
3675 #endif 3572 #endif
3676 for (i = 0; i < len; ++i) { 3573 for (i = 0; i < len; ++i) {
3677 PyObject *item = PyTuple_GET_ITEM(paramflags, i); 3574 PyObject *item = PyTuple_GET_ITEM(paramflags, i);
3678 PyObject *ob; 3575 PyObject *ob;
3679 int flag; 3576 int flag;
3680 char *name = NULL; 3577 PyObject *name = NULL;
3681 PyObject *defval = NULL; 3578 PyObject *defval = NULL;
3682 3579
3683 /* This way seems to be ~2 us faster than the PyArg_ParseTuple 3580 /* This way seems to be ~2 us faster than the PyArg_ParseTuple
3684 calls below. */ 3581 calls below. */
3685 /* We HAVE already checked that the tuple can be parsed with "i|zO", so. .. */ 3582 /* We HAVE already checked that the tuple can be parsed with "i|ZO", so. .. */
3686 Py_ssize_t tsize = PyTuple_GET_SIZE(item); 3583 Py_ssize_t tsize = PyTuple_GET_SIZE(item);
3687 flag = PyInt_AS_LONG(PyTuple_GET_ITEM(item, 0)); 3584 flag = PyLong_AS_LONG(PyTuple_GET_ITEM(item, 0));
3688 name = tsize > 1 ? PyString_AS_STRING(PyTuple_GET_ITEM(item, 1)) : NULL; 3585 name = tsize > 1 ? PyTuple_GET_ITEM(item, 1) : NULL;
3689 defval = tsize > 2 ? PyTuple_GET_ITEM(item, 2) : NULL; 3586 defval = tsize > 2 ? PyTuple_GET_ITEM(item, 2) : NULL;
3690 3587
3691 switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) { 3588 switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) {
3692 case PARAMFLAG_FIN | PARAMFLAG_FLCID: 3589 case PARAMFLAG_FIN | PARAMFLAG_FLCID:
3693 /* ['in', 'lcid'] parameter. Always taken from defval, 3590 /* ['in', 'lcid'] parameter. Always taken from defval,
3694 if given, else the integer 0. */ 3591 if given, else the integer 0. */
3695 if (defval == NULL) { 3592 if (defval == NULL) {
3696 defval = PyInt_FromLong(0); 3593 defval = PyLong_FromLong(0);
3697 if (defval == NULL) 3594 if (defval == NULL)
3698 goto error; 3595 goto error;
3699 } else 3596 } else
3700 Py_INCREF(defval); 3597 Py_INCREF(defval);
3701 PyTuple_SET_ITEM(callargs, i, defval); 3598 PyTuple_SET_ITEM(callargs, i, defval);
3702 break; 3599 break;
3703 case (PARAMFLAG_FIN | PARAMFLAG_FOUT): 3600 case (PARAMFLAG_FIN | PARAMFLAG_FOUT):
3704 *pinoutmask |= (1 << i); /* mark as inout arg */ 3601 *pinoutmask |= (1 << i); /* mark as inout arg */
3705 (*pnumretvals)++; 3602 (*pnumretvals)++;
3706 /* fall through to PARAMFLAG_FIN... */ 3603 /* fall through to PARAMFLAG_FIN... */
(...skipping 25 matching lines...) Expand all
3732 } 3629 }
3733 ob = PyTuple_GET_ITEM(argtypes, i); 3630 ob = PyTuple_GET_ITEM(argtypes, i);
3734 dict = PyType_stgdict(ob); 3631 dict = PyType_stgdict(ob);
3735 if (dict == NULL) { 3632 if (dict == NULL) {
3736 /* Cannot happen: _validate_paramflags() 3633 /* Cannot happen: _validate_paramflags()
3737 would not accept such an object */ 3634 would not accept such an object */
3738 PyErr_Format(PyExc_RuntimeError, 3635 PyErr_Format(PyExc_RuntimeError,
3739 "NULL stgdict unexpected"); 3636 "NULL stgdict unexpected");
3740 goto error; 3637 goto error;
3741 } 3638 }
3742 if (PyString_Check(dict->proto)) { 3639 if (PyUnicode_Check(dict->proto)) {
3743 PyErr_Format( 3640 PyErr_Format(
3744 PyExc_TypeError, 3641 PyExc_TypeError,
3745 "%s 'out' parameter must be passed as default value", 3642 "%s 'out' parameter must be passed as default value",
3746 ((PyTypeObject *)ob)->tp_name); 3643 ((PyTypeObject *)ob)->tp_name);
3747 goto error; 3644 goto error;
3748 } 3645 }
3749 if (PyCArrayTypeObject_Check(ob)) 3646 if (PyCArrayTypeObject_Check(ob))
3750 ob = PyObject_CallObject(ob, NULL); 3647 ob = PyObject_CallObject(ob, NULL);
3751 else 3648 else
3752 /* Create an instance of the pointed-to type */ 3649 /* Create an instance of the pointed-to type */
(...skipping 23 matching lines...) Expand all
3776 /* We have counted the arguments we have consumed in 'inargs_index'. This 3673 /* We have counted the arguments we have consumed in 'inargs_index'. This
3777 must be the same as len(inargs) + len(kwds), otherwise we have 3674 must be the same as len(inargs) + len(kwds), otherwise we have
3778 either too much or not enough arguments. */ 3675 either too much or not enough arguments. */
3779 3676
3780 actual_args = PyTuple_GET_SIZE(inargs) + (kwds ? PyDict_Size(kwds) : 0); 3677 actual_args = PyTuple_GET_SIZE(inargs) + (kwds ? PyDict_Size(kwds) : 0);
3781 if (actual_args != inargs_index) { 3678 if (actual_args != inargs_index) {
3782 /* When we have default values or named parameters, this error 3679 /* When we have default values or named parameters, this error
3783 message is misleading. See unittests/test_paramflags.py 3680 message is misleading. See unittests/test_paramflags.py
3784 */ 3681 */
3785 PyErr_Format(PyExc_TypeError, 3682 PyErr_Format(PyExc_TypeError,
3786 #if (PY_VERSION_HEX < 0x02050000)
3787 "call takes exactly %d arguments (%d given)",
3788 #else
3789 "call takes exactly %d arguments (%zd given)", 3683 "call takes exactly %d arguments (%zd given)",
3790 #endif
3791 inargs_index, actual_args); 3684 inargs_index, actual_args);
3792 goto error; 3685 goto error;
3793 } 3686 }
3794 3687
3795 /* outmask is a bitmask containing indexes into callargs. Items at 3688 /* outmask is a bitmask containing indexes into callargs. Items at
3796 these indexes contain values to return. 3689 these indexes contain values to return.
3797 */ 3690 */
3798 return callargs; 3691 return callargs;
3799 error: 3692 error:
3800 Py_DECREF(callargs); 3693 Py_DECREF(callargs);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
3841 if (bit & inoutmask) { 3734 if (bit & inoutmask) {
3842 v = PyTuple_GET_ITEM(callargs, i); 3735 v = PyTuple_GET_ITEM(callargs, i);
3843 Py_INCREF(v); 3736 Py_INCREF(v);
3844 if (numretvals == 1) { 3737 if (numretvals == 1) {
3845 Py_DECREF(callargs); 3738 Py_DECREF(callargs);
3846 return v; 3739 return v;
3847 } 3740 }
3848 PyTuple_SET_ITEM(tup, index, v); 3741 PyTuple_SET_ITEM(tup, index, v);
3849 index++; 3742 index++;
3850 } else if (bit & outmask) { 3743 } else if (bit & outmask) {
3744 _Py_IDENTIFIER(__ctypes_from_outparam__);
3745
3851 v = PyTuple_GET_ITEM(callargs, i); 3746 v = PyTuple_GET_ITEM(callargs, i);
3852 v = PyObject_CallMethod(v, "__ctypes_from_outparam__", NULL); 3747 v = _PyObject_CallMethodId(v, &PyId___ctypes_from_outparam__, NULL);
3853 if (v == NULL || numretvals == 1) { 3748 if (v == NULL || numretvals == 1) {
3854 Py_DECREF(callargs); 3749 Py_DECREF(callargs);
3855 return v; 3750 return v;
3856 } 3751 }
3857 PyTuple_SET_ITEM(tup, index, v); 3752 PyTuple_SET_ITEM(tup, index, v);
3858 index++; 3753 index++;
3859 } 3754 }
3860 if (index == numretvals) 3755 if (index == numretvals)
3861 break; 3756 break;
3862 } 3757 }
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
4030 { 3925 {
4031 PyCFuncPtr_clear(self); 3926 PyCFuncPtr_clear(self);
4032 Py_TYPE(self)->tp_free((PyObject *)self); 3927 Py_TYPE(self)->tp_free((PyObject *)self);
4033 } 3928 }
4034 3929
4035 static PyObject * 3930 static PyObject *
4036 PyCFuncPtr_repr(PyCFuncPtrObject *self) 3931 PyCFuncPtr_repr(PyCFuncPtrObject *self)
4037 { 3932 {
4038 #ifdef MS_WIN32 3933 #ifdef MS_WIN32
4039 if (self->index) 3934 if (self->index)
4040 return PyString_FromFormat("<COM method offset %d: %s at %p>", 3935 return PyUnicode_FromFormat("<COM method offset %d: %s at %p>",
4041 self->index - 0x1000, 3936 self->index - 0x1000,
4042 Py_TYPE(self)->tp_name, 3937 Py_TYPE(self)->tp_name,
4043 self); 3938 self);
4044 #endif 3939 #endif
4045 return PyString_FromFormat("<%s object at %p>", 3940 return PyUnicode_FromFormat("<%s object at %p>",
4046 Py_TYPE(self)->tp_name, 3941 Py_TYPE(self)->tp_name,
4047 self); 3942 self);
4048 } 3943 }
4049 3944
4050 static int 3945 static int
4051 PyCFuncPtr_nonzero(PyCFuncPtrObject *self) 3946 PyCFuncPtr_bool(PyCFuncPtrObject *self)
4052 { 3947 {
4053 return ((*(void **)self->b_ptr != NULL) 3948 return ((*(void **)self->b_ptr != NULL)
4054 #ifdef MS_WIN32 3949 #ifdef MS_WIN32
4055 || (self->index != 0) 3950 || (self->index != 0)
4056 #endif 3951 #endif
4057 ); 3952 );
4058 } 3953 }
4059 3954
4060 static PyNumberMethods PyCFuncPtr_as_number = { 3955 static PyNumberMethods PyCFuncPtr_as_number = {
4061 0, /* nb_add */ 3956 0, /* nb_add */
4062 0, /* nb_subtract */ 3957 0, /* nb_subtract */
4063 0, /* nb_multiply */ 3958 0, /* nb_multiply */
4064 0, /* nb_divide */
4065 0, /* nb_remainder */ 3959 0, /* nb_remainder */
4066 0, /* nb_divmod */ 3960 0, /* nb_divmod */
4067 0, /* nb_power */ 3961 0, /* nb_power */
4068 0, /* nb_negative */ 3962 0, /* nb_negative */
4069 0, /* nb_positive */ 3963 0, /* nb_positive */
4070 0, /* nb_absolute */ 3964 0, /* nb_absolute */
4071 (inquiry)PyCFuncPtr_nonzero, /* nb_nonzero */ 3965 (inquiry)PyCFuncPtr_bool, /* nb_bool */
4072 }; 3966 };
4073 3967
4074 PyTypeObject PyCFuncPtr_Type = { 3968 PyTypeObject PyCFuncPtr_Type = {
4075 PyVarObject_HEAD_INIT(NULL, 0) 3969 PyVarObject_HEAD_INIT(NULL, 0)
4076 "_ctypes.PyCFuncPtr", 3970 "_ctypes.PyCFuncPtr",
4077 sizeof(PyCFuncPtrObject), /* tp_basicsize */ 3971 sizeof(PyCFuncPtrObject), /* tp_basicsize */
4078 0, /* tp_itemsize */ 3972 0, /* tp_itemsize */
4079 (destructor)PyCFuncPtr_dealloc, /* tp_dealloc */ 3973 (destructor)PyCFuncPtr_dealloc, /* tp_dealloc */
4080 0, /* tp_print */ 3974 0, /* tp_print */
4081 0, /* tp_getattr */ 3975 0, /* tp_getattr */
4082 0, /* tp_setattr */ 3976 0, /* tp_setattr */
4083 0, /* tp_compare */ 3977 0, /* tp_reserved */
4084 (reprfunc)PyCFuncPtr_repr, /* tp_repr */ 3978 (reprfunc)PyCFuncPtr_repr, /* tp_repr */
4085 &PyCFuncPtr_as_number, /* tp_as_number */ 3979 &PyCFuncPtr_as_number, /* tp_as_number */
4086 0, /* tp_as_sequence */ 3980 0, /* tp_as_sequence */
4087 0, /* tp_as_mapping */ 3981 0, /* tp_as_mapping */
4088 0, /* tp_hash */ 3982 0, /* tp_hash */
4089 (ternaryfunc)PyCFuncPtr_call, /* tp_call */ 3983 (ternaryfunc)PyCFuncPtr_call, /* tp_call */
4090 0, /* tp_str */ 3984 0, /* tp_str */
4091 0, /* tp_getattro */ 3985 0, /* tp_getattro */
4092 0, /* tp_setattro */ 3986 0, /* tp_setattro */
4093 &PyCData_as_buffer, /* tp_as_buffer */ 3987 &PyCData_as_buffer, /* tp_as_buffer */
4094 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE, /* tp_ flags */ 3988 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4095 "Function Pointer", /* tp_doc */ 3989 "Function Pointer", /* tp_doc */
4096 (traverseproc)PyCFuncPtr_traverse, /* tp_traverse */ 3990 (traverseproc)PyCFuncPtr_traverse, /* tp_traverse */
4097 (inquiry)PyCFuncPtr_clear, /* tp_clear */ 3991 (inquiry)PyCFuncPtr_clear, /* tp_clear */
4098 0, /* tp_richcompare */ 3992 0, /* tp_richcompare */
4099 0, /* tp_weaklistoffset */ 3993 0, /* tp_weaklistoffset */
4100 0, /* tp_iter */ 3994 0, /* tp_iter */
4101 0, /* tp_iternext */ 3995 0, /* tp_iternext */
4102 0, /* tp_methods */ 3996 0, /* tp_methods */
4103 0, /* tp_members */ 3997 0, /* tp_members */
4104 PyCFuncPtr_getsets, /* tp_getset */ 3998 PyCFuncPtr_getsets, /* tp_getset */
(...skipping 13 matching lines...) Expand all
4118 Struct_Type 4012 Struct_Type
4119 */ 4013 */
4120 /* 4014 /*
4121 This function is called to initialize a Structure or Union with positional 4015 This function is called to initialize a Structure or Union with positional
4122 arguments. It calls itself recursively for all Structure or Union base 4016 arguments. It calls itself recursively for all Structure or Union base
4123 classes, then retrieves the _fields_ member to associate the argument 4017 classes, then retrieves the _fields_ member to associate the argument
4124 position with the correct field name. 4018 position with the correct field name.
4125 4019
4126 Returns -1 on error, or the index of next argument on success. 4020 Returns -1 on error, or the index of next argument on success.
4127 */ 4021 */
4128 static int 4022 static Py_ssize_t
4129 _init_pos_args(PyObject *self, PyTypeObject *type, 4023 _init_pos_args(PyObject *self, PyTypeObject *type,
4130 PyObject *args, PyObject *kwds, 4024 PyObject *args, PyObject *kwds,
4131 int index) 4025 Py_ssize_t index)
4132 { 4026 {
4133 StgDictObject *dict; 4027 StgDictObject *dict;
4134 PyObject *fields; 4028 PyObject *fields;
4135 int i; 4029 Py_ssize_t i;
4136 4030
4137 if (PyType_stgdict((PyObject *)type->tp_base)) { 4031 if (PyType_stgdict((PyObject *)type->tp_base)) {
4138 index = _init_pos_args(self, type->tp_base, 4032 index = _init_pos_args(self, type->tp_base,
4139 args, kwds, 4033 args, kwds,
4140 index); 4034 index);
4141 if (index == -1) 4035 if (index == -1)
4142 return -1; 4036 return -1;
4143 } 4037 }
4144 4038
4145 dict = PyType_stgdict((PyObject *)type); 4039 dict = PyType_stgdict((PyObject *)type);
4146 fields = PyDict_GetItemString((PyObject *)dict, "_fields_"); 4040 fields = PyDict_GetItemString((PyObject *)dict, "_fields_");
4147 if (fields == NULL) 4041 if (fields == NULL)
4148 return index; 4042 return index;
4149 4043
4150 for (i = 0; 4044 for (i = 0;
4151 i < dict->length && (i+index) < PyTuple_GET_SIZE(args); 4045 i < dict->length && (i+index) < PyTuple_GET_SIZE(args);
4152 ++i) { 4046 ++i) {
4153 PyObject *pair = PySequence_GetItem(fields, i); 4047 PyObject *pair = PySequence_GetItem(fields, i);
4154 PyObject *name, *val; 4048 PyObject *name, *val;
4155 int res; 4049 int res;
4156 if (!pair) 4050 if (!pair)
4157 return -1; 4051 return -1;
4158 name = PySequence_GetItem(pair, 0); 4052 name = PySequence_GetItem(pair, 0);
4159 if (!name) { 4053 if (!name) {
4160 Py_DECREF(pair); 4054 Py_DECREF(pair);
4161 return -1; 4055 return -1;
4162 } 4056 }
4163 val = PyTuple_GET_ITEM(args, i + index); 4057 val = PyTuple_GET_ITEM(args, i + index);
4164 if (kwds && PyDict_GetItem(kwds, name)) { 4058 if (kwds && PyDict_GetItem(kwds, name)) {
4165 char *field = PyString_AsString(name); 4059 char *field = PyBytes_AsString(name);
4166 if (field == NULL) { 4060 if (field == NULL) {
4167 PyErr_Clear(); 4061 PyErr_Clear();
4168 field = "???"; 4062 field = "???";
4169 } 4063 }
4170 PyErr_Format(PyExc_TypeError, 4064 PyErr_Format(PyExc_TypeError,
4171 "duplicate values for field '%s'", 4065 "duplicate values for field '%s'",
4172 field); 4066 field);
4173 Py_DECREF(pair); 4067 Py_DECREF(pair);
4174 Py_DECREF(name); 4068 Py_DECREF(name);
4175 return -1; 4069 return -1;
(...skipping 13 matching lines...) Expand all
4189 { 4083 {
4190 /* Optimization possible: Store the attribute names _fields_[x][0] 4084 /* Optimization possible: Store the attribute names _fields_[x][0]
4191 * in C accessible fields somewhere ? 4085 * in C accessible fields somewhere ?
4192 */ 4086 */
4193 if (!PyTuple_Check(args)) { 4087 if (!PyTuple_Check(args)) {
4194 PyErr_SetString(PyExc_TypeError, 4088 PyErr_SetString(PyExc_TypeError,
4195 "args not a tuple?"); 4089 "args not a tuple?");
4196 return -1; 4090 return -1;
4197 } 4091 }
4198 if (PyTuple_GET_SIZE(args)) { 4092 if (PyTuple_GET_SIZE(args)) {
4199 int res = _init_pos_args(self, Py_TYPE(self), 4093 Py_ssize_t res = _init_pos_args(self, Py_TYPE(self),
4200 args, kwds, 0); 4094 args, kwds, 0);
4201 if (res == -1) 4095 if (res == -1)
4202 return -1; 4096 return -1;
4203 if (res < PyTuple_GET_SIZE(args)) { 4097 if (res < PyTuple_GET_SIZE(args)) {
4204 PyErr_SetString(PyExc_TypeError, 4098 PyErr_SetString(PyExc_TypeError,
4205 "too many initializers"); 4099 "too many initializers");
4206 return -1; 4100 return -1;
4207 } 4101 }
4208 } 4102 }
4209 4103
4210 if (kwds) { 4104 if (kwds) {
4211 PyObject *key, *value; 4105 PyObject *key, *value;
4212 Py_ssize_t pos = 0; 4106 Py_ssize_t pos = 0;
4213 while(PyDict_Next(kwds, &pos, &key, &value)) { 4107 while(PyDict_Next(kwds, &pos, &key, &value)) {
4214 if (-1 == PyObject_SetAttr(self, key, value)) 4108 if (-1 == PyObject_SetAttr(self, key, value))
4215 return -1; 4109 return -1;
4216 } 4110 }
4217 } 4111 }
4218 return 0; 4112 return 0;
4219 } 4113 }
4220 4114
4221 static PyTypeObject Struct_Type = { 4115 static PyTypeObject Struct_Type = {
4222 PyVarObject_HEAD_INIT(NULL, 0) 4116 PyVarObject_HEAD_INIT(NULL, 0)
4223 "_ctypes.Structure", 4117 "_ctypes.Structure",
4224 sizeof(CDataObject), /* tp_basicsize */ 4118 sizeof(CDataObject), /* tp_basicsize */
4225 0, /* tp_itemsize */ 4119 0, /* tp_itemsize */
4226 0, /* tp_dealloc */ 4120 0, /* tp_dealloc */
4227 0, /* tp_print */ 4121 0, /* tp_print */
4228 0, /* tp_getattr */ 4122 0, /* tp_getattr */
4229 0, /* tp_setattr */ 4123 0, /* tp_setattr */
4230 0, /* tp_compare */ 4124 0, /* tp_reserved */
4231 0, /* tp_repr */ 4125 0, /* tp_repr */
4232 0, /* tp_as_number */ 4126 0, /* tp_as_number */
4233 0, /* tp_as_sequence */ 4127 0, /* tp_as_sequence */
4234 0, /* tp_as_mapping */ 4128 0, /* tp_as_mapping */
4235 0, /* tp_hash */ 4129 0, /* tp_hash */
4236 0, /* tp_call */ 4130 0, /* tp_call */
4237 0, /* tp_str */ 4131 0, /* tp_str */
4238 0, /* tp_getattro */ 4132 0, /* tp_getattro */
4239 0, /* tp_setattro */ 4133 0, /* tp_setattro */
4240 &PyCData_as_buffer, /* tp_as_buffer */ 4134 &PyCData_as_buffer, /* tp_as_buffer */
4241 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE, /* tp_ flags */ 4135 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4242 "Structure base class", /* tp_doc */ 4136 "Structure base class", /* tp_doc */
4243 (traverseproc)PyCData_traverse, /* tp_traverse */ 4137 (traverseproc)PyCData_traverse, /* tp_traverse */
4244 (inquiry)PyCData_clear, /* tp_clear */ 4138 (inquiry)PyCData_clear, /* tp_clear */
4245 0, /* tp_richcompare */ 4139 0, /* tp_richcompare */
4246 0, /* tp_weaklistoffset */ 4140 0, /* tp_weaklistoffset */
4247 0, /* tp_iter */ 4141 0, /* tp_iter */
4248 0, /* tp_iternext */ 4142 0, /* tp_iternext */
4249 0, /* tp_methods */ 4143 0, /* tp_methods */
4250 0, /* tp_members */ 4144 0, /* tp_members */
4251 0, /* tp_getset */ 4145 0, /* tp_getset */
(...skipping 10 matching lines...) Expand all
4262 4156
4263 static PyTypeObject Union_Type = { 4157 static PyTypeObject Union_Type = {
4264 PyVarObject_HEAD_INIT(NULL, 0) 4158 PyVarObject_HEAD_INIT(NULL, 0)
4265 "_ctypes.Union", 4159 "_ctypes.Union",
4266 sizeof(CDataObject), /* tp_basicsize */ 4160 sizeof(CDataObject), /* tp_basicsize */
4267 0, /* tp_itemsize */ 4161 0, /* tp_itemsize */
4268 0, /* tp_dealloc */ 4162 0, /* tp_dealloc */
4269 0, /* tp_print */ 4163 0, /* tp_print */
4270 0, /* tp_getattr */ 4164 0, /* tp_getattr */
4271 0, /* tp_setattr */ 4165 0, /* tp_setattr */
4272 0, /* tp_compare */ 4166 0, /* tp_reserved */
4273 0, /* tp_repr */ 4167 0, /* tp_repr */
4274 0, /* tp_as_number */ 4168 0, /* tp_as_number */
4275 0, /* tp_as_sequence */ 4169 0, /* tp_as_sequence */
4276 0, /* tp_as_mapping */ 4170 0, /* tp_as_mapping */
4277 0, /* tp_hash */ 4171 0, /* tp_hash */
4278 0, /* tp_call */ 4172 0, /* tp_call */
4279 0, /* tp_str */ 4173 0, /* tp_str */
4280 0, /* tp_getattro */ 4174 0, /* tp_getattro */
4281 0, /* tp_setattro */ 4175 0, /* tp_setattro */
4282 &PyCData_as_buffer, /* tp_as_buffer */ 4176 &PyCData_as_buffer, /* tp_as_buffer */
4283 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE, /* tp_ flags */ 4177 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4284 "Union base class", /* tp_doc */ 4178 "Union base class", /* tp_doc */
4285 (traverseproc)PyCData_traverse, /* tp_traverse */ 4179 (traverseproc)PyCData_traverse, /* tp_traverse */
4286 (inquiry)PyCData_clear, /* tp_clear */ 4180 (inquiry)PyCData_clear, /* tp_clear */
4287 0, /* tp_richcompare */ 4181 0, /* tp_richcompare */
4288 0, /* tp_weaklistoffset */ 4182 0, /* tp_weaklistoffset */
4289 0, /* tp_iter */ 4183 0, /* tp_iter */
4290 0, /* tp_iternext */ 4184 0, /* tp_iternext */
4291 0, /* tp_methods */ 4185 0, /* tp_methods */
4292 0, /* tp_members */ 4186 0, /* tp_members */
4293 0, /* tp_getset */ 4187 0, /* tp_getset */
(...skipping 28 matching lines...) Expand all
4322 for (i = 0; i < n; ++i) { 4216 for (i = 0; i < n; ++i) {
4323 PyObject *v; 4217 PyObject *v;
4324 v = PyTuple_GET_ITEM(args, i); 4218 v = PyTuple_GET_ITEM(args, i);
4325 if (-1 == PySequence_SetItem((PyObject *)self, i, v)) 4219 if (-1 == PySequence_SetItem((PyObject *)self, i, v))
4326 return -1; 4220 return -1;
4327 } 4221 }
4328 return 0; 4222 return 0;
4329 } 4223 }
4330 4224
4331 static PyObject * 4225 static PyObject *
4332 Array_item(PyObject *_self, Py_ssize_t index) 4226 Array_item(PyObject *myself, Py_ssize_t index)
4333 { 4227 {
4334 CDataObject *self = (CDataObject *)_self; 4228 CDataObject *self = (CDataObject *)myself;
4335 Py_ssize_t offset, size; 4229 Py_ssize_t offset, size;
4336 StgDictObject *stgdict; 4230 StgDictObject *stgdict;
4337 4231
4338 4232
4339 if (index < 0 || index >= self->b_length) { 4233 if (index < 0 || index >= self->b_length) {
4340 PyErr_SetString(PyExc_IndexError, 4234 PyErr_SetString(PyExc_IndexError,
4341 "invalid index"); 4235 "invalid index");
4342 return NULL; 4236 return NULL;
4343 } 4237 }
4344 4238
4345 stgdict = PyObject_stgdict((PyObject *)self); 4239 stgdict = PyObject_stgdict((PyObject *)self);
4346 assert(stgdict); /* Cannot be NULL for array instances */ 4240 assert(stgdict); /* Cannot be NULL for array instances */
4347 /* Would it be clearer if we got the item size from 4241 /* Would it be clearer if we got the item size from
4348 stgdict->proto's stgdict? 4242 stgdict->proto's stgdict?
4349 */ 4243 */
4350 size = stgdict->size / stgdict->length; 4244 size = stgdict->size / stgdict->length;
4351 offset = index * size; 4245 offset = index * size;
4352 4246
4353 return PyCData_get(stgdict->proto, stgdict->getfunc, (PyObject *)self, 4247 return PyCData_get(stgdict->proto, stgdict->getfunc, (PyObject *)self,
4354 index, size, self->b_ptr + offset); 4248 index, size, self->b_ptr + offset);
4355 } 4249 }
4356 4250
4357 static PyObject * 4251 static PyObject *
4358 Array_slice(PyObject *_self, Py_ssize_t ilow, Py_ssize_t ihigh) 4252 Array_subscript(PyObject *myself, PyObject *item)
4359 { 4253 {
4360 CDataObject *self = (CDataObject *)_self; 4254 CDataObject *self = (CDataObject *)myself;
4361 StgDictObject *stgdict, *itemdict;
4362 PyObject *proto;
4363 PyListObject *np;
4364 Py_ssize_t i, len;
4365
4366 if (ilow < 0)
4367 ilow = 0;
4368 else if (ilow > self->b_length)
4369 ilow = self->b_length;
4370 if (ihigh < ilow)
4371 ihigh = ilow;
4372 else if (ihigh > self->b_length)
4373 ihigh = self->b_length;
4374 len = ihigh - ilow;
4375
4376 stgdict = PyObject_stgdict((PyObject *)self);
4377 assert(stgdict); /* Cannot be NULL for array object instances */
4378 proto = stgdict->proto;
4379 itemdict = PyType_stgdict(proto);
4380 assert(itemdict); /* proto is the item type of the array, a ctypes
4381 type, so this cannot be NULL */
4382 if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
4383 char *ptr = (char *)self->b_ptr;
4384 return PyString_FromStringAndSize(ptr + ilow, len);
4385 #ifdef CTYPES_UNICODE
4386 } else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
4387 wchar_t *ptr = (wchar_t *)self->b_ptr;
4388 return PyUnicode_FromWideChar(ptr + ilow, len);
4389 #endif
4390 }
4391
4392 np = (PyListObject *) PyList_New(len);
4393 if (np == NULL)
4394 return NULL;
4395
4396 for (i = 0; i < len; i++) {
4397 PyObject *v = Array_item(_self, i+ilow);
4398 PyList_SET_ITEM(np, i, v);
4399 }
4400 return (PyObject *)np;
4401 }
4402
4403 static PyObject *
4404 Array_subscript(PyObject *_self, PyObject *item)
4405 {
4406 CDataObject *self = (CDataObject *)_self;
4407 4255
4408 if (PyIndex_Check(item)) { 4256 if (PyIndex_Check(item)) {
4409 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); 4257 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
4410 4258
4411 if (i == -1 && PyErr_Occurred()) 4259 if (i == -1 && PyErr_Occurred())
4412 return NULL; 4260 return NULL;
4413 if (i < 0) 4261 if (i < 0)
4414 i += self->b_length; 4262 i += self->b_length;
4415 return Array_item(_self, i); 4263 return Array_item(myself, i);
4416 } 4264 }
4417 else if PySlice_Check(item) { 4265 else if (PySlice_Check(item)) {
4418 StgDictObject *stgdict, *itemdict; 4266 StgDictObject *stgdict, *itemdict;
4419 PyObject *proto; 4267 PyObject *proto;
4420 PyObject *np; 4268 PyObject *np;
4421 Py_ssize_t start, stop, step, slicelen, cur, i; 4269 Py_ssize_t start, stop, step, slicelen, cur, i;
4422 4270
4423 if (PySlice_GetIndicesEx((PySliceObject *)item, 4271 if (PySlice_GetIndicesEx(item,
4424 self->b_length, &start, &stop, 4272 self->b_length, &start, &stop,
4425 &step, &slicelen) < 0) { 4273 &step, &slicelen) < 0) {
4426 return NULL; 4274 return NULL;
4427 } 4275 }
4428 4276
4429 stgdict = PyObject_stgdict((PyObject *)self); 4277 stgdict = PyObject_stgdict((PyObject *)self);
4430 assert(stgdict); /* Cannot be NULL for array object instances */ 4278 assert(stgdict); /* Cannot be NULL for array object instances */
4431 proto = stgdict->proto; 4279 proto = stgdict->proto;
4432 itemdict = PyType_stgdict(proto); 4280 itemdict = PyType_stgdict(proto);
4433 assert(itemdict); /* proto is the item type of the array, a 4281 assert(itemdict); /* proto is the item type of the array, a
4434 ctypes type, so this cannot be NULL */ 4282 ctypes type, so this cannot be NULL */
4435 4283
4436 if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) { 4284 if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
4437 char *ptr = (char *)self->b_ptr; 4285 char *ptr = (char *)self->b_ptr;
4438 char *dest; 4286 char *dest;
4439 4287
4440 if (slicelen <= 0) 4288 if (slicelen <= 0)
4441 return PyString_FromString(""); 4289 return PyBytes_FromStringAndSize("", 0);
4442 if (step == 1) { 4290 if (step == 1) {
4443 return PyString_FromStringAndSize(ptr + start, 4291 return PyBytes_FromStringAndSize(ptr + start,
4444 slicelen); 4292 slicelen);
4445 } 4293 }
4446 dest = (char *)PyMem_Malloc(slicelen); 4294 dest = (char *)PyMem_Malloc(slicelen);
4447 4295
4448 if (dest == NULL) 4296 if (dest == NULL)
4449 return PyErr_NoMemory(); 4297 return PyErr_NoMemory();
4450 4298
4451 for (cur = start, i = 0; i < slicelen; 4299 for (cur = start, i = 0; i < slicelen;
4452 cur += step, i++) { 4300 cur += step, i++) {
4453 dest[i] = ptr[cur]; 4301 dest[i] = ptr[cur];
4454 } 4302 }
4455 4303
4456 np = PyString_FromStringAndSize(dest, slicelen); 4304 np = PyBytes_FromStringAndSize(dest, slicelen);
4457 PyMem_Free(dest); 4305 PyMem_Free(dest);
4458 return np; 4306 return np;
4459 } 4307 }
4460 #ifdef CTYPES_UNICODE 4308 #ifdef CTYPES_UNICODE
4461 if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { 4309 if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
4462 wchar_t *ptr = (wchar_t *)self->b_ptr; 4310 wchar_t *ptr = (wchar_t *)self->b_ptr;
4463 wchar_t *dest; 4311 wchar_t *dest;
4464 4312
4465 if (slicelen <= 0) 4313 if (slicelen <= 0)
4466 return PyUnicode_FromUnicode(NULL, 0); 4314 return PyUnicode_New(0, 0);
4467 if (step == 1) { 4315 if (step == 1) {
4468 return PyUnicode_FromWideChar(ptr + start, 4316 return PyUnicode_FromWideChar(ptr + start,
4469 slicelen); 4317 slicelen);
4470 } 4318 }
4471 4319
4472 dest = (wchar_t *)PyMem_Malloc( 4320 dest = (wchar_t *)PyMem_Malloc(
4473 slicelen * sizeof(wchar_t)); 4321 slicelen * sizeof(wchar_t));
4474 4322
4475 for (cur = start, i = 0; i < slicelen; 4323 for (cur = start, i = 0; i < slicelen;
4476 cur += step, i++) { 4324 cur += step, i++) {
4477 dest[i] = ptr[cur]; 4325 dest[i] = ptr[cur];
4478 } 4326 }
4479 4327
4480 np = PyUnicode_FromWideChar(dest, slicelen); 4328 np = PyUnicode_FromWideChar(dest, slicelen);
4481 PyMem_Free(dest); 4329 PyMem_Free(dest);
4482 return np; 4330 return np;
4483 } 4331 }
4484 #endif 4332 #endif
4485 4333
4486 np = PyList_New(slicelen); 4334 np = PyList_New(slicelen);
4487 if (np == NULL) 4335 if (np == NULL)
4488 return NULL; 4336 return NULL;
4489 4337
4490 for (cur = start, i = 0; i < slicelen; 4338 for (cur = start, i = 0; i < slicelen;
4491 cur += step, i++) { 4339 cur += step, i++) {
4492 PyObject *v = Array_item(_self, cur); 4340 PyObject *v = Array_item(myself, cur);
4341 if (v == NULL) {
4342 Py_DECREF(np);
4343 return NULL;
4344 }
4493 PyList_SET_ITEM(np, i, v); 4345 PyList_SET_ITEM(np, i, v);
4494 } 4346 }
4495 return np; 4347 return np;
4496 } 4348 }
4497 else { 4349 else {
4498 PyErr_SetString(PyExc_TypeError, 4350 PyErr_SetString(PyExc_TypeError,
4499 "indices must be integers"); 4351 "indices must be integers");
4500 return NULL; 4352 return NULL;
4501 } 4353 }
4502 4354
4503 } 4355 }
4504 4356
4505 static int 4357 static int
4506 Array_ass_item(PyObject *_self, Py_ssize_t index, PyObject *value) 4358 Array_ass_item(PyObject *myself, Py_ssize_t index, PyObject *value)
4507 { 4359 {
4508 CDataObject *self = (CDataObject *)_self; 4360 CDataObject *self = (CDataObject *)myself;
4509 Py_ssize_t size, offset; 4361 Py_ssize_t size, offset;
4510 StgDictObject *stgdict; 4362 StgDictObject *stgdict;
4511 char *ptr; 4363 char *ptr;
4512 4364
4513 if (value == NULL) { 4365 if (value == NULL) {
4514 PyErr_SetString(PyExc_TypeError, 4366 PyErr_SetString(PyExc_TypeError,
4515 "Array does not support item deletion"); 4367 "Array does not support item deletion");
4516 return -1; 4368 return -1;
4517 } 4369 }
4518 4370
4519 stgdict = PyObject_stgdict((PyObject *)self); 4371 stgdict = PyObject_stgdict((PyObject *)self);
4520 assert(stgdict); /* Cannot be NULL for array object instances */ 4372 assert(stgdict); /* Cannot be NULL for array object instances */
4521 if (index < 0 || index >= stgdict->length) { 4373 if (index < 0 || index >= stgdict->length) {
4522 PyErr_SetString(PyExc_IndexError, 4374 PyErr_SetString(PyExc_IndexError,
4523 "invalid index"); 4375 "invalid index");
4524 return -1; 4376 return -1;
4525 } 4377 }
4526 size = stgdict->size / stgdict->length; 4378 size = stgdict->size / stgdict->length;
4527 offset = index * size; 4379 offset = index * size;
4528 ptr = self->b_ptr + offset; 4380 ptr = self->b_ptr + offset;
4529 4381
4530 return PyCData_set((PyObject *)self, stgdict->proto, stgdict->setfunc, value , 4382 return PyCData_set((PyObject *)self, stgdict->proto, stgdict->setfunc, value ,
4531 index, size, ptr); 4383 index, size, ptr);
4532 } 4384 }
4533 4385
4534 static int 4386 static int
4535 Array_ass_slice(PyObject *_self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *va lue) 4387 Array_ass_subscript(PyObject *myself, PyObject *item, PyObject *value)
4536 { 4388 {
4537 CDataObject *self = (CDataObject *)_self; 4389 CDataObject *self = (CDataObject *)myself;
4538 Py_ssize_t i, len;
4539 4390
4540 if (value == NULL) { 4391 if (value == NULL) {
4541 PyErr_SetString(PyExc_TypeError, 4392 PyErr_SetString(PyExc_TypeError,
4542 "Array does not support item deletion"); 4393 "Array does not support item deletion");
4543 return -1; 4394 return -1;
4544 } 4395 }
4545 4396
4546 if (ilow < 0)
4547 ilow = 0;
4548 else if (ilow > self->b_length)
4549 ilow = self->b_length;
4550 if (ihigh < 0)
4551 ihigh = 0;
4552 if (ihigh < ilow)
4553 ihigh = ilow;
4554 else if (ihigh > self->b_length)
4555 ihigh = self->b_length;
4556
4557 len = PySequence_Length(value);
4558 if (len != ihigh - ilow) {
4559 PyErr_SetString(PyExc_ValueError,
4560 "Can only assign sequence of same size");
4561 return -1;
4562 }
4563 for (i = 0; i < len; i++) {
4564 PyObject *item = PySequence_GetItem(value, i);
4565 int result;
4566 if (item == NULL)
4567 return -1;
4568 result = Array_ass_item(_self, i+ilow, item);
4569 Py_DECREF(item);
4570 if (result == -1)
4571 return -1;
4572 }
4573 return 0;
4574 }
4575
4576 static int
4577 Array_ass_subscript(PyObject *_self, PyObject *item, PyObject *value)
4578 {
4579 CDataObject *self = (CDataObject *)_self;
4580
4581 if (value == NULL) {
4582 PyErr_SetString(PyExc_TypeError,
4583 "Array does not support item deletion");
4584 return -1;
4585 }
4586
4587 if (PyIndex_Check(item)) { 4397 if (PyIndex_Check(item)) {
4588 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); 4398 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
4589 4399
4590 if (i == -1 && PyErr_Occurred()) 4400 if (i == -1 && PyErr_Occurred())
4591 return -1; 4401 return -1;
4592 if (i < 0) 4402 if (i < 0)
4593 i += self->b_length; 4403 i += self->b_length;
4594 return Array_ass_item(_self, i, value); 4404 return Array_ass_item(myself, i, value);
4595 } 4405 }
4596 else if (PySlice_Check(item)) { 4406 else if (PySlice_Check(item)) {
4597 Py_ssize_t start, stop, step, slicelen, otherlen, i, cur; 4407 Py_ssize_t start, stop, step, slicelen, otherlen, i, cur;
4598 4408
4599 if (PySlice_GetIndicesEx((PySliceObject *)item, 4409 if (PySlice_GetIndicesEx(item,
4600 self->b_length, &start, &stop, 4410 self->b_length, &start, &stop,
4601 &step, &slicelen) < 0) { 4411 &step, &slicelen) < 0) {
4602 return -1; 4412 return -1;
4603 } 4413 }
4604 if ((step < 0 && start < stop) || 4414 if ((step < 0 && start < stop) ||
4605 (step > 0 && start > stop)) 4415 (step > 0 && start > stop))
4606 stop = start; 4416 stop = start;
4607 4417
4608 otherlen = PySequence_Length(value); 4418 otherlen = PySequence_Length(value);
4609 if (otherlen != slicelen) { 4419 if (otherlen != slicelen) {
4610 PyErr_SetString(PyExc_ValueError, 4420 PyErr_SetString(PyExc_ValueError,
4611 "Can only assign sequence of same size"); 4421 "Can only assign sequence of same size");
4612 return -1; 4422 return -1;
4613 } 4423 }
4614 for (cur = start, i = 0; i < otherlen; cur += step, i++) { 4424 for (cur = start, i = 0; i < otherlen; cur += step, i++) {
4615 PyObject *item = PySequence_GetItem(value, i); 4425 PyObject *item = PySequence_GetItem(value, i);
4616 int result; 4426 int result;
4617 if (item == NULL) 4427 if (item == NULL)
4618 return -1; 4428 return -1;
4619 result = Array_ass_item(_self, cur, item); 4429 result = Array_ass_item(myself, cur, item);
4620 Py_DECREF(item); 4430 Py_DECREF(item);
4621 if (result == -1) 4431 if (result == -1)
4622 return -1; 4432 return -1;
4623 } 4433 }
4624 return 0; 4434 return 0;
4625 } 4435 }
4626 else { 4436 else {
4627 PyErr_SetString(PyExc_TypeError, 4437 PyErr_SetString(PyExc_TypeError,
4628 "indices must be integer"); 4438 "indices must be integer");
4629 return -1; 4439 return -1;
4630 } 4440 }
4631 } 4441 }
4632 4442
4633 static Py_ssize_t 4443 static Py_ssize_t
4634 Array_length(PyObject *_self) 4444 Array_length(PyObject *myself)
4635 { 4445 {
4636 CDataObject *self = (CDataObject *)_self; 4446 CDataObject *self = (CDataObject *)myself;
4637 return self->b_length; 4447 return self->b_length;
4638 } 4448 }
4639 4449
4640 static PySequenceMethods Array_as_sequence = { 4450 static PySequenceMethods Array_as_sequence = {
4641 Array_length, /* sq_length; */ 4451 Array_length, /* sq_length; */
4642 0, /* sq_concat; */ 4452 0, /* sq_concat; */
4643 0, /* sq_repeat; */ 4453 0, /* sq_repeat; */
4644 Array_item, /* sq_item; */ 4454 Array_item, /* sq_item; */
4645 Array_slice, /* sq_slice; */ 4455 0, /* sq_slice; */
4646 Array_ass_item, /* sq_ass_item; */ 4456 Array_ass_item, /* sq_ass_item; */
4647 Array_ass_slice, /* sq_ass_slice; */ 4457 0, /* sq_ass_slice; */
4648 0, /* sq_contains; */ 4458 0, /* sq_contains; */
4649 4459
4650 0, /* sq_inplace_concat; */ 4460 0, /* sq_inplace_concat; */
4651 0, /* sq_inplace_repeat; */ 4461 0, /* sq_inplace_repeat; */
4652 }; 4462 };
4653 4463
4654 static PyMappingMethods Array_as_mapping = { 4464 static PyMappingMethods Array_as_mapping = {
4655 Array_length, 4465 Array_length,
4656 Array_subscript, 4466 Array_subscript,
4657 Array_ass_subscript, 4467 Array_ass_subscript,
4658 }; 4468 };
4659 4469
4660 PyTypeObject PyCArray_Type = { 4470 PyTypeObject PyCArray_Type = {
4661 PyVarObject_HEAD_INIT(NULL, 0) 4471 PyVarObject_HEAD_INIT(NULL, 0)
4662 "_ctypes.Array", 4472 "_ctypes.Array",
4663 sizeof(CDataObject), /* tp_basicsize */ 4473 sizeof(CDataObject), /* tp_basicsize */
4664 0, /* tp_itemsize */ 4474 0, /* tp_itemsize */
4665 0, /* tp_dealloc */ 4475 0, /* tp_dealloc */
4666 0, /* tp_print */ 4476 0, /* tp_print */
4667 0, /* tp_getattr */ 4477 0, /* tp_getattr */
4668 0, /* tp_setattr */ 4478 0, /* tp_setattr */
4669 0, /* tp_compare */ 4479 0, /* tp_reserved */
4670 0, /* tp_repr */ 4480 0, /* tp_repr */
4671 0, /* tp_as_number */ 4481 0, /* tp_as_number */
4672 &Array_as_sequence, /* tp_as_sequence */ 4482 &Array_as_sequence, /* tp_as_sequence */
4673 &Array_as_mapping, /* tp_as_mapping */ 4483 &Array_as_mapping, /* tp_as_mapping */
4674 0, /* tp_hash */ 4484 0, /* tp_hash */
4675 0, /* tp_call */ 4485 0, /* tp_call */
4676 0, /* tp_str */ 4486 0, /* tp_str */
4677 0, /* tp_getattro */ 4487 0, /* tp_getattro */
4678 0, /* tp_setattro */ 4488 0, /* tp_setattro */
4679 &PyCData_as_buffer, /* tp_as_buffer */ 4489 &PyCData_as_buffer, /* tp_as_buffer */
4680 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE, /* tp_ flags */ 4490 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4681 "XXX to be provided", /* tp_doc */ 4491 "XXX to be provided", /* tp_doc */
4682 (traverseproc)PyCData_traverse, /* tp_traverse */ 4492 (traverseproc)PyCData_traverse, /* tp_traverse */
4683 (inquiry)PyCData_clear, /* tp_clear */ 4493 (inquiry)PyCData_clear, /* tp_clear */
4684 0, /* tp_richcompare */ 4494 0, /* tp_richcompare */
4685 0, /* tp_weaklistoffset */ 4495 0, /* tp_weaklistoffset */
4686 0, /* tp_iter */ 4496 0, /* tp_iter */
4687 0, /* tp_iternext */ 4497 0, /* tp_iternext */
4688 0, /* tp_methods */ 4498 0, /* tp_methods */
4689 0, /* tp_members */ 4499 0, /* tp_members */
4690 0, /* tp_getset */ 4500 0, /* tp_getset */
(...skipping 15 matching lines...) Expand all
4706 PyObject *key; 4516 PyObject *key;
4707 PyObject *result; 4517 PyObject *result;
4708 char name[256]; 4518 char name[256];
4709 PyObject *len; 4519 PyObject *len;
4710 4520
4711 if (cache == NULL) { 4521 if (cache == NULL) {
4712 cache = PyDict_New(); 4522 cache = PyDict_New();
4713 if (cache == NULL) 4523 if (cache == NULL)
4714 return NULL; 4524 return NULL;
4715 } 4525 }
4716 len = PyInt_FromSsize_t(length); 4526 len = PyLong_FromSsize_t(length);
4717 if (len == NULL) 4527 if (len == NULL)
4718 return NULL; 4528 return NULL;
4719 key = PyTuple_Pack(2, itemtype, len); 4529 key = PyTuple_Pack(2, itemtype, len);
4720 Py_DECREF(len); 4530 Py_DECREF(len);
4721 if (!key) 4531 if (!key)
4722 return NULL; 4532 return NULL;
4723 result = PyDict_GetItemProxy(cache, key); 4533 result = PyDict_GetItemProxy(cache, key);
4724 if (result) { 4534 if (result) {
4725 Py_INCREF(result); 4535 Py_INCREF(result);
4726 Py_DECREF(key); 4536 Py_DECREF(key);
4727 return result; 4537 return result;
4728 } 4538 }
4729 4539
4730 if (!PyType_Check(itemtype)) { 4540 if (!PyType_Check(itemtype)) {
4731 PyErr_SetString(PyExc_TypeError, 4541 PyErr_SetString(PyExc_TypeError,
4732 "Expected a type object"); 4542 "Expected a type object");
4733 Py_DECREF(key); 4543 Py_DECREF(key);
4734 return NULL; 4544 return NULL;
4735 } 4545 }
4736 #ifdef MS_WIN64 4546 #ifdef MS_WIN64
4737 sprintf(name, "%.200s_Array_%Id", 4547 sprintf(name, "%.200s_Array_%Id",
4738 ((PyTypeObject *)itemtype)->tp_name, length); 4548 ((PyTypeObject *)itemtype)->tp_name, length);
4739 #else 4549 #else
4740 sprintf(name, "%.200s_Array_%ld", 4550 sprintf(name, "%.200s_Array_%ld",
4741 ((PyTypeObject *)itemtype)->tp_name, (long)length); 4551 ((PyTypeObject *)itemtype)->tp_name, (long)length);
4742 #endif 4552 #endif
4743 4553
4744 result = PyObject_CallFunction((PyObject *)&PyCArrayType_Type, 4554 result = PyObject_CallFunction((PyObject *)&PyCArrayType_Type,
4745 #if (PY_VERSION_HEX < 0x02050000)
4746 "s(O){s:i,s:O}",
4747 #else
4748 "s(O){s:n,s:O}", 4555 "s(O){s:n,s:O}",
4749 #endif
4750 name, 4556 name,
4751 &PyCArray_Type, 4557 &PyCArray_Type,
4752 "_length_", 4558 "_length_",
4753 length, 4559 length,
4754 "_type_", 4560 "_type_",
4755 itemtype 4561 itemtype
4756 ); 4562 );
4757 if (result == NULL) { 4563 if (result == NULL) {
4758 Py_DECREF(key); 4564 Py_DECREF(key);
4759 return NULL; 4565 return NULL;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
4830 } 4636 }
4831 /* call stgdict->getfunc */ 4637 /* call stgdict->getfunc */
4832 return Simple_get_value((CDataObject *)self); 4638 return Simple_get_value((CDataObject *)self);
4833 } 4639 }
4834 4640
4835 static PyMethodDef Simple_methods[] = { 4641 static PyMethodDef Simple_methods[] = {
4836 { "__ctypes_from_outparam__", Simple_from_outparm, METH_NOARGS, }, 4642 { "__ctypes_from_outparam__", Simple_from_outparm, METH_NOARGS, },
4837 { NULL, NULL }, 4643 { NULL, NULL },
4838 }; 4644 };
4839 4645
4840 static int Simple_nonzero(CDataObject *self) 4646 static int Simple_bool(CDataObject *self)
4841 { 4647 {
4842 return memcmp(self->b_ptr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", self->b_size) ; 4648 return memcmp(self->b_ptr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", self->b_size) ;
4843 } 4649 }
4844 4650
4845 static PyNumberMethods Simple_as_number = { 4651 static PyNumberMethods Simple_as_number = {
4846 0, /* nb_add */ 4652 0, /* nb_add */
4847 0, /* nb_subtract */ 4653 0, /* nb_subtract */
4848 0, /* nb_multiply */ 4654 0, /* nb_multiply */
4849 0, /* nb_divide */
4850 0, /* nb_remainder */ 4655 0, /* nb_remainder */
4851 0, /* nb_divmod */ 4656 0, /* nb_divmod */
4852 0, /* nb_power */ 4657 0, /* nb_power */
4853 0, /* nb_negative */ 4658 0, /* nb_negative */
4854 0, /* nb_positive */ 4659 0, /* nb_positive */
4855 0, /* nb_absolute */ 4660 0, /* nb_absolute */
4856 (inquiry)Simple_nonzero, /* nb_nonzero */ 4661 (inquiry)Simple_bool, /* nb_bool */
4857 }; 4662 };
4858 4663
4859 /* "%s(%s)" % (self.__class__.__name__, self.value) */ 4664 /* "%s(%s)" % (self.__class__.__name__, self.value) */
4860 static PyObject * 4665 static PyObject *
4861 Simple_repr(CDataObject *self) 4666 Simple_repr(CDataObject *self)
4862 { 4667 {
4863 PyObject *val, *name, *args, *result; 4668 PyObject *val, *result;
4864 static PyObject *format;
4865 4669
4866 if (Py_TYPE(self)->tp_base != &Simple_Type) { 4670 if (Py_TYPE(self)->tp_base != &Simple_Type) {
4867 return PyString_FromFormat("<%s object at %p>", 4671 return PyUnicode_FromFormat("<%s object at %p>",
4868 Py_TYPE(self)->tp_name, self); 4672 Py_TYPE(self)->tp_name, self);
4869 }
4870
4871 if (format == NULL) {
4872 format = PyString_InternFromString("%s(%r)");
4873 if (format == NULL)
4874 return NULL;
4875 } 4673 }
4876 4674
4877 val = Simple_get_value(self); 4675 val = Simple_get_value(self);
4878 if (val == NULL) 4676 if (val == NULL)
4879 return NULL; 4677 return NULL;
4880 4678
4881 name = PyString_FromString(Py_TYPE(self)->tp_name); 4679 result = PyUnicode_FromFormat("%s(%R)",
4882 if (name == NULL) { 4680 Py_TYPE(self)->tp_name, val);
4883 Py_DECREF(val);
4884 return NULL;
4885 }
4886
4887 args = PyTuple_Pack(2, name, val);
4888 Py_DECREF(name);
4889 Py_DECREF(val); 4681 Py_DECREF(val);
4890 if (args == NULL)
4891 return NULL;
4892
4893 result = PyString_Format(format, args);
4894 Py_DECREF(args);
4895 return result; 4682 return result;
4896 } 4683 }
4897 4684
4898 static PyTypeObject Simple_Type = { 4685 static PyTypeObject Simple_Type = {
4899 PyVarObject_HEAD_INIT(NULL, 0) 4686 PyVarObject_HEAD_INIT(NULL, 0)
4900 "_ctypes._SimpleCData", 4687 "_ctypes._SimpleCData",
4901 sizeof(CDataObject), /* tp_basicsize */ 4688 sizeof(CDataObject), /* tp_basicsize */
4902 0, /* tp_itemsize */ 4689 0, /* tp_itemsize */
4903 0, /* tp_dealloc */ 4690 0, /* tp_dealloc */
4904 0, /* tp_print */ 4691 0, /* tp_print */
4905 0, /* tp_getattr */ 4692 0, /* tp_getattr */
4906 0, /* tp_setattr */ 4693 0, /* tp_setattr */
4907 0, /* tp_compare */ 4694 0, /* tp_reserved */
4908 (reprfunc)&Simple_repr, /* tp_repr */ 4695 (reprfunc)&Simple_repr, /* tp_repr */
4909 &Simple_as_number, /* tp_as_number */ 4696 &Simple_as_number, /* tp_as_number */
4910 0, /* tp_as_sequence */ 4697 0, /* tp_as_sequence */
4911 0, /* tp_as_mapping */ 4698 0, /* tp_as_mapping */
4912 0, /* tp_hash */ 4699 0, /* tp_hash */
4913 0, /* tp_call */ 4700 0, /* tp_call */
4914 0, /* tp_str */ 4701 0, /* tp_str */
4915 0, /* tp_getattro */ 4702 0, /* tp_getattro */
4916 0, /* tp_setattro */ 4703 0, /* tp_setattro */
4917 &PyCData_as_buffer, /* tp_as_buffer */ 4704 &PyCData_as_buffer, /* tp_as_buffer */
4918 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE, /* tp_ flags */ 4705 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4919 "XXX to be provided", /* tp_doc */ 4706 "XXX to be provided", /* tp_doc */
4920 (traverseproc)PyCData_traverse, /* tp_traverse */ 4707 (traverseproc)PyCData_traverse, /* tp_traverse */
4921 (inquiry)PyCData_clear, /* tp_clear */ 4708 (inquiry)PyCData_clear, /* tp_clear */
4922 0, /* tp_richcompare */ 4709 0, /* tp_richcompare */
4923 0, /* tp_weaklistoffset */ 4710 0, /* tp_weaklistoffset */
4924 0, /* tp_iter */ 4711 0, /* tp_iter */
4925 0, /* tp_iternext */ 4712 0, /* tp_iternext */
4926 Simple_methods, /* tp_methods */ 4713 Simple_methods, /* tp_methods */
4927 0, /* tp_members */ 4714 0, /* tp_members */
4928 Simple_getsets, /* tp_getset */ 4715 Simple_getsets, /* tp_getset */
4929 0, /* tp_base */ 4716 0, /* tp_base */
4930 0, /* tp_dict */ 4717 0, /* tp_dict */
4931 0, /* tp_descr_get */ 4718 0, /* tp_descr_get */
4932 0, /* tp_descr_set */ 4719 0, /* tp_descr_set */
4933 0, /* tp_dictoffset */ 4720 0, /* tp_dictoffset */
4934 (initproc)Simple_init, /* tp_init */ 4721 (initproc)Simple_init, /* tp_init */
4935 0, /* tp_alloc */ 4722 0, /* tp_alloc */
4936 GenericPyCData_new, /* tp_new */ 4723 GenericPyCData_new, /* tp_new */
4937 0, /* tp_free */ 4724 0, /* tp_free */
4938 }; 4725 };
4939 4726
4940 /******************************************************************/ 4727 /******************************************************************/
4941 /* 4728 /*
4942 PyCPointer_Type 4729 PyCPointer_Type
4943 */ 4730 */
4944 static PyObject * 4731 static PyObject *
4945 Pointer_item(PyObject *_self, Py_ssize_t index) 4732 Pointer_item(PyObject *myself, Py_ssize_t index)
4946 { 4733 {
4947 CDataObject *self = (CDataObject *)_self; 4734 CDataObject *self = (CDataObject *)myself;
4948 Py_ssize_t size; 4735 Py_ssize_t size;
4949 Py_ssize_t offset; 4736 Py_ssize_t offset;
4950 StgDictObject *stgdict, *itemdict; 4737 StgDictObject *stgdict, *itemdict;
4951 PyObject *proto; 4738 PyObject *proto;
4952 4739
4953 if (*(void **)self->b_ptr == NULL) { 4740 if (*(void **)self->b_ptr == NULL) {
4954 PyErr_SetString(PyExc_ValueError, 4741 PyErr_SetString(PyExc_ValueError,
4955 "NULL pointer access"); 4742 "NULL pointer access");
4956 return NULL; 4743 return NULL;
4957 } 4744 }
4958 4745
4959 stgdict = PyObject_stgdict((PyObject *)self); 4746 stgdict = PyObject_stgdict((PyObject *)self);
4960 assert(stgdict); /* Cannot be NULL for pointer object instances */ 4747 assert(stgdict); /* Cannot be NULL for pointer object instances */
4961 4748
4962 proto = stgdict->proto; 4749 proto = stgdict->proto;
4963 assert(proto); 4750 assert(proto);
4964 itemdict = PyType_stgdict(proto); 4751 itemdict = PyType_stgdict(proto);
4965 assert(itemdict); /* proto is the item type of the pointer, a ctypes 4752 assert(itemdict); /* proto is the item type of the pointer, a ctypes
4966 type, so this cannot be NULL */ 4753 type, so this cannot be NULL */
4967 4754
4968 size = itemdict->size; 4755 size = itemdict->size;
4969 offset = index * itemdict->size; 4756 offset = index * itemdict->size;
4970 4757
4971 return PyCData_get(proto, stgdict->getfunc, (PyObject *)self, 4758 return PyCData_get(proto, stgdict->getfunc, (PyObject *)self,
4972 index, size, (*(char **)self->b_ptr) + offset); 4759 index, size, (*(char **)self->b_ptr) + offset);
4973 } 4760 }
4974 4761
4975 static int 4762 static int
4976 Pointer_ass_item(PyObject *_self, Py_ssize_t index, PyObject *value) 4763 Pointer_ass_item(PyObject *myself, Py_ssize_t index, PyObject *value)
4977 { 4764 {
4978 CDataObject *self = (CDataObject *)_self; 4765 CDataObject *self = (CDataObject *)myself;
4979 Py_ssize_t size; 4766 Py_ssize_t size;
4980 Py_ssize_t offset; 4767 Py_ssize_t offset;
4981 StgDictObject *stgdict, *itemdict; 4768 StgDictObject *stgdict, *itemdict;
4982 PyObject *proto; 4769 PyObject *proto;
4983 4770
4984 if (value == NULL) { 4771 if (value == NULL) {
4985 PyErr_SetString(PyExc_TypeError, 4772 PyErr_SetString(PyExc_TypeError,
4986 "Pointer does not support item deletion"); 4773 "Pointer does not support item deletion");
4987 return -1; 4774 return -1;
4988 } 4775 }
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
5062 /* 4849 /*
5063 A Pointer instance must keep a the value it points to alive. So, a 4850 A Pointer instance must keep a the value it points to alive. So, a
5064 pointer instance has b_length set to 2 instead of 1, and we set 4851 pointer instance has b_length set to 2 instead of 1, and we set
5065 'value' itself as the second item of the b_objects list, additionally. 4852 'value' itself as the second item of the b_objects list, additionally.
5066 */ 4853 */
5067 Py_INCREF(value); 4854 Py_INCREF(value);
5068 if (-1 == KeepRef(self, 1, value)) 4855 if (-1 == KeepRef(self, 1, value))
5069 return -1; 4856 return -1;
5070 4857
5071 keep = GetKeepedObjects(dst); 4858 keep = GetKeepedObjects(dst);
4859 if (keep == NULL)
4860 return -1;
4861
5072 Py_INCREF(keep); 4862 Py_INCREF(keep);
5073 return KeepRef(self, 0, keep); 4863 return KeepRef(self, 0, keep);
5074 } 4864 }
5075 4865
5076 static PyGetSetDef Pointer_getsets[] = { 4866 static PyGetSetDef Pointer_getsets[] = {
5077 { "contents", (getter)Pointer_get_contents, 4867 { "contents", (getter)Pointer_get_contents,
5078 (setter)Pointer_set_contents, 4868 (setter)Pointer_set_contents,
5079 "the object this pointer points to (read-write)", NULL }, 4869 "the object this pointer points to (read-write)", NULL },
5080 { NULL, NULL } 4870 { NULL, NULL }
5081 }; 4871 };
(...skipping 16 matching lines...) Expand all
5098 StgDictObject *dict = PyType_stgdict((PyObject *)type); 4888 StgDictObject *dict = PyType_stgdict((PyObject *)type);
5099 if (!dict || !dict->proto) { 4889 if (!dict || !dict->proto) {
5100 PyErr_SetString(PyExc_TypeError, 4890 PyErr_SetString(PyExc_TypeError,
5101 "Cannot create instance: has no _type_"); 4891 "Cannot create instance: has no _type_");
5102 return NULL; 4892 return NULL;
5103 } 4893 }
5104 return GenericPyCData_new(type, args, kw); 4894 return GenericPyCData_new(type, args, kw);
5105 } 4895 }
5106 4896
5107 static PyObject * 4897 static PyObject *
5108 Pointer_slice(PyObject *_self, Py_ssize_t ilow, Py_ssize_t ihigh) 4898 Pointer_subscript(PyObject *myself, PyObject *item)
5109 { 4899 {
5110 CDataObject *self = (CDataObject *)_self; 4900 CDataObject *self = (CDataObject *)myself;
5111 PyListObject *np;
5112 StgDictObject *stgdict, *itemdict;
5113 PyObject *proto;
5114 Py_ssize_t i, len;
5115
5116 if (ilow < 0)
5117 ilow = 0;
5118 if (ihigh < ilow)
5119 ihigh = ilow;
5120 len = ihigh - ilow;
5121
5122 stgdict = PyObject_stgdict((PyObject *)self);
5123 assert(stgdict); /* Cannot be NULL fr pointer instances */
5124 proto = stgdict->proto;
5125 assert(proto);
5126 itemdict = PyType_stgdict(proto);
5127 assert(itemdict);
5128 if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
5129 char *ptr = *(char **)self->b_ptr;
5130 return PyString_FromStringAndSize(ptr + ilow, len);
5131 #ifdef CTYPES_UNICODE
5132 } else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
5133 wchar_t *ptr = *(wchar_t **)self->b_ptr;
5134 return PyUnicode_FromWideChar(ptr + ilow, len);
5135 #endif
5136 }
5137
5138 np = (PyListObject *) PyList_New(len);
5139 if (np == NULL)
5140 return NULL;
5141
5142 for (i = 0; i < len; i++) {
5143 PyObject *v = Pointer_item(_self, i+ilow);
5144 PyList_SET_ITEM(np, i, v);
5145 }
5146 return (PyObject *)np;
5147 }
5148
5149 static PyObject *
5150 Pointer_subscript(PyObject *_self, PyObject *item)
5151 {
5152 CDataObject *self = (CDataObject *)_self;
5153 if (PyIndex_Check(item)) { 4901 if (PyIndex_Check(item)) {
5154 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); 4902 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
5155 if (i == -1 && PyErr_Occurred()) 4903 if (i == -1 && PyErr_Occurred())
5156 return NULL; 4904 return NULL;
5157 return Pointer_item(_self, i); 4905 return Pointer_item(myself, i);
5158 } 4906 }
5159 else if (PySlice_Check(item)) { 4907 else if (PySlice_Check(item)) {
5160 PySliceObject *slice = (PySliceObject *)item; 4908 PySliceObject *slice = (PySliceObject *)item;
5161 Py_ssize_t start, stop, step; 4909 Py_ssize_t start, stop, step;
5162 PyObject *np; 4910 PyObject *np;
5163 StgDictObject *stgdict, *itemdict; 4911 StgDictObject *stgdict, *itemdict;
5164 PyObject *proto; 4912 PyObject *proto;
5165 Py_ssize_t i, len, cur; 4913 Py_ssize_t i, len, cur;
5166 4914
5167 /* Since pointers have no length, and we want to apply 4915 /* Since pointers have no length, and we want to apply
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
5217 assert(stgdict); /* Cannot be NULL for pointer instances */ 4965 assert(stgdict); /* Cannot be NULL for pointer instances */
5218 proto = stgdict->proto; 4966 proto = stgdict->proto;
5219 assert(proto); 4967 assert(proto);
5220 itemdict = PyType_stgdict(proto); 4968 itemdict = PyType_stgdict(proto);
5221 assert(itemdict); 4969 assert(itemdict);
5222 if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) { 4970 if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
5223 char *ptr = *(char **)self->b_ptr; 4971 char *ptr = *(char **)self->b_ptr;
5224 char *dest; 4972 char *dest;
5225 4973
5226 if (len <= 0) 4974 if (len <= 0)
5227 return PyString_FromString(""); 4975 return PyBytes_FromStringAndSize("", 0);
5228 if (step == 1) { 4976 if (step == 1) {
5229 return PyString_FromStringAndSize(ptr + start, 4977 return PyBytes_FromStringAndSize(ptr + start,
5230 len); 4978 len);
5231 } 4979 }
5232 dest = (char *)PyMem_Malloc(len); 4980 dest = (char *)PyMem_Malloc(len);
5233 if (dest == NULL) 4981 if (dest == NULL)
5234 return PyErr_NoMemory(); 4982 return PyErr_NoMemory();
5235 for (cur = start, i = 0; i < len; cur += step, i++) { 4983 for (cur = start, i = 0; i < len; cur += step, i++) {
5236 dest[i] = ptr[cur]; 4984 dest[i] = ptr[cur];
5237 } 4985 }
5238 np = PyString_FromStringAndSize(dest, len); 4986 np = PyBytes_FromStringAndSize(dest, len);
5239 PyMem_Free(dest); 4987 PyMem_Free(dest);
5240 return np; 4988 return np;
5241 } 4989 }
5242 #ifdef CTYPES_UNICODE 4990 #ifdef CTYPES_UNICODE
5243 if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { 4991 if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
5244 wchar_t *ptr = *(wchar_t **)self->b_ptr; 4992 wchar_t *ptr = *(wchar_t **)self->b_ptr;
5245 wchar_t *dest; 4993 wchar_t *dest;
5246 4994
5247 if (len <= 0) 4995 if (len <= 0)
5248 return PyUnicode_FromUnicode(NULL, 0); 4996 return PyUnicode_New(0, 0);
5249 if (step == 1) { 4997 if (step == 1) {
5250 return PyUnicode_FromWideChar(ptr + start, 4998 return PyUnicode_FromWideChar(ptr + start,
5251 len); 4999 len);
5252 } 5000 }
5253 dest = (wchar_t *)PyMem_Malloc(len * sizeof(wchar_t)); 5001 dest = (wchar_t *)PyMem_Malloc(len * sizeof(wchar_t));
5254 if (dest == NULL) 5002 if (dest == NULL)
5255 return PyErr_NoMemory(); 5003 return PyErr_NoMemory();
5256 for (cur = start, i = 0; i < len; cur += step, i++) { 5004 for (cur = start, i = 0; i < len; cur += step, i++) {
5257 dest[i] = ptr[cur]; 5005 dest[i] = ptr[cur];
5258 } 5006 }
5259 np = PyUnicode_FromWideChar(dest, len); 5007 np = PyUnicode_FromWideChar(dest, len);
5260 PyMem_Free(dest); 5008 PyMem_Free(dest);
5261 return np; 5009 return np;
5262 } 5010 }
5263 #endif 5011 #endif
5264 5012
5265 np = PyList_New(len); 5013 np = PyList_New(len);
5266 if (np == NULL) 5014 if (np == NULL)
5267 return NULL; 5015 return NULL;
5268 5016
5269 for (cur = start, i = 0; i < len; cur += step, i++) { 5017 for (cur = start, i = 0; i < len; cur += step, i++) {
5270 PyObject *v = Pointer_item(_self, cur); 5018 PyObject *v = Pointer_item(myself, cur);
5271 PyList_SET_ITEM(np, i, v); 5019 PyList_SET_ITEM(np, i, v);
5272 } 5020 }
5273 return np; 5021 return np;
5274 } 5022 }
5275 else { 5023 else {
5276 PyErr_SetString(PyExc_TypeError, 5024 PyErr_SetString(PyExc_TypeError,
5277 "Pointer indices must be integer"); 5025 "Pointer indices must be integer");
5278 return NULL; 5026 return NULL;
5279 } 5027 }
5280 } 5028 }
5281 5029
5282 static PySequenceMethods Pointer_as_sequence = { 5030 static PySequenceMethods Pointer_as_sequence = {
5283 0, /* inquiry sq_length; */ 5031 0, /* inquiry sq_length; */
5284 0, /* binaryfunc sq_concat; */ 5032 0, /* binaryfunc sq_concat; */
5285 0, /* intargfunc sq_repeat; */ 5033 0, /* intargfunc sq_repeat; */
5286 Pointer_item, /* intargfunc sq_item; */ 5034 Pointer_item, /* intargfunc sq_item; */
5287 Pointer_slice, /* intintargfunc sq_slice; */ 5035 0, /* intintargfunc sq_slice; */
5288 Pointer_ass_item, /* intobjargproc sq_ass_item; */ 5036 Pointer_ass_item, /* intobjargproc sq_ass_item; */
5289 0, /* intintobjargproc sq_ass_slice ; */ 5037 0, /* intintobjargproc sq_ass_slice ; */
5290 0, /* objobjproc sq_contains; */ 5038 0, /* objobjproc sq_contains; */
5291 /* Added in release 2.0 */ 5039 /* Added in release 2.0 */
5292 0, /* binaryfunc sq_inplace_concat; */ 5040 0, /* binaryfunc sq_inplace_concat; */
5293 0, /* intargfunc sq_inplace_repeat; */ 5041 0, /* intargfunc sq_inplace_repeat; */
5294 }; 5042 };
5295 5043
5296 static PyMappingMethods Pointer_as_mapping = { 5044 static PyMappingMethods Pointer_as_mapping = {
5297 0, 5045 0,
5298 Pointer_subscript, 5046 Pointer_subscript,
5299 }; 5047 };
5300 5048
5301 static int 5049 static int
5302 Pointer_nonzero(CDataObject *self) 5050 Pointer_bool(CDataObject *self)
5303 { 5051 {
5304 return (*(void **)self->b_ptr != NULL); 5052 return (*(void **)self->b_ptr != NULL);
5305 } 5053 }
5306 5054
5307 static PyNumberMethods Pointer_as_number = { 5055 static PyNumberMethods Pointer_as_number = {
5308 0, /* nb_add */ 5056 0, /* nb_add */
5309 0, /* nb_subtract */ 5057 0, /* nb_subtract */
5310 0, /* nb_multiply */ 5058 0, /* nb_multiply */
5311 0, /* nb_divide */
5312 0, /* nb_remainder */ 5059 0, /* nb_remainder */
5313 0, /* nb_divmod */ 5060 0, /* nb_divmod */
5314 0, /* nb_power */ 5061 0, /* nb_power */
5315 0, /* nb_negative */ 5062 0, /* nb_negative */
5316 0, /* nb_positive */ 5063 0, /* nb_positive */
5317 0, /* nb_absolute */ 5064 0, /* nb_absolute */
5318 (inquiry)Pointer_nonzero, /* nb_nonzero */ 5065 (inquiry)Pointer_bool, /* nb_bool */
5319 }; 5066 };
5320 5067
5321 PyTypeObject PyCPointer_Type = { 5068 PyTypeObject PyCPointer_Type = {
5322 PyVarObject_HEAD_INIT(NULL, 0) 5069 PyVarObject_HEAD_INIT(NULL, 0)
5323 "_ctypes._Pointer", 5070 "_ctypes._Pointer",
5324 sizeof(CDataObject), /* tp_basicsize */ 5071 sizeof(CDataObject), /* tp_basicsize */
5325 0, /* tp_itemsize */ 5072 0, /* tp_itemsize */
5326 0, /* tp_dealloc */ 5073 0, /* tp_dealloc */
5327 0, /* tp_print */ 5074 0, /* tp_print */
5328 0, /* tp_getattr */ 5075 0, /* tp_getattr */
5329 0, /* tp_setattr */ 5076 0, /* tp_setattr */
5330 0, /* tp_compare */ 5077 0, /* tp_reserved */
5331 0, /* tp_repr */ 5078 0, /* tp_repr */
5332 &Pointer_as_number, /* tp_as_number */ 5079 &Pointer_as_number, /* tp_as_number */
5333 &Pointer_as_sequence, /* tp_as_sequence */ 5080 &Pointer_as_sequence, /* tp_as_sequence */
5334 &Pointer_as_mapping, /* tp_as_mapping */ 5081 &Pointer_as_mapping, /* tp_as_mapping */
5335 0, /* tp_hash */ 5082 0, /* tp_hash */
5336 0, /* tp_call */ 5083 0, /* tp_call */
5337 0, /* tp_str */ 5084 0, /* tp_str */
5338 0, /* tp_getattro */ 5085 0, /* tp_getattro */
5339 0, /* tp_setattro */ 5086 0, /* tp_setattro */
5340 &PyCData_as_buffer, /* tp_as_buffer */ 5087 &PyCData_as_buffer, /* tp_as_buffer */
5341 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_BASETYPE, /* tp_ flags */ 5088 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
5342 "XXX to be provided", /* tp_doc */ 5089 "XXX to be provided", /* tp_doc */
5343 (traverseproc)PyCData_traverse, /* tp_traverse */ 5090 (traverseproc)PyCData_traverse, /* tp_traverse */
5344 (inquiry)PyCData_clear, /* tp_clear */ 5091 (inquiry)PyCData_clear, /* tp_clear */
5345 0, /* tp_richcompare */ 5092 0, /* tp_richcompare */
5346 0, /* tp_weaklistoffset */ 5093 0, /* tp_weaklistoffset */
5347 0, /* tp_iter */ 5094 0, /* tp_iter */
5348 0, /* tp_iternext */ 5095 0, /* tp_iternext */
5349 0, /* tp_methods */ 5096 0, /* tp_methods */
5350 0, /* tp_members */ 5097 0, /* tp_members */
5351 Pointer_getsets, /* tp_getset */ 5098 Pointer_getsets, /* tp_getset */
5352 0, /* tp_base */ 5099 0, /* tp_base */
5353 0, /* tp_dict */ 5100 0, /* tp_dict */
5354 0, /* tp_descr_get */ 5101 0, /* tp_descr_get */
5355 0, /* tp_descr_set */ 5102 0, /* tp_descr_set */
5356 0, /* tp_dictoffset */ 5103 0, /* tp_dictoffset */
5357 (initproc)Pointer_init, /* tp_init */ 5104 (initproc)Pointer_init, /* tp_init */
5358 0, /* tp_alloc */ 5105 0, /* tp_alloc */
5359 Pointer_new, /* tp_new */ 5106 Pointer_new, /* tp_new */
5360 0, /* tp_free */ 5107 0, /* tp_free */
5361 }; 5108 };
5362 5109
5363 5110
5364 /******************************************************************/ 5111 /******************************************************************/
5365 /* 5112 /*
5366 * Module initialization. 5113 * Module initialization.
5367 */ 5114 */
5368 5115
5369 static char *module_docs = 5116 static const char module_docs[] =
5370 "Create and manipulate C compatible data types in Python."; 5117 "Create and manipulate C compatible data types in Python.";
5371 5118
5372 #ifdef MS_WIN32 5119 #ifdef MS_WIN32
5373 5120
5374 static char comerror_doc[] = "Raised when a COM method call failed."; 5121 static char comerror_doc[] = "Raised when a COM method call failed.";
5375 5122
5376 static PyObject * 5123 int
5377 comerror_init(PyObject *self, PyObject *args) 5124 comerror_init(PyObject *self, PyObject *args, PyObject *kwds)
5378 { 5125 {
5379 PyObject *hresult, *text, *details; 5126 PyObject *hresult, *text, *details;
5127 PyBaseExceptionObject *bself;
5380 PyObject *a; 5128 PyObject *a;
5381 int status; 5129 int status;
5382 5130
5383 if (!PyArg_ParseTuple(args, "OOOO:COMError", &self, &hresult, &text, &detail s)) 5131 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
5384 return NULL; 5132 return -1;
5133
5134 if (!PyArg_ParseTuple(args, "OOO:COMError", &hresult, &text, &details))
5135 return -1;
5385 5136
5386 a = PySequence_GetSlice(args, 1, PySequence_Size(args)); 5137 a = PySequence_GetSlice(args, 1, PySequence_Size(args));
5387 if (!a) 5138 if (!a)
5388 return NULL; 5139 return -1;
5389 status = PyObject_SetAttrString(self, "args", a); 5140 status = PyObject_SetAttrString(self, "args", a);
5390 Py_DECREF(a); 5141 Py_DECREF(a);
5391 if (status < 0) 5142 if (status < 0)
5392 return NULL; 5143 return -1;
5393 5144
5394 if (PyObject_SetAttrString(self, "hresult", hresult) < 0) 5145 if (PyObject_SetAttrString(self, "hresult", hresult) < 0)
5395 return NULL; 5146 return -1;
5396 5147
5397 if (PyObject_SetAttrString(self, "text", text) < 0) 5148 if (PyObject_SetAttrString(self, "text", text) < 0)
5398 return NULL; 5149 return -1;
5399 5150
5400 if (PyObject_SetAttrString(self, "details", details) < 0) 5151 if (PyObject_SetAttrString(self, "details", details) < 0)
5401 return NULL; 5152 return -1;
5402 5153
5403 Py_INCREF(Py_None); 5154 bself = (PyBaseExceptionObject *)self;
5404 return Py_None; 5155 Py_DECREF(bself->args);
5405 } 5156 bself->args = args;
5406 5157 Py_INCREF(bself->args);
5407 static PyMethodDef comerror_methods[] = { 5158
5408 { "__init__", comerror_init, METH_VARARGS }, 5159 return 0;
5409 { NULL, NULL }, 5160 }
5161
5162 static PyTypeObject PyComError_Type = {
5163 PyVarObject_HEAD_INIT(NULL, 0)
5164 "_ctypes.COMError", /* tp_name */
5165 sizeof(PyBaseExceptionObject), /* tp_basicsize */
5166 0, /* tp_itemsize */
5167 0, /* tp_dealloc */
5168 0, /* tp_print */
5169 0, /* tp_getattr */
5170 0, /* tp_setattr */
5171 0, /* tp_reserved */
5172 0, /* tp_repr */
5173 0, /* tp_as_number */
5174 0, /* tp_as_sequence */
5175 0, /* tp_as_mapping */
5176 0, /* tp_hash */
5177 0, /* tp_call */
5178 0, /* tp_str */
5179 0, /* tp_getattro */
5180 0, /* tp_setattro */
5181 0, /* tp_as_buffer */
5182 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
5183 PyDoc_STR(comerror_doc), /* tp_doc */
5184 0, /* tp_traverse */
5185 0, /* tp_clear */
5186 0, /* tp_richcompare */
5187 0, /* tp_weaklistoffset */
5188 0, /* tp_iter */
5189 0, /* tp_iternext */
5190 0, /* tp_methods */
5191 0, /* tp_members */
5192 0, /* tp_getset */
5193 0, /* tp_base */
5194 0, /* tp_dict */
5195 0, /* tp_descr_get */
5196 0, /* tp_descr_set */
5197 0, /* tp_dictoffset */
5198 (initproc)comerror_init, /* tp_init */
5199 0, /* tp_alloc */
5200 0, /* tp_new */
5410 }; 5201 };
5202
5411 5203
5412 static int 5204 static int
5413 create_comerror(void) 5205 create_comerror(void)
5414 { 5206 {
5415 PyObject *dict = PyDict_New(); 5207 PyComError_Type.tp_base = (PyTypeObject*)PyExc_Exception;
5416 PyMethodDef *methods = comerror_methods; 5208 if (PyType_Ready(&PyComError_Type) < 0)
5417 PyObject *s;
5418 int status;
5419
5420 if (dict == NULL)
5421 return -1; 5209 return -1;
5422 5210 Py_INCREF(&PyComError_Type);
5423 while (methods->ml_name) { 5211 ComError = (PyObject*)&PyComError_Type;
5424 /* get a wrapper for the built-in function */
5425 PyObject *func = PyCFunction_New(methods, NULL);
5426 PyObject *meth;
5427 if (func == NULL)
5428 goto error;
5429 meth = PyMethod_New(func, NULL, ComError);
5430 Py_DECREF(func);
5431 if (meth == NULL)
5432 goto error;
5433 PyDict_SetItemString(dict, methods->ml_name, meth);
5434 Py_DECREF(meth);
5435 ++methods;
5436 }
5437
5438 s = PyString_FromString(comerror_doc);
5439 if (s == NULL)
5440 goto error;
5441 status = PyDict_SetItemString(dict, "__doc__", s);
5442 Py_DECREF(s);
5443 if (status == -1)
5444 goto error;
5445
5446 ComError = PyErr_NewException("_ctypes.COMError",
5447 NULL,
5448 dict);
5449 if (ComError == NULL)
5450 goto error;
5451
5452 return 0; 5212 return 0;
5453 error:
5454 Py_DECREF(dict);
5455 return -1;
5456 } 5213 }
5457 5214
5458 #endif 5215 #endif
5459 5216
5460 static PyObject * 5217 static PyObject *
5461 string_at(const char *ptr, int size) 5218 string_at(const char *ptr, int size)
5462 { 5219 {
5463 if (size == -1) 5220 if (size == -1)
5464 return PyString_FromString(ptr); 5221 return PyBytes_FromStringAndSize(ptr, strlen(ptr));
5465 return PyString_FromStringAndSize(ptr, size); 5222 return PyBytes_FromStringAndSize(ptr, size);
5466 } 5223 }
5467 5224
5468 static int 5225 static int
5469 cast_check_pointertype(PyObject *arg) 5226 cast_check_pointertype(PyObject *arg)
5470 { 5227 {
5471 StgDictObject *dict; 5228 StgDictObject *dict;
5472 5229
5473 if (PyCPointerTypeObject_Check(arg)) 5230 if (PyCPointerTypeObject_Check(arg))
5474 return 1; 5231 return 1;
5475 if (PyCFuncPtrTypeObject_Check(arg)) 5232 if (PyCFuncPtrTypeObject_Check(arg))
5476 return 1; 5233 return 1;
5477 dict = PyType_stgdict(arg); 5234 dict = PyType_stgdict(arg);
5478 if (dict) { 5235 if (dict) {
5479 if (PyString_Check(dict->proto) 5236 if (PyUnicode_Check(dict->proto)
5480 && (strchr("sPzUZXO", PyString_AS_STRING(dict->proto)[0]))) { 5237 && (strchr("sPzUZXO", _PyUnicode_AsString(dict->proto)[0]))) {
5481 /* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */ 5238 /* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */
5482 return 1; 5239 return 1;
5483 } 5240 }
5484 } 5241 }
5485 PyErr_Format(PyExc_TypeError, 5242 PyErr_Format(PyExc_TypeError,
5486 "cast() argument 2 must be a pointer type, not %s", 5243 "cast() argument 2 must be a pointer type, not %s",
5487 PyType_Check(arg) 5244 PyType_Check(arg)
5488 ? ((PyTypeObject *)arg)->tp_name 5245 ? ((PyTypeObject *)arg)->tp_name
5489 : Py_TYPE(arg)->tp_name); 5246 : Py_TYPE(arg)->tp_name);
5490 return 0; 5247 return 0;
(...skipping 10 matching lines...) Expand all
5501 return NULL; 5258 return NULL;
5502 5259
5503 /* 5260 /*
5504 The casted objects '_objects' member: 5261 The casted objects '_objects' member:
5505 5262
5506 It must certainly contain the source objects one. 5263 It must certainly contain the source objects one.
5507 It must contain the source object itself. 5264 It must contain the source object itself.
5508 */ 5265 */
5509 if (CDataObject_Check(src)) { 5266 if (CDataObject_Check(src)) {
5510 CDataObject *obj = (CDataObject *)src; 5267 CDataObject *obj = (CDataObject *)src;
5268 CDataObject *container;
5269
5511 /* PyCData_GetContainer will initialize src.b_objects, we need 5270 /* PyCData_GetContainer will initialize src.b_objects, we need
5512 this so it can be shared */ 5271 this so it can be shared */
5513 PyCData_GetContainer(obj); 5272 container = PyCData_GetContainer(obj);
5273 if (container == NULL)
5274 goto failed;
5275
5514 /* But we need a dictionary! */ 5276 /* But we need a dictionary! */
5515 if (obj->b_objects == Py_None) { 5277 if (obj->b_objects == Py_None) {
5516 Py_DECREF(Py_None); 5278 Py_DECREF(Py_None);
5517 obj->b_objects = PyDict_New(); 5279 obj->b_objects = PyDict_New();
5518 if (obj->b_objects == NULL) 5280 if (obj->b_objects == NULL)
5519 goto failed; 5281 goto failed;
5520 } 5282 }
5521 Py_XINCREF(obj->b_objects); 5283 Py_XINCREF(obj->b_objects);
5522 result->b_objects = obj->b_objects; 5284 result->b_objects = obj->b_objects;
5523 if (result->b_objects && PyDict_CheckExact(result->b_objects)) { 5285 if (result->b_objects && PyDict_CheckExact(result->b_objects)) {
(...skipping 21 matching lines...) Expand all
5545 static PyObject * 5307 static PyObject *
5546 wstring_at(const wchar_t *ptr, int size) 5308 wstring_at(const wchar_t *ptr, int size)
5547 { 5309 {
5548 Py_ssize_t ssize = size; 5310 Py_ssize_t ssize = size;
5549 if (ssize == -1) 5311 if (ssize == -1)
5550 ssize = wcslen(ptr); 5312 ssize = wcslen(ptr);
5551 return PyUnicode_FromWideChar(ptr, ssize); 5313 return PyUnicode_FromWideChar(ptr, ssize);
5552 } 5314 }
5553 #endif 5315 #endif
5554 5316
5317
5318 static struct PyModuleDef _ctypesmodule = {
5319 PyModuleDef_HEAD_INIT,
5320 "_ctypes",
5321 module_docs,
5322 -1,
5323 _ctypes_module_methods,
5324 NULL,
5325 NULL,
5326 NULL,
5327 NULL
5328 };
5329
5555 PyMODINIT_FUNC 5330 PyMODINIT_FUNC
5556 init_ctypes(void) 5331 PyInit__ctypes(void)
5557 { 5332 {
5558 PyObject *m; 5333 PyObject *m;
5559 5334
5560 /* Note: 5335 /* Note:
5561 ob_type is the metatype (the 'type'), defaults to PyType_Type, 5336 ob_type is the metatype (the 'type'), defaults to PyType_Type,
5562 tp_base is the base type, defaults to 'object' aka PyBaseObject_Type. 5337 tp_base is the base type, defaults to 'object' aka PyBaseObject_Type.
5563 */ 5338 */
5564 #ifdef WITH_THREAD 5339 #ifdef WITH_THREAD
5565 PyEval_InitThreads(); 5340 PyEval_InitThreads();
5566 #endif 5341 #endif
5567 m = Py_InitModule3("_ctypes", _ctypes_module_methods, module_docs); 5342 m = PyModule_Create(&_ctypesmodule);
5568 if (!m) 5343 if (!m)
5569 return; 5344 return NULL;
5570 5345
5571 _ctypes_ptrtype_cache = PyDict_New(); 5346 _ctypes_ptrtype_cache = PyDict_New();
5572 if (_ctypes_ptrtype_cache == NULL) 5347 if (_ctypes_ptrtype_cache == NULL)
5573 return; 5348 return NULL;
5574 5349
5575 PyModule_AddObject(m, "_pointer_type_cache", (PyObject *)_ctypes_ptrtype_cac he); 5350 PyModule_AddObject(m, "_pointer_type_cache", (PyObject *)_ctypes_ptrtype_cac he);
5576 5351
5577 _unpickle = PyObject_GetAttrString(m, "_unpickle"); 5352 _unpickle = PyObject_GetAttrString(m, "_unpickle");
5578 if (_unpickle == NULL) 5353 if (_unpickle == NULL)
5579 return; 5354 return NULL;
5580 5355
5581 if (PyType_Ready(&PyCArg_Type) < 0) 5356 if (PyType_Ready(&PyCArg_Type) < 0)
5582 return; 5357 return NULL;
5583 5358
5584 if (PyType_Ready(&PyCThunk_Type) < 0) 5359 if (PyType_Ready(&PyCThunk_Type) < 0)
5585 return; 5360 return NULL;
5586 5361
5587 /* StgDict is derived from PyDict_Type */ 5362 /* StgDict is derived from PyDict_Type */
5588 PyCStgDict_Type.tp_base = &PyDict_Type; 5363 PyCStgDict_Type.tp_base = &PyDict_Type;
5589 if (PyType_Ready(&PyCStgDict_Type) < 0) 5364 if (PyType_Ready(&PyCStgDict_Type) < 0)
5590 return; 5365 return NULL;
5591 5366
5592 /************************************************* 5367 /*************************************************
5593 * 5368 *
5594 * Metaclasses 5369 * Metaclasses
5595 */ 5370 */
5596 5371
5597 PyCStructType_Type.tp_base = &PyType_Type; 5372 PyCStructType_Type.tp_base = &PyType_Type;
5598 if (PyType_Ready(&PyCStructType_Type) < 0) 5373 if (PyType_Ready(&PyCStructType_Type) < 0)
5599 return; 5374 return NULL;
5600 5375
5601 UnionType_Type.tp_base = &PyType_Type; 5376 UnionType_Type.tp_base = &PyType_Type;
5602 if (PyType_Ready(&UnionType_Type) < 0) 5377 if (PyType_Ready(&UnionType_Type) < 0)
5603 return; 5378 return NULL;
5604 5379
5605 PyCPointerType_Type.tp_base = &PyType_Type; 5380 PyCPointerType_Type.tp_base = &PyType_Type;
5606 if (PyType_Ready(&PyCPointerType_Type) < 0) 5381 if (PyType_Ready(&PyCPointerType_Type) < 0)
5607 return; 5382 return NULL;
5608 5383
5609 PyCArrayType_Type.tp_base = &PyType_Type; 5384 PyCArrayType_Type.tp_base = &PyType_Type;
5610 if (PyType_Ready(&PyCArrayType_Type) < 0) 5385 if (PyType_Ready(&PyCArrayType_Type) < 0)
5611 return; 5386 return NULL;
5612 5387
5613 PyCSimpleType_Type.tp_base = &PyType_Type; 5388 PyCSimpleType_Type.tp_base = &PyType_Type;
5614 if (PyType_Ready(&PyCSimpleType_Type) < 0) 5389 if (PyType_Ready(&PyCSimpleType_Type) < 0)
5615 return; 5390 return NULL;
5616 5391
5617 PyCFuncPtrType_Type.tp_base = &PyType_Type; 5392 PyCFuncPtrType_Type.tp_base = &PyType_Type;
5618 if (PyType_Ready(&PyCFuncPtrType_Type) < 0) 5393 if (PyType_Ready(&PyCFuncPtrType_Type) < 0)
5619 return; 5394 return NULL;
5620 5395
5621 /************************************************* 5396 /*************************************************
5622 * 5397 *
5623 * Classes using a custom metaclass 5398 * Classes using a custom metaclass
5624 */ 5399 */
5625 5400
5626 if (PyType_Ready(&PyCData_Type) < 0) 5401 if (PyType_Ready(&PyCData_Type) < 0)
5627 return; 5402 return NULL;
5628 5403
5629 Py_TYPE(&Struct_Type) = &PyCStructType_Type; 5404 Py_TYPE(&Struct_Type) = &PyCStructType_Type;
5630 Struct_Type.tp_base = &PyCData_Type; 5405 Struct_Type.tp_base = &PyCData_Type;
5631 if (PyType_Ready(&Struct_Type) < 0) 5406 if (PyType_Ready(&Struct_Type) < 0)
5632 return; 5407 return NULL;
5633 Py_INCREF(&Struct_Type); 5408 Py_INCREF(&Struct_Type);
5634 PyModule_AddObject(m, "Structure", (PyObject *)&Struct_Type); 5409 PyModule_AddObject(m, "Structure", (PyObject *)&Struct_Type);
5635 5410
5636 Py_TYPE(&Union_Type) = &UnionType_Type; 5411 Py_TYPE(&Union_Type) = &UnionType_Type;
5637 Union_Type.tp_base = &PyCData_Type; 5412 Union_Type.tp_base = &PyCData_Type;
5638 if (PyType_Ready(&Union_Type) < 0) 5413 if (PyType_Ready(&Union_Type) < 0)
5639 return; 5414 return NULL;
5640 Py_INCREF(&Union_Type); 5415 Py_INCREF(&Union_Type);
5641 PyModule_AddObject(m, "Union", (PyObject *)&Union_Type); 5416 PyModule_AddObject(m, "Union", (PyObject *)&Union_Type);
5642 5417
5643 Py_TYPE(&PyCPointer_Type) = &PyCPointerType_Type; 5418 Py_TYPE(&PyCPointer_Type) = &PyCPointerType_Type;
5644 PyCPointer_Type.tp_base = &PyCData_Type; 5419 PyCPointer_Type.tp_base = &PyCData_Type;
5645 if (PyType_Ready(&PyCPointer_Type) < 0) 5420 if (PyType_Ready(&PyCPointer_Type) < 0)
5646 return; 5421 return NULL;
5647 Py_INCREF(&PyCPointer_Type); 5422 Py_INCREF(&PyCPointer_Type);
5648 PyModule_AddObject(m, "_Pointer", (PyObject *)&PyCPointer_Type); 5423 PyModule_AddObject(m, "_Pointer", (PyObject *)&PyCPointer_Type);
5649 5424
5650 Py_TYPE(&PyCArray_Type) = &PyCArrayType_Type; 5425 Py_TYPE(&PyCArray_Type) = &PyCArrayType_Type;
5651 PyCArray_Type.tp_base = &PyCData_Type; 5426 PyCArray_Type.tp_base = &PyCData_Type;
5652 if (PyType_Ready(&PyCArray_Type) < 0) 5427 if (PyType_Ready(&PyCArray_Type) < 0)
5653 return; 5428 return NULL;
5654 Py_INCREF(&PyCArray_Type); 5429 Py_INCREF(&PyCArray_Type);
5655 PyModule_AddObject(m, "Array", (PyObject *)&PyCArray_Type); 5430 PyModule_AddObject(m, "Array", (PyObject *)&PyCArray_Type);
5656 5431
5657 Py_TYPE(&Simple_Type) = &PyCSimpleType_Type; 5432 Py_TYPE(&Simple_Type) = &PyCSimpleType_Type;
5658 Simple_Type.tp_base = &PyCData_Type; 5433 Simple_Type.tp_base = &PyCData_Type;
5659 if (PyType_Ready(&Simple_Type) < 0) 5434 if (PyType_Ready(&Simple_Type) < 0)
5660 return; 5435 return NULL;
5661 Py_INCREF(&Simple_Type); 5436 Py_INCREF(&Simple_Type);
5662 PyModule_AddObject(m, "_SimpleCData", (PyObject *)&Simple_Type); 5437 PyModule_AddObject(m, "_SimpleCData", (PyObject *)&Simple_Type);
5663 5438
5664 Py_TYPE(&PyCFuncPtr_Type) = &PyCFuncPtrType_Type; 5439 Py_TYPE(&PyCFuncPtr_Type) = &PyCFuncPtrType_Type;
5665 PyCFuncPtr_Type.tp_base = &PyCData_Type; 5440 PyCFuncPtr_Type.tp_base = &PyCData_Type;
5666 if (PyType_Ready(&PyCFuncPtr_Type) < 0) 5441 if (PyType_Ready(&PyCFuncPtr_Type) < 0)
5667 return; 5442 return NULL;
5668 Py_INCREF(&PyCFuncPtr_Type); 5443 Py_INCREF(&PyCFuncPtr_Type);
5669 PyModule_AddObject(m, "CFuncPtr", (PyObject *)&PyCFuncPtr_Type); 5444 PyModule_AddObject(m, "CFuncPtr", (PyObject *)&PyCFuncPtr_Type);
5670 5445
5671 /************************************************* 5446 /*************************************************
5672 * 5447 *
5673 * Simple classes 5448 * Simple classes
5674 */ 5449 */
5675 5450
5676 /* PyCField_Type is derived from PyBaseObject_Type */ 5451 /* PyCField_Type is derived from PyBaseObject_Type */
5677 if (PyType_Ready(&PyCField_Type) < 0) 5452 if (PyType_Ready(&PyCField_Type) < 0)
5678 return; 5453 return NULL;
5679 5454
5680 /************************************************* 5455 /*************************************************
5681 * 5456 *
5682 * Other stuff 5457 * Other stuff
5683 */ 5458 */
5684 5459
5685 DictRemover_Type.tp_new = PyType_GenericNew; 5460 DictRemover_Type.tp_new = PyType_GenericNew;
5686 if (PyType_Ready(&DictRemover_Type) < 0) 5461 if (PyType_Ready(&DictRemover_Type) < 0)
5687 return; 5462 return NULL;
5688 5463
5689 #ifdef MS_WIN32 5464 #ifdef MS_WIN32
5690 if (create_comerror() < 0) 5465 if (create_comerror() < 0)
5691 return; 5466 return NULL;
5692 PyModule_AddObject(m, "COMError", ComError); 5467 PyModule_AddObject(m, "COMError", ComError);
5693 5468
5694 PyModule_AddObject(m, "FUNCFLAG_HRESULT", PyInt_FromLong(FUNCFLAG_HRESULT)); 5469 PyModule_AddObject(m, "FUNCFLAG_HRESULT", PyLong_FromLong(FUNCFLAG_HRESULT)) ;
5695 PyModule_AddObject(m, "FUNCFLAG_STDCALL", PyInt_FromLong(FUNCFLAG_STDCALL)); 5470 PyModule_AddObject(m, "FUNCFLAG_STDCALL", PyLong_FromLong(FUNCFLAG_STDCALL)) ;
5696 #endif 5471 #endif
5697 PyModule_AddObject(m, "FUNCFLAG_CDECL", PyInt_FromLong(FUNCFLAG_CDECL)); 5472 PyModule_AddObject(m, "FUNCFLAG_CDECL", PyLong_FromLong(FUNCFLAG_CDECL));
5698 PyModule_AddObject(m, "FUNCFLAG_USE_ERRNO", PyInt_FromLong(FUNCFLAG_USE_ERRN O)); 5473 PyModule_AddObject(m, "FUNCFLAG_USE_ERRNO", PyLong_FromLong(FUNCFLAG_USE_ERR NO));
5699 PyModule_AddObject(m, "FUNCFLAG_USE_LASTERROR", PyInt_FromLong(FUNCFLAG_USE_ LASTERROR)); 5474 PyModule_AddObject(m, "FUNCFLAG_USE_LASTERROR", PyLong_FromLong(FUNCFLAG_USE _LASTERROR));
5700 PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyInt_FromLong(FUNCFLAG_PYTHONAP I)); 5475 PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyLong_FromLong(FUNCFLAG_PYTHONA PI));
5701 PyModule_AddStringConstant(m, "__version__", "1.1.0"); 5476 PyModule_AddStringConstant(m, "__version__", "1.1.0");
5702 5477
5703 PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(memmove)); 5478 PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(memmove));
5704 PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(memset)); 5479 PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(memset));
5705 PyModule_AddObject(m, "_string_at_addr", PyLong_FromVoidPtr(string_at)); 5480 PyModule_AddObject(m, "_string_at_addr", PyLong_FromVoidPtr(string_at));
5706 PyModule_AddObject(m, "_cast_addr", PyLong_FromVoidPtr(cast)); 5481 PyModule_AddObject(m, "_cast_addr", PyLong_FromVoidPtr(cast));
5707 #ifdef CTYPES_UNICODE 5482 #ifdef CTYPES_UNICODE
5708 PyModule_AddObject(m, "_wstring_at_addr", PyLong_FromVoidPtr(wstring_at)); 5483 PyModule_AddObject(m, "_wstring_at_addr", PyLong_FromVoidPtr(wstring_at));
5709 #endif 5484 #endif
5710 5485
5711 /* If RTLD_LOCAL is not defined (Windows!), set it to zero. */ 5486 /* If RTLD_LOCAL is not defined (Windows!), set it to zero. */
5712 #ifndef RTLD_LOCAL 5487 #ifndef RTLD_LOCAL
5713 #define RTLD_LOCAL 0 5488 #define RTLD_LOCAL 0
5714 #endif 5489 #endif
5715 5490
5716 /* If RTLD_GLOBAL is not defined (cygwin), set it to the same value as 5491 /* If RTLD_GLOBAL is not defined (cygwin), set it to the same value as
5717 RTLD_LOCAL. 5492 RTLD_LOCAL.
5718 */ 5493 */
5719 #ifndef RTLD_GLOBAL 5494 #ifndef RTLD_GLOBAL
5720 #define RTLD_GLOBAL RTLD_LOCAL 5495 #define RTLD_GLOBAL RTLD_LOCAL
5721 #endif 5496 #endif
5722 5497
5723 PyModule_AddObject(m, "RTLD_LOCAL", PyInt_FromLong(RTLD_LOCAL)); 5498 PyModule_AddObject(m, "RTLD_LOCAL", PyLong_FromLong(RTLD_LOCAL));
5724 PyModule_AddObject(m, "RTLD_GLOBAL", PyInt_FromLong(RTLD_GLOBAL)); 5499 PyModule_AddObject(m, "RTLD_GLOBAL", PyLong_FromLong(RTLD_GLOBAL));
5725 5500
5726 PyExc_ArgError = PyErr_NewException("ctypes.ArgumentError", NULL, NULL); 5501 PyExc_ArgError = PyErr_NewException("ctypes.ArgumentError", NULL, NULL);
5727 if (PyExc_ArgError) { 5502 if (PyExc_ArgError) {
5728 Py_INCREF(PyExc_ArgError); 5503 Py_INCREF(PyExc_ArgError);
5729 PyModule_AddObject(m, "ArgumentError", PyExc_ArgError); 5504 PyModule_AddObject(m, "ArgumentError", PyExc_ArgError);
5730 } 5505 }
5731 } 5506 return m;
5732 5507 }
5733 /*****************************************************************
5734 * replacements for broken Python api functions (in Python 2.3).
5735 * See #1047269 Buffer overwrite in PyUnicode_AsWideChar
5736 */
5737
5738 #if (PY_VERSION_HEX < 0x02040000)
5739 #ifdef HAVE_WCHAR_H
5740
5741 PyObject *My_PyUnicode_FromWideChar(register const wchar_t *w,
5742 Py_ssize_t size)
5743 {
5744 PyUnicodeObject *unicode;
5745
5746 if (w == NULL) {
5747 PyErr_BadInternalCall();
5748 return NULL;
5749 }
5750
5751 unicode = (PyUnicodeObject *)PyUnicode_FromUnicode(NULL, size);
5752 if (!unicode)
5753 return NULL;
5754
5755 /* Copy the wchar_t data into the new object */
5756 #ifdef HAVE_USABLE_WCHAR_T
5757 memcpy(unicode->str, w, size * sizeof(wchar_t));
5758 #else
5759 {
5760 register Py_UNICODE *u;
5761 register int i;
5762 u = PyUnicode_AS_UNICODE(unicode);
5763 /* In Python, the following line has a one-off error */
5764 for (i = size; i > 0; i--)
5765 *u++ = *w++;
5766 }
5767 #endif
5768
5769 return (PyObject *)unicode;
5770 }
5771
5772 Py_ssize_t My_PyUnicode_AsWideChar(PyUnicodeObject *unicode,
5773 register wchar_t *w,
5774 Py_ssize_t size)
5775 {
5776 if (unicode == NULL) {
5777 PyErr_BadInternalCall();
5778 return -1;
5779 }
5780 if (size > PyUnicode_GET_SIZE(unicode))
5781 size = PyUnicode_GET_SIZE(unicode);
5782 #ifdef HAVE_USABLE_WCHAR_T
5783 memcpy(w, unicode->str, size * sizeof(wchar_t));
5784 #else
5785 {
5786 register Py_UNICODE *u;
5787 register int i;
5788 u = PyUnicode_AS_UNICODE(unicode);
5789 /* In Python, the following line has a one-off error */
5790 for (i = size; i > 0; i--)
5791 *w++ = *u++;
5792 }
5793 #endif
5794
5795 return size;
5796 }
5797 #endif
5798 #endif
5799 5508
5800 /* 5509 /*
5801 Local Variables: 5510 Local Variables:
5802 compile-command: "cd .. && python setup.py -q build -g && python setup.py -q bu ild install --home ~" 5511 compile-command: "cd .. && python setup.py -q build -g && python setup.py -q bu ild install --home ~"
5803 End: 5512 End:
5804 */ 5513 */
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+