Skip to content

Commit c76b063

Browse files
committed
ENH: add conversion utility extractdouble
Handle numpy.int types which have no built-in converters in boost python.
1 parent efb45d4 commit c76b063

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/extensions/srreal_converters.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,25 @@ NumPyArray_DoublePtr extractNumPyDoubleArray(::boost::python::object& obj)
294294
}
295295

296296

297+
/// extract double with a support for numpy.int types
298+
double extractdouble(boost::python::object obj)
299+
{
300+
using namespace boost;
301+
python::extract<double> getx(obj);
302+
if (getx.check()) return getx();
303+
PyObject* pobj = obj.ptr();
304+
if (PyArray_CheckScalar(pobj))
305+
{
306+
double x;
307+
PyArray_CastScalarToCtype(pobj, &x,
308+
PyArray_DescrFromType(NPY_DOUBLE));
309+
return x;
310+
}
311+
// nothing worked, call getx which will raise an exception
312+
return getx();
313+
}
314+
315+
297316
/// extract integer with a support for numpy.int types
298317
int extractint(boost::python::object obj)
299318
{

src/extensions/srreal_converters.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,10 @@ extractQuantityType(::boost::python::object obj,
395395
NumPyArray_DoublePtr extractNumPyDoubleArray(::boost::python::object& obj);
396396

397397

398+
/// extract double with a support for numpy numeric types
399+
double extractdouble(::boost::python::object obj);
400+
401+
398402
/// extract integer with a support for numpy.int types
399403
int extractint(::boost::python::object obj);
400404

0 commit comments

Comments
 (0)