[triangle-zpug] Weird platform dependent python error: forking and pseudoterminals

Chris Rossi chris at spacelabstudio.com
Fri Jul 14 17:22:55 CEST 2006


Hi, I'm observing some weird behavior and have written the following 
test program to demonstrate:

huh.py
-------------------------------------------
import pty,os,sys

# Fork
( pid, fd ) = pty.fork()

if pid == 0:
     # Child thread
     print 'Have a happy day!'
     print 'Forza Azzurri!'

else:
     # Controlling process
     # read stdout from child process
     # and send to stderr
     stream = os.fdopen( fd )
     line = stream.readline()
     while len( line ) > 0:
         sys.stderr.write(line)
         line = stream.readline()
     stream.close()
-------------------------------------------

This program runs perfectly under cygwin on Windows XP using python 2.4.3:

========================================================================
crossi at 6M0D481 ~/proj/plum
$ python huh.py
Have a happy day!
Forza Azzurri!

crossi at 6M0D481 ~/proj/plum
$ python huh.py 2>/dev/null

crossi at 6M0D481 ~/proj/plum
$ python
Python 2.4.3 (#1, May 18 2006, 07:40:45)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
 >>>

crossi at 6M0D481 ~/proj/plum
$
========================================================================

But on Gentoo (2.6 kernel) with Python 2.4.3 it dies with an I/O error 
when it hits the end of the stream:

========================================================================
chris at jupiter ~/proj/plum $ python huh.py
Have a happy day!
Forza Azzurri!
Traceback (most recent call last):
   File "huh.py", line 19, in ?
     line = stream.readline()
IOError: [Errno 5] Input/output error
chris at jupiter ~/proj/plum $ python huh.py 2>/dev/null
chris at jupiter ~/proj/plum $ python
Python 2.4.3 (#1, Jul 13 2006, 23:02:30)
[GCC 3.3.6 (Gentoo 3.3.6, ssp-3.3.6-1.0, pie-8.7.8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>>
========================================================================

Anybody have any idea what's going on here?  Is there a better way to 
fork and capture the output of the child process?

Thanks,
Chris
-- 
http://spacelabstudio.com                              +1 919 403 5502
Occasionally an email never arrives in my inbox.  If it seems like
I'm ignoring you, resend or try calling me.



More information about the triangle-zpug mailing list