From 788a9f00c8d34fd1ff6e5d6bb9b6c16cffc63fcf Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Fri, 26 Jul 2024 17:00:13 +0100 Subject: [PATCH] remove private `_Py` symbols from `pyo3-ffi` --- pyo3-ffi/src/abstract_.rs | 53 +++++++--- pyo3-ffi/src/ceval.rs | 1 - pyo3-ffi/src/codecs.rs | 14 +-- pyo3-ffi/src/complexobject.rs | 34 +------ pyo3-ffi/src/cpython/abstract_.rs | 139 ++++++-------------------- pyo3-ffi/src/cpython/ceval.rs | 15 ++- pyo3-ffi/src/cpython/complexobject.rs | 33 ++++++ pyo3-ffi/src/cpython/descrobject.rs | 5 - pyo3-ffi/src/cpython/dictobject.rs | 65 ++++-------- pyo3-ffi/src/cpython/floatobject.rs | 2 +- pyo3-ffi/src/cpython/listobject.rs | 15 +++ pyo3-ffi/src/cpython/longobject.rs | 3 + pyo3-ffi/src/cpython/mod.rs | 4 + pyo3-ffi/src/cpython/objimpl.rs | 11 -- pyo3-ffi/src/cpython/pyhash.rs | 27 +++++ pyo3-ffi/src/cpython/pythonrun.rs | 17 ---- pyo3-ffi/src/cpython/unicodeobject.rs | 11 +- pyo3-ffi/src/datetime.rs | 4 +- pyo3-ffi/src/fileobject.rs | 2 - pyo3-ffi/src/floatobject.rs | 9 -- pyo3-ffi/src/intrcheck.rs | 6 -- pyo3-ffi/src/listobject.rs | 13 --- pyo3-ffi/src/longobject.rs | 46 ++++----- pyo3-ffi/src/memoryobject.rs | 3 - pyo3-ffi/src/moduleobject.rs | 6 +- pyo3-ffi/src/objimpl.rs | 60 ++++++++--- pyo3-ffi/src/pyerrors.rs | 31 +++++- pyo3-ffi/src/pyhash.rs | 45 +-------- pyo3-ffi/src/pylifecycle.rs | 4 +- pyo3-ffi/src/pystrtod.rs | 4 +- pyo3-ffi/src/setobject.rs | 19 ---- pyo3-ffi/src/weakrefobject.rs | 3 + src/ffi/tests.rs | 4 - 33 files changed, 286 insertions(+), 422 deletions(-) create mode 100644 pyo3-ffi/src/cpython/complexobject.rs create mode 100644 pyo3-ffi/src/cpython/pyhash.rs diff --git a/pyo3-ffi/src/abstract_.rs b/pyo3-ffi/src/abstract_.rs index 1899545011a..2bef27f6983 100644 --- a/pyo3-ffi/src/abstract_.rs +++ b/pyo3-ffi/src/abstract_.rs @@ -1,5 +1,7 @@ use crate::object::*; use crate::pyport::Py_ssize_t; +#[cfg(any(Py_3_12, all(Py_3_9, not(Py_LIMITED_API))))] +use libc::size_t; use std::os::raw::{c_char, c_int}; #[inline] @@ -47,22 +49,6 @@ extern "C" { ... ) -> *mut PyObject; - #[cfg(not(Py_3_13))] - #[cfg_attr(PyPy, link_name = "_PyPyObject_CallFunction_SizeT")] - pub fn _PyObject_CallFunction_SizeT( - callable_object: *mut PyObject, - format: *const c_char, - ... - ) -> *mut PyObject; - #[cfg(not(Py_3_13))] - #[cfg_attr(PyPy, link_name = "_PyPyObject_CallMethod_SizeT")] - pub fn _PyObject_CallMethod_SizeT( - o: *mut PyObject, - method: *const c_char, - format: *const c_char, - ... - ) -> *mut PyObject; - #[cfg_attr(PyPy, link_name = "PyPyObject_CallFunctionObjArgs")] pub fn PyObject_CallFunctionObjArgs(callable: *mut PyObject, ...) -> *mut PyObject; #[cfg_attr(PyPy, link_name = "PyPyObject_CallMethodObjArgs")] @@ -71,6 +57,41 @@ extern "C" { method: *mut PyObject, ... ) -> *mut PyObject; + + #[cfg(all(Py_3_12, Py_LIMITED_API))] // used as a function on the stable abi + pub fn PyVectorcall_NARGS(nargsf: libc::size_t) -> Py_ssize_t; + + #[cfg(any(Py_3_12, all(Py_3_9, not(Py_LIMITED_API))))] + #[cfg_attr(PyPy, link_name = "PyPyVectorcall_Call")] + pub fn PyVectorcall_Call( + callable: *mut PyObject, + tuple: *mut PyObject, + dict: *mut PyObject, + ) -> *mut PyObject; +} + +#[cfg(Py_3_12)] +pub const PY_VECTORCALL_ARGUMENTS_OFFSET: size_t = + 1 << (8 * std::mem::size_of::() as size_t - 1); + +extern "C" { + #[cfg_attr(Py_3_9, link_name = "PyPyObject_Vectorcall")] + #[cfg(any(Py_3_12, all(Py_3_9, not(Py_LIMITED_API))))] + pub fn PyObject_Vectorcall( + callable: *mut PyObject, + args: *const *mut PyObject, + nargsf: size_t, + kwnames: *mut PyObject, + ) -> *mut PyObject; + + #[cfg(any(Py_3_12, all(Py_3_9, not(any(Py_LIMITED_API, PyPy, GraalPy)))))] + pub fn PyObject_VectorcallMethod( + name: *mut PyObject, + args: *const *mut PyObject, + nargsf: size_t, + kwnames: *mut PyObject, + ) -> *mut PyObject; + #[cfg_attr(PyPy, link_name = "PyPyObject_Type")] pub fn PyObject_Type(o: *mut PyObject) -> *mut PyObject; #[cfg_attr(PyPy, link_name = "PyPyObject_Size")] diff --git a/pyo3-ffi/src/ceval.rs b/pyo3-ffi/src/ceval.rs index 7aae25f8c3e..7bb7e05f1d8 100644 --- a/pyo3-ffi/src/ceval.rs +++ b/pyo3-ffi/src/ceval.rs @@ -70,7 +70,6 @@ extern "C" { pub fn Py_SetRecursionLimit(arg1: c_int); #[cfg_attr(PyPy, link_name = "PyPy_GetRecursionLimit")] pub fn Py_GetRecursionLimit() -> c_int; - fn _Py_CheckRecursiveCall(_where: *mut c_char) -> c_int; } extern "C" { diff --git a/pyo3-ffi/src/codecs.rs b/pyo3-ffi/src/codecs.rs index 2fd214cbbfe..a30b21fa5de 100644 --- a/pyo3-ffi/src/codecs.rs +++ b/pyo3-ffi/src/codecs.rs @@ -1,13 +1,12 @@ use crate::object::PyObject; use std::os::raw::{c_char, c_int}; +#[cfg_attr(windows, link(name = "pythonXY"))] extern "C" { pub fn PyCodec_Register(search_function: *mut PyObject) -> c_int; #[cfg(Py_3_10)] #[cfg(not(PyPy))] pub fn PyCodec_Unregister(search_function: *mut PyObject) -> c_int; - // skipped non-limited _PyCodec_Lookup from Include/codecs.h - // skipped non-limited _PyCodec_Forget from Include/codecs.h pub fn PyCodec_KnownEncoding(encoding: *const c_char) -> c_int; pub fn PyCodec_Encode( object: *mut PyObject, @@ -19,11 +18,6 @@ extern "C" { encoding: *const c_char, errors: *const c_char, ) -> *mut PyObject; - // skipped non-limited _PyCodec_LookupTextEncoding from Include/codecs.h - // skipped non-limited _PyCodec_EncodeText from Include/codecs.h - // skipped non-limited _PyCodec_DecodeText from Include/codecs.h - // skipped non-limited _PyCodecInfo_GetIncrementalDecoder from Include/codecs.h - // skipped non-limited _PyCodecInfo_GetIncrementalEncoder from Include/codecs.h pub fn PyCodec_Encoder(encoding: *const c_char) -> *mut PyObject; pub fn PyCodec_Decoder(encoding: *const c_char) -> *mut PyObject; #[cfg_attr(PyPy, link_name = "PyPyCodec_IncrementalEncoder")] @@ -53,6 +47,8 @@ extern "C" { pub fn PyCodec_ReplaceErrors(exc: *mut PyObject) -> *mut PyObject; pub fn PyCodec_XMLCharRefReplaceErrors(exc: *mut PyObject) -> *mut PyObject; pub fn PyCodec_BackslashReplaceErrors(exc: *mut PyObject) -> *mut PyObject; - // skipped non-limited PyCodec_NameReplaceErrors from Include/codecs.h - // skipped non-limited Py_hexdigits from Include/codecs.h + pub fn PyCodec_NameReplaceErrors(exc: *mut PyObject) -> *mut PyObject; + + #[cfg(not(Py_LIMITED_API))] + pub static mut Py_hexdigits: *const c_char; } diff --git a/pyo3-ffi/src/complexobject.rs b/pyo3-ffi/src/complexobject.rs index 78bb9ccaaaf..d78df1e9488 100644 --- a/pyo3-ffi/src/complexobject.rs +++ b/pyo3-ffi/src/complexobject.rs @@ -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, -} - #[cfg_attr(windows, link(name = "pythonXY"))] extern "C" { #[cfg_attr(PyPy, link_name = "PyPyComplex_Type")] @@ -53,10 +22,9 @@ 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 } diff --git a/pyo3-ffi/src/cpython/abstract_.rs b/pyo3-ffi/src/cpython/abstract_.rs index 83295e58f61..86b62534e9f 100644 --- a/pyo3-ffi/src/cpython/abstract_.rs +++ b/pyo3-ffi/src/cpython/abstract_.rs @@ -1,10 +1,14 @@ use crate::{PyObject, Py_ssize_t}; +#[cfg(not(Py_3_11))] use std::os::raw::{c_char, c_int}; +#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] +use crate::vectorcallfunc; + #[cfg(not(Py_3_11))] use crate::Py_buffer; -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] +#[cfg(all(Py_3_8, not(any(PyPy, GraalPy, Py_3_9))))] use crate::{ vectorcallfunc, PyCallable_Check, PyThreadState, PyThreadState_GET, PyTuple_Check, PyType_HasFeature, Py_TPFLAGS_HAVE_VECTORCALL, @@ -17,20 +21,17 @@ extern "C" { pub fn _PyStack_AsDict(values: *const *mut PyObject, kwnames: *mut PyObject) -> *mut PyObject; } -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -const _PY_FASTCALL_SMALL_STACK: size_t = 5; - extern "C" { - #[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] - pub fn _Py_CheckFunctionResult( + #[cfg(all(Py_3_8, not(any(PyPy, GraalPy, Py_3_9))))] + fn _Py_CheckFunctionResult( tstate: *mut PyThreadState, callable: *mut PyObject, result: *mut PyObject, where_: *const c_char, ) -> *mut PyObject; - #[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] - pub fn _PyObject_MakeTpCall( + #[cfg(all(Py_3_8, not(any(PyPy, GraalPy, Py_3_9))))] + fn _PyObject_MakeTpCall( tstate: *mut PyThreadState, callable: *mut PyObject, args: *const *mut PyObject, @@ -39,8 +40,8 @@ extern "C" { ) -> *mut PyObject; } -#[cfg(Py_3_8)] -pub const PY_VECTORCALL_ARGUMENTS_OFFSET: size_t = +#[cfg(Py_3_8)] // NB exported as public in abstract.rs from 3.12 +const PY_VECTORCALL_ARGUMENTS_OFFSET: size_t = 1 << (8 * std::mem::size_of::() as size_t - 1); #[cfg(Py_3_8)] @@ -50,9 +51,14 @@ pub unsafe fn PyVectorcall_NARGS(n: size_t) -> Py_ssize_t { n.try_into().expect("cannot fail due to mask") } -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] +extern "C" { + #[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))] + pub fn PyVectorcall_Function(callable: *mut PyObject) -> Option; +} + +#[cfg(all(Py_3_8, not(any(PyPy, GraalPy, Py_3_9))))] #[inline(always)] -pub unsafe fn PyVectorcall_Function(callable: *mut PyObject) -> Option { +pub fn PyVectorcall_Function(callable: *mut PyObject) -> Option { assert!(!callable.is_null()); let tp = crate::Py_TYPE(callable); if PyType_HasFeature(tp, Py_TPFLAGS_HAVE_VECTORCALL) == 0 { @@ -65,9 +71,9 @@ pub unsafe fn PyVectorcall_Function(callable: *mut PyObject) -> Option *mut PyObject; - #[cfg(Py_3_8)] #[cfg_attr( all(not(any(PyPy, GraalPy)), not(Py_3_9)), @@ -125,7 +121,10 @@ extern "C" { kwdict: *mut PyObject, ) -> *mut PyObject; - #[cfg(Py_3_8)] + #[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))] + pub fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject; + + #[cfg(all(Py_3_8, not(Py_3_9)))] // definition located in src/abstract.rs from 3.9 #[cfg_attr(not(any(Py_3_9, PyPy)), link_name = "_PyVectorcall_Call")] #[cfg_attr(PyPy, link_name = "PyPyVectorcall_Call")] pub fn PyVectorcall_Call( @@ -135,46 +134,7 @@ extern "C" { ) -> *mut PyObject; } -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn _PyObject_FastCallTstate( - tstate: *mut PyThreadState, - func: *mut PyObject, - args: *const *mut PyObject, - nargs: Py_ssize_t, -) -> *mut PyObject { - _PyObject_VectorcallTstate(tstate, func, args, nargs as size_t, std::ptr::null_mut()) -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn _PyObject_FastCall( - func: *mut PyObject, - args: *const *mut PyObject, - nargs: Py_ssize_t, -) -> *mut PyObject { - _PyObject_FastCallTstate(PyThreadState_GET(), func, args, nargs) -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] -#[inline(always)] -pub unsafe fn _PyObject_CallNoArg(func: *mut PyObject) -> *mut PyObject { - _PyObject_VectorcallTstate( - PyThreadState_GET(), - func, - std::ptr::null_mut(), - 0, - std::ptr::null_mut(), - ) -} - -extern "C" { - #[cfg(PyPy)] - #[link_name = "_PyPyObject_CallNoArg"] - pub fn _PyObject_CallNoArg(func: *mut PyObject) -> *mut PyObject; -} - -#[cfg(all(Py_3_8, not(any(PyPy, GraalPy))))] +#[cfg(all(Py_3_8, not(any(PyPy, GraalPy, Py_3_9))))] // exported as a function from 3.9, see abstract.rs #[inline(always)] pub unsafe fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *mut PyObject { assert!(!arg.is_null()); @@ -185,23 +145,13 @@ pub unsafe fn PyObject_CallOneArg(func: *mut PyObject, arg: *mut PyObject) -> *m _PyObject_VectorcallTstate(tstate, func, args, nargsf, std::ptr::null_mut()) } -extern "C" { - #[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))] - pub fn PyObject_VectorcallMethod( - name: *mut PyObject, - args: *const *mut PyObject, - nargsf: size_t, - kwnames: *mut PyObject, - ) -> *mut PyObject; -} - #[cfg(all(Py_3_9, not(any(PyPy, GraalPy))))] #[inline(always)] pub unsafe fn PyObject_CallMethodNoArgs( self_: *mut PyObject, name: *mut PyObject, ) -> *mut PyObject { - PyObject_VectorcallMethod( + crate::PyObject_VectorcallMethod( name, &self_, 1 | PY_VECTORCALL_ARGUMENTS_OFFSET, @@ -218,7 +168,7 @@ pub unsafe fn PyObject_CallMethodOneArg( ) -> *mut PyObject { let args = [self_, arg]; assert!(!arg.is_null()); - PyObject_VectorcallMethod( + crate::PyObject_VectorcallMethod( name, args.as_ptr(), 2 | PY_VECTORCALL_ARGUMENTS_OFFSET, @@ -226,12 +176,6 @@ pub unsafe fn PyObject_CallMethodOneArg( ) } -// skipped _PyObject_VectorcallMethodId -// skipped _PyObject_CallMethodIdNoArgs -// skipped _PyObject_CallMethodIdOneArg - -// skipped _PyObject_HasLen - extern "C" { #[cfg_attr(PyPy, link_name = "PyPyObject_LengthHint")] pub fn PyObject_LengthHint(o: *mut PyObject, arg1: Py_ssize_t) -> Py_ssize_t; @@ -303,30 +247,3 @@ extern "C" { // predate the limited API changes. // skipped PySequence_ITEM - -pub const PY_ITERSEARCH_COUNT: c_int = 1; -pub const PY_ITERSEARCH_INDEX: c_int = 2; -pub const PY_ITERSEARCH_CONTAINS: c_int = 3; - -extern "C" { - #[cfg(not(any(PyPy, GraalPy)))] - pub fn _PySequence_IterSearch( - seq: *mut PyObject, - obj: *mut PyObject, - operation: c_int, - ) -> Py_ssize_t; -} - -// skipped _PyObject_RealIsInstance -// skipped _PyObject_RealIsSubclass - -// skipped _PySequence_BytesToCharpArray - -// skipped _Py_FreeCharPArray - -// skipped _Py_add_one_to_index_F -// skipped _Py_add_one_to_index_C - -// skipped _Py_convert_optional_to_ssize_t - -// skipped _PyNumber_Index(*mut PyObject o) diff --git a/pyo3-ffi/src/cpython/ceval.rs b/pyo3-ffi/src/cpython/ceval.rs index 6df10627d2e..908462e80dd 100644 --- a/pyo3-ffi/src/cpython/ceval.rs +++ b/pyo3-ffi/src/cpython/ceval.rs @@ -3,7 +3,13 @@ use crate::object::{freefunc, PyObject}; use std::os::raw::c_int; extern "C" { - // skipped non-limited _PyEval_CallTracing + pub fn PyEval_SetProfile(trace_func: Option, arg1: *mut PyObject); + #[cfg(Py_3_12)] + pub fn PyEval_SetProfileAllThreads(trace_func: Option, arg1: *mut PyObject); + pub fn PyEval_SetTrace(trace_func: Option, arg1: *mut PyObject); + pub fn PyEval_SetTraceAllThreads(trace_func: Option, arg1: *mut PyObject); + + // skipped PyEval_MergeCompilerFlags #[cfg(not(Py_3_11))] pub fn _PyEval_EvalFrameDefault(arg1: *mut crate::PyFrameObject, exc: c_int) -> *mut PyObject; @@ -15,7 +21,10 @@ extern "C" { exc: c_int, ) -> *mut crate::PyObject; + // skipped PyUnstable_Eval_RequestCodeExtraIndex + #[cfg(not(Py_3_13))] pub fn _PyEval_RequestCodeExtraIndex(func: freefunc) -> c_int; - pub fn PyEval_SetProfile(trace_func: Option, arg1: *mut PyObject); - pub fn PyEval_SetTrace(trace_func: Option, arg1: *mut PyObject); + + // skipped private _PyEval_SliceIndex + // skipped private _PyEval_SliceIndexNotNone } diff --git a/pyo3-ffi/src/cpython/complexobject.rs b/pyo3-ffi/src/cpython/complexobject.rs new file mode 100644 index 00000000000..ecd6649addd --- /dev/null +++ b/pyo3-ffi/src/cpython/complexobject.rs @@ -0,0 +1,33 @@ +use crate::PyObject; +use std::os::raw::c_double; + +#[repr(C)] +#[derive(Copy, Clone)] +// non-limited +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, +} + +#[cfg(not(Py_LIMITED_API))] +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; +} diff --git a/pyo3-ffi/src/cpython/descrobject.rs b/pyo3-ffi/src/cpython/descrobject.rs index 1b5ee466c8e..f465e602f48 100644 --- a/pyo3-ffi/src/cpython/descrobject.rs +++ b/pyo3-ffi/src/cpython/descrobject.rs @@ -69,10 +69,5 @@ pub struct PyWrapperDescrObject { pub d_wrapped: *mut c_void, } -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut _PyMethodWrapper_Type: PyTypeObject; -} - // skipped non-limited PyDescr_NewWrapper // skipped non-limited PyDescr_IsData diff --git a/pyo3-ffi/src/cpython/dictobject.rs b/pyo3-ffi/src/cpython/dictobject.rs index 74b970ebac2..ed036c171c6 100644 --- a/pyo3-ffi/src/cpython/dictobject.rs +++ b/pyo3-ffi/src/cpython/dictobject.rs @@ -1,6 +1,5 @@ use crate::object::*; use crate::pyport::Py_ssize_t; -use std::os::raw::c_int; opaque_struct!(PyDictKeysObject); @@ -22,55 +21,27 @@ pub struct PyDictObject { } extern "C" { - // skipped _PyDict_GetItem_KnownHash - // skipped _PyDict_GetItemIdWithError - // skipped _PyDict_GetItemStringWithError + // skipped private _PyDict_GetItem_KnownHash + // skipped private _PyDict_GetItemStringWithError // skipped PyDict_SetDefault - pub fn _PyDict_SetItem_KnownHash( - mp: *mut PyObject, - key: *mut PyObject, - item: *mut PyObject, - hash: crate::Py_hash_t, - ) -> c_int; - // skipped _PyDict_DelItem_KnownHash - // skipped _PyDict_DelItemIf - // skipped _PyDict_NewKeysForClass - pub fn _PyDict_Next( - mp: *mut PyObject, - pos: *mut Py_ssize_t, - key: *mut *mut PyObject, - value: *mut *mut PyObject, - hash: *mut crate::Py_hash_t, - ) -> c_int; + // skipped PyDict_SetDefaultRef + // skipped PyDict_GET_SIZE - // skipped _PyDict_ContainsId - pub fn _PyDict_NewPresized(minused: Py_ssize_t) -> *mut PyObject; - // skipped _PyDict_MaybeUntrack - // skipped _PyDict_HasOnlyStringKeys - // skipped _PyDict_KeysSize - // skipped _PyDict_SizeOf + + // skipped PyDict_ContainsString + + // skipped private _PyDict_NewPresized + + // skipped PyDict_Pop + // skipped PyDict_PopString // skipped _PyDict_Pop - // skipped _PyDict_Pop_KnownHash - // skipped _PyDict_FromKeys - // skipped _PyDict_HasSplitTable - // skipped _PyDict_MergeEx - // skipped _PyDict_SetItemId - // skipped _PyDict_DelItemId - // skipped _PyDict_DebugMallocStats - // skipped _PyObjectDict_SetItem - // skipped _PyDict_LoadGlobal - // skipped _PyDict_GetItemHint - // skipped _PyDictViewObject - // skipped _PyDictView_New - // skipped _PyDictView_Intersect - #[cfg(Py_3_10)] - pub fn _PyDict_Contains_KnownHash( - op: *mut PyObject, - key: *mut PyObject, - hash: crate::Py_hash_t, - ) -> c_int; + // skipped PyDict_WatchEvent + // skipped PyDict_WatchCallback + + // skipped PyDict_AddWatcher + // skipped PyDict_ClearWatcher - #[cfg(not(Py_3_10))] - pub fn _PyDict_Contains(mp: *mut PyObject, key: *mut PyObject, hash: Py_ssize_t) -> c_int; + // skipped PyDict_Watch + // skipped PyDict_Unwatch } diff --git a/pyo3-ffi/src/cpython/floatobject.rs b/pyo3-ffi/src/cpython/floatobject.rs index 8c7ee88543d..3ea8136779c 100644 --- a/pyo3-ffi/src/cpython/floatobject.rs +++ b/pyo3-ffi/src/cpython/floatobject.rs @@ -11,7 +11,7 @@ pub struct PyFloatObject { } #[inline] -pub unsafe fn _PyFloat_CAST(op: *mut PyObject) -> *mut PyFloatObject { +unsafe fn _PyFloat_CAST(op: *mut PyObject) -> *mut PyFloatObject { debug_assert_eq!(PyFloat_Check(op), 1); op.cast() } diff --git a/pyo3-ffi/src/cpython/listobject.rs b/pyo3-ffi/src/cpython/listobject.rs index 963ddfbea87..07582d9002a 100644 --- a/pyo3-ffi/src/cpython/listobject.rs +++ b/pyo3-ffi/src/cpython/listobject.rs @@ -38,3 +38,18 @@ pub unsafe fn PyList_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject pub unsafe fn PyList_GET_SIZE(op: *mut PyObject) -> Py_ssize_t { Py_SIZE(op) } + +extern "C" { + // CPython macros exported as functions on PyPy or GraalPy + #[cfg(any(PyPy, GraalPy))] + #[cfg_attr(PyPy, link_name = "PyPyList_GET_ITEM")] + #[cfg_attr(GraalPy, link_name = "PyList_GetItem")] + pub fn PyList_GET_ITEM(arg1: *mut PyObject, arg2: Py_ssize_t) -> *mut PyObject; + #[cfg(PyPy)] + #[cfg_attr(PyPy, link_name = "PyPyList_GET_SIZE")] + pub fn PyList_GET_SIZE(arg1: *mut PyObject) -> Py_ssize_t; + #[cfg(any(PyPy, GraalPy))] + #[cfg_attr(PyPy, link_name = "PyPyList_SET_ITEM")] + #[cfg_attr(GraalPy, link_name = "_PyList_SET_ITEM")] + pub fn PyList_SET_ITEM(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: *mut PyObject); +} diff --git a/pyo3-ffi/src/cpython/longobject.rs b/pyo3-ffi/src/cpython/longobject.rs index 45acaae577d..c6e54b1a7fc 100644 --- a/pyo3-ffi/src/cpython/longobject.rs +++ b/pyo3-ffi/src/cpython/longobject.rs @@ -53,6 +53,9 @@ extern "C" { // skipped PyUnstable_Long_IsCompact // skipped PyUnstable_Long_CompactValue + #[cfg_attr(PyPy, link_name = "_PyPyLong_NumBits")] + pub fn _PyLong_NumBits(obj: *mut PyObject) -> size_t; + #[cfg_attr(PyPy, link_name = "_PyPyLong_FromByteArray")] pub fn _PyLong_FromByteArray( bytes: *const c_uchar, diff --git a/pyo3-ffi/src/cpython/mod.rs b/pyo3-ffi/src/cpython/mod.rs index fad9cd72f66..eb23a3fe354 100644 --- a/pyo3-ffi/src/cpython/mod.rs +++ b/pyo3-ffi/src/cpython/mod.rs @@ -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; @@ -29,6 +30,7 @@ pub(crate) mod object; pub(crate) mod objimpl; pub(crate) mod pydebug; pub(crate) mod pyerrors; +pub(crate) mod pyhash; #[cfg(all(Py_3_8, not(PyPy)))] pub(crate) mod pylifecycle; pub(crate) mod pymem; @@ -47,6 +49,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::*; @@ -71,6 +74,7 @@ pub use self::pydebug::*; pub use self::pyerrors::*; #[cfg(Py_3_11)] pub use self::pyframe::*; +pub use self::pyhash::*; #[cfg(all(Py_3_8, not(PyPy)))] pub use self::pylifecycle::*; pub use self::pymem::*; diff --git a/pyo3-ffi/src/cpython/objimpl.rs b/pyo3-ffi/src/cpython/objimpl.rs index 3e0270ddc8f..51e43898930 100644 --- a/pyo3-ffi/src/cpython/objimpl.rs +++ b/pyo3-ffi/src/cpython/objimpl.rs @@ -9,11 +9,6 @@ use crate::object::*; // skipped _PyObject_SIZE // skipped _PyObject_VAR_SIZE -#[cfg(not(Py_3_11))] -extern "C" { - pub fn _Py_GetAllocatedBlocks() -> crate::Py_ssize_t; -} - #[cfg(not(any(PyPy, GraalPy)))] #[repr(C)] #[derive(Copy, Clone)] @@ -51,12 +46,6 @@ pub unsafe fn PyObject_IS_GC(o: *mut PyObject) -> c_int { }) as c_int } -#[cfg(not(Py_3_11))] -extern "C" { - pub fn _PyObject_GC_Malloc(size: size_t) -> *mut PyObject; - pub fn _PyObject_GC_Calloc(size: size_t) -> *mut PyObject; -} - #[inline] pub unsafe fn PyType_SUPPORTS_WEAKREFS(t: *mut PyTypeObject) -> c_int { ((*t).tp_weaklistoffset > 0) as c_int diff --git a/pyo3-ffi/src/cpython/pyhash.rs b/pyo3-ffi/src/cpython/pyhash.rs new file mode 100644 index 00000000000..d6cd3db2e07 --- /dev/null +++ b/pyo3-ffi/src/cpython/pyhash.rs @@ -0,0 +1,27 @@ +#[cfg(not(any(PyPy, GraalPy)))] +use crate::pyport::{Py_hash_t, Py_ssize_t}; +#[cfg(not(any(PyPy, GraalPy)))] +use std::os::raw::{c_char, c_int, c_void}; + +#[cfg(not(any(PyPy, GraalPy)))] +#[repr(C)] +#[derive(Copy, Clone)] +pub struct PyHash_FuncDef { + pub hash: Option Py_hash_t>, + pub name: *const c_char, + pub hash_bits: c_int, + pub seed_bits: c_int, +} + +#[cfg(not(any(PyPy, GraalPy)))] +impl Default for PyHash_FuncDef { + #[inline] + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} + +extern "C" { + #[cfg(not(any(PyPy, GraalPy)))] + pub fn PyHash_GetFuncDef() -> *mut PyHash_FuncDef; +} diff --git a/pyo3-ffi/src/cpython/pythonrun.rs b/pyo3-ffi/src/cpython/pythonrun.rs index fe78f55ca07..e1acbd3778c 100644 --- a/pyo3-ffi/src/cpython/pythonrun.rs +++ b/pyo3-ffi/src/cpython/pythonrun.rs @@ -9,24 +9,12 @@ use std::os::raw::{c_char, c_int}; extern "C" { pub fn PyRun_SimpleStringFlags(arg1: *const c_char, arg2: *mut PyCompilerFlags) -> c_int; - pub fn _PyRun_SimpleFileObject( - fp: *mut FILE, - filename: *mut PyObject, - closeit: c_int, - flags: *mut PyCompilerFlags, - ) -> c_int; pub fn PyRun_AnyFileExFlags( fp: *mut FILE, filename: *const c_char, closeit: c_int, flags: *mut PyCompilerFlags, ) -> c_int; - pub fn _PyRun_AnyFileObject( - fp: *mut FILE, - filename: *mut PyObject, - closeit: c_int, - flags: *mut PyCompilerFlags, - ) -> c_int; pub fn PyRun_SimpleFileExFlags( fp: *mut FILE, filename: *const c_char, @@ -48,11 +36,6 @@ extern "C" { filename: *const c_char, flags: *mut PyCompilerFlags, ) -> c_int; - pub fn _PyRun_InteractiveLoopObject( - fp: *mut FILE, - filename: *mut PyObject, - flags: *mut PyCompilerFlags, - ) -> c_int; #[cfg(not(any(PyPy, GraalPy, Py_3_10)))] pub fn PyParser_ASTFromString( diff --git a/pyo3-ffi/src/cpython/unicodeobject.rs b/pyo3-ffi/src/cpython/unicodeobject.rs index feb78cf0c82..be38bd3636e 100644 --- a/pyo3-ffi/src/cpython/unicodeobject.rs +++ b/pyo3-ffi/src/cpython/unicodeobject.rs @@ -403,11 +403,6 @@ pub struct PyUnicodeObject { pub data: PyUnicodeObjectData, } -extern "C" { - #[cfg(not(any(PyPy, GraalPy)))] - pub fn _PyUnicode_CheckConsistency(op: *mut PyObject, check_content: c_int) -> c_int; -} - // skipped PyUnicode_GET_SIZE // skipped PyUnicode_GET_DATA_SIZE // skipped PyUnicode_AS_UNICODE @@ -479,7 +474,7 @@ pub unsafe fn PyUnicode_KIND(op: *mut PyObject) -> c_uint { #[cfg(not(GraalPy))] #[inline] -pub unsafe fn _PyUnicode_COMPACT_DATA(op: *mut PyObject) -> *mut c_void { +unsafe fn _PyUnicode_COMPACT_DATA(op: *mut PyObject) -> *mut c_void { if PyUnicode_IS_ASCII(op) != 0 { (op as *mut PyASCIIObject).offset(1) as *mut c_void } else { @@ -489,7 +484,7 @@ pub unsafe fn _PyUnicode_COMPACT_DATA(op: *mut PyObject) -> *mut c_void { #[cfg(not(any(GraalPy, PyPy)))] #[inline] -pub unsafe fn _PyUnicode_NONCOMPACT_DATA(op: *mut PyObject) -> *mut c_void { +unsafe fn _PyUnicode_NONCOMPACT_DATA(op: *mut PyObject) -> *mut c_void { debug_assert!(!(*(op as *mut PyUnicodeObject)).data.any.is_null()); (*(op as *mut PyUnicodeObject)).data.any @@ -560,7 +555,7 @@ extern "C" { #[cfg_attr(PyPy, link_name = "PyPyUnicode_New")] pub fn PyUnicode_New(size: Py_ssize_t, maxchar: Py_UCS4) -> *mut PyObject; #[cfg_attr(PyPy, link_name = "_PyPyUnicode_Ready")] - pub fn _PyUnicode_Ready(unicode: *mut PyObject) -> c_int; + fn _PyUnicode_Ready(unicode: *mut PyObject) -> c_int; // skipped _PyUnicode_Copy diff --git a/pyo3-ffi/src/datetime.rs b/pyo3-ffi/src/datetime.rs index bbee057d561..e2f6c9224dd 100644 --- a/pyo3-ffi/src/datetime.rs +++ b/pyo3-ffi/src/datetime.rs @@ -48,7 +48,7 @@ pub struct PyDateTime_Delta { #[repr(C)] #[derive(Debug)] /// Structure representing a `datetime.time` without a `tzinfo` member. -pub struct _PyDateTime_BaseTime { +struct _PyDateTime_BaseTime { pub ob_base: PyObject, pub hashcode: Py_hash_t, pub hastzinfo: c_char, @@ -93,7 +93,7 @@ pub struct PyDateTime_Date { #[repr(C)] #[derive(Debug)] /// Structure representing a `datetime.datetime` without a `tzinfo` member. -pub struct _PyDateTime_BaseDateTime { +struct _PyDateTime_BaseDateTime { pub ob_base: PyObject, pub hashcode: Py_hash_t, pub hastzinfo: c_char, diff --git a/pyo3-ffi/src/fileobject.rs b/pyo3-ffi/src/fileobject.rs index 91618ab7fd2..664d215c946 100644 --- a/pyo3-ffi/src/fileobject.rs +++ b/pyo3-ffi/src/fileobject.rs @@ -34,5 +34,3 @@ extern "C" { pub static mut Py_HasFileSystemDefaultEncoding: c_int; // skipped 3.12-deprecated Py_UTF8Mode } - -// skipped _PyIsSelectable_fd diff --git a/pyo3-ffi/src/floatobject.rs b/pyo3-ffi/src/floatobject.rs index 65fc1d4c316..d3909834aef 100644 --- a/pyo3-ffi/src/floatobject.rs +++ b/pyo3-ffi/src/floatobject.rs @@ -36,12 +36,3 @@ extern "C" { #[cfg_attr(PyPy, link_name = "PyPyFloat_AsDouble")] pub fn PyFloat_AsDouble(arg1: *mut PyObject) -> c_double; } - -// skipped non-limited _PyFloat_Pack2 -// skipped non-limited _PyFloat_Pack4 -// skipped non-limited _PyFloat_Pack8 -// skipped non-limited _PyFloat_Unpack2 -// skipped non-limited _PyFloat_Unpack4 -// skipped non-limited _PyFloat_Unpack8 -// skipped non-limited _PyFloat_DebugMallocStats -// skipped non-limited _PyFloat_FormatAdvancedWriter diff --git a/pyo3-ffi/src/intrcheck.rs b/pyo3-ffi/src/intrcheck.rs index 3d8a58f7f2f..2052e9a7410 100644 --- a/pyo3-ffi/src/intrcheck.rs +++ b/pyo3-ffi/src/intrcheck.rs @@ -3,9 +3,6 @@ use std::os::raw::c_int; extern "C" { #[cfg_attr(PyPy, link_name = "PyPyOS_InterruptOccurred")] pub fn PyOS_InterruptOccurred() -> c_int; - #[cfg(not(Py_3_10))] - #[deprecated(note = "Not documented in Python API; see Python 3.10 release notes")] - pub fn PyOS_InitInterrupts(); pub fn PyOS_BeforeFork(); pub fn PyOS_AfterFork_Parent(); @@ -13,7 +10,4 @@ extern "C" { #[deprecated(note = "use PyOS_AfterFork_Child instead")] #[cfg_attr(PyPy, link_name = "PyPyOS_AfterFork")] pub fn PyOS_AfterFork(); - - // skipped non-limited _PyOS_IsMainThread - // skipped non-limited Windows _PyOS_SigintEvent } diff --git a/pyo3-ffi/src/listobject.rs b/pyo3-ffi/src/listobject.rs index 1096f2fe0c8..f134afaaa57 100644 --- a/pyo3-ffi/src/listobject.rs +++ b/pyo3-ffi/src/listobject.rs @@ -55,17 +55,4 @@ extern "C" { pub fn PyList_Reverse(arg1: *mut PyObject) -> c_int; #[cfg_attr(PyPy, link_name = "PyPyList_AsTuple")] pub fn PyList_AsTuple(arg1: *mut PyObject) -> *mut PyObject; - - // CPython macros exported as functions on PyPy or GraalPy - #[cfg(any(PyPy, GraalPy))] - #[cfg_attr(PyPy, link_name = "PyPyList_GET_ITEM")] - #[cfg_attr(GraalPy, link_name = "PyList_GetItem")] - pub fn PyList_GET_ITEM(arg1: *mut PyObject, arg2: Py_ssize_t) -> *mut PyObject; - #[cfg(PyPy)] - #[cfg_attr(PyPy, link_name = "PyPyList_GET_SIZE")] - pub fn PyList_GET_SIZE(arg1: *mut PyObject) -> Py_ssize_t; - #[cfg(any(PyPy, GraalPy))] - #[cfg_attr(PyPy, link_name = "PyPyList_SET_ITEM")] - #[cfg_attr(GraalPy, link_name = "_PyList_SET_ITEM")] - pub fn PyList_SET_ITEM(arg1: *mut PyObject, arg2: Py_ssize_t, arg3: *mut PyObject); } diff --git a/pyo3-ffi/src/longobject.rs b/pyo3-ffi/src/longobject.rs index 68b4ecba540..f877087b403 100644 --- a/pyo3-ffi/src/longobject.rs +++ b/pyo3-ffi/src/longobject.rs @@ -27,6 +27,7 @@ extern "C" { pub fn PyLong_FromSsize_t(arg1: Py_ssize_t) -> *mut PyObject; #[cfg_attr(PyPy, link_name = "PyPyLong_FromDouble")] pub fn PyLong_FromDouble(arg1: c_double) -> *mut PyObject; + #[cfg_attr(PyPy, link_name = "PyPyLong_AsLong")] pub fn PyLong_AsLong(arg1: *mut PyObject) -> c_long; #[cfg_attr(PyPy, link_name = "PyPyLong_AsLongAndOverflow")] @@ -39,30 +40,32 @@ extern "C" { pub fn PyLong_AsUnsignedLong(arg1: *mut PyObject) -> c_ulong; #[cfg_attr(PyPy, link_name = "PyPyLong_AsUnsignedLongMask")] pub fn PyLong_AsUnsignedLongMask(arg1: *mut PyObject) -> c_ulong; - // skipped non-limited _PyLong_AsInt + + #[cfg(Py_3_13)] + pub fn PyLong_AsInt(obj: *mut PyObject) -> c_int; + pub fn PyLong_GetInfo() -> *mut PyObject; - // skipped PyLong_AS_LONG +} - // skipped PyLong_FromPid - // skipped PyLong_AsPid - // skipped _Py_PARSE_INTPTR - // skipped _Py_PARSE_UINTPTR +#[inline(always)] +pub unsafe fn PyLong_AS_LONG(obj: *mut PyObject) -> c_long { + PyLong_AsLong(obj) +} - // skipped non-limited _PyLong_UnsignedShort_Converter - // skipped non-limited _PyLong_UnsignedInt_Converter - // skipped non-limited _PyLong_UnsignedLong_Converter - // skipped non-limited _PyLong_UnsignedLongLong_Converter - // skipped non-limited _PyLong_Size_t_Converter +// skipped PyLong_FromPid +// skipped PyLong_AsPid - // skipped non-limited _PyLong_DigitValue - // skipped non-limited _PyLong_Frexp +// skipped private _Py_PARSE_INTPTR +// skipped private _Py_PARSE_UINTPTR +extern "C" { #[cfg_attr(PyPy, link_name = "PyPyLong_AsDouble")] pub fn PyLong_AsDouble(arg1: *mut PyObject) -> c_double; #[cfg_attr(PyPy, link_name = "PyPyLong_FromVoidPtr")] pub fn PyLong_FromVoidPtr(arg1: *mut c_void) -> *mut PyObject; #[cfg_attr(PyPy, link_name = "PyPyLong_AsVoidPtr")] pub fn PyLong_AsVoidPtr(arg1: *mut PyObject) -> *mut c_void; + #[cfg_attr(PyPy, link_name = "PyPyLong_FromLongLong")] pub fn PyLong_FromLongLong(arg1: c_longlong) -> *mut PyObject; #[cfg_attr(PyPy, link_name = "PyPyLong_FromUnsignedLongLong")] @@ -75,29 +78,14 @@ extern "C" { pub fn PyLong_AsUnsignedLongLongMask(arg1: *mut PyObject) -> c_ulonglong; #[cfg_attr(PyPy, link_name = "PyPyLong_AsLongLongAndOverflow")] pub fn PyLong_AsLongLongAndOverflow(arg1: *mut PyObject, arg2: *mut c_int) -> c_longlong; + #[cfg_attr(PyPy, link_name = "PyPyLong_FromString")] pub fn PyLong_FromString( arg1: *const c_char, arg2: *mut *mut c_char, arg3: c_int, ) -> *mut PyObject; -} - -#[cfg(not(Py_LIMITED_API))] -extern "C" { - #[cfg_attr(PyPy, link_name = "_PyPyLong_NumBits")] - pub fn _PyLong_NumBits(obj: *mut PyObject) -> size_t; -} -// skipped non-limited _PyLong_Format -// skipped non-limited _PyLong_FormatWriter -// skipped non-limited _PyLong_FormatBytesWriter -// skipped non-limited _PyLong_FormatAdvancedWriter - -extern "C" { pub fn PyOS_strtoul(arg1: *const c_char, arg2: *mut *mut c_char, arg3: c_int) -> c_ulong; pub fn PyOS_strtol(arg1: *const c_char, arg2: *mut *mut c_char, arg3: c_int) -> c_long; } - -// skipped non-limited _PyLong_Rshift -// skipped non-limited _PyLong_Lshift diff --git a/pyo3-ffi/src/memoryobject.rs b/pyo3-ffi/src/memoryobject.rs index b7ef9e2ef1d..86d8a65d930 100644 --- a/pyo3-ffi/src/memoryobject.rs +++ b/pyo3-ffi/src/memoryobject.rs @@ -5,9 +5,6 @@ use std::ptr::addr_of_mut; #[cfg_attr(windows, link(name = "pythonXY"))] extern "C" { - #[cfg(not(Py_LIMITED_API))] - pub static mut _PyManagedBuffer_Type: PyTypeObject; - #[cfg_attr(PyPy, link_name = "PyPyMemoryView_Type")] pub static mut PyMemoryView_Type: PyTypeObject; } diff --git a/pyo3-ffi/src/moduleobject.rs b/pyo3-ffi/src/moduleobject.rs index ff6458f4b15..0c2e3d92d19 100644 --- a/pyo3-ffi/src/moduleobject.rs +++ b/pyo3-ffi/src/moduleobject.rs @@ -36,13 +36,11 @@ extern "C" { pub fn PyModule_GetFilename(arg1: *mut PyObject) -> *const c_char; #[cfg(not(PyPy))] pub fn PyModule_GetFilenameObject(arg1: *mut PyObject) -> *mut PyObject; - // skipped non-limited _PyModule_Clear - // skipped non-limited _PyModule_ClearDict - // skipped non-limited _PyModuleSpec_IsInitializing #[cfg_attr(PyPy, link_name = "PyPyModule_GetDef")] pub fn PyModule_GetDef(arg1: *mut PyObject) -> *mut PyModuleDef; #[cfg_attr(PyPy, link_name = "PyPyModule_GetState")] pub fn PyModule_GetState(arg1: *mut PyObject) -> *mut c_void; + #[cfg_attr(PyPy, link_name = "PyPyModuleDef_Init")] pub fn PyModuleDef_Init(arg1: *mut PyModuleDef) -> *mut PyObject; } @@ -96,8 +94,6 @@ pub const Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED: *mut c_void = 1 as *mut c_void #[cfg(Py_3_12)] pub const Py_MOD_PER_INTERPRETER_GIL_SUPPORTED: *mut c_void = 2 as *mut c_void; -// skipped non-limited _Py_mod_LAST_SLOT - #[repr(C)] pub struct PyModuleDef { pub m_base: PyModuleDef_Base, diff --git a/pyo3-ffi/src/objimpl.rs b/pyo3-ffi/src/objimpl.rs index 76835a6d158..64bd37e9f30 100644 --- a/pyo3-ffi/src/objimpl.rs +++ b/pyo3-ffi/src/objimpl.rs @@ -33,15 +33,32 @@ extern "C" { // skipped PyObject_INIT_VAR #[cfg_attr(PyPy, link_name = "_PyPyObject_New")] - pub fn _PyObject_New(arg1: *mut PyTypeObject) -> *mut PyObject; + fn _PyObject_New(arg1: *mut PyTypeObject) -> *mut PyObject; #[cfg_attr(PyPy, link_name = "_PyPyObject_NewVar")] - pub fn _PyObject_NewVar(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyVarObject; + fn _PyObject_NewVar(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyVarObject; +} + +#[inline(always)] +pub unsafe fn PyObject_New(t: *mut PyTypeObject) -> *mut PyObject { + _PyObject_New(t) +} + +#[inline(always)] +pub unsafe fn PyObject_NEW(t: *mut PyTypeObject) -> *mut PyObject { + PyObject_New(t) +} - // skipped PyObject_New - // skipped PyObject_NEW - // skipped PyObject_NewVar - // skipped PyObject_NEW_VAR +#[inline(always)] +pub unsafe fn PyObject_NewVar(t: *mut PyTypeObject, size: Py_ssize_t) -> *mut PyVarObject { + _PyObject_NewVar(t, size) +} + +#[inline(always)] +pub unsafe fn PyObject_NEW_VAR(t: *mut PyTypeObject, size: Py_ssize_t) -> *mut PyVarObject { + PyObject_NewVar(t, size) +} +extern "C" { pub fn PyGC_Collect() -> Py_ssize_t; #[cfg(Py_3_10)] @@ -55,8 +72,6 @@ extern "C" { #[cfg(Py_3_10)] #[cfg_attr(PyPy, link_name = "PyPyGC_IsEnabled")] pub fn PyGC_IsEnabled() -> c_int; - - // skipped PyUnstable_GC_VisitObjects } #[inline] @@ -65,14 +80,19 @@ pub unsafe fn PyType_IS_GC(t: *mut PyTypeObject) -> c_int { } extern "C" { - pub fn _PyObject_GC_Resize(arg1: *mut PyVarObject, arg2: Py_ssize_t) -> *mut PyVarObject; - - // skipped PyObject_GC_Resize + fn _PyObject_GC_Resize(arg1: *mut PyVarObject, arg2: Py_ssize_t) -> *mut PyVarObject; +} +#[inline(always)] +pub unsafe fn PyObject_GC_Resize(op: *mut PyVarObject, n: Py_ssize_t) -> *mut T { + _PyObject_GC_Resize(op, n).cast::() +} +// skipped PyObject_GC_Resize +extern "C" { #[cfg_attr(PyPy, link_name = "_PyPyObject_GC_New")] - pub fn _PyObject_GC_New(arg1: *mut PyTypeObject) -> *mut PyObject; + fn _PyObject_GC_New(arg1: *mut PyTypeObject) -> *mut PyObject; #[cfg_attr(PyPy, link_name = "_PyPyObject_GC_NewVar")] - pub fn _PyObject_GC_NewVar(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyVarObject; + fn _PyObject_GC_NewVar(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyVarObject; #[cfg(not(PyPy))] pub fn PyObject_GC_Track(arg1: *mut c_void); #[cfg(not(PyPy))] @@ -80,9 +100,19 @@ extern "C" { #[cfg_attr(PyPy, link_name = "PyPyObject_GC_Del")] pub fn PyObject_GC_Del(arg1: *mut c_void); - // skipped PyObject_GC_New - // skipped PyObject_GC_NewVar +} + +#[inline(always)] +pub unsafe fn PyObject_GC_New(typeobj: *mut PyTypeObject) -> *mut T { + _PyObject_GC_New(typeobj).cast::() +} +#[inline(always)] +pub unsafe fn PyObject_GC_NewVar(typeobj: *mut PyTypeObject, n: Py_ssize_t) -> *mut T { + _PyObject_GC_NewVar(typeobj, n).cast::() +} + +extern "C" { #[cfg(any(all(Py_3_9, not(PyPy)), Py_3_10))] // added in 3.9, or 3.10 on PyPy #[cfg_attr(PyPy, link_name = "PyPyObject_GC_IsTracked")] pub fn PyObject_GC_IsTracked(arg1: *mut PyObject) -> c_int; diff --git a/pyo3-ffi/src/pyerrors.rs b/pyo3-ffi/src/pyerrors.rs index 6c9313c4ab0..b5f803ddf62 100644 --- a/pyo3-ffi/src/pyerrors.rs +++ b/pyo3-ffi/src/pyerrors.rs @@ -1,6 +1,9 @@ use crate::object::*; use crate::pyport::Py_ssize_t; -use std::os::raw::{c_char, c_int}; +use std::{ + ffi::CString, + os::raw::{c_char, c_int}, +}; extern "C" { #[cfg_attr(PyPy, link_name = "PyPyErr_SetNone")] @@ -99,7 +102,15 @@ pub unsafe fn PyUnicodeDecodeError_Create( end: Py_ssize_t, reason: *const c_char, ) -> *mut PyObject { - crate::_PyObject_CallFunction_SizeT( + // This is an abi-only symbol since Python 3.13, so it's not declared in any headers. + #[link_name = "_PyPyObject_CallFunction_SizeT"] + pub(crate) fn _PyObject_CallFunction_SizeT( + callable_object: *mut PyObject, + format: *const c_char, + ... + ) -> *mut PyObject; + + _PyObject_CallFunction_SizeT( PyExc_UnicodeDecodeError, c_str!("sy#nns").as_ptr(), encoding, @@ -297,9 +308,23 @@ extern "C" { arg2: *mut PyObject, arg3: *mut PyObject, ) -> *mut PyObject; + #[cfg(PyPy)] #[cfg_attr(PyPy, link_name = "PyPyErr_BadInternalCall")] pub fn PyErr_BadInternalCall(); - pub fn _PyErr_BadInternalCall(filename: *const c_char, lineno: c_int); + #[cfg(not(PyPy))] + fn _PyErr_BadInternalCall(filename: *const c_char, lineno: c_int); +} + +#[inline(always)] +#[cfg(not(PyPy))] +pub unsafe fn PyErr_BadInternalCall() { + let location = std::panic::Location::caller(); + let filename = CString::new(location.file()); + let filename = filename.as_deref().unwrap_or(c_str!("")); + _PyErr_BadInternalCall(filename.as_ptr(), location.line() as c_int); +} + +extern "C" { #[cfg_attr(PyPy, link_name = "PyPyErr_NewException")] pub fn PyErr_NewException( name: *const c_char, diff --git a/pyo3-ffi/src/pyhash.rs b/pyo3-ffi/src/pyhash.rs index f42f9730f1b..a6295ff8728 100644 --- a/pyo3-ffi/src/pyhash.rs +++ b/pyo3-ffi/src/pyhash.rs @@ -1,47 +1,4 @@ -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -use crate::pyport::{Py_hash_t, Py_ssize_t}; -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -use std::os::raw::{c_char, c_void}; - -use std::os::raw::{c_int, c_ulong}; - -extern "C" { - // skipped non-limited _Py_HashDouble - // skipped non-limited _Py_HashPointer - // skipped non-limited _Py_HashPointerRaw - - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - pub fn _Py_HashBytes(src: *const c_void, len: Py_ssize_t) -> Py_hash_t; -} - -pub const _PyHASH_MULTIPLIER: c_ulong = 1000003; - -// skipped _PyHASH_BITS - -// skipped non-limited _Py_HashSecret_t - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -#[repr(C)] -#[derive(Copy, Clone)] -pub struct PyHash_FuncDef { - pub hash: Option Py_hash_t>, - pub name: *const c_char, - pub hash_bits: c_int, - pub seed_bits: c_int, -} - -#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] -impl Default for PyHash_FuncDef { - #[inline] - fn default() -> Self { - unsafe { std::mem::zeroed() } - } -} - -extern "C" { - #[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))] - pub fn PyHash_GetFuncDef() -> *mut PyHash_FuncDef; -} +use std::os::raw::c_int; // skipped Py_HASH_CUTOFF diff --git a/pyo3-ffi/src/pylifecycle.rs b/pyo3-ffi/src/pylifecycle.rs index 7f73e3f0e9b..539b70783e5 100644 --- a/pyo3-ffi/src/pylifecycle.rs +++ b/pyo3-ffi/src/pylifecycle.rs @@ -37,8 +37,6 @@ extern "C" { pub fn Py_GetPath() -> *mut wchar_t; pub fn Py_SetPath(arg1: *const wchar_t); - // skipped _Py_CheckPython3 - #[cfg_attr(PyPy, link_name = "PyPy_GetVersion")] pub fn Py_GetVersion() -> *const c_char; pub fn Py_GetPlatform() -> *const c_char; @@ -47,7 +45,7 @@ extern "C" { pub fn Py_GetBuildInfo() -> *const c_char; } -type PyOS_sighandler_t = unsafe extern "C" fn(arg1: c_int); +pub type PyOS_sighandler_t = unsafe extern "C" fn(arg1: c_int); extern "C" { pub fn PyOS_getsig(arg1: c_int) -> PyOS_sighandler_t; diff --git a/pyo3-ffi/src/pystrtod.rs b/pyo3-ffi/src/pystrtod.rs index 1f027686bd9..61e56932ad1 100644 --- a/pyo3-ffi/src/pystrtod.rs +++ b/pyo3-ffi/src/pystrtod.rs @@ -18,13 +18,11 @@ extern "C" { ) -> *mut c_char; } -// skipped non-limited _Py_string_to_number_with_underscores -// skipped non-limited _Py_parse_inf_or_nan - /* PyOS_double_to_string's "flags" parameter can be set to 0 or more of: */ pub const Py_DTSF_SIGN: c_int = 0x01; /* always add the sign */ pub const Py_DTSF_ADD_DOT_0: c_int = 0x02; /* if the result is an integer add ".0" */ pub const Py_DTSF_ALT: c_int = 0x04; /* "alternate" formatting. it's format_code specific */ +pub const Py_DTSF_NO_NEG: c_int = 0x08; /* negative zero result is coerced to 0 */ /* PyOS_double_to_string's "type", if non-NULL, will be set to one of: */ pub const Py_DTST_FINITE: c_int = 0; diff --git a/pyo3-ffi/src/setobject.rs b/pyo3-ffi/src/setobject.rs index 9d5351fc798..48b5b4baa62 100644 --- a/pyo3-ffi/src/setobject.rs +++ b/pyo3-ffi/src/setobject.rs @@ -39,25 +39,6 @@ pub unsafe fn PySet_GET_SIZE(so: *mut PyObject) -> Py_ssize_t { (*so).used } -#[cfg(not(Py_LIMITED_API))] -#[cfg_attr(windows, link(name = "pythonXY"))] -extern "C" { - pub static mut _PySet_Dummy: *mut PyObject; -} - -extern "C" { - #[cfg(not(Py_LIMITED_API))] - #[cfg_attr(PyPy, link_name = "_PyPySet_NextEntry")] - pub fn _PySet_NextEntry( - set: *mut PyObject, - pos: *mut Py_ssize_t, - key: *mut *mut PyObject, - hash: *mut super::Py_hash_t, - ) -> c_int; - - // skipped non-limited _PySet_Update -} - #[cfg_attr(windows, link(name = "pythonXY"))] extern "C" { #[cfg_attr(PyPy, link_name = "PyPySet_Type")] diff --git a/pyo3-ffi/src/weakrefobject.rs b/pyo3-ffi/src/weakrefobject.rs index 7e11a9012e7..fe8ec1b4c98 100644 --- a/pyo3-ffi/src/weakrefobject.rs +++ b/pyo3-ffi/src/weakrefobject.rs @@ -11,8 +11,11 @@ pub use crate::_PyWeakReference as PyWeakReference; #[cfg_attr(windows, link(name = "pythonXY"))] extern "C" { + #[cfg(not(PyPy))] pub static mut _PyWeakref_RefType: PyTypeObject; + #[cfg(not(PyPy))] pub static mut _PyWeakref_ProxyType: PyTypeObject; + #[cfg(not(PyPy))] pub static mut _PyWeakref_CallableProxyType: PyTypeObject; #[cfg(PyPy)] diff --git a/src/ffi/tests.rs b/src/ffi/tests.rs index cd01eed4b1e..ed2c9933b94 100644 --- a/src/ffi/tests.rs +++ b/src/ffi/tests.rs @@ -186,8 +186,6 @@ fn ascii() { // 2 and 4 byte macros return nonsense for this string instance. assert_eq!(PyUnicode_KIND(ptr), PyUnicode_1BYTE_KIND); - assert!(!_PyUnicode_COMPACT_DATA(ptr).is_null()); - // _PyUnicode_NONCOMPACT_DATA isn't valid for compact strings. assert!(!PyUnicode_DATA(ptr).is_null()); assert_eq!(PyUnicode_GET_LENGTH(ptr), s.len().unwrap() as Py_ssize_t); @@ -226,8 +224,6 @@ fn ucs4() { assert!(!PyUnicode_4BYTE_DATA(ptr).is_null()); assert_eq!(PyUnicode_KIND(ptr), PyUnicode_4BYTE_KIND); - assert!(!_PyUnicode_COMPACT_DATA(ptr).is_null()); - // _PyUnicode_NONCOMPACT_DATA isn't valid for compact strings. assert!(!PyUnicode_DATA(ptr).is_null()); assert_eq!(