[Python-checkins] cpython: distiguish between refusing to creating shared keys and error (#13903)

benjamin.peterson python-checkins at python.org
Tue Apr 24 20:44:24 CEST 2012


http://hg.python.org/cpython/rev/5d5b72a71898
changeset:   76535:5d5b72a71898
user:        Benjamin Peterson <benjamin at python.org>
date:        Tue Apr 24 14:44:18 2012 -0400
summary:
  distiguish between refusing to creating shared keys and error (#13903)

files:
  Lib/test/test_dict.py |   7 +++++++
  Objects/dictobject.c  |  11 +++++++----
  2 files changed, 14 insertions(+), 4 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
@@ -889,6 +889,13 @@
         self.assertEqual(f.msg, getattr(f, _str('msg')))
         self.assertEqual(f.msg, f.__dict__[_str('msg')])
 
+    def test_object_set_item_single_instance_non_str_key(self):
+        class Foo: pass
+        f = Foo()
+        f.__dict__[1] = 1
+        f.a = 'a'
+        self.assertEqual(f.__dict__, {1:1, 'a':'a'})
+
 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
@@ -966,6 +966,8 @@
     return 0;
 }
 
+/* Returns NULL if unable to split table.
+ * A NULL return does not necessarily indicate an error */
 static PyDictKeysObject *
 make_keys_shared(PyObject *op)
 {
@@ -973,7 +975,8 @@
     Py_ssize_t size;
     PyDictObject *mp = (PyDictObject *)op;
 
-    assert(PyDict_CheckExact(op));
+    if (!PyDict_CheckExact(op))
+        return NULL;
     if (!_PyDict_HasSplitTable(mp)) {
         PyDictKeyEntry *ep0;
         PyObject **values;
@@ -3694,14 +3697,14 @@
             res = PyDict_SetItem(dict, key, value);
             if (cached != ((PyDictObject *)dict)->ma_keys) {
                 /* Either update tp->ht_cached_keys or delete it */
-                if (cached->dk_refcnt == 1 && PyDict_CheckExact(dict)) {
+                if (cached->dk_refcnt == 1) {
                     CACHED_KEYS(tp) = make_keys_shared(dict);
-                    if (CACHED_KEYS(tp) == NULL)
-                        return -1;
                 } else {
                     CACHED_KEYS(tp) = NULL;
                 }
                 DK_DECREF(cached);
+                if (CACHED_KEYS(tp) == NULL && PyErr_Occurred())
+                    return -1;
             }
         }
     } else {

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


More information about the Python-checkins mailing list