@@ -1206,6 +1206,8 @@ struct handle_type_name<cpp_function> {
1206
1206
1207
1207
PYBIND11_NAMESPACE_END (detail)
1208
1208
1209
+ struct gil_not_used {};
1210
+
1209
1211
// / Wrapper for Python extension modules
1210
1212
class module_ : public object {
1211
1213
public:
@@ -1299,15 +1301,6 @@ class module_ : public object {
1299
1301
PyModule_AddObject (ptr (), name, obj.inc_ref ().ptr () /* steals a reference */ );
1300
1302
}
1301
1303
1302
- /* * \rst
1303
- Mark the module as not requiring the GIL in free-threaded Python builds.
1304
- \endrst */
1305
- void set_gil_not_used () {
1306
- #ifdef Py_GIL_DISABLED
1307
- PyUnstable_Module_SetGIL (m_ptr, Py_MOD_GIL_NOT_USED);
1308
- #endif
1309
- }
1310
-
1311
1304
using module_def = PyModuleDef; // TODO: Can this be removed (it was needed only for Python 2)?
1312
1305
1313
1306
/* * \rst
@@ -1316,6 +1309,20 @@ class module_ : public object {
1316
1309
``def`` should point to a statically allocated module_def.
1317
1310
\endrst */
1318
1311
static module_ create_extension_module (const char *name, const char *doc, module_def *def) {
1312
+ return _create_extension_module (name, doc, def, false );
1313
+ }
1314
+
1315
+ static module_
1316
+ create_extension_module (const char *name, const char *doc, module_def *def, gil_not_used) {
1317
+ return _create_extension_module (name, doc, def, true );
1318
+ }
1319
+
1320
+ private:
1321
+ static module_ _create_extension_module (const char *name,
1322
+ const char *doc,
1323
+ module_def *def,
1324
+ PYBIND11_MAYBE_UNUSED bool gil_disabled) {
1325
+
1319
1326
// module_def is PyModuleDef
1320
1327
// Placement new (not an allocation).
1321
1328
def = new (def)
@@ -1335,6 +1342,11 @@ class module_ : public object {
1335
1342
}
1336
1343
pybind11_fail (" Internal error in module_::create_extension_module()" );
1337
1344
}
1345
+ #ifdef Py_GIL_DISABLED
1346
+ if (gil_disabled) {
1347
+ PyUnstable_Module_SetGIL (m, Py_MOD_GIL_NOT_USED);
1348
+ }
1349
+ #endif
1338
1350
// TODO: Should be reinterpret_steal for Python 3, but Python also steals it again when
1339
1351
// returned from PyInit_...
1340
1352
// For Python 2, reinterpret_borrow was correct.
0 commit comments