Skip to content

rewrite show as writemime for Julia v0.4 #219

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

Merged
merged 7 commits into from
Jun 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ Currently, the `@compat` macro supports the following syntaxes:

* `@compat withenv(f, "a" => a, "b" => b...)` on 0.3.

* `@compat import Base.show => Base.writemime` because writemime is deprecated in Julia 0.5 ([#16563]).

* `@compat function show(args...) => writemime` because writemime is deprecated in Julia 0.5 ([#16563]).

## Type Aliases

* `String` has undergone multiple changes: in Julia 0.3 it was an abstract type and then got renamed to `AbstractString` in 0.4; in 0.5, `ASCIIString` and `ByteString` were deprecated, and `UTF8String` was renamed to the (now concrete) type `String`.
Expand Down
14 changes: 14 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,14 @@ if VERSION < v"0.5.0-dev+961"
end
end

function rewrite_show(ex)
argtypes = ex.args[2:end]
if length(argtypes) == 2
insert!(argtypes, 2, Expr(:(::), :macrocall, MIME"text/plain"))
end
return Expr(:call, :writemime, argtypes...)
end

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

Expand Down Expand Up @@ -438,6 +446,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 VERSION < v"0.5.0-dev+4340" && length(ex.args) > 2 && ex.args[1] === :show
ex = rewrite_show(ex)
end
if VERSION < v"0.5.0-dev+4305"
rewrite_iocontext!(ex)
Expand Down Expand Up @@ -516,6 +526,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 VERSION < v"0.5.0-dev+4340" && 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
21 changes: 21 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
using Compat
import Compat.String
@compat import Base.show
using Base.Test

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

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 @compat 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 @compat String(myio) == "MyTestCustomShowType2"

type TestCustomShowType3 end
@compat show(io::IO, ::TestCustomShowType3) = print(io, "2-Argument-show")
myio = IOBuffer()
display(TextDisplay(myio), TestCustomShowType3())
@test @compat String(myio) == "2-Argument-show"

d = Dict{Int,Int}()
d[1] = 1
@test Compat.@Dict(1 => 1) == d
Expand Down