def test_interact_spawnu_eof(self):
" Ensure subprocess receives unicode, EOF, and exit. "
p = pexpect.spawnu(self.interact_ucs_py, timeout=5, env=self.env)
p.expect('<in >')
p.sendline('?lpha')
p.sendline('?eta')
p.expect('<out>?lpha')
p.expect('<out>?eta')
p.sendeof()
p.expect_exact('<eof>')
p.expect_exact('Escaped interact')
p.expect(pexpect.EOF)
assert not p.isalive()
assert p.exitstatus == 0
python类spawnu()的实例源码
def test_expect_basic (self):
p = pexpect.spawnu('cat')
p.sendline('Hello')
p.sendline('there')
p.sendline('Mr. þython') # þ is more like th than p, but never mind
p.expect('Hello')
p.expect('there')
p.expect('Mr. þython')
p.sendeof ()
p.expect (pexpect.EOF)
def test_expect_setecho_toggle(self):
'''This tests that echo may be toggled off.
'''
p = pexpect.spawnu('cat', timeout=5)
try:
self._expect_echo_toggle_off(p)
except IOError:
if sys.platform.lower().startswith('sunos'):
if hasattr(unittest, 'SkipTest'):
raise unittest.SkipTest("Not supported on this platform.")
return 'skip'
raise
self._expect_echo_toggle_on(p)
def test_expect_echo_exact (self):
'''Like test_expect_echo(), but using expect_exact().
'''
p = pexpect.spawnu('cat', timeout=5)
p.expect = p.expect_exact
self._expect_echo(p)
def test_expect_setecho_toggle_exact(self):
p = pexpect.spawnu('cat', timeout=5)
p.expect = p.expect_exact
try:
self._expect_echo_toggle_off(p)
except IOError:
if sys.platform.lower().startswith('sunos'):
if hasattr(unittest, 'SkipTest'):
raise unittest.SkipTest("Not supported on this platform.")
return 'skip'
raise
self._expect_echo_toggle_on(p)
def test_log_unicode(self):
msg = "abc?÷"
filename_send = tempfile.mktemp()
filename_read = tempfile.mktemp()
p = pexpect.spawnu('cat')
if platform.python_version_tuple() < ('3', '0', '0'):
import codecs
def open(fname, mode, **kwargs):
if 'newline' in kwargs:
del kwargs['newline']
return codecs.open(fname, mode, **kwargs)
else:
import io
open = io.open
p.logfile_send = open(filename_send, 'w', encoding='utf-8')
p.logfile_read = open(filename_read, 'w', encoding='utf-8')
p.sendline(msg)
p.sendeof()
p.expect(pexpect.EOF)
p.close()
p.logfile_send.close()
p.logfile_read.close()
# ensure the 'send' log is correct,
with open(filename_send, 'r', encoding='utf-8') as f:
self.assertEqual(f.read(), msg + '\n\x04')
# ensure the 'read' log is correct,
with open(filename_read, 'r', encoding='utf-8', newline='') as f:
output = f.read().replace(_CAT_EOF, '')
self.assertEqual(output, (msg + '\r\n')*2 )
def test_spawn_utf8_incomplete(self):
# This test case ensures correct incremental decoding, which
# otherwise fails when the stream inspected by os.read()
# does not align exactly at a utf-8 multibyte boundry:
# UnicodeDecodeError: 'utf8' codec can't decode byte 0xe2 in
# position 0: unexpected end of data
p = pexpect.spawnu('cat', maxread=1)
p.sendline('????????')
p.sendeof()
p.expect('????????')
def main():
p = pexpect.spawnu(sys.executable + ' echo_w_prompt.py',
env=no_coverage_env())
p.interact()
print("Escaped interact")