[PATCH 6 of 7] diffhelpers.c: Added support for py3k

Renato Cunha renatoc at gmail.com
Thu Jun 10 08:39:55 CDT 2010


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1276177147 10800
# Node ID 368991cada3171a8695c466b2972a189b7d576b8
# Parent  f11c9563923ff26dfac4bcbe441866b565952a91
diffhelpers.c: Added support for py3k.

This patch adds support for py3k in diffhelpers.c. This is accomplished by
including a header file responsible for abstracting the API differences between
python 2 and python 3.

diff --git a/mercurial/diffhelpers.c b/mercurial/diffhelpers.c
--- a/mercurial/diffhelpers.c
+++ b/mercurial/diffhelpers.c
@@ -11,6 +11,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "util.h"
+
 static char diffhelpers_doc[] = "Efficient diff parsing";
 static PyObject *diffhelpers_Error;
 
@@ -20,20 +22,21 @@
 {
 	int hunksz = PyList_Size(hunk);
 	PyObject *s = PyList_GET_ITEM(hunk, hunksz-1);
-	char *l = PyString_AS_STRING(s);
+	char *l = PYBYTES_AS_STRING(s);
 	int alen = PyList_Size(a);
 	int blen = PyList_Size(b);
 	char c = l[0];
 	PyObject *hline;
-	int sz = PyString_GET_SIZE(s);
+	int sz = PYBYTES_GET_SIZE(s);
 
 	if (sz > 1 && l[sz-2] == '\r')
 		/* tolerate CRLF in last line */
 		sz -= 1;
-	hline = PyString_FromStringAndSize(l, sz-1);
+
+	hline = PYBYTES_FROM_STRING_AND_SIZE(l, sz-1);
 
 	if (c == ' ' || c == '+') {
-		PyObject *rline = PyString_FromStringAndSize(l + 1, sz - 2);
+		PyObject *rline = PYBYTES_FROM_STRING_AND_SIZE(l + 1, sz - 2);
 		PyList_SetItem(b, blen-1, rline);
 	}
 	if (c == ' ' || c == '-') {
@@ -82,7 +85,7 @@
 		    break;
 		for (i = 0; i < num; i++) {
 			x = PyFile_GetLine(fp, 0);
-			s = PyString_AS_STRING(x);
+			s = PYBYTES_AS_STRING(x);
 			c = *s;
 			if (strcmp(s, "\\ No newline at end of file\n") == 0) {
 				_fix_newline(hunk, a, b);
@@ -92,17 +95,17 @@
 				/* Some patches may be missing the control char
 				 * on empty lines. Supply a leading space. */
 				Py_DECREF(x);
-				x = PyString_FromString(" \n");
+				x = PYBYTES_FROM_STRING(" \n");
 			}
 			PyList_Append(hunk, x);
 			if (c == '+') {
-				l = PyString_FromString(s + 1);
+				l = PYBYTES_FROM_STRING(s + 1);
 				PyList_Append(b, l);
 				Py_DECREF(l);
 			} else if (c == '-') {
 				PyList_Append(a, x);
 			} else {
-				l = PyString_FromString(s + 1);
+				l = PYBYTES_FROM_STRING(s + 1);
 				PyList_Append(b, l);
 				Py_DECREF(l);
 				PyList_Append(a, x);
@@ -136,8 +139,8 @@
 		return Py_BuildValue("l", -1);
 	}
 	for (i = 0; i < alen; i++) {
-		sa = PyString_AS_STRING(PyList_GET_ITEM(a, i));
-		sb = PyString_AS_STRING(PyList_GET_ITEM(b, i + bstart));
+		sa = PYBYTES_AS_STRING(PyList_GET_ITEM(a, i));
+		sb = PYBYTES_AS_STRING(PyList_GET_ITEM(b, i + bstart));
 		if (strcmp(sa + 1, sb) != 0)
 			return Py_BuildValue("l", -1);
 	}
@@ -151,6 +154,31 @@
 	{NULL, NULL}
 };
 
+#ifdef IS_PY3K
+static struct PyModuleDef diffhelpers_module = {
+	PyModuleDef_HEAD_INIT,
+	"diffhelpers",
+	diffhelpers_doc,
+	-1,
+	methods
+};
+
+PyMODINIT_FUNC PyInit_diffhelpers(void)
+{
+	PyObject *m;
+
+	m = PyModule_Create(&diffhelpers_module);
+	if (m == NULL)
+		return NULL;
+
+	diffhelpers_Error = PyErr_NewException("diffhelpers.diffhelpersError",
+											NULL, NULL);
+	Py_INCREF(diffhelpers_Error);
+	PyModule_AddObject(m, "diffhelpersError", diffhelpers_Error);
+
+	return m;
+}
+#else
 PyMODINIT_FUNC
 initdiffhelpers(void)
 {
@@ -158,4 +186,5 @@
 	diffhelpers_Error = PyErr_NewException("diffhelpers.diffhelpersError",
 	                                        NULL, NULL);
 }
+#endif
 


More information about the Mercurial-devel mailing list