Skip to content

Commit 5fcc5d7

Browse files
davidhewittIcxolu
andauthored
update FFI definitions in objimpl.h to not expose private symbols (#5591)
* update FFI definitions in `objimpl.h` to not expose private variables * newsfragments * Apply suggestions from code review Co-authored-by: Icxolu <[email protected]> --------- Co-authored-by: Icxolu <[email protected]>
1 parent 58ba6dc commit 5fcc5d7

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

newsfragments/5591.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add FFI definitions `PyObject_New`, `PyObject_NewVar`, `PyObject_GC_Resize`, `PyObject_GC_New`, and `PyObject_GC_NewVar`.

newsfragments/5591.removed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove private FFI definitions `_PyObject_New`, `_PyObject_NewVar`, `_PyObject_GC_Resize`, `_PyObject_GC_New`, and `_PyObject_GC_NewVar`.

pyo3-ffi/src/objimpl.rs

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,27 @@ extern "C" {
3333
// skipped PyObject_INIT_VAR
3434

3535
#[cfg_attr(PyPy, link_name = "_PyPyObject_New")]
36-
pub fn _PyObject_New(arg1: *mut PyTypeObject) -> *mut PyObject;
36+
fn _PyObject_New(typeobj: *mut PyTypeObject) -> *mut PyObject;
3737
#[cfg_attr(PyPy, link_name = "_PyPyObject_NewVar")]
38-
pub fn _PyObject_NewVar(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyVarObject;
38+
fn _PyObject_NewVar(typeobj: *mut PyTypeObject, n: Py_ssize_t) -> *mut PyVarObject;
39+
}
40+
41+
#[inline]
42+
pub unsafe fn PyObject_New<T>(typeobj: *mut PyTypeObject) -> *mut T {
43+
_PyObject_New(typeobj).cast()
44+
}
45+
46+
// skipped PyObject_NEW
47+
48+
#[inline]
49+
pub unsafe fn PyObject_NewVar<T>(typeobj: *mut PyTypeObject, n: Py_ssize_t) -> *mut T {
50+
_PyObject_NewVar(typeobj, n).cast()
51+
}
3952

40-
// skipped PyObject_New
41-
// skipped PyObject_NEW
42-
// skipped PyObject_NewVar
43-
// skipped PyObject_NEW_VAR
53+
// skipped PyObject_NEW_VAR
4454

55+
extern "C" {
56+
#[cfg_attr(PyPy, link_name = "PyPyGC_Collect")]
4557
pub fn PyGC_Collect() -> Py_ssize_t;
4658

4759
#[cfg(Py_3_10)]
@@ -55,8 +67,6 @@ extern "C" {
5567
#[cfg(Py_3_10)]
5668
#[cfg_attr(PyPy, link_name = "PyPyGC_IsEnabled")]
5769
pub fn PyGC_IsEnabled() -> c_int;
58-
59-
// skipped PyUnstable_GC_VisitObjects
6070
}
6171

6272
#[inline]
@@ -65,24 +75,41 @@ pub unsafe fn PyType_IS_GC(t: *mut PyTypeObject) -> c_int {
6575
}
6676

6777
extern "C" {
68-
pub fn _PyObject_GC_Resize(arg1: *mut PyVarObject, arg2: Py_ssize_t) -> *mut PyVarObject;
78+
fn _PyObject_GC_Resize(op: *mut PyVarObject, n: Py_ssize_t) -> *mut PyVarObject;
79+
}
6980

70-
// skipped PyObject_GC_Resize
81+
#[inline]
82+
pub unsafe fn PyObject_GC_Resize<T>(op: *mut PyObject, n: Py_ssize_t) -> *mut T {
83+
_PyObject_GC_Resize(op.cast(), n).cast()
84+
}
7185

86+
extern "C" {
7287
#[cfg_attr(PyPy, link_name = "_PyPyObject_GC_New")]
73-
pub fn _PyObject_GC_New(arg1: *mut PyTypeObject) -> *mut PyObject;
88+
fn _PyObject_GC_New(typeobj: *mut PyTypeObject) -> *mut PyObject;
7489
#[cfg_attr(PyPy, link_name = "_PyPyObject_GC_NewVar")]
75-
pub fn _PyObject_GC_NewVar(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyVarObject;
90+
fn _PyObject_GC_NewVar(typeobj: *mut PyTypeObject, n: Py_ssize_t) -> *mut PyVarObject;
91+
7692
#[cfg(not(PyPy))]
7793
pub fn PyObject_GC_Track(arg1: *mut c_void);
94+
7895
#[cfg(not(PyPy))]
7996
pub fn PyObject_GC_UnTrack(arg1: *mut c_void);
97+
8098
#[cfg_attr(PyPy, link_name = "PyPyObject_GC_Del")]
8199
pub fn PyObject_GC_Del(arg1: *mut c_void);
100+
}
82101

83-
// skipped PyObject_GC_New
84-
// skipped PyObject_GC_NewVar
102+
#[inline]
103+
pub unsafe fn PyObject_GC_New<T>(typeobj: *mut PyTypeObject) -> *mut T {
104+
_PyObject_GC_New(typeobj).cast()
105+
}
106+
107+
#[inline]
108+
pub unsafe fn PyObject_GC_NewVar<T>(typeobj: *mut PyTypeObject, n: Py_ssize_t) -> *mut T {
109+
_PyObject_GC_NewVar(typeobj, n).cast()
110+
}
85111

112+
extern "C" {
86113
#[cfg(any(all(Py_3_9, not(PyPy)), Py_3_10))] // added in 3.9, or 3.10 on PyPy
87114
#[cfg_attr(PyPy, link_name = "PyPyObject_GC_IsTracked")]
88115
pub fn PyObject_GC_IsTracked(arg1: *mut PyObject) -> c_int;

0 commit comments

Comments
 (0)