diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index db3397ca3b329c4d84eb349868fc4f723da3a04d..8f922cb4b4e2b230f160b5c95e2bb400fee7368a 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -1642,11 +1642,8 @@ static CYTHON_INLINE PyObject* __Pyx_PyDict_GetItemDefault(PyObject* d, PyObject PyObject *m; m = __Pyx_GetAttrString(d, "get"); if (!m) return NULL; - if (default_value == Py_None) { - value = PyObject_CallFunctionObjArgs(m, key, default_value, NULL); - } else { - value = PyObject_CallFunctionObjArgs(m, key, NULL); - } + value = PyObject_CallFunctionObjArgs(m, key, + (default_value == Py_None) ? NULL : default_value, NULL); Py_DECREF(m); } #endif diff --git a/tests/run/dict_get.pyx b/tests/run/dict_get.pyx index 546cbe6b58ec3c4cd44bc24d58067d51cef85193..f27a81371afca8288893f26ea02f94ed622d49d8 100644 --- a/tests/run/dict_get.pyx +++ b/tests/run/dict_get.pyx @@ -52,6 +52,11 @@ def get_default(dict d, key, default): >>> get_default(d, 2, 2) 2 + >>> d.get((1,2), 2) + 2 + >>> get_default(d, (1,2), 2) + 2 + >>> class Unhashable: ... def __hash__(self): ... raise ValueError