gh-134891: Add PyUnstable_Unicode_GET_CACHED_HASH (GH-134892) · python/cpython@e413e26 · GitHub
Skip to content

Commit e413e26

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

7 files changed

Lines changed: 55 additions & 5 deletions

File tree

Doc/c-api/unicode.rst

Lines changed: 16 additions & 0 deletions

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

0 commit comments

Comments
 (0)