Skip to content

Commit 19c3079

Browse files
committed
Deprecated public constructors of module
1 parent 1411207 commit 19c3079

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
@@ -311,18 +311,19 @@ extern "C" {
311311
#define PYBIND11_DETAIL_MODULE_STATIC_DEF(name) \
312312
static PyModuleDef PYBIND11_CONCAT(pybind11_module_def_, name);
313313
#define PYBIND11_DETAIL_MODULE_CREATE(name) \
314-
auto m = pybind11::detail::create_top_level_module( \
314+
auto m = ::pybind11::detail::create_top_level_module( \
315315
PYBIND11_TOSTRING(name), nullptr, \
316316
&PYBIND11_CONCAT(pybind11_module_def_, name));
317317
#else
318318
#define PYBIND11_DETAIL_MODULE_STATIC_DEF(name)
319319
#define PYBIND11_DETAIL_MODULE_CREATE(name) \
320-
auto m = pybind11::module_(PYBIND11_TOSTRING(name));
320+
auto m = ::pybind11::detail::create_top_level_module( \
321+
PYBIND11_TOSTRING(name), nullptr)
321322
#endif
322323
#define PYBIND11_MODULE(name, variable) \
323324
PYBIND11_DETAIL_MODULE_STATIC_DEF(name) \
324325
PYBIND11_MAYBE_UNUSED \
325-
static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module_ &); \
326+
static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module_ &); \
326327
PYBIND11_PLUGIN_IMPL(name) { \
327328
PYBIND11_CHECK_PYTHON_VERSION \
328329
PYBIND11_ENSURE_INTERNALS_READY \
@@ -332,7 +333,7 @@ extern "C" {
332333
return m.ptr(); \
333334
} PYBIND11_CATCH_INIT_EXCEPTIONS \
334335
} \
335-
void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module_ &variable)
336+
void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &variable)
336337

337338

338339
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
@@ -856,32 +856,30 @@ class cpp_function : public function {
856856
}
857857
};
858858

859-
860-
#if PY_MAJOR_VERSION >= 3
861859
class module_;
862860

863861
PYBIND11_NAMESPACE_BEGIN(detail)
862+
#if PY_MAJOR_VERSION >= 3
864863
inline module_ create_top_level_module(const char *name, const char *doc, PyModuleDef *def);
865-
PYBIND11_NAMESPACE_END(detail)
864+
#else
865+
inline module_ create_top_level_module(const char *name, const char *doc);
866866
#endif
867+
PYBIND11_NAMESPACE_END(detail)
867868

868869
/// Wrapper for Python extension modules
869870
class module_ : public object {
870871
public:
871872
PYBIND11_OBJECT_DEFAULT(module_, object, PyModule_Check)
872873

873874
/// Create a new top-level Python module with the given name and docstring
874-
explicit module_(const char *name, const char *doc = nullptr)
875+
PYBIND11_DEPRECATED("Use PYBIND11_MODULE, module_::def_submodule, or module_::import instead")
876+
explicit module_(const char *name, const char *doc = nullptr) {
875877
#if PY_MAJOR_VERSION >= 3
876-
: module_(name, doc, new PyModuleDef()) {}
878+
*this = detail::create_top_level_module(name, doc, new PyModuleDef());
877879
#else
878-
{
879-
m_ptr = Py_InitModule3(name, nullptr, options::show_user_defined_docstrings() ? doc : nullptr);
880-
if (m_ptr == nullptr)
881-
pybind11_fail("Internal error in module_::module_()");
882-
inc_ref();
883-
}
880+
*this = detail::create_top_level_module(name, doc);
884881
#endif
882+
}
885883

886884
/** \rst
887885
Create Python binding for a new function within the module scope. ``Func``
@@ -946,43 +944,42 @@ class module_ : public object {
946944

947945
PyModule_AddObject(ptr(), name, obj.inc_ref().ptr() /* steals a reference */);
948946
}
949-
950-
private:
951-
#if PY_MAJOR_VERSION >= 3
952-
friend module_ detail::create_top_level_module(const char *, const char *, PyModuleDef *);
953-
954-
explicit module_(const char *name, const char *doc, PyModuleDef *def) {
955-
def = new (def) PyModuleDef { // Placement new (not an allocation).
956-
/* m_base */ PyModuleDef_HEAD_INIT,
957-
/* m_name */ name,
958-
/* m_doc */ options::show_user_defined_docstrings() ? doc : nullptr,
959-
/* m_size */ -1,
960-
/* m_methods */ nullptr,
961-
/* m_slots */ nullptr,
962-
/* m_traverse */ nullptr,
963-
/* m_clear */ nullptr,
964-
/* m_free */ nullptr
965-
};
966-
m_ptr = PyModule_Create(def);
967-
if (m_ptr == nullptr)
968-
pybind11_fail("Internal error in module_::module_()");
969-
inc_ref();
970-
}
971-
#endif
972947
};
973948

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

979-
#if PY_MAJOR_VERSION >= 3
980954
PYBIND11_NAMESPACE_BEGIN(detail)
955+
#if PY_MAJOR_VERSION >= 3
981956
inline module_ create_top_level_module(const char *name, const char *doc, PyModuleDef *def) {
982-
return module_(name, doc, def);
957+
def = new (def) PyModuleDef { // Placement new (not an allocation).
958+
/* m_base */ PyModuleDef_HEAD_INIT,
959+
/* m_name */ name,
960+
/* m_doc */ options::show_user_defined_docstrings() ? doc : nullptr,
961+
/* m_size */ -1,
962+
/* m_methods */ nullptr,
963+
/* m_slots */ nullptr,
964+
/* m_traverse */ nullptr,
965+
/* m_clear */ nullptr,
966+
/* m_free */ nullptr
967+
};
968+
auto m = PyModule_Create(def);
969+
if (m == nullptr)
970+
pybind11_fail("Internal error in detail::create_top_level_module()");
971+
// TODO: Should be reinterpret_steal, but Python also steals it again when returned from PyInit_...
972+
return reinterpret_borrow<module_>(m);
973+
}
974+
#else
975+
inline module_ create_top_level_module(const char *name, const char *doc) {
976+
auto m = Py_InitModule3(name, nullptr, options::show_user_defined_docstrings() ? doc : nullptr);
977+
if (m == nullptr)
978+
pybind11_fail("Internal error in detail::create_top_level_module()");
979+
return reinterpret_borrow<module_>(m);
983980
}
984-
PYBIND11_NAMESPACE_END(detail)
985981
#endif
982+
PYBIND11_NAMESPACE_END(detail)
986983

987984
/// \ingroup python_builtins
988985
/// 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)