Skip to content

Commit

Permalink
display something useful for text/plain output of invalid String (#18296
Browse files Browse the repository at this point in the history
)

(cherry picked from commit 2b2894c)
  • Loading branch information
stevengj authored and tkelman committed Feb 15, 2017
1 parent 8d4ef37 commit c1c2973
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit c1c2973

Please sign in to comment.