Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8481631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:36:31+00:00 2026-06-10T19:36:31+00:00

I have run into a problem when creating python bindings for an existing library

  • 0

I have run into a problem when creating python bindings for an existing library with boost.python. The scenario is as follows:

#include<boost/python.hpp>

namespace bp = boost::python;

struct Base {
    std::stringstream _myString;
    Base() { };
    Base(const Base& base) { _myString<<base._myString.str(); }

    void getString(std::stringstream& some_string) {
        _myString.str("");
        _myString<<some_string.str();
        std::cout<<"Got string: \""<<_myString.str()<<"\""<<std::endl;
    }
};

struct BaseWrapper : Base,
                     bp::wrapper<Base>
{
    BaseWrapper() :
        Base(),
        bp::wrapper<Base>() { };

    BaseWrapper(const Base& base) :
        Base(base),
        bp::wrapper<Base>() { };

    void getString(bp::object pyObj) {
        std::string strLine = bp::extract<std::string>(pyObj);
        std::stringstream sstrLine;
        sstrLine<<strLine;
        Base::getString(sstrLine);
    }
};

struct Derived : Base
{
    Derived() : Base() { };
    Derived(const Derived& derived) : Base() { _myString<<derived._myString.str(); };
};

struct DerivedWrapper : Derived,
                        bp::wrapper<Derived>
{
    DerivedWrapper() :
        Derived(),
        bp::wrapper<Derived>() { };

    DerivedWrapper(const Derived derived) :
        Derived(derived),
        bp::wrapper<Derived>() { };
};

BOOST_PYTHON_MODULE(testInheritance){
    bp::class_<BaseWrapper>("Base")
        .def("getString", &BaseWrapper::getString);

    bp::class_<DerivedWrapper, bp::bases<Base> >("Derived");
}

(Sorry for the long code block, it was the minimum example I could think of.)

You can see that I had to override getString() method in the BaseWrapper so that it would work with Python strings and this part works fine:

>>> import testInheritance
>>> base = testInheritance.Base()
>>> base.getString("bla")
Got string: "bla"
>>>

The problem appears as soon as I try to call getString from a instance of Derived:

>>> derived = testInheritance.Derived()
>>> derived.getString("bla")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
    Base.getString(Derived, str)
did not match C++ signature:
    getString(BaseWrapper {lvalue}, boost::python::api::object)
>>>

I can understand what is going wrong here, but I have no idea how to fix that. I would appreciate any help!

Best Regards,
eDude

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:36:33+00:00Added an answer on June 10, 2026 at 7:36 pm

    The problem is, that DerivedWrapper has no relationship to BaseWrapper. Thus DerivedWrapper needs to provide its own python adapted implementation of void getString(bp::object pyObj).

    So one way to make it work is like this:

    struct DerivedWrapper : Derived,
                            bp::wrapper<Derived>
    {
        DerivedWrapper() :
            Derived(),
            bp::wrapper<Derived>() { };
    
        DerivedWrapper(const Derived derived) :
            Derived(derived),
            bp::wrapper<Derived>() { };
    
        void getString(bp::object pyObj) {
            std::string strLine = bp::extract<std::string>(pyObj);
            std::stringstream sstrLine;
            sstrLine<<"(from DerivedWrapper) "<<strLine;
            Derived::getString(sstrLine);
        }
    };
    
    [...]
    
        bp::class_<DerivedWrapper, bp::bases<Base> >("Derived")
            .def("getString", &DerivedWrapper::getString);
    

    And the output of

    base = testInheritance.Base()
    base.getString("bla")
    
    derived = testInheritance.Derived()
    derived.getString("blub")
    

    is as expected

    Got string: "bla"
    Got string: "(from DerivedWrapper) blub"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run into a strange problem, i am creating a lot of dynamically
I'm creating a responsive design but have run into a problem where the Facebook
I'm creating a simple shop, but have run into a simple problem.. I've got
I am creating a minesweeper game in python 2.7. I have run into several
I am creating a drawing app and have run into a problem. I have
I'm creating a COM server executable, and have run into a problem with class
I am creating a .bash_profile script, and I have run into a small problem.
I'm creating a file-browser in ASP.Net but I have run into a problem which
I am creating pivot tables in VB.NET and have run into a problem I
I am creating a WP-theme, and run into a problem. I want to have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.