1 #if defined (__SVR4) && defined (__sun)
2 # include <alloca.h>
3 #endif
4
5 #ifndef MS_WIN32
6 #define max(a, b) ((a) > (b) ? (a) : (b))
7 #define min(a, b) ((a) < (b) ? (a) : (b))
8
9 #define PARAMFLAG_FIN 0x1
10 #define PARAMFLAG_FOUT 0x2
11 #define PARAMFLAG_FLCID 0x4
12 #endif
13
14 /*
15 * bpo-13097: Max number of arguments CFuncPtr._argtypes_ and
16 * _ctypes_callproc() will accept.
17 *
18 * This limit is enforced for the `alloca()` call in `_ctypes_callproc`,
19 * to avoid allocating a massive buffer on the stack.
20 */
21 #ifndef CTYPES_MAX_ARGCOUNT
22 #ifdef __EMSCRIPTEN__
23 #define CTYPES_MAX_ARGCOUNT 1000
24 #else
25 #define CTYPES_MAX_ARGCOUNT 1024
26 #endif
27 #endif
28
29 #if defined(__has_builtin)
30 #if __has_builtin(__builtin_available)
31 #define HAVE_BUILTIN_AVAILABLE 1
32 #endif
33 #endif
34
35 typedef struct tagPyCArgObject PyCArgObject;
36 typedef struct tagCDataObject CDataObject;
37 typedef PyObject *(* GETFUNC)(void *, Py_ssize_t size);
38 typedef PyObject *(* SETFUNC)(void *, PyObject *value, Py_ssize_t size);
39 typedef PyCArgObject *(* PARAMFUNC)(CDataObject *obj);
40
41 /* A default buffer in CDataObject, which can be used for small C types. If
42 this buffer is too small, PyMem_Malloc will be called to create a larger one,
43 and this one is not used.
44
45 Making CDataObject a variable size object would be a better solution, but more
46 difficult in the presence of PyCFuncPtrObject. Maybe later.
47 */
48 union value {
49 char c[16];
50 short s;
51 int i;
52 long l;
53 float f;
54 double d;
55 long long ll;
56 long double D;
57 };
58
59 /*
60 Hm. Are there CDataObject's which do not need the b_objects member? In
61 this case we probably should introduce b_flags to mark it as present... If
62 b_objects is not present/unused b_length is unneeded as well.
63 */
64
65 struct tagCDataObject {
66 PyObject_HEAD
67 char *b_ptr; /* pointer to memory block */
68 int b_needsfree; /* need _we_ free the memory? */
69 CDataObject *b_base; /* pointer to base object or NULL */
70 Py_ssize_t b_size; /* size of memory block in bytes */
71 Py_ssize_t b_length; /* number of references we need */
72 Py_ssize_t b_index; /* index of this object into base's
73 b_object list */
74 PyObject *b_objects; /* dictionary of references we need to keep, or Py_None */
75 union value b_value;
76 };
77
78 typedef struct {
79 PyObject_VAR_HEAD
80 ffi_closure *pcl_write; /* the C callable, writeable */
81 void *pcl_exec; /* the C callable, executable */
82 ffi_cif cif;
83 int flags;
84 PyObject *converters;
85 PyObject *callable;
86 PyObject *restype;
87 SETFUNC setfunc;
88 ffi_type *ffi_restype;
89 ffi_type *atypes[1];
90 } CThunkObject;
91 extern PyTypeObject PyCThunk_Type;
92 #define CThunk_CheckExact(v) Py_IS_TYPE(v, &PyCThunk_Type)
93
94 typedef struct {
95 /* First part identical to tagCDataObject */
96 PyObject_HEAD
97 char *b_ptr; /* pointer to memory block */
98 int b_needsfree; /* need _we_ free the memory? */
99 CDataObject *b_base; /* pointer to base object or NULL */
100 Py_ssize_t b_size; /* size of memory block in bytes */
101 Py_ssize_t b_length; /* number of references we need */
102 Py_ssize_t b_index; /* index of this object into base's
103 b_object list */
104 PyObject *b_objects; /* list of references we need to keep */
105 union value b_value;
106 /* end of tagCDataObject, additional fields follow */
107
108 CThunkObject *thunk;
109 PyObject *callable;
110
111 /* These two fields will override the ones in the type's stgdict if
112 they are set */
113 PyObject *converters;
114 PyObject *argtypes;
115 PyObject *restype;
116 PyObject *checker;
117 PyObject *errcheck;
118 #ifdef MS_WIN32
119 int index;
120 GUID *iid;
121 #endif
122 PyObject *paramflags;
123 } PyCFuncPtrObject;
124
125 extern PyTypeObject PyCStgDict_Type;
126 #define PyCStgDict_CheckExact(v) Py_IS_TYPE(v, &PyCStgDict_Type)
127 #define PyCStgDict_Check(v) PyObject_TypeCheck(v, &PyCStgDict_Type)
128
129 extern int PyCStructUnionType_update_stgdict(PyObject *fields, PyObject *type, int isStruct);
130 extern int PyType_stginfo(PyTypeObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
131 extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
132
133
134
135 extern PyTypeObject PyCData_Type;
136 #define CDataObject_CheckExact(v) Py_IS_TYPE(v, &PyCData_Type)
137 #define CDataObject_Check(v) PyObject_TypeCheck(v, &PyCData_Type)
138 #define _CDataObject_HasExternalBuffer(v) ((v)->b_ptr != (char *)&(v)->b_value)
139
140 extern PyTypeObject PyCSimpleType_Type;
141 #define PyCSimpleTypeObject_CheckExact(v) Py_IS_TYPE(v, &PyCSimpleType_Type)
142 #define PyCSimpleTypeObject_Check(v) PyObject_TypeCheck(v, &PyCSimpleType_Type)
143
144 extern PyTypeObject PyCField_Type;
145 extern struct fielddesc *_ctypes_get_fielddesc(const char *fmt);
146
147
148 extern PyObject *
149 PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
150 Py_ssize_t *pfield_size, int bitsize, int *pbitofs,
151 Py_ssize_t *psize, Py_ssize_t *poffset, Py_ssize_t *palign,
152 int pack, int is_big_endian);
153
154 extern PyObject *PyCData_AtAddress(PyObject *type, void *buf);
155 extern PyObject *PyCData_FromBytes(PyObject *type, char *data, Py_ssize_t length);
156
157 extern PyTypeObject PyCArrayType_Type;
158 extern PyTypeObject PyCArray_Type;
159 extern PyTypeObject PyCPointerType_Type;
160 extern PyTypeObject PyCPointer_Type;
161 extern PyTypeObject PyCFuncPtr_Type;
162 extern PyTypeObject PyCFuncPtrType_Type;
163 extern PyTypeObject PyCStructType_Type;
164
165 #define PyCArrayTypeObject_Check(v) PyObject_TypeCheck(v, &PyCArrayType_Type)
166 #define ArrayObject_Check(v) PyObject_TypeCheck(v, &PyCArray_Type)
167 #define PointerObject_Check(v) PyObject_TypeCheck(v, &PyCPointer_Type)
168 #define PyCPointerTypeObject_Check(v) PyObject_TypeCheck(v, &PyCPointerType_Type)
169 #define PyCFuncPtrObject_Check(v) PyObject_TypeCheck(v, &PyCFuncPtr_Type)
170 #define PyCFuncPtrTypeObject_Check(v) PyObject_TypeCheck(v, &PyCFuncPtrType_Type)
171 #define PyCStructTypeObject_Check(v) PyObject_TypeCheck(v, &PyCStructType_Type)
172
173 extern PyObject *
174 PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length);
175
176 extern PyMethodDef _ctypes_module_methods[];
177
178 extern CThunkObject *_ctypes_alloc_callback(PyObject *callable,
179 PyObject *converters,
180 PyObject *restype,
181 int flags);
182 /* a table entry describing a predefined ctypes type */
183 struct fielddesc {
184 char code;
185 SETFUNC setfunc;
186 GETFUNC getfunc;
187 ffi_type *pffi_type; /* always statically allocated */
188 SETFUNC setfunc_swapped;
189 GETFUNC getfunc_swapped;
190 };
191
192 typedef struct {
193 PyObject_HEAD
194 Py_ssize_t offset;
195 Py_ssize_t size;
196 Py_ssize_t index; /* Index into CDataObject's
197 object array */
198 PyObject *proto; /* a type or NULL */
199 GETFUNC getfunc; /* getter function if proto is NULL */
200 SETFUNC setfunc; /* setter function if proto is NULL */
201 int anonymous;
202 } CFieldObject;
203
204 /* A subclass of PyDictObject, used as the instance dictionary of ctypes
205 metatypes */
206 typedef struct {
207 PyDictObject dict; /* first part identical to PyDictObject */
208 /* The size and align fields are unneeded, they are in ffi_type as well. As
209 an experiment shows, it's trivial to get rid of them, the only thing to
210 remember is that in PyCArrayType_new the ffi_type fields must be filled in -
211 so far it was unneeded because libffi doesn't support arrays at all
212 (because they are passed as pointers to function calls anyway). But it's
213 too much risk to change that now, and there are other fields which doesn't
214 belong into this structure anyway. Maybe in ctypes 2.0... (ctypes 2000?)
215 */
216 Py_ssize_t size; /* number of bytes */
217 Py_ssize_t align; /* alignment requirements */
218 Py_ssize_t length; /* number of fields */
219 ffi_type ffi_type_pointer;
220 PyObject *proto; /* Only for Pointer/ArrayObject */
221 SETFUNC setfunc; /* Only for simple objects */
222 GETFUNC getfunc; /* Only for simple objects */
223 PARAMFUNC paramfunc;
224
225 /* Following fields only used by PyCFuncPtrType_Type instances */
226 PyObject *argtypes; /* tuple of CDataObjects */
227 PyObject *converters; /* tuple([t.from_param for t in argtypes]) */
228 PyObject *restype; /* CDataObject or NULL */
229 PyObject *checker;
230 int flags; /* calling convention and such */
231
232 /* pep3118 fields, pointers need PyMem_Free */
233 char *format;
234 int ndim;
235 Py_ssize_t *shape;
236 /* Py_ssize_t *strides; */ /* unused in ctypes */
237 /* Py_ssize_t *suboffsets; */ /* unused in ctypes */
238
239 } StgDictObject;
240
241 /****************************************************************
242 StgDictObject fields
243
244 setfunc and getfunc is only set for simple data types, it is copied from the
245 corresponding fielddesc entry. These are functions to set and get the value
246 in a memory block.
247 They should probably by used by other types as well.
248
249 proto is only used for Pointer and Array types - it points to the item type
250 object.
251
252 Probably all the magic ctypes methods (like from_param) should have C
253 callable wrappers in the StgDictObject. For simple data type, for example,
254 the fielddesc table could have entries for C codec from_param functions or
255 other methods as well, if a subtype overrides this method in Python at
256 construction time, or assigns to it later, tp_setattro should update the
257 StgDictObject function to a generic one.
258
259 Currently, PyCFuncPtr types have 'converters' and 'checker' entries in their
260 type dict. They are only used to cache attributes from other entries, which
261 is wrong.
262
263 One use case is the .value attribute that all simple types have. But some
264 complex structures, like VARIANT, represent a single value also, and should
265 have this attribute.
266
267 Another use case is a _check_retval_ function, which is called when a ctypes
268 type is used as return type of a function to validate and compute the return
269 value.
270
271 Common ctypes protocol:
272
273 - setfunc: store a python value in a memory block
274 - getfunc: convert data from a memory block into a python value
275
276 - checkfunc: validate and convert a return value from a function call
277 - toparamfunc: convert a python value into a function argument
278
279 *****************************************************************/
280
281 /* May return NULL, but does not set an exception! */
282 extern StgDictObject *PyType_stgdict(PyObject *obj);
283
284 /* May return NULL, but does not set an exception! */
285 extern StgDictObject *PyObject_stgdict(PyObject *self);
286
287 extern int PyCStgDict_clone(StgDictObject *src, StgDictObject *dst);
288
289 typedef int(* PPROC)(void);
290
291 PyObject *_ctypes_callproc(PPROC pProc,
292 PyObject *arguments,
293 #ifdef MS_WIN32
294 IUnknown *pIUnk,
295 GUID *iid,
296 #endif
297 int flags,
298 PyObject *argtypes,
299 PyObject *restype,
300 PyObject *checker);
301
302
303 #define FUNCFLAG_STDCALL 0x0
304 #define FUNCFLAG_CDECL 0x1
305 #define FUNCFLAG_HRESULT 0x2
306 #define FUNCFLAG_PYTHONAPI 0x4
307 #define FUNCFLAG_USE_ERRNO 0x8
308 #define FUNCFLAG_USE_LASTERROR 0x10
309
310 #define TYPEFLAG_ISPOINTER 0x100
311 #define TYPEFLAG_HASPOINTER 0x200
312 #define TYPEFLAG_HASUNION 0x400
313 #define TYPEFLAG_HASBITFIELD 0x800
314
315 #define DICTFLAG_FINAL 0x1000
316
317 struct tagPyCArgObject {
318 PyObject_HEAD
319 ffi_type *pffi_type;
320 char tag;
321 union {
322 char c;
323 char b;
324 short h;
325 int i;
326 long l;
327 long long q;
328 long double D;
329 double d;
330 float f;
331 void *p;
332 } value;
333 PyObject *obj;
334 Py_ssize_t size; /* for the 'V' tag */
335 };
336
337 extern PyTypeObject PyCArg_Type;
338 #define PyCArg_CheckExact(v) Py_IS_TYPE(v, &PyCArg_Type)
339 extern PyCArgObject *PyCArgObject_new(void);
340
341 extern PyObject *
342 PyCData_get(PyObject *type, GETFUNC getfunc, PyObject *src,
343 Py_ssize_t index, Py_ssize_t size, char *ptr);
344
345 extern int
346 PyCData_set(PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
347 Py_ssize_t index, Py_ssize_t size, char *ptr);
348
349 extern void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...);
350
351 struct basespec {
352 CDataObject *base;
353 Py_ssize_t index;
354 char *adr;
355 };
356
357 extern char basespec_string[];
358
359 extern ffi_type *_ctypes_get_ffi_type(PyObject *obj);
360
361 /* exception classes */
362 extern PyObject *PyExc_ArgError;
363
364 extern char *_ctypes_conversion_encoding;
365 extern char *_ctypes_conversion_errors;
366
367
368 extern void _ctypes_free_closure(void *);
369 extern void *_ctypes_alloc_closure(void);
370
371 extern PyObject *PyCData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t index, char *adr);
372 extern char *_ctypes_alloc_format_string(const char *prefix, const char *suffix);
373 extern char *_ctypes_alloc_format_string_with_shape(int ndim,
374 const Py_ssize_t *shape,
375 const char *prefix, const char *suffix);
376
377 extern int _ctypes_simple_instance(PyObject *obj);
378
379 extern PyObject *_ctypes_ptrtype_cache;
380 PyObject *_ctypes_get_errobj(int **pspace);
381
382 #ifdef MS_WIN32
383 extern PyObject *ComError;
384 #endif
385
386 #ifdef USING_MALLOC_CLOSURE_DOT_C
387 void Py_ffi_closure_free(void *p);
388 void *Py_ffi_closure_alloc(size_t size, void** codeloc);
389 #else
390 #define Py_ffi_closure_free ffi_closure_free
391 #define Py_ffi_closure_alloc ffi_closure_alloc
392 #endif
393
394 /*
395 Local Variables:
396 compile-command: "python setup.py -q build install --home ~"
397 End:
398 */