Commit 01b8d449 authored by Jan Möbius's avatar Jan Möbius

Convert tuple and list to Vector for python

parent 440acd7d
......@@ -117,23 +117,35 @@ namespace pybind11 { namespace detail {
/* Extract PyObject from handle */
PyObject* source = src.ptr();
if (! PyList_Check(source) ) {
PyErr_SetString(PyExc_RuntimeError, "Vector Conversion: Not a list.");
throw py::error_already_set();
return false;
}
if ( PyList_Check(source) ) {
if ( PyList_Size(source) != 3) {
PyErr_SetString(PyExc_RuntimeError, "Vector Conversion: List size should be 3!");
throw py::error_already_set();
return false;
}
/* Now try to convert into a C++ int */
value = Vector(PyFloat_AsDouble(PyList_GetItem(source,0)),PyFloat_AsDouble(PyList_GetItem(source,1)),PyFloat_AsDouble(PyList_GetItem(source,2)));
} else if ( PyTuple_Check(source) ) {
if ( PyTuple_Size(source) != 3) {
PyErr_SetString(PyExc_RuntimeError, "Vector Conversion: Tuple size should be 3!");
throw py::error_already_set();
return false;
}
if ( PyList_Size(source) == 3) {
PyErr_SetString(PyExc_RuntimeError, "Vector Conversion: Size should be 3!");
/* Now try to convert into a C++ int */
value = Vector(PyFloat_AsDouble(PyTuple_GetItem(source,0)),PyFloat_AsDouble(PyTuple_GetItem(source,1)),PyFloat_AsDouble(PyTuple_GetItem(source,2)));
} else {
PyErr_SetString(PyExc_RuntimeError, "Vector Conversion: Not a list or a tuple.");
throw py::error_already_set();
return false;
}
/* Now try to convert into a C++ int */
value = Vector(PyFloat_AsDouble(PyList_GetItem(source,0)),PyFloat_AsDouble(PyList_GetItem(source,1)),PyFloat_AsDouble(PyList_GetItem(source,2)));
/* Ensure return code was OK (to avoid out-of-range errors etc) */
return ( !PyErr_Occurred() );
/* Ensure return code was OK (to avoid out-of-range errors etc) */
return ( !PyErr_Occurred() );
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment