Skip to content

add Py_HashBuffer to pyo3-ffi #5086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 28, 2025
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
3 changes: 3 additions & 0 deletions newsfragments/5086.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- add `Py_HashBuffer` to `pyo3-ffi`
- add `Py_HashPointer` to `pyo3-ffi`
- add `PyObject_GenericHash` to `pyo3-ffi`
2 changes: 2 additions & 0 deletions pyo3-ffi/src/compat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ macro_rules! compat_function {

mod py_3_10;
mod py_3_13;
mod py_3_14;
mod py_3_9;

pub use self::py_3_10::*;
pub use self::py_3_13::*;
pub use self::py_3_14::*;
pub use self::py_3_9::*;
26 changes: 26 additions & 0 deletions pyo3-ffi/src/compat/py_3_14.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
compat_function!(
originally_defined_for(all(Py_3_14, not(Py_LIMITED_API)));

#[inline]
pub unsafe fn Py_HashBuffer(
ptr: *const std::ffi::c_void,
len: crate::Py_ssize_t,
) -> crate::Py_hash_t {
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
{
crate::_Py_HashBytes(ptr, len)
}

#[cfg(any(Py_LIMITED_API, PyPy))]
{
let bytes = crate::PyBytes_FromStringAndSize(ptr as *const std::os::raw::c_char, len);
if bytes.is_null() {
-1
} else {
let result = crate::PyObject_Hash(bytes);
crate::Py_DECREF(bytes);
result
}
}
}
);
2 changes: 2 additions & 0 deletions pyo3-ffi/src/cpython/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub(crate) mod pythonrun;
// skipped sysmodule.h
pub(crate) mod floatobject;
pub(crate) mod pyframe;
pub(crate) mod pyhash;
pub(crate) mod tupleobject;
pub(crate) mod unicodeobject;
pub(crate) mod weakrefobject;
Expand Down Expand Up @@ -73,6 +74,7 @@ pub use self::pydebug::*;
pub use self::pyerrors::*;
#[cfg(all(Py_3_11, not(PyPy)))]
pub use self::pyframe::*;
pub use self::pyhash::*;
#[cfg(all(Py_3_8, not(PyPy)))]
pub use self::pylifecycle::*;
pub use self::pymem::*;
Expand Down
30 changes: 30 additions & 0 deletions pyo3-ffi/src/cpython/pyhash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#[cfg(Py_3_13)]
use crate::PyObject;
use crate::{Py_hash_t, Py_ssize_t};
use std::os::raw::{c_char, c_int, c_void};

#[repr(C)]
#[derive(Copy, Clone)]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#[derive(Copy, Clone)]
#[derive(Copy, Clone)]
#[cfg(not(any(PyPy, GraalPy)))]`

... and similar for PyHash_GetFuncDef, perhaps.

pub struct PyHash_FuncDef {
pub hash: Option<extern "C" fn(arg1: *const c_void, arg2: Py_ssize_t) -> Py_hash_t>,
pub name: *const c_char,
pub hash_bits: c_int,
pub seed_bits: c_int,
}

impl Default for PyHash_FuncDef {
#[inline]
fn default() -> Self {
unsafe { std::mem::zeroed() }
}
}

extern "C" {
pub fn PyHash_GetFuncDef() -> *mut PyHash_FuncDef;
#[cfg(Py_3_13)]
pub fn Py_HashPointer(ptr: *const c_void) -> Py_hash_t;
#[cfg(Py_3_13)]
pub fn PyObject_GenericHash(obj: *mut PyObject) -> Py_hash_t;
#[cfg(Py_3_14)]
pub fn Py_HashBuffer(ptr: *const c_void, len: Py_ssize_t) -> Py_hash_t;
}
25 changes: 0 additions & 25 deletions pyo3-ffi/src/pyhash.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
use crate::pyport::{Py_hash_t, Py_ssize_t};
#[cfg(not(any(Py_LIMITED_API, PyPy, GraalPy)))]
use std::os::raw::c_char;
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
use std::os::raw::c_void;

Expand All @@ -22,29 +20,6 @@ pub const _PyHASH_MULTIPLIER: c_ulong = 1000003;

// 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<extern "C" fn(arg1: *const c_void, arg2: Py_ssize_t) -> 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;
}

// skipped Py_HASH_CUTOFF

pub const Py_HASH_EXTERNAL: c_int = 0;
Expand Down
Loading