GeezerVison is showing github.com — automatic addition of __module__ to class dict · mborgerding/python@7a832f1
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: include/boost/python/detail/module_base.hpp +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,6 +26,9 @@ class BOOST_PYTHON_DECL module_base |
26 | 26 | // Return a reference to the Python module object being built |
27 | 27 | inline ref object() const; |
28 | 28 |
|
| 29 | + protected: |
| 30 | + void generic_add_class(ref class_obj); |
| 31 | + |
29 | 32 | private: |
30 | 33 | ref m_module; |
31 | 34 | static PyMethodDef initial_methods[1]; |
|
Collapse file
Copy file name to clipboardExpand all lines: include/boost/python/module.hpp +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,8 +31,7 @@ class module : public detail::module_base |
31 | 31 | template <class T, class Bases, class HolderGenerator> |
32 | 32 | module& add(class_<T,Bases,HolderGenerator> const& c) |
33 | 33 | { |
34 | | - Py_INCREF(c.object()); |
35 | | - this->add_type(c.object()); |
| 34 | + this->generic_add_class(c.object()); |
36 | 35 | return *this; |
37 | 36 | } |
38 | 37 |
|
|
Collapse file
Copy file name to clipboardExpand all lines: src/module.cpp +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,6 +45,17 @@ void module_base::add_type(ref x) |
45 | 45 | add((PyTypeObject*)x.release()); |
46 | 46 | } |
47 | 47 |
|
| 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 | + |
48 | 59 | PyMethodDef module_base::initial_methods[] = { { 0, 0, 0, 0 } }; |
49 | 60 |
|
50 | 61 | }}} // namespace boost::python::detail |