Skip to content

Commit 10f8790

Browse files
committed
Document the new module_ constructor and add an extensive note to the upgrade guide
1 parent 7f64ed3 commit 10f8790

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

docs/upgrade.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,47 @@ to a new version. But it goes into more detail. This includes things like
88
deprecated APIs and their replacements, build system changes, general code
99
modernization and other useful information.
1010

11+
.. _upgrade-guide-2.7:
12+
13+
v2.7
14+
====
15+
16+
The previously-deprecated constructor of ``py::module_`` now creates a Python
17+
module object that is *not* used as the top-level module of a C extension.
18+
Previous usage of the deprecated ``PYBIND11_PLUGIN`` macro using the constructor
19+
instead of ``py::module_::create_extension_module`` will result in the following error
20+
on import: ``SystemError: initialization of example did not return an extension module``.
21+
22+
In this case, change the following double-deprecated, memory-leaking pattern:
23+
24+
.. code-block:: cpp
25+
26+
PYBIND11_PLUGIN(example) {
27+
auto m = pybind11::module_("example", "pybind11 example plugin");
28+
/// Set up bindings here
29+
return m.ptr();
30+
}
31+
32+
into this:
33+
34+
.. code-block:: cpp
35+
36+
static pybind11::module_::module_def example_module_def;
37+
PYBIND11_PLUGIN(example) {
38+
auto m = pybind11::module_::create_extension_module(
39+
"example", "pybind11 example plugin", &example_module_def);
40+
/// Set up bindings here
41+
return m.ptr();
42+
}
43+
44+
or, preferably, avoid the deprecated ``PYBIND11_PLUGIN`` completely:
45+
46+
.. code-block:: cpp
47+
48+
PYBIND11_MODULE(example, m) {
49+
/// Set up bindings here
50+
}
51+
1152
.. _upgrade-guide-2.6:
1253

1354
v2.6

include/pybind11/pybind11.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,17 @@ class module_ : public object {
892892
public:
893893
PYBIND11_OBJECT_DEFAULT(module_, object, PyModule_Check)
894894

895-
/// Create a new top-level Python module with the given name and docstring
895+
/** \rst
896+
Create a new top-level Python module with the given name and docstring.
897+
898+
Note that this cannot be used as the top-level module for a C extension.
899+
Use `module_::create_extension_module` to create a module object that is
900+
accepted by Python as top-level C extension module, returned from the
901+
extension's ``PyInit_foo`` entry point (``initfoo`` in Python 2).
902+
If you do *not* need a top-level C extension module, this constructor has
903+
the advantage of not needing a ``PyModuleDef`` and thus being easier to use
904+
w.r.t. memory management.
905+
\endrst */
896906
explicit module_(const char *name, const char *doc = nullptr) {
897907
#if defined(PYPY_VERSION) && (PYPY_VERSION_NUM < 0x07030400)
898908
m_ptr = PyModule_New(const_cast<char *>(name));

0 commit comments

Comments
 (0)