Skip to content

Commit 2df0cb6

Browse files
authored
Merge pull request #1823 from deantvv/ffi-cleanup
ffi cleanup: pylifecycle to pystate
2 parents de87971 + ebeee22 commit 2df0cb6

File tree

7 files changed

+71
-57
lines changed

7 files changed

+71
-57
lines changed

src/ffi/cpython/ceval.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::ffi::{freefunc, PyObject, Py_tracefunc};
1+
use crate::ffi::cpython::pystate::Py_tracefunc;
2+
use crate::ffi::object::{freefunc, PyObject};
23
use std::os::raw::c_int;
34

45
extern "C" {

src/ffi/cpython/pymem.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use libc::size_t;
2+
use std::os::raw::c_void;
3+
4+
extern "C" {
5+
#[cfg_attr(PyPy, link_name = "PyPyMem_RawMalloc")]
6+
pub fn PyMem_RawMalloc(size: size_t) -> *mut c_void;
7+
#[cfg_attr(PyPy, link_name = "PyPyMem_RawCalloc")]
8+
pub fn PyMem_RawCalloc(nelem: size_t, elsize: size_t) -> *mut c_void;
9+
#[cfg_attr(PyPy, link_name = "PyPyMem_RawRealloc")]
10+
pub fn PyMem_RawRealloc(ptr: *mut c_void, new_size: size_t) -> *mut c_void;
11+
#[cfg_attr(PyPy, link_name = "PyPyMem_RawFree")]
12+
pub fn PyMem_RawFree(ptr: *mut c_void);
13+
14+
// skipped _PyMem_GetCurrentAllocatorName
15+
// skipped _PyMem_RawStrdup
16+
// skipped _PyMem_Strdup
17+
// skipped _PyMem_RawWcsdup
18+
}
19+
20+
#[repr(C)]
21+
#[derive(Copy, Clone)]
22+
pub enum PyMemAllocatorDomain {
23+
PYMEM_DOMAIN_RAW,
24+
PYMEM_DOMAIN_MEM,
25+
PYMEM_DOMAIN_OBJ,
26+
}
27+
28+
// skipped PyMemAllocatorName
29+
30+
#[repr(C)]
31+
#[derive(Copy, Clone)]
32+
pub struct PyMemAllocatorEx {
33+
pub ctx: *mut c_void,
34+
pub malloc: Option<extern "C" fn(ctx: *mut c_void, size: size_t) -> *mut c_void>,
35+
pub calloc:
36+
Option<extern "C" fn(ctx: *mut c_void, nelem: size_t, elsize: size_t) -> *mut c_void>,
37+
pub realloc:
38+
Option<extern "C" fn(ctx: *mut c_void, ptr: *mut c_void, new_size: size_t) -> *mut c_void>,
39+
pub free: Option<extern "C" fn(ctx: *mut c_void, ptr: *mut c_void)>,
40+
}
41+
42+
extern "C" {
43+
pub fn PyMem_GetAllocator(domain: PyMemAllocatorDomain, allocator: *mut PyMemAllocatorEx);
44+
pub fn PyMem_SetAllocator(domain: PyMemAllocatorDomain, allocator: *mut PyMemAllocatorEx);
45+
pub fn PyMem_SetupDebugHooks();
46+
}

src/ffi/cpython/pystate.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ extern "C" {
5555
#[cfg(not(PyPy))]
5656
#[cfg_attr(docsrs, doc(cfg(not(PyPy))))]
5757
pub fn PyThreadState_Next(tstate: *mut PyThreadState) -> *mut PyThreadState;
58-
// skipped PyThreadState_DeleteCurrent
58+
59+
#[cfg(py_sys_config = "WITH_THREAD")]
60+
#[cfg_attr(PyPy, link_name = "PyPyThreadState_DeleteCurrent")]
61+
pub fn PyThreadState_DeleteCurrent();
5962
}
6063

6164
#[cfg(Py_3_9)]

src/ffi/genobject.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ pub unsafe fn PyGen_CheckExact(op: *mut PyObject) -> c_int {
3535

3636
extern "C" {
3737
pub fn PyGen_New(frame: *mut PyFrameObject) -> *mut PyObject;
38-
// skipped PyGen_New
3938
// skipped PyGen_NewWithQualName
4039
// skipped _PyGen_SetStopIterationValue
4140
// skipped _PyGen_FetchStopIterationValue

src/ffi/pylifecycle.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ extern "C" {
3737
pub fn Py_GetPath() -> *mut wchar_t;
3838
pub fn Py_SetPath(arg1: *const wchar_t);
3939

40+
// skipped _Py_CheckPython3
41+
4042
#[cfg_attr(PyPy, link_name = "PyPy_GetVersion")]
4143
pub fn Py_GetVersion() -> *const c_char;
4244
pub fn Py_GetPlatform() -> *const c_char;

src/ffi/pymem.rs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
use libc::size_t;
22
use std::os::raw::c_void;
33

4-
#[cfg(not(Py_LIMITED_API))]
5-
extern "C" {
6-
#[cfg_attr(PyPy, link_name = "PyPyMem_RawMalloc")]
7-
pub fn PyMem_RawMalloc(size: size_t) -> *mut c_void;
8-
#[cfg_attr(PyPy, link_name = "PyPyMem_RawCalloc")]
9-
pub fn PyMem_RawCalloc(nelem: size_t, elsize: size_t) -> *mut c_void;
10-
#[cfg_attr(PyPy, link_name = "PyPyMem_RawRealloc")]
11-
pub fn PyMem_RawRealloc(ptr: *mut c_void, new_size: size_t) -> *mut c_void;
12-
#[cfg_attr(PyPy, link_name = "PyPyMem_RawFree")]
13-
pub fn PyMem_RawFree(ptr: *mut c_void);
14-
}
15-
164
extern "C" {
175
#[cfg_attr(PyPy, link_name = "PyPyMem_Malloc")]
186
pub fn PyMem_Malloc(size: size_t) -> *mut c_void;
@@ -23,32 +11,3 @@ extern "C" {
2311
#[cfg_attr(PyPy, link_name = "PyPyMem_Free")]
2412
pub fn PyMem_Free(ptr: *mut c_void);
2513
}
26-
27-
#[cfg(not(Py_LIMITED_API))]
28-
#[repr(C)]
29-
#[derive(Copy, Clone)]
30-
pub enum PyMemAllocatorDomain {
31-
PYMEM_DOMAIN_RAW,
32-
PYMEM_DOMAIN_MEM,
33-
PYMEM_DOMAIN_OBJ,
34-
}
35-
36-
#[repr(C)]
37-
#[derive(Copy, Clone)]
38-
#[cfg(not(Py_LIMITED_API))]
39-
pub struct PyMemAllocatorEx {
40-
pub ctx: *mut c_void,
41-
pub malloc: Option<extern "C" fn(ctx: *mut c_void, size: size_t) -> *mut c_void>,
42-
pub calloc:
43-
Option<extern "C" fn(ctx: *mut c_void, nelem: size_t, elsize: size_t) -> *mut c_void>,
44-
pub realloc:
45-
Option<extern "C" fn(ctx: *mut c_void, ptr: *mut c_void, new_size: size_t) -> *mut c_void>,
46-
pub free: Option<extern "C" fn(ctx: *mut c_void, ptr: *mut c_void)>,
47-
}
48-
49-
#[cfg(not(Py_LIMITED_API))]
50-
extern "C" {
51-
pub fn PyMem_GetAllocator(domain: PyMemAllocatorDomain, allocator: *mut PyMemAllocatorEx);
52-
pub fn PyMem_SetAllocator(domain: PyMemAllocatorDomain, allocator: *mut PyMemAllocatorEx);
53-
pub fn PyMem_SetupDebugHooks();
54-
}

src/ffi/pystate.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use crate::ffi::moduleobject::PyModuleDef;
33
use crate::ffi::object::PyObject;
44
use std::os::raw::c_int;
5+
56
#[cfg(not(PyPy))]
67
use std::os::raw::c_long;
78

@@ -27,15 +28,16 @@ extern "C" {
2728

2829
#[cfg(all(Py_3_8, not(PyPy)))]
2930
#[cfg_attr(docsrs, doc(all(Py_3_8, not(PyPy))))]
30-
pub fn PyInterpreterState_GetDict() -> *mut PyObject;
31+
pub fn PyInterpreterState_GetDict(arg1: *mut PyInterpreterState) -> *mut PyObject;
3132

3233
#[cfg(all(Py_3_7, not(PyPy)))]
3334
#[cfg_attr(docsrs, doc(all(Py_3_7, not(PyPy))))]
34-
pub fn PyInterpreterState_GetID() -> i64;
35+
pub fn PyInterpreterState_GetID(arg1: *mut PyInterpreterState) -> i64;
3536

3637
#[cfg(not(PyPy))]
3738
#[cfg_attr(docsrs, doc(cfg(not(PyPy))))]
3839
pub fn PyState_AddModule(arg1: *mut PyObject, arg2: *mut PyModuleDef) -> c_int;
40+
3941
#[cfg(not(PyPy))]
4042
#[cfg_attr(docsrs, doc(cfg(not(PyPy))))]
4143
pub fn PyState_RemoveModule(arg1: *mut PyModuleDef) -> c_int;
@@ -46,18 +48,21 @@ extern "C" {
4648

4749
#[cfg_attr(PyPy, link_name = "PyPyThreadState_New")]
4850
pub fn PyThreadState_New(arg1: *mut PyInterpreterState) -> *mut PyThreadState;
49-
//fn _PyThreadState_Prealloc(arg1: *mut PyInterpreterState)
50-
// -> *mut PyThreadState;
51-
//fn _PyThreadState_Init(arg1: *mut PyThreadState);
5251
#[cfg_attr(PyPy, link_name = "PyPyThreadState_Clear")]
5352
pub fn PyThreadState_Clear(arg1: *mut PyThreadState);
5453
#[cfg_attr(PyPy, link_name = "PyPyThreadState_Delete")]
5554
pub fn PyThreadState_Delete(arg1: *mut PyThreadState);
56-
#[cfg(py_sys_config = "WITH_THREAD")]
57-
#[cfg_attr(PyPy, link_name = "PyPyThreadState_DeleteCurrent")]
58-
pub fn PyThreadState_DeleteCurrent();
55+
5956
#[cfg_attr(PyPy, link_name = "PyPyThreadState_Get")]
6057
pub fn PyThreadState_Get() -> *mut PyThreadState;
58+
}
59+
60+
#[inline]
61+
pub unsafe fn PyThreadState_GET() -> *mut PyThreadState {
62+
PyThreadState_Get()
63+
}
64+
65+
extern "C" {
6166
#[cfg_attr(PyPy, link_name = "PyPyThreadState_Swap")]
6267
pub fn PyThreadState_Swap(arg1: *mut PyThreadState) -> *mut PyThreadState;
6368
#[cfg_attr(PyPy, link_name = "PyPyThreadState_GetDict")]
@@ -67,6 +72,10 @@ extern "C" {
6772
pub fn PyThreadState_SetAsyncExc(arg1: c_long, arg2: *mut PyObject) -> c_int;
6873
}
6974

75+
// skipped non-limited / 3.9 PyThreadState_GetInterpreter
76+
// skipped non-limited / 3.9 PyThreadState_GetFrame
77+
// skipped non-limited / 3.9 PyThreadState_GetID
78+
7079
#[repr(C)]
7180
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7281
pub enum PyGILState_STATE {
@@ -83,8 +92,3 @@ extern "C" {
8392
#[cfg_attr(docsrs, doc(cfg(not(PyPy))))]
8493
pub fn PyGILState_GetThisThreadState() -> *mut PyThreadState;
8594
}
86-
87-
#[inline]
88-
pub unsafe fn PyThreadState_GET() -> *mut PyThreadState {
89-
PyThreadState_Get()
90-
}

0 commit comments

Comments
 (0)