comparison Python/pythonrun.c @ 42167:29ba6c399090

Upon exit, flush stdout and stderr (twice: before and after the code that attempts to delete all modules). This makes test_subprocess work again. (I can't quite figure out why stdout/stderr don't get deleted properly, which would flush them anyway, but that's a separate issue.)
author Guido van Rossum <guido@python.org>
date Mon, 09 Jul 2007 15:04:50 +0000
parents 825041fc8e2c
children 9f99ea4f81e9
comparison
equal deleted inserted replaced
42166:78203298b42b 42167:29ba6c399090
289 289
290 #ifdef COUNT_ALLOCS 290 #ifdef COUNT_ALLOCS
291 extern void dump_counts(FILE*); 291 extern void dump_counts(FILE*);
292 #endif 292 #endif
293 293
294 /* Flush stdout and stderr */
295
296 void
297 flush_std_files()
298 {
299 PyObject *fout = PySys_GetObject("stdout");
300 PyObject *ferr = PySys_GetObject("stderr");
301 PyObject *tmp;
302
303 if (fout != NULL) {
304 tmp = PyObject_CallMethod(fout, "flush", "");
305 if (tmp == NULL)
306 PyErr_Clear();
307 else
308 Py_DECREF(tmp);
309 }
310
311 if (ferr != NULL) {
312 tmp = PyObject_CallMethod(ferr, "flush", "");
313 if (tmp == NULL)
314 PyErr_Clear();
315 else
316 Py_DECREF(tmp);
317 }
318 }
319
294 /* Undo the effect of Py_Initialize(). 320 /* Undo the effect of Py_Initialize().
295 321
296 Beware: if multiple interpreter and/or thread states exist, these 322 Beware: if multiple interpreter and/or thread states exist, these
297 are not wiped out; only the current thread and interpreter state 323 are not wiped out; only the current thread and interpreter state
298 are deleted. But since everything else is deleted, those other 324 are deleted. But since everything else is deleted, those other
323 * threads created thru it, so this also protects pending imports in 349 * threads created thru it, so this also protects pending imports in
324 * the threads created via Threading. 350 * the threads created via Threading.
325 */ 351 */
326 call_py_exitfuncs(); 352 call_py_exitfuncs();
327 initialized = 0; 353 initialized = 0;
354
355 /* Flush stdout+stderr */
356 flush_std_files();
328 357
329 /* Get current thread state and interpreter pointer */ 358 /* Get current thread state and interpreter pointer */
330 tstate = PyThreadState_GET(); 359 tstate = PyThreadState_GET();
331 interp = tstate->interp; 360 interp = tstate->interp;
332 361
358 /* nothing */; 387 /* nothing */;
359 #endif 388 #endif
360 389
361 /* Destroy all modules */ 390 /* Destroy all modules */
362 PyImport_Cleanup(); 391 PyImport_Cleanup();
392
393 /* Flush stdout+stderr (again, in case more was printed) */
394 flush_std_files();
363 395
364 /* Collect final garbage. This disposes of cycles created by 396 /* Collect final garbage. This disposes of cycles created by
365 * new-style class definitions, for example. 397 * new-style class definitions, for example.
366 * XXX This is disabled because it caused too many problems. If 398 * XXX This is disabled because it caused too many problems. If
367 * XXX a __del__ or weakref callback triggers here, Python code has 399 * XXX a __del__ or weakref callback triggers here, Python code has