- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
REPL shouldn't auto-show
giant outputs
#40735
Comments
Sebastian pointed me to https://github.com/JuliaDebug/Debugger.jl/blob/master/src/limitio.jl and https://github.com/JuliaDebug/Debugger.jl/blob/master/src/printing.jl#L2-L20 and that it should probably be part of Using just the diff --git a/stdlib/REPL/src/REPL.jl b/stdlib/REPL/src/REPL.jl
index 0c5a6c6267..3aa3eee2b0 100644
--- a/stdlib/REPL/src/REPL.jl
+++ b/stdlib/REPL/src/REPL.jl
@@ -239,6 +239,20 @@ function repl_backend_loop(backend::REPLBackend)
return nothing
end
+mutable struct LimitIO{IO_t <: IO} <: IO
+ io::IO_t
+ maxbytes::Int
+ n::Int # max bytes to write
+end
+LimitIO(io::IO, maxbytes) = LimitIO(io, maxbytes, 0)
+
+struct LimitIOException <: Exception end
+
+function Base.write(io::LimitIO, v::UInt8)
+ io.n > io.maxbytes && throw(LimitIOException())
+ io.n += write(io.io, v)
+end
+
struct REPLDisplay{R<:AbstractREPL} <: AbstractDisplay
repl::R
end
@@ -254,7 +268,7 @@ function display(d::REPLDisplay, mime::MIME"text/plain", x)
# this can override the :limit property set initially
io = foldl(IOContext, d.repl.options.iocontext, init=io)
end
- show(io, mime, x[])
+ show(LimitIO(io, 100_000), mime, x[])
println(io)
end
return nothing I get what seems like nice behavior to me: it will print the first 100k bytes of data from |
show
giant outputs
We already have a For example, the |
Agreed with that! However, I think it's a worse bug if an invalid show method can hang/crash your Julia session. Usually in Julia if you aren't doing something unsafe and aren't doing some terrible piracy, then if you define a bad method you'll just get an exception. But here you can make the mistake of So I think the problematic methods should be fixed by respecting the limit in whatever way is appropriate for the thing being shown, but also the REPL itself should refuse to show something that's way too big. |
Cthulhu has a width-limiting IO object that I've long wondered about making more widely available. Limiting by size of output is a feature that arguably belongs more to the IO object than the object being displayed, although of course you can do "smarter" things (e.g., informative summaries) by dispatching on the object itself. |
Debugger has https://github.com/JuliaDebug/Debugger.jl/blob/master/src/limitio.jl which has the same purpose but is much more rudimentary. |
closes #40735 --------- Co-authored-by: Jameson Nash <[email protected]>
copied from Slack:
I think it should error ("Output too large to display in REPL") or something instead of hanging or trying to print something giant to the REPL.
@pfitzseb mentioned
edit: changed the title to "auto-
show
" instead ofprint
. In my mind, the issue is that when you unexpectedly dump a huge amount of text to the REPL. If you're typing outprint(...)
then it's probably fine (there's at least some opting-in) but when it's an automatic display, then it's worse if it behaves badly or unexpectedly.The text was updated successfully, but these errors were encountered: