[Python-checkins] cpython (merge 3.5 -> default): Issue #25410: C implementation of OrderedDict now uses type(self) instead of

serhiy.storchaka python-checkins at python.org
Thu Oct 22 13:19:53 EDT 2015


https://hg.python.org/cpython/rev/10b965d59b49
changeset:   98831:10b965d59b49
parent:      98829:5759e9cc3639
parent:      98830:a42c0c1c5133
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Thu Oct 22 20:19:14 2015 +0300
summary:
  Issue #25410: C implementation of OrderedDict now uses type(self) instead of
self.__class__ in __repr__() and __reduce__() for simplicity and reliability.

files:
  Objects/odictobject.c |  45 +++++++++---------------------
  1 files changed, 14 insertions(+), 31 deletions(-)


diff --git a/Objects/odictobject.c b/Objects/odictobject.c
--- a/Objects/odictobject.c
+++ b/Objects/odictobject.c
@@ -964,9 +964,8 @@
 odict_reduce(register PyODictObject *od)
 {
     _Py_IDENTIFIER(__dict__);
-    _Py_IDENTIFIER(__class__);
     _Py_IDENTIFIER(items);
-    PyObject *dict = NULL, *result = NULL, *cls = NULL;
+    PyObject *dict = NULL, *result = NULL;
     PyObject *items_iter, *items, *args = NULL;
 
     /* capture any instance state */
@@ -985,10 +984,6 @@
     }
 
     /* build the result */
-    cls = _PyObject_GetAttrId((PyObject *)od, &PyId___class__);
-    if (cls == NULL)
-        goto Done;
-
     args = PyTuple_New(0);
     if (args == NULL)
         goto Done;
@@ -1002,12 +997,11 @@
     if (items_iter == NULL)
         goto Done;
 
-    result = PyTuple_Pack(5, cls, args, dict ? dict : Py_None, Py_None, items_iter);
+    result = PyTuple_Pack(5, Py_TYPE(od), args, dict ? dict : Py_None, Py_None, items_iter);
     Py_DECREF(items_iter);
 
 Done:
     Py_XDECREF(dict);
-    Py_XDECREF(cls);
     Py_XDECREF(args);
 
     return result;
@@ -1456,23 +1450,25 @@
 odict_repr(PyODictObject *self)
 {
     int i;
-    _Py_IDENTIFIER(__class__);
-    _Py_IDENTIFIER(__name__);
     _Py_IDENTIFIER(items);
     Py_ssize_t count = -1;
-    PyObject *pieces = NULL, *result = NULL, *cls = NULL;
-    PyObject *classname = NULL;
+    PyObject *pieces = NULL, *result = NULL;
+    const char *classname;
+
+    classname = strrchr(Py_TYPE(self)->tp_name, '.');
+    if (classname == NULL)
+        classname = Py_TYPE(self)->tp_name;
+    else
+        classname++;
+
+    if (PyODict_SIZE(self) == 0)
+        return PyUnicode_FromFormat("%s()", classname);
 
     i = Py_ReprEnter((PyObject *)self);
     if (i != 0) {
         return i > 0 ? PyUnicode_FromString("...") : NULL;
     }
 
-    if (PyODict_SIZE(self) == 0) {
-        /* "OrderedDict()" */
-        goto Finish;
-    }
-
     if (PyODict_CheckExact(self)) {
         _ODictNode *node;
         pieces = PyList_New(PyODict_SIZE(self));
@@ -1506,23 +1502,10 @@
             goto Done;
     }
 
-Finish:
-    cls = _PyObject_GetAttrId((PyObject *)self, &PyId___class__);
-    if (cls == NULL)
-        goto Done;
-    classname = _PyObject_GetAttrId(cls, &PyId___name__);
-    if (classname == NULL)
-        goto Done;
-
-    if (pieces == NULL)
-        result = PyUnicode_FromFormat("%S()", classname, pieces);
-    else
-        result = PyUnicode_FromFormat("%S(%R)", classname, pieces);
+    result = PyUnicode_FromFormat("%s(%R)", classname, pieces);
 
 Done:
     Py_XDECREF(pieces);
-    Py_XDECREF(cls);
-    Py_XDECREF(classname);
     Py_ReprLeave((PyObject *)self);
     return result;
 };

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list