From 1a6b77b5dd1ebccd183183801da8a1e3c7458f3f Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Tue, 23 Feb 2010 15:43:28 +0100 Subject: [PATCH] fix dict.get() fallback in Py2 --- Cython/Compiler/Optimize.py | 7 ++----- tests/run/dict_get.pyx | 5 +++++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index db3397ca..8f922cb4 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 546cbe6b..f27a8137 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 -- 2.26.2