]> granicus.if.org Git - python/commitdiff
Bug #1576657: when setting a KeyError for a tuple key, make sure that
authorGeorg Brandl <georg@python.org>
Sun, 29 Oct 2006 18:31:42 +0000 (18:31 +0000)
committerGeorg Brandl <georg@python.org>
Sun, 29 Oct 2006 18:31:42 +0000 (18:31 +0000)
the tuple isn't used as the "exception arguments tuple".

Lib/test/test_dict.py
Misc/NEWS
Objects/dictobject.c

index bbca79886c8d689250f55a53188fbadb2f96f59b..218f7cc7aa2a1d41372ac6781b6bb030b7bd49b9 100644 (file)
@@ -444,6 +444,16 @@ class DictTest(unittest.TestCase):
         else:
             self.fail_("g[42] didn't raise KeyError")
 
+    def test_tuple_keyerror(self):
+        # SF #1576657
+        d = {}
+        try:
+            d[(1,)]
+        except KeyError, e:
+            self.assertEqual(e.args, ((1,),))
+        else:
+            self.fail("missing KeyError")
+
 
 from test import mapping_tests
 
index d7548499ff92c6eb4fc4676515852b56ff9ba9ba..4d1396c207a3d5d9bf67cf1902038ed49f0cfc99 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 1?
 Core and builtins
 -----------------
 
+- Bug #1576657: when setting a KeyError for a tuple key, make sure that
+  the tuple isn't used as the "exception arguments tuple".
+
 - Bug #1565514, SystemError not raised on too many nested blocks.
 
 - Bug #1576174: WindowsError now displays the windows error code
index e127d96a3c79a8be51a56bce3f78f0e7fd104d2e..1fcfe1cc34907febf92e91761346ce9a2b56a880 100644 (file)
 typedef PyDictEntry dictentry;
 typedef PyDictObject dictobject;
 
+/* Set a key error with the specified argument, wrapping it in a
+ * tuple automatically so that tuple keys are not unpacked as the
+ * exception arguments. */
+static void
+set_key_error(PyObject *arg)
+{
+       PyObject *tup;
+       tup = PyTuple_Pack(1, arg);
+       if (!tup)
+               return; /* caller will expect error to be set anyway */
+       PyErr_SetObject(PyExc_KeyError, tup);
+}
+
 /* Define this out if you don't want conversion statistics on exit. */
 #undef SHOW_CONVERSION_COUNTS
 
@@ -665,7 +678,7 @@ PyDict_DelItem(PyObject *op, PyObject *key)
        if (ep == NULL)
                return -1;
        if (ep->me_value == NULL) {
-               PyErr_SetObject(PyExc_KeyError, key);
+               set_key_error(key);
                return -1;
        }
        old_key = ep->me_key;
@@ -974,7 +987,7 @@ dict_subscript(dictobject *mp, register PyObject *key)
                                return PyObject_CallFunctionObjArgs(missing,
                                        (PyObject *)mp, key, NULL);
                }
-               PyErr_SetObject(PyExc_KeyError, key);
+               set_key_error(key);
                return NULL;
        }
        else
@@ -1746,7 +1759,7 @@ dict_pop(dictobject *mp, PyObject *args)
                        Py_INCREF(deflt);
                        return deflt;
                }
-               PyErr_SetObject(PyExc_KeyError, key);
+               set_key_error(key);
                return NULL;
        }
        old_key = ep->me_key;