summaryrefslogtreecommitdiffstats
path: root/Python
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 /Python
parent3d60dfbe1755e00ab20d0ee81281886be77ad5da (diff)
downloadcpython-d8e69b2c1b3388c31a6083cfdd9dc9afff5b9860.zip
cpython-d8e69b2c1b3388c31a6083cfdd9dc9afff5b9860.tar.gz
cpython-d8e69b2c1b3388c31a6083cfdd9dc9afff5b9860.tar.bz2
gh-122854: Add Py_HashBuffer() function (#122855)
Diffstat (limited to 'Python')
-rw-r--r--Python/import.c2
-rw-r--r--Python/pyhash.c15
2 files changed, 10 insertions, 7 deletions
diff --git a/Python/import.c b/Python/import.c
index f4c0d54..c9212ec 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -1174,7 +1174,7 @@ hashtable_key_from_2_strings(PyObject *str1, PyObject *str2, const char sep)
static Py_uhash_t
hashtable_hash_str(const void *key)
{
- return _Py_HashBytes(key, strlen((const char *)key));
+ return Py_HashBuffer(key, strlen((const char *)key));
}
static int
diff --git a/Python/pyhash.c b/Python/pyhash.c
index 1504fa2..216f437 100644
--- a/Python/pyhash.c
+++ b/Python/pyhash.c
@@ -22,7 +22,7 @@ extern PyHash_FuncDef PyHash_Func;
static PyHash_FuncDef PyHash_Func;
#endif
-/* Count _Py_HashBytes() calls */
+/* Count Py_HashBuffer() calls */
#ifdef Py_HASH_STATS
#define Py_HASH_STATS_MAX 32
static Py_ssize_t hashstats[Py_HASH_STATS_MAX + 1] = {0};
@@ -146,9 +146,8 @@ PyObject_GenericHash(PyObject *obj)
}
Py_hash_t
-_Py_HashBytes(const void *src, Py_ssize_t len)
+Py_HashBuffer(const void *ptr, Py_ssize_t len)
{
- Py_hash_t x;
/*
We make the hash of the empty string be 0, rather than using
(prefix ^ suffix), since this slightly obfuscates the hash secret
@@ -161,11 +160,12 @@ _Py_HashBytes(const void *src, Py_ssize_t len)
hashstats[(len <= Py_HASH_STATS_MAX) ? len : 0]++;
#endif
+ Py_hash_t x;
#if Py_HASH_CUTOFF > 0
if (len < Py_HASH_CUTOFF) {
/* Optimize hashing of very small strings with inline DJBX33A. */
Py_uhash_t hash;
- const unsigned char *p = src;
+ const unsigned char *p = ptr;
hash = 5381; /* DJBX33A starts with 5381 */
switch(len) {
@@ -186,10 +186,13 @@ _Py_HashBytes(const void *src, Py_ssize_t len)
}
else
#endif /* Py_HASH_CUTOFF */
- x = PyHash_Func.hash(src, len);
+ {
+ x = PyHash_Func.hash(ptr, len);
+ }
- if (x == -1)
+ if (x == -1) {
return -2;
+ }
return x;
}