Boost Python properties/getter functions for strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shawn  McGrath

    #1

    Boost Python properties/getter functions for strings

    Hi, I'm trying to expose a C++ class' internals to python via
    boost::python. I can do integer/boolean functions fine, but as soon
    as I do a string get/set it craps out.

    Code:
    boost::python::class_<Entity, std::auto_ptr<pyEntity("Entity")
    //publics
    .def("isActive", &Entity::isActive) //bool
    .def("activate", &Entity::activate) //bool
    .def("deactivate", &Entity::deactivate) //bool
    //...
    .add_property("name", &Entity::getName) //compile error (1)
    .def("getName", &Entity::getName,
    boost::python::return_internal_reference<>()); //runtime error(2)
    
    
    Compile error (1) shows this: C:/MinGW/include/boost/python/detail/
    invoke.hpp: In function `PyObject*
    boost::python::detail::invoke(boost::python::detail::invoke_tag_<
    false,  true>, const RC&, F&, TC&) [with RC =
    boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<const
    std::string&>, F = const std::string&(rsblsb::Entity::*)() const, TC =
    boost::python::arg_from_python<rsblsb::Entity&>]':
    C:/MinGW/include/boost/python/detail/caller.hpp:199:   instantiated
    from `PyObject* boost::python::detail::caller_arity<1u>::impl<F,
    Policies, Sig>::operator()(PyObject*, PyObject*) [with F = const
    std::string&(rsblsb::Entity::*)() const, Policies =
    boost::python::default_call_policies, Sig = boost::mpl::vector2<const
    std::string&, rsblsb::Entity&>]'
    C:/MinGW/include/boost/python/object/py_function.hpp:38:
    instantiated from `PyObject*
    boost::python::objects::caller_py_function_impl<Caller>::operator()
    (PyObject*, PyObject*) [with Caller =
    boost::python::detail::caller<const std::string&(rsblsb::Entity::*)()
    const, boost::python::default_call_policies, boost::mpl::vector2<const
    std::string&, rsblsb::Entity&]'
    C:\Game\svn\Platform\Framework\Python\PyModuleSetup.cc:58:
    instantiated from here
    C:/MinGW/include/boost/python/detail/invoke.hpp:88: error: no match
    for call to `(const
    boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<const
    std::string&>) (const std::basic_string<char, std::char_traits<char>,
    std::allocator<char&)'
    Runtime error 2 just crashes whenever I try:
    import modulename
    I = modulename.Enti ty()
    I.getName()

    Anyone have any idea what I can try? thanks a lot!

    -Shawn.
    Last edited by zmbd; Apr 8 '14, 04:02 PM.
  • Shawn  McGrath

    #2
    Re: Boost Python properties/getter functions for strings

    I forgot to mention, getname is defined as:
    const std::string &Entity::getNam e() const;

    Comment

    • Shawn  McGrath

      #3
      Re: Boost Python properties/getter functions for strings

      On Mar 19, 12:00 pm, "Shawn McGrath" <shawn.mcgr...@ gmail.comwrote:
      I forgot to mention, getname is defined as:
      const std::string &Entity::getNam e() const;
      After more reading I found the copy_const_refe rence, and replaced:
      boost::python:: return_internal _reference<>()) ;
      with:

      boost::python:: return_value_po licy<boost::pyt hon::copy_const _reference>());

      and it fixed my problem. Is there any downside to using
      copy_const_refe rence over return_internal _reference?

      Thanks,
      Shawn.

      Comment

      • Jon Clements

        #4
        Re: Boost Python properties/getter functions for strings

        On 19 Mar, 16:40, "Shawn McGrath" <shawn.mcgr...@ gmail.comwrote:
        On Mar 19, 12:00 pm, "Shawn McGrath" <shawn.mcgr...@ gmail.comwrote:
        >
        I forgot to mention, getname is defined as:
        const std::string &Entity::getNam e() const;
        >
        After more reading I found the copy_const_refe rence, and replaced:
        boost::python:: return_internal _reference<>()) ;
        with:
        >
        boost::python:: return_value_po licy<boost::pyt hon::copy_const _reference>());
        >
        and it fixed my problem. Is there any downside to using
        copy_const_refe rence over return_internal _reference?
        You might get some answers here; if not, can I suggest
        http://mail.python.org/mailman/listinfo/c++-sig ? I think a lot of the
        Boost.Python developers hang around on that list.


        hth,

        Jon.


        Comment

        • Shawn  McGrath

          #5
          Re: Boost Python properties/getter functions for strings

          On Mar 19, 12:49 pm, "Jon Clements" <jon...@googlem ail.comwrote:
          On 19 Mar, 16:40, "Shawn McGrath" <shawn.mcgr...@ gmail.comwrote:
          >
          On Mar 19, 12:00 pm, "Shawn McGrath" <shawn.mcgr...@ gmail.comwrote:
          >
          I forgot to mention, getname is defined as:
          const std::string &Entity::getNam e() const;
          >
          After more reading I found the copy_const_refe rence, and replaced:
          boost::python:: return_internal _reference<>()) ;
          with:
          >
          boost::python:: return_value_po licy<boost::pyt hon::copy_const _reference>());
          >
          and it fixed my problem. Is there any downside to using
          copy_const_refe rence over return_internal _reference?
          >
          You might get some answers here; if not, can I suggesthttp://mail.python.org/mailman/listinfo/c++-sig? I think a lot of the
          Boost.Python developers hang around on that list.
          >
          hth,
          >
          Jon.
          Cool thanks a lot.

          The problem is actually due to python's strings being immutable (I
          knew this, but I thought returning const std::string& would do it).
          return_internal _reference<work s for other pointers/references, just
          not strings.

          (I just answered it so if it gets searched later on people will find
          the solution)

          -Shawn.

          Comment

          Working...