blob: 303eda31ec9c8a91f0ab7c09d78fa11e7e0b1872 [file] [log] [blame]
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001/*****************************************************************
2 This file should be kept compatible with Python 2.3, see PEP 291.
3 *****************************************************************/
Thomas Hellerd4c93202006-03-08 19:35:11 +00004
Thomas Woutersa9773292006-04-21 09:43:23 +00005#if (PY_VERSION_HEX < 0x02050000)
6typedef int Py_ssize_t;
Thomas Woutersa9773292006-04-21 09:43:23 +00007#endif
8
Thomas Hellerd4c93202006-03-08 19:35:11 +00009#ifndef MS_WIN32
10#define max(a, b) ((a) > (b) ? (a) : (b))
11#define min(a, b) ((a) < (b) ? (a) : (b))
12
13#define PARAMFLAG_FIN 0x1
14#define PARAMFLAG_FOUT 0x2
15#define PARAMFLAG_FLCID 0x4
16#endif
17
18/*
19 Backwards compatibility:
20 Python2.2 used LONG_LONG instead of PY_LONG_LONG
21*/
22#if defined(HAVE_LONG_LONG) && !defined(PY_LONG_LONG)
23#define PY_LONG_LONG LONG_LONG
24#endif
25
Thomas Hellerd4c93202006-03-08 19:35:11 +000026typedef struct tagCDataObject CDataObject;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027typedef PyObject *(* GETFUNC)(void *, unsigned size);
28typedef PyObject *(* SETFUNC)(void *, PyObject *value, unsigned size);
Thomas Hellerd4c93202006-03-08 19:35:11 +000029
30/* A default buffer in CDataObject, which can be used for small C types. If
31this buffer is too small, PyMem_Malloc will be called to create a larger one,
32and this one is not used.
33
34Making CDataObject a variable size object would be a better solution, but more
35difficult in the presence of CFuncPtrObject. Maybe later.
36*/
37union value {
38 char c[16];
39 short s;
40 int i;
41 long l;
42 float f;
43 double d;
44#ifdef HAVE_LONG_LONG
45 PY_LONG_LONG ll;
46#endif
47};
48
49/*
50 Hm. Are there CDataObject's which do not need the b_objects member? In
51 this case we probably should introduce b_flags to mark it as present... If
52 b_objects is not present/unused b_length is unneeded as well.
53*/
54
55struct tagCDataObject {
56 PyObject_HEAD
57 char *b_ptr; /* pointer to memory block */
58 int b_needsfree; /* need _we_ free the memory? */
59 CDataObject *b_base; /* pointer to base object or NULL */
Thomas Hellerfe8f8622006-03-14 19:53:09 +000060 Py_ssize_t b_size; /* size of memory block in bytes */
61 Py_ssize_t b_length; /* number of references we need */
62 Py_ssize_t b_index; /* index of this object into base's
Thomas Hellerd4c93202006-03-08 19:35:11 +000063 b_object list */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064 PyObject *b_objects; /* dictionary of references we need to keep, or Py_None */
Thomas Hellerd4c93202006-03-08 19:35:11 +000065 union value b_value;
66};
67
68typedef struct {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000069 ffi_closure *pcl; /* the C callable */
70 ffi_cif cif;
71 PyObject *converters;
72 PyObject *callable;
73 SETFUNC setfunc;
74 ffi_type *restype;
75 ffi_type *atypes[0];
76} ffi_info;
77
78typedef struct {
Thomas Hellerd4c93202006-03-08 19:35:11 +000079 /* First part identical to tagCDataObject */
80 PyObject_HEAD
81 char *b_ptr; /* pointer to memory block */
82 int b_needsfree; /* need _we_ free the memory? */
83 CDataObject *b_base; /* pointer to base object or NULL */
Thomas Hellerfe8f8622006-03-14 19:53:09 +000084 Py_ssize_t b_size; /* size of memory block in bytes */
85 Py_ssize_t b_length; /* number of references we need */
86 Py_ssize_t b_index; /* index of this object into base's
Thomas Hellerd4c93202006-03-08 19:35:11 +000087 b_object list */
88 PyObject *b_objects; /* list of references we need to keep */
89 union value b_value;
90 /* end of tagCDataObject, additional fields follow */
91
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000092 ffi_info *thunk;
Thomas Hellerd4c93202006-03-08 19:35:11 +000093 PyObject *callable;
94
95 /* These two fields will override the ones in the type's stgdict if
96 they are set */
97 PyObject *converters;
98 PyObject *argtypes;
99 PyObject *restype;
100 PyObject *checker;
101 PyObject *errcheck;
102#ifdef MS_WIN32
103 int index;
104 GUID *iid;
105#endif
106 PyObject *paramflags;
107} CFuncPtrObject;
108
109extern PyTypeObject StgDict_Type;
110#define StgDict_CheckExact(v) ((v)->ob_type == &StgDict_Type)
111#define StgDict_Check(v) PyObject_TypeCheck(v, &StgDict_Type)
112
113extern int StructUnionType_update_stgdict(PyObject *fields, PyObject *type, int isStruct);
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000114extern int PyType_stginfo(PyTypeObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
115extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000116
117
118
119extern PyTypeObject CData_Type;
120#define CDataObject_CheckExact(v) ((v)->ob_type == &CData_Type)
121#define CDataObject_Check(v) PyObject_TypeCheck(v, &CData_Type)
122
123extern PyTypeObject SimpleType_Type;
124#define SimpleTypeObject_CheckExact(v) ((v)->ob_type == &SimpleType_Type)
125#define SimpleTypeObject_Check(v) PyObject_TypeCheck(v, &SimpleType_Type)
126
127extern PyTypeObject CField_Type;
128extern struct fielddesc *getentry(char *fmt);
129
130
131extern PyObject *
132CField_FromDesc(PyObject *desc, int index,
133 int *pfield_size, int bitsize, int *pbitofs,
134 int *psize, int *poffset, int *palign,
135 int pack, int is_big_endian);
136
137extern PyObject *CData_AtAddress(PyObject *type, void *buf);
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000138extern PyObject *CData_FromBytes(PyObject *type, char *data, Py_ssize_t length);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000139
140extern PyTypeObject ArrayType_Type;
141extern PyTypeObject Array_Type;
142extern PyTypeObject PointerType_Type;
143extern PyTypeObject Pointer_Type;
144extern PyTypeObject CFuncPtr_Type;
145extern PyTypeObject CFuncPtrType_Type;
146extern PyTypeObject StructType_Type;
147
148#define ArrayTypeObject_Check(v) PyObject_TypeCheck(v, &ArrayType_Type)
149#define ArrayObject_Check(v) PyObject_TypeCheck(v, &Array_Type)
150#define PointerObject_Check(v) PyObject_TypeCheck(v, &Pointer_Type)
151#define PointerTypeObject_Check(v) PyObject_TypeCheck(v, &PointerType_Type)
152#define CFuncPtrObject_Check(v) PyObject_TypeCheck(v, &CFuncPtr_Type)
153#define CFuncPtrTypeObject_Check(v) PyObject_TypeCheck(v, &CFuncPtrType_Type)
154#define StructTypeObject_Check(v) PyObject_TypeCheck(v, &StructType_Type)
155
156extern PyObject *
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000157CreateArrayType(PyObject *itemtype, Py_ssize_t length);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000158
159extern void init_callbacks_in_module(PyObject *m);
160
Thomas Hellerd4c93202006-03-08 19:35:11 +0000161extern PyMethodDef module_methods[];
162
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000163extern ffi_info *AllocFunctionCallback(PyObject *callable,
164 PyObject *converters,
165 PyObject *restype,
166 int stdcall);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000167/* a table entry describing a predefined ctypes type */
168struct fielddesc {
169 char code;
170 SETFUNC setfunc;
171 GETFUNC getfunc;
172 ffi_type *pffi_type; /* always statically allocated */
173 SETFUNC setfunc_swapped;
174 GETFUNC getfunc_swapped;
175};
176
177typedef struct {
178 PyObject_HEAD
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000179 Py_ssize_t offset;
180 Py_ssize_t size;
181 Py_ssize_t index; /* Index into CDataObject's
Thomas Hellerd4c93202006-03-08 19:35:11 +0000182 object array */
183 PyObject *proto; /* a type or NULL */
184 GETFUNC getfunc; /* getter function if proto is NULL */
185 SETFUNC setfunc; /* setter function if proto is NULL */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000186 int anonymous;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000187} CFieldObject;
188
189/* A subclass of PyDictObject, used as the instance dictionary of ctypes
190 metatypes */
191typedef struct {
192 PyDictObject dict; /* first part identical to PyDictObject */
193/* The size and align fields are unneeded, they are in ffi_type as well. As
194 an experiment shows, it's trivial to get rid of them, the only thing to
195 remember is that in ArrayType_new the ffi_type fields must be filled in -
196 so far it was unneeded because libffi doesn't support arrays at all
197 (because they are passed as pointers to function calls anyway). But it's
198 too much risk to change that now, and there are other fields which doen't
199 belong into this structure anyway. Maybe in ctypes 2.0... (ctypes 2000?)
200*/
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000201 Py_ssize_t size; /* number of bytes */
202 Py_ssize_t align; /* alignment requirements */
203 Py_ssize_t length; /* number of fields */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000204 ffi_type ffi_type_pointer;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000205 PyObject *proto; /* Only for Pointer/ArrayObject */
206 SETFUNC setfunc; /* Only for simple objects */
207 GETFUNC getfunc; /* Only for simple objects */
208
209 /* Following fields only used by CFuncPtrType_Type instances */
210 PyObject *argtypes; /* tuple of CDataObjects */
211 PyObject *converters; /* tuple([t.from_param for t in argtypes]) */
212 PyObject *restype; /* CDataObject or NULL */
213 PyObject *checker;
214 int flags; /* calling convention and such */
215} StgDictObject;
216
217/****************************************************************
218 StgDictObject fields
219
220 setfunc and getfunc is only set for simple data types, it is copied from the
221 corresponding fielddesc entry. These are functions to set and get the value
222 in a memory block.
223 They should probably by used by other types as well.
224
225 proto is only used for Pointer and Array types - it points to the item type
226 object.
227
228 Probably all the magic ctypes methods (like from_param) should have C
229 callable wrappers in the StgDictObject. For simple data type, for example,
230 the fielddesc table could have entries for C codec from_param functions or
231 other methods as well, if a subtype overrides this method in Python at
232 construction time, or assigns to it later, tp_setattro should update the
233 StgDictObject function to a generic one.
234
235 Currently, CFuncPtr types have 'converters' and 'checker' entries in their
236 type dict. They are only used to cache attributes from other entries, whihc
237 is wrong.
238
239 One use case is the .value attribute that all simple types have. But some
240 complex structures, like VARIANT, represent a single value also, and should
241 have this attribute.
242
243 Another use case is a _check_retval_ function, which is called when a ctypes
244 type is used as return type of a function to validate and compute the return
245 value.
246
247 Common ctypes protocol:
248
249 - setfunc: store a python value in a memory block
250 - getfunc: convert data from a memory block into a python value
251
252 - checkfunc: validate and convert a return value from a function call
253 - toparamfunc: convert a python value into a function argument
254
255*****************************************************************/
256
257/* May return NULL, but does not set an exception! */
258extern StgDictObject *PyType_stgdict(PyObject *obj);
259
260/* May return NULL, but does not set an exception! */
261extern StgDictObject *PyObject_stgdict(PyObject *self);
262
263extern int StgDict_clone(StgDictObject *src, StgDictObject *dst);
264
265typedef int(* PPROC)(void);
266
267PyObject *_CallProc(PPROC pProc,
268 PyObject *arguments,
269#ifdef MS_WIN32
270 IUnknown *pIUnk,
271 GUID *iid,
272#endif
273 int flags,
274 PyObject *argtypes,
275 PyObject *restype,
276 PyObject *checker);
277
278
279#define FUNCFLAG_STDCALL 0x0
280#define FUNCFLAG_CDECL 0x1
281#define FUNCFLAG_HRESULT 0x2
282#define FUNCFLAG_PYTHONAPI 0x4
283
284#define DICTFLAG_FINAL 0x1000
285
286typedef struct {
287 PyObject_HEAD
288 ffi_type *pffi_type;
289 char tag;
290 union {
291 char c;
292 char b;
293 short h;
294 int i;
295 long l;
296#ifdef HAVE_LONG_LONG
297 PY_LONG_LONG q;
298#endif
299 double d;
300 float f;
301 void *p;
302 } value;
303 PyObject *obj;
304 int size; /* for the 'V' tag */
305} PyCArgObject;
306
307extern PyTypeObject PyCArg_Type;
308extern PyCArgObject *new_CArgObject(void);
309#define PyCArg_CheckExact(v) ((v)->ob_type == &PyCArg_Type)
310extern PyCArgObject *new_CArgObject(void);
311
312extern PyObject *
313CData_get(PyObject *type, GETFUNC getfunc, PyObject *src,
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000314 Py_ssize_t index, Py_ssize_t size, char *ptr);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000315
316extern int
317CData_set(PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
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 void Extend_Error_Info(PyObject *exc_class, char *fmt, ...);
321
322struct basespec {
323 CDataObject *base;
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000324 Py_ssize_t index;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000325 char *adr;
326};
327
328extern char basespec_string[];
329
330extern ffi_type *GetType(PyObject *obj);
331
332/* exception classes */
333extern PyObject *PyExc_ArgError;
334
335extern char *conversion_mode_encoding;
336extern char *conversion_mode_errors;
337
338/* Python 2.4 macros, which are not available in Python 2.3 */
339
340#ifndef Py_CLEAR
341#define Py_CLEAR(op) \
342 do { \
343 if (op) { \
344 PyObject *tmp = (PyObject *)(op); \
345 (op) = NULL; \
346 Py_DECREF(tmp); \
347 } \
348 } while (0)
349#endif
350
351#ifndef Py_VISIT
352/* Utility macro to help write tp_traverse functions.
353 * To use this macro, the tp_traverse function must name its arguments
354 * "visit" and "arg". This is intended to keep tp_traverse functions
355 * looking as much alike as possible.
356 */
357#define Py_VISIT(op) \
358 do { \
359 if (op) { \
360 int vret = visit((op), arg); \
361 if (vret) \
362 return vret; \
363 } \
364 } while (0)
365#endif
366
367/* Python's PyUnicode_*WideChar functions are broken ... */
368#if defined(Py_USING_UNICODE) && defined(HAVE_WCHAR_H)
369# define CTYPES_UNICODE
370#endif
371
372
373#ifdef CTYPES_UNICODE
374# undef PyUnicode_FromWideChar
375# define PyUnicode_FromWideChar My_PyUnicode_FromWideChar
376
377# undef PyUnicode_AsWideChar
378# define PyUnicode_AsWideChar My_PyUnicode_AsWideChar
379
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000380extern PyObject *My_PyUnicode_FromWideChar(const wchar_t *, Py_ssize_t);
381extern int My_PyUnicode_AsWideChar(PyUnicodeObject *, wchar_t *, Py_ssize_t);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000382
383#endif
384
385extern void FreeClosure(void *);
386extern void *MallocClosure(void);
387
388extern void _AddTraceback(char *, char *, int);
389
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000390extern PyObject *CData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t index, char *adr);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000391
392/* XXX better name needed! */
393extern int IsSimpleSubType(PyObject *obj);
394
395
396#ifdef MS_WIN32
397extern PyObject *ComError;
398#endif
399
400/*
401 Local Variables:
402 compile-command: "python setup.py -q build install --home ~"
403 End:
404*/