Skip to content
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

Python 3.13 REPL support #447

Merged
merged 2 commits into from
Oct 18, 2024
Merged

Python 3.13 REPL support #447

merged 2 commits into from
Oct 18, 2024

Conversation

brenns10
Copy link
Contributor

This implements #446, it's essentially the same workaround (using the internal APIs of _pyrepl), but I needed to make some further tweaks to get it working.

  1. I stuck all of the import madness into another internal module so it only needs to be included once, not both in cli.py and rlcompleter.py.
  2. I had to import readline from the _pyrepl package. It's honestly quite nice that this is a drop-in enough replacement. The history file works as expected without any tweaks.
  3. Unfortunately, there's a _setup() function that resets the completer, regardless of what you have set previously. So I had to add a call to that so that our set_completer() call would stick.
  4. Mypy is very unhappy with the use of this internal API, so we've got some type: ignore added to the imports.
  5. I added an escape hatch so that we can revert to the old behavior in case some internals change in a Python release.

Incidentally, I did this by factoring out a simple interact() API that can be implemented via code or _pyrepl, and that makes it trivial to substitute in the prompt_toolkit implementation from contrib.ptdrgn. So I took the opportunity to simplify that script in a follow-up commit.

I'm not entirely sure that this is good overall. It does work on my build of Python 3.13, and it is genuinely nice to use. It's not like Python is making frequent releases, but it does seem totally possible for these internals to change, and that would be a pain to handle.

Copy link
Owner

@osandov osandov left a comment

Choose a reason for hiding this comment

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

This isn't too bad. Is there a CPython issue asking for a public API yet? Ideally we should at least ask before merging this and link to the issue in a comment in the code.

drgn/internal/repl.py Outdated Show resolved Hide resolved
drgn/internal/repl.py Outdated Show resolved Hide resolved
drgn/internal/repl.py Outdated Show resolved Hide resolved
@brenns10
Copy link
Contributor Author

I'm searching through the Github issues on cpython now, will link a few relevant issues into the #446 issue for tracking.

The new drgn.internal.repl module exports a convenient interact()
method, which accepts a banner and a namespace. So we can now drop-in
the prompt_toolkit embed function as an implementation of interact(),
rather than copying the run_interactive function and modifying it.

This does mean that the readline configuration steps from
run_interactive(), which had been stripped from this version, will be
run. But honestly, there's not much harm in configuring readline,
because prompt_toolkit won't use it anyway. It's much nicer to re-use
the existing run_interactive() function so that we don't need to modify
this script as it changes.

Signed-off-by: Stephen Brennan <[email protected]>
Copy link
Owner

@osandov osandov left a comment

Choose a reason for hiding this comment

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

Thanks, I think this is reasonably protected against the internals changing, and hopefully 3.14 adds a public API so we won't have to worry long about breakages. We can always chicken out and revert it before cutting the next release, too.

@osandov osandov merged commit 02d4b32 into osandov:main Oct 18, 2024
39 checks passed
@brenns10 brenns10 deleted the repl branch October 18, 2024 22:00
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.

2 participants