Skip to content

Commit 1bd8cf9

Browse files
authored
gh-141510: Remove unncessary lock holding for frozendict repr (gh-144920)
1 parent fc05e5e commit 1bd8cf9

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

Objects/dictobject.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3381,19 +3381,18 @@ dict_dealloc(PyObject *self)
33813381

33823382

33833383
static PyObject *
3384-
dict_repr_lock_held(PyObject *self)
3384+
anydict_repr_impl(PyObject *self)
33853385
{
33863386
PyDictObject *mp = (PyDictObject *)self;
33873387
PyObject *key = NULL, *value = NULL;
3388-
ASSERT_DICT_LOCKED(mp);
33893388

3390-
int res = Py_ReprEnter((PyObject *)mp);
3389+
int res = Py_ReprEnter(self);
33913390
if (res != 0) {
33923391
return (res > 0 ? PyUnicode_FromString("{...}") : NULL);
33933392
}
33943393

33953394
if (mp->ma_used == 0) {
3396-
Py_ReprLeave((PyObject *)mp);
3395+
Py_ReprLeave(self);
33973396
return PyUnicode_FromString("{}");
33983397
}
33993398

@@ -3412,7 +3411,7 @@ dict_repr_lock_held(PyObject *self)
34123411
Note that repr may mutate the dict. */
34133412
Py_ssize_t i = 0;
34143413
int first = 1;
3415-
while (_PyDict_Next((PyObject *)mp, &i, &key, &value, NULL)) {
3414+
while (_PyDict_Next(self, &i, &key, &value, NULL)) {
34163415
// Prevent repr from deleting key or value during key format.
34173416
Py_INCREF(key);
34183417
Py_INCREF(value);
@@ -3454,18 +3453,25 @@ dict_repr_lock_held(PyObject *self)
34543453
goto error;
34553454
}
34563455

3457-
Py_ReprLeave((PyObject *)mp);
3456+
Py_ReprLeave(self);
34583457

34593458
return PyUnicodeWriter_Finish(writer);
34603459

34613460
error:
3462-
Py_ReprLeave((PyObject *)mp);
3461+
Py_ReprLeave(self);
34633462
PyUnicodeWriter_Discard(writer);
34643463
Py_XDECREF(key);
34653464
Py_XDECREF(value);
34663465
return NULL;
34673466
}
34683467

3468+
static PyObject *
3469+
dict_repr_lock_held(PyObject *self)
3470+
{
3471+
ASSERT_DICT_LOCKED((PyDictObject *)self);
3472+
return anydict_repr_impl(self);
3473+
}
3474+
34693475
static PyObject *
34703476
dict_repr(PyObject *self)
34713477
{
@@ -7862,7 +7868,7 @@ static PyMethodDef frozendict_methods[] = {
78627868
static PyObject *
78637869
frozendict_repr(PyObject *self)
78647870
{
7865-
PyObject *repr = dict_repr(self);
7871+
PyObject *repr = anydict_repr_impl(self);
78667872
if (repr == NULL) {
78677873
return NULL;
78687874
}

0 commit comments

Comments
 (0)