~ubuntu-branches/ubuntu/oneiric/python2.5/oneiric

« back to all changes in this revision

Viewing changes to Modules/_ctypes/callbacks.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-12-21 08:57:49 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20081221085749-bijjr25h8na5jdsu
Tags: 2.5.3-0ubuntu1
* New upstream version.
* Regenerate the included documentation.
* Add an option --install-layout=deb, which is ignored for 2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#endif
13
13
#include "ctypes.h"
14
14
 
 
15
/**************************************************************/
 
16
 
 
17
static CThunkObject_dealloc(PyObject *_self)
 
18
{
 
19
        CThunkObject *self = (CThunkObject *)_self;
 
20
        Py_XDECREF(self->converters);
 
21
        Py_XDECREF(self->callable);
 
22
        Py_XDECREF(self->restype);
 
23
        if (self->pcl)
 
24
                FreeClosure(self->pcl);
 
25
        PyObject_Del(self);
 
26
}
 
27
 
 
28
static int
 
29
CThunkObject_traverse(PyObject *_self, visitproc visit, void *arg)
 
30
{
 
31
        CThunkObject *self = (CThunkObject *)_self;
 
32
        Py_VISIT(self->converters);
 
33
        Py_VISIT(self->callable);
 
34
        Py_VISIT(self->restype);
 
35
        return 0;
 
36
}
 
37
 
 
38
static int
 
39
CThunkObject_clear(PyObject *_self)
 
40
{
 
41
        CThunkObject *self = (CThunkObject *)_self;
 
42
        Py_CLEAR(self->converters);
 
43
        Py_CLEAR(self->callable);
 
44
        Py_CLEAR(self->restype);
 
45
        return 0;
 
46
}
 
47
 
 
48
PyTypeObject CThunk_Type = {
 
49
        PyObject_HEAD_INIT(NULL)
 
50
        0,
 
51
        "_ctypes.CThunkObject",
 
52
        sizeof(CThunkObject),                   /* tp_basicsize */
 
53
        sizeof(ffi_type),                       /* tp_itemsize */
 
54
        CThunkObject_dealloc,                   /* tp_dealloc */
 
55
        0,                                      /* tp_print */
 
56
        0,                                      /* tp_getattr */
 
57
        0,                                      /* tp_setattr */
 
58
        0,                                      /* tp_compare */
 
59
        0,                                      /* tp_repr */
 
60
        0,                                      /* tp_as_number */
 
61
        0,                                      /* tp_as_sequence */
 
62
        0,                                      /* tp_as_mapping */
 
63
        0,                                      /* tp_hash */
 
64
        0,                                      /* tp_call */
 
65
        0,                                      /* tp_str */
 
66
        0,                                      /* tp_getattro */
 
67
        0,                                      /* tp_setattro */
 
68
        0,                                      /* tp_as_buffer */
 
69
        Py_TPFLAGS_DEFAULT,                     /* tp_flags */
 
70
        "CThunkObject",                         /* tp_doc */
 
71
        CThunkObject_traverse,                  /* tp_traverse */
 
72
        CThunkObject_clear,                     /* tp_clear */
 
73
        0,                                      /* tp_richcompare */
 
74
        0,                                      /* tp_weaklistoffset */
 
75
        0,                                      /* tp_iter */
 
76
        0,                                      /* tp_iternext */
 
77
        0,                                      /* tp_methods */
 
78
        0,                                      /* tp_members */
 
79
};
 
80
 
 
81
/**************************************************************/
 
82
 
15
83
static void
16
84
PrintError(char *msg, ...)
17
85
{
247
315
                        void **args,
248
316
                        void *userdata)
249
317
{
250
 
        ffi_info *p = userdata;
251
 
 
 
318
        CThunkObject *p = (CThunkObject *)userdata;
 
319
        
252
320
        _CallPythonObject(resp,
253
 
                          p->restype,
 
321
                          p->ffi_restype,
254
322
                          p->setfunc,
255
323
                          p->callable,
256
324
                          p->converters,
257
325
                          args);
258
326
}
259
327
 
260
 
ffi_info *AllocFunctionCallback(PyObject *callable,
261
 
                                PyObject *converters,
262
 
                                PyObject *restype,
263
 
                                int is_cdecl)
 
328
static CThunkObject* CThunkObject_new(Py_ssize_t nArgs)
 
329
{
 
330
        CThunkObject *p;
 
331
        int i;
 
332
 
 
333
        p = PyObject_NewVar(CThunkObject, &CThunk_Type, nArgs);
 
334
        if (p == NULL) {
 
335
                PyErr_NoMemory();
 
336
                return NULL;
 
337
        }
 
338
 
 
339
        p->pcl = NULL;
 
340
        memset(&p->cif, 0, sizeof(p->cif));
 
341
        p->converters = NULL;
 
342
        p->callable = NULL;
 
343
        p->setfunc = NULL;
 
344
        p->ffi_restype = NULL;
 
345
        
 
346
        for (i = 0; i < nArgs + 1; ++i)
 
347
                p->atypes[i] = NULL;
 
348
        return p;
 
349
}
 
350
 
 
351
CThunkObject *AllocFunctionCallback(PyObject *callable,
 
352
                                    PyObject *converters,
 
353
                                    PyObject *restype,
 
354
                                    int is_cdecl)
264
355
{
265
356
        int result;
266
 
        ffi_info *p;
 
357
        CThunkObject *p;
267
358
        int nArgs, i;
268
359
        ffi_abi cc;
269
360
 
270
361
        nArgs = PySequence_Size(converters);
271
 
        p = (ffi_info *)PyMem_Malloc(sizeof(ffi_info) + sizeof(ffi_type) * (nArgs));
272
 
        if (p == NULL) {
273
 
                PyErr_NoMemory();
 
362
        p = CThunkObject_new(nArgs);
 
363
        if (p == NULL)
274
364
                return NULL;
275
 
        }
 
365
 
 
366
        assert(CThunk_CheckExact(p));
 
367
 
276
368
        p->pcl = MallocClosure();
277
369
        if (p->pcl == NULL) {
278
370
                PyErr_NoMemory();
288
380
        }
289
381
        p->atypes[i] = NULL;
290
382
 
 
383
        Py_INCREF(restype);
 
384
        p->restype = restype;
291
385
        if (restype == Py_None) {
292
386
                p->setfunc = NULL;
293
 
                p->restype = &ffi_type_void;
 
387
                p->ffi_restype = &ffi_type_void;
294
388
        } else {
295
389
                StgDictObject *dict = PyType_stgdict(restype);
296
390
                if (dict == NULL || dict->setfunc == NULL) {
299
393
                  goto error;
300
394
                }
301
395
                p->setfunc = dict->setfunc;
302
 
                p->restype = &dict->ffi_type_pointer;
 
396
                p->ffi_restype = &dict->ffi_type_pointer;
303
397
        }
304
398
 
305
399
        cc = FFI_DEFAULT_ABI;
322
416
                goto error;
323
417
        }
324
418
 
 
419
        Py_INCREF(converters);
325
420
        p->converters = converters;
 
421
        Py_INCREF(callable);
326
422
        p->callable = callable;
327
423
        return p;
328
424
 
329
425
  error:
330
 
        if (p) {
331
 
                if (p->pcl)
332
 
                        FreeClosure(p->pcl);
333
 
                PyMem_Free(p);
334
 
        }
 
426
        Py_XDECREF(p);
335
427
        return NULL;
336
428
}
337
429