@@ -868,32 +868,30 @@ class cpp_function : public function {
868
868
}
869
869
};
870
870
871
-
872
- #if PY_MAJOR_VERSION >= 3
873
871
class module_ ;
874
872
875
873
PYBIND11_NAMESPACE_BEGIN (detail)
874
+ #if PY_MAJOR_VERSION >= 3
876
875
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);
878
878
#endif
879
+ PYBIND11_NAMESPACE_END (detail)
879
880
880
881
// / Wrapper for Python extension modules
881
882
class module_ : public object {
882
883
public:
883
884
PYBIND11_OBJECT_DEFAULT (module_, object, PyModule_Check)
884
885
885
886
// / 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 ) {
887
889
#if PY_MAJOR_VERSION >= 3
888
- : module_ (name, doc, new PyModuleDef()) {}
890
+ * this = detail::create_top_level_module (name, doc, new PyModuleDef ());
889
891
#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);
896
893
#endif
894
+ }
897
895
898
896
/* * \rst
899
897
Create Python binding for a new function within the module scope. ``Func``
@@ -958,43 +956,42 @@ class module_ : public object {
958
956
959
957
PyModule_AddObject (ptr (), name, obj.inc_ref ().ptr () /* steals a reference */ );
960
958
}
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
984
959
};
985
960
986
961
// When inside a namespace (or anywhere as long as it's not the first item on a line),
987
962
// C++20 allows "module" to be used. This is provided for backward compatibility, and for
988
963
// simplicity, if someone wants to use py::module for example, that is perfectly safe.
989
964
using module = module_;
990
965
991
- #if PY_MAJOR_VERSION >= 3
992
966
PYBIND11_NAMESPACE_BEGIN (detail)
967
+ #if PY_MAJOR_VERSION >= 3
993
968
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);
995
992
}
996
- PYBIND11_NAMESPACE_END (detail)
997
993
#endif
994
+ PYBIND11_NAMESPACE_END (detail)
998
995
999
996
// / \ingroup python_builtins
1000
997
// / Return a dictionary representing the global variables in the current execution frame,
0 commit comments