Skip to content

Commit 3bacae5

Browse files
authored
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.
1 parent 181a2f4 commit 3bacae5

File tree

4 files changed

+3
-6
lines changed

4 files changed

+3
-6
lines changed

Include/cpython/unicodeobject.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ static inline Py_ssize_t PyUnicode_GET_LENGTH(PyObject *op) {
301301
/* Returns the cached hash, or -1 if not cached yet. */
302302
static inline Py_hash_t
303303
PyUnstable_Unicode_GET_CACHED_HASH(PyObject *op) {
304-
assert(PyUnicode_Check(op));
305304
#ifdef Py_GIL_DISABLED
306305
return _Py_atomic_load_ssize_relaxed(&_PyASCIIObject_CAST(op)->hash);
307306
#else

Include/internal/pycore_object.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,7 @@ static inline Py_hash_t
863863
_PyObject_HashFast(PyObject *op)
864864
{
865865
if (PyUnicode_CheckExact(op)) {
866-
Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(
867-
_PyASCIIObject_CAST(op)->hash);
866+
Py_hash_t hash = PyUnstable_Unicode_GET_CACHED_HASH(op);
868867
if (hash != -1) {
869868
return hash;
870869
}

Objects/dictobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,7 @@ static int _PyObject_InlineValuesConsistencyCheck(PyObject *obj);
400400
static inline Py_hash_t
401401
unicode_get_hash(PyObject *o)
402402
{
403-
assert(PyUnicode_CheckExact(o));
404-
return FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyASCIIObject_CAST(o)->hash);
403+
return PyUnstable_Unicode_GET_CACHED_HASH(o);
405404
}
406405

407406
/* Print summary info about the state of the optimized allocator */

Objects/typeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6036,7 +6036,7 @@ static PyObject *
60366036
update_cache(struct type_cache_entry *entry, PyObject *name, unsigned int version_tag, PyObject *value)
60376037
{
60386038
_Py_atomic_store_ptr_relaxed(&entry->value, value); /* borrowed */
6039-
assert(_PyASCIIObject_CAST(name)->hash != -1);
6039+
assert(PyUnstable_Unicode_GET_CACHED_HASH(name) != -1);
60406040
OBJECT_STAT_INC_COND(type_cache_collisions, entry->name != Py_None && entry->name != name);
60416041
// We're releasing this under the lock for simplicity sake because it's always a
60426042
// exact unicode object or Py_None so it's safe to do so.

0 commit comments

Comments
 (0)