mpatch: raise MemoryError instead of mpatchError if lalloc() failed
authorYuya Nishihara <yuya@tcha.org>
Sun, 07 Aug 2016 10:06:56 +0900
changeset 29761 155f0cc3f813
parent 29760 5e2365698d44
child 29762 297a0dc50320
mpatch: raise MemoryError instead of mpatchError if lalloc() failed MemoryError is handled differently in dispatch._runcatch(). Since mpatch_errors[] isn't that useful now, I've changed it to a simple switch statement.
mercurial/mpatch.c
mercurial/mpatch.h
mercurial/mpatch_module.c
--- a/mercurial/mpatch.c	Sun Aug 07 18:09:58 2016 -0700
+++ b/mercurial/mpatch.c	Sun Aug 07 10:06:56 2016 +0900
@@ -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;
--- a/mercurial/mpatch.h	Sun Aug 07 18:09:58 2016 -0700
+++ b/mercurial/mpatch.h	Sun Aug 07 10:06:56 2016 +0900
@@ -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
--- a/mercurial/mpatch_module.c	Sun Aug 07 18:09:58 2016 -0700
+++ b/mercurial/mpatch_module.c	Sun Aug 07 10:06:56 2016 +0900
@@ -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 @@
 		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 @@
 cleanup:
 	mpatch_lfree(patch);
 	if (!result && !PyErr_Occurred())
-		PyErr_SetString(mpatch_Error, mpatch_errors[-r]);
+		setpyerr(r);
 	return result;
 }