Skip to content

Commit 7619e62

Browse files
add PyModule_Add to pyo3-ffi (PyO3#5085)
* add `PyModule_Add` to `pyo3-ffi` * add `PyModule_AddObjectRef` to `pyo3-ffi`
1 parent 0062d5e commit 7619e62

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

newsfragments/5085.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add `PyModule_Add` to `pyo3-ffi`

pyo3-ffi/src/compat/py_3_10.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,29 @@ compat_function!(
1717
obj
1818
}
1919
);
20+
21+
compat_function!(
22+
originally_defined_for(Py_3_10);
23+
24+
#[inline]
25+
pub unsafe fn PyModule_AddObjectRef(
26+
module: *mut crate::PyObject,
27+
name: *const std::os::raw::c_char,
28+
value: *mut crate::PyObject,
29+
) -> std::os::raw::c_int {
30+
if value.is_null() && crate::PyErr_Occurred().is_null() {
31+
crate::PyErr_SetString(
32+
crate::PyExc_SystemError,
33+
c_str!("PyModule_AddObjectRef() must be called with an exception raised if value is NULL").as_ptr(),
34+
);
35+
return -1;
36+
}
37+
38+
crate::Py_XINCREF(value);
39+
let result = crate::PyModule_AddObject(module, name, value);
40+
if result < 0 {
41+
crate::Py_XDECREF(value);
42+
}
43+
result
44+
}
45+
);

pyo3-ffi/src/compat/py_3_13.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,18 @@ compat_function!(
104104
crate::PyList_SetSlice(list, 0, crate::PY_SSIZE_T_MAX, std::ptr::null_mut())
105105
}
106106
);
107+
108+
compat_function!(
109+
originally_defined_for(Py_3_13);
110+
111+
#[inline]
112+
pub unsafe fn PyModule_Add(
113+
module: *mut crate::PyObject,
114+
name: *const std::os::raw::c_char,
115+
value: *mut crate::PyObject,
116+
) -> std::os::raw::c_int {
117+
let result = crate::compat::PyModule_AddObjectRef(module, name, value);
118+
crate::Py_XDECREF(value);
119+
result
120+
}
121+
);

pyo3-ffi/src/modsupport.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ extern "C" {
3636
pub fn Py_BuildValue(arg1: *const c_char, ...) -> *mut PyObject;
3737
// skipped Py_VaBuildValue
3838

39+
#[cfg(Py_3_13)]
40+
pub fn PyModule_Add(module: *mut PyObject, name: *const c_char, value: *mut PyObject) -> c_int;
3941
#[cfg(Py_3_10)]
4042
#[cfg_attr(PyPy, link_name = "PyPyModule_AddObjectRef")]
4143
pub fn PyModule_AddObjectRef(

0 commit comments

Comments
 (0)