blob: b4047a41bb3fe653c228d51444bb4f0fff9ab3d1 [file] [log] [blame]
Thomas Hellerd4c93202006-03-08 19:35:11 +00001#ifndef MS_WIN32
2#define max(a, b) ((a) > (b) ? (a) : (b))
3#define min(a, b) ((a) < (b) ? (a) : (b))
4
5#define PARAMFLAG_FIN 0x1
6#define PARAMFLAG_FOUT 0x2
7#define PARAMFLAG_FLCID 0x4
8#endif
9
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000010typedef struct tagPyCArgObject PyCArgObject;
Thomas Hellerd4c93202006-03-08 19:35:11 +000011typedef struct tagCDataObject CDataObject;
Guido van Rossumcd16bf62007-06-13 18:07:49 +000012typedef PyObject *(* GETFUNC)(void *, Py_ssize_t size);
13typedef PyObject *(* SETFUNC)(void *, PyObject *value, Py_ssize_t size);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000014typedef PyCArgObject *(* PARAMFUNC)(CDataObject *obj);
Thomas Hellerd4c93202006-03-08 19:35:11 +000015
16/* A default buffer in CDataObject, which can be used for small C types. If
17this buffer is too small, PyMem_Malloc will be called to create a larger one,
18and this one is not used.
19
20Making CDataObject a variable size object would be a better solution, but more
Thomas Heller34596a92009-04-24 20:50:00 +000021difficult in the presence of PyCFuncPtrObject. Maybe later.
Thomas Hellerd4c93202006-03-08 19:35:11 +000022*/
23union value {
24 char c[16];
25 short s;
26 int i;
27 long l;
28 float f;
29 double d;
30#ifdef HAVE_LONG_LONG
31 PY_LONG_LONG ll;
32#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +000033 long double D;
Thomas Hellerd4c93202006-03-08 19:35:11 +000034};
35
36/*
37 Hm. Are there CDataObject's which do not need the b_objects member? In
38 this case we probably should introduce b_flags to mark it as present... If
39 b_objects is not present/unused b_length is unneeded as well.
40*/
41
42struct tagCDataObject {
43 PyObject_HEAD
44 char *b_ptr; /* pointer to memory block */
45 int b_needsfree; /* need _we_ free the memory? */
46 CDataObject *b_base; /* pointer to base object or NULL */
Thomas Hellerfe8f8622006-03-14 19:53:09 +000047 Py_ssize_t b_size; /* size of memory block in bytes */
48 Py_ssize_t b_length; /* number of references we need */
49 Py_ssize_t b_index; /* index of this object into base's
Thomas Hellerd4c93202006-03-08 19:35:11 +000050 b_object list */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000051 PyObject *b_objects; /* dictionary of references we need to keep, or Py_None */
Thomas Hellerd4c93202006-03-08 19:35:11 +000052 union value b_value;
53};
54
55typedef struct {
Thomas Hellere106dc32008-04-24 18:39:36 +000056 PyObject_VAR_HEAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000057 ffi_closure *pcl; /* the C callable */
58 ffi_cif cif;
Thomas Heller9cac7b62008-06-06 09:31:40 +000059 int flags;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000060 PyObject *converters;
61 PyObject *callable;
Thomas Hellere106dc32008-04-24 18:39:36 +000062 PyObject *restype;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000063 SETFUNC setfunc;
Thomas Hellere106dc32008-04-24 18:39:36 +000064 ffi_type *ffi_restype;
Guido van Rossumb5a755e2007-07-18 18:15:48 +000065 ffi_type *atypes[1];
Thomas Hellere106dc32008-04-24 18:39:36 +000066} CThunkObject;
Thomas Heller34596a92009-04-24 20:50:00 +000067extern PyTypeObject PyCThunk_Type;
68#define CThunk_CheckExact(v) ((v)->ob_type == &PyCThunk_Type)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000069
70typedef struct {
Thomas Hellerd4c93202006-03-08 19:35:11 +000071 /* First part identical to tagCDataObject */
72 PyObject_HEAD
73 char *b_ptr; /* pointer to memory block */
74 int b_needsfree; /* need _we_ free the memory? */
75 CDataObject *b_base; /* pointer to base object or NULL */
Thomas Hellerfe8f8622006-03-14 19:53:09 +000076 Py_ssize_t b_size; /* size of memory block in bytes */
77 Py_ssize_t b_length; /* number of references we need */
78 Py_ssize_t b_index; /* index of this object into base's
Thomas Hellerd4c93202006-03-08 19:35:11 +000079 b_object list */
80 PyObject *b_objects; /* list of references we need to keep */
81 union value b_value;
82 /* end of tagCDataObject, additional fields follow */
83
Thomas Hellere106dc32008-04-24 18:39:36 +000084 CThunkObject *thunk;
Thomas Hellerd4c93202006-03-08 19:35:11 +000085 PyObject *callable;
86
87 /* These two fields will override the ones in the type's stgdict if
88 they are set */
89 PyObject *converters;
90 PyObject *argtypes;
91 PyObject *restype;
92 PyObject *checker;
93 PyObject *errcheck;
94#ifdef MS_WIN32
95 int index;
96 GUID *iid;
97#endif
98 PyObject *paramflags;
Thomas Heller34596a92009-04-24 20:50:00 +000099} PyCFuncPtrObject;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000100
Thomas Heller34596a92009-04-24 20:50:00 +0000101extern PyTypeObject PyCStgDict_Type;
102#define PyCStgDict_CheckExact(v) ((v)->ob_type == &PyCStgDict_Type)
103#define PyCStgDict_Check(v) PyObject_TypeCheck(v, &PyCStgDict_Type)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000104
Thomas Heller34596a92009-04-24 20:50:00 +0000105extern int PyCStructUnionType_update_stgdict(PyObject *fields, PyObject *type, int isStruct);
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000106extern int PyType_stginfo(PyTypeObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
107extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000108
109
110
Thomas Heller34596a92009-04-24 20:50:00 +0000111extern PyTypeObject PyCData_Type;
112#define CDataObject_CheckExact(v) ((v)->ob_type == &PyCData_Type)
113#define CDataObject_Check(v) PyObject_TypeCheck(v, &PyCData_Type)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000114
Thomas Heller34596a92009-04-24 20:50:00 +0000115extern PyTypeObject PyCSimpleType_Type;
116#define PyCSimpleTypeObject_CheckExact(v) ((v)->ob_type == &PyCSimpleType_Type)
117#define PyCSimpleTypeObject_Check(v) PyObject_TypeCheck(v, &PyCSimpleType_Type)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000118
Thomas Heller34596a92009-04-24 20:50:00 +0000119extern PyTypeObject PyCField_Type;
120extern struct fielddesc *_ctypes_get_fielddesc(const char *fmt);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000121
122
123extern PyObject *
Thomas Heller34596a92009-04-24 20:50:00 +0000124PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000125 Py_ssize_t *pfield_size, int bitsize, int *pbitofs,
126 Py_ssize_t *psize, Py_ssize_t *poffset, Py_ssize_t *palign,
Thomas Hellerd4c93202006-03-08 19:35:11 +0000127 int pack, int is_big_endian);
128
Thomas Heller34596a92009-04-24 20:50:00 +0000129extern PyObject *PyCData_AtAddress(PyObject *type, void *buf);
130extern PyObject *PyCData_FromBytes(PyObject *type, char *data, Py_ssize_t length);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000131
Thomas Heller34596a92009-04-24 20:50:00 +0000132extern PyTypeObject PyCArrayType_Type;
133extern PyTypeObject PyCArray_Type;
134extern PyTypeObject PyCPointerType_Type;
135extern PyTypeObject PyCPointer_Type;
136extern PyTypeObject PyCFuncPtr_Type;
137extern PyTypeObject PyCFuncPtrType_Type;
138extern PyTypeObject PyCStructType_Type;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000139
Thomas Heller34596a92009-04-24 20:50:00 +0000140#define PyCArrayTypeObject_Check(v) PyObject_TypeCheck(v, &PyCArrayType_Type)
141#define ArrayObject_Check(v) PyObject_TypeCheck(v, &PyCArray_Type)
142#define PointerObject_Check(v) PyObject_TypeCheck(v, &PyCPointer_Type)
143#define PyCPointerTypeObject_Check(v) PyObject_TypeCheck(v, &PyCPointerType_Type)
144#define PyCFuncPtrObject_Check(v) PyObject_TypeCheck(v, &PyCFuncPtr_Type)
145#define PyCFuncPtrTypeObject_Check(v) PyObject_TypeCheck(v, &PyCFuncPtrType_Type)
146#define PyCStructTypeObject_Check(v) PyObject_TypeCheck(v, &PyCStructType_Type)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000147
148extern PyObject *
Thomas Heller34596a92009-04-24 20:50:00 +0000149PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000150
Thomas Heller34596a92009-04-24 20:50:00 +0000151extern PyMethodDef _ctypes_module_methods[];
Thomas Hellerd4c93202006-03-08 19:35:11 +0000152
Thomas Heller34596a92009-04-24 20:50:00 +0000153extern CThunkObject *_ctypes_alloc_callback(PyObject *callable,
Thomas Hellere106dc32008-04-24 18:39:36 +0000154 PyObject *converters,
155 PyObject *restype,
Thomas Heller9cac7b62008-06-06 09:31:40 +0000156 int flags);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000157/* a table entry describing a predefined ctypes type */
158struct fielddesc {
159 char code;
160 SETFUNC setfunc;
161 GETFUNC getfunc;
162 ffi_type *pffi_type; /* always statically allocated */
163 SETFUNC setfunc_swapped;
164 GETFUNC getfunc_swapped;
165};
166
167typedef struct {
168 PyObject_HEAD
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000169 Py_ssize_t offset;
170 Py_ssize_t size;
171 Py_ssize_t index; /* Index into CDataObject's
Thomas Hellerd4c93202006-03-08 19:35:11 +0000172 object array */
173 PyObject *proto; /* a type or NULL */
174 GETFUNC getfunc; /* getter function if proto is NULL */
175 SETFUNC setfunc; /* setter function if proto is NULL */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000176 int anonymous;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000177} CFieldObject;
178
179/* A subclass of PyDictObject, used as the instance dictionary of ctypes
180 metatypes */
181typedef struct {
182 PyDictObject dict; /* first part identical to PyDictObject */
183/* The size and align fields are unneeded, they are in ffi_type as well. As
184 an experiment shows, it's trivial to get rid of them, the only thing to
Thomas Heller34596a92009-04-24 20:50:00 +0000185 remember is that in PyCArrayType_new the ffi_type fields must be filled in -
Thomas Hellerd4c93202006-03-08 19:35:11 +0000186 so far it was unneeded because libffi doesn't support arrays at all
187 (because they are passed as pointers to function calls anyway). But it's
188 too much risk to change that now, and there are other fields which doen't
189 belong into this structure anyway. Maybe in ctypes 2.0... (ctypes 2000?)
190*/
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000191 Py_ssize_t size; /* number of bytes */
192 Py_ssize_t align; /* alignment requirements */
193 Py_ssize_t length; /* number of fields */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000194 ffi_type ffi_type_pointer;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000195 PyObject *proto; /* Only for Pointer/ArrayObject */
196 SETFUNC setfunc; /* Only for simple objects */
197 GETFUNC getfunc; /* Only for simple objects */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000198 PARAMFUNC paramfunc;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000199
Thomas Heller34596a92009-04-24 20:50:00 +0000200 /* Following fields only used by PyCFuncPtrType_Type instances */
Thomas Hellerd4c93202006-03-08 19:35:11 +0000201 PyObject *argtypes; /* tuple of CDataObjects */
202 PyObject *converters; /* tuple([t.from_param for t in argtypes]) */
203 PyObject *restype; /* CDataObject or NULL */
204 PyObject *checker;
205 int flags; /* calling convention and such */
Thomas Hellerb041fda2008-04-30 17:11:46 +0000206
207 /* pep3118 fields, pointers neeed PyMem_Free */
208 char *format;
209 int ndim;
210 Py_ssize_t *shape;
211/* Py_ssize_t *strides; */ /* unused in ctypes */
212/* Py_ssize_t *suboffsets; */ /* unused in ctypes */
213
Thomas Hellerd4c93202006-03-08 19:35:11 +0000214} StgDictObject;
215
216/****************************************************************
217 StgDictObject fields
218
219 setfunc and getfunc is only set for simple data types, it is copied from the
220 corresponding fielddesc entry. These are functions to set and get the value
221 in a memory block.
222 They should probably by used by other types as well.
223
224 proto is only used for Pointer and Array types - it points to the item type
225 object.
226
227 Probably all the magic ctypes methods (like from_param) should have C
228 callable wrappers in the StgDictObject. For simple data type, for example,
229 the fielddesc table could have entries for C codec from_param functions or
230 other methods as well, if a subtype overrides this method in Python at
231 construction time, or assigns to it later, tp_setattro should update the
232 StgDictObject function to a generic one.
233
Thomas Heller34596a92009-04-24 20:50:00 +0000234 Currently, PyCFuncPtr types have 'converters' and 'checker' entries in their
Thomas Hellerd4c93202006-03-08 19:35:11 +0000235 type dict. They are only used to cache attributes from other entries, whihc
236 is wrong.
237
238 One use case is the .value attribute that all simple types have. But some
239 complex structures, like VARIANT, represent a single value also, and should
240 have this attribute.
241
242 Another use case is a _check_retval_ function, which is called when a ctypes
243 type is used as return type of a function to validate and compute the return
244 value.
245
246 Common ctypes protocol:
247
248 - setfunc: store a python value in a memory block
249 - getfunc: convert data from a memory block into a python value
250
251 - checkfunc: validate and convert a return value from a function call
252 - toparamfunc: convert a python value into a function argument
253
254*****************************************************************/
255
256/* May return NULL, but does not set an exception! */
257extern StgDictObject *PyType_stgdict(PyObject *obj);
258
259/* May return NULL, but does not set an exception! */
260extern StgDictObject *PyObject_stgdict(PyObject *self);
261
Thomas Heller34596a92009-04-24 20:50:00 +0000262extern int PyCStgDict_clone(StgDictObject *src, StgDictObject *dst);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000263
264typedef int(* PPROC)(void);
265
Thomas Heller34596a92009-04-24 20:50:00 +0000266PyObject *_ctypes_callproc(PPROC pProc,
Thomas Hellerd4c93202006-03-08 19:35:11 +0000267 PyObject *arguments,
268#ifdef MS_WIN32
269 IUnknown *pIUnk,
270 GUID *iid,
271#endif
272 int flags,
273 PyObject *argtypes,
274 PyObject *restype,
275 PyObject *checker);
276
277
278#define FUNCFLAG_STDCALL 0x0
279#define FUNCFLAG_CDECL 0x1
280#define FUNCFLAG_HRESULT 0x2
281#define FUNCFLAG_PYTHONAPI 0x4
Thomas Heller9cac7b62008-06-06 09:31:40 +0000282#define FUNCFLAG_USE_ERRNO 0x8
283#define FUNCFLAG_USE_LASTERROR 0x10
Thomas Hellerd4c93202006-03-08 19:35:11 +0000284
Thomas Heller13394e92008-02-13 20:40:44 +0000285#define TYPEFLAG_ISPOINTER 0x100
286#define TYPEFLAG_HASPOINTER 0x200
287
Thomas Hellerd4c93202006-03-08 19:35:11 +0000288#define DICTFLAG_FINAL 0x1000
289
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000290struct tagPyCArgObject {
Thomas Hellerd4c93202006-03-08 19:35:11 +0000291 PyObject_HEAD
292 ffi_type *pffi_type;
293 char tag;
294 union {
295 char c;
296 char b;
297 short h;
298 int i;
299 long l;
300#ifdef HAVE_LONG_LONG
301 PY_LONG_LONG q;
302#endif
Thomas Wouters89d996e2007-09-08 17:39:28 +0000303 long double D;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000304 double d;
305 float f;
306 void *p;
307 } value;
308 PyObject *obj;
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000309 Py_ssize_t size; /* for the 'V' tag */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000310};
Thomas Hellerd4c93202006-03-08 19:35:11 +0000311
312extern PyTypeObject PyCArg_Type;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000313#define PyCArg_CheckExact(v) ((v)->ob_type == &PyCArg_Type)
Thomas Heller34596a92009-04-24 20:50:00 +0000314extern PyCArgObject *PyCArgObject_new(void);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000315
316extern PyObject *
Thomas Heller34596a92009-04-24 20:50:00 +0000317PyCData_get(PyObject *type, GETFUNC getfunc, PyObject *src,
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000318 Py_ssize_t index, Py_ssize_t size, char *ptr);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000319
320extern int
Thomas Heller34596a92009-04-24 20:50:00 +0000321PyCData_set(PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000322 Py_ssize_t index, Py_ssize_t size, char *ptr);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000323
Thomas Heller34596a92009-04-24 20:50:00 +0000324extern void _ctypes_extend_error(PyObject *exc_class, char *fmt, ...);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000325
326struct basespec {
327 CDataObject *base;
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000328 Py_ssize_t index;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000329 char *adr;
330};
331
332extern char basespec_string[];
333
Thomas Heller34596a92009-04-24 20:50:00 +0000334extern ffi_type *_ctypes_get_ffi_type(PyObject *obj);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000335
336/* exception classes */
337extern PyObject *PyExc_ArgError;
338
Thomas Heller34596a92009-04-24 20:50:00 +0000339extern char *_ctypes_conversion_encoding;
340extern char *_ctypes_conversion_errors;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000341
Thomas Heller5fa3f052007-07-12 11:00:22 +0000342#if defined(HAVE_WCHAR_H)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000343# define CTYPES_UNICODE
344#endif
345
346
Thomas Heller34596a92009-04-24 20:50:00 +0000347extern void _ctypes_free_closure(void *);
348extern void *_ctypes_alloc_closure(void);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000349
Thomas Heller34596a92009-04-24 20:50:00 +0000350extern void _ctypes_add_traceback(char *, char *, int);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000351
Thomas Heller34596a92009-04-24 20:50:00 +0000352extern PyObject *PyCData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t index, char *adr);
353extern char *_ctypes_alloc_format_string(const char *prefix, const char *suffix);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000354
Thomas Heller34596a92009-04-24 20:50:00 +0000355extern int _ctypes_simple_instance(PyObject *obj);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000356
Thomas Heller34596a92009-04-24 20:50:00 +0000357extern PyObject *_ctypes_ptrtype_cache;
358PyObject *_ctypes_get_errobj(int **pspace);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000359
360#ifdef MS_WIN32
361extern PyObject *ComError;
362#endif
363
364/*
365 Local Variables:
366 compile-command: "python setup.py -q build install --home ~"
367 End:
368*/