Problem flushing stderr in embedded python

Farshid Lashkari lashkariNO at
Wed Nov 10 21:00:09 CET 2004

Here's the code for PySys_GetObject. It's getting the object from the sys
dictionary, so it should be the correct object.

PyObject *
PySys_GetObject(char *name)
    PyThreadState *tstate = PyThreadState_Get();
    PyObject *sd = tstate->interp->sysdict;
    if (sd == NULL)
        return NULL;
    return PyDict_GetItemString(sd, name);

I've tried calling fflush on stderr, but that doesn't work either.

I've encountered another weird problem also. If my python code causes an
error and I don't flush stderr, then I get a runtime error when my app
exits. It also prints out:

Exception exceptions.NameError: "global name 'y' is not defined" in 'garbage
collection' ignored
Fatal Python error: unexpected exception during garbage collection.

Does this give anybody a clue as to what's happening?

"Donn Cave" <donn at> wrote in message
news:1100065836.697971 at yasure...
> Quoth "Farshid Lashkari" <lashkariNO at>:
> | I don't understand it from a C perspective either. I poked around the
> | code for python and found where it is flushing the file. It is simply
> | calling fflush on the FILE handle. Also, I'm somewhat bewildered as to
> | running the string "sys.stderr.flush()" is different than directly
> | the flush method of the stderr object. Oh well, I guess some things are
> | better left unknown.
> It could be different if the stderr object - i.e.,
PySys_GetObject("stderr") -
> is actually not sys.stderr.  I don't understand why, but am not familiar
> with PySys_*, so I offer that as a hypothesis you might be able to test.
> By the way, since as you observed it's only calling fflush(), I think you
> could leave Python out of it altogether and do this with just
>    fflush(stdout);
>    fflush(stderr);
> Donn Cave, donn at

More information about the Python-list mailing list