Index: Modules/posixmodule.c =================================================================== --- Modules/posixmodule.c (révision 84684) +++ Modules/posixmodule.c (copie de travail) @@ -2319,166 +2319,113 @@ PyObject *d, *v; HANDLE hFindFile; BOOL result; - WIN32_FIND_DATA FileData; PyObject *opath; - char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */ - char *bufptr = namebuf; - Py_ssize_t len = sizeof(namebuf)-5; /* only claim to have space for MAX_PATH */ - + Py_ssize_t len; + WIN32_FIND_DATAW wFileData; + Py_UNICODE *wnamebuf, *po_wchars; + int unicode; PyObject *po = NULL; - if (PyArg_ParseTuple(args, "|U:listdir", &po)) { - WIN32_FIND_DATAW wFileData; - Py_UNICODE *wnamebuf, *po_wchars; - - if (po == NULL) { // Default arg: "." - po_wchars = L"."; - len = 1; - } else { - po_wchars = PyUnicode_AS_UNICODE(po); - len = PyUnicode_GET_SIZE(po); - } - /* Overallocate for \\*.*\0 */ - wnamebuf = malloc((len + 5) * sizeof(wchar_t)); - if (!wnamebuf) { - PyErr_NoMemory(); - return NULL; - } - wcscpy(wnamebuf, po_wchars); - if (len > 0) { - Py_UNICODE wch = wnamebuf[len-1]; - if (wch != L'/' && wch != L'\\' && wch != L':') - wnamebuf[len++] = L'\\'; - wcscpy(wnamebuf + len, L"*.*"); - } - if ((d = PyList_New(0)) == NULL) { - free(wnamebuf); - return NULL; - } - Py_BEGIN_ALLOW_THREADS - hFindFile = FindFirstFileW(wnamebuf, &wFileData); - Py_END_ALLOW_THREADS - if (hFindFile == INVALID_HANDLE_VALUE) { - int error = GetLastError(); - if (error == ERROR_FILE_NOT_FOUND) { - free(wnamebuf); - return d; - } - Py_DECREF(d); - win32_error_unicode("FindFirstFileW", wnamebuf); - free(wnamebuf); - return NULL; - } - do { - /* Skip over . and .. */ - if (wcscmp(wFileData.cFileName, L".") != 0 && - wcscmp(wFileData.cFileName, L"..") != 0) { - v = PyUnicode_FromUnicode(wFileData.cFileName, wcslen(wFileData.cFileName)); - if (v == NULL) { - Py_DECREF(d); - d = NULL; - break; - } - if (PyList_Append(d, v) != 0) { - Py_DECREF(v); - Py_DECREF(d); - d = NULL; - break; - } - Py_DECREF(v); - } - Py_BEGIN_ALLOW_THREADS - result = FindNextFileW(hFindFile, &wFileData); - Py_END_ALLOW_THREADS - /* FindNextFile sets error to ERROR_NO_MORE_FILES if - it got to the end of the directory. */ - if (!result && GetLastError() != ERROR_NO_MORE_FILES) { - Py_DECREF(d); - win32_error_unicode("FindNextFileW", wnamebuf); - FindClose(hFindFile); - free(wnamebuf); - return NULL; - } - } while (result == TRUE); - if (FindClose(hFindFile) == FALSE) { - Py_DECREF(d); - win32_error_unicode("FindClose", wnamebuf); - free(wnamebuf); + if (!PyArg_ParseTuple(args, "|U:listdir", &po)) { + /* Drop the argument parsing error as narrow strings + are also valid. */ + PyErr_Clear(); + + if (!PyArg_ParseTuple(args, "O&:listdir", + PyUnicode_FSConverter, &opath)) return NULL; - } - free(wnamebuf); - return d; + unicode = 0; } - /* Drop the argument parsing error as narrow strings - are also valid. */ - PyErr_Clear(); + else + unicode = 1; - if (!PyArg_ParseTuple(args, "O&:listdir", - PyUnicode_FSConverter, &opath)) + if (po == NULL) { // Default arg: "." + po_wchars = L"."; + len = 1; + } + else { + po_wchars = PyUnicode_AS_UNICODE(po); + len = PyUnicode_GET_SIZE(po); + } + /* Overallocate for \\*.*\0 */ + wnamebuf = malloc((len + 5) * sizeof(wchar_t)); + if (!wnamebuf) { + PyErr_NoMemory(); return NULL; - if (PyBytes_GET_SIZE(opath)+1 > MAX_PATH) { - PyErr_SetString(PyExc_ValueError, "path too long"); - Py_DECREF(opath); - return NULL; } - strcpy(namebuf, PyBytes_AsString(opath)); - len = PyObject_Size(opath); + wcscpy(wnamebuf, po_wchars); if (len > 0) { - char ch = namebuf[len-1]; - if (ch != SEP && ch != ALTSEP && ch != ':') - namebuf[len++] = '/'; - strcpy(namebuf + len, "*.*"); + Py_UNICODE wch = wnamebuf[len-1]; + if (wch != L'/' && wch != L'\\' && wch != L':') + wnamebuf[len++] = L'\\'; + wcscpy(wnamebuf + len, L"*.*"); } - - if ((d = PyList_New(0)) == NULL) + d = PyList_New(0); + if (d == NULL) { + free(wnamebuf); return NULL; - + } Py_BEGIN_ALLOW_THREADS - hFindFile = FindFirstFile(namebuf, &FileData); + hFindFile = FindFirstFileW(wnamebuf, &wFileData); Py_END_ALLOW_THREADS if (hFindFile == INVALID_HANDLE_VALUE) { int error = GetLastError(); - if (error == ERROR_FILE_NOT_FOUND) + if (error == ERROR_FILE_NOT_FOUND) { + free(wnamebuf); return d; + } Py_DECREF(d); - return win32_error("FindFirstFile", namebuf); + win32_error_unicode("FindFirstFileW", wnamebuf); + free(wnamebuf); + return NULL; } do { /* Skip over . and .. */ - if (strcmp(FileData.cFileName, ".") != 0 && - strcmp(FileData.cFileName, "..") != 0) { - v = PyBytes_FromString(FileData.cFileName); + if (wcscmp(wFileData.cFileName, L".") != 0 && + wcscmp(wFileData.cFileName, L"..") != 0) { + v = PyUnicode_FromUnicode(wFileData.cFileName, + wcslen(wFileData.cFileName)); if (v == NULL) { - Py_DECREF(d); - d = NULL; + Py_CLEAR(d); break; } + if (!unicode) { + PyObject *bytes = PyUnicode_EncodeFSDefault(v); + Py_DECREF(v); + if (bytes == NULL) { + Py_CLEAR(d); + break; + } + v = bytes; + } if (PyList_Append(d, v) != 0) { Py_DECREF(v); - Py_DECREF(d); - d = NULL; + Py_CLEAR(d); break; } Py_DECREF(v); } Py_BEGIN_ALLOW_THREADS - result = FindNextFile(hFindFile, &FileData); + result = FindNextFileW(hFindFile, &wFileData); Py_END_ALLOW_THREADS /* FindNextFile sets error to ERROR_NO_MORE_FILES if it got to the end of the directory. */ if (!result && GetLastError() != ERROR_NO_MORE_FILES) { Py_DECREF(d); - win32_error("FindNextFile", namebuf); + win32_error_unicode("FindNextFileW", wnamebuf); FindClose(hFindFile); + free(wnamebuf); return NULL; } } while (result == TRUE); if (FindClose(hFindFile) == FALSE) { - Py_DECREF(d); - return win32_error("FindClose", namebuf); + Py_XDECREF(d); + win32_error_unicode("FindClose", wnamebuf); + free(wnamebuf); + return NULL; } - + free(wnamebuf); return d; #elif defined(PYOS_OS2)