Skip to content

Commit 7a832f1

Browse files
author
Ralf W. Grosse-Kunstleve
committed
automatic addition of __module__ to class dict
[SVN r14072]
1 parent 16c391c commit 7a832f1

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

include/boost/python/detail/module_base.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class BOOST_PYTHON_DECL module_base
2626
// Return a reference to the Python module object being built
2727
inline ref object() const;
2828

29+
protected:
30+
void generic_add_class(ref class_obj);
31+
2932
private:
3033
ref m_module;
3134
static PyMethodDef initial_methods[1];

include/boost/python/module.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ class module : public detail::module_base
3131
template <class T, class Bases, class HolderGenerator>
3232
module& add(class_<T,Bases,HolderGenerator> const& c)
3333
{
34-
Py_INCREF(c.object());
35-
this->add_type(c.object());
34+
this->generic_add_class(c.object());
3635
return *this;
3736
}
3837

src/module.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ void module_base::add_type(ref x)
4545
add((PyTypeObject*)x.release());
4646
}
4747

48+
void module_base::generic_add_class(ref class_obj)
49+
{
50+
Py_INCREF(class_obj.get());
51+
this->add_type(class_obj);
52+
ref module_name(PyObject_GetAttrString(m_module.get(),
53+
const_cast<char*>("__name__")));
54+
int status = PyObject_SetAttrString(class_obj.get(),
55+
const_cast<char*>("__module__"), module_name.get());
56+
if (status == -1) throw_error_already_set();
57+
}
58+
4859
PyMethodDef module_base::initial_methods[] = { { 0, 0, 0, 0 } };
4960

5061
}}} // namespace boost::python::detail

0 commit comments

Comments
 (0)