Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Python 3.15

See `PyUnstable_Unicode_GET_CACHED_HASH() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnstable_Unicode_GET_CACHED_HASH>`__.

Not available on PyPy.
On PyPy, always returns ``-1``.


Python 3.14
Expand Down
7 changes: 5 additions & 2 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down