Skip to content

Commit

Permalink
Use PyModule_New in module_ public constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
YannickJadoul committed Oct 18, 2020
1 parent 0b9acc4 commit b479f2a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
4 changes: 3 additions & 1 deletion include/pybind11/detail/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@ extern "C" {
.. code-block:: cpp
static pybind11::module_::module_def example_module_def;
PYBIND11_PLUGIN(example) {
pybind11::module_ m("example", "pybind11 example plugin");
auto m = pybind11::module_::create_extension_module(
"example", "pybind11 example plugin", &example_module_def);
/// Set up bindings here
return m.ptr();
}
Expand Down
19 changes: 14 additions & 5 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -883,19 +883,28 @@ class cpp_function : public function {
}
};

/// Wrapper for Python extension modules
/// Wrapper for Python modules
class module_ : public object {
public:
PYBIND11_OBJECT_DEFAULT(module_, object, PyModule_Check)

/// Create a new top-level Python module with the given name and docstring
PYBIND11_DEPRECATED("Use PYBIND11_MODULE or module_::create_extension_module instead")
explicit module_(const char *name, const char *doc = nullptr) {
#if PY_MAJOR_VERSION >= 3
*this = create_extension_module(name, doc, new PyModuleDef());
#if defined(PYPY_VERSION)
m_ptr = PyModule_New(const_cast<char *>(name));
#else
*this = create_extension_module(name, doc, nullptr);
m_ptr = PyModule_New(name);
#endif
if (m_ptr == nullptr)
pybind11_fail("Internal error in module_::module_()");
if (doc && options::show_user_defined_docstrings()) {
#if PY_MAJOR_VERSION >= 3 && !defined(PYPY_VERSION)
if (PyModule_SetDocString(m_ptr, doc) != 0)
throw error_already_set();
#else
setattr(m_ptr, "__doc__", PYBIND11_STR_TYPE(doc));
#endif
}
}

/** \rst
Expand Down

0 comments on commit b479f2a

Please sign in to comment.