changeset: 89335:16229573e73e user: Antoine Pitrou date: Sun Feb 23 16:50:07 2014 +0100 files: Lib/test/test_types.py Misc/ACKS Misc/NEWS Objects/typeobject.c description: Issue #20637: Key-sharing now also works for instance dictionaries of subclasses. Patch by Peter Ingebretson. diff -r 569589d3abb5 -r 16229573e73e Lib/test/test_types.py --- a/Lib/test/test_types.py Sun Feb 23 17:13:31 2014 +0200 +++ b/Lib/test/test_types.py Sun Feb 23 16:50:07 2014 +0100 @@ -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 @@ 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() diff -r 569589d3abb5 -r 16229573e73e Misc/ACKS --- a/Misc/ACKS Sun Feb 23 17:13:31 2014 +0200 +++ b/Misc/ACKS Sun Feb 23 16:50:07 2014 +0100 @@ -585,6 +585,7 @@ Lars Immisch Bobby Impollonia Meador Inge +Peter Ingebretson Tony Ingraldi John Interrante Bob Ippolito diff -r 569589d3abb5 -r 16229573e73e Misc/NEWS --- a/Misc/NEWS Sun Feb 23 17:13:31 2014 +0200 +++ b/Misc/NEWS Sun Feb 23 16:50:07 2014 +0100 @@ -5,6 +5,12 @@ 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 ------- diff -r 569589d3abb5 -r 16229573e73e Objects/typeobject.c --- a/Objects/typeobject.c Sun Feb 23 17:13:31 2014 +0200 +++ b/Objects/typeobject.c Sun Feb 23 16:50:07 2014 +0100 @@ -2472,6 +2472,9 @@ 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(); }