comparison Python/errors.c @ 10638:df8a1c281ad7 legacy-trunk

Patch by Christian Tismer for Win32, to use FormatMessage() instead of strerror(). This improves the quality of the error messages.
author Guido van Rossum <guido@python.org>
date Wed, 21 Apr 1999 15:27:31 +0000
parents 1d40bc25703a
children fe960ad3db65
comparison
equal deleted inserted replaced
10637:bed15a015c1c 10638:df8a1c281ad7
45 45
46 #ifndef __STDC__ 46 #ifndef __STDC__
47 #ifndef MS_WINDOWS 47 #ifndef MS_WINDOWS
48 extern char *strerror Py_PROTO((int)); 48 extern char *strerror Py_PROTO((int));
49 #endif 49 #endif
50 #endif
51
52 #ifdef MS_WIN32
53 #include "windows.h"
54 #include "winbase.h"
50 #endif 55 #endif
51 56
52 void 57 void
53 PyErr_Restore(type, value, traceback) 58 PyErr_Restore(type, value, traceback)
54 PyObject *type; 59 PyObject *type;
140 if (PyClass_Check(err) && PyClass_Check(exc)) 145 if (PyClass_Check(err) && PyClass_Check(exc))
141 return PyClass_IsSubclass(err, exc); 146 return PyClass_IsSubclass(err, exc);
142 147
143 return err == exc; 148 return err == exc;
144 } 149 }
145 150
146 151
147 int 152 int
148 PyErr_ExceptionMatches(exc) 153 PyErr_ExceptionMatches(exc)
149 PyObject *exc; 154 PyObject *exc;
150 { 155 {
289 return NULL; 294 return NULL;
290 #endif 295 #endif
291 if (i == 0) 296 if (i == 0)
292 s = "Error"; /* Sometimes errno didn't get set */ 297 s = "Error"; /* Sometimes errno didn't get set */
293 else 298 else
299 #ifndef MS_WIN32
294 s = strerror(i); 300 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
295 if (filename != NULL && Py_UseClassExceptionsFlag) 319 if (filename != NULL && Py_UseClassExceptionsFlag)
296 v = Py_BuildValue("(iss)", i, s, filename); 320 v = Py_BuildValue("(iss)", i, s, filename);
297 else 321 else
298 v = Py_BuildValue("(is)", i, s); 322 v = Py_BuildValue("(is)", i, s);
299 if (v != NULL) { 323 if (v != NULL) {
300 PyErr_SetObject(exc, v); 324 PyErr_SetObject(exc, v);
301 Py_DECREF(v); 325 Py_DECREF(v);
302 } 326 }
327 #ifdef MS_WIN32
328 LocalFree(s);
329 #endif
303 return NULL; 330 return NULL;
304 } 331 }
305 332
306 333
307 PyObject * 334 PyObject *
308 PyErr_SetFromErrno(exc) 335 PyErr_SetFromErrno(exc)
309 PyObject *exc; 336 PyObject *exc;
310 { 337 {