[PATCH 3 of 6] imported patch parsers.c-py3k-port.diff

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


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1276015325 10800
# Node ID 1f11288014db47774e0b05574cc41fd0b3784616
# Parent  6161c0ce8de625954cb7e45ed0a7e08d1b87f8ba
imported patch parsers.c-py3k-port.diff

diff --git a/mercurial/parsers.c b/mercurial/parsers.c
--- a/mercurial/parsers.c
+++ b/mercurial/parsers.c
@@ -11,6 +11,10 @@
 #include <ctype.h>
 #include <string.h>
 
+#if PY_MAJOR_VERSION >= 3
+#define IS_PY3K
+#endif
+
 static int hexdigit(char c)
 {
 	if (c >= '0' && c <= '9')
@@ -33,11 +37,21 @@
 	const char *c;
 	char *d;
 
+#ifdef IS_PY3K
+	ret = PyBytes_FromStringAndSize(NULL, len / 2);
+#else
 	ret = PyString_FromStringAndSize(NULL, len / 2);
+#endif
+
 	if (!ret)
 		return NULL;
 
+#ifdef IS_PY3K
+	d = PyBytes_AS_STRING(ret);
+#else
 	d = PyString_AS_STRING(ret);
+#endif
+
 	for (c = str; c < str + len;) {
 		int hi = hexdigit(*c++);
 		int lo = hexdigit(*c++);
@@ -81,7 +95,11 @@
 			goto quit;
 		}
 
+#ifdef IS_PY3K
+		file = PyBytes_FromStringAndSize(start, zero - start);
+#else
 		file = PyString_FromStringAndSize(start, zero - start);
+#endif
 		if (!file)
 			goto bail;
 
@@ -92,8 +110,13 @@
 			goto bail;
 
 		if (nlen > 40) {
+#ifdef IS_PY3K
+			flags = PyBytes_FromStringAndSize(zero + 41,
+							   nlen - 40);
+#else
 			flags = PyString_FromStringAndSize(zero + 41,
 							   nlen - 40);
+#endif
 			if (!flags)
 				goto bail;
 
@@ -206,16 +229,26 @@
 
 		cpos = memchr(cur, 0, flen);
 		if (cpos) {
+#ifdef IS_PY3K
+			fname = PyBytes_FromStringAndSize(cur, cpos - cur);
+			cname = PyBytes_FromStringAndSize(cpos + 1,
+							   flen - (cpos - cur) - 1);
+#else
 			fname = PyString_FromStringAndSize(cur, cpos - cur);
 			cname = PyString_FromStringAndSize(cpos + 1,
 							   flen - (cpos - cur) - 1);
+#endif
 			if (!fname || !cname ||
 			    PyDict_SetItem(cmap, fname, cname) == -1 ||
 			    PyDict_SetItem(dmap, fname, entry) == -1)
 				goto quit;
 			Py_DECREF(cname);
 		} else {
+#ifdef IS_PY3K
+			fname = PyBytes_FromStringAndSize(cur, flen);
+#else
 			fname = PyString_FromStringAndSize(cur, flen);
+#endif
 			if (!fname ||
 			    PyDict_SetItem(dmap, fname, entry) == -1)
 				goto quit;
@@ -248,8 +281,13 @@
 	int err;
 	PyObject *entry, *node_id, *n_obj;
 
+#ifdef IS_PY3K
+	node_id = PyBytes_FromStringAndSize(c_node_id, 20);
+	n_obj = PyLong_FromLong(n);
+#else
 	node_id = PyString_FromStringAndSize(c_node_id, 20);
 	n_obj = PyInt_FromLong(n);
+#endif
 	if (!node_id || !n_obj)
 		err = -1;
 	else
@@ -427,7 +465,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