Skip to content

Difference in CTRL+D Behavior Between code.interact and Python REPL #136730

@adqm

Description

@adqm

Bug report

Bug description:

I'm not sure whether I would call this a bug per se, but I just noticed that code.interact() has different behavior from both the old and the new REPL when working with multiline input. When in multiline mode and hitting CTRL+D on a new, blank line, e.g.:

>>> def foo(x):
... [ctrl+d]
  • The old REPL (tested in Python 3.11) seems to run the input up to the EOF, leaving us in the REPL:

    Python 3.11.2 (main, Apr 28 2025, 14:11:48) [GCC 12.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def foo(x):
    ... 
      File "<stdin>", line 1
        def foo(x):
                   ^
    IndentationError: expected an indented sensibleblock after function definition on line 1
    
  • In new REPL (testing in Python 3.13), it seems to be a NOP (we can still edit from here):

    Python 3.13.1 (main, Feb  2 2025, 23:16:12) [GCC 12.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def foo(x):
    ... 
    
  • In code.interact(), though, it exits the REPL entirely, without running the code:

    >>> code.interact()
    Python 3.13.1 (main, Feb  2 2025, 23:16:12) [GCC 12.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> def foo(x):
    ... 
    
    now exiting InteractiveConsole...
    

I could see arguments for any of those three behaviors being the "right" one, but regardless of what the right behavior is, it feels like code.interact and the Python REPL should have the same behavior here.

I'd be happy to have a go at a PR for this but am unsure what the most reasonable change is (or if this is even worth changing).

CPython versions tested on:

3.11, 3.13

Operating systems tested on:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-replRelated to the interactive shelltype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions