]>
Commit | Line | Data |
---|---|---|
0190c472 RD |
1 | // These fragments are inserted in modules that need to convert PyObjects to |
2 | // integer values, my versions allow any numeric type to be used, as long as | |
3 | // it can be converted to a PyInt. (Specifically, I allow floats where the | |
4 | // default SWIG_AsVal_long would just raise an exception. | |
5 | // | |
6 | // NOTE: This file has to be %included very early in the SWIGging process as | |
7 | // it no longer allows existing fragments to be replaced with one of the same | |
8 | // name. So to make this work I had to bring a copy of python.swg into this | |
9 | // project and do the %include there before most other of the standard swiglib | |
10 | // files are %included. This may change in 1.3.23, so adjust accordingly then. | |
11 | ||
12 | ||
13 | ||
14 | %fragment(SWIG_AsVal_frag(long), "header") { | |
15 | // See my_fragments.i | |
16 | SWIGSTATICINLINE(int) | |
17 | SWIG_AsVal(long)(PyObject* obj, long* val) | |
18 | { | |
19 | if (PyNumber_Check(obj)) { | |
20 | if (val) *val = PyInt_AsLong(obj); | |
21 | return 1; | |
22 | } | |
23 | else { | |
24 | PyObject* errmsg = PyString_FromFormat("Expected number, got %s", | |
25 | obj->ob_type->tp_name); | |
26 | PyErr_SetObject(PyExc_TypeError, errmsg); | |
27 | Py_DECREF(errmsg); | |
28 | } | |
29 | return 0; | |
30 | } | |
31 | } | |
32 | ||
33 | ||
34 | %fragment(SWIG_AsVal_frag(unsigned long), "header", | |
35 | fragment=SWIG_AsVal_frag(long)) { | |
36 | // See my_fragments.i | |
37 | SWIGSTATICINLINE(int) | |
38 | SWIG_AsVal(unsigned long)(PyObject* obj, unsigned long* val) | |
39 | { | |
40 | long v = 0; | |
41 | if (SWIG_AsVal_long(obj, &v) && v < 0) { | |
42 | PyErr_SetString(PyExc_TypeError, "negative value received for unsigned type"); | |
43 | return 0; | |
44 | } | |
45 | else if (val) | |
46 | *val = (unsigned long)v; | |
47 | return 1; | |
48 | } | |
49 | } | |
50 | ||
51 | ||
52 | %fragment(SWIG_AsVal_frag(double), "header") { | |
53 | // See my_fragments.i | |
54 | SWIGSTATICINLINE(int) | |
55 | SWIG_AsVal(double)(PyObject *obj, double* val) | |
56 | { | |
57 | if (PyNumber_Check(obj)) { | |
58 | if (val) *val = PyFloat_AsDouble(obj); | |
59 | return 1; | |
60 | } | |
61 | else { | |
62 | PyObject* errmsg = PyString_FromFormat("Expected number, got %s", | |
63 | obj->ob_type->tp_name); | |
64 | PyErr_SetObject(PyExc_TypeError, errmsg); | |
65 | Py_DECREF(errmsg); | |
66 | } | |
67 | return 0; | |
68 | } | |
69 | } |