Skip to content

Commit 7cfb655

Browse files
authored
gh-148233: Add Py_HashBuffer() to the limited C API (#157966)
1 parent b0dda15 commit 7cfb655

16 files changed

Lines changed: 63 additions & 20 deletions

File tree

‎Doc/data/stable_abi.dat‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Doc/whatsnew/3.16.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,8 @@ C API changes
10841084
New features
10851085
------------
10861086

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

10891090
Porting to Python 3.16
10901091
----------------------

‎Include/cpython/pyhash.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,3 @@ _Py_HashPointer(const void *ptr)
5050
}
5151

5252
PyAPI_FUNC(Py_hash_t) PyObject_GenericHash(PyObject *);
53-
54-
PyAPI_FUNC(Py_hash_t) Py_HashBuffer(const void *ptr, Py_ssize_t len);

‎Include/pyhash.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ extern "C" {
4747
# endif /* uint64_t && uint32_t && aligned */
4848
#endif /* Py_HASH_ALGORITHM */
4949

50+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 16)
51+
PyAPI_FUNC(Py_hash_t) Py_HashBuffer(const void *ptr, Py_ssize_t len);
52+
#endif
53+
5054
#ifndef Py_LIMITED_API
5155
# define Py_CPYTHON_HASH_H
5256
# include "cpython/pyhash.h"

‎Lib/test/test_capi/test_hash.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import sys
22
import unittest
33
from test.support import import_helper
4+
45
_testcapi = import_helper.import_module('_testcapi')
6+
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
57

68

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

8183
def test_hash_buffer(self):
82-
hash_buffer = _testcapi.hash_buffer
84+
# Test Py_HashBuffer()
85+
hash_buffer = _testlimitedcapi.hash_buffer
8386

8487
def check(data):
8588
self.assertEqual(hash_buffer(data), hash(data))

‎Lib/test/test_stable_abi_ctypes.py‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :c:func:`Py_HashBuffer` to the limited C API. Patch by Victor Stinner.

‎Misc/stable_abi.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,6 +2773,8 @@
27732773
added = '3.15'
27742774
[const.Py_tp_module]
27752775
added = '3.15'
2776+
[function.Py_HashBuffer]
2777+
added = '3.16'
27762778

27772779
# PEP 757 import/export API.
27782780

‎Modules/Setup.stdlib.in‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@
174174
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
175175
@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
176176
@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
177-
@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
177+
@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
178178
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
179179
@MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c
180180

‎Modules/_testcapi/hash.c‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,6 @@ hash_pointer(PyObject *Py_UNUSED(module), PyObject *arg)
6666
}
6767

6868

69-
static PyObject *
70-
hash_buffer(PyObject *Py_UNUSED(module), PyObject *args)
71-
{
72-
char *ptr;
73-
Py_ssize_t len;
74-
if (!PyArg_ParseTuple(args, "y#", &ptr, &len)) {
75-
return NULL;
76-
}
77-
78-
Py_hash_t hash = Py_HashBuffer(ptr, len);
79-
return long_from_hash(hash);
80-
}
81-
82-
8369
static PyObject *
8470
object_generichash(PyObject *Py_UNUSED(module), PyObject *arg)
8571
{
@@ -92,7 +78,6 @@ object_generichash(PyObject *Py_UNUSED(module), PyObject *arg)
9278
static PyMethodDef test_methods[] = {
9379
{"hash_getfuncdef", hash_getfuncdef, METH_NOARGS},
9480
{"hash_pointer", hash_pointer, METH_O},
95-
{"hash_buffer", hash_buffer, METH_VARARGS},
9681
{"object_generichash", object_generichash, METH_O},
9782
{NULL},
9883
};

0 commit comments

Comments
 (0)