Skip to content

Commit b043c6d

Browse files
committed
Use PyModule_New in module_ public constructor
1 parent a65a38b commit b043c6d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

include/pybind11/detail/common.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,10 @@ extern "C" {
270270
271271
.. code-block:: cpp
272272
273+
static pybind11::module_::module_def example_module_def;
273274
PYBIND11_PLUGIN(example) {
274-
pybind11::module_ m("example", "pybind11 example plugin");
275+
auto m = pybind11::module_::create_extension_module(
276+
"example", "pybind11 example plugin", &example_module_def);
275277
/// Set up bindings here
276278
return m.ptr();
277279
}

include/pybind11/pybind11.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -868,19 +868,28 @@ class cpp_function : public function {
868868
}
869869
};
870870

871-
/// Wrapper for Python extension modules
871+
/// Wrapper for Python modules
872872
class module_ : public object {
873873
public:
874874
PYBIND11_OBJECT_DEFAULT(module_, object, PyModule_Check)
875875

876876
/// Create a new top-level Python module with the given name and docstring
877-
PYBIND11_DEPRECATED("Use PYBIND11_MODULE, module_::def_submodule, or module_::import instead")
878877
explicit module_(const char *name, const char *doc = nullptr) {
879-
#if PY_MAJOR_VERSION >= 3
880-
*this = create_extension_module(name, doc, new PyModuleDef());
878+
#if defined(PYPY_VERSION)
879+
m_ptr = PyModule_New(const_cast<char *>(name));
880+
#else
881+
m_ptr = PyModule_New(name);
882+
#endif
883+
if (m_ptr == nullptr)
884+
pybind11_fail("Internal error in module_::module_()");
885+
if (doc && options::show_user_defined_docstrings()) {
886+
#if PY_MAJOR_VERSION >= 3 && !defined(PYPY_VERSION)
887+
if (PyModule_SetDocString(m_ptr, doc) != 0)
888+
throw error_already_set();
881889
#else
882-
*this = create_extension_module(name, doc, nullptr);
890+
setattr(m_ptr, "__doc__", PYBIND11_STR_TYPE(doc));
883891
#endif
892+
}
884893
}
885894

886895
/** \rst

0 commit comments

Comments
 (0)