Skip to content

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

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

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

Conversation

@gaogaotiantian

@gaogaotiantiangaogaotiantian commented Mar 1, 2026

Copy link
Copy Markdown
Member

@gaogaotiantian

Copy link
Copy Markdown
MemberAuthor

@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 threadLib/pdb.py
@read-the-docs-community

read-the-docs-communityBot commented Apr 29, 2026

Copy link
Copy Markdown

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

@corona10corona10 self-assigned this Apr 29, 2026
Comment threadLib/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
ifbegidx>0:
ifbegidx>0:

Comment threadLib/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 threadLib/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 threadLib/pdb.py Outdated
Comment threadLib/pdb.py
return [e for e in self.displaying.get(self.curframe, {})
if e.startswith(text)]

def do_interact(self, arg):

@corona10corona10Apr 29, 2026

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.

defdo_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*"ifself.pyrepl_inputisnotNone:
console.run_interact(banner, exitmsg, use_pyrepl=True)
else:
withself._enable_rlcompleter(ns):
console.run_interact(banner, exitmsg, use_pyrepl=False)

Comment threadLib/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
ifnotos.getenv("PYTHON_BASIC_REPL"):
from_pyrepl.mainimportCAN_USE_PYREPL
returnCAN_USE_PYREPL
returnFalse
ifos.getenv('PYTHON_BASIC_REPL'):
CAN_USE_PYREPL=False
else:
try:
from_pyrepl.mainimportCAN_USE_PYREPL
exceptModuleNotFoundError:
CAN_USE_PYREPL=False
returnCAN_USE_PYREPL

Comment threadLib/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=messagesuper().__init__(locals=ns, local_exit=True)
defwrite(self, data):
ifself._messageisnotNone:
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)
ifn>0andtext[n-1] =='\n':
text=srctry:
code_obj=self.compile(text, "<stdin>", "single")
except (OverflowError, SyntaxError, ValueError):
lines=text.splitlines(keepends=True)
iflen(lines) ==1:
returnFalselast=lines[-1]
return ((last.startswith((" ", "\t")) orlast.strip() !="")
andnotlast.endswith("\n"))
returncode_objisNone

Comment threadLib/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 threadLib/pdb.py Outdated

@corona10corona10 left a comment

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.

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

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
MemberAuthor

@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.

@corona10corona10 left a comment

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.

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
MemberAuthor

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.

@corona10corona10 left a comment

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.

lgtm!

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

@gaogaotiantian
gaogaotiantian merged commit 234c12c into python:mainApr 30, 2026
96 of 99 checks passed
Sign up for freeto 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

@gaogaotiantian@corona10@tanloong