diff --git a/base/replutil.jl b/base/replutil.jl index fbd14f79b4608..6a5002a4efaba 100644 --- a/base/replutil.jl +++ b/base/replutil.jl @@ -153,6 +153,16 @@ end show(io::IO, ::MIME"text/plain", X::AbstractArray) = showarray(io, X, false) show(io::IO, ::MIME"text/plain", r::Range) = show(io, r) # always use the compact form for printing ranges +# display something useful even for strings containing arbitrary +# (non-UTF8) binary data: +function show(io::IO, ::MIME"text/plain", s::String) + if isvalid(s) + show(io, s) + else + println(io, sizeof(s), "-byte String of invalid UTF-8 data:") + showarray(io, s.data, false; header=false) + end +end # showing exception objects as descriptive error messages diff --git a/test/show.jl b/test/show.jl index 50bd7258d960e..0ad5ba6e6c2c2 100644 --- a/test/show.jl +++ b/test/show.jl @@ -572,3 +572,6 @@ end @test repr(:(f.(X,Y))) == ":(f.(X,Y))" @test repr(:(f.(X))) == ":(f.(X))" @test repr(:(f.())) == ":(f.())" + +# Test that REPL/mime display of invalid UTF-8 data doesn't throw an exception: +@test isa(stringmime("text/plain", String(UInt8[0x00:0xff;])), String)