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

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


# HG changeset patch
# User Renato Cunha <renatoc at gmail.com>
# Date 1276642196 10800
# Node ID fc581e60eedce886fa939b1fed0b52fee71620f8
# Parent  d5069029108e380312638f6ca55df9375bc1e51f
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.

After testing on windows, some modifications were also made in the posixfile
function, as it calls PyFile_FromFile and PyFile_SetBufSize, which are gone in
py3k. In py3k the PyFile_* API is, actually a wrapper over the io module, and
code has been adapted accordingly to fit py3k.

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
@@ -95,8 +97,7 @@
 }
 
 static PyTypeObject listdir_stat_type = {
-	PyObject_HEAD_INIT(NULL)
-	0,                         /*ob_size*/
+	PyVarObject_HEAD_INIT(NULL, 0)
 	"osutil.stat",             /*tp_name*/
 	sizeof(struct listdir_stat), /*tp_basicsize*/
 	0,                         /*tp_itemsize*/
@@ -392,7 +393,7 @@
 	wantstat = statobj && PyObject_IsTrue(statobj);
 
 	if (skipobj && skipobj != Py_None) {
-		skip = PyString_AsString(skipobj);
+		skip = PyBytes_AsString(skipobj);
 		if (!skip)
 			return NULL;
 	}
@@ -486,12 +487,13 @@
 	}
 
 	fd = _open_osfhandle((intptr_t)handle, flags);
+
 	if (fd == -1) {
 		CloseHandle(handle);
 		PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
 		goto bail;
 	}
-
+#ifndef IS_PY3K
 	fp = _fdopen(fd, fpmode);
 	if (fp == NULL) {
 		_close(fd);
@@ -506,6 +508,11 @@
 	}
 
 	PyFile_SetBufSize(file_obj, bufsize);
+#else
+	file_obj = PyFile_FromFd(fd, name, mode, bufsize, NULL, NULL, NULL, 1);
+	if (file_obj == NULL)
+		goto bail;
+#endif
 bail:
 	PyMem_Free(name);
 	return file_obj;
@@ -525,6 +532,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 +556,4 @@
 
 	Py_InitModule3("osutil", methods, osutil_doc);
 }
+#endif


More information about the Mercurial-devel mailing list