1"""
2Test sending SIGINT to the embedded Python REPL.
3"""
4
5import os
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test.lldbpexpect import PExpectTest
11
12class TestCase(PExpectTest):
13
14    mydir = TestBase.compute_mydir(__file__)
15
16    def start_python_repl(self):
17        """ Starts up the embedded Python REPL."""
18        self.launch()
19        # Start the embedded Python REPL via the 'script' command.
20        self.child.send("script -l python --\n")
21        # Wait for the Python REPL prompt.
22        self.child.expect(">>>")
23
24    # PExpect uses many timeouts internally and doesn't play well
25    # under ASAN on a loaded machine..
26    @skipIfAsan
27    @skipIfWindows
28    def test_while_evaluating_code(self):
29        """ Tests SIGINT handling while Python code is being evaluated."""
30        self.start_python_repl()
31
32        # Start a long-running command that we try to abort with SIGINT.
33        # Note that we dont actually wait 10000s in this code as pexpect or
34        # lit will kill the test way before that.
35        self.child.send("import time; print('running' + 'now'); time.sleep(10000);\n")
36
37        # Make sure the command is actually being evaluated at the moment by
38        # looking at the string that the command is printing.
39        # Don't check for a needle that also occurs in the program itself to
40        # prevent that echoing will make this check pass unintentionally.
41        self.child.expect("runningnow")
42
43        # Send SIGINT to the LLDB process.
44        self.child.sendintr()
45
46        # This should get transformed to a KeyboardInterrupt which is the same
47        # behaviour as the standalone Python REPL. It should also interrupt
48        # the evaluation of our sleep statement.
49        self.child.expect("KeyboardInterrupt")
50        # Send EOF to quit the Python REPL.
51        self.child.sendeof()
52
53        self.quit()
54
55    # PExpect uses many timeouts internally and doesn't play well
56    # under ASAN on a loaded machine..
57    @skipIfAsan
58    # FIXME: On Linux the Python code that reads from stdin seems to block until
59    # it has finished reading a line before handling any queued signals.
60    @skipIfLinux
61    @skipIfWindows
62    def test_while_waiting_on_input(self):
63        """ Tests SIGINT handling while the REPL is waiting on input from
64        stdin."""
65        self.start_python_repl()
66
67        # Send SIGINT to the LLDB process.
68        self.child.sendintr()
69        # This should get transformed to a KeyboardInterrupt which is the same
70        # behaviour as the standalone Python REPL.
71        self.child.expect("KeyboardInterrupt")
72        # Send EOF to quit the Python REPL.
73        self.child.sendeof()
74
75        self.quit()
76