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

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


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

This patch adds support for py3k in bdiff.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/bdiff.c b/mercurial/bdiff.c
--- a/mercurial/bdiff.c
+++ b/mercurial/bdiff.c
@@ -46,6 +46,8 @@
 #include <inttypes.h>
 #endif
 
+#include "util.h"
+
 struct line {
 	int h, len, n, e;
 	const char *l;
@@ -309,8 +311,9 @@
 	if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb))
 		return NULL;
 
-	an = splitlines(PyString_AsString(sa), PyString_Size(sa), &a);
-	bn = splitlines(PyString_AsString(sb), PyString_Size(sb), &b);
+	an = splitlines(PYBYTES_AS_STRING(sa), PYBYTES_SIZE(sa), &a);
+	bn = splitlines(PYBYTES_AS_STRING(sb), PYBYTES_SIZE(sb), &b);
+
 	if (!a || !b)
 		goto nomem;
 
@@ -363,12 +366,13 @@
 		lb = h->b2;
 	}
 
-	result = PyString_FromStringAndSize(NULL, len);
+	result = PYBYTES_FROM_STRING_AND_SIZE(NULL, len);
+
 	if (!result)
 		goto nomem;
 
 	/* build binary patch */
-	rb = PyString_AsString(result);
+	rb = PYBYTES_AS_STRING(result);
 	la = lb = 0;
 
 	for (h = l.base; h != l.head; h++) {
@@ -400,8 +404,23 @@
 	{NULL, NULL}
 };
 
+#ifdef IS_PY3K
+static struct PyModuleDef bdiff_module = {
+	PyModuleDef_HEAD_INIT,
+	"bdiff",
+	mdiff_doc,
+	-1,
+	methods
+};
+
+PyMODINIT_FUNC PyInit_bdiff(void)
+{
+	return PyModule_Create(&bdiff_module);
+}
+#else
 PyMODINIT_FUNC initbdiff(void)
 {
 	Py_InitModule3("bdiff", methods, mdiff_doc);
 }
+#endif
 


More information about the Mercurial-devel mailing list