|
| 1 | +""" |
| 2 | +Test that Ctrl-C interrupts running code in the REPL. |
| 3 | +
|
| 4 | +This test is run by run-tests.py which sets up a PTY with ISIG enabled |
| 5 | +and spawns MicroPython with the PTY as its controlling terminal. |
| 6 | +
|
| 7 | +Globals provided by run-tests.py: |
| 8 | +- master: PTY master file descriptor |
| 9 | +- read_until_quiet: Function to read from PTY until no data for timeout seconds |
| 10 | +""" |
| 11 | + |
| 12 | +import os |
| 13 | +import time |
| 14 | + |
| 15 | + |
| 16 | +# Wait for banner |
| 17 | +time.sleep(0.2) |
| 18 | +banner = read_until_quiet(master) |
| 19 | + |
| 20 | +if b"MicroPython" not in banner: |
| 21 | + print("SKIP") |
| 22 | + raise SystemExit |
| 23 | + |
| 24 | +# Check if time module is available |
| 25 | +os.write(master, b"import time\n") |
| 26 | +time.sleep(0.05) |
| 27 | +check_output = read_until_quiet(master, timeout=0.05) |
| 28 | +if b"ImportError" in check_output: |
| 29 | + print("SKIP") |
| 30 | + raise SystemExit |
| 31 | + |
| 32 | +# Start a long-running operation |
| 33 | +os.write(master, b"time.sleep(30)\n") |
| 34 | +time.sleep(0.1) # Give it time to start sleeping |
| 35 | + |
| 36 | +# Send Ctrl-C byte (0x03) through the terminal |
| 37 | +# This tests that the terminal mode allows SIGINT generation |
| 38 | +os.write(master, b"\x03") |
| 39 | + |
| 40 | +# Read output |
| 41 | +time.sleep(0.2) |
| 42 | +output = read_until_quiet(master, timeout=0.1) |
| 43 | + |
| 44 | +# Check for KeyboardInterrupt |
| 45 | +if b"KeyboardInterrupt" in output: |
| 46 | + print("PASS: Ctrl-C interrupted execution") |
| 47 | +else: |
| 48 | + print("FAIL: No KeyboardInterrupt found") |
| 49 | + |
| 50 | +# Verify REPL still works |
| 51 | +os.write(master, b"print('OK')\n") |
| 52 | +time.sleep(0.1) |
| 53 | +output = read_until_quiet(master) |
| 54 | + |
| 55 | +if b"OK" in output: |
| 56 | + print("PASS: REPL functional after interrupt") |
| 57 | +else: |
| 58 | + print("FAIL: REPL not functional") |
0 commit comments