Skip to content

Commit e413e26

Browse files
authored
gh-134891: Add PyUnstable_Unicode_GET_CACHED_HASH (GH-134892)
1 parent 3431828 commit e413e26

File tree

7 files changed

+55
-5
lines changed

7 files changed

+55
-5
lines changed

Doc/c-api/unicode.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,22 @@ access to internal read-only data of Unicode objects:
191191
.. versionadded:: 3.2
192192
193193
194+
.. c:function:: Py_hash_t PyUnstable_Unicode_GET_CACHED_HASH(PyObject *str)
195+
196+
If the hash of *str*, as returned by :c:func:`PyObject_Hash`, has been
197+
cached and is immediately available, return it.
198+
Otherwise, return ``-1`` *without* setting an exception.
199+
200+
If *str* is not a string (that is, if ``PyUnicode_Check(obj)``
201+
is false), the behavior is undefined.
202+
203+
This function never fails with an exception.
204+
205+
Note that there are no guarantees on when an object's hash is cached,
206+
and the (non-)existence of a cached hash does not imply that the string has
207+
any other properties.
208+
209+
194210
Unicode Character Properties
195211
""""""""""""""""""""""""""""
196212

Doc/whatsnew/3.15.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@ New features
304304
input string contains non-ASCII characters.
305305
(Contributed by Victor Stinner in :gh:`133968`.)
306306

307+
* Add :c:type:`PyUnstable_Unicode_GET_CACHED_HASH` to get the cached hash of
308+
a string. See the documentation for caveats.
309+
(Contributed by Petr Viktorin in :gh:`131510`)
310+
307311

308312
Porting to Python 3.15
309313
----------------------

Include/cpython/unicodeobject.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,17 @@ static inline Py_ssize_t PyUnicode_GET_LENGTH(PyObject *op) {
300300
}
301301
#define PyUnicode_GET_LENGTH(op) PyUnicode_GET_LENGTH(_PyObject_CAST(op))
302302

303+
/* Returns the cached hash, or -1 if not cached yet. */
304+
static inline Py_hash_t
305+
PyUnstable_Unicode_GET_CACHED_HASH(PyObject *op) {
306+
assert(PyUnicode_Check(op));
307+
#ifdef Py_GIL_DISABLED
308+
return _Py_atomic_load_ssize_relaxed(&_PyASCIIObject_CAST(op)->hash);
309+
#else
310+
return _PyASCIIObject_CAST(op)->hash;
311+
#endif
312+
}
313+
303314
/* Write into the canonical representation, this function does not do any sanity
304315
checks and is intended for usage in loops. The caller should cache the
305316
kind and data pointers obtained from other function calls.

Lib/test/test_capi/test_unicode.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,20 @@ def test_pep393_utf8_caching_bug(self):
17391739
# Check that the second call returns the same result
17401740
self.assertEqual(getargs_s_hash(s), chr(k).encode() * (i + 1))
17411741

1742+
@support.cpython_only
1743+
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
1744+
def test_GET_CACHED_HASH(self):
1745+
from _testcapi import unicode_GET_CACHED_HASH
1746+
content_bytes = b'some new string'
1747+
# avoid parser interning & constant folding
1748+
obj = str(content_bytes, 'ascii')
1749+
# impl detail: fresh strings do not have cached hash
1750+
self.assertEqual(unicode_GET_CACHED_HASH(obj), -1)
1751+
# impl detail: adding string to a dict caches its hash
1752+
{obj: obj}
1753+
# impl detail: ASCII string hashes are equal to bytes ones
1754+
self.assertEqual(unicode_GET_CACHED_HASH(obj), hash(content_bytes))
1755+
17421756

17431757
class PyUnicodeWriterTest(unittest.TestCase):
17441758
def create_writer(self, size):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :c:type:`PyUnstable_Unicode_GET_CACHED_HASH` to get the cached hash of a
2+
string.

Modules/_testcapi/unicode.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ unicode_copycharacters(PyObject *self, PyObject *args)
220220
return Py_BuildValue("(Nn)", to_copy, copied);
221221
}
222222

223+
static PyObject*
224+
unicode_GET_CACHED_HASH(PyObject *self, PyObject *arg)
225+
{
226+
return PyLong_FromSsize_t(PyUnstable_Unicode_GET_CACHED_HASH(arg));
227+
}
228+
223229

224230
// --- PyUnicodeWriter type -------------------------------------------------
225231

@@ -570,6 +576,7 @@ static PyMethodDef TestMethods[] = {
570576
{"unicode_asucs4copy", unicode_asucs4copy, METH_VARARGS},
571577
{"unicode_asutf8", unicode_asutf8, METH_VARARGS},
572578
{"unicode_copycharacters", unicode_copycharacters, METH_VARARGS},
579+
{"unicode_GET_CACHED_HASH", unicode_GET_CACHED_HASH, METH_O},
573580
{NULL},
574581
};
575582

Objects/unicodeobject.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,7 @@ static inline void PyUnicode_SET_UTF8_LENGTH(PyObject *op, Py_ssize_t length)
167167
#define _PyUnicode_HASH(op) \
168168
(_PyASCIIObject_CAST(op)->hash)
169169

170-
static inline Py_hash_t PyUnicode_HASH(PyObject *op)
171-
{
172-
assert(_PyUnicode_CHECK(op));
173-
return FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyASCIIObject_CAST(op)->hash);
174-
}
170+
#define PyUnicode_HASH PyUnstable_Unicode_GET_CACHED_HASH
175171

176172
static inline void PyUnicode_SET_HASH(PyObject *op, Py_hash_t hash)
177173
{

0 commit comments

Comments
 (0)