views:

49

answers:

1

Hello,

I am currently trying to send binary data out through pexpect. For some reason, the data gets through just find except for a 0x04, which is just skipped over. I tracked down the pexpect call to determine that all thats happening is an os.write() call to a file descriptor opened from a pty.fork() command.

Any ideas?

(example code that exemplifies the problem)

import os, pty, sys


pid, child_fd = pty.fork()

if pid: # Parent
    os.write(child_fd, b"'\x04hmm\x04'\n")
    buf = os.read(child_fd, 100)
    print buf
else:   # Child
    text = sys.stdin.readline()
    print ''.join(["%02X " % ord(x) for x in text])

Result:

$ python test.py
'hmm'
27 68 6D 6D 27 0A
+1  A: 

0x04 is ^D, which is the end-of-file keypress. Has the pty been set in raw mode? Maybe the driver is eating it.

If you make it:

os.write(child_fd, b"'\x04hmm\x16\x04'\n")

you can see that indeed the driver is doing translation. \x16 is the same as ^V, which is how you quote things. It makes sense the translation would only be happening from the master (the pretend physical terminal) and the slave. The pretend physical terminal is where (on a normal terminal device) the person would be typing

I'm not sure how to get the driver to stop doing that. If the child sets its terminal to raw mode then that will likely do it.

Omnifarious
Also, i know you can send this character over stdin because if you pass it in through an "exec -e" call, it gets in just fine.
Rannick
@Rannick - Made comment, then realized it would do better as an answer edit.
Omnifarious
@Omnifarious, the tip works for my particular situation, i think im jsut going to write a binary driver that quotes all of this data so i can get away with it. If you don't feel the question is totally answered, i'll leave this open, otherwise, at least for my purposes, i have a path to the functionality i need from pexpect.
Rannick
@omnifarious, i deleted my first comment, as you totally proved me wrong. i was indeed off base. I see that the exec -e call is NOT creating a pseudo-terminal.
Rannick