@@ -868,32 +868,20 @@ class cpp_function : public function {
868
868
}
869
869
};
870
870
871
-
872
- #if PY_MAJOR_VERSION >= 3
873
- class module_ ;
874
-
875
- PYBIND11_NAMESPACE_BEGIN (detail)
876
- inline module_ create_top_level_module (const char *name, const char *doc, PyModuleDef *def);
877
- PYBIND11_NAMESPACE_END (detail)
878
- #endif
879
-
880
871
// / Wrapper for Python extension modules
881
872
class module_ : public object {
882
873
public:
883
874
PYBIND11_OBJECT_DEFAULT (module_, object, PyModule_Check)
884
875
885
876
// / Create a new top-level Python module with the given name and docstring
886
- explicit module_ (const char *name, const char *doc = nullptr )
877
+ PYBIND11_DEPRECATED (" Use PYBIND11_MODULE or module_::create_extension_module instead" )
878
+ explicit module_ (const char *name, const char *doc = nullptr ) {
887
879
#if PY_MAJOR_VERSION >= 3
888
- : module_ (name, doc, new PyModuleDef()) {}
880
+ * this = create_extension_module (name, doc, new PyModuleDef ());
889
881
#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
- }
882
+ *this = create_extension_module (name, doc, nullptr );
896
883
#endif
884
+ }
897
885
898
886
/* * \rst
899
887
Create Python binding for a new function within the module scope. ``Func``
@@ -946,11 +934,13 @@ class module_ : public object {
946
934
*this = reinterpret_steal<module_>(obj);
947
935
}
948
936
949
- // Adds an object to the module using the given name. Throws if an object with the given name
950
- // already exists.
951
- //
952
- // overwrite should almost always be false: attempting to overwrite objects that pybind11 has
953
- // established will, in most cases, break things.
937
+ /* * \rst
938
+ Adds an object to the module using the given name. Throws if an object with the given name
939
+ already exists.
940
+
941
+ ``overwrite`` should almost always be false: attempting to overwrite objects that pybind11 has
942
+ established will, in most cases, break things.
943
+ \endrst */
954
944
PYBIND11_NOINLINE void add_object (const char *name, handle obj, bool overwrite = false ) {
955
945
if (!overwrite && hasattr (*this , name))
956
946
pybind11_fail (" Error during initialization: multiple incompatible definitions with name \" " +
@@ -959,11 +949,21 @@ class module_ : public object {
959
949
PyModule_AddObject (ptr (), name, obj.inc_ref ().ptr () /* steals a reference */ );
960
950
}
961
951
962
- private:
963
952
#if PY_MAJOR_VERSION >= 3
964
- friend module_ detail::create_top_level_module (const char *, const char *, PyModuleDef *);
953
+ using module_def = PyModuleDef;
954
+ #else
955
+ struct module_def {};
956
+ #endif
965
957
966
- explicit module_ (const char *name, const char *doc, PyModuleDef *def) {
958
+ /* * \rst
959
+ Create a new top-level module that can be used as the main module of a C extension.
960
+
961
+ For Python 3, ``def`` should point to a staticly allocated module_def.
962
+ For Python 2, ``def`` can be a nullptr and is completely ignored.
963
+ \endrst */
964
+ static module_ create_extension_module (const char *name, const char *doc, module_def *def) {
965
+ #if PY_MAJOR_VERSION >= 3
966
+ // module_def is PyModuleDef
967
967
def = new (def) PyModuleDef { // Placement new (not an allocation).
968
968
/* m_base */ PyModuleDef_HEAD_INIT,
969
969
/* m_name */ name,
@@ -975,27 +975,28 @@ class module_ : public object {
975
975
/* m_clear */ nullptr ,
976
976
/* m_free */ nullptr
977
977
};
978
- m_ptr = PyModule_Create (def);
979
- if (m_ptr == nullptr )
980
- pybind11_fail ( " Internal error in module_::module_() " );
981
- inc_ref () ;
982
- }
978
+ auto m = PyModule_Create (def);
979
+ # else
980
+ // Ignore module_def *def; only necessary for Python 3
981
+ ( void ) def ;
982
+ auto m = Py_InitModule3 (name, nullptr , options::show_user_defined_docstrings () ? doc : nullptr );
983
983
#endif
984
+ if (m == nullptr ) {
985
+ if (PyErr_Occurred ())
986
+ throw error_already_set ();
987
+ pybind11_fail (" Internal error in module_::create_extension_module()" );
988
+ }
989
+ // TODO: Sould be reinterpret_steal for Python 3, but Python also steals it again when returned from PyInit_...
990
+ // For Python 2, reinterpret_borrow is correct.
991
+ return reinterpret_borrow<module_>(m);
992
+ }
984
993
};
985
994
986
995
// When inside a namespace (or anywhere as long as it's not the first item on a line),
987
996
// C++20 allows "module" to be used. This is provided for backward compatibility, and for
988
997
// simplicity, if someone wants to use py::module for example, that is perfectly safe.
989
998
using module = module_;
990
999
991
- #if PY_MAJOR_VERSION >= 3
992
- PYBIND11_NAMESPACE_BEGIN (detail)
993
- inline module_ create_top_level_module (const char *name, const char *doc, PyModuleDef *def) {
994
- return module_ (name, doc, def);
995
- }
996
- PYBIND11_NAMESPACE_END (detail)
997
- #endif
998
-
999
1000
// / \ingroup python_builtins
1000
1001
// / Return a dictionary representing the global variables in the current execution frame,
1001
1002
// / or ``__main__.__dict__`` if there is no frame (usually when the interpreter is embedded).
0 commit comments