Skip to content

Commit 92528bf

Browse files
committed
pythongh-101446: Change repr of collections.OrderedDict
1 parent f87f6e2 commit 92528bf

File tree

3 files changed

+6
-51
lines changed

3 files changed

+6
-51
lines changed

Lib/collections/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def __repr__(self):
267267
'od.__repr__() <==> repr(od)'
268268
if not self:
269269
return '%s()' % (self.__class__.__name__,)
270-
return '%s(%r)' % (self.__class__.__name__, list(self.items()))
270+
return '%s(%r)' % (self.__class__.__name__, dict(self.items()))
271271

272272
def __reduce__(self):
273273
'Return state information for pickling'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Change repr of :class:`collections.OrderedDict` to use regular dictionary
2+
formating instead of pair of keys and values.

Objects/odictobject.c

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,7 @@ static PyObject *
13671367
odict_repr(PyODictObject *self)
13681368
{
13691369
int i;
1370-
PyObject *pieces = NULL, *result = NULL;
1370+
PyObject *result = NULL;
13711371

13721372
if (PyODict_SIZE(self) == 0)
13731373
return PyUnicode_FromFormat("%s()", _PyType_Name(Py_TYPE(self)));
@@ -1377,57 +1377,10 @@ odict_repr(PyODictObject *self)
13771377
return i > 0 ? PyUnicode_FromString("...") : NULL;
13781378
}
13791379

1380-
if (PyODict_CheckExact(self)) {
1381-
Py_ssize_t count = 0;
1382-
_ODictNode *node;
1383-
pieces = PyList_New(PyODict_SIZE(self));
1384-
if (pieces == NULL)
1385-
goto Done;
1386-
1387-
_odict_FOREACH(self, node) {
1388-
PyObject *pair;
1389-
PyObject *key = _odictnode_KEY(node);
1390-
PyObject *value = _odictnode_VALUE(node, self);
1391-
if (value == NULL) {
1392-
if (!PyErr_Occurred())
1393-
PyErr_SetObject(PyExc_KeyError, key);
1394-
goto Done;
1395-
}
1396-
pair = PyTuple_Pack(2, key, value);
1397-
if (pair == NULL)
1398-
goto Done;
1399-
1400-
if (count < PyList_GET_SIZE(pieces))
1401-
PyList_SET_ITEM(pieces, count, pair); /* steals reference */
1402-
else {
1403-
if (PyList_Append(pieces, pair) < 0) {
1404-
Py_DECREF(pair);
1405-
goto Done;
1406-
}
1407-
Py_DECREF(pair);
1408-
}
1409-
count++;
1410-
}
1411-
if (count < PyList_GET_SIZE(pieces)) {
1412-
Py_SET_SIZE(pieces, count);
1413-
}
1414-
}
1415-
else {
1416-
PyObject *items = PyObject_CallMethodNoArgs(
1417-
(PyObject *)self, &_Py_ID(items));
1418-
if (items == NULL)
1419-
goto Done;
1420-
pieces = PySequence_List(items);
1421-
Py_DECREF(items);
1422-
if (pieces == NULL)
1423-
goto Done;
1424-
}
1425-
14261380
result = PyUnicode_FromFormat("%s(%R)",
1427-
_PyType_Name(Py_TYPE(self)), pieces);
1381+
_PyType_Name(Py_TYPE(self)),
1382+
PyDict_Copy((PyObject *)self));
14281383

1429-
Done:
1430-
Py_XDECREF(pieces);
14311384
Py_ReprLeave((PyObject *)self);
14321385
return result;
14331386
}

0 commit comments

Comments
 (0)