annotate Modules/_ctypes/stgdict.c @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents d51e4f62bc8c
children fd25030223b0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38780
381a72ab5fb8 Add pep-291 compatibility markers.
Thomas Heller <theller@ctypes.org>
parents: 38729
diff changeset
1 /*****************************************************************
381a72ab5fb8 Add pep-291 compatibility markers.
Thomas Heller <theller@ctypes.org>
parents: 38729
diff changeset
2 This file should be kept compatible with Python 2.3, see PEP 291.
381a72ab5fb8 Add pep-291 compatibility markers.
Thomas Heller <theller@ctypes.org>
parents: 38729
diff changeset
3 *****************************************************************/
381a72ab5fb8 Add pep-291 compatibility markers.
Thomas Heller <theller@ctypes.org>
parents: 38729
diff changeset
4
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
5 #include "Python.h"
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
6 #include <ffi.h>
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
7 #ifdef MS_WIN32
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
8 #include <windows.h>
47475
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
9 #include <malloc.h>
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
10 #endif
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
11 #include "ctypes.h"
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
12
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
13 /******************************************************************/
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
14 /*
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
15 StdDict - a dictionary subclass, containing additional C accessible fields
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
16
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
17 XXX blabla more
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
18 */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
19
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
20 /* Seems we need this, otherwise we get problems when calling
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
21 * PyDict_SetItem() (ma_lookup is NULL)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
22 */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
23 static int
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
24 PyCStgDict_init(StgDictObject *self, PyObject *args, PyObject *kwds)
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
25 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
26 if (PyDict_Type.tp_init((PyObject *)self, args, kwds) < 0)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
27 return -1;
47475
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
28 self->format = NULL;
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
29 self->ndim = 0;
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
30 self->shape = NULL;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
31 return 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
32 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
33
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
34 static int
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
35 PyCStgDict_clear(StgDictObject *self)
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
36 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
37 Py_CLEAR(self->proto);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
38 Py_CLEAR(self->argtypes);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
39 Py_CLEAR(self->converters);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
40 Py_CLEAR(self->restype);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
41 Py_CLEAR(self->checker);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
42 return 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
43 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
44
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
45 static void
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
46 PyCStgDict_dealloc(StgDictObject *self)
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
47 {
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
48 PyCStgDict_clear(self);
47475
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
49 PyMem_Free(self->format);
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
50 PyMem_Free(self->shape);
37848
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
51 PyMem_Free(self->ffi_type_pointer.elements);
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
52 PyDict_Type.tp_dealloc((PyObject *)self);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
53 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
54
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
55 int
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
56 PyCStgDict_clone(StgDictObject *dst, StgDictObject *src)
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
57 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
58 char *d, *s;
41998
7b84e8919b00 [ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
Thomas Heller <theller@ctypes.org>
parents: 41562
diff changeset
59 Py_ssize_t size;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
60
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
61 PyCStgDict_clear(dst);
37848
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
62 PyMem_Free(dst->ffi_type_pointer.elements);
47475
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
63 PyMem_Free(dst->format);
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
64 dst->format = NULL;
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
65 PyMem_Free(dst->shape);
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
66 dst->shape = NULL;
37848
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
67 dst->ffi_type_pointer.elements = NULL;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
68
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
69 d = (char *)dst;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
70 s = (char *)src;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
71 memcpy(d + sizeof(PyDictObject),
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
72 s + sizeof(PyDictObject),
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
73 sizeof(StgDictObject) - sizeof(PyDictObject));
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
74
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
75 Py_XINCREF(dst->proto);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
76 Py_XINCREF(dst->argtypes);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
77 Py_XINCREF(dst->converters);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
78 Py_XINCREF(dst->restype);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
79 Py_XINCREF(dst->checker);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
80
47475
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
81 if (src->format) {
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
82 dst->format = PyMem_Malloc(strlen(src->format) + 1);
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
83 if (dst->format == NULL)
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
84 return -1;
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
85 strcpy(dst->format, src->format);
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
86 }
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
87 if (src->shape) {
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
88 dst->shape = PyMem_Malloc(sizeof(Py_ssize_t) * src->ndim);
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
89 if (dst->shape == NULL)
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
90 return -1;
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
91 memcpy(dst->shape, src->shape,
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
92 sizeof(Py_ssize_t) * src->ndim);
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
93 }
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
94
37848
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
95 if (src->ffi_type_pointer.elements == NULL)
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
96 return 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
97 size = sizeof(ffi_type *) * (src->length + 1);
37848
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
98 dst->ffi_type_pointer.elements = PyMem_Malloc(size);
41562
6a56aedbe280 Make sure to call PyErr_NoMemory() in several places where
Thomas Heller <theller@ctypes.org>
parents: 40393
diff changeset
99 if (dst->ffi_type_pointer.elements == NULL) {
6a56aedbe280 Make sure to call PyErr_NoMemory() in several places where
Thomas Heller <theller@ctypes.org>
parents: 40393
diff changeset
100 PyErr_NoMemory();
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
101 return -1;
41562
6a56aedbe280 Make sure to call PyErr_NoMemory() in several places where
Thomas Heller <theller@ctypes.org>
parents: 40393
diff changeset
102 }
37848
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
103 memcpy(dst->ffi_type_pointer.elements,
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
104 src->ffi_type_pointer.elements,
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
105 size);
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
106 return 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
107 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
108
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
109 PyTypeObject PyCStgDict_Type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 42268
diff changeset
110 PyVarObject_HEAD_INIT(NULL, 0)
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
111 "StgDict",
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
112 sizeof(StgDictObject),
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
113 0,
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
114 (destructor)PyCStgDict_dealloc, /* tp_dealloc */
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
115 0, /* tp_print */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
116 0, /* tp_getattr */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
117 0, /* tp_setattr */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
118 0, /* tp_compare */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
119 0, /* tp_repr */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
120 0, /* tp_as_number */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
121 0, /* tp_as_sequence */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
122 0, /* tp_as_mapping */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
123 0, /* tp_hash */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
124 0, /* tp_call */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
125 0, /* tp_str */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
126 0, /* tp_getattro */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
127 0, /* tp_setattro */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
128 0, /* tp_as_buffer */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
129 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
130 0, /* tp_doc */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
131 0, /* tp_traverse */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
132 0, /* tp_clear */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
133 0, /* tp_richcompare */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
134 0, /* tp_weaklistoffset */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
135 0, /* tp_iter */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
136 0, /* tp_iternext */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
137 0, /* tp_methods */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
138 0, /* tp_members */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
139 0, /* tp_getset */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
140 0, /* tp_base */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
141 0, /* tp_dict */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
142 0, /* tp_descr_get */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
143 0, /* tp_descr_set */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
144 0, /* tp_dictoffset */
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
145 (initproc)PyCStgDict_init, /* tp_init */
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
146 0, /* tp_alloc */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
147 0, /* tp_new */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
148 0, /* tp_free */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
149 };
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
150
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
151 /* May return NULL, but does not set an exception! */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
152 StgDictObject *
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
153 PyType_stgdict(PyObject *obj)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
154 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
155 PyTypeObject *type;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
156
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
157 if (!PyType_Check(obj))
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
158 return NULL;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
159 type = (PyTypeObject *)obj;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
160 if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS))
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
161 return NULL;
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
162 if (!type->tp_dict || !PyCStgDict_CheckExact(type->tp_dict))
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
163 return NULL;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
164 return (StgDictObject *)type->tp_dict;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
165 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
166
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
167 /* May return NULL, but does not set an exception! */
39343
1567c01cca73 Speed up PyType_stgdict and PyObject_stgdict.
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
168 /*
1567c01cca73 Speed up PyType_stgdict and PyObject_stgdict.
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
169 This function should be as fast as possible, so we don't call PyType_stgdict
1567c01cca73 Speed up PyType_stgdict and PyObject_stgdict.
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
170 above but inline the code, and avoid the PyType_Check().
1567c01cca73 Speed up PyType_stgdict and PyObject_stgdict.
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
171 */
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
172 StgDictObject *
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
173 PyObject_stgdict(PyObject *self)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
174 {
39343
1567c01cca73 Speed up PyType_stgdict and PyObject_stgdict.
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
175 PyTypeObject *type = self->ob_type;
1567c01cca73 Speed up PyType_stgdict and PyObject_stgdict.
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
176 if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS))
1567c01cca73 Speed up PyType_stgdict and PyObject_stgdict.
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
177 return NULL;
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
178 if (!type->tp_dict || !PyCStgDict_CheckExact(type->tp_dict))
39343
1567c01cca73 Speed up PyType_stgdict and PyObject_stgdict.
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
179 return NULL;
1567c01cca73 Speed up PyType_stgdict and PyObject_stgdict.
Thomas Heller <theller@ctypes.org>
parents: 38780
diff changeset
180 return (StgDictObject *)type->tp_dict;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
181 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
182
38720
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
183 /* descr is the descriptor for a field marked as anonymous. Get all the
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
184 _fields_ descriptors from descr->proto, create new descriptors with offset
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
185 and index adjusted, and stuff them into type.
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
186 */
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
187 static int
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
188 MakeFields(PyObject *type, CFieldObject *descr,
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
189 Py_ssize_t index, Py_ssize_t offset)
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
190 {
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
191 Py_ssize_t i;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
192 PyObject *fields;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
193 PyObject *fieldlist;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
194
38720
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
195 fields = PyObject_GetAttrString(descr->proto, "_fields_");
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
196 if (fields == NULL)
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
197 return -1;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
198 fieldlist = PySequence_Fast(fields, "_fields_ must be a sequence");
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
199 Py_DECREF(fields);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
200 if (fieldlist == NULL)
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
201 return -1;
38720
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
202
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
203 for (i = 0; i < PySequence_Fast_GET_SIZE(fieldlist); ++i) {
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
204 PyObject *pair = PySequence_Fast_GET_ITEM(fieldlist, i); /* borrowed */
39804
a065b55e1fb1 Anonymous structure fields that have a bit-width specified did not work,
Thomas Heller <theller@ctypes.org>
parents: 39583
diff changeset
205 PyObject *fname, *ftype, *bits;
38720
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
206 CFieldObject *fdescr;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
207 CFieldObject *new_descr;
38729
7b318e96f80a Don't use C++ comment.
Thomas Heller <theller@ctypes.org>
parents: 38720
diff changeset
208 /* Convert to PyArg_UnpackTuple... */
39804
a065b55e1fb1 Anonymous structure fields that have a bit-width specified did not work,
Thomas Heller <theller@ctypes.org>
parents: 39583
diff changeset
209 if (!PyArg_ParseTuple(pair, "OO|O", &fname, &ftype, &bits)) {
38720
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
210 Py_DECREF(fieldlist);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
211 return -1;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
212 }
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
213 fdescr = (CFieldObject *)PyObject_GetAttr(descr->proto, fname);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
214 if (fdescr == NULL) {
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
215 Py_DECREF(fieldlist);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
216 return -1;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
217 }
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
218 if (Py_TYPE(fdescr) != &PyCField_Type) {
38720
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
219 PyErr_SetString(PyExc_TypeError, "unexpected type");
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
220 Py_DECREF(fdescr);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
221 Py_DECREF(fieldlist);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
222 return -1;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
223 }
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
224 if (fdescr->anonymous) {
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
225 int rc = MakeFields(type, fdescr,
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
226 index + fdescr->index,
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
227 offset + fdescr->offset);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
228 Py_DECREF(fdescr);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
229 if (rc == -1) {
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
230 Py_DECREF(fieldlist);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
231 return -1;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
232 }
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
233 continue;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
234 }
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
235 new_descr = (CFieldObject *)PyObject_CallObject((PyObject *)&PyCField_Type, NULL);
38720
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
236 if (new_descr == NULL) {
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
237 Py_DECREF(fdescr);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
238 Py_DECREF(fieldlist);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
239 return -1;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
240 }
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
241 assert(Py_TYPE(new_descr) == &PyCField_Type);
38720
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
242 new_descr->size = fdescr->size;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
243 new_descr->offset = fdescr->offset + offset;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
244 new_descr->index = fdescr->index + index;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
245 new_descr->proto = fdescr->proto;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
246 Py_XINCREF(new_descr->proto);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
247 new_descr->getfunc = fdescr->getfunc;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
248 new_descr->setfunc = fdescr->setfunc;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
249
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
250 Py_DECREF(fdescr);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
251
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
252 if (-1 == PyObject_SetAttr(type, fname, (PyObject *)new_descr)) {
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
253 Py_DECREF(fieldlist);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
254 Py_DECREF(new_descr);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
255 return -1;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
256 }
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
257 Py_DECREF(new_descr);
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
258 }
38720
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
259 Py_DECREF(fieldlist);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
260 return 0;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
261 }
38720
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
262
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
263 /* Iterate over the names in the type's _anonymous_ attribute, if present,
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
264 */
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
265 static int
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
266 MakeAnonFields(PyObject *type)
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
267 {
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
268 PyObject *anon;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
269 PyObject *anon_names;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
270 Py_ssize_t i;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
271
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
272 anon = PyObject_GetAttrString(type, "_anonymous_");
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
273 if (anon == NULL) {
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
274 PyErr_Clear();
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
275 return 0;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
276 }
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
277 anon_names = PySequence_Fast(anon, "_anonymous_ must be a sequence");
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
278 Py_DECREF(anon);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
279 if (anon_names == NULL)
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
280 return -1;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
281
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
282 for (i = 0; i < PySequence_Fast_GET_SIZE(anon_names); ++i) {
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
283 PyObject *fname = PySequence_Fast_GET_ITEM(anon_names, i); /* borrowed */
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
284 CFieldObject *descr = (CFieldObject *)PyObject_GetAttr(type, fname);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
285 if (descr == NULL) {
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
286 Py_DECREF(anon_names);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
287 return -1;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
288 }
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
289 assert(Py_TYPE(descr) == &PyCField_Type);
38720
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
290 descr->anonymous = 1;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
291
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
292 /* descr is in the field descriptor. */
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
293 if (-1 == MakeFields(type, (CFieldObject *)descr,
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
294 ((CFieldObject *)descr)->index,
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
295 ((CFieldObject *)descr)->offset)) {
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
296 Py_DECREF(descr);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
297 Py_DECREF(anon_names);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
298 return -1;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
299 }
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
300 Py_DECREF(descr);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
301 }
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
302
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
303 Py_DECREF(anon_names);
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
304 return 0;
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
305 }
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
306
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
307 /*
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
308 Retrive the (optional) _pack_ attribute from a type, the _fields_ attribute,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
309 and create an StgDictObject. Used for Structure and Union subclasses.
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
310 */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
311 int
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
312 PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct)
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
313 {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
314 StgDictObject *stgdict, *basedict;
41998
7b84e8919b00 [ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
Thomas Heller <theller@ctypes.org>
parents: 41562
diff changeset
315 Py_ssize_t len, offset, size, align, i;
7b84e8919b00 [ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
Thomas Heller <theller@ctypes.org>
parents: 41562
diff changeset
316 Py_ssize_t union_size, total_align;
7b84e8919b00 [ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
Thomas Heller <theller@ctypes.org>
parents: 41562
diff changeset
317 Py_ssize_t field_size = 0;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
318 int bitofs;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
319 PyObject *isPacked;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
320 int pack = 0;
41998
7b84e8919b00 [ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
Thomas Heller <theller@ctypes.org>
parents: 41562
diff changeset
321 Py_ssize_t ffi_ofs;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
322 int big_endian;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
323
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
324 /* HACK Alert: I cannot be bothered to fix ctypes.com, so there has to
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
325 be a way to use the old, broken sematics: _fields_ are not extended
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
326 but replaced in subclasses.
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
327
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
328 XXX Remove this in ctypes 1.0!
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
329 */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
330 int use_broken_old_ctypes_semantics;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
331
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
332 if (fields == NULL)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
333 return 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
334
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
335 #ifdef WORDS_BIGENDIAN
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
336 big_endian = PyObject_HasAttrString(type, "_swappedbytes_") ? 0 : 1;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
337 #else
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
338 big_endian = PyObject_HasAttrString(type, "_swappedbytes_") ? 1 : 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
339 #endif
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
340
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
341 use_broken_old_ctypes_semantics = \
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
342 PyObject_HasAttrString(type, "_use_broken_old_ctypes_structure_semantics_");
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
343
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
344 isPacked = PyObject_GetAttrString(type, "_pack_");
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
345 if (isPacked) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
346 pack = PyInt_AsLong(isPacked);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
347 if (pack < 0 || PyErr_Occurred()) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
348 Py_XDECREF(isPacked);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
349 PyErr_SetString(PyExc_ValueError,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
350 "_pack_ must be a non-negative integer");
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
351 return -1;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
352 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
353 Py_DECREF(isPacked);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
354 } else
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
355 PyErr_Clear();
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
356
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
357 len = PySequence_Length(fields);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
358 if (len == -1) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
359 PyErr_SetString(PyExc_TypeError,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
360 "'_fields_' must be a sequence of pairs");
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
361 return -1;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
362 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
363
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
364 stgdict = PyType_stgdict(type);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
365 if (!stgdict)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
366 return -1;
40393
96be9d74ad36 Fix bug #1598620: A ctypes structure cannot contain itself.
Thomas Heller <theller@ctypes.org>
parents: 39804
diff changeset
367 /* If this structure/union is already marked final we cannot assign
96be9d74ad36 Fix bug #1598620: A ctypes structure cannot contain itself.
Thomas Heller <theller@ctypes.org>
parents: 39804
diff changeset
368 _fields_ anymore. */
96be9d74ad36 Fix bug #1598620: A ctypes structure cannot contain itself.
Thomas Heller <theller@ctypes.org>
parents: 39804
diff changeset
369
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
370 if (stgdict->flags & DICTFLAG_FINAL) {/* is final ? */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
371 PyErr_SetString(PyExc_AttributeError,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
372 "_fields_ is final");
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
373 return -1;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
374 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
375
47475
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
376 if (stgdict->format) {
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
377 PyMem_Free(stgdict->format);
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
378 stgdict->format = NULL;
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
379 }
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
380
37848
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
381 if (stgdict->ffi_type_pointer.elements)
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
382 PyMem_Free(stgdict->ffi_type_pointer.elements);
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
383
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
384 basedict = PyType_stgdict((PyObject *)((PyTypeObject *)type)->tp_base);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
385 if (basedict && !use_broken_old_ctypes_semantics) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
386 size = offset = basedict->size;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
387 align = basedict->align;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
388 union_size = 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
389 total_align = align ? align : 1;
37848
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
390 stgdict->ffi_type_pointer.type = FFI_TYPE_STRUCT;
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
391 stgdict->ffi_type_pointer.elements = PyMem_Malloc(sizeof(ffi_type *) * (basedict->length + len + 1));
41562
6a56aedbe280 Make sure to call PyErr_NoMemory() in several places where
Thomas Heller <theller@ctypes.org>
parents: 40393
diff changeset
392 if (stgdict->ffi_type_pointer.elements == NULL) {
6a56aedbe280 Make sure to call PyErr_NoMemory() in several places where
Thomas Heller <theller@ctypes.org>
parents: 40393
diff changeset
393 PyErr_NoMemory();
6a56aedbe280 Make sure to call PyErr_NoMemory() in several places where
Thomas Heller <theller@ctypes.org>
parents: 40393
diff changeset
394 return -1;
6a56aedbe280 Make sure to call PyErr_NoMemory() in several places where
Thomas Heller <theller@ctypes.org>
parents: 40393
diff changeset
395 }
37848
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
396 memset(stgdict->ffi_type_pointer.elements, 0,
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
397 sizeof(ffi_type *) * (basedict->length + len + 1));
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
398 memcpy(stgdict->ffi_type_pointer.elements,
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
399 basedict->ffi_type_pointer.elements,
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
400 sizeof(ffi_type *) * (basedict->length));
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
401 ffi_ofs = basedict->length;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
402 } else {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
403 offset = 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
404 size = 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
405 align = 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
406 union_size = 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
407 total_align = 1;
37848
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
408 stgdict->ffi_type_pointer.type = FFI_TYPE_STRUCT;
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
409 stgdict->ffi_type_pointer.elements = PyMem_Malloc(sizeof(ffi_type *) * (len + 1));
41562
6a56aedbe280 Make sure to call PyErr_NoMemory() in several places where
Thomas Heller <theller@ctypes.org>
parents: 40393
diff changeset
410 if (stgdict->ffi_type_pointer.elements == NULL) {
6a56aedbe280 Make sure to call PyErr_NoMemory() in several places where
Thomas Heller <theller@ctypes.org>
parents: 40393
diff changeset
411 PyErr_NoMemory();
6a56aedbe280 Make sure to call PyErr_NoMemory() in several places where
Thomas Heller <theller@ctypes.org>
parents: 40393
diff changeset
412 return -1;
6a56aedbe280 Make sure to call PyErr_NoMemory() in several places where
Thomas Heller <theller@ctypes.org>
parents: 40393
diff changeset
413 }
37848
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
414 memset(stgdict->ffi_type_pointer.elements, 0,
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
415 sizeof(ffi_type *) * (len + 1));
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
416 ffi_ofs = 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
417 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
418
48847
Neal Norwitz <nnorwitz@gmail.com>
parents: 47475
diff changeset
419 assert(stgdict->format == NULL);
47475
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
420 if (isStruct && !isPacked) {
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
421 stgdict->format = _ctypes_alloc_format_string(NULL, "T{");
47475
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
422 } else {
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
423 /* PEP3118 doesn't support union, or packed structures (well,
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
424 only standard packing, but we dont support the pep for
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
425 that). Use 'B' for bytes. */
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
426 stgdict->format = _ctypes_alloc_format_string(NULL, "B");
47475
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
427 }
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
428
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
429 #define realdict ((PyObject *)&stgdict->dict)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
430 for (i = 0; i < len; ++i) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
431 PyObject *name = NULL, *desc = NULL;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
432 PyObject *pair = PySequence_GetItem(fields, i);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
433 PyObject *prop;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
434 StgDictObject *dict;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
435 int bitsize = 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
436
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
437 if (!pair || !PyArg_ParseTuple(pair, "OO|i", &name, &desc, &bitsize)) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
438 PyErr_SetString(PyExc_AttributeError,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
439 "'_fields_' must be a sequence of pairs");
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
440 Py_XDECREF(pair);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
441 return -1;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
442 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
443 dict = PyType_stgdict(desc);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
444 if (dict == NULL) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
445 Py_DECREF(pair);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
446 PyErr_Format(PyExc_TypeError,
41999
b4f4ee84c30d Fix gcc warnings intruduced by passing Py_ssize_t to PyErr_Format calls.
Thomas Heller <theller@ctypes.org>
parents: 41998
diff changeset
447 #if (PY_VERSION_HEX < 0x02050000)
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
448 "second item in _fields_ tuple (index %d) must be a C type",
41999
b4f4ee84c30d Fix gcc warnings intruduced by passing Py_ssize_t to PyErr_Format calls.
Thomas Heller <theller@ctypes.org>
parents: 41998
diff changeset
449 #else
b4f4ee84c30d Fix gcc warnings intruduced by passing Py_ssize_t to PyErr_Format calls.
Thomas Heller <theller@ctypes.org>
parents: 41998
diff changeset
450 "second item in _fields_ tuple (index %zd) must be a C type",
b4f4ee84c30d Fix gcc warnings intruduced by passing Py_ssize_t to PyErr_Format calls.
Thomas Heller <theller@ctypes.org>
parents: 41998
diff changeset
451 #endif
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
452 i);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
453 return -1;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
454 }
37848
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
455 stgdict->ffi_type_pointer.elements[ffi_ofs + i] = &dict->ffi_type_pointer;
45315
beea35b0ed50 Add pickle support to ctypes types.
Thomas Heller <theller@ctypes.org>
parents: 44663
diff changeset
456 if (dict->flags & (TYPEFLAG_ISPOINTER | TYPEFLAG_HASPOINTER))
beea35b0ed50 Add pickle support to ctypes types.
Thomas Heller <theller@ctypes.org>
parents: 44663
diff changeset
457 stgdict->flags |= TYPEFLAG_HASPOINTER;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
458 dict->flags |= DICTFLAG_FINAL; /* mark field type final */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
459 if (PyTuple_Size(pair) == 3) { /* bits specified */
37848
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
460 switch(dict->ffi_type_pointer.type) {
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
461 case FFI_TYPE_UINT8:
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
462 case FFI_TYPE_UINT16:
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
463 case FFI_TYPE_UINT32:
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
464 case FFI_TYPE_SINT64:
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
465 case FFI_TYPE_UINT64:
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
466 break;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
467
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
468 case FFI_TYPE_SINT8:
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
469 case FFI_TYPE_SINT16:
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
470 case FFI_TYPE_SINT32:
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
471 if (dict->getfunc != _ctypes_get_fielddesc("c")->getfunc
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
472 #ifdef CTYPES_UNICODE
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
473 && dict->getfunc != _ctypes_get_fielddesc("u")->getfunc
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
474 #endif
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
475 )
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
476 break;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
477 /* else fall through */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
478 default:
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
479 PyErr_Format(PyExc_TypeError,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
480 "bit fields not allowed for type %s",
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
481 ((PyTypeObject *)desc)->tp_name);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
482 Py_DECREF(pair);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
483 return -1;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
484 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
485 if (bitsize <= 0 || bitsize > dict->size * 8) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
486 PyErr_SetString(PyExc_ValueError,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
487 "number of bits invalid for bit field");
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
488 Py_DECREF(pair);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
489 return -1;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
490 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
491 } else
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
492 bitsize = 0;
47475
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
493 if (isStruct && !isPacked) {
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
494 char *fieldfmt = dict->format ? dict->format : "B";
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
495 char *fieldname = PyString_AsString(name);
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
496 char *ptr;
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
497 Py_ssize_t len = strlen(fieldname) + strlen(fieldfmt);
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
498 char *buf = alloca(len + 2 + 1);
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
499
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
500 sprintf(buf, "%s:%s:", fieldfmt, fieldname);
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
501
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
502 ptr = stgdict->format;
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
503 stgdict->format = _ctypes_alloc_format_string(stgdict->format, buf);
47475
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
504 PyMem_Free(ptr);
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
505
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
506 if (stgdict->format == NULL) {
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
507 Py_DECREF(pair);
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
508 return -1;
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
509 }
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
510 }
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
511 if (isStruct) {
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
512 prop = PyCField_FromDesc(desc, i,
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
513 &field_size, bitsize, &bitofs,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
514 &size, &offset, &align,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
515 pack, big_endian);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
516 } else /* union */ {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
517 size = 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
518 offset = 0;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
519 align = 0;
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
520 prop = PyCField_FromDesc(desc, i,
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
521 &field_size, bitsize, &bitofs,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
522 &size, &offset, &align,
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
523 pack, big_endian);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
524 union_size = max(size, union_size);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
525 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
526 total_align = max(align, total_align);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
527
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
528 if (!prop) {
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
529 Py_DECREF(pair);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
530 return -1;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
531 }
44663
4293a06afd72 As discussed in issue 1700288:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 44350
diff changeset
532 if (-1 == PyObject_SetAttr(type, name, prop)) {
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
533 Py_DECREF(prop);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
534 Py_DECREF(pair);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
535 return -1;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
536 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
537 Py_DECREF(pair);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
538 Py_DECREF(prop);
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
539 }
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
540 #undef realdict
47475
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
541
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
542 if (isStruct && !isPacked) {
48847
Neal Norwitz <nnorwitz@gmail.com>
parents: 47475
diff changeset
543 char *ptr = stgdict->format;
53365
d51e4f62bc8c Issue #3102: All global symbols that the _ctypes extension defines are
Thomas Heller <theller@ctypes.org>
parents: 48847
diff changeset
544 stgdict->format = _ctypes_alloc_format_string(stgdict->format, "}");
48847
Neal Norwitz <nnorwitz@gmail.com>
parents: 47475
diff changeset
545 PyMem_Free(ptr);
47475
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
546 if (stgdict->format == NULL)
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
547 return -1;
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
548 }
bbce18d00d87 Backport from py3k: Implement the new buffer interface from pep3118
Thomas Heller <theller@ctypes.org>
parents: 45315
diff changeset
549
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
550 if (!isStruct)
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
551 size = union_size;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
552
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
553 /* Adjust the size according to the alignment requirements */
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
554 size = ((size + total_align - 1) / total_align) * total_align;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
555
41998
7b84e8919b00 [ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
Thomas Heller <theller@ctypes.org>
parents: 41562
diff changeset
556 stgdict->ffi_type_pointer.alignment = Py_SAFE_DOWNCAST(total_align,
7b84e8919b00 [ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
Thomas Heller <theller@ctypes.org>
parents: 41562
diff changeset
557 Py_ssize_t,
7b84e8919b00 [ 1715718 ] x64 clean compile patch for _ctypes, by Kristj?n Valur
Thomas Heller <theller@ctypes.org>
parents: 41562
diff changeset
558 unsigned short);
37848
dd4156c36703 C++ compiler cleanup: the typical few casts, and ... C++ didn't like that
Skip Montanaro <skip@pobox.com>
parents: 36897
diff changeset
559 stgdict->ffi_type_pointer.size = size;
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
560
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
561 stgdict->size = size;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
562 stgdict->align = total_align;
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
563 stgdict->length = len; /* ADD ffi_ofs? */
40393
96be9d74ad36 Fix bug #1598620: A ctypes structure cannot contain itself.
Thomas Heller <theller@ctypes.org>
parents: 39804
diff changeset
564
96be9d74ad36 Fix bug #1598620: A ctypes structure cannot contain itself.
Thomas Heller <theller@ctypes.org>
parents: 39804
diff changeset
565 /* We did check that this flag was NOT set above, it must not
96be9d74ad36 Fix bug #1598620: A ctypes structure cannot contain itself.
Thomas Heller <theller@ctypes.org>
parents: 39804
diff changeset
566 have been set until now. */
96be9d74ad36 Fix bug #1598620: A ctypes structure cannot contain itself.
Thomas Heller <theller@ctypes.org>
parents: 39804
diff changeset
567 if (stgdict->flags & DICTFLAG_FINAL) {
96be9d74ad36 Fix bug #1598620: A ctypes structure cannot contain itself.
Thomas Heller <theller@ctypes.org>
parents: 39804
diff changeset
568 PyErr_SetString(PyExc_AttributeError,
96be9d74ad36 Fix bug #1598620: A ctypes structure cannot contain itself.
Thomas Heller <theller@ctypes.org>
parents: 39804
diff changeset
569 "Structure or union cannot contain itself");
96be9d74ad36 Fix bug #1598620: A ctypes structure cannot contain itself.
Thomas Heller <theller@ctypes.org>
parents: 39804
diff changeset
570 return -1;
96be9d74ad36 Fix bug #1598620: A ctypes structure cannot contain itself.
Thomas Heller <theller@ctypes.org>
parents: 39804
diff changeset
571 }
96be9d74ad36 Fix bug #1598620: A ctypes structure cannot contain itself.
Thomas Heller <theller@ctypes.org>
parents: 39804
diff changeset
572 stgdict->flags |= DICTFLAG_FINAL;
96be9d74ad36 Fix bug #1598620: A ctypes structure cannot contain itself.
Thomas Heller <theller@ctypes.org>
parents: 39804
diff changeset
573
38720
aa5a1bca28dd Upgrade to ctypes version 0.9.9.7.
Thomas Heller <theller@ctypes.org>
parents: 37848
diff changeset
574 return MakeAnonFields(type);
36897
6f2eda495d9d Copy ctypes-0.9.9.4 sources from external into the trunk.
Thomas Heller <theller@ctypes.org>
parents:
diff changeset
575 }