Index: Objects/dictobject.c =================================================================== --- Objects/dictobject.c (revision 85893) +++ Objects/dictobject.c (working copy) @@ -1815,15 +1815,8 @@ if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt)) return NULL; - if (mp->ma_used == 0) { - if (deflt) { - Py_INCREF(deflt); - return deflt; - } - PyErr_SetString(PyExc_KeyError, - "pop(): dictionary is empty"); - return NULL; - } + if (mp->ma_used == 0) + goto notfound; if (!PyUnicode_CheckExact(key) || (hash = ((PyUnicodeObject *) key)->hash) == -1) { hash = PyObject_Hash(key); @@ -1833,14 +1826,8 @@ ep = (mp->ma_lookup)(mp, key, hash); if (ep == NULL) return NULL; - if (ep->me_value == NULL) { - if (deflt) { - Py_INCREF(deflt); - return deflt; - } - set_key_error(key); - return NULL; - } + if (ep->me_value == NULL) + goto notfound; old_key = ep->me_key; Py_INCREF(dummy); ep->me_key = dummy; @@ -1849,6 +1836,13 @@ mp->ma_used--; Py_DECREF(old_key); return old_value; + notfound: + if (deflt) { + Py_INCREF(deflt); + return deflt; + } + set_key_error(key); + return NULL; } static PyObject * Index: Lib/test/test_dict.py =================================================================== --- Lib/test/test_dict.py (revision 85893) +++ Lib/test/test_dict.py (working copy) @@ -329,11 +329,19 @@ k, v = 'abc', 'def' d[k] = v self.assertRaises(KeyError, d.pop, 'ghi') + try: + d.pop('ghi') + except KeyError as e: + self.assertEquals(e.args[0], 'ghi') self.assertEqual(d.pop(k), v) self.assertEqual(len(d), 0) self.assertRaises(KeyError, d.pop, k) + try: + d.pop(k) + except KeyError as e: + self.assertEquals(e.args[0], k) self.assertEqual(d.pop(k, v), v) d[k] = v