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.
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.
14 %fragment(SWIG_AsVal_frag(long), "header") {
17 SWIG_AsVal(long)(PyObject* obj, long* val)
19 if (PyNumber_Check(obj)) {
20 if (val) *val = PyInt_AsLong(obj);
24 PyObject* errmsg = PyString_FromFormat("Expected number, got %s",
25 obj->ob_type->tp_name);
26 PyErr_SetObject(PyExc_TypeError, errmsg);
34 %fragment(SWIG_AsVal_frag(unsigned long), "header",
35 fragment=SWIG_AsVal_frag(long)) {
38 SWIG_AsVal(unsigned long)(PyObject* obj, unsigned long* val)
41 if (SWIG_AsVal_long(obj, &v) && v < 0) {
42 PyErr_SetString(PyExc_TypeError, "negative value received for unsigned type");
46 *val = (unsigned long)v;
52 %fragment(SWIG_AsVal_frag(double), "header") {
55 SWIG_AsVal(double)(PyObject *obj, double* val)
57 if (PyNumber_Check(obj)) {
58 if (val) *val = PyFloat_AsDouble(obj);
62 PyObject* errmsg = PyString_FromFormat("Expected number, got %s",
63 obj->ob_type->tp_name);
64 PyErr_SetObject(PyExc_TypeError, errmsg);