/Modules/_ctypes/ctypes.h

http://unladen-swallow.googlecode.com/ · C Header · 452 lines · 284 code · 72 blank · 96 comment · 14 complexity · 26cd32ac24814d3c5b1711f757f378b4 MD5 · raw file

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