diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2023-02-08 02:01:10 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-08 02:01:10 (GMT) |
commit | 790ff6bc6a56b4bd6e403aa43a984b99f7171dd7 (patch) | |
tree | f07a6246d1ac10353febb2fe878079d24fd6fab4 /Objects/odictobject.c | |
parent | b2b85b5db9cfdb24f966b61757536a898abc3830 (diff) | |
download | cpython-790ff6bc6a56b4bd6e403aa43a984b99f7171dd7.zip cpython-790ff6bc6a56b4bd6e403aa43a984b99f7171dd7.tar.gz cpython-790ff6bc6a56b4bd6e403aa43a984b99f7171dd7.tar.bz2 |
gh-101446: Change `repr` of `collections.OrderedDict` (#101661)
Diffstat (limited to 'Objects/odictobject.c')
-rw-r--r-- | Objects/odictobject.c | 53 |
1 files changed, 6 insertions, 47 deletions
diff --git a/Objects/odictobject.c b/Objects/odictobject.c index 4976b70..ab2bbed 100644 --- a/Objects/odictobject.c +++ b/Objects/odictobject.c @@ -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))); @@ -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; } |