summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornoah <noah@656d521f-e311-0410-88e0-e7920216d269>2007-02-19 22:57:59 +0000
committernoah <noah@656d521f-e311-0410-88e0-e7920216d269>2007-02-19 22:57:59 +0000
commit0051dad3264b6bbdbdbce2f5d1ee9ee2289dbc71 (patch)
tree6140567956aa087968465b358ddf97ac9018653e
parente80bfc08f3aa2b92e1bbb6eea8e48dcd91aa9bc8 (diff)
downloadpexpect-0051dad3264b6bbdbdbce2f5d1ee9ee2289dbc71.tar.gz
Added some of Guillaume Chazarain's changes.
Added sendeof and sendintr. Also I added sendcontrl. git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@454 656d521f-e311-0410-88e0-e7920216d269
-rw-r--r--pexpect/pexpect.py70
-rwxr-xr-xpexpect/tests/test_log.py3
2 files changed, 59 insertions, 14 deletions
diff --git a/pexpect/pexpect.py b/pexpect/pexpect.py
index b64ab30..1f96405 100644
--- a/pexpect/pexpect.py
+++ b/pexpect/pexpect.py
@@ -34,7 +34,8 @@ Credits: Noah Spurrier, Richard Holden, Marco Molteni, Kimberley Burchett,
Robert Stone, Hartmut Goebel, Chad Schroeder, Erick Tryzelaar, Dave Kirby, Ids
vander Molen, George Todd, Noel Taylor, Nicolas D. Cesar, Alexander Gattin,
Geoffrey Marshall, Francisco Lourenco, Glen Mabey, Karthik Gurusamy, Fernando
-Perez, Corey Minyard, Jon Cohen (Let me know if I forgot anyone.)
+Perez, Corey Minyard, Jon Cohen, Guillaume Chazarain (Let me know if I forgot
+anyone.)
Free, open source, and all that good stuff.
@@ -895,6 +896,30 @@ class spawn (object):
n = n + self.send (os.linesep)
return n
+ def sendcontrol(self, char):
+
+ """This sends a control character to the child such as Ctrl-C or
+ Ctrl-D. For example, to send a Ctrl-G (ASCII 7):
+
+ child.sendcontrol('g')
+
+ See also, sendintr() and sendeof().
+ """
+
+ c = c.tolower()
+ a = ord(char)
+ if a>=97 and a<=122:
+ a = a - ord('a') + 1
+ self.send (chr(a))
+ d = {'@':0, '`':0,
+ '[':27, '{':27,
+ '\\':28, '|':28,
+ ']':29, '}': 29,
+ '^':30, '~':30,
+ '_':31,
+ '?':127}
+ self.send (chr(d[char]))
+
def sendeof(self):
"""This sends an EOF to the child. This sends a character which causes
@@ -909,18 +934,37 @@ class spawn (object):
### Hmmm... how do I send an EOF?
###C if ((m = write(pty, *buf, p - *buf)) < 0)
###C return (errno == EWOULDBLOCK) ? n : -1;
- fd = sys.stdin.fileno()
- old = termios.tcgetattr(fd) # remember current state
- new = termios.tcgetattr(fd)
- new[3] = new[3] | termios.ICANON # ICANON must be set to recognize EOF
- try: # use try/finally to ensure state gets restored
- termios.tcsetattr(fd, termios.TCSADRAIN, new)
- if 'CEOF' in dir(termios):
- os.write (self.child_fd, '%c' % termios.CEOF)
- else:
- os.write (self.child_fd, '%c' % 4) # Silly platform does not define CEOF so assume CTRL-D
- finally: # restore state
- termios.tcsetattr(fd, termios.TCSADRAIN, old)
+ #fd = sys.stdin.fileno()
+ #old = termios.tcgetattr(fd) # remember current state
+ #new = termios.tcgetattr(fd)
+ #new[3] = new[3] | termios.ICANON # ICANON must be set to recognize EOF
+ #try: # use try/finally to ensure state gets restored
+ # termios.tcsetattr(fd, termios.TCSADRAIN, new)
+ # if 'CEOF' in dir(termios):
+ # os.write (self.child_fd, '%c' % termios.CEOF)
+ # else:
+ # # Silly platform does not define CEOF so assume CTRL-D
+ # os.write (self.child_fd, '%c' % 4)
+ #finally: # restore state
+ # termios.tcsetattr(fd, termios.TCSADRAIN, old)
+ if 'VEOF' in dir(termios):
+ char = termios.tcgetattr(self.child_fd)[6][termios.VEOF]
+ else:
+ # platform does not define VEOF so assume CTRL-D
+ char = chr(4)
+ self.send(char)
+
+ def sendintr(self):
+
+ """This sends a SIGINT to the child. It does not require
+ the SIGINT to be the first character on a line. """
+
+ if 'VINTR' in dir(termios):
+ char = termios.tcgetattr(self.child_fd)[6][termios.VINTR]
+ else:
+ # platform does not define VINTR so assume CTRL-C
+ char = chr(3)
+ self.send (char)
def eof (self):
diff --git a/pexpect/tests/test_log.py b/pexpect/tests/test_log.py
index 5cf91e7..d2de205 100755
--- a/pexpect/tests/test_log.py
+++ b/pexpect/tests/test_log.py
@@ -32,7 +32,8 @@ class TestCaseLog(PexpectTestCase.PexpectTestCase):
mylog.close()
lf = open(filename).read()
os.unlink (filename)
- assert lf == 'This is a test.\nThis is a test.\r\nThis is a test.\r\n'
+ lf = lf.replace(chr(4),'')
+ assert lf == 'This is a test.\nThis is a test.\r\nThis is a test.\r\n', repr(lf)
if __name__ == '__main__':
unittest.main()