communicate with external process via pty

Tim Arnold Tim.Arnold at sas.com
Tue Oct 9 15:44:49 EDT 2012


I have an external process, 'tralics' that emits mathml when you feed it 
latex equations. I want to get that mathml into a string.

The problem for me is that tralics wants to talk to a tty and I've never 
done that before; it basically starts its own subshell.

I have the following code which works for simple things. I'm not sure 
this is the best way though: basically I got this from google...

import os,sys
import subprocess
import shlex
import pty
cmd =  'tralics --interactivemath'

(master, slave) = pty.openpty()
p = subprocess.Popen(shlex.split(cmd),close_fds=True,
                      stdin=slave,stdout=slave,stderr=slave)

os.read(master,1024)                # start the process
os.write(master,'$\sqrt{x}$\n')     # feed it an equation
mathml.append(os.read(master,1024)) # get the mathml in a string

os.write(master,'$\sqrt{x}$\n')     # feed more equations
mathml.append(os.read(master,1024)) # get the next string


Any suggestions for improvement?
thanks,
--Tim



More information about the Python-list mailing list