Skip to content

Commit 4be6029

Browse files
committed
Deprecated public constructors of module
1 parent 00edc30 commit 4be6029

File tree

4 files changed

+65
-62
lines changed

4 files changed

+65
-62
lines changed

include/pybind11/detail/common.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,19 @@ extern "C" {
313313
#define PYBIND11_DETAIL_MODULE_STATIC_DEF(name) \
314314
static PyModuleDef PYBIND11_CONCAT(pybind11_module_def_, name);
315315
#define PYBIND11_DETAIL_MODULE_CREATE(name) \
316-
auto m = pybind11::detail::create_top_level_module( \
316+
auto m = ::pybind11::detail::create_top_level_module( \
317317
PYBIND11_TOSTRING(name), nullptr, \
318318
&PYBIND11_CONCAT(pybind11_module_def_, name));
319319
#else
320320
#define PYBIND11_DETAIL_MODULE_STATIC_DEF(name)
321321
#define PYBIND11_DETAIL_MODULE_CREATE(name) \
322-
auto m = pybind11::module_(PYBIND11_TOSTRING(name));
322+
auto m = ::pybind11::detail::create_top_level_module( \
323+
PYBIND11_TOSTRING(name), nullptr);
323324
#endif
324325
#define PYBIND11_MODULE(name, variable) \
325326
PYBIND11_DETAIL_MODULE_STATIC_DEF(name) \
326327
PYBIND11_MAYBE_UNUSED \
327-
static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module_ &); \
328+
static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module_ &); \
328329
PYBIND11_PLUGIN_IMPL(name) { \
329330
PYBIND11_CHECK_PYTHON_VERSION \
330331
PYBIND11_ENSURE_INTERNALS_READY \
@@ -334,7 +335,7 @@ extern "C" {
334335
return m.ptr(); \
335336
} PYBIND11_CATCH_INIT_EXCEPTIONS \
336337
} \
337-
void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module_ &variable)
338+
void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &variable)
338339

339340

340341
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

include/pybind11/embed.h

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,27 @@
4545
});
4646
}
4747
\endrst */
48-
#define PYBIND11_EMBEDDED_MODULE(name, variable) \
49-
static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module_ &); \
50-
static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
51-
auto m = pybind11::module_(PYBIND11_TOSTRING(name)); \
52-
try { \
53-
PYBIND11_CONCAT(pybind11_init_, name)(m); \
54-
return m.ptr(); \
55-
} catch (pybind11::error_already_set &e) { \
56-
PyErr_SetString(PyExc_ImportError, e.what()); \
57-
return nullptr; \
58-
} catch (const std::exception &e) { \
59-
PyErr_SetString(PyExc_ImportError, e.what()); \
60-
return nullptr; \
61-
} \
62-
} \
63-
PYBIND11_EMBEDDED_MODULE_IMPL(name) \
64-
pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name) \
65-
(PYBIND11_TOSTRING(name), \
66-
PYBIND11_CONCAT(pybind11_init_impl_, name)); \
67-
void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module_ &variable)
48+
#define PYBIND11_EMBEDDED_MODULE(name, variable) \
49+
PYBIND11_DETAIL_MODULE_STATIC_DEF(name) \
50+
static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \
51+
static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
52+
PYBIND11_DETAIL_MODULE_CREATE(name) \
53+
try { \
54+
PYBIND11_CONCAT(pybind11_init_, name)(m); \
55+
return m.ptr(); \
56+
} catch (::pybind11::error_already_set &e) { \
57+
PyErr_SetString(PyExc_ImportError, e.what()); \
58+
return nullptr; \
59+
} catch (const std::exception &e) { \
60+
PyErr_SetString(PyExc_ImportError, e.what()); \
61+
return nullptr; \
62+
} \
63+
} \
64+
PYBIND11_EMBEDDED_MODULE_IMPL(name) \
65+
::pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name) \
66+
(PYBIND11_TOSTRING(name), \
67+
PYBIND11_CONCAT(pybind11_init_impl_, name)); \
68+
void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &variable)
6869

6970

7071
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

include/pybind11/pybind11.h

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -868,32 +868,30 @@ class cpp_function : public function {
868868
}
869869
};
870870

871-
872-
#if PY_MAJOR_VERSION >= 3
873871
class module_;
874872

875873
PYBIND11_NAMESPACE_BEGIN(detail)
874+
#if PY_MAJOR_VERSION >= 3
876875
inline module_ create_top_level_module(const char *name, const char *doc, PyModuleDef *def);
877-
PYBIND11_NAMESPACE_END(detail)
876+
#else
877+
inline module_ create_top_level_module(const char *name, const char *doc);
878878
#endif
879+
PYBIND11_NAMESPACE_END(detail)
879880

880881
/// Wrapper for Python extension modules
881882
class module_ : public object {
882883
public:
883884
PYBIND11_OBJECT_DEFAULT(module_, object, PyModule_Check)
884885

885886
/// Create a new top-level Python module with the given name and docstring
886-
explicit module_(const char *name, const char *doc = nullptr)
887+
PYBIND11_DEPRECATED("Use PYBIND11_MODULE, module_::def_submodule, or module_::import instead")
888+
explicit module_(const char *name, const char *doc = nullptr) {
887889
#if PY_MAJOR_VERSION >= 3
888-
: module_(name, doc, new PyModuleDef()) {}
890+
*this = detail::create_top_level_module(name, doc, new PyModuleDef());
889891
#else
890-
{
891-
m_ptr = Py_InitModule3(name, nullptr, options::show_user_defined_docstrings() ? doc : nullptr);
892-
if (m_ptr == nullptr)
893-
pybind11_fail("Internal error in module_::module_()");
894-
inc_ref();
895-
}
892+
*this = detail::create_top_level_module(name, doc);
896893
#endif
894+
}
897895

898896
/** \rst
899897
Create Python binding for a new function within the module scope. ``Func``
@@ -958,43 +956,42 @@ class module_ : public object {
958956

959957
PyModule_AddObject(ptr(), name, obj.inc_ref().ptr() /* steals a reference */);
960958
}
961-
962-
private:
963-
#if PY_MAJOR_VERSION >= 3
964-
friend module_ detail::create_top_level_module(const char *, const char *, PyModuleDef *);
965-
966-
explicit module_(const char *name, const char *doc, PyModuleDef *def) {
967-
def = new (def) PyModuleDef { // Placement new (not an allocation).
968-
/* m_base */ PyModuleDef_HEAD_INIT,
969-
/* m_name */ name,
970-
/* m_doc */ options::show_user_defined_docstrings() ? doc : nullptr,
971-
/* m_size */ -1,
972-
/* m_methods */ nullptr,
973-
/* m_slots */ nullptr,
974-
/* m_traverse */ nullptr,
975-
/* m_clear */ nullptr,
976-
/* m_free */ nullptr
977-
};
978-
m_ptr = PyModule_Create(def);
979-
if (m_ptr == nullptr)
980-
pybind11_fail("Internal error in module_::module_()");
981-
inc_ref();
982-
}
983-
#endif
984959
};
985960

986961
// When inside a namespace (or anywhere as long as it's not the first item on a line),
987962
// C++20 allows "module" to be used. This is provided for backward compatibility, and for
988963
// simplicity, if someone wants to use py::module for example, that is perfectly safe.
989964
using module = module_;
990965

991-
#if PY_MAJOR_VERSION >= 3
992966
PYBIND11_NAMESPACE_BEGIN(detail)
967+
#if PY_MAJOR_VERSION >= 3
993968
inline module_ create_top_level_module(const char *name, const char *doc, PyModuleDef *def) {
994-
return module_(name, doc, def);
969+
def = new (def) PyModuleDef { // Placement new (not an allocation).
970+
/* m_base */ PyModuleDef_HEAD_INIT,
971+
/* m_name */ name,
972+
/* m_doc */ options::show_user_defined_docstrings() ? doc : nullptr,
973+
/* m_size */ -1,
974+
/* m_methods */ nullptr,
975+
/* m_slots */ nullptr,
976+
/* m_traverse */ nullptr,
977+
/* m_clear */ nullptr,
978+
/* m_free */ nullptr
979+
};
980+
auto m = PyModule_Create(def);
981+
if (m == nullptr)
982+
pybind11_fail("Internal error in detail::create_top_level_module()");
983+
// TODO: Should be reinterpret_steal, but Python also steals it again when returned from PyInit_...
984+
return reinterpret_borrow<module_>(m);
985+
}
986+
#else
987+
inline module_ create_top_level_module(const char *name, const char *doc) {
988+
auto m = Py_InitModule3(name, nullptr, options::show_user_defined_docstrings() ? doc : nullptr);
989+
if (m == nullptr)
990+
pybind11_fail("Internal error in detail::create_top_level_module()");
991+
return reinterpret_borrow<module_>(m);
995992
}
996-
PYBIND11_NAMESPACE_END(detail)
997993
#endif
994+
PYBIND11_NAMESPACE_END(detail)
998995

999996
/// \ingroup python_builtins
1000997
/// Return a dictionary representing the global variables in the current execution frame,

tests/test_modules.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ TEST_SUBMODULE(modules, m) {
6262
class Dupe3 { };
6363
class DupeException { };
6464

65-
auto dm = py::module_("dummy");
65+
#if PY_MAJOR_VERSION >= 3
66+
auto dm = py::detail::create_top_level_module("dummy", nullptr, new PyModuleDef);
67+
#else
68+
auto dm = py::detail::create_top_level_module("dummy", nullptr);
69+
#endif
6670
auto failures = py::list();
6771

6872
py::class_<Dupe1>(dm, "Dupe1");

0 commit comments

Comments
 (0)