Skip to content

Commit 387e87d

Browse files
author
Georges Racinet
authored
Merge pull request #211 from indygreg/initialization-apis
Implement PEP-587 initialization APIs
2 parents 7fb4dd2 + 599f2fb commit 387e87d

3 files changed

Lines changed: 236 additions & 2 deletions

File tree

python3-sys/src/initconfig.rs

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// This entire module is Python 3.8+ and !Py_LIMITED_API only.
2+
3+
use crate::pyport::Py_ssize_t;
4+
use libc::{c_char, c_int, c_ulong, wchar_t};
5+
6+
#[repr(C)]
7+
#[derive(Copy, Clone)]
8+
pub enum PyStatusType {
9+
_PyStatus_TYPE_OK = 0,
10+
_PyStatus_TYPE_ERROR = 1,
11+
_PyStatus_TYPE_EXIT = 2,
12+
}
13+
14+
#[repr(C)]
15+
#[derive(Copy, Clone)]
16+
pub struct PyStatus {
17+
_type: PyStatusType,
18+
func: *const c_char,
19+
err_msg: *const c_char,
20+
exitcode: c_int,
21+
}
22+
23+
#[cfg_attr(windows, link(name = "pythonXY"))]
24+
extern "C" {
25+
pub fn PyStatus_Ok() -> PyStatus;
26+
pub fn PyStatus_Error(err_msg: *const c_char) -> PyStatus;
27+
pub fn PyStatus_NoMemory() -> PyStatus;
28+
pub fn PyStatus_Exit(exitcode: c_int) -> PyStatus;
29+
30+
pub fn PyStatus_IsError(err: PyStatus) -> c_int;
31+
pub fn PyStatus_IsExit(err: PyStatus) -> c_int;
32+
pub fn PyStatus_Exception(err: PyStatus) -> c_int;
33+
}
34+
35+
#[repr(C)]
36+
#[derive(Copy, Clone)]
37+
pub struct PyWideStringList {
38+
length: Py_ssize_t,
39+
items: *mut *mut wchar_t,
40+
}
41+
42+
#[cfg_attr(windows, link(name = "pythonXY"))]
43+
extern "C" {
44+
pub fn PyWideStringList_Append(list: *mut PyWideStringList, item: *const wchar_t) -> PyStatus;
45+
pub fn PyWideStringList_Insert(
46+
list: *mut PyWideStringList,
47+
index: Py_ssize_t,
48+
item: *const wchar_t,
49+
) -> PyStatus;
50+
}
51+
52+
#[repr(C)]
53+
#[derive(Clone)]
54+
pub struct PyPreConfig {
55+
_config_init: c_int,
56+
parse_argv: c_int,
57+
isolated: c_int,
58+
use_environment: c_int,
59+
configure_locale: c_int,
60+
coerce_c_locale: c_int,
61+
coerce_c_locale_warn: c_int,
62+
#[cfg(windows)]
63+
legacy_windows_fs_encoding: c_int,
64+
utf8_mode: c_int,
65+
dev_mode: c_int,
66+
allocator: c_int,
67+
}
68+
69+
#[cfg_attr(windows, link(name = "pythonXY"))]
70+
extern "C" {
71+
pub fn PyPreConfig_InitPythonConfig(config: *mut PyPreConfig) -> ();
72+
pub fn PyPreConfig_InitIsolatedConfig(config: *mut PyPreConfig) -> ();
73+
}
74+
75+
#[repr(C)]
76+
#[derive(Clone)]
77+
pub struct PyConfig {
78+
_config_init: c_int,
79+
isolated: c_int,
80+
use_environment: c_int,
81+
dev_mode: c_int,
82+
install_signal_handlers: c_int,
83+
use_hash_seed: c_int,
84+
hash_seed: c_ulong,
85+
faulthandler: c_int,
86+
tracemalloc: c_int,
87+
import_time: c_int,
88+
show_ref_count: c_int,
89+
show_alloc_count: c_int,
90+
dump_refs: c_int,
91+
malloc_stats: c_int,
92+
filesystem_encoding: *mut wchar_t,
93+
filesystem_errors: *mut wchar_t,
94+
pycache_prefix: *mut wchar_t,
95+
parse_argv: c_int,
96+
argv: PyWideStringList,
97+
program_name: *mut wchar_t,
98+
xoptions: PyWideStringList,
99+
warnoptions: PyWideStringList,
100+
site_import: c_int,
101+
bytes_warning: c_int,
102+
inspect: c_int,
103+
interactive: c_int,
104+
optimization_level: c_int,
105+
parser_debug: c_int,
106+
write_bytecode: c_int,
107+
verbose: c_int,
108+
quiet: c_int,
109+
user_site_directory: c_int,
110+
configure_c_stdio: c_int,
111+
buffered_stdio: c_int,
112+
stdio_encoding: *mut wchar_t,
113+
stdio_errors: *mut wchar_t,
114+
#[cfg(windows)]
115+
legacy_windows_stdio: c_int,
116+
check_hash_pycs_mode: *mut wchar_t,
117+
pathconfig_warnings: c_int,
118+
pythonpath_env: *mut wchar_t,
119+
home: *mut wchar_t,
120+
module_search_paths_set: c_int,
121+
module_search_paths: PyWideStringList,
122+
executable: *mut wchar_t,
123+
base_executable: *mut wchar_t,
124+
prefix: *mut wchar_t,
125+
base_prefix: *mut wchar_t,
126+
exec_prefix: *mut wchar_t,
127+
base_exec_prefix: *mut wchar_t,
128+
skip_source_first_line: c_int,
129+
run_command: *mut wchar_t,
130+
run_module: *mut wchar_t,
131+
run_filename: *mut wchar_t,
132+
_install_importlib: c_int,
133+
_init_main: c_int,
134+
}
135+
136+
#[cfg_attr(windows, link(name = "pythonXY"))]
137+
extern "C" {
138+
pub fn PyConfig_InitPythonConfig(config: *mut PyConfig) -> ();
139+
pub fn PyConfig_InitIsolatedConfig(config: *mut PyConfig) -> ();
140+
pub fn PyConfig_Clear(config: *mut PyConfig) -> ();
141+
pub fn PyConfig_SetString(
142+
config: *mut PyConfig,
143+
config_str: *mut *mut wchar_t,
144+
value: *const wchar_t,
145+
) -> PyStatus;
146+
pub fn PyConfig_SetBytesString(
147+
config: *mut PyConfig,
148+
config_str: *mut *mut wchar_t,
149+
value: *const c_char,
150+
) -> PyStatus;
151+
pub fn PyConfig_Read(config: *mut PyConfig) -> PyStatus;
152+
pub fn PyConfig_SetBytesArgv(
153+
config: *mut PyConfig,
154+
argc: Py_ssize_t,
155+
argv: *const *mut c_char,
156+
) -> PyStatus;
157+
pub fn PyConfig_SetArgv(
158+
config: *mut PyConfig,
159+
argc: Py_ssize_t,
160+
argv: *const *mut wchar_t,
161+
) -> PyStatus;
162+
pub fn PyConfig_SetWideStringList(
163+
config: *mut PyConfig,
164+
list: *mut PyWideStringList,
165+
length: Py_ssize_t,
166+
items: *mut *mut wchar_t,
167+
) -> PyStatus;
168+
}

python3-sys/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub use crate::fileutils::*;
2828
pub use crate::floatobject::*;
2929
pub use crate::frameobject::PyFrameObject;
3030
pub use crate::import::*;
31+
#[cfg(all(Py_3_8, not(Py_LIMITED_API)))]
32+
pub use crate::initconfig::*;
3133
pub use crate::intrcheck::*;
3234
pub use crate::iterobject::*;
3335
pub use crate::listobject::*;
@@ -48,6 +50,7 @@ pub use crate::pydebug::*;
4850
pub use crate::pyerrors::*;
4951
#[cfg(Py_3_4)]
5052
pub use crate::pyhash::*;
53+
pub use crate::pylifecycle::*;
5154
pub use crate::pymem::*;
5255
pub use crate::pyport::*;
5356
pub use crate::pystate::*;
@@ -214,8 +217,7 @@ mod modsupport;
214217
// TODO some functions need to be moved to pylifecycle
215218
mod pythonrun;
216219

217-
// TODO new in 3.5
218-
// mod pylifecycle;
220+
mod pylifecycle;
219221

220222
// TODO supports PEP-384 only; needs adjustment for Python 3.3 and 3.5
221223
mod ceval;
@@ -284,3 +286,6 @@ pub mod frameobject {
284286
}
285287

286288
mod marshal;
289+
290+
#[cfg(all(Py_3_8, not(Py_LIMITED_API)))]
291+
mod initconfig;

python3-sys/src/pylifecycle.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// There are 2 pylifecycle.h in CPython. We currently only define the Py_LIMITED_API
2+
// symbols because other symbols exist in their legacy locations in other modules.
3+
4+
#[cfg(Py_3_8)]
5+
#[cfg(not(Py_LIMITED_API))]
6+
use crate::initconfig::{PyConfig, PyPreConfig, PyStatus};
7+
#[cfg(Py_3_8)]
8+
#[cfg(not(Py_LIMITED_API))]
9+
use crate::object::PyObject;
10+
#[cfg(Py_3_8)]
11+
#[cfg(not(Py_LIMITED_API))]
12+
use crate::pyport::Py_ssize_t;
13+
#[cfg(Py_3_8)]
14+
#[cfg(not(Py_LIMITED_API))]
15+
use libc::{c_char, c_int, c_void, wchar_t, FILE};
16+
17+
// Symbols from Include/pylifecycle.h
18+
19+
// TODO move these symbols from their legacy locations into this module.
20+
21+
// Symbols from Include/cpython/pylifecycle.h
22+
23+
#[cfg(Py_3_8)]
24+
#[cfg(not(Py_LIMITED_API))]
25+
#[cfg_attr(windows, link(name = "pythonXY"))]
26+
extern "C" {
27+
pub fn Py_PreInitialize(src_config: *const PyPreConfig) -> PyStatus;
28+
pub fn Py_PreInitializeFromBytesArgs(
29+
src_config: *const PyPreConfig,
30+
argc: Py_ssize_t,
31+
argv: *mut *mut c_char,
32+
) -> PyStatus;
33+
pub fn Py_PreInitializeFromArgs(
34+
src_config: *const PyPreConfig,
35+
argc: Py_ssize_t,
36+
argv: *mut *mut wchar_t,
37+
) -> PyStatus;
38+
39+
pub fn _Py_IsCoreInitialized() -> c_int;
40+
41+
pub fn Py_InitializeFromConfig(config: *const PyConfig) -> PyStatus;
42+
pub fn _Py_InitializeMain() -> PyStatus;
43+
pub fn Py_RunMain() -> c_int;
44+
45+
pub fn Py_ExitStatusException(err: PyStatus) -> ();
46+
pub fn _Py_PyAtExit(
47+
func: Option<extern "C" fn(obj: *mut PyObject) -> ()>,
48+
module: *mut PyObject,
49+
) -> ();
50+
pub fn _Py_RestoreSignals() -> ();
51+
pub fn Py_FdIsInteractive(file: *mut FILE, filename: *const c_char) -> c_int;
52+
pub fn _Py_SetProgramFullPath(path: *const wchar_t) -> ();
53+
pub fn _Py_gitidentifier() -> *const c_char;
54+
pub fn _Py_gitversion() -> *const c_char;
55+
pub fn _Py_IsFinalizing() -> c_int;
56+
pub fn _PyOS_URandom(buffer: *mut c_void, size: Py_ssize_t) -> c_int;
57+
pub fn _PyOS_URandomNonblock(buffer: *mut c_void, size: Py_ssize_t) -> c_int;
58+
pub fn _Py_CoerceLegacyLocale(warn: c_int) -> c_int;
59+
pub fn _Py_LegacyLocaleDetected(warn: c_int) -> c_int;
60+
pub fn _Py_SetLocaleFromEnv(category: c_int) -> *mut c_char;
61+
}

0 commit comments

Comments
 (0)