\rst Create a new top-level module that can be used as the main module of a C extension. ``def`` should point to a statically allocated PyModuleDef. \endrst */
| 1706 | ``def`` should point to a statically allocated PyModuleDef. |
| 1707 | \endrst */ |
| 1708 | static module_ create_extension_module(const char *name, |
| 1709 | const char *doc, |
| 1710 | PyModuleDef *def, |
| 1711 | mod_gil_not_used gil_not_used |
| 1712 | = mod_gil_not_used(false)) { |
| 1713 | // Placement new (not an allocation). |
| 1714 | new (def) PyModuleDef{/* m_base */ PyModuleDef_HEAD_INIT, |
| 1715 | /* m_name */ name, |
| 1716 | /* m_doc */ options::show_user_defined_docstrings() ? doc : nullptr, |
| 1717 | /* m_size */ -1, |
| 1718 | /* m_methods */ nullptr, |
| 1719 | /* m_slots */ nullptr, |
| 1720 | /* m_traverse */ nullptr, |
| 1721 | /* m_clear */ nullptr, |
| 1722 | /* m_free */ nullptr}; |
| 1723 | auto *m = PyModule_Create(def); |
| 1724 | if (m == nullptr) { |
| 1725 | if (PyErr_Occurred()) { |
| 1726 | throw error_already_set(); |
| 1727 | } |
| 1728 | pybind11_fail("Internal error in module_::create_extension_module()"); |
| 1729 | } |
| 1730 | if (gil_not_used.flag()) { |
| 1731 | #ifdef Py_GIL_DISABLED |
| 1732 | PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED); |
| 1733 | #endif |
| 1734 | } |
| 1735 | // TODO: Should be reinterpret_steal for Python 3, but Python also steals it again when |
| 1736 | // returned from PyInit_... |
| 1737 | // For Python 2, reinterpret_borrow was correct. |
| 1738 | return reinterpret_borrow<module_>(m); |
| 1739 | } |
| 1740 | }; |
| 1741 | |
| 1742 | PYBIND11_NAMESPACE_BEGIN(detail) |