[Python-checkins] cpython: fix instance dicts with str subclasses (#13903)

benjamin.peterson python-checkins at python.org
Mon Apr 23 19:50:13 CEST 2012


http://hg.python.org/cpython/rev/34b6998efd2c
changeset:   76503:34b6998efd2c
parent:      76489:e3ea9e975689
user:        Benjamin Peterson <benjamin at python.org>
date:        Mon Apr 23 13:44:32 2012 -0400
summary:
  fix instance dicts with str subclasses (#13903)

files:
  Lib/test/test_dict.py |  10 ++++++++++
  Objects/dictobject.c  |   6 +++++-
  2 files changed, 15 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_dict.py b/Lib/test/test_dict.py
--- a/Lib/test/test_dict.py
+++ b/Lib/test/test_dict.py
@@ -879,6 +879,16 @@
         values = list(it) + [drop]
         self.assertEqual(sorted(values), sorted(list(data.values())))
 
+    def test_instance_dict_getattr_str_subclass(self):
+        class Foo:
+            def __init__(self, msg):
+                self.msg = msg
+        f = Foo('123')
+        class _str(str):
+            pass
+        self.assertEqual(f.msg, getattr(f, _str('msg')))
+        self.assertEqual(f.msg, f.__dict__[_str('msg')])
+
 from test import mapping_tests
 
 class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -641,7 +641,11 @@
     register PyDictKeyEntry *ep;
 
     if (!PyUnicode_CheckExact(key)) {
-        return lookdict(mp, key, hash, value_addr);
+        ep = lookdict(mp, key, hash, value_addr);
+        /* lookdict expects a combined-table, so fix value_addr */
+        i = ep - ep0;
+        *value_addr = &mp->ma_values[i];
+        return ep;
     }
     i = (size_t)hash & mask;
     ep = &ep0[i];

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


More information about the Python-checkins mailing list