Skip to content

Commit cf7204a

Browse files
skirpichevvstinner
authored andcommitted
gh-111389: expose PyHASH_INF/BITS/MODULUS/IMAG macros as public (#111418)
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent e0e87af commit cf7204a

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

Doc/c-api/hash.rst

+24-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
PyHash API
44
----------
55

6-
See also the :c:member:`PyTypeObject.tp_hash` member.
6+
See also the :c:member:`PyTypeObject.tp_hash` member and :ref:`numeric-hash`.
77

88
.. c:type:: Py_hash_t
99
@@ -17,6 +17,29 @@ See also the :c:member:`PyTypeObject.tp_hash` member.
1717

1818
.. versionadded:: 3.2
1919

20+
.. c:macro:: PyHASH_MODULUS
21+
22+
The `Mersenne prime <https://en.wikipedia.org/wiki/Mersenne_prime>`_ ``P = 2**n -1``, used for numeric hash scheme.
23+
24+
.. versionadded:: 3.13
25+
26+
.. c:macro:: PyHASH_BITS
27+
28+
The exponent ``n`` of ``P`` in :c:macro:`PyHASH_MODULUS`.
29+
30+
.. versionadded:: 3.13
31+
32+
.. c:macro:: PyHASH_INF
33+
34+
The hash value returned for a positive infinity.
35+
36+
.. versionadded:: 3.13
37+
38+
.. c:macro:: PyHASH_IMAG
39+
40+
The multiplier used for the imaginary part of a complex number.
41+
42+
.. versionadded:: 3.13
2043

2144
.. c:type:: PyHash_FuncDef
2245

Include/cpython/pyhash.h

+11-5
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@
1010
reduction modulo the prime 2**_PyHASH_BITS - 1. */
1111

1212
#if SIZEOF_VOID_P >= 8
13-
# define _PyHASH_BITS 61
13+
# define PyHASH_BITS 61
1414
#else
15-
# define _PyHASH_BITS 31
15+
# define PyHASH_BITS 31
1616
#endif
1717

18-
#define _PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
19-
#define _PyHASH_INF 314159
20-
#define _PyHASH_IMAG _PyHASH_MULTIPLIER
18+
#define PyHASH_MODULUS (((size_t)1 << _PyHASH_BITS) - 1)
19+
#define PyHASH_INF 314159
20+
#define PyHASH_IMAG _PyHASH_MULTIPLIER
21+
22+
/* Aliases kept for backward compatibility with Python 3.12 */
23+
#define _PyHASH_BITS PyHASH_BITS
24+
#define _PyHASH_MODULUS PyHASH_MODULUS
25+
#define _PyHASH_INF PyHASH_INF
26+
#define _PyHASH_IMAG PyHASH_IMAG
2127

2228
/* Helpers for hash functions */
2329
PyAPI_FUNC(Py_hash_t) _Py_HashDouble(PyObject *, double);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :c:macro:`PyHASH_MODULUS`, :c:macro:`PyHASH_BITS`, :c:macro:`PyHASH_INF`
2+
and :c:macro:`PyHASH_IMAG` C macros. Patch by Sergey B Kirpichev.

0 commit comments

Comments
 (0)