From: Ovidiu Mircescu Date: Wed, 9 Oct 2019 14:03:39 +0000 (+0200) Subject: Conversion from py tuple to std list and vector. X-Git-Tag: v2.0^0 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=5b7c1510615d5742238d36cbab7cf0a88f36b781;p=tools%2Fpy2cpp.git Conversion from py tuple to std list and vector. --- diff --git a/src/Test/ConversionTest.cxx b/src/Test/ConversionTest.cxx index c54e549..edfa6ee 100644 --- a/src/Test/ConversionTest.cxx +++ b/src/Test/ConversionTest.cxx @@ -115,6 +115,11 @@ void ConversionTest::vectorTest() py2cpp::pyResult(result) = fn(v); CPPUNIT_ASSERT(std::vector({2.1, 3.2, 4.3, 5.4}) == result); + + auto tup = std::make_tuple(4, 4.2); + py2cpp::PyPtr pyTup = py2cpp::toPyPtr(tup); + py2cpp::fromPyPtr(pyTup, result); + CPPUNIT_ASSERT(std::vector({4, 4.2}) == result); } void ConversionTest::listTest() @@ -129,6 +134,11 @@ void ConversionTest::listTest() py2cpp::pyResult(result) = fn(v); CPPUNIT_ASSERT(std::list({2.1, 3.2, 4.3, 5.4}) == result); + + auto tup = std::make_tuple(4, 4.2); + py2cpp::PyPtr pyTup = py2cpp::toPyPtr(tup); + py2cpp::fromPyPtr(pyTup, result); + CPPUNIT_ASSERT(std::list({4, 4.2}) == result); } void ConversionTest::pyobjTest() diff --git a/src/TypeConversions.hxx b/src/TypeConversions.hxx index 9e04d57..48ec9da 100644 --- a/src/TypeConversions.hxx +++ b/src/TypeConversions.hxx @@ -210,6 +210,16 @@ ConversionCheck fromPy( PyObject *obj, std::vector& result) if(!check) check.addError("std::vector", obj); } + else if(PyTuple_Check(obj)) + { + result.clear(); + std::size_t size = PyTuple_Size(obj); + result.resize(size); + for(std::size_t i=0; i < size && check; i++) + check.addError(fromPy(PyTuple_GetItem(obj, i), result[i])); + if(!check) + check.addError("std::vector", obj); + } else check.addError("std::vector", obj); return check; @@ -236,6 +246,23 @@ ConversionCheck fromPy( PyObject *obj, std::list& result) ++i; } } + else if(PyTuple_Check(obj)) + { + result.clear(); + std::size_t size = PyTuple_Size(obj); + result.resize(size); //result will have "size" default constructed elements. + std::size_t i = 0; + for(T& it : result) + { + check.addError(fromPy(PyTuple_GetItem(obj, i), it)); + if(!check) + { + check.addError("std::list", obj); + break; + } + ++i; + } + } else check.addError("std::list", obj); return check;