Skip to content

GH-145378: Use PyREPL as the default input console for pdb#145379

Merged
gaogaotiantian merged 9 commits intopython:mainfrom
gaogaotiantian:pdb-pyrepl
Apr 30, 2026
Merged

GH-145378: Use PyREPL as the default input console for pdb#145379
gaogaotiantian merged 9 commits intopython:mainfrom
gaogaotiantian:pdb-pyrepl

Conversation

@gaogaotiantian
Copy link
Copy Markdown
Member

@gaogaotiantian gaogaotiantian commented Mar 1, 2026

@gaogaotiantian
Copy link
Copy Markdown
Member Author

@pablogsal and @ambv as the original author for the new interactive shell. Could you take a look when you have some time, thanks!

@johnslavik johnslavik self-requested a review March 2, 2026 19:54
Comment thread Lib/pdb.py
@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented Apr 29, 2026

Documentation build overview

📚 cpython-previews | 🛠️ Build #32477993 | 📁 Comparing b04b192 against main (d71e3bc)

  🔍 Preview build  

4 files changed
± contents.html
± download.html
± whatsnew/3.15.html
± whatsnew/changelog.html

@corona10 corona10 self-assigned this Apr 29, 2026
Comment thread Lib/pdb.py Outdated
stripped = len(origline) - len(line)
begidx = self.readline_wrapper.get_begidx() - stripped
endidx = self.readline_wrapper.get_endidx() - stripped
if begidx>0:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if begidx>0:
if begidx > 0:

Comment thread Lib/pdb.py Outdated
self.message("*exit from pdb interact command*")
else:
with self._enable_rlcompleter(ns):
console = _PdbInteractiveConsole(ns, message=self.message)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not redundant declaration from L2486?

Comment thread Lib/pdb.py Outdated
console = _PdbInteractiveConsole(ns, message=self.message)
if self.pyrepl_input is not None:
from _pyrepl.simple_interact import run_multiline_interactive_console
self.message("*pdb interact start*")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Please declare as common variables

banner = "*pdb interact start*"
exitmsg = "*exit from pdb interact command*"

Comment thread Lib/pdb.py Outdated
Comment thread Lib/pdb.py
return [e for e in self.displaying.get(self.curframe, {})
if e.startswith(text)]

def do_interact(self, arg):
Copy link
Copy Markdown
Member

@corona10 corona10 Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def do_interact(self, arg):
    """..."""
    ns = {**self.curframe.f_globals, **self.curframe.f_locals}
    console = _PdbInteractiveConsole(ns, message=self.message)
    banner = "*pdb interact start*"
    exitmsg = "*exit from pdb interact command*"
    if self.pyrepl_input is not None:
        console.run_interact(banner, exitmsg, use_pyrepl=True)
    else:
        with self._enable_rlcompleter(ns):
            console.run_interact(banner, exitmsg, use_pyrepl=False)

Comment thread Lib/pdb.py Outdated
Comment on lines +357 to +361
if not os.getenv("PYTHON_BASIC_REPL"):
from _pyrepl.main import CAN_USE_PYREPL

return CAN_USE_PYREPL
return False
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if not os.getenv("PYTHON_BASIC_REPL"):
from _pyrepl.main import CAN_USE_PYREPL
return CAN_USE_PYREPL
return False
if os.getenv('PYTHON_BASIC_REPL'):
CAN_USE_PYREPL = False
else:
try:
from _pyrepl.main import CAN_USE_PYREPL
except ModuleNotFoundError:
CAN_USE_PYREPL = False
return CAN_USE_PYREPL

Comment thread Lib/pdb.py Outdated

self.pdb_instance = pdb_instance
self.prompt = prompt
self.console = code.InteractiveConsole()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this should be something like this:

class _PdbInteractiveConsole(code.InteractiveConsole):
    def __init__(self, ns=None, message=None):
        self._message = message
        super().__init__(locals=ns, local_exit=True)

    def write(self, data):
        if self._message is not None:
            self._message(data, end='')
        else:
            super().write(data)

    def _more_lines(self, text):
        # Generic Python multi-line completeness heuristic.
        # Strips pyrepl's trailing auto-indent before compiling.
        src = text.rstrip(" \t")
        n = len(src)
        if n > 0 and text[n-1] == '\n':
            text = src
        try:
            code_obj = self.compile(text, "<stdin>", "single")
        except (OverflowError, SyntaxError, ValueError):
            lines = text.splitlines(keepends=True)
            if len(lines) == 1:
                return False
            last = lines[-1]
            return ((last.startswith((" ", "\t")) or last.strip() != "")
                    and not last.endswith("\n"))
        return code_obj is None

Comment thread Lib/pdb.py Outdated

self.pdb_instance = pdb_instance
self.prompt = prompt
self.console = code.InteractiveConsole()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.console = code.InteractiveConsole()
self.console = _PdbInteractiveConsole()

Comment thread Lib/pdb.py Outdated
Copy link
Copy Markdown
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments to avoid making this fragile against future _pyrepl changes.
Let’s depend only on APIs that asyncio already uses.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented Apr 29, 2026

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@gaogaotiantian
Copy link
Copy Markdown
Member Author

@corona10 I addressed most of the comments. There are a few things left to discuss:

  1. for do_interact, we have to use run_multiline_interactive_console anyway. If you want a clearer entry I can put this logic into _PdbInteractiveConsole but we won't be able to get rid of the dependency of _pyrepl.
  2. I added the history for pdb. I think one of the pain point users have is that pdb lost all the history when it restarts. For now pdb shares the same history file with python interpreter - I don't think that's a big deal right? It kind of makes sense. We can of course use a separate file, but the existing file is controlled by PYTHON_HISTORY env var and we support it natively if we just use the same history file.

@gaogaotiantian gaogaotiantian requested a review from corona10 April 30, 2026 02:29
Copy link
Copy Markdown
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for do_interact, we have to use run_multiline_interactive_console anyway. If you want a clearer entry I can put this logic into _PdbInteractiveConsole but we won't be able to get rid of the dependency of _pyrepl.

I think that it would be fine. asyncio already depends on it.

run_multiline_interactive_console,

I added the history for pdb.

Could we separate the PR for this? I think that it would be another feature.
I would like to see just pdb using pyrepl feature before the beta branch cut off, but this new feature would make another noise.

@gaogaotiantian
Copy link
Copy Markdown
Member Author

Could we separate the PR for this? I think that it would be another feature.

Yeah sure. I was kind of hoping this could make it way it because that's one of the reasons that I wanted to use pyrepl for pdb - the inherited history. However, if you think it's a bit risky to onboard both, we can do only pyrepl for 3.15.

Copy link
Copy Markdown
Member

@corona10 corona10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

Let's fix upcoming bug during the beta period
Thanks you @gaogaotiantian

@gaogaotiantian gaogaotiantian merged commit 234c12c into python:main Apr 30, 2026
96 of 99 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants