@@ -57,7 +57,7 @@ static int allocated;
5757 DEBUG_UNCOLLECTABLE | \
5858 DEBUG_INSTANCES | \
5959 DEBUG_OBJECTS
60- static int debug ;
60+ static int debug = 0 ;
6161
6262/* list of uncollectable objects */
6363static PyObject * garbage ;
@@ -222,9 +222,11 @@ move_finalizers(PyGC_Head *unreachable, PyGC_Head *finalizers)
222222{
223223 PyGC_Head * next ;
224224 PyGC_Head * gc = unreachable -> gc_next ;
225- static PyObject * delstr ;
225+ static PyObject * delstr = NULL ;
226226 if (delstr == NULL ) {
227227 delstr = PyString_InternFromString ("__del__" );
228+ if (delstr == NULL )
229+ Py_FatalError ("PyGC: can't initialize __del__ string" );
228230 }
229231 for (; gc != unreachable ; gc = next ) {
230232 PyObject * op = PyObject_FROM_GC (gc );
@@ -268,30 +270,27 @@ move_finalizer_reachable(PyGC_Head *finalizers)
268270}
269271
270272static void
271- debug_instance (PyObject * output , char * msg , PyInstanceObject * inst )
273+ debug_instance (char * msg , PyInstanceObject * inst )
272274{
273- char buf [200 ];
274275 char * cname ;
275276 /* be careful not to create new dictionaries */
276277 PyObject * classname = inst -> in_class -> cl_name ;
277278 if (classname != NULL && PyString_Check (classname ))
278279 cname = PyString_AsString (classname );
279280 else
280281 cname = "?" ;
281- sprintf ( buf , "gc: %s <%.100s instance at %p>\n" , msg , cname , inst );
282- PyFile_WriteString ( buf , output );
282+ PySys_WriteStderr ( "gc: %.100s <%.100s instance at %p>\n" ,
283+ msg , cname , inst );
283284}
284285
285286static void
286- debug_cycle (PyObject * output , char * msg , PyObject * op )
287+ debug_cycle (char * msg , PyObject * op )
287288{
288289 if ((debug & DEBUG_INSTANCES ) && PyInstance_Check (op )) {
289- debug_instance (output , msg , (PyInstanceObject * )op );
290+ debug_instance (msg , (PyInstanceObject * )op );
290291 } else if (debug & DEBUG_OBJECTS ) {
291- char buf [200 ];
292- sprintf (buf , "gc: %s<%.100s %p>\n" , msg ,
293- op -> ob_type -> tp_name , op );
294- PyFile_WriteString (buf , output );
292+ PySys_WriteStderr ("gc: %.100s <%.100s %p>\n" ,
293+ msg , op -> ob_type -> tp_name , op );
295294 }
296295}
297296
@@ -357,20 +356,15 @@ collect(PyGC_Head *young, PyGC_Head *old)
357356 PyGC_Head unreachable ;
358357 PyGC_Head finalizers ;
359358 PyGC_Head * gc ;
360- PyObject * output = NULL ;
361359
362- if (debug ) {
363- output = PySys_GetObject ("stderr" );
364- }
365360 if (debug & DEBUG_STATS ) {
366- char buf [ 100 ];
367- sprintf ( buf , "gc: collecting generation %d...\n" , generation );
368- PyFile_WriteString ( buf , output );
369- sprintf ( buf , "gc: objects in each generation: %ld %ld %ld\n" ,
361+ PySys_WriteStderr (
362+ "gc: collecting generation %d...\n"
363+ "gc: objects in each generation: %ld %ld %ld\n" ,
364+ generation ,
370365 gc_list_size (& generation0 ),
371366 gc_list_size (& generation1 ),
372367 gc_list_size (& generation2 ));
373- PyFile_WriteString (buf ,output );
374368 }
375369
376370 /* Using ob_refcnt and gc_refs, calculate which objects in the
@@ -408,8 +402,8 @@ collect(PyGC_Head *young, PyGC_Head *old)
408402 for (gc = unreachable .gc_next ; gc != & unreachable ;
409403 gc = gc -> gc_next ) {
410404 m ++ ;
411- if (output != NULL && ( debug & DEBUG_COLLECTABLE ) ) {
412- debug_cycle (output , "collectable " , PyObject_FROM_GC (gc ));
405+ if (debug & DEBUG_COLLECTABLE ) {
406+ debug_cycle ("collectable" , PyObject_FROM_GC (gc ));
413407 }
414408 }
415409 /* call tp_clear on objects in the collectable set. This will cause
@@ -422,19 +416,17 @@ collect(PyGC_Head *young, PyGC_Head *old)
422416 for (gc = finalizers .gc_next ; gc != & finalizers ;
423417 gc = gc -> gc_next ) {
424418 n ++ ;
425- if (output != NULL && ( debug & DEBUG_UNCOLLECTABLE ) ) {
426- debug_cycle (output , "uncollectable " , PyObject_FROM_GC (gc ));
419+ if (debug & DEBUG_UNCOLLECTABLE ) {
420+ debug_cycle ("uncollectable" , PyObject_FROM_GC (gc ));
427421 }
428422 }
429- if (output != NULL && ( debug & DEBUG_STATS ) ) {
423+ if (debug & DEBUG_STATS ) {
430424 if (m == 0 && n == 0 ) {
431- PyFile_WriteString ("gc: done.\n" , output );
425+ PySys_WriteStderr ("gc: done.\n" );
432426 } else {
433- char buf [200 ];
434- sprintf (buf ,
435- "gc: done, %ld unreachable, %ld uncollectable.\n" ,
436- n + m , n );
437- PyFile_WriteString (buf , output );
427+ PySys_WriteStderr (
428+ "gc: done, %ld unreachable, %ld uncollectable.\n" ,
429+ n + m , n );
438430 }
439431 }
440432
@@ -444,7 +436,6 @@ collect(PyGC_Head *young, PyGC_Head *old)
444436 handle_finalizers (& finalizers , old );
445437
446438 allocated = 0 ;
447- PyErr_Clear (); /* in case writing to sys.stderr failed */
448439 return n + m ;
449440}
450441
0 commit comments