Skip to content

KeyboardInterrupt about test for REPL was directly captured by the test program #128676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Natural-selection1 opened this issue Jan 9, 2025 · 3 comments
Labels
topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@Natural-selection1
Copy link

Natural-selection1 commented Jan 9, 2025

Bug report

Bug description:

When I was writing test for PR #128467

# under class TestPyReplCompleter(line ≈ 806) Lib/test/test_pyrepl/test_pyrepl.py
    def test_completion_menu_cleared_after_KeyboardInterrupt(self):
        events = itertools.chain(
            code_to_events("int."),
            [
                Event(evt="key", data="\t", raw=bytearray(b"\t")),
                Event(evt="key", data="\t", raw=bytearray(b"\t")),
                Event(evt="key", data="\x03", raw=bytearray(b"\x03")),  # Ctrl+C
            ],
        )

        namespace = {}
        reader = self.prepare_reader(events, namespace)
        output = multiline_input(reader, namespace)

        self.assertEqual(clean_screen(reader.screen), "int.") # asser condition isn't good now

I found the KeyboardInterrupt caused by Event(evt="key", data="\x03", raw=bytearray(b"\x03")) will be directly captured by the test program itself

# result of ".\python.bat -m test test_pyrepl.test_pyrepl -v"
== Tests result: INTERRUPTED ==

1 test omitted:
    test_pyrepl.test_pyrepl

Test suite interrupted by signal SIGINT.

Total duration: 1.3 sec
Total tests: run=0
Total test files: run=0/1
Result: INTERRUPTED

So I added a traceback in the test function

    def test_completion_menu_cleared_after_KeyboardInterrupt(self):
        events = itertools.chain(
            code_to_events("int."),
            [
                Event(evt="key", data="\t", raw=bytearray(b"\t")),
                Event(evt="key", data="\t", raw=bytearray(b"\t")),
                Event(evt="key", data="\x03", raw=bytearray(b"\x03")),  # Ctrl+C
            ],
        )

        namespace = {}
        reader = self.prepare_reader(events, namespace)

        try:
            output = multiline_input(reader, namespace)
        except KeyboardInterrupt:
            traceback.print_exc()

        self.assertEqual(clean_screen(reader.screen), "int.")

I also added traceback.print_exc() in Lib/_pyrepl/simple_interact.py(line ≈ 164)(the user-facing REPL)

        except KeyboardInterrupt:
            traceback.print_exc() # here
            r = _get_reader()
            if r.input_trans is r.isearch_trans:
                r.do_cmd(("isearch-end", [""]))
            if r.cmpltn_menu_choices:
                r.cmpltn_reset()
            r.pos = len(r.get_unicode())
            r.dirty = True
            r.refresh()
            r.in_bracketed_paste = False
            console.write("\nKeyboardInterrupt\n")
            console.resetbuffer()

recompile and run

Python 3.14.0a3+ (heads/clean_suggestion-dirty:9801a103467, Jan  9 2025, 14:45:54) [MSC v.1942 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> Traceback (most recent call last):
  File "E:\0000__Python_Project\00__cpython\Lib\_pyrepl\simple_interact.py", line 151, in run_multiline_interactive_console
    statement = multiline_input(more_lines, ps1, ps2)
  File "E:\0000__Python_Project\00__cpython\Lib\_pyrepl\readline.py", line 389, in multiline_input
    return reader.readline()
           ~~~~~~~~~~~~~~~^^
  File "E:\0000__Python_Project\00__cpython\Lib\_pyrepl\reader.py", line 801, in readline
    self.handle1()
    ~~~~~~~~~~~~^^
  File "E:\0000__Python_Project\00__cpython\Lib\_pyrepl\reader.py", line 756, in handle1
    self.console.wait(100)
    ~~~~~~~~~~~~~~~~~^^^^^
  File "E:\0000__Python_Project\00__cpython\Lib\_pyrepl\windows_console.py", line 486, in wait
    time.sleep(0.01)
    ~~~~~~~~~~^^^^^^
KeyboardInterrupt

KeyboardInterrupt
>>>

At the same time, the related error in the test is

File "E:\0000__Python_Project\00__cpython\Lib\test\test_pyrepl\test_pyrepl.py", line 864, in test_completion_menu_cleared_after_KeyboardInterrupt
    output = multiline_input(reader, namespace)
  File "E:\0000__Python_Project\00__cpython\Lib\test\test_pyrepl\support.py", line 18, in multiline_input
    return reader.readline()
           ~~~~~~~~~~~~~~~^^
  File "E:\0000__Python_Project\00__cpython\Lib\_pyrepl\reader.py", line 801, in readline
    self.handle1()
    ~~~~~~~~~~~~^^
  File "E:\0000__Python_Project\00__cpython\Lib\_pyrepl\reader.py", line 784, in handle1
    self.do_cmd(cmd)
    ~~~~~~~~~~~^^^^^
  File "E:\0000__Python_Project\00__cpython\Lib\_pyrepl\reader.py", line 709, in do_cmd
    command.do()
    ~~~~~~~~~~^^
  File "E:\0000__Python_Project\00__cpython\Lib\_pyrepl\commands.py", line 227, in do
    raise KeyboardInterrupt
KeyboardInterrupt
FAIL

# something...

======================================================================
FAIL: test_completion_menu_cleared_after_KeyboardInterrupt (test.test_pyrepl.test_pyrepl.TestPyReplCompleter.test_completion_menu_cleared_after_KeyboardInterrupt)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "E:\0000__Python_Project\00__cpython\Lib\test\test_pyrepl\test_pyrepl.py", line 868, in test_completion_menu_cleared_after_KeyboardInterrupt
    self.assertEqual(clean_screen(reader.screen), "int.")
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 'int.as_integer_ratio(  int.denominator       [259 chars]int.' != 'int.'
+ int.
- int.as_integer_ratio(  int.denominator        int.mro()
- int.bit_count(         int.from_bytes(        int.numerator
- int.bit_length(        int.imag               int.real
- int.conjugate(         int.is_integer(        int.to_bytes(
- >>>int.


----------------------------------------------------------------------

So the KeyboardInterrupt in test did not behave correctly as if the user pressed Ctrl+C, but was directly captured by the test program

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

@Natural-selection1 Natural-selection1 added the type-bug An unexpected behavior, bug, or error label Jan 9, 2025
@ZeroIntensity ZeroIntensity added the topic-repl Related to the interactive shell label Jan 10, 2025
@Natural-selection1
Copy link
Author

@cfbolz Hi there, I've seen you in several REPL-related issues both in CPython and PyPy repositories. You must be an expert in this area. I wonder if you have any insights on this current issue?

@ambv
Copy link
Contributor

ambv commented Mar 20, 2025

This is being fixed by #131509.

@ambv ambv closed this as completed Mar 21, 2025
@Natural-selection1
Copy link
Author

This is being fixed by #131509

@ambv Hello, since hearing the good news that this issue has been fixed, I've been trying to write tests again for my first CPython PR #128467. However, after several days of attempts, I still haven't been able to do it. It seems a bit too difficult for me to handle the KeyboardInterrupt, and you appear to be an expert in this area. I was wondering if I could get some help and guidance from you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants