1 /*****************************************************************
2 This file should be kept compatible with Python 2.3, see PEP 291.
3 *****************************************************************/
5 #if defined (__SVR4) && defined (__sun)
9 #if (PY_VERSION_HEX < 0x02040000)
10 #define PyDict_CheckExact(ob) (Py_TYPE(ob) == &PyDict_Type)
13 #if (PY_VERSION_HEX < 0x02050000)
14 typedef int Py_ssize_t
;
15 #define PyInt_FromSsize_t PyInt_FromLong
16 #define PyNumber_AsSsize_t(ob, exc) PyInt_AsLong(ob)
17 #define PyIndex_Check(ob) PyInt_Check(ob)
18 typedef Py_ssize_t (*readbufferproc
)(PyObject
*, Py_ssize_t
, void **);
19 typedef Py_ssize_t (*writebufferproc
)(PyObject
*, Py_ssize_t
, void **);
20 typedef Py_ssize_t (*segcountproc
)(PyObject
*, Py_ssize_t
*);
21 typedef Py_ssize_t (*charbufferproc
)(PyObject
*, Py_ssize_t
, char **);
24 #if (PY_VERSION_HEX < 0x02060000)
25 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
26 #define PyVarObject_HEAD_INIT(type, size) \
27 PyObject_HEAD_INIT(type) size,
28 #define PyImport_ImportModuleNoBlock PyImport_ImportModule
29 #define PyLong_FromSsize_t PyInt_FromLong
30 #define Py_TPFLAGS_HAVE_NEWBUFFER 0
35 #define max(a, b) ((a) > (b) ? (a) : (b))
36 #define min(a, b) ((a) < (b) ? (a) : (b))
38 #define PARAMFLAG_FIN 0x1
39 #define PARAMFLAG_FOUT 0x2
40 #define PARAMFLAG_FLCID 0x4
44 Backwards compatibility:
45 Python2.2 used LONG_LONG instead of PY_LONG_LONG
47 #if defined(HAVE_LONG_LONG) && !defined(PY_LONG_LONG)
48 #define PY_LONG_LONG LONG_LONG
51 typedef struct tagPyCArgObject PyCArgObject
;
52 typedef struct tagCDataObject CDataObject
;
53 typedef PyObject
*(* GETFUNC
)(void *, Py_ssize_t size
);
54 typedef PyObject
*(* SETFUNC
)(void *, PyObject
*value
, Py_ssize_t size
);
55 typedef PyCArgObject
*(* PARAMFUNC
)(CDataObject
*obj
);
57 /* A default buffer in CDataObject, which can be used for small C types. If
58 this buffer is too small, PyMem_Malloc will be called to create a larger one,
59 and this one is not used.
61 Making CDataObject a variable size object would be a better solution, but more
62 difficult in the presence of CFuncPtrObject. Maybe later.
78 Hm. Are there CDataObject's which do not need the b_objects member? In
79 this case we probably should introduce b_flags to mark it as present... If
80 b_objects is not present/unused b_length is unneeded as well.
83 struct tagCDataObject
{
85 char *b_ptr
; /* pointer to memory block */
86 int b_needsfree
; /* need _we_ free the memory? */
87 CDataObject
*b_base
; /* pointer to base object or NULL */
88 Py_ssize_t b_size
; /* size of memory block in bytes */
89 Py_ssize_t b_length
; /* number of references we need */
90 Py_ssize_t b_index
; /* index of this object into base's
92 PyObject
*b_objects
; /* dictionary of references we need to keep, or Py_None */
98 ffi_closure
*pcl
; /* the C callable */
101 PyObject
*converters
;
105 ffi_type
*ffi_restype
;
108 extern PyTypeObject CThunk_Type
;
109 #define CThunk_CheckExact(v) ((v)->ob_type == &CThunk_Type)
112 /* First part identical to tagCDataObject */
114 char *b_ptr
; /* pointer to memory block */
115 int b_needsfree
; /* need _we_ free the memory? */
116 CDataObject
*b_base
; /* pointer to base object or NULL */
117 Py_ssize_t b_size
; /* size of memory block in bytes */
118 Py_ssize_t b_length
; /* number of references we need */
119 Py_ssize_t b_index
; /* index of this object into base's
121 PyObject
*b_objects
; /* list of references we need to keep */
123 /* end of tagCDataObject, additional fields follow */
128 /* These two fields will override the ones in the type's stgdict if
130 PyObject
*converters
;
139 PyObject
*paramflags
;
142 extern PyTypeObject StgDict_Type
;
143 #define StgDict_CheckExact(v) ((v)->ob_type == &StgDict_Type)
144 #define StgDict_Check(v) PyObject_TypeCheck(v, &StgDict_Type)
146 extern int StructUnionType_update_stgdict(PyObject
*fields
, PyObject
*type
, int isStruct
);
147 extern int PyType_stginfo(PyTypeObject
*self
, Py_ssize_t
*psize
, Py_ssize_t
*palign
, Py_ssize_t
*plength
);
148 extern int PyObject_stginfo(PyObject
*self
, Py_ssize_t
*psize
, Py_ssize_t
*palign
, Py_ssize_t
*plength
);
152 extern PyTypeObject CData_Type
;
153 #define CDataObject_CheckExact(v) ((v)->ob_type == &CData_Type)
154 #define CDataObject_Check(v) PyObject_TypeCheck(v, &CData_Type)
156 extern PyTypeObject SimpleType_Type
;
157 #define SimpleTypeObject_CheckExact(v) ((v)->ob_type == &SimpleType_Type)
158 #define SimpleTypeObject_Check(v) PyObject_TypeCheck(v, &SimpleType_Type)
160 extern PyTypeObject CField_Type
;
161 extern struct fielddesc
*getentry(char *fmt
);
165 CField_FromDesc(PyObject
*desc
, Py_ssize_t index
,
166 Py_ssize_t
*pfield_size
, int bitsize
, int *pbitofs
,
167 Py_ssize_t
*psize
, Py_ssize_t
*poffset
, Py_ssize_t
*palign
,
168 int pack
, int is_big_endian
);
170 extern PyObject
*CData_AtAddress(PyObject
*type
, void *buf
);
171 extern PyObject
*CData_FromBytes(PyObject
*type
, char *data
, Py_ssize_t length
);
173 extern PyTypeObject ArrayType_Type
;
174 extern PyTypeObject Array_Type
;
175 extern PyTypeObject PointerType_Type
;
176 extern PyTypeObject Pointer_Type
;
177 extern PyTypeObject CFuncPtr_Type
;
178 extern PyTypeObject CFuncPtrType_Type
;
179 extern PyTypeObject StructType_Type
;
181 #define ArrayTypeObject_Check(v) PyObject_TypeCheck(v, &ArrayType_Type)
182 #define ArrayObject_Check(v) PyObject_TypeCheck(v, &Array_Type)
183 #define PointerObject_Check(v) PyObject_TypeCheck(v, &Pointer_Type)
184 #define PointerTypeObject_Check(v) PyObject_TypeCheck(v, &PointerType_Type)
185 #define CFuncPtrObject_Check(v) PyObject_TypeCheck(v, &CFuncPtr_Type)
186 #define CFuncPtrTypeObject_Check(v) PyObject_TypeCheck(v, &CFuncPtrType_Type)
187 #define StructTypeObject_Check(v) PyObject_TypeCheck(v, &StructType_Type)
190 CreateArrayType(PyObject
*itemtype
, Py_ssize_t length
);
192 extern PyMethodDef module_methods
[];
194 extern CThunkObject
*AllocFunctionCallback(PyObject
*callable
,
195 PyObject
*converters
,
198 /* a table entry describing a predefined ctypes type */
203 ffi_type
*pffi_type
; /* always statically allocated */
204 SETFUNC setfunc_swapped
;
205 GETFUNC getfunc_swapped
;
212 Py_ssize_t index
; /* Index into CDataObject's
214 PyObject
*proto
; /* a type or NULL */
215 GETFUNC getfunc
; /* getter function if proto is NULL */
216 SETFUNC setfunc
; /* setter function if proto is NULL */
220 /* A subclass of PyDictObject, used as the instance dictionary of ctypes
223 PyDictObject dict
; /* first part identical to PyDictObject */
224 /* The size and align fields are unneeded, they are in ffi_type as well. As
225 an experiment shows, it's trivial to get rid of them, the only thing to
226 remember is that in ArrayType_new the ffi_type fields must be filled in -
227 so far it was unneeded because libffi doesn't support arrays at all
228 (because they are passed as pointers to function calls anyway). But it's
229 too much risk to change that now, and there are other fields which doen't
230 belong into this structure anyway. Maybe in ctypes 2.0... (ctypes 2000?)
232 Py_ssize_t size
; /* number of bytes */
233 Py_ssize_t align
; /* alignment requirements */
234 Py_ssize_t length
; /* number of fields */
235 ffi_type ffi_type_pointer
;
236 PyObject
*proto
; /* Only for Pointer/ArrayObject */
237 SETFUNC setfunc
; /* Only for simple objects */
238 GETFUNC getfunc
; /* Only for simple objects */
241 /* Following fields only used by CFuncPtrType_Type instances */
242 PyObject
*argtypes
; /* tuple of CDataObjects */
243 PyObject
*converters
; /* tuple([t.from_param for t in argtypes]) */
244 PyObject
*restype
; /* CDataObject or NULL */
246 int flags
; /* calling convention and such */
248 /* pep3118 fields, pointers neeed PyMem_Free */
252 /* Py_ssize_t *strides; */ /* unused in ctypes */
253 /* Py_ssize_t *suboffsets; */ /* unused in ctypes */
257 /****************************************************************
260 setfunc and getfunc is only set for simple data types, it is copied from the
261 corresponding fielddesc entry. These are functions to set and get the value
263 They should probably by used by other types as well.
265 proto is only used for Pointer and Array types - it points to the item type
268 Probably all the magic ctypes methods (like from_param) should have C
269 callable wrappers in the StgDictObject. For simple data type, for example,
270 the fielddesc table could have entries for C codec from_param functions or
271 other methods as well, if a subtype overrides this method in Python at
272 construction time, or assigns to it later, tp_setattro should update the
273 StgDictObject function to a generic one.
275 Currently, CFuncPtr types have 'converters' and 'checker' entries in their
276 type dict. They are only used to cache attributes from other entries, whihc
279 One use case is the .value attribute that all simple types have. But some
280 complex structures, like VARIANT, represent a single value also, and should
283 Another use case is a _check_retval_ function, which is called when a ctypes
284 type is used as return type of a function to validate and compute the return
287 Common ctypes protocol:
289 - setfunc: store a python value in a memory block
290 - getfunc: convert data from a memory block into a python value
292 - checkfunc: validate and convert a return value from a function call
293 - toparamfunc: convert a python value into a function argument
295 *****************************************************************/
297 /* May return NULL, but does not set an exception! */
298 extern StgDictObject
*PyType_stgdict(PyObject
*obj
);
300 /* May return NULL, but does not set an exception! */
301 extern StgDictObject
*PyObject_stgdict(PyObject
*self
);
303 extern int StgDict_clone(StgDictObject
*src
, StgDictObject
*dst
);
305 typedef int(* PPROC
)(void);
307 PyObject
*_CallProc(PPROC pProc
,
319 #define FUNCFLAG_STDCALL 0x0
320 #define FUNCFLAG_CDECL 0x1
321 #define FUNCFLAG_HRESULT 0x2
322 #define FUNCFLAG_PYTHONAPI 0x4
323 #define FUNCFLAG_USE_ERRNO 0x8
324 #define FUNCFLAG_USE_LASTERROR 0x10
326 #define TYPEFLAG_ISPOINTER 0x100
327 #define TYPEFLAG_HASPOINTER 0x200
329 #define DICTFLAG_FINAL 0x1000
331 struct tagPyCArgObject
{
341 #ifdef HAVE_LONG_LONG
350 Py_ssize_t size
; /* for the 'V' tag */
353 extern PyTypeObject PyCArg_Type
;
354 #define PyCArg_CheckExact(v) ((v)->ob_type == &PyCArg_Type)
355 extern PyCArgObject
*new_CArgObject(void);
358 CData_get(PyObject
*type
, GETFUNC getfunc
, PyObject
*src
,
359 Py_ssize_t index
, Py_ssize_t size
, char *ptr
);
362 CData_set(PyObject
*dst
, PyObject
*type
, SETFUNC setfunc
, PyObject
*value
,
363 Py_ssize_t index
, Py_ssize_t size
, char *ptr
);
365 extern void Extend_Error_Info(PyObject
*exc_class
, char *fmt
, ...);
373 extern char basespec_string
[];
375 extern ffi_type
*GetType(PyObject
*obj
);
377 /* exception classes */
378 extern PyObject
*PyExc_ArgError
;
380 extern char *conversion_mode_encoding
;
381 extern char *conversion_mode_errors
;
383 /* Python 2.4 macros, which are not available in Python 2.3 */
386 #define Py_CLEAR(op) \
389 PyObject *tmp = (PyObject *)(op); \
397 /* Utility macro to help write tp_traverse functions.
398 * To use this macro, the tp_traverse function must name its arguments
399 * "visit" and "arg". This is intended to keep tp_traverse functions
400 * looking as much alike as possible.
402 #define Py_VISIT(op) \
405 int vret = visit((op), arg); \
412 /* Python's PyUnicode_*WideChar functions are broken ... */
413 #if defined(Py_USING_UNICODE) && defined(HAVE_WCHAR_H)
414 # define CTYPES_UNICODE
418 #ifdef CTYPES_UNICODE
419 # undef PyUnicode_FromWideChar
420 # define PyUnicode_FromWideChar My_PyUnicode_FromWideChar
422 # undef PyUnicode_AsWideChar
423 # define PyUnicode_AsWideChar My_PyUnicode_AsWideChar
425 extern PyObject
*My_PyUnicode_FromWideChar(const wchar_t *, Py_ssize_t
);
426 extern Py_ssize_t
My_PyUnicode_AsWideChar(PyUnicodeObject
*, wchar_t *, Py_ssize_t
);
430 extern void FreeClosure(void *);
431 extern void *MallocClosure(void);
433 extern void _AddTraceback(char *, char *, int);
435 extern PyObject
*CData_FromBaseObj(PyObject
*type
, PyObject
*base
, Py_ssize_t index
, char *adr
);
436 extern char *alloc_format_string(const char *prefix
, const char *suffix
);
438 /* XXX better name needed! */
439 extern int IsSimpleSubType(PyObject
*obj
);
441 extern PyObject
*_pointer_type_cache
;
442 PyObject
*get_error_object(int **pspace
);
445 extern PyObject
*ComError
;
450 compile-command: "python setup.py -q build install --home ~"