Pyrepl.multiline_input usability in my own code

I love the new repl features in Python 3.13.

I’d like to use the pyrepl multiline_input routine in my own code. I appear to have two choices:

  • Use 3.13’s _pyrepl module. However, as it is underscored and undocumented, it does not appear to be part of Python’s public API
  • Use the pypi pyrepl package. However, this project warns when imported that it is deprecated (under python 3.13 and later).

What is a person to do? It would be nice if this routine was a public part of Python; is this a suitable idea for Ideas?

Here’s a simple example of using multiline_input. It uses a little routine to calculate whether brackets in the input are balanced. Whenever they are not balanced, the input is considered incomplete. It’s quite simple to get this much working, and really provides a great interactive experience.

import sys
from _pyrepl.readline import multiline_input

def count_brackets(s):
    return s.count("[") - s.count("]")

def more_lines(s):
    return count_brackets(s) > 0

ps1 = getattr(sys, "ps1", "pydc> ")
ps2 = getattr(sys, "ps1", "  ... ")

while True:
    try:
        statement = multiline_input(more_lines, ps1, ps2)
    except EOFError:
        break
    print(repr(statement))

I really like it too!

But I suspect making this library public is a question of justifying the effort to maintain another public API and surface of guaranteed behaviours.

I think the most productive way forward long term, is to create a third party library on PyPi for, to demonstrate demand. All users are free to fork code in the stdlib, under the normal PSF license.

Make it easy for them to show they too are also out there, and they want this too.

It feels like it would be a weird situation to re-fork the python _pyrepl, which used to be a distinct github and pypi project, which designated itself deprecated when python took a vendored copy and made it a private API…

Use _pyrepl under the same condition that one uses pyrepl, at your own risk that apis change.

The latest version of pyrepl, a year ago, is 0.11.4, so it had no stability guarantee either. The release years for available older pyrepl versions were 2019, 2012, 2010, and 2007, so it was not very actively maintained.

The previous C-coded REPL had no python interface, so being able to import _pyrepl in an improvement. It has seen much more active maintainance. But its purpose is to implement REPL in a more maintainable form.

The public CLI module is code, which uses codeop to compile, for a Python CLI. It might be usable, but changing the criterion for more lines would be harder, requiring subclassing and overriding. I would just use the imported readline function.

The problem with exposing just multiline_input() is that _pyrepl.readline implements a subset of the unix readline module + multiline_input on top of _pyrepl. The function itself is a method of a class instance and not directly separable from everything else: _ReadlineWrapper().multiline_input. The method depends on the _pyrepl Console and reader.

I’ve documented what I’ve learned on my blog: Python multiline input: Using _pyrepl in your own code

image

The blog shows how to do multiline input with your own “more_lines” function in 3.13+, and how to do custom colorization in 3.14+, tested through 3.16.

I still think this would be a fantastic facility to expose in a structured way, but I appreciate that doing so might be a burden to core developers.