FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-101446: Change `repr` of `collections.OrderedDict` by sobolevn · Pull Request #101661 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (2) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion Lib/collections/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def __repr__(self):
'od.__repr__() <==> repr(od)'
if not self:
return '%s()' % (self.__class__.__name__,)
return '%s(%r)' % (self.__class__.__name__, list(self.items()))
return '%s(%r)' % (self.__class__.__name__, dict(self.items()))

def __reduce__(self):
'Return state information for pickling'
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_ordered_dict.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def test_repr(self):
OrderedDict = self.OrderedDict
od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])
self.assertEqual(repr(od),
"OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])")
"OrderedDict({'c': 1, 'b': 2, 'a': 3, 'd': 4, 'e': 5, 'f': 6})")
self.assertEqual(eval(repr(od)), od)
self.assertEqual(repr(OrderedDict()), "OrderedDict()")

Expand All @@ -372,7 +372,7 @@ def test_repr_recursive(self):
od = OrderedDict.fromkeys('abc')
od['x'] = od
self.assertEqual(repr(od),
"OrderedDict([('a', None), ('b', None), ('c', None), ('x', ...)])")
"OrderedDict({'a': None, 'b': None, 'c': None, 'x': ...})")

def test_repr_recursive_values(self):
OrderedDict = self.OrderedDict
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Change repr of :class:`collections.OrderedDict` to use regular dictionary
formating instead of pairs of keys and values.
53 changes: 6 additions & 47 deletions Objects/odictobject.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ static PyObject *
odict_repr(PyODictObject *self)
{
int i;
PyObject *pieces = NULL, *result = NULL;
PyObject *result = NULL, *dcopy = NULL;

if (PyODict_SIZE(self) == 0)
return PyUnicode_FromFormat("%s()", _PyType_Name(Py_TYPE(self)));
Expand All @@ -1377,57 +1377,16 @@ odict_repr(PyODictObject *self)
return i > 0 ? PyUnicode_FromString("...") : NULL;
}

if (PyODict_CheckExact(self)) {
Py_ssize_t count = 0;
_ODictNode *node;
pieces = PyList_New(PyODict_SIZE(self));
if (pieces == NULL)
goto Done;

_odict_FOREACH(self, node) {
PyObject *pair;
PyObject *key = _odictnode_KEY(node);
PyObject *value = _odictnode_VALUE(node, self);
if (value == NULL) {
if (!PyErr_Occurred())
PyErr_SetObject(PyExc_KeyError, key);
goto Done;
}
pair = PyTuple_Pack(2, key, value);
if (pair == NULL)
goto Done;

if (count < PyList_GET_SIZE(pieces))
PyList_SET_ITEM(pieces, count, pair); /* steals reference */
else {
if (PyList_Append(pieces, pair) < 0) {
Py_DECREF(pair);
goto Done;
}
Py_DECREF(pair);
}
count++;
}
if (count < PyList_GET_SIZE(pieces)) {
Py_SET_SIZE(pieces, count);
}
}
else {
PyObject *items = PyObject_CallMethodNoArgs(
(PyObject *)self, &_Py_ID(items));
if (items == NULL)
goto Done;
pieces = PySequence_List(items);
Py_DECREF(items);
if (pieces == NULL)
goto Done;
dcopy = PyDict_Copy((PyObject *)self);
if (dcopy == NULL) {
goto Done;
}

result = PyUnicode_FromFormat("%s(%R)",
_PyType_Name(Py_TYPE(self)), pieces);
_PyType_Name(Py_TYPE(self)),
dcopy);

Done:
Py_XDECREF(pieces);
Py_ReprLeave((PyObject *)self);
return result;
}
Expand Down

Back | FazBrowse Home | New Git URL