[PATCH 5 of 6] imported patch diffhelpers.c-py3k-port.diff

Renato Cunha renatoc at gmail.com
Tue Jun 8 12:57:31 CDT 2010


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1276015326 10800
# Node ID b71f390cb6204ba2719a3f97c536c4d4c1ef86e7
# Parent  da107bdf89397034141966b10855de21b429bec6
imported patch diffhelpers.c-py3k-port.diff

diff --git a/mercurial/diffhelpers.c b/mercurial/diffhelpers.c
--- a/mercurial/diffhelpers.c
+++ b/mercurial/diffhelpers.c
@@ -11,6 +11,10 @@
 #include <stdlib.h>
 #include <string.h>
 
+#if PY_MAJOR_VERSION >= 3
+#define IS_PY3K
+#endif
+
 static char diffhelpers_doc[] = "Efficient diff parsing";
 static PyObject *diffhelpers_Error;
 
@@ -20,20 +24,36 @@
 {
 	int hunksz = PyList_Size(hunk);
 	PyObject *s = PyList_GET_ITEM(hunk, hunksz-1);
+#ifdef IS_PY3K
+	char *l = PyBytes_AS_STRING(s);
+#else
 	char *l = PyString_AS_STRING(s);
+#endif
 	int alen = PyList_Size(a);
 	int blen = PyList_Size(b);
 	char c = l[0];
 	PyObject *hline;
+#ifdef IS_PY3K
+	int sz = PyBytes_GET_SIZE(s);
+#else
 	int sz = PyString_GET_SIZE(s);
+#endif
 
 	if (sz > 1 && l[sz-2] == '\r')
 		/* tolerate CRLF in last line */
 		sz -= 1;
+#ifdef IS_PY3K
+	hline = PyBytes_FromStringAndSize(l, sz-1);
+#else
 	hline = PyString_FromStringAndSize(l, sz-1);
+#endif
 
 	if (c == ' ' || c == '+') {
+#ifdef IS_PY3K
+		PyObject *rline = PyBytes_FromStringAndSize(l + 1, sz - 2);
+#else
 		PyObject *rline = PyString_FromStringAndSize(l + 1, sz - 2);
+#endif
 		PyList_SetItem(b, blen-1, rline);
 	}
 	if (c == ' ' || c == '-') {
@@ -82,7 +102,11 @@
 		    break;
 		for (i = 0; i < num; i++) {
 			x = PyFile_GetLine(fp, 0);
+#ifdef IS_PY3K
+			s = PyBytes_AS_STRING(x);
+#else
 			s = PyString_AS_STRING(x);
+#endif
 			c = *s;
 			if (strcmp(s, "\\ No newline at end of file\n") == 0) {
 				_fix_newline(hunk, a, b);
@@ -92,17 +116,29 @@
 				/* Some patches may be missing the control char
 				 * on empty lines. Supply a leading space. */
 				Py_DECREF(x);
+#ifdef IS_PY3K
+				x = PyBytes_FromString(" \n");
+#else
 				x = PyString_FromString(" \n");
+#endif
 			}
 			PyList_Append(hunk, x);
 			if (c == '+') {
+#ifdef IS_PY3K
+				l = PyBytes_FromString(s + 1);
+#else
 				l = PyString_FromString(s + 1);
+#endif
 				PyList_Append(b, l);
 				Py_DECREF(l);
 			} else if (c == '-') {
 				PyList_Append(a, x);
 			} else {
+#ifdef IS_PY3K
+				l = PyBytes_FromString(s + 1);
+#else
 				l = PyString_FromString(s + 1);
+#endif
 				PyList_Append(b, l);
 				Py_DECREF(l);
 				PyList_Append(a, x);
@@ -136,8 +172,13 @@
 		return Py_BuildValue("l", -1);
 	}
 	for (i = 0; i < alen; i++) {
+#ifdef IS_PY3K
+		sa = PyBytes_AS_STRING(PyList_GET_ITEM(a, i));
+		sb = PyBytes_AS_STRING(PyList_GET_ITEM(b, i + bstart));
+#else
 		sa = PyString_AS_STRING(PyList_GET_ITEM(a, i));
 		sb = PyString_AS_STRING(PyList_GET_ITEM(b, i + bstart));
+#endif
 		if (strcmp(sa + 1, sb) != 0)
 			return Py_BuildValue("l", -1);
 	}
@@ -151,6 +192,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 +224,5 @@
 	diffhelpers_Error = PyErr_NewException("diffhelpers.diffhelpersError",
 	                                        NULL, NULL);
 }
+#endif
 


More information about the Mercurial-devel mailing list