ctypes.h 14.1 KB
Newer Older
1 2 3
/*****************************************************************
  This file should be kept compatible with Python 2.3, see PEP 291.
 *****************************************************************/
4

5 6 7 8
#if defined (__SVR4) && defined (__sun)
#   include <alloca.h>
#endif

9 10 11 12
#if (PY_VERSION_HEX < 0x02040000)
#define PyDict_CheckExact(ob) (Py_TYPE(ob) == &PyDict_Type)
#endif

13 14
#if (PY_VERSION_HEX < 0x02050000)
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 19 20 21
typedef Py_ssize_t (*readbufferproc)(PyObject *, Py_ssize_t, void **);
typedef Py_ssize_t (*writebufferproc)(PyObject *, Py_ssize_t, void **);
typedef Py_ssize_t (*segcountproc)(PyObject *, Py_ssize_t *);
typedef Py_ssize_t (*charbufferproc)(PyObject *, Py_ssize_t, char **);
22 23
#endif

24 25 26 27 28
#if (PY_VERSION_HEX < 0x02060000)
#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
#define PyVarObject_HEAD_INIT(type, size) \
	PyObject_HEAD_INIT(type) size,
#define PyImport_ImportModuleNoBlock PyImport_ImportModule
29 30
#define PyLong_FromSsize_t PyInt_FromLong
#define Py_TPFLAGS_HAVE_NEWBUFFER 0
31 32 33
#endif


34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#ifndef MS_WIN32
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))

#define PARAMFLAG_FIN 0x1
#define PARAMFLAG_FOUT 0x2
#define PARAMFLAG_FLCID 0x4
#endif

/*
  Backwards compatibility:
  Python2.2 used LONG_LONG instead of PY_LONG_LONG
*/
#if defined(HAVE_LONG_LONG) && !defined(PY_LONG_LONG)
#define PY_LONG_LONG LONG_LONG
#endif

51
typedef struct tagPyCArgObject PyCArgObject;
52
typedef struct tagCDataObject CDataObject;
53 54
typedef PyObject *(* GETFUNC)(void *, Py_ssize_t size);
typedef PyObject *(* SETFUNC)(void *, PyObject *value, Py_ssize_t size);
55
typedef PyCArgObject *(* PARAMFUNC)(CDataObject *obj);
56 57 58 59 60 61

/* A default buffer in CDataObject, which can be used for small C types.  If
this buffer is too small, PyMem_Malloc will be called to create a larger one,
and this one is not used.

Making CDataObject a variable size object would be a better solution, but more
62
difficult in the presence of PyCFuncPtrObject.  Maybe later.
63 64 65 66 67 68 69 70 71 72 73
*/
union value {
		char c[16];
		short s;
		int i;
		long l;
		float f;
		double d;
#ifdef HAVE_LONG_LONG
		PY_LONG_LONG ll;
#endif
74
		long double D;
75 76 77 78 79 80 81 82 83 84 85 86 87
};

/*
  Hm. Are there CDataObject's which do not need the b_objects member?  In
  this case we probably should introduce b_flags to mark it as present...  If
  b_objects is not present/unused b_length is unneeded as well.
*/

struct tagCDataObject {
	PyObject_HEAD
	char *b_ptr;		/* pointer to memory block */
	int  b_needsfree;	/* need _we_ free the memory? */
	CDataObject *b_base;	/* pointer to base object or NULL */
88 89 90
	Py_ssize_t b_size;	/* size of memory block in bytes */
	Py_ssize_t b_length;	/* number of references we need */
	Py_ssize_t b_index;	/* index of this object into base's
91
				   b_object list */
92
	PyObject *b_objects;	/* dictionary of references we need to keep, or Py_None */
93 94 95
	union value b_value;
};

96
typedef struct {
97
	PyObject_VAR_HEAD
98 99
	ffi_closure *pcl; /* the C callable */
	ffi_cif cif;
100
	int flags;
101 102
	PyObject *converters;
	PyObject *callable;
103
	PyObject *restype;
104
	SETFUNC setfunc;
105
	ffi_type *ffi_restype;
106
	ffi_type *atypes[1];
107
} CThunkObject;
108 109
extern PyTypeObject PyCThunk_Type;
#define CThunk_CheckExact(v)	    ((v)->ob_type == &PyCThunk_Type)
110

111 112 113 114 115 116
typedef struct {
	/* First part identical to tagCDataObject */
	PyObject_HEAD
	char *b_ptr;		/* pointer to memory block */
	int  b_needsfree;	/* need _we_ free the memory? */
	CDataObject *b_base;	/* pointer to base object or NULL */
117 118 119
	Py_ssize_t b_size;	/* size of memory block in bytes */
	Py_ssize_t b_length;	/* number of references we need */
	Py_ssize_t b_index;	/* index of this object into base's
120 121 122 123 124
				   b_object list */
	PyObject *b_objects;	/* list of references we need to keep */
	union value b_value;
	/* end of tagCDataObject, additional fields follow */

125
	CThunkObject *thunk;
126 127 128 129 130 131 132 133 134 135 136 137 138 139
	PyObject *callable;

	/* These two fields will override the ones in the type's stgdict if
	   they are set */
	PyObject *converters;
	PyObject *argtypes;
	PyObject *restype;
	PyObject *checker;
	PyObject *errcheck;
#ifdef MS_WIN32
	int index;
	GUID *iid;
#endif
	PyObject *paramflags;
140
} PyCFuncPtrObject;
141

142 143 144
extern PyTypeObject PyCStgDict_Type;
#define PyCStgDict_CheckExact(v)	    ((v)->ob_type == &PyCStgDict_Type)
#define PyCStgDict_Check(v)	    PyObject_TypeCheck(v, &PyCStgDict_Type)
145

146
extern int PyCStructUnionType_update_stgdict(PyObject *fields, PyObject *type, int isStruct);
147 148
extern int PyType_stginfo(PyTypeObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
149 150 151



152 153 154
extern PyTypeObject PyCData_Type;
#define CDataObject_CheckExact(v)	((v)->ob_type == &PyCData_Type)
#define CDataObject_Check(v)		PyObject_TypeCheck(v, &PyCData_Type)
155

156 157 158
extern PyTypeObject PyCSimpleType_Type;
#define PyCSimpleTypeObject_CheckExact(v)	((v)->ob_type == &PyCSimpleType_Type)
#define PyCSimpleTypeObject_Check(v)	PyObject_TypeCheck(v, &PyCSimpleType_Type)
159

160 161
extern PyTypeObject PyCField_Type;
extern struct fielddesc *_ctypes_get_fielddesc(char *fmt);
162 163 164


extern PyObject *
165
PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
166 167
		Py_ssize_t *pfield_size, int bitsize, int *pbitofs,
		Py_ssize_t *psize, Py_ssize_t *poffset, Py_ssize_t *palign,
168 169
		int pack, int is_big_endian);

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
extern PyObject *PyCData_AtAddress(PyObject *type, void *buf);
extern PyObject *PyCData_FromBytes(PyObject *type, char *data, Py_ssize_t length);

extern PyTypeObject PyCArrayType_Type;
extern PyTypeObject PyCArray_Type;
extern PyTypeObject PyCPointerType_Type;
extern PyTypeObject PyCPointer_Type;
extern PyTypeObject PyCFuncPtr_Type;
extern PyTypeObject PyCFuncPtrType_Type;
extern PyTypeObject PyCStructType_Type;

#define PyCArrayTypeObject_Check(v)	PyObject_TypeCheck(v, &PyCArrayType_Type)
#define ArrayObject_Check(v)		PyObject_TypeCheck(v, &PyCArray_Type)
#define PointerObject_Check(v)		PyObject_TypeCheck(v, &PyCPointer_Type)
#define PyCPointerTypeObject_Check(v)	PyObject_TypeCheck(v, &PyCPointerType_Type)
#define PyCFuncPtrObject_Check(v)		PyObject_TypeCheck(v, &PyCFuncPtr_Type)
#define PyCFuncPtrTypeObject_Check(v)	PyObject_TypeCheck(v, &PyCFuncPtrType_Type)
#define PyCStructTypeObject_Check(v)	PyObject_TypeCheck(v, &PyCStructType_Type)
188 189

extern PyObject *
190
PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length);
191

192
extern PyMethodDef _ctypes_module_methods[];
193

194
extern CThunkObject *_ctypes_alloc_callback(PyObject *callable,
195 196
					   PyObject *converters,
					   PyObject *restype,
197
					   int flags);
198 199 200 201 202 203 204 205 206 207 208 209
/* a table entry describing a predefined ctypes type */
struct fielddesc {
	char code;
	SETFUNC setfunc;
	GETFUNC getfunc;
	ffi_type *pffi_type; /* always statically allocated */
	SETFUNC setfunc_swapped;
	GETFUNC getfunc_swapped;
};

typedef struct {
	PyObject_HEAD
210 211 212
	Py_ssize_t offset;
	Py_ssize_t size;
	Py_ssize_t index;		/* Index into CDataObject's
213 214 215 216
					   object array */
	PyObject *proto;		/* a type or NULL */
	GETFUNC getfunc;		/* getter function if proto is NULL */
	SETFUNC setfunc;		/* setter function if proto is NULL */
217
	int anonymous;
218 219 220 221 222 223 224 225
} CFieldObject;

/* A subclass of PyDictObject, used as the instance dictionary of ctypes
   metatypes */
typedef struct {
	PyDictObject dict;	/* first part identical to PyDictObject */
/* The size and align fields are unneeded, they are in ffi_type as well.  As
   an experiment shows, it's trivial to get rid of them, the only thing to
226
   remember is that in PyCArrayType_new the ffi_type fields must be filled in -
227 228 229 230 231
   so far it was unneeded because libffi doesn't support arrays at all
   (because they are passed as pointers to function calls anyway).  But it's
   too much risk to change that now, and there are other fields which doen't
   belong into this structure anyway.  Maybe in ctypes 2.0... (ctypes 2000?)
*/
232 233 234
	Py_ssize_t size;	/* number of bytes */
	Py_ssize_t align;	/* alignment requirements */
	Py_ssize_t length;	/* number of fields */
235
	ffi_type ffi_type_pointer;
236 237 238
	PyObject *proto;	/* Only for Pointer/ArrayObject */
	SETFUNC setfunc;	/* Only for simple objects */
	GETFUNC getfunc;	/* Only for simple objects */
239
	PARAMFUNC paramfunc;
240

241
	/* Following fields only used by PyCFuncPtrType_Type instances */
242 243 244 245 246
	PyObject *argtypes;	/* tuple of CDataObjects */
	PyObject *converters;	/* tuple([t.from_param for t in argtypes]) */
	PyObject *restype;	/* CDataObject or NULL */
	PyObject *checker;
	int flags;		/* calling convention and such */
247 248 249 250 251 252 253 254

	/* pep3118 fields, pointers neeed PyMem_Free */
	char *format;
	int ndim;
	Py_ssize_t *shape;
/*	Py_ssize_t *strides;	*/ /* unused in ctypes */
/*	Py_ssize_t *suboffsets;	*/ /* unused in ctypes */

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
} StgDictObject;

/****************************************************************
 StgDictObject fields

 setfunc and getfunc is only set for simple data types, it is copied from the
 corresponding fielddesc entry.  These are functions to set and get the value
 in a memory block.
 They should probably by used by other types as well.

 proto is only used for Pointer and Array types - it points to the item type
 object.

 Probably all the magic ctypes methods (like from_param) should have C
 callable wrappers in the StgDictObject.  For simple data type, for example,
 the fielddesc table could have entries for C codec from_param functions or
 other methods as well, if a subtype overrides this method in Python at
 construction time, or assigns to it later, tp_setattro should update the
 StgDictObject function to a generic one.

275
 Currently, PyCFuncPtr types have 'converters' and 'checker' entries in their
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
 type dict.  They are only used to cache attributes from other entries, whihc
 is wrong.

 One use case is the .value attribute that all simple types have.  But some
 complex structures, like VARIANT, represent a single value also, and should
 have this attribute.

 Another use case is a _check_retval_ function, which is called when a ctypes
 type is used as return type of a function to validate and compute the return
 value.

 Common ctypes protocol:

  - setfunc: store a python value in a memory block
  - getfunc: convert data from a memory block into a python value

  - checkfunc: validate and convert a return value from a function call
  - toparamfunc: convert a python value into a function argument

*****************************************************************/

/* May return NULL, but does not set an exception! */
extern StgDictObject *PyType_stgdict(PyObject *obj);

/* May return NULL, but does not set an exception! */
extern StgDictObject *PyObject_stgdict(PyObject *self);

303
extern int PyCStgDict_clone(StgDictObject *src, StgDictObject *dst);
304 305 306

typedef int(* PPROC)(void);

307
PyObject *_ctypes_callproc(PPROC pProc,
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
		    PyObject *arguments,
#ifdef MS_WIN32
		    IUnknown *pIUnk,
		    GUID *iid,
#endif
		    int flags,
		    PyObject *argtypes,
		    PyObject *restype,
		    PyObject *checker);
 

#define FUNCFLAG_STDCALL 0x0
#define FUNCFLAG_CDECL   0x1
#define FUNCFLAG_HRESULT 0x2
#define FUNCFLAG_PYTHONAPI 0x4
323 324
#define FUNCFLAG_USE_ERRNO 0x8
#define FUNCFLAG_USE_LASTERROR 0x10
325

326 327 328
#define TYPEFLAG_ISPOINTER 0x100
#define TYPEFLAG_HASPOINTER 0x200

329 330
#define DICTFLAG_FINAL 0x1000

331
struct tagPyCArgObject {
332 333 334 335 336 337 338 339 340 341 342 343
	PyObject_HEAD
	ffi_type *pffi_type;
	char tag;
	union {
		char c;
		char b;
		short h;
		int i;
		long l;
#ifdef HAVE_LONG_LONG
		PY_LONG_LONG q;
#endif
344
		long double D;
345 346 347 348 349
		double d;
		float f;
		void *p;
	} value;
	PyObject *obj;
350
	Py_ssize_t size; /* for the 'V' tag */
351
};
352 353 354

extern PyTypeObject PyCArg_Type;
#define PyCArg_CheckExact(v)	    ((v)->ob_type == &PyCArg_Type)
355
extern PyCArgObject *PyCArgObject_new(void);
356 357

extern PyObject *
358
PyCData_get(PyObject *type, GETFUNC getfunc, PyObject *src,
359
	  Py_ssize_t index, Py_ssize_t size, char *ptr);
360 361

extern int
362
PyCData_set(PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
363
	  Py_ssize_t index, Py_ssize_t size, char *ptr);
364

365
extern void _ctypes_extend_error(PyObject *exc_class, char *fmt, ...);
366 367 368

struct basespec {
	CDataObject *base;
369
	Py_ssize_t index;
370 371 372 373 374
	char *adr;
};

extern char basespec_string[];

375
extern ffi_type *_ctypes_get_ffi_type(PyObject *obj);
376 377 378 379

/* exception classes */
extern PyObject *PyExc_ArgError;

380 381
extern char *_ctypes_conversion_encoding;
extern char *_ctypes_conversion_errors;
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

/* Python 2.4 macros, which are not available in Python 2.3 */

#ifndef Py_CLEAR
#define Py_CLEAR(op)				\
        do {                            	\
                if (op) {			\
                        PyObject *tmp = (PyObject *)(op);	\
                        (op) = NULL;		\
                        Py_DECREF(tmp);		\
                }				\
        } while (0)
#endif

#ifndef Py_VISIT
/* Utility macro to help write tp_traverse functions.
 * To use this macro, the tp_traverse function must name its arguments
 * "visit" and "arg".  This is intended to keep tp_traverse functions
 * looking as much alike as possible.
 */
#define Py_VISIT(op)					\
        do { 						\
                if (op) {				\
                        int vret = visit((op), arg);	\
                        if (vret)			\
                                return vret;		\
                }					\
        } while (0)
#endif

/* Python's PyUnicode_*WideChar functions are broken ... */
#if defined(Py_USING_UNICODE) && defined(HAVE_WCHAR_H)
#  define CTYPES_UNICODE
#endif


418
#if (PY_VERSION_HEX < 0x02040000)
419 420
#ifdef CTYPES_UNICODE
#  undef PyUnicode_FromWideChar
421
#  define PyUnicode_FromWideChar PyUnicode_FromWideChar_fixed
422 423

#  undef PyUnicode_AsWideChar
424
#  define PyUnicode_AsWideChar PyUnicode_AsWideChar_fixed
425

426 427 428
extern PyObject *PyUnicode_FromWideChar_fixed(const wchar_t *, Py_ssize_t);
extern Py_ssize_t PyUnicode_AsWideChar_fixed(PyUnicodeObject *, wchar_t *, Py_ssize_t);
#endif
429 430
#endif

431 432
extern void _ctypes_free_closure(void *);
extern void *_ctypes_alloc_closure(void);
433

434
extern void _ctypes_add_traceback(char *, char *, int);
435

436 437
extern PyObject *PyCData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t index, char *adr);
extern char *_ctypes_alloc_format_string(const char *prefix, const char *suffix);
438

439
extern int _ctypes_simple_instance(PyObject *obj);
440

441 442
extern PyObject *_ctypes_ptrtype_cache;
PyObject *_ctypes_get_errobj(int **pspace);
443 444 445 446 447 448 449 450 451 452

#ifdef MS_WIN32
extern PyObject *ComError;
#endif

/*
 Local Variables:
 compile-command: "python setup.py -q build install --home ~"
 End:
*/