Index: Python/errors.c =================================================================== --- Python/errors.c (révision 81870) +++ Python/errors.c (copie de travail) @@ -367,8 +367,6 @@ int i = errno; #ifndef MS_WINDOWS char *s; -#else - WCHAR *s_buf = NULL; #endif /* Unix/Windows */ #ifdef EINTR @@ -396,40 +394,13 @@ message = PyUnicode_FromString(_sys_errlist[i]); } else { - int len = FormatMessageW( - FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, /* no message source */ - i, - MAKELANGID(LANG_NEUTRAL, - SUBLANG_DEFAULT), - /* Default language */ - (LPWSTR) &s_buf, - 0, /* size not used */ - NULL); /* no args */ - if (len==0) { - /* Only ever seen this in out-of-mem - situations */ - s_buf = NULL; - message = PyUnicode_FromFormat("Windows Error 0x%X", i); - } else { - /* remove trailing cr/lf and dots */ - while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) - s_buf[--len] = L'\0'; - message = PyUnicode_FromUnicode(s_buf, len); - } + message = PyErr_GetWindowsMessage(i); } } #endif /* Unix/Windows */ if (message == NULL) - { -#ifdef MS_WINDOWS - LocalFree(s_buf); -#endif return NULL; - } if (filenameObject != NULL) v = Py_BuildValue("(iOO)", i, message, filenameObject); @@ -441,9 +412,6 @@ PyErr_SetObject(exc, v); Py_DECREF(v); } -#ifdef MS_WINDOWS - LocalFree(s_buf); -#endif return NULL; } @@ -477,18 +445,15 @@ } #ifdef MS_WINDOWS -/* Windows specific error code handling */ -PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject( - PyObject *exc, - int ierr, - PyObject *filenameObject) +PyObject * +PyErr_GetWindowsMessage(int ierr) { int len; WCHAR *s_buf = NULL; /* Free via LocalFree */ PyObject *message; - PyObject *v; DWORD err = (DWORD)ierr; - if (err==0) err = GetLastError(); + if (err == 0) + err = GetLastError(); len = FormatMessageW( /* Error API error */ FORMAT_MESSAGE_ALLOCATE_BUFFER | @@ -496,39 +461,46 @@ FORMAT_MESSAGE_IGNORE_INSERTS, NULL, /* no message source */ err, - MAKELANGID(LANG_NEUTRAL, - SUBLANG_DEFAULT), /* Default language */ + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */ (LPWSTR) &s_buf, 0, /* size not used */ NULL); /* no args */ - if (len==0) { - /* Only seen this in out of mem situations */ - message = PyUnicode_FromFormat("Windows Error 0x%X", err); - s_buf = NULL; - } else { + if (len != 0) { /* remove trailing cr/lf and dots */ while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) s_buf[--len] = L'\0'; message = PyUnicode_FromUnicode(s_buf, len); + } else { + /* Only seen this in out of mem situations */ + message = PyUnicode_FromFormat("Windows Error 0x%X", err); } + if (s_buf != NULL) + LocalFree(s_buf); + return message; +} +/* Windows specific error code handling */ +PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject( + PyObject *exc, + int ierr, + PyObject *filenameObject) +{ + PyObject *message, *v; + + message = PyErr_GetWindowsMessage(ierr); if (message == NULL) - { - LocalFree(s_buf); return NULL; - } if (filenameObject != NULL) - v = Py_BuildValue("(iOO)", err, message, filenameObject); + v = Py_BuildValue("(iOO)", ierr, message, filenameObject); else - v = Py_BuildValue("(iO)", err, message); + v = Py_BuildValue("(iO)", ierr, message); Py_DECREF(message); if (v != NULL) { PyErr_SetObject(exc, v); Py_DECREF(v); } - LocalFree(s_buf); return NULL; } Index: Include/pyerrors.h =================================================================== --- Include/pyerrors.h (révision 81870) +++ Include/pyerrors.h (copie de travail) @@ -186,6 +186,8 @@ PyAPI_FUNC(PyObject *) PyErr_Format(PyObject *, const char *, ...); #ifdef MS_WINDOWS +PyAPI_FUNC(PyObject *) PyErr_GetWindowsMessage( + int); PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilenameObject( int, const char *); PyAPI_FUNC(PyObject *) PyErr_SetFromWindowsErrWithFilename(