# HG changeset patch # User Renato Cunha # Date 2010-06-15 22:49:56 # Node ID 4eaacccbb2ca5104d42136cf9abe51648a5b569f # Parent 4494fb02d549e42211061fe3d28057dcf28834c3 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 #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 void listdir_stat_dealloc(PyObjec } 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 @@ static PyObject *listdir(PyObject *self, 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 @@ static PyObject *posixfile(PyObject *sel } 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 @@ static PyObject *posixfile(PyObject *sel } 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 @@ static PyMethodDef methods[] = { {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 @@ PyMODINIT_FUNC initosutil(void) Py_InitModule3("osutil", methods, osutil_doc); } +#endif