changeset 11359:4eaacccbb2ca

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.
author Renato Cunha <renatoc@gmail.com>
date Tue, 15 Jun 2010 19:49:56 -0300
parents 4494fb02d549
children 2ac98313b26c
files mercurial/osutil.c
diffstat 1 files changed, 29 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/osutil.c	Tue Jun 15 19:49:56 2010 -0300
+++ b/mercurial/osutil.c	Tue Jun 15 19:49:56 2010 -0300
@@ -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