summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapi
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2024-08-30 15:42:27 (GMT)
committerGitHub <noreply@github.com>2024-08-30 15:42:27 (GMT)
commitd8e69b2c1b3388c31a6083cfdd9dc9afff5b9860 (patch)
treeeb78955c066631fdd52b00ddfd26d4fb676f9e36 /Modules/_testcapi
parent3d60dfbe1755e00ab20d0ee81281886be77ad5da (diff)
downloadcpython-d8e69b2c1b3388c31a6083cfdd9dc9afff5b9860.zip
cpython-d8e69b2c1b3388c31a6083cfdd9dc9afff5b9860.tar.gz
cpython-d8e69b2c1b3388c31a6083cfdd9dc9afff5b9860.tar.bz2
gh-122854: Add Py_HashBuffer() function (#122855)
Diffstat (limited to 'Modules/_testcapi')
-rw-r--r--Modules/_testcapi/hash.c29
1 files changed, 25 insertions, 4 deletions
diff --git a/Modules/_testcapi/hash.c b/Modules/_testcapi/hash.c
index 809d537..1525344 100644
--- a/Modules/_testcapi/hash.c
+++ b/Modules/_testcapi/hash.c
@@ -46,6 +46,14 @@ hash_getfuncdef(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
static PyObject *
+long_from_hash(Py_hash_t hash)
+{
+ Py_BUILD_ASSERT(sizeof(long long) >= sizeof(hash));
+ return PyLong_FromLongLong(hash);
+}
+
+
+static PyObject *
hash_pointer(PyObject *Py_UNUSED(module), PyObject *arg)
{
void *ptr = PyLong_AsVoidPtr(arg);
@@ -54,8 +62,21 @@ hash_pointer(PyObject *Py_UNUSED(module), PyObject *arg)
}
Py_hash_t hash = Py_HashPointer(ptr);
- Py_BUILD_ASSERT(sizeof(long long) >= sizeof(hash));
- return PyLong_FromLongLong(hash);
+ return long_from_hash(hash);
+}
+
+
+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);
}
@@ -64,14 +85,14 @@ object_generichash(PyObject *Py_UNUSED(module), PyObject *arg)
{
NULLABLE(arg);
Py_hash_t hash = PyObject_GenericHash(arg);
- Py_BUILD_ASSERT(sizeof(long long) >= sizeof(hash));
- return PyLong_FromLongLong(hash);
+ return long_from_hash(hash);
}
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},
};