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..fed0822 100644 --- a/tests/test_pythoncapi_compat_cext.c +++ b/tests/test_pythoncapi_compat_cext.c @@ -1611,8 +1611,10 @@ 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() +#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);