In some sites PyUnicode_FromFormat() and PyErr_Format() can be used instead of sprintf to a static or dynamically allocated buffer followed by (implicit) PyUnicode_FromString() or PyErr_SetString(). It would consume slightly more CPU and memory resources, but none of these places are performance critical and the new code is clearer.