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

Renato Cunha renatoc at gmail.com
Tue Jun 15 17:50:39 CDT 2010


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1276642196 10800
# Node ID b0aad5e908f7c1104401fa3f23ef0ec9700dd09e
# Parent  4b3427b7888e0510ee24f9fb14105768832eb773
parsers.c: Added support for py3k.

This patch adds support for py3k in parsers.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/parsers.c b/mercurial/parsers.c
--- a/mercurial/parsers.c
+++ b/mercurial/parsers.c
@@ -11,6 +11,8 @@
 #include <ctype.h>
 #include <string.h>
 
+#include "util.h"
+
 static int hexdigit(char c)
 {
 	if (c >= '0' && c <= '9')
@@ -33,11 +35,13 @@
 	const char *c;
 	char *d;
 
-	ret = PyString_FromStringAndSize(NULL, len / 2);
+	ret = PyBytes_FromStringAndSize(NULL, len / 2);
+
 	if (!ret)
 		return NULL;
 
-	d = PyString_AS_STRING(ret);
+	d = PyBytes_AsString(ret);
+
 	for (c = str; c < str + len;) {
 		int hi = hexdigit(*c++);
 		int lo = hexdigit(*c++);
@@ -81,7 +85,8 @@
 			goto quit;
 		}
 
-		file = PyString_FromStringAndSize(start, zero - start);
+		file = PyBytes_FromStringAndSize(start, zero - start);
+
 		if (!file)
 			goto bail;
 
@@ -92,7 +97,7 @@
 			goto bail;
 
 		if (nlen > 40) {
-			flags = PyString_FromStringAndSize(zero + 41,
+			flags = PyBytes_FromStringAndSize(zero + 41,
 							   nlen - 40);
 			if (!flags)
 				goto bail;
@@ -206,8 +211,8 @@
 
 		cpos = memchr(cur, 0, flen);
 		if (cpos) {
-			fname = PyString_FromStringAndSize(cur, cpos - cur);
-			cname = PyString_FromStringAndSize(cpos + 1,
+			fname = PyBytes_FromStringAndSize(cur, cpos - cur);
+			cname = PyBytes_FromStringAndSize(cpos + 1,
 							   flen - (cpos - cur) - 1);
 			if (!fname || !cname ||
 			    PyDict_SetItem(cmap, fname, cname) == -1 ||
@@ -215,7 +220,7 @@
 				goto quit;
 			Py_DECREF(cname);
 		} else {
-			fname = PyString_FromStringAndSize(cur, flen);
+			fname = PyBytes_FromStringAndSize(cur, flen);
 			if (!fname ||
 			    PyDict_SetItem(dmap, fname, entry) == -1)
 				goto quit;
@@ -248,8 +253,9 @@
 	int err;
 	PyObject *entry, *node_id, *n_obj;
 
-	node_id = PyString_FromStringAndSize(c_node_id, 20);
+	node_id = PyBytes_FromStringAndSize(c_node_id, 20);
 	n_obj = PyInt_FromLong(n);
+
 	if (!node_id || !n_obj)
 		err = -1;
 	else
@@ -427,7 +433,23 @@
 	{NULL, NULL}
 };
 
+#ifdef IS_PY3K
+static struct PyModuleDef parsers_module = {
+	PyModuleDef_HEAD_INIT,
+	"parsers",
+	parsers_doc,
+	-1,
+	methods
+};
+
+PyMODINIT_FUNC PyInit_parsers(void)
+{
+	return PyModule_Create(&parsers_module);
+}
+#else
 PyMODINIT_FUNC initparsers(void)
 {
 	Py_InitModule3("parsers", methods, parsers_doc);
 }
+#endif
+


More information about the Mercurial-devel mailing list