diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2014-02-23 16:50:07 +0100 |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2014-02-23 16:50:07 +0100 |
commit | 7f4474a4fe5878bfd341c10adf94b11473410511 (patch) | |
tree | e70756036c57327268b997b581af7f83ac75a15f /Lib/test | |
parent | 887004e821cac2c4e526152a695a963c82b064e0 (diff) | |
download | cpython-7f4474a4fe5878bfd341c10adf94b11473410511.tar.gz |
Issue #20637: Key-sharing now also works for instance dictionaries of subclasses. Patch by Peter Ingebretson.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_types.py | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index ec10752e6a..18e6b0a89c 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -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() |