https://github.com/python/cpython/commit/3bacae55980561cb99095a20a70c45d6174e056d
commit: 3bacae55980561cb99095a20a70c45d6174e056d
branch: main
author: Victor Stinner <[email protected]>
committer: encukou <[email protected]>
date: 2025-11-14T11:13:24+01:00
summary:

gh-131510: Use PyUnstable_Unicode_GET_CACHED_HASH() (GH-141520)

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.

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

diff --git a/Include/cpython/unicodeobject.h b/Include/cpython/unicodeobject.h
index 73e3bc44d6c9ca..2853d24c34b66e 100644
--- a/Include/cpython/unicodeobject.h
+++ b/Include/cpython/unicodeobject.h
@@ -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
diff --git a/Include/internal/pycore_object.h b/Include/internal/pycore_object.h
index 980d6d7764bd2c..fb50acd62da5eb 100644
--- a/Include/internal/pycore_object.h
+++ b/Include/internal/pycore_object.h
@@ -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;
         }
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 65eed151c2829d..14de21f3c67210 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -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 */
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 58228d6248522e..61bcc21ce13d47 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to