[Python-checkins] cpython: Issue #25949: __dict__ for an OrderedDict instance is now created only when

serhiy.storchaka python-checkins at python.org
Mon Feb 8 09:39:26 EST 2016


https://hg.python.org/cpython/rev/caab6b356a9e
changeset:   100187:caab6b356a9e
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Mon Feb 08 16:39:05 2016 +0200
summary:
  Issue #25949: __dict__ for an OrderedDict instance is now created only when
needed.

files:
  Lib/test/test_ordered_dict.py |   4 ++-
  Misc/NEWS                     |   3 ++
  Objects/odictobject.c         |  25 +++++++---------------
  3 files changed, 14 insertions(+), 18 deletions(-)


diff --git a/Lib/test/test_ordered_dict.py b/Lib/test/test_ordered_dict.py
--- a/Lib/test/test_ordered_dict.py
+++ b/Lib/test/test_ordered_dict.py
@@ -298,9 +298,11 @@
         # do not save instance dictionary if not needed
         pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
         od = OrderedDict(pairs)
+        self.assertIsInstance(od.__dict__, dict)
         self.assertIsNone(od.__reduce__()[2])
         od.x = 10
-        self.assertIsNotNone(od.__reduce__()[2])
+        self.assertEqual(od.__dict__['x'], 10)
+        self.assertEqual(od.__reduce__()[2], {'x': 10})
 
     def test_pickle_recursive(self):
         OrderedDict = self.OrderedDict
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -170,6 +170,9 @@
 Library
 -------
 
+- Issue #25949: __dict__ for an OrderedDict instance is now created only when
+  needed.
+
 - Issue #25911: Restored support of bytes paths in os.walk() on Windows.
 
 - Issue #26045: Add UTF-8 suggestion to error message when posting a
diff --git a/Objects/odictobject.c b/Objects/odictobject.c
--- a/Objects/odictobject.c
+++ b/Objects/odictobject.c
@@ -1424,14 +1424,13 @@
  * OrderedDict members
  */
 
-/* tp_members */
+/* tp_getset */
 
-static PyMemberDef odict_members[] = {
-    {"__dict__", T_OBJECT, offsetof(PyODictObject, od_inst_dict), READONLY},
-    {0}
+static PyGetSetDef odict_getset[] = {
+    {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
+    {NULL}
 };
 
-
 /* ----------------------------------------------
  * OrderedDict type slot methods
  */
@@ -1653,20 +1652,12 @@
 static PyObject *
 odict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-    PyObject *dict;
     PyODictObject *od;
 
-    dict = PyDict_New();
-    if (dict == NULL)
+    od = (PyODictObject *)PyDict_Type.tp_new(type, args, kwds);
+    if (od == NULL)
         return NULL;
 
-    od = (PyODictObject *)PyDict_Type.tp_new(type, args, kwds);
-    if (od == NULL) {
-        Py_DECREF(dict);
-        return NULL;
-    }
-
-    od->od_inst_dict = dict;
     /* type constructor fills the memory with zeros (see
        PyType_GenericAlloc()), there is no need to set them to zero again */
     if (_odict_resize(od) < 0) {
@@ -1708,8 +1699,8 @@
     (getiterfunc)odict_iter,                    /* tp_iter */
     0,                                          /* tp_iternext */
     odict_methods,                              /* tp_methods */
-    odict_members,                              /* tp_members */
-    0,                                          /* tp_getset */
+    0,                                          /* tp_members */
+    odict_getset,                               /* tp_getset */
     &PyDict_Type,                               /* tp_base */
     0,                                          /* tp_dict */
     0,                                          /* tp_descr_get */

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


More information about the Python-checkins mailing list