@@ -856,32 +856,30 @@ class cpp_function : public function {
856
856
}
857
857
};
858
858
859
-
860
- #if PY_MAJOR_VERSION >= 3
861
859
class module_ ;
862
860
863
861
PYBIND11_NAMESPACE_BEGIN (detail)
862
+ #if PY_MAJOR_VERSION >= 3
864
863
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);
866
866
#endif
867
+ PYBIND11_NAMESPACE_END (detail)
867
868
868
869
// / Wrapper for Python extension modules
869
870
class module_ : public object {
870
871
public:
871
872
PYBIND11_OBJECT_DEFAULT (module_, object, PyModule_Check)
872
873
873
874
// / 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 ) {
875
877
#if PY_MAJOR_VERSION >= 3
876
- : module_ (name, doc, new PyModuleDef()) {}
878
+ * this = detail::create_top_level_module (name, doc, new PyModuleDef ());
877
879
#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);
884
881
#endif
882
+ }
885
883
886
884
/* * \rst
887
885
Create Python binding for a new function within the module scope. ``Func``
@@ -946,43 +944,42 @@ class module_ : public object {
946
944
947
945
PyModule_AddObject (ptr (), name, obj.inc_ref ().ptr () /* steals a reference */ );
948
946
}
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
972
947
};
973
948
974
949
// When inside a namespace (or anywhere as long as it's not the first item on a line),
975
950
// C++20 allows "module" to be used. This is provided for backward compatibility, and for
976
951
// simplicity, if someone wants to use py::module for example, that is perfectly safe.
977
952
using module = module_;
978
953
979
- #if PY_MAJOR_VERSION >= 3
980
954
PYBIND11_NAMESPACE_BEGIN (detail)
955
+ #if PY_MAJOR_VERSION >= 3
981
956
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);
983
980
}
984
- PYBIND11_NAMESPACE_END (detail)
985
981
#endif
982
+ PYBIND11_NAMESPACE_END (detail)
986
983
987
984
// / \ingroup python_builtins
988
985
// / Return a dictionary representing the global variables in the current execution frame,
0 commit comments