Skip to content

Commit 198fd9a

Browse files
committed
document Py_HASH_* macros
1 parent da65f38 commit 198fd9a

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

Doc/c-api/hash.rst

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,66 @@ See also the :c:member:`PyTypeObject.tp_hash` member and :ref:`numeric-hash`.
1717

1818
.. versionadded:: 3.2
1919

20+
.. c:macro:: Py_HASH_ALGORITHM
21+
22+
A numerical value indicating the algorithm for hashing of :class:`str`,
23+
:class:`bytes`, and :class:`memoryview`.
24+
25+
The algorithm name is exposed by :data:`sys.hash_info.algorithm`.
26+
27+
.. c:macro:: Py_HASH_FNV
28+
Py_HASH_SIPHASH13
29+
Py_HASH_SIPHASH24
30+
31+
Numerical values to compare to :c:macro:`Py_HASH_ALGORITHM` to determine
32+
which algorithm is used for hashing. The hash algorithm can be configured
33+
via the configure :option:`--with-hash-algorithm` option.
34+
35+
.. c:macro:: Py_HASH_EXTERNAL
36+
37+
If :c:macro:`Py_HASH_ALGORITHM` is set to that value, this means that
38+
the hash function is externally implemented, that is, embedders must
39+
provide a definition for ``extern PyHash_FuncDef PyHash_Func`` when
40+
building Python:
41+
42+
.. code-block:: c
43+
44+
static Py_hash_t
45+
my_siphash24(const void *src, Py_ssize_t src_sz) { ... }
46+
47+
PyHash_FuncDef PyHash_Func = {
48+
.hash = my_siphash24,
49+
.name = "my_siphash24",
50+
.hash_bits = 64,
51+
.seed_bits = 128,
52+
};
53+
54+
.. availability:: Unix
55+
56+
.. c:macro:: Py_HASH_CUTOFF
57+
58+
Buffers of length in range ``[1, Py_HASH_CUTOFF)`` are hashed using DJBX33A
59+
instead of the algorithm described by :c:macro:`Py_HASH_ALGORITHM`.
60+
61+
- A :c:macro:`!Py_HASH_CUTOFF` of 0 disables the optimization.
62+
- :c:macro:`!Py_HASH_CUTOFF` must non-negative and less or equal than 7.
63+
64+
32-bit platforms should use a cutoff smaller than 64-bit platforms because
65+
it is easier to create colliding strings. A cutoff of 7 on 64-bit platforms
66+
and 5 on 32-bit platforms should provide a decent safety margin.
67+
2068
.. c:macro:: PyHASH_MODULUS
2169
22-
The `Mersenne prime <https://en.wikipedia.org/wiki/Mersenne_prime>`_ ``P = 2**n -1``, used for numeric hash scheme.
70+
The `Mersenne prime <https://en.wikipedia.org/wiki/Mersenne_prime>`_ ``P = 2**n -1``,
71+
used for numeric hash scheme.
72+
This corresponds to the :data:`sys.hash_info.modulus` constant.
2373

2474
.. versionadded:: 3.13
2575

2676
.. c:macro:: PyHASH_BITS
2777
2878
The exponent ``n`` of ``P`` in :c:macro:`PyHASH_MODULUS`.
79+
This corresponds to the :data:`sys.hash_info.hash_bits` constant.
2980

3081
.. versionadded:: 3.13
3182

@@ -38,12 +89,14 @@ See also the :c:member:`PyTypeObject.tp_hash` member and :ref:`numeric-hash`.
3889
.. c:macro:: PyHASH_INF
3990
4091
The hash value returned for a positive infinity.
92+
This corresponds to the :data:`sys.hash_info.inf` constant.
4193

4294
.. versionadded:: 3.13
4395

4496
.. c:macro:: PyHASH_IMAG
4597
4698
The multiplier used for the imaginary part of a complex number.
99+
This corresponds to the :data:`sys.hash_info.imag` constant.
47100

48101
.. versionadded:: 3.13
49102

0 commit comments

Comments
 (0)