]> granicus.if.org Git - python/commitdiff
Issue #20637: Key-sharing now also works for instance dictionaries of subclasses...
authorAntoine Pitrou <solipsis@pitrou.net>
Sun, 23 Feb 2014 15:50:07 +0000 (16:50 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Sun, 23 Feb 2014 15:50:07 +0000 (16:50 +0100)
Lib/test/test_types.py
Misc/ACKS
Misc/NEWS
Objects/typeobject.c

index ec10752e6a2ae0eac233c1cdd9bf7cd8ed858b54..18e6b0a89c628db4c37e4f83cbaff4f75ccda66b 100644 (file)
@@ -1,6 +1,6 @@
 # Python test set -- part 6, built-in types
 
-from test.support import run_unittest, run_with_locale
+from test.support import run_unittest, run_with_locale, cpython_only
 import collections
 import pickle
 import locale
@@ -1170,9 +1170,31 @@ class SimpleNamespaceTests(unittest.TestCase):
             self.assertEqual(ns, ns_roundtrip, pname)
 
 
+class SharedKeyTests(unittest.TestCase):
+
+    @cpython_only
+    def test_subclasses(self):
+        # Verify that subclasses can share keys (per PEP 412)
+        class A:
+            pass
+        class B(A):
+            pass
+
+        a, b = A(), B()
+        self.assertEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(b)))
+        self.assertLess(sys.getsizeof(vars(a)), sys.getsizeof({}))
+        a.x, a.y, a.z, a.w = range(4)
+        self.assertNotEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(b)))
+        a2 = A()
+        self.assertEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(a2)))
+        self.assertLess(sys.getsizeof(vars(a)), sys.getsizeof({}))
+        b.u, b.v, b.w, b.t = range(4)
+        self.assertLess(sys.getsizeof(vars(b)), sys.getsizeof({}))
+
+
 def test_main():
     run_unittest(TypesTests, MappingProxyTests, ClassCreationTests,
-                 SimpleNamespaceTests)
+                 SimpleNamespaceTests, SharedKeyTests)
 
 if __name__ == '__main__':
     test_main()
index 12b5ab582f7e10ccf4aec58c118c2de1e1698a87..14a8cb84ef876bcd9d58beebbd5a76d5bd5cd28f 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -585,6 +585,7 @@ Aaron Iles
 Lars Immisch
 Bobby Impollonia
 Meador Inge
+Peter Ingebretson
 Tony Ingraldi
 John Interrante
 Bob Ippolito
index 306a8d6fbd870bc6a8869c032d5ce230a362a5ed..cfb05c2dbff2a079a13ff7c1e6aeee64cf585a5c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -5,6 +5,12 @@ Python News
 What's New in Python 3.4.1?
 ===========================
 
+Core and Builtins
+-----------------
+
+- Issue #20637: Key-sharing now also works for instance dictionaries of
+  subclasses.  Patch by Peter Ingebretson.
+
 Library
 -------
 
index f58960d28cb4dcf7696788f5cde363a385bc203d..5a41387bc1263a2422c8bec87be437bdef3e453b 100644 (file)
@@ -2472,6 +2472,9 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
             type->tp_dictoffset = slotoffset;
         slotoffset += sizeof(PyObject *);
     }
+    else if (!type->tp_dictoffset) {
+        type->tp_dictoffset = base->tp_dictoffset;
+    }
     if (type->tp_dictoffset) {
         et->ht_cached_keys = _PyDict_NewKeysForClass();
     }