blob: 179dcf16166b5ab03b0d148696d30a8cc2085403 [file] [log] [blame]
Thomas Hellerd4c93202006-03-08 19:35:11 +00001/******************************************************************/
2
Thomas Woutersa9773292006-04-21 09:43:23 +00003#if (PY_VERSION_HEX < 0x02050000)
4typedef int Py_ssize_t;
5#define lenfunc inquiry
6#define readbufferproc getreadbufferproc
7#define writebufferproc getwritebufferproc
8#define segcountproc getsegcountproc
9#define charbufferproc getcharbufferproc
10#define ssizeargfunc intargfunc
11#define ssizessizeargfunc intintargfunc
12#define ssizeobjargproc intobjargproc
13#define ssizessizeobjargproc intintobjargproc
14#endif
15
Thomas Hellerd4c93202006-03-08 19:35:11 +000016#ifndef MS_WIN32
17#define max(a, b) ((a) > (b) ? (a) : (b))
18#define min(a, b) ((a) < (b) ? (a) : (b))
19
20#define PARAMFLAG_FIN 0x1
21#define PARAMFLAG_FOUT 0x2
22#define PARAMFLAG_FLCID 0x4
23#endif
24
25/*
26 Backwards compatibility:
27 Python2.2 used LONG_LONG instead of PY_LONG_LONG
28*/
29#if defined(HAVE_LONG_LONG) && !defined(PY_LONG_LONG)
30#define PY_LONG_LONG LONG_LONG
31#endif
32
33typedef int (*THUNK)(void);
34typedef struct tagCDataObject CDataObject;
35
36/* A default buffer in CDataObject, which can be used for small C types. If
37this buffer is too small, PyMem_Malloc will be called to create a larger one,
38and this one is not used.
39
40Making CDataObject a variable size object would be a better solution, but more
41difficult in the presence of CFuncPtrObject. Maybe later.
42*/
43union value {
44 char c[16];
45 short s;
46 int i;
47 long l;
48 float f;
49 double d;
50#ifdef HAVE_LONG_LONG
51 PY_LONG_LONG ll;
52#endif
53};
54
55/*
56 Hm. Are there CDataObject's which do not need the b_objects member? In
57 this case we probably should introduce b_flags to mark it as present... If
58 b_objects is not present/unused b_length is unneeded as well.
59*/
60
61struct tagCDataObject {
62 PyObject_HEAD
63 char *b_ptr; /* pointer to memory block */
64 int b_needsfree; /* need _we_ free the memory? */
65 CDataObject *b_base; /* pointer to base object or NULL */
Thomas Hellerfe8f8622006-03-14 19:53:09 +000066 Py_ssize_t b_size; /* size of memory block in bytes */
67 Py_ssize_t b_length; /* number of references we need */
68 Py_ssize_t b_index; /* index of this object into base's
Thomas Hellerd4c93202006-03-08 19:35:11 +000069 b_object list */
70 PyObject *b_objects; /* list of references we need to keep */
71 union value b_value;
72};
73
74typedef struct {
75 /* First part identical to tagCDataObject */
76 PyObject_HEAD
77 char *b_ptr; /* pointer to memory block */
78 int b_needsfree; /* need _we_ free the memory? */
79 CDataObject *b_base; /* pointer to base object or NULL */
Thomas Hellerfe8f8622006-03-14 19:53:09 +000080 Py_ssize_t b_size; /* size of memory block in bytes */
81 Py_ssize_t b_length; /* number of references we need */
82 Py_ssize_t b_index; /* index of this object into base's
Thomas Hellerd4c93202006-03-08 19:35:11 +000083 b_object list */
84 PyObject *b_objects; /* list of references we need to keep */
85 union value b_value;
86 /* end of tagCDataObject, additional fields follow */
87
88 THUNK thunk;
89 PyObject *callable;
90
91 /* These two fields will override the ones in the type's stgdict if
92 they are set */
93 PyObject *converters;
94 PyObject *argtypes;
95 PyObject *restype;
96 PyObject *checker;
97 PyObject *errcheck;
98#ifdef MS_WIN32
99 int index;
100 GUID *iid;
101#endif
102 PyObject *paramflags;
103} CFuncPtrObject;
104
105extern PyTypeObject StgDict_Type;
106#define StgDict_CheckExact(v) ((v)->ob_type == &StgDict_Type)
107#define StgDict_Check(v) PyObject_TypeCheck(v, &StgDict_Type)
108
109extern int StructUnionType_update_stgdict(PyObject *fields, PyObject *type, int isStruct);
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000110extern int PyType_stginfo(PyTypeObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
111extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000112
113
114
115extern PyTypeObject CData_Type;
116#define CDataObject_CheckExact(v) ((v)->ob_type == &CData_Type)
117#define CDataObject_Check(v) PyObject_TypeCheck(v, &CData_Type)
118
119extern PyTypeObject SimpleType_Type;
120#define SimpleTypeObject_CheckExact(v) ((v)->ob_type == &SimpleType_Type)
121#define SimpleTypeObject_Check(v) PyObject_TypeCheck(v, &SimpleType_Type)
122
123extern PyTypeObject CField_Type;
124extern struct fielddesc *getentry(char *fmt);
125
126
127extern PyObject *
128CField_FromDesc(PyObject *desc, int index,
129 int *pfield_size, int bitsize, int *pbitofs,
130 int *psize, int *poffset, int *palign,
131 int pack, int is_big_endian);
132
133extern PyObject *CData_AtAddress(PyObject *type, void *buf);
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000134extern PyObject *CData_FromBytes(PyObject *type, char *data, Py_ssize_t length);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000135
136extern PyTypeObject ArrayType_Type;
137extern PyTypeObject Array_Type;
138extern PyTypeObject PointerType_Type;
139extern PyTypeObject Pointer_Type;
140extern PyTypeObject CFuncPtr_Type;
141extern PyTypeObject CFuncPtrType_Type;
142extern PyTypeObject StructType_Type;
143
144#define ArrayTypeObject_Check(v) PyObject_TypeCheck(v, &ArrayType_Type)
145#define ArrayObject_Check(v) PyObject_TypeCheck(v, &Array_Type)
146#define PointerObject_Check(v) PyObject_TypeCheck(v, &Pointer_Type)
147#define PointerTypeObject_Check(v) PyObject_TypeCheck(v, &PointerType_Type)
148#define CFuncPtrObject_Check(v) PyObject_TypeCheck(v, &CFuncPtr_Type)
149#define CFuncPtrTypeObject_Check(v) PyObject_TypeCheck(v, &CFuncPtrType_Type)
150#define StructTypeObject_Check(v) PyObject_TypeCheck(v, &StructType_Type)
151
152extern PyObject *
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000153CreateArrayType(PyObject *itemtype, Py_ssize_t length);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000154
155extern void init_callbacks_in_module(PyObject *m);
156
157extern THUNK AllocFunctionCallback(PyObject *callable,
158 PyObject *converters,
159 PyObject *restype,
160 int stdcall);
161extern void FreeCallback(THUNK);
162
163extern PyMethodDef module_methods[];
164
165typedef PyObject *(* GETFUNC)(void *, unsigned size);
166typedef PyObject *(* SETFUNC)(void *, PyObject *value, unsigned size);
167
168/* a table entry describing a predefined ctypes type */
169struct fielddesc {
170 char code;
171 SETFUNC setfunc;
172 GETFUNC getfunc;
173 ffi_type *pffi_type; /* always statically allocated */
174 SETFUNC setfunc_swapped;
175 GETFUNC getfunc_swapped;
176};
177
178typedef struct {
179 PyObject_HEAD
Thomas Hellerfe8f8622006-03-14 19:53:09 +0000180 Py_ssize_t offset;
181 Py_ssize_t size;
182 Py_ssize_t index; /* Index into CDataObject's
Thomas Hellerd4c93202006-03-08 19:35:11 +0000183 object array */
184 PyObject *proto; /* a type or NULL */
185 GETFUNC getfunc; /* getter function if proto is NULL */
186 SETFUNC setfunc; /* setter function if proto is NULL */
187} 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 Hellerd4c93202006-03-08 19:35:11 +0000204 ffi_type ffi_type;
205 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
380extern PyObject *My_PyUnicode_FromWideChar(const wchar_t *, int);
381extern int My_PyUnicode_AsWideChar(PyUnicodeObject *, wchar_t *, int);
382
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*/