Skip to content

Commit b479f2a

Browse files
committed
Use PyModule_New in module_ public constructor
1 parent 0b9acc4 commit b479f2a

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
@@ -883,19 +883,28 @@ class cpp_function : public function {
883883
}
884884
};
885885

886-
/// Wrapper for Python extension modules
886+
/// Wrapper for Python modules
887887
class module_ : public object {
888888
public:
889889
PYBIND11_OBJECT_DEFAULT(module_, object, PyModule_Check)
890890

891891
/// Create a new top-level Python module with the given name and docstring
892-
PYBIND11_DEPRECATED("Use PYBIND11_MODULE or module_::create_extension_module instead")
893892
explicit module_(const char *name, const char *doc = nullptr) {
894-
#if PY_MAJOR_VERSION >= 3
895-
*this = create_extension_module(name, doc, new PyModuleDef());
893+
#if defined(PYPY_VERSION)
894+
m_ptr = PyModule_New(const_cast<char *>(name));
896895
#else
897-
*this = create_extension_module(name, doc, nullptr);
896+
m_ptr = PyModule_New(name);
898897
#endif
898+
if (m_ptr == nullptr)
899+
pybind11_fail("Internal error in module_::module_()");
900+
if (doc && options::show_user_defined_docstrings()) {
901+
#if PY_MAJOR_VERSION >= 3 && !defined(PYPY_VERSION)
902+
if (PyModule_SetDocString(m_ptr, doc) != 0)
903+
throw error_already_set();
904+
#else
905+
setattr(m_ptr, "__doc__", PYBIND11_STR_TYPE(doc));
906+
#endif
907+
}
899908
}
900909

901910
/** \rst

0 commit comments

Comments
 (0)