pyo3_ffi/cpython/
pylifecycle.rs

1use crate::{PyConfig, PyPreConfig, PyStatus, Py_ssize_t};
2use libc::wchar_t;
3use std::ffi::{c_char, c_int};
4
5extern "C" {
6
7    // skipped Py_FrozenMain
8
9    pub fn Py_PreInitialize(src_config: *const PyPreConfig) -> PyStatus;
10    pub fn Py_PreInitializeFromBytesArgs(
11        src_config: *const PyPreConfig,
12        argc: Py_ssize_t,
13        argv: *mut *mut c_char,
14    ) -> PyStatus;
15    pub fn Py_PreInitializeFromArgs(
16        src_config: *const PyPreConfig,
17        argc: Py_ssize_t,
18        argv: *mut *mut wchar_t,
19    ) -> PyStatus;
20
21    pub fn Py_InitializeFromConfig(config: *const PyConfig) -> PyStatus;
22
23    pub fn Py_RunMain() -> c_int;
24
25    pub fn Py_ExitStatusException(status: PyStatus) -> !;
26
27    // skipped Py_FdIsInteractive
28}
29
30#[cfg(Py_3_12)]
31pub const PyInterpreterConfig_DEFAULT_GIL: c_int = 0;
32#[cfg(Py_3_12)]
33pub const PyInterpreterConfig_SHARED_GIL: c_int = 1;
34#[cfg(Py_3_12)]
35pub const PyInterpreterConfig_OWN_GIL: c_int = 2;
36
37#[cfg(Py_3_12)]
38#[repr(C)]
39pub struct PyInterpreterConfig {
40    pub use_main_obmalloc: c_int,
41    pub allow_fork: c_int,
42    pub allow_exec: c_int,
43    pub allow_threads: c_int,
44    pub allow_daemon_threads: c_int,
45    pub check_multi_interp_extensions: c_int,
46    pub gil: c_int,
47}
48
49#[cfg(Py_3_12)]
50pub const _PyInterpreterConfig_INIT: PyInterpreterConfig = PyInterpreterConfig {
51    use_main_obmalloc: 0,
52    allow_fork: 0,
53    allow_exec: 0,
54    allow_threads: 1,
55    allow_daemon_threads: 0,
56    check_multi_interp_extensions: 1,
57    gil: PyInterpreterConfig_OWN_GIL,
58};
59
60// https://github.com/python/cpython/blob/902de283a8303177eb95bf5bc252d2421fcbd758/Include/cpython/pylifecycle.h#L63-L65
61#[cfg(Py_3_12)]
62const _PyInterpreterConfig_LEGACY_CHECK_MULTI_INTERP_EXTENSIONS: c_int =
63    if cfg!(Py_GIL_DISABLED) { 1 } else { 0 };
64
65#[cfg(Py_3_12)]
66pub const _PyInterpreterConfig_LEGACY_INIT: PyInterpreterConfig = PyInterpreterConfig {
67    use_main_obmalloc: 1,
68    allow_fork: 1,
69    allow_exec: 1,
70    allow_threads: 1,
71    allow_daemon_threads: 1,
72    check_multi_interp_extensions: _PyInterpreterConfig_LEGACY_CHECK_MULTI_INTERP_EXTENSIONS,
73    gil: PyInterpreterConfig_SHARED_GIL,
74};
75
76extern "C" {
77    #[cfg(Py_3_12)]
78    pub fn Py_NewInterpreterFromConfig(
79        tstate_p: *mut *mut crate::PyThreadState,
80        config: *const PyInterpreterConfig,
81    ) -> PyStatus;
82}
83
84// skipped atexit_datacallbackfunc
85// skipped PyUnstable_AtExit