From de44bcf5abfe6a664c6d9c151145b18834708ff6 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Sat, 16 Feb 2013 17:18:32 +0100 Subject: [PATCH] fix CPython compatibility problem on lookup failure in optimised PyDict_GetItem() --- Cython/Utility/ObjectHandling.c | 8 ++++++-- tests/run/dict_getitem.pyx | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Cython/Utility/ObjectHandling.c b/Cython/Utility/ObjectHandling.c index e0797f6..dd8d143 100644 --- a/Cython/Utility/ObjectHandling.c +++ b/Cython/Utility/ObjectHandling.c @@ -221,8 +221,12 @@ static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) { PyObject *value; value = PyDict_GetItemWithError(d, key); if (unlikely(!value)) { - if (!PyErr_Occurred()) - PyErr_SetObject(PyExc_KeyError, key); + if (!PyErr_Occurred()) { + PyObject* args = PyTuple_Pack(1, key); + if (likely(args)) + PyErr_SetObject(PyExc_KeyError, args); + Py_XDECREF(args); + } return NULL; } Py_INCREF(value); diff --git a/tests/run/dict_getitem.pyx b/tests/run/dict_getitem.pyx index 07622b0..772bebe 100644 --- a/tests/run/dict_getitem.pyx +++ b/tests/run/dict_getitem.pyx @@ -17,6 +17,22 @@ def test(dict d, index): Traceback (most recent call last): KeyError: (1, 2) + >>> import sys + >>> try: d[(1,)] + ... except KeyError: + ... args = sys.exc_info()[1].args + ... if sys.version_info >= (2,5): print(args) + ... else: print((args,)) # fake it for older CPython versions + ((1,),) + + >>> import sys + >>> try: test(d, (1,)) + ... except KeyError: + ... args = sys.exc_info()[1].args + ... if sys.version_info >= (2,5): print(args) + ... else: print((args,)) # fake it for older CPython versions + ((1,),) + >>> class Unhashable: ... def __hash__(self): ... raise ValueError -- 2.7.4