From f58daad9f6311b085f6d4b56664e3407e6c72ebd Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Wed, 15 Oct 2025 10:34:47 +0200 Subject: [PATCH 1/2] Make PyUnstable_Unicode_GET_CACHED_HASH return -1 on PyPy --- docs/api.rst | 2 +- pythoncapi_compat.h | 7 +++++-- tests/test_pythoncapi_compat_cext.c | 4 +++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 7b64c64..3f30e53 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -68,7 +68,7 @@ Python 3.15 See `PyUnstable_Unicode_GET_CACHED_HASH() documentation `__. - Not available on PyPy. + On PyPy, always returns ``-1``. Python 3.14 diff --git a/pythoncapi_compat.h b/pythoncapi_compat.h index 96d462e..b16075f 100644 --- a/pythoncapi_compat.h +++ b/pythoncapi_compat.h @@ -2572,11 +2572,14 @@ PyTuple_FromArray(PyObject *const *array, Py_ssize_t size) #endif -#if PY_VERSION_HEX < 0x030F00A1 && !defined(PYPY_VERSION) +#if PY_VERSION_HEX < 0x030F00A1 static inline Py_hash_t PyUnstable_Unicode_GET_CACHED_HASH(PyObject *op) { -#if PY_VERSION_HEX >= 0x03000000 +#ifdef PYPY_VERSION + (void)op; // unused argument + return -1; +#elif PY_VERSION_HEX >= 0x03000000 return ((PyASCIIObject*)op)->hash; #else return ((PyUnicodeObject*)op)->hash; diff --git a/tests/test_pythoncapi_compat_cext.c b/tests/test_pythoncapi_compat_cext.c index dd8c54b..c66c55e 100644 --- a/tests/test_pythoncapi_compat_cext.c +++ b/tests/test_pythoncapi_compat_cext.c @@ -1611,10 +1611,12 @@ test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) assert(PyErr_ExceptionMatches(PyExc_TypeError)); PyErr_Clear(); -#ifndef PYPY_VERSION // Test PyUnstable_Unicode_GET_CACHED_HASH() Py_hash_t hash = PyObject_Hash(abc); assert(hash != -1); +#ifdef PYPY_VERSION + assert(PyUnstable_Unicode_GET_CACHED_HASH(abc) == -1); +#else assert(PyUnstable_Unicode_GET_CACHED_HASH(abc) == hash); #endif From 8e97b8e1e9dc34dbd33ea257cecfa144dd20393c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 15 Oct 2025 13:25:21 +0200 Subject: [PATCH 2/2] Update tests/test_pythoncapi_compat_cext.c --- tests/test_pythoncapi_compat_cext.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_pythoncapi_compat_cext.c b/tests/test_pythoncapi_compat_cext.c index c66c55e..fed0822 100644 --- a/tests/test_pythoncapi_compat_cext.c +++ b/tests/test_pythoncapi_compat_cext.c @@ -1612,11 +1612,11 @@ test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) PyErr_Clear(); // Test PyUnstable_Unicode_GET_CACHED_HASH() - Py_hash_t hash = PyObject_Hash(abc); - assert(hash != -1); #ifdef PYPY_VERSION assert(PyUnstable_Unicode_GET_CACHED_HASH(abc) == -1); #else + Py_hash_t hash = PyObject_Hash(abc); + assert(hash != -1); assert(PyUnstable_Unicode_GET_CACHED_HASH(abc) == hash); #endif