[PATCH 4 of 4] mpatch: raise MemoryError instead of mpatchError if lalloc() failed

Yuya Nishihara yuya at tcha.org
Sun Aug 7 08:09:58 EDT 2016


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1470532016 -32400
#      Sun Aug 07 10:06:56 2016 +0900
# Node ID fee7202891c3ef87f6c0f5db36edfc3386faec41
# Parent  888e432366ac66145869421a67732e33b70a80c9
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.

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;
 }
 


More information about the Mercurial-devel mailing list