Index: Modules/posixmodule.c =================================================================== --- Modules/posixmodule.c (revision 62989) +++ Modules/posixmodule.c (working copy) @@ -1954,6 +1954,43 @@ Return a string representing the current working directory."); static PyObject * +_posix_getcwd_dynamic() +{ + int bufsize_incr = 1026; + int bufsize = 1026; + char *tmpbuf = NULL; + char *res; + PyObject *dynamic_return; + + Py_BEGIN_ALLOW_THREADS + do { + bufsize = bufsize + bufsize_incr; + tmpbuf = malloc(bufsize); + if (tmpbuf == NULL) { + break; + } +#if defined(PYOS_OS2) && defined(PYCC_GCC) + res = _getcwd2(tmpbuf, bufsize); +#else + res = getcwd(tmpbuf, bufsize); +#endif + + if (res == NULL) { + free(tmpbuf); + } + } while ((res == NULL) && (errno == ERANGE)); + Py_END_ALLOW_THREADS + + if (res == NULL) + return posix_error(); + + dynamic_return = PyString_FromString(tmpbuf); + free(tmpbuf); + + return dynamic_return; +} + +static PyObject * posix_getcwd(PyObject *self, PyObject *noargs) { char buf[1026]; @@ -1966,9 +2003,14 @@ res = getcwd(buf, sizeof buf); #endif Py_END_ALLOW_THREADS - if (res == NULL) + + if (res != NULL) + return PyString_FromString(buf); + + if ((res == NULL) && (errno != ERANGE)) return posix_error(); - return PyString_FromString(buf); + + return _posix_getcwd_dynamic(); } #ifdef Py_USING_UNICODE