Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: The pty module, reading from a pty, and Python 2/3

31 views
Skip to first unread message

Evan Driscoll

unread,
Oct 23, 2012, 11:03:31 PM10/23/12
to pytho...@python.org
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 <module>
> 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
>

Evan Driscoll

unread,
Oct 23, 2012, 10:59:53 PM10/23/12
to pytho...@python.org

Evan Driscoll

unread,
Oct 23, 2012, 11:41:10 PM10/23/12
to pytho...@python.org
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:

David Hutto

unread,
Oct 24, 2012, 2:02:08 AM10/24/12
to Evan Driscoll, pytho...@python.org
at first glance, you have the file open for writing('w'), not
reading('r'), but may not be that.

I'll check if I get a few minutes.



--
Best Regards,
David Hutto
CEO: http://www.hitwebdevelopment.com

David Hutto

unread,
Oct 24, 2012, 2:33:45 AM10/24/12
to Evan Driscoll, pytho...@python.org
import sys
import pty
import os

def get_text(filename):
#you need to find the file size, and place it as an integer in read
below, where you return the value
statinfo = os.stat(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)
#you have to input into read, how many characters you want read in. if
you place a random integer in, it will read to that integer within the
file
return child_pty.read(statinfo.st_size)


if __name__ == "__main__":
print(get_text("my-pty-test.py"))


David Hutto

unread,
Oct 24, 2012, 2:40:44 AM10/24/12
to Evan Driscoll, pytho...@python.org
#better coded for you to understand

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)
#you need to find the file size, and place it as an integer in read
below, where you return the value
#you have to input into read, how many characters you want read in
with statinfo.st_size
statinfo = os.stat(filename)

David Hutto

unread,
Oct 24, 2012, 2:46:52 AM10/24/12
to Evan Driscoll, pytho...@python.org
in other words, the last two lines of your function should be:

statinfo = os.stat(filename)
return child_pty.read(statinfo.st_size)


0 new messages