|
283 | 283 | #define PYBIND11_BUILTINS_MODULE "builtins"
|
284 | 284 | // Providing a separate declaration to make Clang's -Wmissing-prototypes happy.
|
285 | 285 | // See comment for PYBIND11_MODULE below for why this is marked "maybe unused".
|
| 286 | +#define PYBIND11_PLUGIN_DECL(name) \ |
| 287 | + extern "C" PYBIND11_MAYBE_UNUSED PYBIND11_EXPORT PyObject *PyInit_##name(); |
286 | 288 | #define PYBIND11_PLUGIN_IMPL(name) \
|
287 |
| - extern "C" PYBIND11_MAYBE_UNUSED PYBIND11_EXPORT PyObject *PyInit_##name(); \ |
| 289 | + PYBIND11_PLUGIN_DECL(name) \ |
288 | 290 | extern "C" PYBIND11_EXPORT PyObject *PyInit_##name()
|
289 | 291 |
|
290 | 292 | #define PYBIND11_TRY_NEXT_OVERLOAD ((PyObject *) 1) // special failure return code
|
|
361 | 363 | } \
|
362 | 364 | PyObject *pybind11_init()
|
363 | 365 |
|
| 366 | +PYBIND11_WARNING_PUSH |
| 367 | +PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments") |
| 368 | +#define PYBIND11_MODULE_PYINIT(name, pre_init, ...) \ |
| 369 | + static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name); \ |
| 370 | + static ::pybind11::module_::slots_array PYBIND11_CONCAT(pybind11_module_slots_, name); \ |
| 371 | + static int PYBIND11_CONCAT(pybind11_exec_, name)(PyObject *); \ |
| 372 | + static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \ |
| 373 | + PYBIND11_PLUGIN_IMPL(name) { \ |
| 374 | + PYBIND11_CHECK_PYTHON_VERSION \ |
| 375 | + pre_init; \ |
| 376 | + PYBIND11_ENSURE_INTERNALS_READY \ |
| 377 | + static auto result = []() { \ |
| 378 | + auto &slots = PYBIND11_CONCAT(pybind11_module_slots_, name); \ |
| 379 | + slots[0] = {Py_mod_exec, \ |
| 380 | + reinterpret_cast<void *>(&PYBIND11_CONCAT(pybind11_exec_, name))}; \ |
| 381 | + slots[1] = {0, nullptr}; \ |
| 382 | + return ::pybind11::module_::initialize_multiphase_module_def( \ |
| 383 | + PYBIND11_TOSTRING(name), \ |
| 384 | + nullptr, \ |
| 385 | + &PYBIND11_CONCAT(pybind11_module_def_, name), \ |
| 386 | + slots, \ |
| 387 | + ##__VA_ARGS__); \ |
| 388 | + }(); \ |
| 389 | + return result.ptr(); \ |
| 390 | + } |
| 391 | + |
| 392 | +PYBIND11_WARNING_POP |
| 393 | + |
| 394 | +#define PYBIND11_MODULE_EXEC(name, variable) \ |
| 395 | + int PYBIND11_CONCAT(pybind11_exec_, name)(PyObject * pm) { \ |
| 396 | + try { \ |
| 397 | + auto m = pybind11::reinterpret_borrow<::pybind11::module_>(pm); \ |
| 398 | + PYBIND11_CONCAT(pybind11_init_, name)(m); \ |
| 399 | + return 0; \ |
| 400 | + } \ |
| 401 | + PYBIND11_CATCH_INIT_EXCEPTIONS \ |
| 402 | + return -1; \ |
| 403 | + } \ |
| 404 | + void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ \ |
| 405 | + & variable) // NOLINT(bugprone-macro-parentheses) |
| 406 | + |
364 | 407 | /** \rst
|
365 | 408 | This macro creates the entry point that will be invoked when the Python interpreter
|
366 | 409 | imports an extension module. The module name is given as the first argument and it
|
367 | 410 | should not be in quotes. The second macro argument defines a variable of type
|
368 |
| - `py::module_` which can be used to initialize the module. |
| 411 | + ``py::module_`` which can be used to initialize the module. |
369 | 412 |
|
370 | 413 | The entry point is marked as "maybe unused" to aid dead-code detection analysis:
|
371 | 414 | since the entry point is typically only looked up at runtime and not referenced
|
|
382 | 425 | });
|
383 | 426 | }
|
384 | 427 |
|
385 |
| - The third macro argument is optional (available since 2.13.0), and can be used to |
386 |
| - mark the extension module as safe to run without the GIL under a free-threaded CPython |
387 |
| - interpreter. Passing this argument has no effect on other interpreters. |
| 428 | + The third and subsequent macro arguments are optional (available since 2.13.0), and |
| 429 | + can be used to mark the extension module as supporting various Python features. |
| 430 | +
|
| 431 | + - ``mod_gil_not_used()`` |
| 432 | + - ``multiple_interpreters::per_interpreter_gil()`` |
| 433 | + - ``multiple_interpreters::per_interprshareeter_gil()`` |
388 | 434 |
|
389 | 435 | .. code-block:: cpp
|
390 | 436 |
|
391 | 437 | PYBIND11_MODULE(example, m, py::mod_gil_not_used()) {
|
392 | 438 | m.doc() = "pybind11 example module safe to run without the GIL";
|
393 |
| -
|
394 |
| - // Add bindings here |
395 | 439 | m.def("foo", []() {
|
396 | 440 | return "Hello, Free-threaded World!";
|
397 | 441 | });
|
|
401 | 445 | PYBIND11_WARNING_PUSH
|
402 | 446 | PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments")
|
403 | 447 | #define PYBIND11_MODULE(name, variable, ...) \
|
404 |
| - static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name); \ |
405 |
| - static ::pybind11::module_::slots_array PYBIND11_CONCAT(pybind11_module_slots_, name); \ |
406 |
| - static int PYBIND11_CONCAT(pybind11_exec_, name)(PyObject *); \ |
407 |
| - static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \ |
408 |
| - PYBIND11_PLUGIN_IMPL(name) { \ |
409 |
| - PYBIND11_CHECK_PYTHON_VERSION \ |
410 |
| - PYBIND11_ENSURE_INTERNALS_READY \ |
411 |
| - static auto result = []() { \ |
412 |
| - auto &slots = PYBIND11_CONCAT(pybind11_module_slots_, name); \ |
413 |
| - slots[0] = {Py_mod_exec, \ |
414 |
| - reinterpret_cast<void *>(&PYBIND11_CONCAT(pybind11_exec_, name))}; \ |
415 |
| - slots[1] = {0, nullptr}; \ |
416 |
| - return ::pybind11::module_::initialize_multiphase_module_def( \ |
417 |
| - PYBIND11_TOSTRING(name), \ |
418 |
| - nullptr, \ |
419 |
| - &PYBIND11_CONCAT(pybind11_module_def_, name), \ |
420 |
| - slots, \ |
421 |
| - ##__VA_ARGS__); \ |
422 |
| - }(); \ |
423 |
| - return result.ptr(); \ |
424 |
| - } \ |
425 |
| - int PYBIND11_CONCAT(pybind11_exec_, name)(PyObject * pm) { \ |
426 |
| - pybind11::detail::get_num_interpreters_seen() += 1; \ |
427 |
| - try { \ |
428 |
| - auto m = pybind11::reinterpret_borrow<::pybind11::module_>(pm); \ |
429 |
| - PYBIND11_CONCAT(pybind11_init_, name)(m); \ |
430 |
| - return 0; \ |
431 |
| - } \ |
432 |
| - PYBIND11_CATCH_INIT_EXCEPTIONS \ |
433 |
| - return -1; \ |
434 |
| - } \ |
435 |
| - void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ & (variable)) |
| 448 | + PYBIND11_MODULE_PYINIT( \ |
| 449 | + name, (pybind11::detail::get_num_interpreters_seen() += 1), ##__VA_ARGS__) \ |
| 450 | + PYBIND11_MODULE_EXEC(name, variable) |
436 | 451 | PYBIND11_WARNING_POP
|
437 | 452 |
|
438 | 453 | PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
|
0 commit comments