Kaydet (Commit) f0b5a7c0 authored tarafından Antoine Pitrou's avatar Antoine Pitrou

Issue #20637: Key-sharing now also works for instance dictionaries of…

Issue #20637: Key-sharing now also works for instance dictionaries of subclasses.  Patch by Peter Ingebretson.
üst 32f30a8d
# Python test set -- part 6, built-in types # 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 collections
import pickle import pickle
import locale import locale
...@@ -1170,9 +1170,31 @@ class SimpleNamespaceTests(unittest.TestCase): ...@@ -1170,9 +1170,31 @@ class SimpleNamespaceTests(unittest.TestCase):
self.assertEqual(ns, ns_roundtrip, pname) 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(): def test_main():
run_unittest(TypesTests, MappingProxyTests, ClassCreationTests, run_unittest(TypesTests, MappingProxyTests, ClassCreationTests,
SimpleNamespaceTests) SimpleNamespaceTests, SharedKeyTests)
if __name__ == '__main__': if __name__ == '__main__':
test_main() test_main()
...@@ -585,6 +585,7 @@ Aaron Iles ...@@ -585,6 +585,7 @@ Aaron Iles
Lars Immisch Lars Immisch
Bobby Impollonia Bobby Impollonia
Meador Inge Meador Inge
Peter Ingebretson
Tony Ingraldi Tony Ingraldi
John Interrante John Interrante
Bob Ippolito Bob Ippolito
......
...@@ -5,6 +5,12 @@ Python News ...@@ -5,6 +5,12 @@ Python News
What's New in Python 3.4.1? 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 Library
------- -------
......
...@@ -2472,6 +2472,9 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) ...@@ -2472,6 +2472,9 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
type->tp_dictoffset = slotoffset; type->tp_dictoffset = slotoffset;
slotoffset += sizeof(PyObject *); slotoffset += sizeof(PyObject *);
} }
else if (!type->tp_dictoffset) {
type->tp_dictoffset = base->tp_dictoffset;
}
if (type->tp_dictoffset) { if (type->tp_dictoffset) {
et->ht_cached_keys = _PyDict_NewKeysForClass(); et->ht_cached_keys = _PyDict_NewKeysForClass();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment