From 2d3a91d97f8f3c49f25bb96aa0c20232dc6e4fe3 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 9 Mar 2024 21:38:04 +0100 Subject: [PATCH] Add hash constants --- docs/changelog.rst | 7 +++++++ pythoncapi_compat.h | 12 ++++++++++++ tests/test_pythoncapi_compat_cext.c | 14 ++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 76b3d82..2569314 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,13 @@ Changelog ========= +* 2024-03-09: Add hash constants: + + * ``PyHASH_BITS`` + * ``PyHASH_IMAG`` + * ``PyHASH_INF`` + * ``PyHASH_MODULUS`` + * 2024-02-20: Add PyTime API: * ``PyTime_t`` type diff --git a/pythoncapi_compat.h b/pythoncapi_compat.h index ebf4cbe..6afd297 100644 --- a/pythoncapi_compat.h +++ b/pythoncapi_compat.h @@ -1196,6 +1196,18 @@ static inline int PyTime_PerfCounter(PyTime_t *result) #endif +// gh-111389 added hash constants to Python 3.13.0a5. These constants were +// added first as private macros to Python 3.4.0b1 and PyPy 7.3.9. +#if (!defined(PyHASH_BITS) \ + && ((!defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x030400B1) \ + || (defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x03070000 \ + && PYPY_VERSION_NUM >= 0x07090000)) +# define PyHASH_BITS _PyHASH_BITS +# define PyHASH_MODULUS _PyHASH_MODULUS +# define PyHASH_INF _PyHASH_INF +# define PyHASH_IMAG _PyHASH_IMAG +#endif + #ifdef __cplusplus } diff --git a/tests/test_pythoncapi_compat_cext.c b/tests/test_pythoncapi_compat_cext.c index 94a2cfb..4e9fb83 100644 --- a/tests/test_pythoncapi_compat_cext.c +++ b/tests/test_pythoncapi_compat_cext.c @@ -1522,6 +1522,20 @@ test_hash(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) assert(Py_HashPointer(ptr1) == (Py_hash_t)ptr1); #endif +#if ((!defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x030400B1) \ + || (defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x03070000 \ + && PYPY_VERSION_NUM >= 0x07090000)) + // Just check that constants are available + size_t bits = PyHASH_BITS; + assert(bits >= 8); + size_t mod = PyHASH_MODULUS; + assert(mod >= 7); + size_t inf = PyHASH_INF; + assert(inf != 0); + size_t imag = PyHASH_IMAG; + assert(imag != 0); +#endif + Py_RETURN_NONE; }