Skip to content
Snippets Groups Projects
Commit c66c3def authored by Per Cederqvist's avatar Per Cederqvist
Browse files

(inherit_stty): New variable.

(stty_init): New variable.
(set_cloexec_flag): New function.
(spawn): New class.
(spawn.__init__): New method.
(spawn.send): New method.
(spawn.close): New method.
parent 4e4f22be
No related branches found
No related tags found
No related merge requests found
import fcntl
import os import os
import pty
import re import re
import select import select
import sets import sets
...@@ -17,6 +19,14 @@ timeout = 10.0 ...@@ -17,6 +19,14 @@ timeout = 10.0
# Default is to raise an exception. # Default is to raise an exception.
timeout_raises_exception = True timeout_raises_exception = True
# When a new pty is created by spawn(), the terminal settings will be
# inherited from the settings of stdin of the current process.
# Setting inherit_stty to False will disable this inheritance. After
# the inheritance (if any), if stty_init is set to a string, stty will
# be run with that string as its argument.
inherit_stty = True
stty_init = None
# #
# Internal code # Internal code
# #
...@@ -66,6 +76,52 @@ class popen(expectable): ...@@ -66,6 +76,52 @@ class popen(expectable):
def close(self): def close(self):
return self.__cmd.close() return self.__cmd.close()
def set_cloexec_flag(fd):
old = fcntl.fcntl(fd, fcntl.F_GETFD)
fcntl.fcntl(fd, fcntl.F_SETFD, old | fcntl.FD_CLOEXEC)
class spawn(expectable):
def __init__(self, cmd):
if inherit_stty:
f = os.popen("stty -g")
settings = f.read()
f.close()
if isinstance(cmd, types.StringTypes):
cmd = cmd.split(" ")
(r, w) = os.pipe()
set_cloexec_flag(w)
self.__child, self.__pty = pty.fork()
if self.__child == 0:
try:
os.close(r)
if inherit_stty:
os.system("stty %s" % settings)
if stty_init is not None:
os.system("stty %s" % stty_init)
try:
os.execvp(cmd[0], cmd)
except OSError:
os.write(w, "exec failed\n")
finally:
os._exit(1)
else:
os.close(w)
res = os.read(r, 100)
os.close(r)
if res != "":
os.close(self.__pty)
os.waitpid(self.__child, 0)
raise OSError("exec failed")
expectable.__init__(self, self.__pty)
def send(self, s):
os.write(self.__pty, s)
def close(self):
os.close(self.__pty)
return os.waitpid(self.__child, 0)
class impl: class impl:
def __init__(self, timeout = None): def __init__(self, timeout = None):
self.__first = True self.__first = True
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment