comparison Python/errors.c @ 25544:2fa559fb6ec5 legacy-trunk

Fix [ 616716 ] Bug in PyErr_SetExcFromWindows Ensure that even if FormatMessage fails we (a) don't crash, and (b) provide something useful. Bugfix candidate.
author Mark Hammond <mhammond@skippinet.com.au>
date Fri, 04 Oct 2002 00:13:02 +0000
parents dfeec3b34fae
children cc05c2c8660e
comparison
equal deleted inserted replaced
25543:dfeec3b34fae 25544:2fa559fb6ec5
267 #ifdef PLAN9 267 #ifdef PLAN9
268 char errbuf[ERRMAX]; 268 char errbuf[ERRMAX];
269 #endif 269 #endif
270 #ifdef MS_WINDOWS 270 #ifdef MS_WINDOWS
271 char *s_buf = NULL; 271 char *s_buf = NULL;
272 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
272 #endif 273 #endif
273 #ifdef EINTR 274 #ifdef EINTR
274 if (i == EINTR && PyErr_CheckSignals()) 275 if (i == EINTR && PyErr_CheckSignals())
275 return NULL; 276 return NULL;
276 #endif 277 #endif
304 SUBLANG_DEFAULT), 305 SUBLANG_DEFAULT),
305 /* Default language */ 306 /* Default language */
306 (LPTSTR) &s_buf, 307 (LPTSTR) &s_buf,
307 0, /* size not used */ 308 0, /* size not used */
308 NULL); /* no args */ 309 NULL); /* no args */
309 s = s_buf; 310 if (len==0) {
310 /* remove trailing cr/lf and dots */ 311 /* Only ever seen this in out-of-mem
311 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) 312 situations */
312 s[--len] = '\0'; 313 sprintf(s_small_buf, "Windows Error 0x%X", i);
314 s = s_small_buf;
315 s_buf = NULL;
316 } else {
317 s = s_buf;
318 /* remove trailing cr/lf and dots */
319 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
320 s[--len] = '\0';
321 }
313 } 322 }
314 } 323 }
315 #endif /* Unix/Windows */ 324 #endif /* Unix/Windows */
316 #endif /* PLAN 9*/ 325 #endif /* PLAN 9*/
317 if (filenameObject != NULL) 326 if (filenameObject != NULL)
364 int ierr, 373 int ierr,
365 PyObject *filenameObject) 374 PyObject *filenameObject)
366 { 375 {
367 int len; 376 int len;
368 char *s; 377 char *s;
378 char *s_buf = NULL; /* Free via LocalFree */
379 char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
369 PyObject *v; 380 PyObject *v;
370 DWORD err = (DWORD)ierr; 381 DWORD err = (DWORD)ierr;
371 if (err==0) err = GetLastError(); 382 if (err==0) err = GetLastError();
372 len = FormatMessage( 383 len = FormatMessage(
373 /* Error API error */ 384 /* Error API error */
376 FORMAT_MESSAGE_IGNORE_INSERTS, 387 FORMAT_MESSAGE_IGNORE_INSERTS,
377 NULL, /* no message source */ 388 NULL, /* no message source */
378 err, 389 err,
379 MAKELANGID(LANG_NEUTRAL, 390 MAKELANGID(LANG_NEUTRAL,
380 SUBLANG_DEFAULT), /* Default language */ 391 SUBLANG_DEFAULT), /* Default language */
381 (LPTSTR) &s, 392 (LPTSTR) &s_buf,
382 0, /* size not used */ 393 0, /* size not used */
383 NULL); /* no args */ 394 NULL); /* no args */
384 /* remove trailing cr/lf and dots */ 395 if (len==0) {
385 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) 396 /* Only seen this in out of mem situations */
386 s[--len] = '\0'; 397 sprintf(s_small_buf, "Windows Error 0x%X", err);
398 s = s_small_buf;
399 s_buf = NULL;
400 } else {
401 s = s_buf;
402 /* remove trailing cr/lf and dots */
403 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
404 s[--len] = '\0';
405 }
387 if (filenameObject != NULL) 406 if (filenameObject != NULL)
388 v = Py_BuildValue("(isO)", err, s, filenameObject); 407 v = Py_BuildValue("(isO)", err, s, filenameObject);
389 else 408 else
390 v = Py_BuildValue("(is)", err, s); 409 v = Py_BuildValue("(is)", err, s);
391 if (v != NULL) { 410 if (v != NULL) {
392 PyErr_SetObject(exc, v); 411 PyErr_SetObject(exc, v);
393 Py_DECREF(v); 412 Py_DECREF(v);
394 } 413 }
395 LocalFree(s); 414 LocalFree(s_buf);
396 return NULL; 415 return NULL;
397 } 416 }
398 417
399 PyObject *PyErr_SetExcFromWindowsErrWithFilename( 418 PyObject *PyErr_SetExcFromWindowsErrWithFilename(
400 PyObject *exc, 419 PyObject *exc,