[C++-sig] sub module support in V2

Dave Hawkes daveh at cadlink.com
Wed Jun 5 17:51:23 CEST 2002


It would seem that it would be very easy to add sub module support to boost
python V2. This is useful for splitting up a large API into separate modules
even though they are all defined in the same executable. A large API occurs
more frequently when python is embedded in another large application and
this can save creating a myriad of small libraries.

As an example I tested the appended code (on Windows) and it appears to work
without problem, However it would be better if sub_module code was
integrated with the module code.

Would it be possible to add this to V2?
I can modify the module class and submit it if desired.


Dave Hawkes


#include <boost/python/module.hpp>

int Test1()
{
  return 1;
}

int Test2()
{
  return 2;
}

namespace boost { namespace python {

class sub_module : public module {
public:
  sub_module(const char* name) : module(name) {}
  sub_module(module& parent, const char* name) : module(name) {
    parent.setattr(name, object());
  }
  sub_module sub(const char* name) {
    return sub_module(*this, name);
  }
  template <class Fn>
  sub_module& def(char const* name, Fn fn) {
    this->module::def(name, fn);
    return  *this;
  }
};

}}

BOOST_PYTHON_MODULE_INIT(PyTest)
{
  boost::python::sub_module("PyTest").def("Test1",
Test1).sub("sm").def("Test2", Test2);
}



In python:

import PyTest
print PyTest.sm.Test2()










More information about the Cplusplus-sig mailing list