def_readonly and const array members
I am trying to wrap structs that have character arrays and I'm having problems since I moved from MSVC 7 to MSVC 7.1 (VS .NET 2003). Here is the struct I'm trying to expose the theArray member of using class_::def_readonly: struct testStruct { unsigned char theArray[16]; }; I've created a to_python converter for the unsigned char [16] type but I'm getting a "TypeError: No to_python (by-value) converter found for C++ type: unsigned char const volatile [8]" when I try to access the theArray member in Python. I've tried putting volatile in my typedef for the array, but that doesn't seem to help (how C++ binds these type modifiers always confuses me so maybe I put it in the wrong place). Thank you for the help, Jeff Brewer jeff@purplemagma.com p.s. The full code follows: Python code: import test theStruct = test.getStruct() print theStruct.theArray C++ code: typedef unsigned char uchar16array[16]; struct uchar16array_to_python { static PyObject *convert(uchar16array const &value) { PyObject *result = PyTuple_New(16); for (int i = 0; i < 16; i++) PyTuple_SetItem(result,i,PyInt_FromLong(value[i])); return result; } }; struct testStruct { unsigned char theArray[16]; }; testStruct getStruct() { static testStruct theStruct = {{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}}; return theStruct; } BOOST_PYTHON_MODULE(test) { to_python_converter<uchar16array, uchar16array_to_python>(); class_<testStruct>("testStruct", init<>()) .def_readonly("theArray", &testStruct::theArray); def("getStruct", getStruct); }
--- Jeff Brewer <jeff@brewer.com> wrote:
I am trying to wrap structs that have character arrays and I'm having problems since I moved from MSVC 7 to MSVC 7.1 (VS .NET 2003). Here is the struct I'm trying to expose the theArray member of using class_::def_readonly:
Are you also moving to a more recent boost?
.def_readonly("theArray", &testStruct::theArray);
Try this instead: #include <boost/python/return_value_policy.hpp> #include <boost/python/return_by_value.hpp> .add_property("theArray", make_getter(&testStruct::theArray, return_value_policy<return_by_value>())); If you want to support both Boost 1.29 and never versions: #include <boost/version.hpp> #if BOOST_VERSION >= 103000 // use add_property #else // use def_readonly or def_readwrite #endif Ralf __________________________________ Do you Yahoo!? Yahoo! SiteBuilder - Free, easy-to-use web site design software http://sitebuilder.yahoo.com
"Jeff Brewer" <jeff@brewer.com> writes:
I am trying to wrap structs that have character arrays and I'm having problems since I moved from MSVC 7 to MSVC 7.1 (VS .NET 2003). Here is the struct I'm trying to expose the theArray member of using class_::def_readonly:
struct testStruct { unsigned char theArray[16]; };
I've created a to_python converter for the unsigned char [16] type but I'm getting a "TypeError: No to_python (by-value) converter found for C++ type: unsigned char const volatile [8]" when I try to access the theArray member in Python. I've tried putting volatile in my typedef for the array, but that doesn't seem to help (how C++ binds these type modifiers always confuses me so maybe I put it in the wrong place).
Thank you for the help,
Jeff Brewer jeff@purplemagma.com
Jeff, the problem appears to be a bug in MSVC 7.1, namely that typeid(unsigned char[16]) != typeid(unsigned char const volatile[16]) As it should be. I'm looking into a workaround now. -- Dave Abrahams Boost Consulting www.boost-consulting.com
David Abrahams <dave@boost-consulting.com> writes:
"Jeff Brewer" <jeff@brewer.com> writes:
I am trying to wrap structs that have character arrays and I'm having problems since I moved from MSVC 7 to MSVC 7.1 (VS .NET 2003). Here is the struct I'm trying to expose the theArray member of using class_::def_readonly:
struct testStruct { unsigned char theArray[16]; };
I've created a to_python converter for the unsigned char [16] type but I'm getting a "TypeError: No to_python (by-value) converter found for C++ type: unsigned char const volatile [8]" when I try to access the theArray member in Python. I've tried putting volatile in my typedef for the array, but that doesn't seem to help (how C++ binds these type modifiers always confuses me so maybe I put it in the wrong place).
Thank you for the help,
Jeff Brewer jeff@purplemagma.com
Jeff, the problem appears to be a bug in MSVC 7.1, namely that
typeid(unsigned char[16]) != typeid(unsigned char const volatile[16])
As it should be. I'm looking into a workaround now.
Done and checked in. Here's the patch to boost/python/registered.hpp: --- registered.hpp.~1.2.~ 2002-08-14 02:26:32.000000000 -0400 +++ registered.hpp 2003-08-06 09:49:09.000000000 -0400 @@ -10,6 +10,7 @@ # include <boost/python/converter/registrations.hpp> # include <boost/type_traits/transform_traits.hpp> # include <boost/type_traits/cv_traits.hpp> +# include <boost/detail/workaround.hpp> namespace boost { namespace python { namespace converter { @@ -34,10 +35,15 @@ { }; -# ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION -// collapses a few more types to the same static instance +# if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) \ + && !BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1310)) +// collapses a few more types to the same static instance. MSVC7.1 +// fails to strip cv-qualification from array types in typeid. For +// some reason we can't use this collapse there or array converters +// will not be found. template <class T> -struct registered<T&> : registered<T> {}; +struct registered<T&> + : registered<T> {}; # endif // -- Dave Abrahams Boost Consulting www.boost-consulting.com
"Jeff Brewer" <jeff@brewer.com> writes:
I am trying to wrap structs that have character arrays and I'm having problems since I moved from MSVC 7 to MSVC 7.1 (VS .NET 2003). Here is the struct I'm trying to expose the theArray member of using class_::def_readonly:
struct testStruct { unsigned char theArray[16]; };
I've created a to_python converter for the unsigned char [16] type but I'm getting a "TypeError: No to_python (by-value) converter found for C++ type: unsigned char const volatile [8]" when I try to access the theArray member in Python. I've tried putting volatile in my typedef for the array, but that doesn't seem to help (how C++ binds these type modifiers always confuses me so maybe I put it in the wrong place).
Thank you for the help,
Jeff Brewer jeff@purplemagma.com
Jeff, the problem appears to be a bug in MSVC 7.1, namely that typeid(unsigned char[16]) != typeid(unsigned char const volatile[16]) As it should be. I'm looking into a workaround now. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (3)
-
David Abrahams -
Jeff Brewer -
Ralf W. Grosse-Kunstleve