diff --git a/include/boost/python/detail/module_base.hpp b/include/boost/python/detail/module_base.hpp index e8a00ba6..043a6f47 100644 --- a/include/boost/python/detail/module_base.hpp +++ b/include/boost/python/detail/module_base.hpp @@ -26,6 +26,9 @@ class BOOST_PYTHON_DECL module_base // Return a reference to the Python module object being built inline ref object() const; + protected: + void generic_add_class(ref class_obj); + private: ref m_module; static PyMethodDef initial_methods[1]; diff --git a/include/boost/python/module.hpp b/include/boost/python/module.hpp index c05d1032..41286ac2 100644 --- a/include/boost/python/module.hpp +++ b/include/boost/python/module.hpp @@ -31,8 +31,7 @@ class module : public detail::module_base template module& add(class_ const& c) { - Py_INCREF(c.object()); - this->add_type(c.object()); + this->generic_add_class(c.object()); return *this; } diff --git a/src/module.cpp b/src/module.cpp index 0291c9c1..5020170c 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -45,6 +45,17 @@ void module_base::add_type(ref x) add((PyTypeObject*)x.release()); } +void module_base::generic_add_class(ref class_obj) +{ + Py_INCREF(class_obj.get()); + this->add_type(class_obj); + ref module_name(PyObject_GetAttrString(m_module.get(), + const_cast("__name__"))); + int status = PyObject_SetAttrString(class_obj.get(), + const_cast("__module__"), module_name.get()); + if (status == -1) throw_error_already_set(); +} + PyMethodDef module_base::initial_methods[] = { { 0, 0, 0, 0 } }; }}} // namespace boost::python::detail