Skip to content

Commit a49e0fb

Browse files
authored
Add PyUnstable_Unicode_GET_CACHED_HASH() function (#155)
1 parent 89e023e commit a49e0fb

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

docs/api.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ Python 3.15
6464

6565
See `PyTuple_FromArray() documentation <https://docs.python.org/dev/c-api/tuple.html#c.PyTuple_FromArray>`__.
6666

67+
.. c:function:: Py_hash_t PyUnstable_Unicode_GET_CACHED_HASH(PyObject *op)
68+
69+
See `PyUnstable_Unicode_GET_CACHED_HASH() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnstable_Unicode_GET_CACHED_HASH>`__.
70+
71+
Not available on PyPy.
72+
6773

6874
Python 3.14
6975
-----------

docs/changelog.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
Changelog
22
=========
33

4-
* 2025-10-14: Add ``PyTuple_FromArray()`` function.
4+
* 2025-10-14: Add functions:
5+
6+
* ``PyTuple_FromArray()``
7+
* ``PyUnstable_Unicode_GET_CACHED_HASH()``
8+
59
* 2025-09-18: Add PEP 782 functions:
610

711
* ``PyBytesWriter_Create()``

pythoncapi_compat.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2570,6 +2570,19 @@ PyTuple_FromArray(PyObject *const *array, Py_ssize_t size)
25702570
#endif
25712571

25722572

2573+
#if PY_VERSION_HEX < 0x030F00A1 && !defined(PYPY_VERSION)
2574+
static inline Py_hash_t
2575+
PyUnstable_Unicode_GET_CACHED_HASH(PyObject *op)
2576+
{
2577+
#if PY_VERSION_HEX >= 0x03000000
2578+
return ((PyASCIIObject*)op)->hash;
2579+
#else
2580+
return ((PyUnicodeObject*)op)->hash;
2581+
#endif
2582+
}
2583+
#endif
2584+
2585+
25732586
#ifdef __cplusplus
25742587
}
25752588
#endif

tests/test_pythoncapi_compat_cext.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,13 @@ test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
16111611
assert(PyErr_ExceptionMatches(PyExc_TypeError));
16121612
PyErr_Clear();
16131613

1614+
#ifndef PYPY_VERSION
1615+
// Test PyUnstable_Unicode_GET_CACHED_HASH()
1616+
Py_hash_t hash = PyObject_Hash(abc);
1617+
assert(hash != -1);
1618+
assert(PyUnstable_Unicode_GET_CACHED_HASH(abc) == hash);
1619+
#endif
1620+
16141621
Py_DECREF(abc);
16151622
Py_DECREF(abc0def);
16161623
Py_RETURN_NONE;

0 commit comments

Comments
 (0)