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    def test_while_evaluating_code(self):
28        """ Tests SIGINT handling while Python code is being evaluated."""
29        self.start_python_repl()
30
31        # Start a long-running command that we try to abort with SIGINT.
32        # Note that we dont actually wait 10000s in this code as pexpect or
33        # lit will kill the test way before that.
34        self.child.send("import time; print('running' + 'now'); time.sleep(10000);\n")
35
36        # Make sure the command is actually being evaluated at the moment by
37        # looking at the string that the command is printing.
38        # Don't check for a needle that also occurs in the program itself to
39        # prevent that echoing will make this check pass unintentionally.
40        self.child.expect("runningnow")
41
42        # Send SIGINT to the LLDB process.
43        self.child.sendintr()
44
45        # This should get transformed to a KeyboardInterrupt which is the same
46        # behaviour as the standalone Python REPL. It should also interrupt
47        # the evaluation of our sleep statement.
48        self.child.expect("KeyboardInterrupt")
49        # Send EOF to quit the Python REPL.
50        self.child.sendeof()
51
52        self.quit()
53
54    # PExpect uses many timeouts internally and doesn't play well
55    # under ASAN on a loaded machine..
56    @skipIfAsan
57    # FIXME: On Linux the Python code that reads from stdin seems to block until
58    # it has finished reading a line before handling any queued signals.
59    @skipIfLinux
60    def test_while_waiting_on_input(self):
61        """ Tests SIGINT handling while the REPL is waiting on input from
62        stdin."""
63        self.start_python_repl()
64
65        # Send SIGINT to the LLDB process.
66        self.child.sendintr()
67        # This should get transformed to a KeyboardInterrupt which is the same
68        # behaviour as the standalone Python REPL.
69        self.child.expect("KeyboardInterrupt")
70        # Send EOF to quit the Python REPL.
71        self.child.sendeof()
72
73        self.quit()
74