def test_esc_delay_cbreak_timout_0(): """esc_delay still in effect with timeout of 0 ("nonblocking").""" pid, master_fd = pty.fork() if pid is 0: # child try: cov = __import__('cov_core_init').init() except ImportError: cov = None term = TestTerminal() os.write(sys.__stdout__.fileno(), SEMAPHORE) with term.cbreak(): stime = time.time() inp = term.inkey(timeout=0) measured_time = (time.time() - stime) * 100 os.write(sys.__stdout__.fileno(), ( '%s %i' % (inp.name, measured_time,)).encode('ascii')) sys.stdout.flush() if cov is not None: cov.stop() cov.save() os._exit(0) with echo_off(master_fd): os.write(master_fd, u'\x1b'.encode('ascii')) read_until_semaphore(master_fd) stime = time.time() key_name, duration_ms = read_until_eof(master_fd).split() pid, status = os.waitpid(pid, 0) assert key_name == u'KEY_ESCAPE' assert os.WEXITSTATUS(status) == 0 assert math.floor(time.time() - stime) == 0.0 assert 35 <= int(duration_ms) <= 45, int(duration_ms)
def child(kind): t = TestTerminal(kind=kind, stream=StringIO()) assert t._init_descriptor == sys.__stdout__.fileno() assert isinstance(t.height, int) assert isinstance(t.width, int) assert t.height == t._height_and_width()[0] assert t.width == t._height_and_width()[1]
def child(): term = TestTerminal() with term.cbreak(): stime = time.time() inp = term.inkey(timeout=0) assert (inp == u'') assert (math.floor(time.time() - stime) == 0.0)
def child(kind): t = TestTerminal(kind=kind, stream=StringIO()) assert t._init_descriptor == sys.__stdout__.fileno() assert (isinstance(t.height, int)) assert (isinstance(t.width, int)) assert t.height == t._height_and_width()[0] assert t.width == t._height_and_width()[1]
def test_esc_delay_cbreak_timout_0(): """esc_delay still in effect with timeout of 0 ("nonblocking").""" pid, master_fd = pty.fork() if pid is 0: # child try: cov = __import__('cov_core_init').init() except ImportError: cov = None term = TestTerminal() os.write(sys.__stdout__.fileno(), SEMAPHORE) with term.cbreak(): stime = time.time() inp = term.inkey(timeout=0) measured_time = (time.time() - stime) * 100 os.write(sys.__stdout__.fileno(), ('%s %i' % ( inp.name, measured_time, )).encode('ascii')) sys.stdout.flush() if cov is not None: cov.stop() cov.save() os._exit(0) with echo_off(master_fd): os.write(master_fd, u'\x1b'.encode('ascii')) read_until_semaphore(master_fd) stime = time.time() key_name, duration_ms = read_until_eof(master_fd).split() pid, status = os.waitpid(pid, 0) assert key_name == u'KEY_ESCAPE' assert os.WEXITSTATUS(status) == 0 assert math.floor(time.time() - stime) == 0.0 assert 35 <= int(duration_ms) <= 45, int(duration_ms)
def child(kind): t = TestTerminal(kind=kind) # test simple sugar, if t.bold: expected_output = u''.join((t.bold, u'hi', t.normal)) else: expected_output = u'hi' assert t.bold(u'hi') == expected_output # Plain strs for Python 2.x if t.green: expected_output = u''.join((t.green, 'hi', t.normal)) else: expected_output = u'hi' assert t.green('hi') == expected_output # Test unicode if t.underline: expected_output = u''.join((t.underline, u'boö', t.normal)) else: expected_output = u'boö' assert (t.underline(u'boö') == expected_output) if t.subscript: expected_output = u''.join((t.subscript, u'[1]', t.normal)) else: expected_output = u'[1]' assert (t.subscript(u'[1]') == expected_output)
def test_inkey_0s_cbreak_multibyte_utf8(): "0-second inkey with multibyte utf-8 input; should decode immediately." # utf-8 bytes represent "latin capital letter upsilon". pid, master_fd = pty.fork() if pid is 0: # child try: cov = __import__('cov_core_init').init() except ImportError: cov = None term = TestTerminal() read_until_semaphore(sys.__stdin__.fileno(), semaphore=SEMAPHORE) os.write(sys.__stdout__.fileno(), SEMAPHORE) with term.cbreak(): inp = term.inkey(timeout=0) os.write(sys.__stdout__.fileno(), inp.encode('utf-8')) if cov is not None: cov.stop() cov.save() os._exit(0) with echo_off(master_fd): os.write(master_fd, SEND_SEMAPHORE) os.write(master_fd, u'\u01b1'.encode('utf-8')) read_until_semaphore(master_fd) stime = time.time() output = read_until_eof(master_fd) pid, status = os.waitpid(pid, 0) assert output == u'Ʊ' assert os.WEXITSTATUS(status) == 0 assert math.floor(time.time() - stime) == 0.0
def test_inkey_1s_cbreak_input(): "1-second inkey w/multibyte sequence; should return after ~1 second." pid, master_fd = pty.fork() if pid is 0: # child try: cov = __import__('cov_core_init').init() except ImportError: cov = None term = TestTerminal() os.write(sys.__stdout__.fileno(), SEMAPHORE) with term.cbreak(): inp = term.inkey(timeout=3) os.write(sys.__stdout__.fileno(), inp.name.encode('utf-8')) sys.stdout.flush() if cov is not None: cov.stop() cov.save() os._exit(0) with echo_off(master_fd): read_until_semaphore(master_fd) stime = time.time() time.sleep(1) os.write(master_fd, u'\x1b[C'.encode('ascii')) output = read_until_eof(master_fd) pid, status = os.waitpid(pid, 0) assert output == u'KEY_RIGHT' assert os.WEXITSTATUS(status) == 0 assert math.floor(time.time() - stime) == 1.0
def test_inkey_0s_cbreak_input(): "0-second inkey with input; Keypress should be immediately returned." pid, master_fd = pty.fork() if pid is 0: try: cov = __import__('cov_core_init').init() except ImportError: cov = None # child pauses, writes semaphore and begins awaiting input term = TestTerminal() read_until_semaphore(sys.__stdin__.fileno(), semaphore=SEMAPHORE) os.write(sys.__stdout__.fileno(), SEMAPHORE) with term.cbreak(): inp = term.inkey(timeout=0) os.write(sys.__stdout__.fileno(), inp.encode('utf-8')) if cov is not None: cov.stop() cov.save() os._exit(0) with echo_off(master_fd): os.write(master_fd, SEND_SEMAPHORE) os.write(master_fd, u'x'.encode('ascii')) read_until_semaphore(master_fd) stime = time.time() output = read_until_eof(master_fd) pid, status = os.waitpid(pid, 0) assert output == u'x' assert os.WEXITSTATUS(status) == 0 assert math.floor(time.time() - stime) == 0.0
def child(): with tempfile.NamedTemporaryFile() as stream: term = TestTerminal(stream=stream) with mock.patch("tty.setraw") as mock_setraw: with term.raw(): assert not mock_setraw.called assert term.keyboard_fd is None
def child_mnemonics_wontmove(kind): from blessed.sequences import measure_length t = TestTerminal(kind=kind) assert (0 == measure_length(u'', t)) # not even a mbs assert (0 == measure_length(u'xyzzy', t)) # negative numbers, though printable as %d, do not result # in movement; just garbage. Also not a valid sequence. assert (0 == measure_length(t.cuf(-333), t)) assert (len(t.clear_eol) == measure_length(t.clear_eol, t)) # various erases don't *move* assert (len(t.clear_bol) == measure_length(t.clear_bol, t)) assert (len(t.clear_eos) == measure_length(t.clear_eos, t)) assert (len(t.bold) == measure_length(t.bold, t)) # various paints don't move assert (len(t.red) == measure_length(t.red, t)) assert (len(t.civis) == measure_length(t.civis, t)) if t.cvvis: assert (len(t.cvvis) == measure_length(t.cvvis, t)) assert (len(t.underline) == measure_length(t.underline, t)) assert (len(t.reverse) == measure_length(t.reverse, t)) for _num in range(t.number_of_colors): assert (len(t.color(_num)) == measure_length(t.color(_num), t)) assert (len(t.normal) == measure_length(t.normal, t)) assert (len(t.normal_cursor) == measure_length(t.normal_cursor, t)) assert (len(t.hide_cursor) == measure_length(t.hide_cursor, t)) assert (len(t.save) == measure_length(t.save, t)) assert (len(t.italic) == measure_length(t.italic, t)) assert (len(t.standout) == measure_length(t.standout, t)), (t.standout, t._wont_move)
def child(): term = TestTerminal(stream=StringIO.StringIO()) with term.cbreak(): stime = time.time() inp = term.inkey(timeout=1) assert (inp == u'') assert (math.floor(time.time() - stime) == 1.0)
def child_mnemonics_wontmove(kind): from blessed.sequences import measure_length t = TestTerminal(kind=kind) assert (0 == measure_length(u'', t)) # not even a mbs assert (0 == measure_length(u'xyzzy', t)) # negative numbers, though printable as %d, do not result # in movement; just garbage. Also not a valid sequence. assert (0 == measure_length(t.cuf(-333), t)) assert (len(t.clear_eol) == measure_length(t.clear_eol, t)) # various erases don't *move* assert (len(t.clear_bol) == measure_length(t.clear_bol, t)) assert (len(t.clear_eos) == measure_length(t.clear_eos, t)) assert (len(t.bold) == measure_length(t.bold, t)) # various paints don't move assert (len(t.red) == measure_length(t.red, t)) assert (len(t.civis) == measure_length(t.civis, t)) if t.cvvis: assert (len(t.cvvis) == measure_length(t.cvvis, t)) assert (len(t.underline) == measure_length(t.underline, t)) assert (len(t.reverse) == measure_length(t.reverse, t)) for _num in range(t.number_of_colors): assert (len(t.color(_num)) == measure_length(t.color(_num), t)) assert (len(t.normal) == measure_length(t.normal, t)) assert (len(t.normal_cursor) == measure_length(t.normal_cursor, t)) assert (len(t.hide_cursor) == measure_length(t.hide_cursor, t)) assert (len(t.save) == measure_length(t.save, t)) assert (len(t.italic) == measure_length(t.italic, t)) assert (len(t.standout) == measure_length(t.standout, t) ), (t.standout, t._wont_move)
def child(kind): t = TestTerminal(kind=kind) try: t.bold_misspelled('hey') assert not t.is_a_tty or False, 'Should have thrown exception' except TypeError: e = sys.exc_info()[1] assert 'probably misspelled' in e.args[0] try: t.bold_misspelled(u'hey') # unicode assert not t.is_a_tty or False, 'Should have thrown exception' except TypeError: e = sys.exc_info()[1] assert 'probably misspelled' in e.args[0] try: t.bold_misspelled(None) # an arbitrary non-string assert not t.is_a_tty or False, 'Should have thrown exception' except TypeError: e = sys.exc_info()[1] assert 'probably misspelled' not in e.args[0] if platform.python_implementation() != 'PyPy': # PyPy fails to toss an exception, Why?! try: t.bold_misspelled('a', 'b') # >1 string arg assert not t.is_a_tty or False, 'Should have thrown exception' except TypeError: e = sys.exc_info()[1] assert 'probably misspelled' in e.args[0], e.args
def child(kind): t = TestTerminal(kind=kind, stream=StringIO(), force_styling=True) with t.location(0, 0): pass expected_output = u''.join( (unicode_cap('sc'), unicode_parm('cup', 0, 0), unicode_cap('rc'))) assert (t.stream.getvalue() == expected_output)
def child(): t = TestTerminal() try: my_wrapped = t.wrap(u'------- -------------', WIDTH) except ValueError, err: assert err.args[0] == ( "invalid width %r(%s) (must be integer > 0)" % (WIDTH, type(WIDTH)))
def child(kind): t = TestTerminal(stream=StringIO(), force_styling=True) t.hide_cursor = u'BEGIN' t.normal_cursor = u'END' with t.hidden_cursor(): pass expected_output = u''.join((t.hide_cursor, t.normal_cursor)) assert (t.stream.getvalue() == expected_output)
def child(kind): buf = StringIO() t = TestTerminal(stream=buf, force_styling=True) y, x = 10, 20 with t.location(y, x): xy_val = t.move(x, y) yx_val = buf.getvalue()[len(t.sc) :] assert xy_val == yx_val
def child(kind): t = TestTerminal(stream=StringIO(), force_styling=True) t.enter_fullscreen = u"BEGIN" t.exit_fullscreen = u"END" with t.fullscreen(): pass expected_output = u"".join((t.enter_fullscreen, t.exit_fullscreen)) assert t.stream.getvalue() == expected_output
def child_with_styling(kind): t = TestTerminal(kind=kind, stream=StringIO(), force_styling=True) with t.location(3, 4): t.stream.write(u'hi') expected_output = u''.join( (unicode_cap('sc'), unicode_parm('cup', 4, 3), u'hi', unicode_cap('rc'))) assert (t.stream.getvalue() == expected_output)
def child_without_styling(): """No side effect for location as a context manager without styling.""" t = TestTerminal(stream=StringIO(), force_styling=None) with t.location(3, 4): t.stream.write(u'hi') assert t.stream.getvalue() == u'hi'
def child(kind): t = TestTerminal(stream=StringIO(), force_styling=True) t.hide_cursor = u"BEGIN" t.normal_cursor = u"END" with t.hidden_cursor(): pass expected_output = u"".join((t.hide_cursor, t.normal_cursor)) assert t.stream.getvalue() == expected_output
def child(kind): buf = StringIO() t = TestTerminal(stream=buf, force_styling=True) y, x = 10, 20 with t.location(y, x): xy_val = t.move(x, y) yx_val = buf.getvalue()[len(t.sc):] assert xy_val == yx_val
def child(kind): t = TestTerminal(stream=StringIO(), force_styling=True) t.enter_fullscreen = u'BEGIN' t.exit_fullscreen = u'END' with t.fullscreen(): pass expected_output = u''.join((t.enter_fullscreen, t.exit_fullscreen)) assert (t.stream.getvalue() == expected_output)
def child(): t = TestTerminal() try: my_wrapped = t.wrap(u'------- -------------', WIDTH) except ValueError, err: assert err.args[0] == ( "invalid width %r(%s) (must be integer > 0)" % ( WIDTH, type(WIDTH)))
def child(): term = TestTerminal(kind='xterm-256color') # given, given = term.bold_red(u'コンニチハ, セカイ!') expected = sum((2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 1,)) # exercise, assert term.length(given) == expected
def child(kind): t = TestTerminal(kind=kind, stream=StringIO(), force_styling=True) ROW = 5 with t.location(y=ROW): pass expected_output = u''.join( (unicode_cap('sc'), u'\x1b[{0}d'.format(ROW + 1), unicode_cap('rc'))) assert (t.stream.getvalue() == expected_output)
def child(): def side_effect(fd): raise IOError term = TestTerminal() term._winsize = side_effect os.environ["COLUMNS"] = "1984" os.environ["LINES"] = "1888" assert term._height_and_width() == (1888, 1984, None, None)
def child(): def side_effect(fd): raise IOError term = TestTerminal() term._winsize = side_effect os.environ['COLUMNS'] = '1984' os.environ['LINES'] = '1888' assert term._height_and_width() == (1888, 1984, None, None)
def child(lines=25, cols=80): # set the pty's virtual window size val = struct.pack('HHHH', lines, cols, 0, 0) fcntl.ioctl(sys.__stdout__.fileno(), termios.TIOCSWINSZ, val) t = TestTerminal() winsize = t._height_and_width() assert t.width == cols assert t.height == lines assert winsize.ws_col == cols assert winsize.ws_row == lines
def child_mnemonics_willmove(kind): from blessed.sequences import measure_length t = TestTerminal(kind=kind) # movements assert (len(t.move(98, 76)) == measure_length(t.move(98, 76), t)) assert (len(t.move(54)) == measure_length(t.move(54), t)) assert not t.cud1 or (len(t.cud1) == measure_length(t.cud1, t)) assert not t.cub1 or (len(t.cub1) == measure_length(t.cub1, t)) assert not t.cuf1 or (len(t.cuf1) == measure_length(t.cuf1, t)) assert not t.cuu1 or (len(t.cuu1) == measure_length(t.cuu1, t)) assert not t.cub or (len(t.cub(333)) == measure_length(t.cub(333), t)) assert not t.cuf or (len(t.cuf(333)) == measure_length(t.cuf(333), t)) assert not t.home or (len(t.home) == measure_length(t.home, t)) assert not t.restore or (len(t.restore) == measure_length(t.restore, t)) assert not t.clear or (len(t.clear) == measure_length(t.clear, t))
def child(kind): t = TestTerminal(stream=StringIO(), kind=kind) assert (t.clear == '') assert (t.move(1 == 2) == '') assert (t.move_x(1) == '') assert (t.bold() == '') assert (t.bold('', 'x', 'huh?') == '') assert (t.bold('', 9876) == '') assert (t.uhh(9876) == '') assert (t.clear('x') == 'x')
def child(): import codecs from blessed.sequences import Sequence term = TestTerminal(kind='xterm-256color') # this 'ansi' art contributed by xzip!impure for another project, # unlike most CP-437 DOS ansi art, this is actually utf-8 encoded. fname = os.path.join(os.path.dirname(__file__), 'wall.ans') lines = codecs.open(fname, 'r', 'utf-8').readlines() assert term.length(lines[0]) == 67 # ^[[64C^[[34m▄▓▄ assert term.length(lines[1]) == 75 assert term.length(lines[2]) == 78 assert term.length(lines[3]) == 78 assert term.length(lines[4]) == 78 assert term.length(lines[5]) == 78 assert term.length(lines[6]) == 77
def child(kind): t = TestTerminal(kind=kind) if any((t.bold, t.green)): expected_output = u''.join((t.bold, t.green, u'boö', t.normal)) else: expected_output = u'boö' assert t.bold_green(u'boö') == expected_output if any((t.on_bright_red, t.bold, t.bright_green, t.underline)): expected_output = u''.join( (t.on_bright_red, t.bold, t.bright_green, t.underline, u'meh', t.normal)) else: expected_output = u'meh' assert (t.on_bright_red_bold_bright_green_underline('meh') == expected_output)
def child(): # Also test that Terminal grabs a reasonable default stream. This test # assumes it will be run from a tty. t = TestTerminal() sc = unicode_cap('sc') assert t.save == sc assert t.save == sc # Make sure caching doesn't screw it up.
def child_mnemonics_willmove(kind): from blessed.sequences import measure_length t = TestTerminal(kind=kind) # movements assert (len(t.move(98, 76)) == measure_length(t.move(98, 76), t)) assert (len(t.move(54)) == measure_length(t.move(54), t)) assert not t.cud1 or (len(t.cud1) == measure_length(t.cud1, t)) assert not t.cub1 or (len(t.cub1) == measure_length(t.cub1, t)) assert not t.cuf1 or (len(t.cuf1) == measure_length(t.cuf1, t)) assert not t.cuu1 or (len(t.cuu1) == measure_length(t.cuu1, t)) assert not t.cub or (len(t.cub(333)) == measure_length(t.cub(333), t)) assert not t.cuf or (len(t.cuf(333)) == measure_length(t.cuf(333), t)) assert not t.home or (len(t.home) == measure_length(t.home, t)) assert not t.restore or (len(t.restore) == measure_length( t.restore, t)) assert not t.clear or (len(t.clear) == measure_length(t.clear, t))
def child(): warnings.filterwarnings("ignore", category=UserWarning) term = TestTerminal(kind='unknown', force_styling=True) assert term._kind is None assert term.does_styling is False assert term.number_of_colors == 0 warnings.resetwarnings()
def child(): term = TestTerminal(force_styling=True) maxlen = None for sequence, code in term._keymap.items(): if maxlen is not None: assert len(sequence) <= maxlen assert sequence maxlen = len(sequence)
def child(kind, lines=25, cols=80): # set the pty's virtual window size val = struct.pack('HHHH', lines, cols, 0, 0) fcntl.ioctl(sys.__stdout__.fileno(), termios.TIOCSWINSZ, val) # build a test paragraph, along with a very colorful version t = TestTerminal(kind=kind) pgraph = u' '.join( ('a', 'ab', 'abc', 'abcd', 'abcde', 'abcdef', 'abcdefgh', 'abcdefghi', 'abcdefghij', 'abcdefghijk', 'abcdefghijkl', 'abcdefghijklm', 'abcdefghijklmn', 'abcdefghijklmno',) * 4) pgraph_colored = u''.join([ t.color(n % 7) + t.bold + ch for n, ch in enumerate(pgraph)]) internal_wrapped = textwrap.wrap(pgraph, t.width, break_long_words=False) my_wrapped = t.wrap(pgraph) my_wrapped_colored = t.wrap(pgraph_colored) # ensure we textwrap ascii the same as python assert (internal_wrapped == my_wrapped) # ensure our first and last line wraps at its ends first_l = internal_wrapped[0] last_l = internal_wrapped[-1] my_first_l = my_wrapped_colored[0] my_last_l = my_wrapped_colored[-1] assert (len(first_l) == t.length(my_first_l)) assert (len(last_l) == t.length(my_last_l)) assert (len(internal_wrapped[-1]) == t.length(my_wrapped_colored[-1]))
def child(): # set the pty's virtual window size os.environ['COLUMNS'] = '99' os.environ['LINES'] = '11' t = TestTerminal(stream=StringIO()) save_init = t._init_descriptor save_stdout = sys.__stdout__ try: t._init_descriptor = None sys.__stdout__ = None winsize = t._height_and_width() width = t.width height = t.height finally: t._init_descriptor = save_init sys.__stdout__ = save_stdout assert winsize.ws_col == width == 99 assert winsize.ws_row == height == 11
def child(): with mock.patch('locale.getpreferredencoding') as get_enc: with warnings.catch_warnings(record=True) as warned: get_enc.return_value = '---unknown--encoding---' t = TestTerminal() assert t._encoding == 'ascii' assert len(warned) == 1 assert issubclass(warned[-1].category, UserWarning) assert "fallback to ASCII" in str(warned[-1].message)
def child(): # build a test paragraph, along with a very colorful version t = TestTerminal() pgraph = u' '.join(( 'a', 'ab', 'abc', 'abcd', 'abcde', 'abcdef', 'abcdefgh', 'abcdefghi', 'abcdefghij', 'abcdefghijk', 'abcdefghijkl', 'abcdefghijklm', 'abcdefghijklmn', 'abcdefghijklmno ', ) * 4) pgraph_colored = u''.join([ t.color(n % 7) + t.bold + ch if ch != ' ' else ' ' for n, ch in enumerate(pgraph) ]) internal_wrapped = textwrap.wrap(pgraph, width=WIDTH, break_long_words=False, drop_whitespace=True, subsequent_indent=u' ' * 3) my_wrapped = t.wrap(pgraph, width=WIDTH, drop_whitespace=True, subsequent_indent=u' ' * 3) my_wrapped_colored = t.wrap(pgraph_colored, width=WIDTH, drop_whitespace=True, subsequent_indent=u' ' * 3) # ensure we textwrap ascii the same as python assert (internal_wrapped == my_wrapped) # ensure our first and last line wraps at its ends first_l = internal_wrapped[0] last_l = internal_wrapped[-1] my_first_l = my_wrapped_colored[0] my_last_l = my_wrapped_colored[-1] assert (len(first_l) == t.length(my_first_l)) assert (len(last_l) == t.length(my_last_l)), (internal_wrapped, my_wrapped_colored) assert (len(internal_wrapped[-1]) == t.length(my_wrapped_colored[-1])) # ensure our colored textwrap is the same line length assert (len(internal_wrapped) == len(my_wrapped_colored))
def child(kind): t = TestTerminal(kind=kind, stream=StringIO(), force_styling=False) assert (t.bold(u'hi') == u'hi') assert (t.green('hi') == u'hi') # Test non-ASCII chars, no longer really necessary: assert (t.bold_green(u'boö') == u'boö') assert (t.bold_underline_green_on_red('loo') == u'loo') assert (t.on_bright_red_bold_bright_green_underline('meh') == u'meh')
def child(): warnings.filterwarnings("error", category=UserWarning) # instantiate first terminal, of type xterm-256color term = TestTerminal(force_styling=True) try: # a second instantiation raises UserWarning term = TestTerminal(kind="vt220", force_styling=True) except UserWarning: err = sys.exc_info()[1] assert (err.args[0].startswith( 'A terminal of kind "vt220" has been requested')), err.args[0] assert ('a terminal of kind "xterm-256color" will ' 'continue to be returned' in err.args[0]), err.args[0] else: # unless term is not a tty and setupterm() is not called assert not term.is_a_tty or False, 'Should have thrown exception' warnings.resetwarnings()
def test_kbhit_interrupted_nonetype_no_continue(): "kbhit() may be interrupted when _intr_continue=False with timeout None." pid, master_fd = pty.fork() if pid is 0: try: cov = __import__('cov_core_init').init() except ImportError: cov = None # child pauses, writes semaphore and begins awaiting input global got_sigwinch got_sigwinch = False def on_resize(sig, action): global got_sigwinch got_sigwinch = True term = TestTerminal() signal.signal(signal.SIGWINCH, on_resize) read_until_semaphore(sys.__stdin__.fileno(), semaphore=SEMAPHORE) os.write(sys.__stdout__.fileno(), SEMAPHORE) with term.raw(): term.inkey(timeout=None, _intr_continue=False) os.write(sys.__stdout__.fileno(), b'complete') assert got_sigwinch is True if cov is not None: cov.stop() cov.save() os._exit(0) with echo_off(master_fd): os.write(master_fd, SEND_SEMAPHORE) read_until_semaphore(master_fd) stime = time.time() time.sleep(0.05) os.kill(pid, signal.SIGWINCH) os.write(master_fd, b'X') output = read_until_eof(master_fd) pid, status = os.waitpid(pid, 0) assert output == u'complete' assert os.WEXITSTATUS(status) == 0 assert math.floor(time.time() - stime) == 0.0
def test_inkey_0s_raw_ctrl_c(): "0-second inkey with raw allows receiving ^C." pid, master_fd = pty.fork() if pid is 0: # child try: cov = __import__('cov_core_init').init() except ImportError: cov = None term = TestTerminal() read_until_semaphore(sys.__stdin__.fileno(), semaphore=SEMAPHORE) with term.raw(): os.write(sys.__stdout__.fileno(), RECV_SEMAPHORE) inp = term.inkey(timeout=0) os.write(sys.__stdout__.fileno(), inp.encode('latin1')) if cov is not None: cov.stop() cov.save() os._exit(0) with echo_off(master_fd): os.write(master_fd, SEND_SEMAPHORE) # ensure child is in raw mode before sending ^C, read_until_semaphore(master_fd) os.write(master_fd, u'\x03'.encode('latin1')) stime = time.time() output = read_until_eof(master_fd) pid, status = os.waitpid(pid, 0) if os.environ.get('TRAVIS', None) is not None: # For some reason, setraw has no effect travis-ci, # is still accepts ^C, causing system exit on py26, # but exit 0 on py27, and either way on py33 # .. strange, huh? assert output in (u'', u'\x03') assert os.WEXITSTATUS(status) in (0, 2) else: assert (output == u'\x03' or output == u'' and not os.isatty(0)) assert os.WEXITSTATUS(status) == 0 assert math.floor(time.time() - stime) == 0.0