diff --git a/mercurial/mpatch.c b/mercurial/mpatch.c --- a/mercurial/mpatch.c +++ b/mercurial/mpatch.c @@ -27,9 +27,6 @@ #include "compat.h" #include "mpatch.h" -char *mpatch_errors[] = {NULL, "invalid patch", "patch cannot be decoded", - "no memory"}; - static struct mpatch_flist *lalloc(ssize_t size) { struct mpatch_flist *a = NULL; diff --git a/mercurial/mpatch.h b/mercurial/mpatch.h --- a/mercurial/mpatch.h +++ b/mercurial/mpatch.h @@ -1,8 +1,6 @@ #ifndef _HG_MPATCH_H_ #define _HG_MPATCH_H_ -extern char *mpatch_errors[]; - #define MPATCH_ERR_NO_MEM -3 #define MPATCH_ERR_CANNOT_BE_DECODED -2 #define MPATCH_ERR_INVALID_PATCH -1 diff --git a/mercurial/mpatch_module.c b/mercurial/mpatch_module.c --- a/mercurial/mpatch_module.c +++ b/mercurial/mpatch_module.c @@ -33,6 +33,21 @@ static char mpatch_doc[] = "Efficient binary patching."; static PyObject *mpatch_Error; +static void setpyerr(int r) +{ + switch (r) { + case MPATCH_ERR_NO_MEM: + PyErr_NoMemory(); + break; + case MPATCH_ERR_CANNOT_BE_DECODED: + PyErr_SetString(mpatch_Error, "patch cannot be decoded"); + break; + case MPATCH_ERR_INVALID_PATCH: + PyErr_SetString(mpatch_Error, "invalid patch"); + break; + } +} + struct mpatch_flist *cpygetitem(void *bins, ssize_t pos) { const char *buffer; @@ -47,7 +62,7 @@ struct mpatch_flist *cpygetitem(void *bi return NULL; if ((r = mpatch_decode(buffer, blen, &res)) < 0) { if (!PyErr_Occurred()) - PyErr_SetString(mpatch_Error, mpatch_errors[-r]); + setpyerr(r); return NULL; } return res; @@ -102,7 +117,7 @@ patches(PyObject *self, PyObject *args) cleanup: mpatch_lfree(patch); if (!result && !PyErr_Occurred()) - PyErr_SetString(mpatch_Error, mpatch_errors[-r]); + setpyerr(r); return result; }