Skip to content
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

rewrite show as writemime for Julia v0.4 #219

Merged
merged 7 commits into from
Jun 15, 2016
10 changes: 10 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ if VERSION < v"0.5.0-dev+961"
end
end

function rewrite_show(ex)
return Expr(:call, :writemime, ex.args[2], ex.args[3], ex.args[4])
end

function rewrite_dict(ex)
length(ex.args) == 1 && return ex

Expand Down Expand Up @@ -438,6 +442,8 @@ function _compat(ex::Expr)
rewrite_pairs_to_tuples!(ex)
elseif VERSION < v"0.4.0-dev+1246" && f == :String
ex = Expr(:call, :bytestring, ex.args[2:end]...)
elseif length(ex.args) == 4 && ex.args[1] === :show
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be version dependent otherwise it's calling a deprecated function on 0.5

ex = rewrite_show(ex)
end
if VERSION < v"0.5.0-dev+4305"
rewrite_iocontext!(ex)
Expand Down Expand Up @@ -516,6 +522,10 @@ function _compat(ex::Expr)
# f.(arg) -> broadcast(f, arg)
return Expr(:call, :broadcast, _compat(ex.args[1]), _compat(ex.args[2]))
end
elseif ex.head === :import
if length(ex.args) == 2 && ex.args[1] === :Base && ex.args[2] === :show
ex.args[2] = :writemime
end
end
return Expr(ex.head, map(_compat, ex.args)...)
end
Expand Down
15 changes: 15 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
using Compat
import Compat.String
@compat import Base.show
using Base.Test

type TestCustomShowType end
@compat function show(io::IO, ::MIME"text/plain", ::TestCustomShowType)
print(io, "MyTestCustomShowType")
end
myio = IOBuffer()
display(TextDisplay(myio), MIME"text/plain"(), TestCustomShowType())
@test String(myio) == "MyTestCustomShowType"

type TestCustomShowType2 end
@compat show(io::IO, ::MIME"text/plain", ::TestCustomShowType2) = print(io, "MyTestCustomShowType2")
myio = IOBuffer()
display(TextDisplay(myio), MIME"text/plain"(), TestCustomShowType2())
@test String(myio) == "MyTestCustomShowType2"

v = 1
@test_throws AssertionError @assert(v < 1)

Expand Down