@@ -868,16 +868,6 @@ class cpp_function : public function {
868
868
}
869
869
};
870
870
871
- class module_ ;
872
-
873
- PYBIND11_NAMESPACE_BEGIN (detail)
874
- #if PY_MAJOR_VERSION >= 3
875
- inline module_ create_top_level_module (const char *name, const char *doc, PyModuleDef *def);
876
- #else
877
- inline module_ create_top_level_module (const char *name, const char *doc);
878
- #endif
879
- PYBIND11_NAMESPACE_END (detail)
880
-
881
871
// / Wrapper for Python extension modules
882
872
class module_ : public object {
883
873
public:
@@ -887,9 +877,9 @@ class module_ : public object {
887
877
PYBIND11_DEPRECATED (" Use PYBIND11_MODULE, module_::def_submodule, or module_::import instead" )
888
878
explicit module_ (const char *name, const char *doc = nullptr ) {
889
879
#if PY_MAJOR_VERSION >= 3
890
- *this = detail::create_top_level_module (name, doc, new PyModuleDef ());
880
+ *this = create_extension_module (name, doc, new PyModuleDef ());
891
881
#else
892
- *this = detail::create_top_level_module (name, doc);
882
+ *this = create_extension_module (name, doc, nullptr );
893
883
#endif
894
884
}
895
885
@@ -958,43 +948,54 @@ class module_ : public object {
958
948
959
949
PyModule_AddObject (ptr (), name, obj.inc_ref ().ptr () /* steals a reference */ );
960
950
}
951
+
952
+ #if PY_MAJOR_VERSION >= 3
953
+ using module_def = PyModuleDef;
954
+ #else
955
+ struct module_def {};
956
+ #endif
957
+
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
+ 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
+ auto m = PyModule_Create (def);
979
+ if (m == nullptr )
980
+ pybind11_fail (" Internal error in module_::create_extension_module()" );
981
+ // TODO: Should be reinterpret_steal, but Python also steals it again when returned from PyInit_...
982
+ return reinterpret_borrow<module_>(m);
983
+ #else
984
+ // Ignore module_def *def; only necessary for Python 3
985
+ (void ) def;
986
+ auto m = Py_InitModule3 (name, nullptr , options::show_user_defined_docstrings () ? doc : nullptr );
987
+ if (m == nullptr )
988
+ pybind11_fail (" Internal error in module_::create_extension_module()" );
989
+ return reinterpret_borrow<module_>(m);
990
+ #endif
991
+ }
961
992
};
962
993
963
994
// When inside a namespace (or anywhere as long as it's not the first item on a line),
964
995
// C++20 allows "module" to be used. This is provided for backward compatibility, and for
965
996
// simplicity, if someone wants to use py::module for example, that is perfectly safe.
966
997
using module = module_;
967
998
968
- PYBIND11_NAMESPACE_BEGIN (detail)
969
- #if PY_MAJOR_VERSION >= 3
970
- inline module_ create_top_level_module (const char *name, const char *doc, PyModuleDef *def) {
971
- def = new (def) PyModuleDef { // Placement new (not an allocation).
972
- /* m_base */ PyModuleDef_HEAD_INIT,
973
- /* m_name */ name,
974
- /* m_doc */ options::show_user_defined_docstrings () ? doc : nullptr ,
975
- /* m_size */ -1 ,
976
- /* m_methods */ nullptr ,
977
- /* m_slots */ nullptr ,
978
- /* m_traverse */ nullptr ,
979
- /* m_clear */ nullptr ,
980
- /* m_free */ nullptr
981
- };
982
- auto m = PyModule_Create (def);
983
- if (m == nullptr )
984
- pybind11_fail (" Internal error in detail::create_top_level_module()" );
985
- // TODO: Should be reinterpret_steal, but Python also steals it again when returned from PyInit_...
986
- return reinterpret_borrow<module_>(m);
987
- }
988
- #else
989
- inline module_ create_top_level_module (const char *name, const char *doc) {
990
- auto m = Py_InitModule3 (name, nullptr , options::show_user_defined_docstrings () ? doc : nullptr );
991
- if (m == nullptr )
992
- pybind11_fail (" Internal error in detail::create_top_level_module()" );
993
- return reinterpret_borrow<module_>(m);
994
- }
995
- #endif
996
- PYBIND11_NAMESPACE_END (detail)
997
-
998
999
// / \ingroup python_builtins
999
1000
// / Return a dictionary representing the global variables in the current execution frame,
1000
1001
// / or ``__main__.__dict__`` if there is no frame (usually when the interpreter is embedded).
0 commit comments