--- Modules/posixmodule.c.old 2010-07-28 04:03:26.000000000 +0200 +++ Modules/posixmodule.c 2010-07-28 04:03:17.000000000 +0200 @@ -2207,8 +2207,11 @@ static PyObject * posix_getcwd(int use_bytes) { - char buf[1026]; - char *res; + const int bufsize_incr = 1024; + int bufsize = 0; + char *buf = NULL, *oldbuf; + char *res = NULL; + PyObject *path; #ifdef MS_WINDOWS if (!use_bytes) { @@ -2242,17 +2245,37 @@ #endif Py_BEGIN_ALLOW_THREADS + do { + bufsize = bufsize + bufsize_incr; + oldbuf = buf; + buf = realloc(oldbuf, bufsize); + if (buf == NULL) { + if (oldbuf != NULL) + free(oldbuf); + break; + } + #if defined(PYOS_OS2) && defined(PYCC_GCC) - res = _getcwd2(buf, sizeof buf); + res = _getcwd2(buf, bufsize); #else - res = getcwd(buf, sizeof buf); + res = getcwd(buf, bufsize); #endif + } while ((res == NULL) && (errno == ERANGE)); Py_END_ALLOW_THREADS - if (res == NULL) + + if (buf == NULL) + return PyErr_NoMemory(); + if (res == NULL) { + free(buf); return posix_error(); + } + if (use_bytes) - return PyBytes_FromStringAndSize(buf, strlen(buf)); - return PyUnicode_DecodeFSDefault(buf); + path = PyBytes_FromStringAndSize(buf, strlen(buf)); + else + path = PyUnicode_DecodeFSDefault(buf); + free(buf); + return path; } PyDoc_STRVAR(posix_getcwd__doc__,