]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-131510: Use PyUnstable_Unicode_GET_CACHED_HASH() (GH-141520)
authorVictor Stinner <vstinner@python.org>
Fri, 14 Nov 2025 10:13:24 +0000 (11:13 +0100)
committerGitHub <noreply@github.com>
Fri, 14 Nov 2025 10:13:24 +0000 (11:13 +0100)
Replace code that directly accesses PyASCIIObject.hash with
PyUnstable_Unicode_GET_CACHED_HASH().

Remove redundant "assert(PyUnicode_Check(op))" from
PyUnstable_Unicode_GET_CACHED_HASH(), _PyASCIIObject_CAST() already
implements the check.

Include/cpython/unicodeobject.h
Include/internal/pycore_object.h
Objects/dictobject.c
Objects/typeobject.c

index 73e3bc44d6c9ca6a12f6f17c63fe48e4b42ef5c5..2853d24c34b66ecd54bb2e38e027cd841c6b7bb9 100644 (file)
@@ -301,7 +301,6 @@ static inline Py_ssize_t PyUnicode_GET_LENGTH(PyObject *op) {
 /* Returns the cached hash, or -1 if not cached yet. */
 static inline Py_hash_t
 PyUnstable_Unicode_GET_CACHED_HASH(PyObject *op) {
-    assert(PyUnicode_Check(op));
 #ifdef Py_GIL_DISABLED
     return _Py_atomic_load_ssize_relaxed(&_PyASCIIObject_CAST(op)->hash);
 #else
index 980d6d7764bd2cd35051a0dbf780486d930c4e1b..fb50acd62da5eb1aa8919ff82413f9596ebf6d0d 100644 (file)
@@ -863,8 +863,7 @@ static inline Py_hash_t
 _PyObject_HashFast(PyObject *op)
 {
     if (PyUnicode_CheckExact(op)) {
-        Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(
-                             _PyASCIIObject_CAST(op)->hash);
+        Py_hash_t hash = PyUnstable_Unicode_GET_CACHED_HASH(op);
         if (hash != -1) {
             return hash;
         }
index 65eed151c2829d9520d950ef495f1d136b387741..14de21f3c672107b86d731383a02fb2db5796759 100644 (file)
@@ -400,8 +400,7 @@ static int _PyObject_InlineValuesConsistencyCheck(PyObject *obj);
 static inline Py_hash_t
 unicode_get_hash(PyObject *o)
 {
-    assert(PyUnicode_CheckExact(o));
-    return FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyASCIIObject_CAST(o)->hash);
+    return PyUnstable_Unicode_GET_CACHED_HASH(o);
 }
 
 /* Print summary info about the state of the optimized allocator */
index 58228d6248522e65f96427367bff489fb134bebf..61bcc21ce13d471b1f4b3f269d67a794e33b2575 100644 (file)
@@ -6036,7 +6036,7 @@ static PyObject *
 update_cache(struct type_cache_entry *entry, PyObject *name, unsigned int version_tag, PyObject *value)
 {
     _Py_atomic_store_ptr_relaxed(&entry->value, value); /* borrowed */
-    assert(_PyASCIIObject_CAST(name)->hash != -1);
+    assert(PyUnstable_Unicode_GET_CACHED_HASH(name) != -1);
     OBJECT_STAT_INC_COND(type_cache_collisions, entry->name != Py_None && entry->name != name);
     // We're releasing this under the lock for simplicity sake because it's always a
     // exact unicode object or Py_None so it's safe to do so.