xref: /llvm-project/lldb/test/API/iohandler/sigint/TestIOHandlerPythonREPLSigint.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
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
12
13class TestCase(PExpectTest):
14    def start_python_repl(self):
15        """Starts up the embedded Python REPL."""
16        self.launch()
17        # Start the embedded Python REPL via the 'script' command.
18        self.child.send("script -l python --\n")
19        # Wait for the Python REPL prompt.
20        self.child.expect(">>>")
21
22    # PExpect uses many timeouts internally and doesn't play well
23    # under ASAN on a loaded machine..
24    @skipIfAsan
25    @skipIfWindows
26    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])
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    @skipIf(hostoslist=["linux"])
60    @skipIfWindows
61    def test_while_waiting_on_input(self):
62        """Tests SIGINT handling while the REPL is waiting on input from
63        stdin."""
64        self.start_python_repl()
65
66        # Send SIGINT to the LLDB process.
67        self.child.sendintr()
68        # This should get transformed to a KeyboardInterrupt which is the same
69        # behaviour as the standalone Python REPL.
70        self.child.expect("KeyboardInterrupt")
71        # Send EOF to quit the Python REPL.
72        self.child.sendeof()
73
74        self.quit()
75