Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
gh-148233: Add Py_HashBuffer() to the limited C API
  • Loading branch information
vstinner committed Sep 22, 2026
commit db16b6021ac322930f6e8ff4e69ff8dde6fbfe3e
3 changes: 2 additions & 1 deletion Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,8 @@ C API changes
New features
------------

* TODO
* Add :c:func:`Py_HashBuffer` to the limited C API.
(Contributed by Victor Stinner in :gh:`148233`.)

Porting to Python 3.16
----------------------
Expand Down
2 changes: 0 additions & 2 deletions Include/cpython/pyhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,3 @@ _Py_HashPointer(const void *ptr)
}

PyAPI_FUNC(Py_hash_t) PyObject_GenericHash(PyObject *);

PyAPI_FUNC(Py_hash_t) Py_HashBuffer(const void *ptr, Py_ssize_t len);
4 changes: 4 additions & 0 deletions Include/pyhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ extern "C" {
# endif /* uint64_t && uint32_t && aligned */
#endif /* Py_HASH_ALGORITHM */

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 16)
PyAPI_FUNC(Py_hash_t) Py_HashBuffer(const void *ptr, Py_ssize_t len);
#endif

#ifndef Py_LIMITED_API
# define Py_CPYTHON_HASH_H
# include "cpython/pyhash.h"
Expand Down
5 changes: 4 additions & 1 deletion Lib/test/test_capi/test_hash.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import sys
import unittest
from test.support import import_helper

_testcapi = import_helper.import_module('_testcapi')
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')


SIZEOF_VOID_P = _testcapi.SIZEOF_VOID_P
Expand Down Expand Up @@ -79,7 +81,8 @@ def python_hash_pointer(x):
self.assertEqual(hash_pointer(VOID_P_MAX), -2)

def test_hash_buffer(self):
hash_buffer = _testcapi.hash_buffer
# Test Py_HashBuffer()
hash_buffer = _testlimitedcapi.hash_buffer

def check(data):
self.assertEqual(hash_buffer(data), hash(data))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :c:func:`Py_HashBuffer` to the limited C API. Patch by Victor Stinner.
2 changes: 1 addition & 1 deletion Modules/Setup.stdlib.in
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c _testinternalcapi/complex.c _testinternalcapi/interpreter.c _testinternalcapi/tokenizer.c _testinternalcapi/tuple.c _testinternalcapi/typecache.c
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c _testcapi/bytes.c _testcapi/object.c _testcapi/modsupport.c _testcapi/monitoring.c _testcapi/config.c _testcapi/import.c _testcapi/frame.c _testcapi/type.c _testcapi/function.c _testcapi/module.c _testcapi/weakref.c _testcapi/marshal.c
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/capsule.c _testlimitedcapi/codec.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/eval.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/import.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/slots.c _testlimitedcapi/sys.c _testlimitedcapi/threadstate.c _testlimitedcapi/tuple.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c _testlimitedcapi/version.c _testlimitedcapi/file.c _testlimitedcapi/weakref.c _testlimitedcapi/run.c _testlimitedcapi/type.c
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/capsule.c _testlimitedcapi/codec.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/eval.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/import.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/slots.c _testlimitedcapi/sys.c _testlimitedcapi/threadstate.c _testlimitedcapi/tuple.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c _testlimitedcapi/version.c _testlimitedcapi/file.c _testlimitedcapi/weakref.c _testlimitedcapi/run.c _testlimitedcapi/type.c _testlimitedcapi/hash.c
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
@MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c

Expand Down
15 changes: 0 additions & 15 deletions Modules/_testcapi/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,6 @@ hash_pointer(PyObject *Py_UNUSED(module), PyObject *arg)
}


static PyObject *
hash_buffer(PyObject *Py_UNUSED(module), PyObject *args)
{
char *ptr;
Py_ssize_t len;
if (!PyArg_ParseTuple(args, "y#", &ptr, &len)) {
return NULL;
}

Py_hash_t hash = Py_HashBuffer(ptr, len);
return long_from_hash(hash);
}


static PyObject *
object_generichash(PyObject *Py_UNUSED(module), PyObject *arg)
{
Expand All @@ -92,7 +78,6 @@ object_generichash(PyObject *Py_UNUSED(module), PyObject *arg)
static PyMethodDef test_methods[] = {
{"hash_getfuncdef", hash_getfuncdef, METH_NOARGS},
{"hash_pointer", hash_pointer, METH_O},
{"hash_buffer", hash_buffer, METH_VARARGS},
{"object_generichash", object_generichash, METH_O},
{NULL},
};
Expand Down
3 changes: 3 additions & 0 deletions Modules/_testlimitedcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ module_exec(PyObject *mod)
if (_PyTestLimitedCAPI_Init_Type(mod) < 0) {
return -1;
}
if (_PyTestLimitedCAPI_Init_Hash(mod) < 0) {
return -1;
}
return 0;
}

Expand Down
40 changes: 40 additions & 0 deletions Modules/_testlimitedcapi/hash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Need limited C API version 3.16 for Py_HashBuffer()
#define Py_LIMITED_API 0x03100000

#include "parts.h"
#include "util.h"


static PyObject*
long_from_hash(Py_hash_t hash)
{
Py_BUILD_ASSERT(sizeof(long long) >= sizeof(hash));
return PyLong_FromLongLong(hash);
}


// Test Py_HashBuffer()
static PyObject*
hash_buffer(PyObject *Py_UNUSED(module), PyObject *args)
{
char *ptr;
Py_ssize_t len;
if (!PyArg_ParseTuple(args, "y#", &ptr, &len)) {
return NULL;
}

Py_hash_t hash = Py_HashBuffer(ptr, len);
return long_from_hash(hash);
}


static PyMethodDef test_methods[] = {
{"hash_buffer", hash_buffer, METH_VARARGS},
{NULL},
};

int
_PyTestLimitedCAPI_Init_Hash(PyObject *m)
{
return PyModule_AddFunctions(m, test_methods);
}
1 change: 1 addition & 0 deletions Modules/_testlimitedcapi/parts.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ int _PyTestLimitedCAPI_Init_File(PyObject *module);
int _PyTestLimitedCAPI_Init_Weakref(PyObject *module);
int _PyTestLimitedCAPI_Init_Run(PyObject *module);
int _PyTestLimitedCAPI_Init_Type(PyObject *module);
int _PyTestLimitedCAPI_Init_Hash(PyObject *module);

#endif // Py_TESTLIMITEDCAPI_PARTS_H
1 change: 1 addition & 0 deletions PCbuild/_testlimitedcapi.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<ClCompile Include="..\Modules\_testlimitedcapi\weakref.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\run.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\type.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\hash.c" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\PC\python_nt.rc" />
Expand Down
1 change: 1 addition & 0 deletions PCbuild/_testlimitedcapi.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<ClCompile Include="..\Modules\_testlimitedcapi\weakref.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\run.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\type.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\hash.c" />
<ClCompile Include="..\Modules\_testlimitedcapi.c" />
</ItemGroup>
<ItemGroup>
Expand Down
Loading