38
38
#endif
39
39
40
40
41
+ #define PUTS (fd , str ) _Py_write_noraise(fd, str, (int)strlen(str))
42
+
43
+
41
44
_Py_IDENTIFIER (flush );
42
45
_Py_IDENTIFIER (name );
43
46
_Py_IDENTIFIER (stdin );
@@ -2348,8 +2351,7 @@ static void
2348
2351
_Py_FatalError_DumpTracebacks (int fd , PyInterpreterState * interp ,
2349
2352
PyThreadState * tstate )
2350
2353
{
2351
- fputc ('\n' , stderr );
2352
- fflush (stderr );
2354
+ PUTS (fd , "\n" );
2353
2355
2354
2356
/* display the current Python stack */
2355
2357
_Py_DumpTracebackThreads (fd , interp , tstate );
@@ -2451,30 +2453,31 @@ fatal_output_debug(const char *msg)
2451
2453
2452
2454
2453
2455
static void
2454
- fatal_error_dump_runtime (FILE * stream , _PyRuntimeState * runtime )
2456
+ fatal_error_dump_runtime (int fd , _PyRuntimeState * runtime )
2455
2457
{
2456
- fprintf ( stream , "Python runtime state: " );
2458
+ PUTS ( fd , "Python runtime state: " );
2457
2459
PyThreadState * finalizing = _PyRuntimeState_GetFinalizing (runtime );
2458
2460
if (finalizing ) {
2459
- fprintf (stream , "finalizing (tstate=%p)" , finalizing );
2461
+ PUTS (fd , "finalizing (tstate=0x" );
2462
+ _Py_DumpHexadecimal (fd , (uintptr_t )finalizing , sizeof (finalizing ) * 2 );
2463
+ PUTS (fd , ")" );
2460
2464
}
2461
2465
else if (runtime -> initialized ) {
2462
- fprintf ( stream , "initialized" );
2466
+ PUTS ( fd , "initialized" );
2463
2467
}
2464
2468
else if (runtime -> core_initialized ) {
2465
- fprintf ( stream , "core initialized" );
2469
+ PUTS ( fd , "core initialized" );
2466
2470
}
2467
2471
else if (runtime -> preinitialized ) {
2468
- fprintf ( stream , "preinitialized" );
2472
+ PUTS ( fd , "preinitialized" );
2469
2473
}
2470
2474
else if (runtime -> preinitializing ) {
2471
- fprintf ( stream , "preinitializing" );
2475
+ PUTS ( fd , "preinitializing" );
2472
2476
}
2473
2477
else {
2474
- fprintf ( stream , "unknown" );
2478
+ PUTS ( fd , "unknown" );
2475
2479
}
2476
- fprintf (stream , "\n" );
2477
- fflush (stream );
2480
+ PUTS (fd , "\n" );
2478
2481
}
2479
2482
2480
2483
@@ -2494,10 +2497,9 @@ fatal_error_exit(int status)
2494
2497
2495
2498
2496
2499
static void _Py_NO_RETURN
2497
- fatal_error (FILE * stream , int header , const char * prefix , const char * msg ,
2500
+ fatal_error (int fd , int header , const char * prefix , const char * msg ,
2498
2501
int status )
2499
2502
{
2500
- const int fd = fileno (stream );
2501
2503
static int reentrant = 0 ;
2502
2504
2503
2505
if (reentrant ) {
@@ -2508,29 +2510,22 @@ fatal_error(FILE *stream, int header, const char *prefix, const char *msg,
2508
2510
reentrant = 1 ;
2509
2511
2510
2512
if (header ) {
2511
- fprintf ( stream , "Fatal Python error: " );
2513
+ PUTS ( fd , "Fatal Python error: " );
2512
2514
if (prefix ) {
2513
- fputs ( prefix , stream );
2514
- fputs ( ": " , stream );
2515
+ PUTS ( fd , prefix );
2516
+ PUTS ( fd , ": " );
2515
2517
}
2516
2518
if (msg ) {
2517
- fputs ( msg , stream );
2519
+ PUTS ( fd , msg );
2518
2520
}
2519
2521
else {
2520
- fprintf ( stream , "<message not set>" );
2522
+ PUTS ( fd , "<message not set>" );
2521
2523
}
2522
- fputs ("\n" , stream );
2523
- fflush (stream );
2524
+ PUTS (fd , "\n" );
2524
2525
}
2525
2526
2526
2527
_PyRuntimeState * runtime = & _PyRuntime ;
2527
- fatal_error_dump_runtime (stream , runtime );
2528
-
2529
- PyThreadState * tstate = _PyRuntimeState_GetThreadState (runtime );
2530
- PyInterpreterState * interp = NULL ;
2531
- if (tstate != NULL ) {
2532
- interp = tstate -> interp ;
2533
- }
2528
+ fatal_error_dump_runtime (fd , runtime );
2534
2529
2535
2530
/* Check if the current thread has a Python thread state
2536
2531
and holds the GIL.
@@ -2540,8 +2535,17 @@ fatal_error(FILE *stream, int header, const char *prefix, const char *msg,
2540
2535
2541
2536
tss_tstate != tstate if the current Python thread does not hold the GIL.
2542
2537
*/
2538
+ PyThreadState * tstate = _PyRuntimeState_GetThreadState (runtime );
2539
+ PyInterpreterState * interp = NULL ;
2543
2540
PyThreadState * tss_tstate = PyGILState_GetThisThreadState ();
2541
+ if (tstate != NULL ) {
2542
+ interp = tstate -> interp ;
2543
+ }
2544
+ else if (tss_tstate != NULL ) {
2545
+ interp = tss_tstate -> interp ;
2546
+ }
2544
2547
int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate );
2548
+
2545
2549
if (has_tstate_and_gil ) {
2546
2550
/* If an exception is set, print the exception with its traceback */
2547
2551
if (!_Py_FatalError_PrintExc (tss_tstate )) {
@@ -2578,14 +2582,14 @@ fatal_error(FILE *stream, int header, const char *prefix, const char *msg,
2578
2582
void _Py_NO_RETURN
2579
2583
Py_FatalError (const char * msg )
2580
2584
{
2581
- fatal_error (stderr , 1 , NULL , msg , -1 );
2585
+ fatal_error (fileno ( stderr ) , 1 , NULL , msg , -1 );
2582
2586
}
2583
2587
2584
2588
2585
2589
void _Py_NO_RETURN
2586
2590
_Py_FatalErrorFunc (const char * func , const char * msg )
2587
2591
{
2588
- fatal_error (stderr , 1 , func , msg , -1 );
2592
+ fatal_error (fileno ( stderr ) , 1 , func , msg , -1 );
2589
2593
}
2590
2594
2591
2595
@@ -2600,12 +2604,12 @@ _Py_FatalErrorFormat(const char *func, const char *format, ...)
2600
2604
reentrant = 1 ;
2601
2605
2602
2606
FILE * stream = stderr ;
2603
- fprintf (stream , "Fatal Python error: " );
2607
+ const int fd = fileno (stream );
2608
+ PUTS (fd , "Fatal Python error: " );
2604
2609
if (func ) {
2605
- fputs ( func , stream );
2606
- fputs ( ": " , stream );
2610
+ PUTS ( fd , func );
2611
+ PUTS ( fd , ": " );
2607
2612
}
2608
- fflush (stream );
2609
2613
2610
2614
va_list vargs ;
2611
2615
#ifdef HAVE_STDARG_PROTOTYPES
@@ -2619,7 +2623,7 @@ _Py_FatalErrorFormat(const char *func, const char *format, ...)
2619
2623
fputs ("\n" , stream );
2620
2624
fflush (stream );
2621
2625
2622
- fatal_error (stream , 0 , NULL , NULL , -1 );
2626
+ fatal_error (fd , 0 , NULL , NULL , -1 );
2623
2627
}
2624
2628
2625
2629
@@ -2630,7 +2634,7 @@ Py_ExitStatusException(PyStatus status)
2630
2634
exit (status .exitcode );
2631
2635
}
2632
2636
else if (_PyStatus_IS_ERROR (status )) {
2633
- fatal_error (stderr , 1 , status .func , status .err_msg , 1 );
2637
+ fatal_error (fileno ( stderr ) , 1 , status .func , status .err_msg , 1 );
2634
2638
}
2635
2639
else {
2636
2640
Py_FatalError ("Py_ExitStatusException() must not be called on success" );
0 commit comments