[Python-ideas] Have REPL print less by default

Franklin? Lee leewangzhong+python at gmail.com
Sun Apr 17 23:03:21 EDT 2016

On Apr 17, 2016 9:43 PM, "Ben Finney" <ben+python at benfinney.id.au> wrote:
> Perhaps you want a different “print the interactive-REPL-safe text
> representation of this object” function, and to have the interactive
> REPL use that new function to represent objects.

You mean that the REPL should call `repr_limited(...)` instead of
`repr(...)`, and not that class makers should implement `__repr_limited__`,
right? I think one could make a `pprint` class for that.

Thing is, I would also want the following to have limited output.

    >>> def dumb(n):
    ...        for i in range(n):
    ...                print(i)
    >>> dumb(10**10)

That could print the first 50 or so lines, then the REPL buffers the rest
and prints out the message about suppressed output.

On the other hand, for a long-running computation with logging statements,
I don't wanna start it, come back, and lose all that would have been
printed, just because I forgot that the REPL does that now. Storing it all
could be a use of memory that the computation might not be able to afford.

Possible solution:
1. After output starts getting buffered, print out warnings every X seconds
about Y lines being buffered.
2. Detect screen limit, and only store that many lines back. You don't lose
anything that wasn't already going to be lost. (Optional, but possibly
useful: Also warn about Z lines being lost due to screen limit.)

It was going to be in the terminal's memory, anyway, so just store it (as
efficiently as possible) in Python's memory. I think this only uses up to
twice as much total system memory (actual screen mem + dump). Since you
won't use the output as Python at least until the user comes back, it could
be lazy about converting to `str`, and maybe even compress the buffer on
the fly (if queue compression is cheaper than printing).

It gets trickier when you want user input during the eval part of the REPL.
3. Allow the user to interrupt the eval to print the last page of output,
or dump the entire current output, and then Python will continue the eval.
(Include in the warning, "Press Ctrl+?? to show <thing>.")
4. If the eval is waiting for user input, show the last page. (I don't know
how the user could ask for more pages without sending something that the
eval is allowed to interrupt as input. I don't understand terminal keyboard
control that much.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160417/e07b51a1/attachment.html>

More information about the Python-ideas mailing list