From: Federico Caselli Date: Mon, 25 Jan 2021 20:39:42 +0000 (+0100) Subject: Improve c implementation of immutabledict X-Git-Tag: rel_1_4_0b2~20^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=78c97c1b54980a3e2c17d33fc4029da53309edb0;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Improve c implementation of immutabledict Avoids using the call methods and instead uses the dict c interface. Fixes: #5871 Change-Id: I7a1211104da319b0793a149f1a24f9f95c9f6630 --- diff --git a/lib/sqlalchemy/cextension/immutabledict.c b/lib/sqlalchemy/cextension/immutabledict.c index 53e59c195a..9b864bf028 100644 --- a/lib/sqlalchemy/cextension/immutabledict.c +++ b/lib/sqlalchemy/cextension/immutabledict.c @@ -80,9 +80,13 @@ ImmutableDict_subscript(ImmutableDict *self, PyObject *key) PyObject *err_bytes; #endif - value = PyDict_GetItem((PyObject *)self->dict, key); + value = PyDict_GetItemWithError(self->dict, key); if (value == NULL) { + if (PyErr_Occurred() != NULL) { + // there was an error while hashing the key + return NULL; + } #if PY_MAJOR_VERSION >= 3 err_bytes = PyUnicode_AsUTF8String(key); if (err_bytes == NULL) @@ -280,20 +284,34 @@ static PyObject * ImmutableDict_get(ImmutableDict *self, PyObject *args) { PyObject *key; + PyObject *value; PyObject *default_value = Py_None; if (!PyArg_UnpackTuple(args, "key", 1, 2, &key, &default_value)) { return NULL; } + value = PyDict_GetItemWithError(self->dict, key); - return PyObject_CallMethod(self->dict, "get", "OO", key, default_value); + if (value == NULL) { + if (PyErr_Occurred() != NULL) { + // there was an error while hashing the key + return NULL; + } else { + // return default + Py_INCREF(default_value); + return default_value; + } + } else { + Py_INCREF(value); + return value; + } } static PyObject * ImmutableDict_keys(ImmutableDict *self) { - return PyObject_CallMethod(self->dict, "keys", ""); + return PyDict_Keys(self->dict); } static int @@ -312,20 +330,19 @@ ImmutableDict_richcompare(ImmutableDict *self, PyObject *other, int op) static PyObject * ImmutableDict_iter(ImmutableDict *self) { - return PyObject_CallMethod(self->dict, "__iter__", ""); + return PyObject_GetIter(self->dict); } static PyObject * ImmutableDict_items(ImmutableDict *self) { - return PyObject_CallMethod(self->dict, "items", ""); + return PyDict_Items(self->dict); } static PyObject * ImmutableDict_values(ImmutableDict *self) { - return PyObject_CallMethod(self->dict, "values", ""); - + return PyDict_Values(self->dict); } static PyObject *