[PATCH 3 of 7] mpatch.c: Added preliminary support for py3k

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


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1276177147 10800
# Node ID 1f8eaf7708647708ca830eaa635da73424944b45
# Parent  8fd3727ae819a37e90be5b0ce7fc342ca24900c0
mpatch.c: Added preliminary support for py3k.

This is done by including the util.h header file, that defines appropriate
macros according to the current python version.

diff --git a/mercurial/mpatch.c b/mercurial/mpatch.c
--- a/mercurial/mpatch.c
+++ b/mercurial/mpatch.c
@@ -24,6 +24,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "util.h"
+
 /* Definitions to get compatibility with python 2.4 and earlier which
    does not have Py_ssize_t. See also PEP 353.
    Note: msvc (8 or earlier) does not have ssize_t, so we use Py_ssize_t.
@@ -373,12 +375,12 @@
 		result = NULL;
 		goto cleanup;
 	}
-	result = PyString_FromStringAndSize(NULL, outlen);
+	result = PYBYTES_FROM_STRING_AND_SIZE(NULL, outlen);
 	if (!result) {
 		result = NULL;
 		goto cleanup;
 	}
-	out = PyString_AsString(result);
+	out = PYBYTES_AS_STRING(result);
 	if (!apply(out, in, inlen, patch)) {
 		Py_DECREF(result);
 		result = NULL;
@@ -435,10 +437,34 @@
 	{NULL, NULL}
 };
 
+#ifdef IS_PY3K
+static struct PyModuleDef mpatch_module = {
+	PyModuleDef_HEAD_INIT,
+	"mpatch",
+	mpatch_doc,
+	-1,
+	methods
+};
+
+PyMODINIT_FUNC PyInit_mpatch(void)
+{
+	PyObject *m;
+
+	m = PyModule_Create(&mpatch_module);
+	if (m == NULL)
+		return NULL;
+
+	mpatch_Error = PyErr_NewException("mpatch.mpatchError", NULL, NULL);
+	Py_INCREF(mpatch_Error);
+	PyModule_AddObject(m, "mpatchError", mpatch_Error);
+
+	return m;
+}
+#else
 PyMODINIT_FUNC
 initmpatch(void)
 {
 	Py_InitModule3("mpatch", methods, mpatch_doc);
 	mpatch_Error = PyErr_NewException("mpatch.mpatchError", NULL, NULL);
 }
-
+#endif


More information about the Mercurial-devel mailing list