Commit de44bcf5 authored by Stefan Behnel's avatar Stefan Behnel

fix CPython compatibility problem on lookup failure in optimised PyDict_GetItem()

parent ed58a611
......@@ -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);
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment