Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 743007d

Browse files
committed
Patch by Christian Tismer for Win32, to use FormatMessage() instead of
strerror(). This improves the quality of the error messages.
1 parent 085b812 commit 743007d

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

Python/errors.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ extern char *strerror Py_PROTO((int));
4949
#endif
5050
#endif
5151

52+
#ifdef MS_WIN32
53+
#include "windows.h"
54+
#include "winbase.h"
55+
#endif
56+
5257
void
5358
PyErr_Restore(type, value, traceback)
5459
PyObject *type;
@@ -142,7 +147,7 @@ PyErr_GivenExceptionMatches(err, exc)
142147

143148
return err == exc;
144149
}
145-
150+
146151

147152
int
148153
PyErr_ExceptionMatches(exc)
@@ -291,7 +296,26 @@ PyErr_SetFromErrnoWithFilename(exc, filename)
291296
if (i == 0)
292297
s = "Error"; /* Sometimes errno didn't get set */
293298
else
299+
#ifndef MS_WIN32
294300
s = strerror(i);
301+
#else
302+
{
303+
int len = FormatMessage(
304+
FORMAT_MESSAGE_ALLOCATE_BUFFER |
305+
FORMAT_MESSAGE_FROM_SYSTEM |
306+
FORMAT_MESSAGE_IGNORE_INSERTS,
307+
NULL, /* no message source */
308+
i,
309+
MAKELANGID(LANG_NEUTRAL,
310+
SUBLANG_DEFAULT), /* Default language */
311+
(LPTSTR) &s,
312+
0, /* size not used */
313+
NULL); /* no args */
314+
/* remove trailing cr/lf and dots */
315+
while (len > 0 && s[len-1] <= '.')
316+
s[--len] = '\0';
317+
}
318+
#endif
295319
if (filename != NULL && Py_UseClassExceptionsFlag)
296320
v = Py_BuildValue("(iss)", i, s, filename);
297321
else
@@ -300,9 +324,12 @@ PyErr_SetFromErrnoWithFilename(exc, filename)
300324
PyErr_SetObject(exc, v);
301325
Py_DECREF(v);
302326
}
327+
#ifdef MS_WIN32
328+
LocalFree(s);
329+
#endif
303330
return NULL;
304331
}
305-
332+
306333

307334
PyObject *
308335
PyErr_SetFromErrno(exc)

0 commit comments

Comments
 (0)