diff --git a/src/ft2font_wrapper.cpp b/src/ft2font_wrapper.cpp index ef73a5efbd36..e4a12d9eee27 100644 --- a/src/ft2font_wrapper.cpp +++ b/src/ft2font_wrapper.cpp @@ -975,44 +975,24 @@ const char *PyFT2Font_get_charmap__doc__ = static PyObject *PyFT2Font_get_charmap(PyFT2Font *self, PyObject *args, PyObject *kwds) { PyObject *charmap; - - charmap = PyDict_New(); - if (charmap == NULL) { + if (!(charmap = PyDict_New())) { return NULL; } - FT_UInt index; FT_ULong code = FT_Get_First_Char(self->x->get_face(), &index); while (index != 0) { - PyObject *key; - PyObject *val; - - key = PyLong_FromLong(code); - if (key == NULL) { - Py_DECREF(charmap); - return NULL; - } - - val = PyLong_FromLong(index); - if (val == NULL) { - Py_DECREF(key); - Py_DECREF(charmap); - return NULL; - } - - if (PyDict_SetItem(charmap, key, val)) { - Py_DECREF(key); - Py_DECREF(val); + PyObject *key = NULL, *val = NULL; + bool error = (!(key = PyLong_FromLong(code)) + || !(val = PyLong_FromLong(index)) + || (PyDict_SetItem(charmap, key, val) == -1)); + Py_XDECREF(key); + Py_XDECREF(val); + if (error) { Py_DECREF(charmap); return NULL; } - - Py_DECREF(key); - Py_DECREF(val); - code = FT_Get_Next_Char(self->x->get_face(), code, &index); } - return charmap; }