Help with a piping error

Hans Mulder hansmu at xs4all.nl
Fri Jun 10 17:56:07 EDT 2011


On 10/06/11 22:56:06, virdo wrote:
> On Jun 10, 4:48 pm, John Gordon<gor... at panix.com>  wrote:
>> In<6e035898-8938-4a61-91de-7a0ea7ead... at y30g2000yqb.googlegroups.com>  virdo<vir... at gmail.com>  writes:
>>
>>> My python file is simple print "test". I run it, it works no problem.
>>> I pipe the output to a file "run>  logfile" and that's the error I
>>> get. This is with Windows Server 2008 (64 bit) using ActivePython
>>> 2.7.1.4 (64 bit).
>>
>> Are you using an actual pipe symbol in your command?  Your post title
>> suggests you are, but your sample command uses the greater-than symbol.
>
> My apologies, I miswrote. It is the greater than symbol rather than a
> pipe.
>
> Example:
>
> c:\PRG>test.py>  test.log
> close failed in file object destructor:
> sys.excepthook is missing
> lost sys.stderr

I think your problem is that some other process still has opened the
file test.log and Python cannot write it.  Unfortunately, Python only
finds out when it is shutting down and cleaning out the "sys" module.
An exception is raised, but sys.excepthook has already been disposed.
Python's normal fallback stategy is to write the exception and the
traceback to sys.stderr.  Unfortunately, sys.stderr has been disposed
as well.

I get a similar message if I try to write to a closed pipe:

$  python -c 'print "test"' | false
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr
$

I think this is a bug in Pyhton: the interpreter should flush sys.stdout
before tering dwn the 'sys' module, so that it can report an excption if
the flush attempt fails.

A work-around is to add "import sys" at the top of your script, and
"sys.stdout.flush()" at the bottom:

import sys
print "test"
sys.stdout.flush()

That should report a proper exception; for example:

$ python -c 'import sys; print "test"; sys.stdout.flush();' | false
Traceback (most recent call last):
   File "<string>", line 1, in <module>
IOError: [Errno 32] Broken pipe
$

Hope this helps,

-- HansM



More information about the Python-list mailing list