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 | |
14 | typedef struct tagPyCArgObject PyCArgObject; |
15 | typedef struct tagCDataObject CDataObject; |
16 | typedef PyObject *(* GETFUNC)(void *, Py_ssize_t size); |
17 | typedef PyObject *(* SETFUNC)(void *, PyObject *value, Py_ssize_t size); |
18 | typedef PyCArgObject *(* PARAMFUNC)(CDataObject *obj); |
19 | |
20 | /* A default buffer in CDataObject, which can be used for small C types. If |
21 | this buffer is too small, PyMem_Malloc will be called to create a larger one, |
22 | and this one is not used. |
23 | |
24 | Making CDataObject a variable size object would be a better solution, but more |
25 | difficult in the presence of PyCFuncPtrObject. Maybe later. |
26 | */ |
27 | union 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 | |
44 | struct 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 | |
57 | typedef 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; |
70 | extern PyTypeObject PyCThunk_Type; |
71 | #define CThunk_CheckExact(v) Py_IS_TYPE(v, &PyCThunk_Type) |
72 | |
73 | typedef 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 | |
104 | extern PyTypeObject PyCStgDict_Type; |
105 | #define PyCStgDict_CheckExact(v) Py_IS_TYPE(v, &PyCStgDict_Type) |
106 | #define PyCStgDict_Check(v) PyObject_TypeCheck(v, &PyCStgDict_Type) |
107 | |
108 | extern int PyCStructUnionType_update_stgdict(PyObject *fields, PyObject *type, int isStruct); |
109 | extern int PyType_stginfo(PyTypeObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength); |
110 | extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength); |
111 | |
112 | |
113 | |
114 | extern PyTypeObject PyCData_Type; |
115 | #define CDataObject_CheckExact(v) Py_IS_TYPE(v, &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 | |
119 | extern PyTypeObject PyCSimpleType_Type; |
120 | #define PyCSimpleTypeObject_CheckExact(v) Py_IS_TYPE(v, &PyCSimpleType_Type) |
121 | #define PyCSimpleTypeObject_Check(v) PyObject_TypeCheck(v, &PyCSimpleType_Type) |
122 | |
123 | extern PyTypeObject PyCField_Type; |
124 | extern struct fielddesc *_ctypes_get_fielddesc(const char *fmt); |
125 | |
126 | |
127 | extern PyObject * |
128 | PyCField_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 | |
133 | extern PyObject *PyCData_AtAddress(PyObject *type, void *buf); |
134 | extern PyObject *PyCData_FromBytes(PyObject *type, char *data, Py_ssize_t length); |
135 | |
136 | extern PyTypeObject PyCArrayType_Type; |
137 | extern PyTypeObject PyCArray_Type; |
138 | extern PyTypeObject PyCPointerType_Type; |
139 | extern PyTypeObject PyCPointer_Type; |
140 | extern PyTypeObject PyCFuncPtr_Type; |
141 | extern PyTypeObject PyCFuncPtrType_Type; |
142 | extern 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 | |
152 | extern PyObject * |
153 | PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length); |
154 | |
155 | extern PyMethodDef _ctypes_module_methods[]; |
156 | |
157 | extern CThunkObject *_ctypes_alloc_callback(PyObject *callable, |
158 | PyObject *converters, |
159 | PyObject *restype, |
160 | int flags); |
161 | /* a table entry describing a predefined ctypes type */ |
162 | struct 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 | |
171 | typedef 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 */ |
185 | typedef 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 need 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! */ |
261 | extern StgDictObject *PyType_stgdict(PyObject *obj); |
262 | |
263 | /* May return NULL, but does not set an exception! */ |
264 | extern StgDictObject *PyObject_stgdict(PyObject *self); |
265 | |
266 | extern int PyCStgDict_clone(StgDictObject *src, StgDictObject *dst); |
267 | |
268 | typedef int(* PPROC)(void); |
269 | |
270 | PyObject *_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 | #define TYPEFLAG_HASUNION 0x400 |
292 | #define TYPEFLAG_HASBITFIELD 0x800 |
293 | |
294 | #define DICTFLAG_FINAL 0x1000 |
295 | |
296 | struct tagPyCArgObject { |
297 | PyObject_HEAD |
298 | ffi_type *pffi_type; |
299 | char tag; |
300 | union { |
301 | char c; |
302 | char b; |
303 | short h; |
304 | int i; |
305 | long l; |
306 | long long q; |
307 | long double D; |
308 | double d; |
309 | float f; |
310 | void *p; |
311 | } value; |
312 | PyObject *obj; |
313 | Py_ssize_t size; /* for the 'V' tag */ |
314 | }; |
315 | |
316 | extern PyTypeObject PyCArg_Type; |
317 | #define PyCArg_CheckExact(v) Py_IS_TYPE(v, &PyCArg_Type) |
318 | extern PyCArgObject *PyCArgObject_new(void); |
319 | |
320 | extern PyObject * |
321 | PyCData_get(PyObject *type, GETFUNC getfunc, PyObject *src, |
322 | Py_ssize_t index, Py_ssize_t size, char *ptr); |
323 | |
324 | extern int |
325 | PyCData_set(PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value, |
326 | Py_ssize_t index, Py_ssize_t size, char *ptr); |
327 | |
328 | extern void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...); |
329 | |
330 | struct basespec { |
331 | CDataObject *base; |
332 | Py_ssize_t index; |
333 | char *adr; |
334 | }; |
335 | |
336 | extern char basespec_string[]; |
337 | |
338 | extern ffi_type *_ctypes_get_ffi_type(PyObject *obj); |
339 | |
340 | /* exception classes */ |
341 | extern PyObject *PyExc_ArgError; |
342 | |
343 | extern char *_ctypes_conversion_encoding; |
344 | extern char *_ctypes_conversion_errors; |
345 | |
346 | |
347 | extern void _ctypes_free_closure(void *); |
348 | extern void *_ctypes_alloc_closure(void); |
349 | |
350 | extern PyObject *PyCData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t index, char *adr); |
351 | extern char *_ctypes_alloc_format_string(const char *prefix, const char *suffix); |
352 | extern char *_ctypes_alloc_format_string_with_shape(int ndim, |
353 | const Py_ssize_t *shape, |
354 | const char *prefix, const char *suffix); |
355 | |
356 | extern int _ctypes_simple_instance(PyObject *obj); |
357 | |
358 | extern PyObject *_ctypes_ptrtype_cache; |
359 | PyObject *_ctypes_get_errobj(int **pspace); |
360 | |
361 | #ifdef MS_WIN32 |
362 | extern PyObject *ComError; |
363 | #endif |
364 | |
365 | #if USING_MALLOC_CLOSURE_DOT_C |
366 | void Py_ffi_closure_free(void *p); |
367 | void *Py_ffi_closure_alloc(size_t size, void** codeloc); |
368 | #else |
369 | #define Py_ffi_closure_free ffi_closure_free |
370 | #define Py_ffi_closure_alloc ffi_closure_alloc |
371 | #endif |
372 | |
373 | /* |
374 | Local Variables: |
375 | compile-command: "python setup.py -q build install --home ~" |
376 | End: |
377 | */ |
378 | |