Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4521.removed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove private FFI definitions `_Py_c_sum`, `_Py_c_diff`, `_Py_c_neg`, `_Py_c_prod`, `_Py_c_quot`, `_Py_c_pow`, `_Py_c_abs`.
36 changes: 2 additions & 34 deletions pyo3-ffi/src/complexobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,6 @@ use crate::object::*;
use std::os::raw::{c_double, c_int};
use std::ptr::addr_of_mut;

#[repr(C)]
#[derive(Copy, Clone)]
// non-limited
pub struct Py_complex {
pub real: c_double,
pub imag: c_double,
}

#[cfg(not(Py_LIMITED_API))]
extern "C" {
pub fn _Py_c_sum(left: Py_complex, right: Py_complex) -> Py_complex;
pub fn _Py_c_diff(left: Py_complex, right: Py_complex) -> Py_complex;
pub fn _Py_c_neg(complex: Py_complex) -> Py_complex;
pub fn _Py_c_prod(left: Py_complex, right: Py_complex) -> Py_complex;
pub fn _Py_c_quot(dividend: Py_complex, divisor: Py_complex) -> Py_complex;
pub fn _Py_c_pow(num: Py_complex, exp: Py_complex) -> Py_complex;
pub fn _Py_c_abs(arg: Py_complex) -> c_double;
#[cfg_attr(PyPy, link_name = "PyPyComplex_FromCComplex")]
pub fn PyComplex_FromCComplex(v: Py_complex) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyComplex_AsCComplex")]
pub fn PyComplex_AsCComplex(op: *mut PyObject) -> Py_complex;
}

#[repr(C)]
// non-limited
pub struct PyComplexObject {
pub ob_base: PyObject,
#[cfg(not(GraalPy))]
pub cval: Py_complex,
}

Copy link
Contributor

@ngoldbaum ngoldbaum Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, do you care about exposing these structs in the same locations as the C API does in all python versions? cpython/complexobject.h first appeared in 3.11 so these definitions are correct on 3.10 and older.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally I try to keep the locations matching latest CPython we support, to make reconciliation against the headers more straightforward.

If a symbol just didn't exist at all on older python versions then we can cfg it out.

I think having cfg-d implementations at different locations just makes the overall complexity a bit higher.

Of course, I don't think we've been perfect at following this exact strategy (and often lag CPython a bit).

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyComplex_Type")]
Expand All @@ -46,17 +15,16 @@ pub unsafe fn PyComplex_Check(op: *mut PyObject) -> c_int {

#[inline]
pub unsafe fn PyComplex_CheckExact(op: *mut PyObject) -> c_int {
(Py_TYPE(op) == addr_of_mut!(PyComplex_Type)) as c_int
Py_IS_TYPE(op, addr_of_mut!(PyComplex_Type))
}

extern "C" {
// skipped non-limited PyComplex_FromCComplex
#[cfg_attr(PyPy, link_name = "PyPyComplex_FromDoubles")]
pub fn PyComplex_FromDoubles(real: c_double, imag: c_double) -> *mut PyObject;

#[cfg_attr(PyPy, link_name = "PyPyComplex_RealAsDouble")]
pub fn PyComplex_RealAsDouble(op: *mut PyObject) -> c_double;
#[cfg_attr(PyPy, link_name = "PyPyComplex_ImagAsDouble")]
pub fn PyComplex_ImagAsDouble(op: *mut PyObject) -> c_double;
// skipped non-limited PyComplex_AsCComplex
// skipped non-limited _PyComplex_FormatAdvancedWriter
}
31 changes: 31 additions & 0 deletions pyo3-ffi/src/cpython/complexobject.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::PyObject;
use std::os::raw::c_double;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Py_complex {
pub real: c_double,
pub imag: c_double,
}

// skipped private function _Py_c_sum
// skipped private function _Py_c_diff
// skipped private function _Py_c_neg
// skipped private function _Py_c_prod
// skipped private function _Py_c_quot
// skipped private function _Py_c_pow
// skipped private function _Py_c_abs

#[repr(C)]
pub struct PyComplexObject {
pub ob_base: PyObject,
#[cfg(not(GraalPy))]
pub cval: Py_complex,
}

extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyComplex_FromCComplex")]
pub fn PyComplex_FromCComplex(v: Py_complex) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyComplex_AsCComplex")]
pub fn PyComplex_AsCComplex(op: *mut PyObject) -> Py_complex;
}
2 changes: 2 additions & 0 deletions pyo3-ffi/src/cpython/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub(crate) mod bytesobject;
pub(crate) mod ceval;
pub(crate) mod code;
pub(crate) mod compile;
pub(crate) mod complexobject;
#[cfg(Py_3_13)]
pub(crate) mod critical_section;
pub(crate) mod descrobject;
Expand Down Expand Up @@ -47,6 +48,7 @@ pub use self::bytesobject::*;
pub use self::ceval::*;
pub use self::code::*;
pub use self::compile::*;
pub use self::complexobject::*;
#[cfg(Py_3_13)]
pub use self::critical_section::*;
pub use self::descrobject::*;
Expand Down