Skip to content

Commit ee9b18a

Browse files
committed
Reuse can_modify_dict()
1 parent 3456cd1 commit ee9b18a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Objects/dictobject.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ can_modify_dict(PyDictObject *mp)
295295
return PyUnstable_Object_IsUniquelyReferenced(_PyObject_CAST(mp));
296296
}
297297
else {
298+
// Locking is only required if the dictionary is not
299+
// uniquely referenced.
298300
ASSERT_DICT_LOCKED(mp);
299301
return 1;
300302
}
@@ -3239,6 +3241,8 @@ _PyDict_Pop(PyObject *dict, PyObject *key, PyObject *default_value)
32393241
static PyDictObject *
32403242
dict_dict_fromkeys(PyDictObject *mp, PyObject *iterable, PyObject *value)
32413243
{
3244+
assert(can_modify_dict(mp));
3245+
32423246
PyObject *oldvalue;
32433247
Py_ssize_t pos = 0;
32443248
PyObject *key;
@@ -3264,6 +3268,8 @@ dict_dict_fromkeys(PyDictObject *mp, PyObject *iterable, PyObject *value)
32643268
static PyDictObject *
32653269
dict_set_fromkeys(PyDictObject *mp, PyObject *iterable, PyObject *value)
32663270
{
3271+
assert(can_modify_dict(mp));
3272+
32673273
Py_ssize_t pos = 0;
32683274
PyObject *key;
32693275
Py_hash_t hash;
@@ -3319,7 +3325,7 @@ _PyDict_FromKeys(PyObject *cls, PyObject *iterable, PyObject *value)
33193325
}
33203326
Py_SETREF(d, copy);
33213327
}
3322-
assert(!PyFrozenDict_Check(d) || PyUnstable_Object_IsUniquelyReferenced(d));
3328+
assert(!PyFrozenDict_Check(d) || can_modify_dict((PyDictObject*)d));
33233329

33243330
if (PyDict_CheckExact(d)) {
33253331
if (PyDict_CheckExact(iterable)) {
@@ -8025,7 +8031,7 @@ frozendict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
80258031
if (d == NULL) {
80268032
return NULL;
80278033
}
8028-
assert(PyUnstable_Object_IsUniquelyReferenced(d));
8034+
assert(can_modify_dict(_PyAnyDict_CAST(d)));
80298035

80308036
PyFrozenDictObject *self = _PyFrozenDictObject_CAST(d);
80318037
self->ma_hash = -1;

0 commit comments

Comments
 (0)