From 8470ed1a3ab8b9083d31d79aa36d2a5e76f94126 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Tue, 30 Mar 2004 19:50:53 +0000 Subject: [PATCH] Better conversion functions for integral and floating point types, allows truncated floats to be used where integers are expeceted, raises more meaningful exceptions, etc. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@26502 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- wxPython/src/my_typemaps.i | 56 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/wxPython/src/my_typemaps.i b/wxPython/src/my_typemaps.i index b130f580be..78ea572224 100644 --- a/wxPython/src/my_typemaps.i +++ b/wxPython/src/my_typemaps.i @@ -273,6 +273,62 @@ } +// These fragments are iserted in modules that need to convert PyObjects to +// integer values, my versions allow any numeric type to be used, as long as +// it can be converted to a PyInt. (Specifically, I allow floats where the +// default SWIG_AsLong would raise an obsucre exception from within +// PyLong_AsLong.) + +%fragment("SWIG_AsLong","header") %{ +SWIGSTATICINLINE(long) +SWIG_AsLong(PyObject * obj) +{ + if (PyNumber_Check(obj)) + return PyInt_AsLong(obj); + else { + PyObject* errmsg = PyString_FromFormat("Expected number, got %s", + obj->ob_type->tp_name); + PyErr_SetObject(PyExc_TypeError, errmsg); + Py_DECREF(errmsg); + return 0; + } +} +%} + +%fragment("SWIG_AsUnsignedLong","header", fragment="SWIG_AsLong") %{ +SWIGSTATICINLINE(unsigned long) +SWIG_AsUnsignedLong(PyObject * obj) +{ + if (PyLong_Check(obj)) { + return PyLong_AsUnsignedLong(obj); + } else { + long i = SWIG_AsLong(obj); + if ( !PyErr_Occurred() && (i < 0)) { + PyErr_SetString(PyExc_TypeError, "negative value received for unsigned type"); + } + return i; + } +} +%} + + +%fragment("SWIG_AsDouble","header") %{ +SWIGSTATICINLINE(double) +SWIG_AsDouble(PyObject *obj) +{ + if (PyNumber_Check(obj)) + return PyFloat_AsDouble(obj); + else { + PyObject* errmsg = PyString_FromFormat("Expected number, got %s", + obj->ob_type->tp_name); + PyErr_SetObject(PyExc_TypeError, errmsg); + Py_DECREF(errmsg); + return 0; + } +} +%} + + //--------------------------------------------------------------------------- // Typemap for when GDI objects are returned by reference. This will cause a // copy to be made instead of returning a reference to the same instance. The -- 2.45.2