• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*****************************************************************
2   This file contains remnant Python 2.3 compatibility code that is no longer
3   strictly required.
4  *****************************************************************/
5 
6 #if defined (__SVR4) && defined (__sun)
7 #   include <alloca.h>
8 #endif
9 
10 #if (PY_VERSION_HEX < 0x02040000)
11 #define PyDict_CheckExact(ob) (Py_TYPE(ob) == &PyDict_Type)
12 #endif
13 
14 #if (PY_VERSION_HEX < 0x02050000)
15 typedef int Py_ssize_t;
16 #define PyInt_FromSsize_t PyInt_FromLong
17 #define PyNumber_AsSsize_t(ob, exc) PyInt_AsLong(ob)
18 #define PyIndex_Check(ob) PyInt_Check(ob)
19 typedef Py_ssize_t (*readbufferproc)(PyObject *, Py_ssize_t, void **);
20 typedef Py_ssize_t (*writebufferproc)(PyObject *, Py_ssize_t, void **);
21 typedef Py_ssize_t (*segcountproc)(PyObject *, Py_ssize_t *);
22 typedef Py_ssize_t (*charbufferproc)(PyObject *, Py_ssize_t, char **);
23 #endif
24 
25 #if (PY_VERSION_HEX < 0x02060000)
26 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
27 #define PyVarObject_HEAD_INIT(type, size) \
28     PyObject_HEAD_INIT(type) size,
29 #define PyImport_ImportModuleNoBlock PyImport_ImportModule
30 #define PyLong_FromSsize_t PyInt_FromLong
31 #define Py_TPFLAGS_HAVE_NEWBUFFER 0
32 #endif
33 
34 
35 #ifndef MS_WIN32
36 #define max(a, b) ((a) > (b) ? (a) : (b))
37 #define min(a, b) ((a) < (b) ? (a) : (b))
38 
39 #define PARAMFLAG_FIN 0x1
40 #define PARAMFLAG_FOUT 0x2
41 #define PARAMFLAG_FLCID 0x4
42 #endif
43 
44 /*
45   Backwards compatibility:
46   Python2.2 used LONG_LONG instead of PY_LONG_LONG
47 */
48 #if defined(HAVE_LONG_LONG) && !defined(PY_LONG_LONG)
49 #define PY_LONG_LONG LONG_LONG
50 #endif
51 
52 typedef struct tagPyCArgObject PyCArgObject;
53 typedef struct tagCDataObject CDataObject;
54 typedef PyObject *(* GETFUNC)(void *, Py_ssize_t size);
55 typedef PyObject *(* SETFUNC)(void *, PyObject *value, Py_ssize_t size);
56 typedef PyCArgObject *(* PARAMFUNC)(CDataObject *obj);
57 
58 /* A default buffer in CDataObject, which can be used for small C types.  If
59 this buffer is too small, PyMem_Malloc will be called to create a larger one,
60 and this one is not used.
61 
62 Making CDataObject a variable size object would be a better solution, but more
63 difficult in the presence of PyCFuncPtrObject.  Maybe later.
64 */
65 union value {
66                 char c[16];
67                 short s;
68                 int i;
69                 long l;
70                 float f;
71                 double d;
72 #ifdef HAVE_LONG_LONG
73                 PY_LONG_LONG ll;
74 #endif
75                 long double D;
76 };
77 
78 /*
79   Hm. Are there CDataObject's which do not need the b_objects member?  In
80   this case we probably should introduce b_flags to mark it as present...  If
81   b_objects is not present/unused b_length is unneeded as well.
82 */
83 
84 struct tagCDataObject {
85     PyObject_HEAD
86     char *b_ptr;                /* pointer to memory block */
87     int  b_needsfree;           /* need _we_ free the memory? */
88     CDataObject *b_base;        /* pointer to base object or NULL */
89     Py_ssize_t b_size;          /* size of memory block in bytes */
90     Py_ssize_t b_length;        /* number of references we need */
91     Py_ssize_t b_index;         /* index of this object into base's
92                                b_object list */
93     PyObject *b_objects;        /* dictionary of references we need to keep, or Py_None */
94     union value b_value;
95 };
96 
97 typedef struct {
98     PyObject_VAR_HEAD
99     ffi_closure *pcl_write; /* the C callable, writeable */
100     void *pcl_exec;         /* the C callable, executable */
101     ffi_cif cif;
102     int flags;
103     PyObject *converters;
104     PyObject *callable;
105     PyObject *restype;
106     SETFUNC setfunc;
107     ffi_type *ffi_restype;
108     ffi_type *atypes[1];
109 } CThunkObject;
110 extern PyTypeObject PyCThunk_Type;
111 #define CThunk_CheckExact(v)        ((v)->ob_type == &PyCThunk_Type)
112 
113 typedef struct {
114     /* First part identical to tagCDataObject */
115     PyObject_HEAD
116     char *b_ptr;                /* pointer to memory block */
117     int  b_needsfree;           /* need _we_ free the memory? */
118     CDataObject *b_base;        /* pointer to base object or NULL */
119     Py_ssize_t b_size;          /* size of memory block in bytes */
120     Py_ssize_t b_length;        /* number of references we need */
121     Py_ssize_t b_index;         /* index of this object into base's
122                                b_object list */
123     PyObject *b_objects;        /* list of references we need to keep */
124     union value b_value;
125     /* end of tagCDataObject, additional fields follow */
126 
127     CThunkObject *thunk;
128     PyObject *callable;
129 
130     /* These two fields will override the ones in the type's stgdict if
131        they are set */
132     PyObject *converters;
133     PyObject *argtypes;
134     PyObject *restype;
135     PyObject *checker;
136     PyObject *errcheck;
137 #ifdef MS_WIN32
138     int index;
139     GUID *iid;
140 #endif
141     PyObject *paramflags;
142 } PyCFuncPtrObject;
143 
144 extern PyTypeObject PyCStgDict_Type;
145 #define PyCStgDict_CheckExact(v)            ((v)->ob_type == &PyCStgDict_Type)
146 #define PyCStgDict_Check(v)         PyObject_TypeCheck(v, &PyCStgDict_Type)
147 
148 extern int PyCStructUnionType_update_stgdict(PyObject *fields, PyObject *type, int isStruct);
149 extern int PyType_stginfo(PyTypeObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
150 extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
151 
152 
153 
154 extern PyTypeObject PyCData_Type;
155 #define CDataObject_CheckExact(v)       ((v)->ob_type == &PyCData_Type)
156 #define CDataObject_Check(v)            PyObject_TypeCheck(v, &PyCData_Type)
157 #define _CDataObject_HasExternalBuffer(v)  ((v)->b_ptr != (char *)&(v)->b_value)
158 
159 extern PyTypeObject PyCSimpleType_Type;
160 #define PyCSimpleTypeObject_CheckExact(v)       ((v)->ob_type == &PyCSimpleType_Type)
161 #define PyCSimpleTypeObject_Check(v)    PyObject_TypeCheck(v, &PyCSimpleType_Type)
162 
163 extern PyTypeObject PyCField_Type;
164 extern struct fielddesc *_ctypes_get_fielddesc(char *fmt);
165 
166 
167 extern PyObject *
168 PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
169                 Py_ssize_t *pfield_size, int bitsize, int *pbitofs,
170                 Py_ssize_t *psize, Py_ssize_t *poffset, Py_ssize_t *palign,
171                 int pack, int is_big_endian);
172 
173 extern PyObject *PyCData_AtAddress(PyObject *type, void *buf);
174 extern PyObject *PyCData_FromBytes(PyObject *type, char *data, Py_ssize_t length);
175 
176 extern PyTypeObject PyCArrayType_Type;
177 extern PyTypeObject PyCArray_Type;
178 extern PyTypeObject PyCPointerType_Type;
179 extern PyTypeObject PyCPointer_Type;
180 extern PyTypeObject PyCFuncPtr_Type;
181 extern PyTypeObject PyCFuncPtrType_Type;
182 extern PyTypeObject PyCStructType_Type;
183 
184 #define PyCArrayTypeObject_Check(v)     PyObject_TypeCheck(v, &PyCArrayType_Type)
185 #define ArrayObject_Check(v)            PyObject_TypeCheck(v, &PyCArray_Type)
186 #define PointerObject_Check(v)          PyObject_TypeCheck(v, &PyCPointer_Type)
187 #define PyCPointerTypeObject_Check(v)   PyObject_TypeCheck(v, &PyCPointerType_Type)
188 #define PyCFuncPtrObject_Check(v)               PyObject_TypeCheck(v, &PyCFuncPtr_Type)
189 #define PyCFuncPtrTypeObject_Check(v)   PyObject_TypeCheck(v, &PyCFuncPtrType_Type)
190 #define PyCStructTypeObject_Check(v)    PyObject_TypeCheck(v, &PyCStructType_Type)
191 
192 extern PyObject *
193 PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length);
194 
195 extern PyMethodDef _ctypes_module_methods[];
196 
197 extern CThunkObject *_ctypes_alloc_callback(PyObject *callable,
198                                            PyObject *converters,
199                                            PyObject *restype,
200                                            int flags);
201 /* a table entry describing a predefined ctypes type */
202 struct fielddesc {
203     char code;
204     SETFUNC setfunc;
205     GETFUNC getfunc;
206     ffi_type *pffi_type; /* always statically allocated */
207     SETFUNC setfunc_swapped;
208     GETFUNC getfunc_swapped;
209 };
210 
211 typedef struct {
212     PyObject_HEAD
213     Py_ssize_t offset;
214     Py_ssize_t size;
215     Py_ssize_t index;                   /* Index into CDataObject's
216                                        object array */
217     PyObject *proto;                    /* a type or NULL */
218     GETFUNC getfunc;                    /* getter function if proto is NULL */
219     SETFUNC setfunc;                    /* setter function if proto is NULL */
220     int anonymous;
221 } CFieldObject;
222 
223 /* A subclass of PyDictObject, used as the instance dictionary of ctypes
224    metatypes */
225 typedef struct {
226     PyDictObject dict;          /* first part identical to PyDictObject */
227 /* The size and align fields are unneeded, they are in ffi_type as well.  As
228    an experiment shows, it's trivial to get rid of them, the only thing to
229    remember is that in PyCArrayType_new the ffi_type fields must be filled in -
230    so far it was unneeded because libffi doesn't support arrays at all
231    (because they are passed as pointers to function calls anyway).  But it's
232    too much risk to change that now, and there are other fields which doesn't
233    belong into this structure anyway.  Maybe in ctypes 2.0... (ctypes 2000?)
234 */
235     Py_ssize_t size;            /* number of bytes */
236     Py_ssize_t align;           /* alignment requirements */
237     Py_ssize_t length;          /* number of fields */
238     ffi_type ffi_type_pointer;
239     PyObject *proto;            /* Only for Pointer/ArrayObject */
240     SETFUNC setfunc;            /* Only for simple objects */
241     GETFUNC getfunc;            /* Only for simple objects */
242     PARAMFUNC paramfunc;
243 
244     /* Following fields only used by PyCFuncPtrType_Type instances */
245     PyObject *argtypes;         /* tuple of CDataObjects */
246     PyObject *converters;       /* tuple([t.from_param for t in argtypes]) */
247     PyObject *restype;          /* CDataObject or NULL */
248     PyObject *checker;
249     int flags;                  /* calling convention and such */
250 
251     /* pep3118 fields, pointers neeed PyMem_Free */
252     char *format;
253     int ndim;
254     Py_ssize_t *shape;
255 /*      Py_ssize_t *strides;    */ /* unused in ctypes */
256 /*      Py_ssize_t *suboffsets; */ /* unused in ctypes */
257 
258 } StgDictObject;
259 
260 /****************************************************************
261  StgDictObject fields
262 
263  setfunc and getfunc is only set for simple data types, it is copied from the
264  corresponding fielddesc entry.  These are functions to set and get the value
265  in a memory block.
266  They should probably by used by other types as well.
267 
268  proto is only used for Pointer and Array types - it points to the item type
269  object.
270 
271  Probably all the magic ctypes methods (like from_param) should have C
272  callable wrappers in the StgDictObject.  For simple data type, for example,
273  the fielddesc table could have entries for C codec from_param functions or
274  other methods as well, if a subtype overrides this method in Python at
275  construction time, or assigns to it later, tp_setattro should update the
276  StgDictObject function to a generic one.
277 
278  Currently, PyCFuncPtr types have 'converters' and 'checker' entries in their
279  type dict.  They are only used to cache attributes from other entries, which
280  is wrong.
281 
282  One use case is the .value attribute that all simple types have.  But some
283  complex structures, like VARIANT, represent a single value also, and should
284  have this attribute.
285 
286  Another use case is a _check_retval_ function, which is called when a ctypes
287  type is used as return type of a function to validate and compute the return
288  value.
289 
290  Common ctypes protocol:
291 
292   - setfunc: store a python value in a memory block
293   - getfunc: convert data from a memory block into a python value
294 
295   - checkfunc: validate and convert a return value from a function call
296   - toparamfunc: convert a python value into a function argument
297 
298 *****************************************************************/
299 
300 /* May return NULL, but does not set an exception! */
301 extern StgDictObject *PyType_stgdict(PyObject *obj);
302 
303 /* May return NULL, but does not set an exception! */
304 extern StgDictObject *PyObject_stgdict(PyObject *self);
305 
306 extern int PyCStgDict_clone(StgDictObject *src, StgDictObject *dst);
307 
308 typedef int(* PPROC)(void);
309 
310 PyObject *_ctypes_callproc(PPROC pProc,
311                     PyObject *arguments,
312 #ifdef MS_WIN32
313                     IUnknown *pIUnk,
314                     GUID *iid,
315 #endif
316                     int flags,
317                     PyObject *argtypes,
318                     PyObject *restype,
319                     PyObject *checker);
320 
321 
322 #define FUNCFLAG_STDCALL 0x0
323 #define FUNCFLAG_CDECL   0x1
324 #define FUNCFLAG_HRESULT 0x2
325 #define FUNCFLAG_PYTHONAPI 0x4
326 #define FUNCFLAG_USE_ERRNO 0x8
327 #define FUNCFLAG_USE_LASTERROR 0x10
328 
329 #define TYPEFLAG_ISPOINTER 0x100
330 #define TYPEFLAG_HASPOINTER 0x200
331 
332 #define DICTFLAG_FINAL 0x1000
333 
334 struct tagPyCArgObject {
335     PyObject_HEAD
336     ffi_type *pffi_type;
337     char tag;
338     union {
339         char c;
340         char b;
341         short h;
342         int i;
343         long l;
344 #ifdef HAVE_LONG_LONG
345         PY_LONG_LONG q;
346 #endif
347         long double D;
348         double d;
349         float f;
350         void *p;
351     } value;
352     PyObject *obj;
353     Py_ssize_t size; /* for the 'V' tag */
354 };
355 
356 extern PyTypeObject PyCArg_Type;
357 #define PyCArg_CheckExact(v)        ((v)->ob_type == &PyCArg_Type)
358 extern PyCArgObject *PyCArgObject_new(void);
359 
360 extern PyObject *
361 PyCData_get(PyObject *type, GETFUNC getfunc, PyObject *src,
362           Py_ssize_t index, Py_ssize_t size, char *ptr);
363 
364 extern int
365 PyCData_set(PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
366           Py_ssize_t index, Py_ssize_t size, char *ptr);
367 
368 extern void _ctypes_extend_error(PyObject *exc_class, char *fmt, ...);
369 
370 struct basespec {
371     CDataObject *base;
372     Py_ssize_t index;
373     char *adr;
374 };
375 
376 extern char basespec_string[];
377 
378 extern ffi_type *_ctypes_get_ffi_type(PyObject *obj);
379 
380 /* exception classes */
381 extern PyObject *PyExc_ArgError;
382 
383 extern char *_ctypes_conversion_encoding;
384 extern char *_ctypes_conversion_errors;
385 
386 /* Python 2.4 macros, which are not available in Python 2.3 */
387 
388 #ifndef Py_CLEAR
389 #define Py_CLEAR(op)                            \
390     do {                                        \
391         if (op) {                               \
392             PyObject *tmp = (PyObject *)(op);                   \
393             (op) = NULL;                        \
394             Py_DECREF(tmp);                     \
395         }                                       \
396     } while (0)
397 #endif
398 
399 #ifndef Py_VISIT
400 /* Utility macro to help write tp_traverse functions.
401  * To use this macro, the tp_traverse function must name its arguments
402  * "visit" and "arg".  This is intended to keep tp_traverse functions
403  * looking as much alike as possible.
404  */
405 #define Py_VISIT(op)                                    \
406     do {                                                \
407         if (op) {                                       \
408             int vret = visit((op), arg);                \
409             if (vret)                                   \
410                 return vret;                            \
411         }                                               \
412     } while (0)
413 #endif
414 
415 /* Python's PyUnicode_*WideChar functions are broken ... */
416 #if defined(Py_USING_UNICODE) && defined(HAVE_WCHAR_H)
417 #  define CTYPES_UNICODE
418 #endif
419 
420 
421 #if (PY_VERSION_HEX < 0x02040000)
422 #ifdef CTYPES_UNICODE
423 #  undef PyUnicode_FromWideChar
424 #  define PyUnicode_FromWideChar PyUnicode_FromWideChar_fixed
425 
426 #  undef PyUnicode_AsWideChar
427 #  define PyUnicode_AsWideChar PyUnicode_AsWideChar_fixed
428 
429 extern PyObject *PyUnicode_FromWideChar_fixed(const wchar_t *, Py_ssize_t);
430 extern Py_ssize_t PyUnicode_AsWideChar_fixed(PyUnicodeObject *, wchar_t *, Py_ssize_t);
431 #endif
432 #endif
433 
434 extern void _ctypes_add_traceback(char *, char *, int);
435 
436 extern PyObject *PyCData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t index, char *adr);
437 extern char *_ctypes_alloc_format_string(const char *prefix, const char *suffix);
438 extern char *_ctypes_alloc_format_string_with_shape(int ndim,
439                                                 const Py_ssize_t *shape,
440                                                 const char *prefix, const char *suffix);
441 
442 extern int _ctypes_simple_instance(PyObject *obj);
443 
444 extern PyObject *_ctypes_ptrtype_cache;
445 PyObject *_ctypes_get_errobj(int **pspace);
446 
447 #ifdef MS_WIN32
448 extern PyObject *ComError;
449 #endif
450 
451 #if PY_VERSION_HEX >= 0x020700A4
452 /* Use PyCapsule for 2.7 */
453 
454 #define CTYPES_USING_CAPSULE
455 
456 #define CTYPES_CAPSULE_INSTANTIATE_DESTRUCTOR(name) \
457 static void capsule_destructor_ ## name(PyObject *ptr) \
458 { \
459     void *p = PyCapsule_GetPointer(ptr, name); \
460     if (p) { \
461         PyMem_Free(p); \
462     } \
463 } \
464 
465 #define CAPSULE_NEW(pointer, name) \
466     (PyCapsule_New(pointer, name, capsule_destructor_ ## name))
467 
468 #define CAPSULE_DEREFERENCE(capsule, name) \
469   (PyCapsule_GetPointer(capsule, name))
470 
471 #else /* PY_VERSION_HEX >= 0x020700A4 */
472 /* Use CObject for 2.6 and before */
473 
474 #define CTYPES_CAPSULE_INSTANTIATE_DESTRUCTOR(name)
475 
476 #define CAPSULE_NEW(pointer, name) \
477     (PyCObject_FromVoidPtr(pointer, PyMem_Free))
478 
479 #define CAPSULE_DEREFERENCE(capsule, name) \
480   (PyCObject_AsVoidPtr(capsule))
481 
482 #endif /* PY_VERSION_HEX >= 0x020700A4 */
483 
484 
485 /*
486  Local Variables:
487  compile-command: "python setup.py -q build install --home ~"
488  End:
489 */
490