Skip to content

Commit

Permalink
Assert we don't get -1 from hashing exact unicode, lock in _PyObject_…
Browse files Browse the repository at this point in the history
…StoreInstanceAttribute
  • Loading branch information
DinoV committed Feb 29, 2024
1 parent 22d7bfb commit d6e97e9
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1597,7 +1597,7 @@ insertion_resize(PyInterpreterState *interp, PyDictObject *mp, int unicode)
}

static Py_ssize_t
insert_into_splitdictkeys_locked(PyDictKeysObject *keys, PyObject *name)
insert_into_splitdictkeys(PyDictKeysObject *keys, PyObject *name)
{
assert(PyUnicode_CheckExact(name));
ASSERT_KEYS_LOCKED(keys);
Expand Down Expand Up @@ -1629,15 +1629,6 @@ insert_into_splitdictkeys_locked(PyDictKeysObject *keys, PyObject *name)
return ix;
}

static Py_ssize_t
insert_into_splitdictkeys(PyDictKeysObject *keys, PyObject *name)
{
LOCK_KEYS(keys);
Py_ssize_t ix = insert_into_splitdictkeys_locked(keys, name);
UNLOCK_KEYS(keys);
return ix;
}

static inline int
insert_combined_dict(PyInterpreterState *interp, PyDictObject *mp,
Py_hash_t hash, PyObject *key, PyObject *value)
Expand Down Expand Up @@ -6705,17 +6696,16 @@ _PyObject_StoreInstanceAttribute(PyObject *obj, PyDictValues *values,
Py_hash_t hash = unicode_get_hash(name);
if (hash == -1) {
hash = PyUnicode_Type.tp_hash(name);
if (hash == -1) {
PyErr_Clear();
return DKIX_EMPTY;
}
assert(hash != -1);
}

// Try a thread-safe lookup to see if the index is already allocated
ix = unicodekeys_lookup_unicode_threadsafe(keys, name, hash);
if (ix == DKIX_EMPTY) {
// Fall back to a version that will lock and maybe insert
// Lock keys and do insert
LOCK_KEYS(keys);
ix = insert_into_splitdictkeys(keys, name);
UNLOCK_KEYS(keys);
}
#else
ix = insert_into_splitdictkeys(keys, name);
Expand Down

0 comments on commit d6e97e9

Please sign in to comment.