print(....,file=sys.stderr) buffered?

Grant Edwards invalid at invalid.invalid
Mon Aug 13 17:43:31 CEST 2012


On 2012-08-13, Helmut Jarausch <jarausch at skynet.be> wrote:
> Hi,
>
> for tracing purposes I have added some print outs like
>
> print('+++ before calling foo',file=sys.stderr)
> x=foo(..)
> print('--- after  calling foo',
>
> and within 'foo'
> print('>>> entering foo ...',file=sys.stderr)
>
> Now, when executing this, I always get
>
> +++ before calling foo
> --- after  calling foo
>>>> entering foo ...
>
> When outputting to stderr from C/C++ it's guaranteed that the different 
> outputs appear in the same order as they have been generated.

You're not printing to stderr in the second print() call -- you're
printing to stdout.  The two file objects have separate buffers and
may even be using two different buffering modes (e.g. line vs. block).
You can't interleave writes to stderr and stdout and assume order is
preserved unless you take specific steps (such as forcing them both to
be unbuffered or flushing them at certain points).

> Is this guarantee no more valid in Python 3.2 ?

If you write to stderr all three times, it should work the way you
want it to.

-- 
Grant Edwards               grant.b.edwards        Yow! ... I'm IMAGINING a
                                  at               sensuous GIRAFFE, CAVORTING
                              gmail.com            in the BACK ROOM of a
                                                   KOSHER DELI --



More information about the Python-list mailing list