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