[C++-sig] Exposing base classes in different Boost.Python modules

Roman Yakovenko roman.yakovenko at gmail.com
Thu Jul 26 06:54:18 CEST 2007


On 7/25/07, Lawrence Spector <Lawrence.Spector at canfieldsci.com> wrote:
>
>
>
>
> Hello again.  I'm running into another issue.  Based on the Boost.Python
> tutorial, I have set up a hierarchy to mimic the namespaces that are in our
> actual C++ code, using a few tricks from the site, including making the
> module name start with an _ and then the __init__.py trick to make the name
> what I want.
>
>
>  That being said, due to this approach I have multiple modules, since some
> modules are in different namespaces.  Here's a situation I hit today:
>
>
>
> namespace MyNs
>
> {
>
>
>
> class MyStream : public std::wostream
>
> {
>
>>  };
>
>
>
> } // end MyNs
>
>
>
> Now, in the Boost.Python source file that is compiled to be a Python
> extension, I expose the class, but apparently I also need to say that
> std::wostream is a base if I want it to be used polymorphically.  So, I do
> so, like this:
>
>
>
> bp::class<MyNs::MyStream, bp::bases<std::wostream>,
> boost::noncopyable>("MyStream")
>
>  … // etc…
>
>
>
> It builds fine, but when I import I get a python RuntimeError as the
> following:
>
>
>
> RuntimeError: extension class wrapper for base class class
> std::basic_ostream<wchar_t, struct std::char_traits<wchar_t> > has not been
> created yet.
>
>
>  Fine, this makes sense.  However, if I'm going to expose this, I want to
> make a different module called std, so it's in the right namespace.  Is
> there a way to tell Boost.Python to automatically import this one before my
> other module, so it knows what to do with std::wostream or another way to
> make this work?

No, such way doesn't exists. You can create package for every ext.
module and import the dependencies within it or you can mess with C
API and import module before you start to expose your classes.

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list