Index: Modules/posixmodule.c =================================================================== --- Modules/posixmodule.c (revision 66863) +++ Modules/posixmodule.c (working copy) @@ -2384,40 +2384,67 @@ posix__getfullpathname(PyObject *self, PyObject *args) { /* assume encoded strings wont more than double no of chars */ - char inbuf[MAX_PATH*2]; + char inbuf[MAX_PATH+1]; char *inbufp = inbuf; - Py_ssize_t insize = sizeof(inbuf); - char outbuf[MAX_PATH*2]; + Py_ssize_t insize = sizeof(inbuf)/sizeof(inbuf[0]); + char outbuf[MAX_PATH+1]; char *temp; + DWORD result; + + if (PyArg_ParseTuple (args, "et#:_getfullpathname", + Py_FileSystemDefaultEncoding, &inbufp, + &insize)) { + result = GetFullPathNameA(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), + outbuf, &temp); + if (result) { + /* In the ANSI API, there should not be any paths longer + than MAX_PATH. */ + assert(result <= MAX_PATH+1); + return PyString_FromString(outbuf); + } + } + + /* Drop the argument parsing error as narrow strings + are also valid. */ + PyErr_Clear(); + #ifdef Py_WIN_WIDE_FILENAMES if (unicode_file_names()) { - PyUnicodeObject *po; - if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) { + PyObject *po; + if (PyArg_ParseTuple(args, "O:_getfullpathname", &po)) { + Py_UNICODE *wpath; Py_UNICODE woutbuf[MAX_PATH*2]; + Py_UNICODE *woutbufp = woutbuf; Py_UNICODE *wtemp; - if (!GetFullPathNameW(PyUnicode_AS_UNICODE(po), - sizeof(woutbuf)/sizeof(woutbuf[0]), - woutbuf, &wtemp)) - return win32_error("GetFullPathName", ""); - return PyUnicode_FromUnicode(woutbuf, wcslen(woutbuf)); + PyObject *v; + + if (!convert_to_unicode(&po)) + return NULL; + wpath = PyUnicode_AS_UNICODE(po); + + result = GetFullPathNameW(wpath, sizeof(woutbuf)/sizeof(woutbuf[0]), + woutbuf, &wtemp); + if (result > sizeof(woutbuf)/sizeof(woutbuf[0])) { + woutbufp = malloc(result * sizeof(Py_UNICODE)); + if (!woutbufp) + return PyErr_NoMemory(); + result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); + } + if (!result) + v = win32_error_unicode("GetFullPathName", wpath); + else if (PyUnicode_Check(PyTuple_GetItem(args, 0))) + v = PyUnicode_FromUnicode(woutbufp, wcslen(woutbufp)); + else + v = PyUnicode_Encode(woutbufp, wcslen(woutbufp), + Py_FileSystemDefaultEncoding, NULL); + if (woutbufp != woutbuf) + free(woutbufp); + Py_DECREF(po); + return v; } - /* Drop the argument parsing error as narrow strings - are also valid. */ - PyErr_Clear(); } #endif - if (!PyArg_ParseTuple (args, "et#:_getfullpathname", - Py_FileSystemDefaultEncoding, &inbufp, - &insize)) - return NULL; - if (!GetFullPathName(inbuf, sizeof(outbuf)/sizeof(outbuf[0]), - outbuf, &temp)) - return win32_error("GetFullPathName", inbuf); - if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { - return PyUnicode_Decode(outbuf, strlen(outbuf), - Py_FileSystemDefaultEncoding, NULL); - } - return PyString_FromString(outbuf); + return NULL; } /* end of posix__getfullpathname */ #endif /* MS_WINDOWS */