Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afbea7a

Browse files
Issue #25410: C implementation of OrderedDict now uses type(self) instead of
self.__class__ in __repr__() and __reduce__() for simplicity and reliability.
2 parents 5682850 + 4575beb commit afbea7a

1 file changed

Lines changed: 14 additions & 31 deletions

File tree

Objects/odictobject.c

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -964,9 +964,8 @@ static PyObject *
964964
odict_reduce(register PyODictObject *od)
965965
{
966966
_Py_IDENTIFIER(__dict__);
967-
_Py_IDENTIFIER(__class__);
968967
_Py_IDENTIFIER(items);
969-
PyObject *dict = NULL, *result = NULL, *cls = NULL;
968+
PyObject *dict = NULL, *result = NULL;
970969
PyObject *items_iter, *items, *args = NULL;
971970

972971
/* capture any instance state */
@@ -985,10 +984,6 @@ odict_reduce(register PyODictObject *od)
985984
}
986985

987986
/* build the result */
988-
cls = _PyObject_GetAttrId((PyObject *)od, &PyId___class__);
989-
if (cls == NULL)
990-
goto Done;
991-
992987
args = PyTuple_New(0);
993988
if (args == NULL)
994989
goto Done;
@@ -1002,12 +997,11 @@ odict_reduce(register PyODictObject *od)
1002997
if (items_iter == NULL)
1003998
goto Done;
1004999

1005-
result = PyTuple_Pack(5, cls, args, dict ? dict : Py_None, Py_None, items_iter);
1000+
result = PyTuple_Pack(5, Py_TYPE(od), args, dict ? dict : Py_None, Py_None, items_iter);
10061001
Py_DECREF(items_iter);
10071002

10081003
Done:
10091004
Py_XDECREF(dict);
1010-
Py_XDECREF(cls);
10111005
Py_XDECREF(args);
10121006

10131007
return result;
@@ -1456,23 +1450,25 @@ static PyObject *
14561450
odict_repr(PyODictObject *self)
14571451
{
14581452
int i;
1459-
_Py_IDENTIFIER(__class__);
1460-
_Py_IDENTIFIER(__name__);
14611453
_Py_IDENTIFIER(items);
14621454
Py_ssize_t count = -1;
1463-
PyObject *pieces = NULL, *result = NULL, *cls = NULL;
1464-
PyObject *classname = NULL;
1455+
PyObject *pieces = NULL, *result = NULL;
1456+
const char *classname;
1457+
1458+
classname = strrchr(Py_TYPE(self)->tp_name, '.');
1459+
if (classname == NULL)
1460+
classname = Py_TYPE(self)->tp_name;
1461+
else
1462+
classname++;
1463+
1464+
if (PyODict_SIZE(self) == 0)
1465+
return PyUnicode_FromFormat("%s()", classname);
14651466

14661467
i = Py_ReprEnter((PyObject *)self);
14671468
if (i != 0) {
14681469
return i > 0 ? PyUnicode_FromString("...") : NULL;
14691470
}
14701471

1471-
if (PyODict_SIZE(self) == 0) {
1472-
/* "OrderedDict()" */
1473-
goto Finish;
1474-
}
1475-
14761472
if (PyODict_CheckExact(self)) {
14771473
_ODictNode *node;
14781474
pieces = PyList_New(PyODict_SIZE(self));
@@ -1506,23 +1502,10 @@ odict_repr(PyODictObject *self)
15061502
goto Done;
15071503
}
15081504

1509-
Finish:
1510-
cls = _PyObject_GetAttrId((PyObject *)self, &PyId___class__);
1511-
if (cls == NULL)
1512-
goto Done;
1513-
classname = _PyObject_GetAttrId(cls, &PyId___name__);
1514-
if (classname == NULL)
1515-
goto Done;
1516-
1517-
if (pieces == NULL)
1518-
result = PyUnicode_FromFormat("%S()", classname, pieces);
1519-
else
1520-
result = PyUnicode_FromFormat("%S(%R)", classname, pieces);
1505+
result = PyUnicode_FromFormat("%s(%R)", classname, pieces);
15211506

15221507
Done:
15231508
Py_XDECREF(pieces);
1524-
Py_XDECREF(cls);
1525-
Py_XDECREF(classname);
15261509
Py_ReprLeave((PyObject *)self);
15271510
return result;
15281511
};

0 commit comments

Comments
 (0)