trouble controlling vim with subprocess on windows machine

Josiah Carlson josiah.carlson at sbcglobal.net
Tue Jul 10 23:31:44 EDT 2007


agc wrote:
> Hi Josiah,
> 
>>>> This recipe for asynchronous communication usingsubprocesscould be
>>>> used to write an expect-like tool:
>>>>    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440554
> 
> I have played with the above recipe and it is excellent,
> but could you please go into some more detail about what is needed
> to make a cross platform [p]expect like (non-pty based) tool?

We only get ptys.  Unless you can figure out some incantation to fake 
the creation of a tty (so that ssh and other software don't conmplain), 
all you get is a pty.

To make it work like expect, one would (strictly speaking) need to do 
the pattern matching that expect does, which may require writing a new 
regular expression library.


> Specifically, could you describe how you would connect to
> *another* interactive Python process with your subclass of
> subprocess.Popen?
> i.e:
> 
> a = Popen('python', stdin=?, stdout=?, stderr=?)
> #now run an interactive session with 'a'

Some platforms have very strange buffering behavior with Python.  What I
have done in my own code (for creating a Python subprocess, though not 
using the subprocess module) is to do something like the following...

python_cmd = '''<path to python> -u -c "import sys; \
  sys.stderr=sys.__stderr__=sys.stdout;import __builtin__;\
  __builtin__.quit=__builtin__.exit=\
  'use Ctrl-Break to restart *this* interpreter';import code;\
  code.interact(readfunc=raw_input)"'''

a = Popen(python_cmd, subprocess.PIPE, subprocess.PIPE)

> I have tried several combinations of the above and I seem
> to be stuck on the fact that python is interacting with a
> 'tty', not 'std*'.  Maybe I'm missing a basic piece?

I've had buffering issues on certain platforms (Windows), error messages 
about not having a tty (when trying to control an ssh session on *nix 
and Windows), etc.  Getting this to work for arbitrary programs that 
expect a tty, and/or being able to query the terminal for other 
information (dimension, terminal emulation, etc.), will be a chore, if 
not impossible.


  - Josiah



More information about the Python-list mailing list