~ubuntu-branches/ubuntu/intrepid/openipmi/intrepid

« back to all changes in this revision

Viewing changes to swig/python/OpenIPMI_lang.i

  • Committer: Bazaar Package Importer
  • Author(s): Noèl Köthe
  • Date: 2005-07-04 21:29:17 UTC
  • Revision ID: james.westby@ubuntu.com-20050704212917-igddk5jawjmhrlay
Tags: upstream-2.0.1
Import upstream version 2.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * OpenIPMI_lang.i
 
3
 *
 
4
 * Python-specific OpenIPMI SWIG language information
 
5
 *
 
6
 * Author: MontaVista Software, Inc.
 
7
 *         Corey Minyard <minyard@mvista.com>
 
8
 *         source@mvista.com
 
9
 *
 
10
 * Copyright 2004 MontaVista Software Inc.
 
11
 *
 
12
 *  This program is free software; you can redistribute it and/or
 
13
 *  modify it under the terms of the GNU Lesser General Public License
 
14
 *  as published by the Free Software Foundation; either version 2 of
 
15
 *  the License, or (at your option) any later version.
 
16
 *
 
17
 *
 
18
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 
19
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
20
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 
21
 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 
22
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 
23
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 
24
 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
25
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 
26
 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 
27
 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 *
 
29
 *  You should have received a copy of the GNU Lesser General Public
 
30
 *  License along with this program; if not, write to the Free
 
31
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
32
 */
 
33
 
 
34
 
 
35
%typemap(in) swig_cb {
 
36
    $1 = $input;
 
37
}
 
38
 
 
39
%typemap(arginit) intarray {
 
40
    $1.val = NULL;
 
41
}
 
42
 
 
43
%typemap(in) intarray {
 
44
    int i;
 
45
 
 
46
    if (!PySequence_Check($input)) {
 
47
        PyErr_SetString(PyExc_TypeError,"Expecting a sequence");
 
48
        return NULL;
 
49
    }
 
50
    $1.len = PyObject_Length($input);
 
51
    $1.val = (int *) malloc($1.len*sizeof(int));
 
52
 
 
53
    for (i=0; i<$1.len; i++) {
 
54
        PyObject *o = PySequence_GetItem($input,i);
 
55
        if (!o) {
 
56
            PyErr_SetString(PyExc_ValueError, "Expecting a sequence of ints");
 
57
            return NULL;
 
58
        }
 
59
        if (!PyInt_Check(o)) {
 
60
            free($1.val);
 
61
            PyErr_SetString(PyExc_ValueError,"Expecting a sequence of ints");
 
62
            Py_DECREF(o);
 
63
            return NULL;
 
64
        }
 
65
        $1.val[i] = PyInt_AS_LONG(o);
 
66
        Py_DECREF(o);
 
67
    }
 
68
}
 
69
 
 
70
%typemap(freearg) intarray {
 
71
    if ($1.val)
 
72
        free($1.val);
 
73
};
 
74
 
 
75
%typemap(out) intarray {
 
76
    PyObject *list;
 
77
    int i;
 
78
 
 
79
    list = PyList_New($1.len);
 
80
    if (!list) {
 
81
        PyErr_SetString(PyExc_ValueError,
 
82
                        "Unable to allocate intarray object");
 
83
        return NULL;
 
84
    }
 
85
    for (i=0; i<$1.len; i++) {
 
86
        PyObject *o = PyInt_FromLong($1.val[i]);
 
87
        if (!o) {
 
88
            int j;
 
89
            for (j=0; j<i; j++) {
 
90
                o = PyList_GetItem(list, j);
 
91
                Py_DECREF(o);
 
92
            }
 
93
            Py_DECREF(list);
 
94
            PyErr_SetString(PyExc_ValueError,
 
95
                            "Unable to allocate intarray object");
 
96
            return NULL;
 
97
        }
 
98
        PyList_SET_ITEM(list, i, o);
 
99
    }
 
100
    $result = list;
 
101
}
 
102
 
 
103
%typemap(in) char ** {
 
104
    int i, len;
 
105
 
 
106
    if (!PySequence_Check($input)) {
 
107
        PyErr_SetString(PyExc_TypeError,"Expecting a sequence");
 
108
        return NULL;
 
109
    }
 
110
    len = PyObject_Length($input);
 
111
    $1 = (char **) malloc((len+1)*sizeof(char *));
 
112
    for (i=0; i<len; i++) {
 
113
        PyObject *o = PySequence_GetItem($input,i);
 
114
        if (!o) {
 
115
            PyErr_SetString(PyExc_ValueError, "Expecting a sequence of strings");
 
116
            return NULL;
 
117
        }
 
118
        if (!PyString_Check(o)) {
 
119
            PyErr_SetString(PyExc_ValueError,"Expecting a sequence of strings");
 
120
            Py_DECREF(o);
 
121
            return NULL;
 
122
        }
 
123
        $1[i] = PyString_AS_STRING(o);
 
124
        Py_DECREF(o);
 
125
    }
 
126
    $1[i] = NULL;
 
127
};
 
128
 
 
129
%typemap(freearg) char ** {
 
130
    free($1);
 
131
};
 
132
 
 
133
%typemap(in) double * (double dvalue) {
 
134
    PyObject *o;
 
135
    if (!PySequence_Check($input)) {
 
136
        PyErr_SetString(PyExc_ValueError,"Expecting a sequence");
 
137
        return NULL;
 
138
    }
 
139
    o = PySequence_GetItem($input,0);
 
140
    if (!o) {
 
141
        PyErr_SetString(PyExc_ValueError, "Expecting a floating point number");
 
142
        return NULL;
 
143
    }
 
144
    if (!PyFloat_Check(o)) {
 
145
        Py_DECREF(o);
 
146
        PyErr_SetString(PyExc_ValueError, "expected a floating point number");
 
147
        return NULL;
 
148
    }
 
149
    dvalue = PyFloat_AS_DOUBLE(o);
 
150
    Py_DECREF(o);
 
151
    $1 = &dvalue;
 
152
}
 
153
 
 
154
%typemap(argout) double * {
 
155
    PyObject *o = PyFloat_FromDouble(*$1);
 
156
    if (!o) {
 
157
        PyErr_SetString(PyExc_TypeError, "Unable to allocate double object");
 
158
        return NULL;
 
159
    }
 
160
    if (PySequence_SetItem($input, 0, o) == -1) {
 
161
        PyErr_SetString(PyExc_TypeError, "Unable to set double object item");
 
162
        Py_DECREF(o);
 
163
        return NULL;
 
164
    }
 
165
    Py_DECREF(o);
 
166
}
 
167
 
 
168
%typemap(in) int * (int ivalue) {
 
169
    PyObject *o;
 
170
    if (!PySequence_Check($input)) {
 
171
        PyErr_SetString(PyExc_ValueError, "Expecting a sequence");
 
172
        return NULL;
 
173
    }
 
174
    o = PySequence_GetItem($input, 0);
 
175
    if (!o) {
 
176
        PyErr_SetString(PyExc_ValueError, "Expecting an integer number");
 
177
        return NULL;
 
178
    }
 
179
    if (!PyInt_Check(o)) {
 
180
        Py_DECREF(o);
 
181
        PyErr_SetString(PyExc_ValueError, "expected an integer number");
 
182
        return NULL;
 
183
    }
 
184
    ivalue = PyInt_AS_LONG(o);
 
185
    Py_DECREF(o);
 
186
    $1 = &ivalue;
 
187
}
 
188
 
 
189
%typemap(argout) int * {
 
190
    PyObject *o = PyInt_FromLong(*$1);
 
191
    if (!o) {
 
192
        PyErr_SetString(PyExc_TypeError, "Unable to allocate int object");
 
193
        return NULL;
 
194
    }
 
195
    if (PySequence_SetItem($input, 0, o) == -1) {
 
196
        PyErr_SetString(PyExc_TypeError, "Unable to set int object item");
 
197
        Py_DECREF(o);
 
198
        return NULL;
 
199
    }
 
200
    Py_DECREF(o);
 
201
}
 
202
 
 
203
%typemap(in) unsigned int * (unsigned int ivalue) {
 
204
    PyObject *o;
 
205
    if (!PySequence_Check($input)) {
 
206
        PyErr_SetString(PyExc_ValueError, "Expecting a sequence");
 
207
        return NULL;
 
208
    }
 
209
    o = PySequence_GetItem($input, 0);
 
210
    if (!o) {
 
211
        PyErr_SetString(PyExc_ValueError, "Expecting an integer number");
 
212
        return NULL;
 
213
    }
 
214
    if (!PyInt_Check(o)) {
 
215
        PyErr_SetString(PyExc_ValueError, "expected an integer number");
 
216
        Py_DECREF(o);
 
217
        return NULL;
 
218
    }
 
219
    ivalue = PyInt_AS_LONG(o);
 
220
    Py_DECREF(o);
 
221
    $1 = &ivalue;
 
222
}
 
223
 
 
224
%typemap(argout) unsigned int * {
 
225
    PyObject *o = PyInt_FromLong(*$1);
 
226
    if (!o) {
 
227
        PyErr_SetString(PyExc_TypeError, "Unable to allocate int object");
 
228
        return NULL;
 
229
    }
 
230
    if (PySequence_SetItem($input, 0, o) == -1) {
 
231
        PyErr_SetString(PyExc_TypeError, "Unable to set int object item");
 
232
        Py_DECREF(o);
 
233
        return NULL;
 
234
    }
 
235
    Py_DECREF(o);
 
236
}
 
237
 
 
238
%{
 
239
 
 
240
static swig_ref
 
241
swig_make_ref_destruct_i(void *item, swig_type_info *class)
 
242
{
 
243
    swig_ref rv;
 
244
 
 
245
    rv.val = SWIG_NewPointerObj(item, class, 1);
 
246
    return rv;
 
247
}
 
248
 
 
249
/* Make a reference whose destructor will be called when everything
 
250
   is done with it. */
 
251
#define swig_make_ref_destruct(item, name) \
 
252
        swig_make_ref_destruct_i(item, SWIGTYPE_p_ ## name)
 
253
 
 
254
static swig_ref
 
255
swig_make_ref_i(void *item, swig_type_info *class)
 
256
{
 
257
    swig_ref rv;
 
258
 
 
259
    rv.val = SWIG_NewPointerObj(item, class, 0);
 
260
    return rv;
 
261
}
 
262
 
 
263
#define swig_make_ref(item, name) \
 
264
        swig_make_ref_i(item, SWIGTYPE_p_ ## name)
 
265
 
 
266
static void
 
267
swig_free_ref(swig_ref ref)
 
268
{
 
269
    Py_DECREF(ref.val);
 
270
}
 
271
 
 
272
static swig_cb_val
 
273
get_swig_cb_i(swig_cb cb)
 
274
{
 
275
    return cb;
 
276
}
 
277
#define get_swig_cb(cb, func) get_swig_cb_i(cb)
 
278
 
 
279
static swig_cb_val
 
280
ref_swig_cb_i(swig_cb cb)
 
281
{
 
282
    Py_INCREF(cb);
 
283
    return cb;
 
284
}
 
285
#define ref_swig_cb(cb, func) ref_swig_cb_i(cb)
 
286
 
 
287
static swig_cb_val
 
288
deref_swig_cb(swig_cb cb)
 
289
{
 
290
    Py_DECREF(cb);
 
291
    return cb;
 
292
}
 
293
 
 
294
static swig_cb_val
 
295
deref_swig_cb_val(swig_cb_val cb)
 
296
{
 
297
    Py_DECREF(cb);
 
298
    return cb;
 
299
}
 
300
 
 
301
static int
 
302
valid_swig_cb_i(swig_cb cb, char *func)
 
303
{
 
304
    PyObject *meth = PyObject_GetAttrString(cb, func);
 
305
    if (!meth)
 
306
        return 0;
 
307
    if (!PyMethod_Check(meth))
 
308
        return 0;
 
309
    Py_DECREF(meth);
 
310
    return 1;
 
311
}
 
312
#define valid_swig_cb(v, func) valid_swig_cb_i(v, #func)
 
313
 
 
314
static int
 
315
swig_count_format(char *format)
 
316
{
 
317
    int count = 0;
 
318
 
 
319
    for (; *format; format++) {
 
320
        if (*format != '%')
 
321
            continue;
 
322
        format++;
 
323
        if (*format == '\0')
 
324
            break;
 
325
        switch (*format) {
 
326
        case 'd':
 
327
        case 'f':
 
328
        case 's':
 
329
        case 'p':
 
330
            count++;
 
331
            break;
 
332
 
 
333
        case '*':
 
334
            format++;
 
335
            if (*format == '\0')
 
336
                break;
 
337
            switch(*format) {
 
338
            case 's':
 
339
            case 'p':
 
340
            case 'o':
 
341
                count++;
 
342
                break;
 
343
 
 
344
            default:
 
345
                break;
 
346
            }
 
347
            break;
 
348
 
 
349
        case 'l':
 
350
            format++;
 
351
            if (*format == '\0')
 
352
                break;
 
353
            switch(*format) {
 
354
            case 'd':
 
355
                count++;
 
356
                break;
 
357
 
 
358
            default:
 
359
                break;
 
360
            }
 
361
            break;
 
362
 
 
363
        default:
 
364
            break;
 
365
        }
 
366
    }
 
367
    return count;
 
368
}
 
369
 
 
370
static void swig_call_cb(swig_cb_val cb, char *method_name, char *format, ...)
 
371
#ifdef __GNUC__
 
372
     __attribute__ ((__format__ (__printf__, 3, 4)))
 
373
#endif
 
374
;
 
375
static void
 
376
swig_call_cb(swig_cb_val cb, char *method_name,
 
377
             char *format, ...)
 
378
{
 
379
    va_list       ap;
 
380
    int           len;
 
381
    unsigned char *data;
 
382
    int           *idata;
 
383
    PyObject      *args = NULL;
 
384
    int           n;
 
385
    int           i;
 
386
    int           pos;
 
387
    char          *errstr;
 
388
    PyObject      *o = NULL;
 
389
    PyObject      *p;
 
390
 
 
391
    n = swig_count_format(format);
 
392
 
 
393
    args = PyTuple_New(n);
 
394
    if (!args) {
 
395
        errstr = "cannot allocate PyTyple";
 
396
        goto out_err;
 
397
    }
 
398
 
 
399
    va_start(ap, format);
 
400
 
 
401
    pos = 0;
 
402
    for (; *format; format++) {
 
403
        if (*format != '%')
 
404
            continue;
 
405
        format++;
 
406
        if (*format == '\0')
 
407
            break;
 
408
        o = NULL;
 
409
        switch (*format) {
 
410
        case 'd':
 
411
            o = PyInt_FromLong(va_arg(ap, int));
 
412
            break;
 
413
 
 
414
        case 'f':
 
415
            o = PyFloat_FromDouble(va_arg(ap, double));
 
416
            break;
 
417
 
 
418
        case '*':
 
419
            format++;
 
420
            if (*format == '\0')
 
421
                break;
 
422
            switch(*format) {
 
423
            case 's':
 
424
                /* An array of unsigned characters */
 
425
                len = va_arg(ap, int);
 
426
                data = va_arg(ap, unsigned char *);
 
427
                o = PyList_New(len);
 
428
                if (!o) {
 
429
                    errstr = "cannot allocate list";
 
430
                    goto out_err;
 
431
                }
 
432
                for (i=0; i<len; i++, data++) {
 
433
                    p = PyInt_FromLong(*data);
 
434
                    if (!p) {
 
435
                        errstr = "cannot allocate uchar list item";
 
436
                        goto out_err;
 
437
                    }
 
438
                    PyList_SET_ITEM(o, i, p);
 
439
                }
 
440
                break;
 
441
 
 
442
            case 'p':
 
443
                /* An array of integers */
 
444
                len = va_arg(ap, int);
 
445
                idata = va_arg(ap, int *);
 
446
                o = PyList_New(len);
 
447
                if (!o) {
 
448
                    errstr = "cannot allocate list";
 
449
                    goto out_err;
 
450
                }
 
451
                for (i=0; i<len; i++, idata++) {
 
452
                    p = PyInt_FromLong(*idata);
 
453
                    if (!p) {
 
454
                        errstr = "cannot allocate uchar list item";
 
455
                        goto out_err;
 
456
                    }
 
457
                    PyList_SET_ITEM(o, i, p);
 
458
                }
 
459
                break;
 
460
 
 
461
            case 'o':
 
462
                /* An array of objects */
 
463
                {
 
464
                    swig_ref **list;
 
465
                    len = va_arg(ap, int);
 
466
                    list = va_arg(ap, swig_ref **);
 
467
                    o = PyList_New(len);
 
468
                    if (!o) {
 
469
                        errstr = "cannot allocate list";
 
470
                        goto out_err;
 
471
                    }
 
472
                    for (i=0; i<len; i++, list++)
 
473
                        PyList_SET_ITEM(o, i, (*list)->val);
 
474
                }
 
475
                break;
 
476
 
 
477
            default:
 
478
                break;
 
479
            }
 
480
            break;
 
481
 
 
482
        case 'l':
 
483
            format++;
 
484
            if (*format == '\0')
 
485
                break;
 
486
            switch(*format) {
 
487
            case 'd':
 
488
                /* Long int */
 
489
                o = PyInt_FromLong(va_arg(ap, long));
 
490
                break;
 
491
 
 
492
            default:
 
493
                break;
 
494
            }
 
495
            break;
 
496
            
 
497
        case 's':
 
498
            /* String */
 
499
            o = PyString_FromString(va_arg(ap, char *));
 
500
            break;
 
501
 
 
502
        case 'p':
 
503
            /* Object pointer (swig_ref) */
 
504
            {
 
505
                swig_ref *v = va_arg(ap, swig_ref *);
 
506
                o = v->val;
 
507
            }
 
508
            break;
 
509
 
 
510
        default:
 
511
            break;
 
512
        }
 
513
 
 
514
        if (!o) {
 
515
            errstr = "Problem getting object";
 
516
            goto out_err;
 
517
        }
 
518
 
 
519
        PyTuple_SET_ITEM(args, pos, o);
 
520
        o = NULL;
 
521
        pos++;
 
522
    }
 
523
 
 
524
    va_end(ap);
 
525
 
 
526
    p = PyObject_GetAttrString(cb, method_name);
 
527
    if (p) {
 
528
        o = PyObject_CallObject(p, args);
 
529
        Py_DECREF(p);
 
530
        if (o)
 
531
            Py_DECREF(o);
 
532
    }
 
533
    Py_DECREF(args);
 
534
    return;
 
535
 
 
536
 out_err:
 
537
    if (o) {
 
538
        Py_DECREF(o);
 
539
    }
 
540
}
 
541
 
 
542
%}