/
test_pep_587.rs
76 lines (61 loc) · 2.25 KB
/
test_pep_587.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#![cfg(all(Py_3_8, not(any(PyPy, Py_LIMITED_API))))]
use pyo3::ffi;
#[cfg(Py_3_10)]
use widestring::WideCString;
#[test]
fn test_default_interpreter() {
macro_rules! ensure {
($py_call:expr) => {{
let status = $py_call;
unsafe {
if ffi::PyStatus_Exception(status) != 0 {
ffi::Py_ExitStatusException(status);
}
}
}};
}
let mut preconfig = unsafe { std::mem::zeroed() };
unsafe { ffi::PyPreConfig_InitPythonConfig(&mut preconfig) };
preconfig.utf8_mode = 1;
ensure!(unsafe { ffi::Py_PreInitialize(&preconfig) });
let mut config = unsafe { std::mem::zeroed() };
unsafe { ffi::PyConfig_InitPythonConfig(&mut config) };
// Require manually calling _Py_InitializeMain to exercise more ffi code
config._init_main = 0;
// Set program_name as regression test for #2370
#[cfg(all(Py_3_10, windows))]
{
use libc::wchar_t;
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
let mut value: Vec<wchar_t> = OsStr::new("some_test\0").encode_wide().collect();
unsafe {
ffi::PyConfig_SetString(&mut config, &mut config.program_name, value.as_ptr());
}
}
#[cfg(all(Py_3_10, unix))]
{
unsafe {
ffi::PyConfig_SetBytesString(
&mut config,
&mut config.program_name,
"some_test\0".as_ptr().cast(),
);
}
}
ensure!(unsafe { ffi::Py_InitializeFromConfig(&config) });
// The GIL is held.
assert_eq!(unsafe { ffi::PyGILState_Check() }, 1);
// Now proceed with the Python main initialization. This will initialize
// importlib. And if the custom importlib bytecode was registered above,
// our extension module will get imported and initialized.
ensure!(unsafe { ffi::_Py_InitializeMain() });
// The GIL is held after finishing initialization.
assert_eq!(unsafe { ffi::PyGILState_Check() }, 1);
// Confirm program name set above was picked up correctly
#[cfg(Py_3_10)]
{
let program_name = unsafe { WideCString::from_ptr_str(ffi::Py_GetProgramName().cast()) };
assert_eq!(program_name.to_string().unwrap(), "some_test");
}
}