summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Quast <contact@jeffquast.com>2015-09-15 17:26:50 -0700
committerJeff Quast <contact@jeffquast.com>2015-09-15 17:26:50 -0700
commit1a8bd9750d7023b32b647677874211b38a3772f6 (patch)
tree3b8821621e229cecf3e13007ae4c0dc0ea7b225f
parentcde8e15893edd87c61acf0515fd217554d21ce51 (diff)
downloadpexpect-allow-disable-escape-interact.tar.gz
Allow {p}.interact(escape_character=None)allow-disable-escape-interact
For those who wish to disable the ability to escape using process (likely, human) input until normal process termination, they may now set the value of escape_character to None. Some of the related docstring on escape_character was made more brief and clear about its behaviour.
-rw-r--r--pexpect/pty_spawn.py15
-rwxr-xr-xtests/interact.py5
-rwxr-xr-xtests/test_interact.py12
3 files changed, 24 insertions, 8 deletions
diff --git a/pexpect/pty_spawn.py b/pexpect/pty_spawn.py
index 6fac890..aadb2b6 100644
--- a/pexpect/pty_spawn.py
+++ b/pexpect/pty_spawn.py
@@ -684,11 +684,10 @@ class spawn(SpawnBase):
the stdout and stderr output of the child process is printed. This
simply echos the child stdout and child stderr to the real stdout and
it echos the real stdin to the child stdin. When the user types the
- escape_character this method will stop. The default for
- escape_character is ^]. This should not be confused with ASCII 27 --
- the ESC character. ASCII 29 was chosen for historical merit because
- this is the character used by 'telnet' as the escape character. The
- escape_character will not be sent to the child process.
+ escape_character this method will return None. The escape_character
+ will not be transmitted. The default for escape_character is
+ entered as ``Ctrl - ]``, the very same for bsd telnet. To prevent
+ escaping, escape_character may be set to None.
You may pass in optional input and output filter functions. These
functions should take a string and return a string. The output_filter
@@ -720,7 +719,7 @@ class spawn(SpawnBase):
self.buffer = self.string_type()
mode = tty.tcgetattr(self.STDIN_FILENO)
tty.setraw(self.STDIN_FILENO)
- if PY3:
+ if escape_character is not None and PY3:
escape_character = escape_character.encode('latin-1')
try:
self.__interact_copy(escape_character, input_filter, output_filter)
@@ -770,7 +769,9 @@ class spawn(SpawnBase):
data = self.__interact_read(self.STDIN_FILENO)
if input_filter:
data = input_filter(data)
- i = data.rfind(escape_character)
+ i = -1
+ if escape_character is not None:
+ i = data.rfind(escape_character)
if i != -1:
data = data[:i]
self.__interact_writen(self.child_fd, data)
diff --git a/tests/interact.py b/tests/interact.py
index 9f8e672..93b9bea 100755
--- a/tests/interact.py
+++ b/tests/interact.py
@@ -33,7 +33,10 @@ import sys
def main():
p = pexpect.spawn(sys.executable + ' echo_w_prompt.py',
env=no_coverage_env())
- p.interact()
+ escape_character = chr(29) # default matches api
+ if len(sys.argv) > 2 and sys.argv[1] == '--no-escape':
+ escape_character = None
+ p.interact(escape_character=escape_character)
print("Escaped interact")
if __name__ == '__main__':
diff --git a/tests/test_interact.py b/tests/test_interact.py
index e635cb0..21d083b 100755
--- a/tests/test_interact.py
+++ b/tests/test_interact.py
@@ -57,6 +57,18 @@ class InteractTestCase (PexpectTestCase.PexpectTestCase):
assert not p.isalive()
assert p.exitstatus == 0
+ def test_interact_escape_None(self):
+ " Return only after Termination when `escape_character=None'. "
+ p = pexpect.spawn('{self.interact_py} --no-escape'.format(self=self),
+ timeout=5, env=self.env)
+ p.expect('<in >')
+ p.sendcontrol('d')
+ p.expect('<eof>')
+ p.expect_exact('Escaped interact')
+ p.expect(pexpect.EOF)
+ assert not p.isalive()
+ assert p.exitstatus == 0
+
def test_interact_spawn_eof(self):
" Ensure subprocess receives EOF and exit. "
p = pexpect.spawn(self.interact_py, timeout=5, env=self.env)