Skip to content

Commit 7539c7f

Browse files
authored
Add hash constants (#85)
1 parent 52486a9 commit 7539c7f

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

Diff for: docs/changelog.rst

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
* 2024-03-09: Add hash constants:
5+
6+
* ``PyHASH_BITS``
7+
* ``PyHASH_IMAG``
8+
* ``PyHASH_INF``
9+
* ``PyHASH_MODULUS``
10+
411
* 2024-02-20: Add PyTime API:
512

613
* ``PyTime_t`` type

Diff for: pythoncapi_compat.h

+12
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,18 @@ static inline int PyTime_PerfCounter(PyTime_t *result)
11961196

11971197
#endif
11981198

1199+
// gh-111389 added hash constants to Python 3.13.0a5. These constants were
1200+
// added first as private macros to Python 3.4.0b1 and PyPy 7.3.9.
1201+
#if (!defined(PyHASH_BITS) \
1202+
&& ((!defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x030400B1) \
1203+
|| (defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x03070000 \
1204+
&& PYPY_VERSION_NUM >= 0x07090000)))
1205+
# define PyHASH_BITS _PyHASH_BITS
1206+
# define PyHASH_MODULUS _PyHASH_MODULUS
1207+
# define PyHASH_INF _PyHASH_INF
1208+
# define PyHASH_IMAG _PyHASH_IMAG
1209+
#endif
1210+
11991211

12001212
#ifdef __cplusplus
12011213
}

Diff for: tests/test_pythoncapi_compat_cext.c

+14
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,20 @@ test_hash(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
15221522
assert(Py_HashPointer(ptr1) == (Py_hash_t)ptr1);
15231523
#endif
15241524

1525+
#if ((!defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x030400B1) \
1526+
|| (defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x03070000 \
1527+
&& PYPY_VERSION_NUM >= 0x07090000))
1528+
// Just check that constants are available
1529+
size_t bits = PyHASH_BITS;
1530+
assert(bits >= 8);
1531+
size_t mod = PyHASH_MODULUS;
1532+
assert(mod >= 7);
1533+
size_t inf = PyHASH_INF;
1534+
assert(inf != 0);
1535+
size_t imag = PyHASH_IMAG;
1536+
assert(imag != 0);
1537+
#endif
1538+
15251539
Py_RETURN_NONE;
15261540
}
15271541

0 commit comments

Comments
 (0)