Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'importing': 0.04; 'subject:Python': 0.05; 'sys': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'except:': 0.07; 'raised': 0.07; 'try:': 0.07; 'python': 0.09; '"w")': 0.09; '2to3': 0.09; 'exits': 0.09; 'filename)': 0.09; 'ioerror': 0.09; 'ioerror:': 0.09; 'naturally': 0.09; 'oh,': 0.09; 'oserror': 0.09; 'spawn': 0.09; 'subject:module': 0.09; 'through,': 0.09; 'worked,': 0.09; 'def': 0.10; ':-)': 0.13; '"about': 0.16; '(just': 0.16; '2.)': 0.16; '2.7.3': 0.16; '24,': 0.16; 'driscoll': 0.16; 'propagated': 0.16; 'pty': 0.16; 'py3': 0.16; 'record,': 0.16; 'relevant.': 0.16; 'sys.exit(1)': 0.16; 'wrote:': 0.17; 'skip:p 30': 0.20; 'mostly': 0.20; 'import': 0.21; 'pipe': 0.22; 'example': 0.23; "i've": 0.23; 'seems': 0.23; 'url:bugs': 0.24; 'linux': 0.24; 'testing': 0.24; 'header:User-Agent:1': 0.26; '(which': 0.26; '(most': 0.27; "doesn't": 0.28; 'subject:/': 0.28; 'actual': 0.28; 'run': 0.28; 'parent': 0.29; 'though.': 0.29; "i'm": 0.29; 'worked': 0.30; 'writes': 0.30; 'error': 0.30; 'url:python': 0.32; 'file': 0.32; 'suggestion': 0.32; 'print': 0.32; 'curious': 0.33; 'rid': 0.33; 'traceback': 0.33; 'ubuntu': 0.33; 'problem': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'returning': 0.35; 'pm,': 0.35; 'received:192.168.0': 0.35; "won't": 0.35; 'except': 0.36; 'but': 0.36; 'url:org': 0.36; 'child': 0.36; 'test': 0.36; 'charset:us-ascii': 0.36; 'enough': 0.36; 'ok,': 0.37; 'being': 0.37; 'why': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'easily': 0.39; 'little': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'think': 0.40; 'further': 0.61; 'subject:, ': 0.61; 'more': 0.63; 'enthusiastic': 0.65; 'subject:The': 0.71; 'sounds': 0.71; 'information:': 0.74; '100%': 0.76; 'dumb': 0.84; "it'd": 0.84; 'quits.': 0.84; 'received:192.168.0.3': 0.84; 'sunshine': 0.84; 'reducing': 0.95 MIME-version: 1.0 Content-transfer-encoding: 7BIT Content-type: text/plain; CHARSET=US-ASCII; format=flowed Date: Tue, 23 Oct 2012 22:41:10 -0500 From: Evan Driscoll Subject: Re: The pty module, reading from a pty, and Python 2/3 In-reply-to: <50875A83.6030806@wisc.edu> To: python-list@python.org X-Spam-Report: AuthenticatedSender=yes, SenderIP=192.168.0.3 X-Spam-PmxInfo: Server=avs-15, Version=5.6.1.2065439, Antispam-Engine: 2.7.2.376379, Antispam-Data: 2012.10.24.33015, SenderIP=192.168.0.3 References: <508759A9.7070601@wisc.edu> <50875A83.6030806@wisc.edu> User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351053627 news.xs4all.nl 6946 [2001:888:2000:d::a6]:41373 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31989 OK, one more self-reply. :-) I found http://bugs.python.org/issue5380 which seems to be relevant. Sounds like the OS is returning an error, and that's naturally being propagated through Python. And further testing reveals the problem only surfaces when the child actually exits -- if the child writes data to the pipe and is more enthusiastic about living, then the parent can read it successfully. While it'd be nice if my example worked, for my actual purpose I think that's good enough (I just won't be able to test *quite* as easily for a while). I am still curious if anyone know why it worked in 2 though. Evan On 10/23/2012 10:03 PM, Evan Driscoll wrote: > Oh, and a little more information: > > The log.txt file I create has the message that it's "about to execlp", > and the exec() *does* actually happen -- the IOError is raised after > the child process quits. > > Evan > > > > On 10/23/2012 09:59 PM, Evan Driscoll wrote: >> I have the following program. Everything is sunshine and rainbows >> when I run in in Python 2, but when I run it under Python 3 I get an >> IOError. 2to3 only reports one dumb suggestion re. a print call >> (which I can get rid of by importing __future__'s print_function, and >> then it just suggests removing that import). >> >> Can anyone shed any light? I am on Ubuntu Linux with Python 2.7.3 and >> 3.2.3. >> >> >> (Just for the record, I figured out that it ran under Python 2 by >> accident as I was reducing it for a "why doesn't this run?" email. >> :-) I'm not super-familiar with Py3 as I've mostly only worked with 2.) >> >> I'm not 100% sure how this will come through, so I've also put it at >> http://pastebin.com/60wjXSF3. >> >> Evan >> >> >> import sys >> import pty >> import os >> >> def get_text(filename): >> try: >> ( child_pid, fd ) = pty.fork() # OK >> except OSError as e: >> print(str(e)) >> sys.exit(1) >> >> if child_pid == 0: >> try: >> with open("log.txt", "w") as f: >> f.write("about to execlp") >> os.execlp("cat", "cat", filename) >> except: >> with open("log.txt", "w") as f: >> f.write("could not spawn process") >> print("Could not spawn") >> sys.exit(1) >> >> child_pty = os.fdopen(fd) >> return child_pty.read() >> >> >> if __name__ == "__main__": >> print(get_text("my-pty-test.py")) >> >> >> The read error I get is >> >> Traceback (most recent call last): >> File "my-pty-test.py", line 28, in >> print(get_text("my-pty-test.py")) >> File "my-pty-test.py", line 24, in get_text >> return child_pty.read() >> IOError: [Errno 5] Input/output error >> >