]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-141510: Remove unncessary lock holding for frozendict repr (gh-144920)
authorDonghee Na <donghee.na@python.org>
Tue, 17 Feb 2026 14:52:50 +0000 (23:52 +0900)
committerGitHub <noreply@github.com>
Tue, 17 Feb 2026 14:52:50 +0000 (14:52 +0000)
Objects/dictobject.c

index 510a0fab468cc60c814fd3e15de55d9c16f1d438..62abb793d002e019d057bc19c20bc03bc6a8a74f 100644 (file)
@@ -3381,19 +3381,18 @@ dict_dealloc(PyObject *self)
 
 
 static PyObject *
-dict_repr_lock_held(PyObject *self)
+anydict_repr_impl(PyObject *self)
 {
     PyDictObject *mp = (PyDictObject *)self;
     PyObject *key = NULL, *value = NULL;
-    ASSERT_DICT_LOCKED(mp);
 
-    int res = Py_ReprEnter((PyObject *)mp);
+    int res = Py_ReprEnter(self);
     if (res != 0) {
         return (res > 0 ? PyUnicode_FromString("{...}") : NULL);
     }
 
     if (mp->ma_used == 0) {
-        Py_ReprLeave((PyObject *)mp);
+        Py_ReprLeave(self);
         return PyUnicode_FromString("{}");
     }
 
@@ -3412,7 +3411,7 @@ dict_repr_lock_held(PyObject *self)
        Note that repr may mutate the dict. */
     Py_ssize_t i = 0;
     int first = 1;
-    while (_PyDict_Next((PyObject *)mp, &i, &key, &value, NULL)) {
+    while (_PyDict_Next(self, &i, &key, &value, NULL)) {
         // Prevent repr from deleting key or value during key format.
         Py_INCREF(key);
         Py_INCREF(value);
@@ -3454,18 +3453,25 @@ dict_repr_lock_held(PyObject *self)
         goto error;
     }
 
-    Py_ReprLeave((PyObject *)mp);
+    Py_ReprLeave(self);
 
     return PyUnicodeWriter_Finish(writer);
 
 error:
-    Py_ReprLeave((PyObject *)mp);
+    Py_ReprLeave(self);
     PyUnicodeWriter_Discard(writer);
     Py_XDECREF(key);
     Py_XDECREF(value);
     return NULL;
 }
 
+static PyObject *
+dict_repr_lock_held(PyObject *self)
+{
+    ASSERT_DICT_LOCKED((PyDictObject *)self);
+    return anydict_repr_impl(self);
+}
+
 static PyObject *
 dict_repr(PyObject *self)
 {
@@ -7862,7 +7868,7 @@ static PyMethodDef frozendict_methods[] = {
 static PyObject *
 frozendict_repr(PyObject *self)
 {
-    PyObject *repr = dict_repr(self);
+    PyObject *repr = anydict_repr_impl(self);
     if (repr == NULL) {
         return NULL;
     }