Skip to content

Commit 3989e12

Browse files
authored
gh-141004: Document Py_HASH_* macros (#141205)
1 parent 9420795 commit 3989e12

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

Doc/c-api/hash.rst

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,94 @@ See also the :c:member:`PyTypeObject.tp_hash` member and :ref:`numeric-hash`.
1111

1212
.. versionadded:: 3.2
1313

14+
1415
.. c:type:: Py_uhash_t
1516
1617
Hash value type: unsigned integer.
1718

1819
.. versionadded:: 3.2
1920

21+
22+
.. c:macro:: Py_HASH_ALGORITHM
23+
24+
A numerical value indicating the algorithm for hashing of :class:`str`,
25+
:class:`bytes`, and :class:`memoryview`.
26+
27+
The algorithm name is exposed by :data:`sys.hash_info.algorithm`.
28+
29+
.. versionadded:: 3.4
30+
31+
32+
.. c:macro:: Py_HASH_FNV
33+
Py_HASH_SIPHASH24
34+
Py_HASH_SIPHASH13
35+
36+
Numerical values to compare to :c:macro:`Py_HASH_ALGORITHM` to determine
37+
which algorithm is used for hashing. The hash algorithm can be configured
38+
via the configure :option:`--with-hash-algorithm` option.
39+
40+
.. versionadded:: 3.4
41+
Add :c:macro:`!Py_HASH_FNV` and :c:macro:`!Py_HASH_SIPHASH24`.
42+
43+
.. versionadded:: 3.13
44+
Add :c:macro:`!Py_HASH_SIPHASH13`.
45+
46+
47+
.. c:macro:: Py_HASH_CUTOFF
48+
49+
Buffers of length in range ``[1, Py_HASH_CUTOFF)`` are hashed using DJBX33A
50+
instead of the algorithm described by :c:macro:`Py_HASH_ALGORITHM`.
51+
52+
- A :c:macro:`!Py_HASH_CUTOFF` of 0 disables the optimization.
53+
- :c:macro:`!Py_HASH_CUTOFF` must be non-negative and less or equal than 7.
54+
55+
32-bit platforms should use a cutoff smaller than 64-bit platforms because
56+
it is easier to create colliding strings. A cutoff of 7 on 64-bit platforms
57+
and 5 on 32-bit platforms should provide a decent safety margin.
58+
59+
.. versionadded:: 3.4
60+
61+
2062
.. c:macro:: PyHASH_MODULUS
2163
22-
The `Mersenne prime <https://en.wikipedia.org/wiki/Mersenne_prime>`_ ``P = 2**n -1``, used for numeric hash scheme.
64+
The `Mersenne prime <https://en.wikipedia.org/wiki/Mersenne_prime>`_ ``P = 2**n -1``,
65+
used for numeric hash scheme.
66+
This corresponds to the :data:`sys.hash_info.modulus` constant.
2367

2468
.. versionadded:: 3.13
2569

70+
2671
.. c:macro:: PyHASH_BITS
2772
2873
The exponent ``n`` of ``P`` in :c:macro:`PyHASH_MODULUS`.
74+
This corresponds to the :data:`sys.hash_info.hash_bits` constant.
2975

3076
.. versionadded:: 3.13
3177

78+
3279
.. c:macro:: PyHASH_MULTIPLIER
3380
3481
Prime multiplier used in string and various other hashes.
3582

3683
.. versionadded:: 3.13
3784

85+
3886
.. c:macro:: PyHASH_INF
3987
4088
The hash value returned for a positive infinity.
89+
This corresponds to the :data:`sys.hash_info.inf` constant.
4190

4291
.. versionadded:: 3.13
4392

93+
4494
.. c:macro:: PyHASH_IMAG
4595
4696
The multiplier used for the imaginary part of a complex number.
97+
This corresponds to the :data:`sys.hash_info.imag` constant.
4798

4899
.. versionadded:: 3.13
49100

101+
50102
.. c:type:: PyHash_FuncDef
51103
52104
Hash function definition used by :c:func:`PyHash_GetFuncDef`.

0 commit comments

Comments
 (0)