[Python] script bash-pipe compatibile

Matteo Bertini matteo a naufraghi.net
Sab 24 Ott 2009 17:59:37 CEST


Dopo ulteriori indagini, la cosa sembra legata al
http://bugs.python.org/issue1488934

in particolare:

###
$ cat produce.py
import sys

for x in range(400):
    print 'line '*20
    if 'flush' in sys.argv:
        try:
            sys.stdout.flush()
        except IOError:
            if 'pass' in sys.argv:
                pass
            else:
                raise
###

ha come effetto:

$ python produce.py | head -1
line line line line line line line line line line line line line line line
line line line line line
Traceback (most recent call last):
  File "produce.py", line 4, in <module>
    sys.stdout.write('line '*20+'\n')
IOError: [Errno 32] Broken pipe
$ python produce.py | head -1
line line line line line line line line line line line line line line line
line line line line line
close failed in file object destructor:
Error in sys.excepthook:

Original exception was:  <--------------- random, a volte IOError, a volte
errore del distruttore

$ python produce.py flush | head -1
line line line line line line line line line line line line line line line
line line line line line
Traceback (most recent call last):
  File "produce.py", line 7, in <module>
    sys.stdout.flush()
IOError: [Errno 32] Broken pipe   <------------- "sempre" IOError (ho
provato un po' di volte)


$ python produce.py flush pass | head -1
line line line line line line line line line line line line line line line
line line line line line

sempre OK (idem, per un numero N << infinito di prove)
-------------- parte successiva --------------
Un allegato HTML è stato rimosso...
URL: http://lists.python.it/pipermail/python/attachments/20091024/b34a54db/attachment.htm 


Maggiori informazioni sulla lista Python