[Python-checkins] r52536 - in python/branches/release25-maint: Lib/test/test_dict.py Misc/NEWS Objects/dictobject.c

georg.brandl python-checkins at python.org
Sun Oct 29 19:31:46 CET 2006


Author: georg.brandl
Date: Sun Oct 29 19:31:45 2006
New Revision: 52536

Modified:
   python/branches/release25-maint/Lib/test/test_dict.py
   python/branches/release25-maint/Misc/NEWS
   python/branches/release25-maint/Objects/dictobject.c
Log:
Bug #1576657: when setting a KeyError for a tuple key, make sure that
the tuple isn't used as the "exception arguments tuple".
 (backport from rev. 52535)

Modified: python/branches/release25-maint/Lib/test/test_dict.py
==============================================================================
--- python/branches/release25-maint/Lib/test/test_dict.py	(original)
+++ python/branches/release25-maint/Lib/test/test_dict.py	Sun Oct 29 19:31:45 2006
@@ -444,6 +444,16 @@
         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
 

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Sun Oct 29 19:31:45 2006
@@ -12,6 +12,9 @@
 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

Modified: python/branches/release25-maint/Objects/dictobject.c
==============================================================================
--- python/branches/release25-maint/Objects/dictobject.c	(original)
+++ python/branches/release25-maint/Objects/dictobject.c	Sun Oct 29 19:31:45 2006
@@ -12,6 +12,19 @@
 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 @@
 	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 @@
 				return PyObject_CallFunctionObjArgs(missing,
 					(PyObject *)mp, key, NULL);
 		}
-		PyErr_SetObject(PyExc_KeyError, key);
+		set_key_error(key);
 		return NULL;
 	}
 	else
@@ -1746,7 +1759,7 @@
 			Py_INCREF(deflt);
 			return deflt;
 		}
-		PyErr_SetObject(PyExc_KeyError, key);
+		set_key_error(key);
 		return NULL;
 	}
 	old_key = ep->me_key;


More information about the Python-checkins mailing list