[PATCH 2 of 7] osutil.c: Support for py3k added

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


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1276177147 10800
# Node ID 8fd3727ae819a37e90be5b0ce7fc342ca24900c0
# Parent  8530f003b636e75a64eb8b4c7bb283697f829791
osutil.c: Support for py3k added.

This patch adds support for py3k in osutil.c. This is accomplished by including
a header file responsible for abstracting the API differences between python 2
and python 3.

listdir_stat_type is also changed in the following way: A previous call to
PyObject_HEAD_INIT is substituted to a call to PyVarObject_HEAD_INIT, which
makes the object buildable in both python 2.x and 3.x without weird warnings.

diff --git a/mercurial/osutil.c b/mercurial/osutil.c
--- a/mercurial/osutil.c
+++ b/mercurial/osutil.c
@@ -23,6 +23,8 @@
 #include <unistd.h>
 #endif
 
+#include "util.h"
+
 /* some platforms lack the PATH_MAX definition (eg. GNU/Hurd) */
 #ifndef PATH_MAX
 #define PATH_MAX 4096
@@ -53,11 +55,19 @@
 };
 #endif
 
+#ifdef IS_PY3K
+#define listdir_slot(name) \
+	static PyObject *listdir_stat_##name(PyObject *self, void *x) \
+	{ \
+		return PyLong_FromLong(((struct listdir_stat *)self)->st.name); \
+	}
+#else
 #define listdir_slot(name) \
 	static PyObject *listdir_stat_##name(PyObject *self, void *x) \
 	{ \
 		return PyInt_FromLong(((struct listdir_stat *)self)->st.name); \
 	}
+#endif
 
 listdir_slot(st_dev)
 listdir_slot(st_mode)
@@ -95,45 +105,44 @@
 }
 
 static PyTypeObject listdir_stat_type = {
-	PyObject_HEAD_INIT(NULL)
-	0,                         /*ob_size*/
-	"osutil.stat",             /*tp_name*/
-	sizeof(struct listdir_stat), /*tp_basicsize*/
-	0,                         /*tp_itemsize*/
-	(destructor)listdir_stat_dealloc, /*tp_dealloc*/
-	0,                         /*tp_print*/
-	0,                         /*tp_getattr*/
-	0,                         /*tp_setattr*/
-	0,                         /*tp_compare*/
-	0,                         /*tp_repr*/
-	0,                         /*tp_as_number*/
-	0,                         /*tp_as_sequence*/
-	0,                         /*tp_as_mapping*/
-	0,                         /*tp_hash */
-	0,                         /*tp_call*/
-	0,                         /*tp_str*/
-	0,                         /*tp_getattro*/
-	0,                         /*tp_setattro*/
-	0,                         /*tp_as_buffer*/
-	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
-	"stat objects",            /* tp_doc */
-	0,                         /* tp_traverse */
-	0,                         /* tp_clear */
-	0,                         /* tp_richcompare */
-	0,                         /* tp_weaklistoffset */
-	0,                         /* tp_iter */
-	0,                         /* tp_iternext */
-	0,                         /* tp_methods */
-	0,                         /* tp_members */
-	listdir_stat_getsets,      /* tp_getset */
-	0,                         /* tp_base */
-	0,                         /* tp_dict */
-	0,                         /* tp_descr_get */
-	0,                         /* tp_descr_set */
-	0,                         /* tp_dictoffset */
-	0,                         /* tp_init */
-	0,                         /* tp_alloc */
-	listdir_stat_new,          /* tp_new */
+    PyVarObject_HEAD_INIT(NULL, 0)
+    "osutil.stat",             /* tp_name */
+    sizeof(struct listdir_stat), /* tp_basicsize */
+    0,                         /* tp_itemsize */
+    (destructor)listdir_stat_dealloc, /* tp_dealloc */
+    0,                         /* tp_print */
+    0,                         /* tp_getattr */
+    0,                         /* tp_setattr */
+    0,                         /* tp_reserved */
+    0,                         /* tp_repr */
+    0,                         /* tp_as_number */
+    0,                         /* tp_as_sequence */
+    0,                         /* tp_as_mapping */
+    0,                         /* tp_hash  */
+    0,                         /* tp_call */
+    0,                         /* tp_str */
+    0,                         /* tp_getattro */
+    0,                         /* tp_setattro */
+    0,                         /* tp_as_buffer */
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
+    "stat objects",            /* tp_doc */
+    0,                         /* tp_traverse */
+    0,                         /* tp_clear */
+    0,                         /* tp_richcompare */
+    0,                         /* tp_weaklistoffset */
+    0,                         /* tp_iter */
+    0,                         /* tp_iternext */
+    0,                         /* tp_methods */
+    0,                         /* tp_members */
+    listdir_stat_getsets,      /* tp_getset */
+    0,                         /* tp_base */
+    0,                         /* tp_dict */
+    0,                         /* tp_descr_get */
+    0,                         /* tp_descr_set */
+    0,                         /* tp_dictoffset */
+    0,                         /* tp_init */
+    0,                         /* tp_alloc */
+    listdir_stat_new,          /* tp_new */
 };
 
 #ifdef _WIN32
@@ -392,7 +401,7 @@
 	wantstat = statobj && PyObject_IsTrue(statobj);
 
 	if (skipobj && skipobj != Py_None) {
-		skip = PyString_AsString(skipobj);
+		skip = PYBYTES_AS_STRING(skipobj);
 		if (!skip)
 			return NULL;
 	}
@@ -525,6 +534,23 @@
 	{NULL, NULL}
 };
 
+#ifdef IS_PY3K
+static struct PyModuleDef osutil_module = {
+	PyModuleDef_HEAD_INIT,
+	"osutil",
+	osutil_doc,
+	-1,
+	methods
+};
+
+PyMODINIT_FUNC PyInit_osutil(void)
+{
+	if (PyType_Ready(&listdir_stat_type) < 0)
+		return NULL;
+
+	return PyModule_Create(&osutil_module);
+}
+#else
 PyMODINIT_FUNC initosutil(void)
 {
 	if (PyType_Ready(&listdir_stat_type) == -1)
@@ -532,3 +558,4 @@
 
 	Py_InitModule3("osutil", methods, osutil_doc);
 }
+#endif


More information about the Mercurial-devel mailing list