Skip to content

Commit

Permalink
Use fewer anonymous functions for sprint
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao committed Feb 10, 2017
1 parent 79471c1 commit f7a69bd
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 49 deletions.
4 changes: 2 additions & 2 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ macro deprecate(old,new,ex=true)
end))
elseif isa(old,Expr) && old.head == :call
remove_linenums!(new)
oldcall = sprint(io->show_unquoted(io,old))
newcall = sprint(io->show_unquoted(io,new))
oldcall = sprint(show_unquoted, old)
newcall = sprint(show_unquoted, new)
oldsym = if isa(old.args[1],Symbol)
old.args[1]
elseif isa(old.args[1],Expr) && old.args[1].head == :curly
Expand Down
2 changes: 1 addition & 1 deletion base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ elseif is_windows()
systemerror(:SetClipboardData, pdata!=p)
ccall((:CloseClipboard, "user32"), stdcall, Void, ())
end
clipboard(x) = clipboard(sprint(io->print(io,x))::String)
clipboard(x) = clipboard(sprint(print, x)::String)
function clipboard()
systemerror(:OpenClipboard, 0==ccall((:OpenClipboard, "user32"), stdcall, Cint, (Ptr{Void},), C_NULL))
pdata = ccall((:GetClipboardData, "user32"), stdcall, Ptr{UInt16}, (UInt32,), 13)
Expand Down
8 changes: 4 additions & 4 deletions base/markdown/render/plain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ end
function plain(io::IO, list::List)
for (i, item) in enumerate(list.items)
print(io, isordered(list) ? "$(i + list.ordered - 1). " : " * ")
lines = split(rstrip(sprint(buf -> plain(buf, item))), "\n")
lines = split(rstrip(sprint(plain, item)), "\n")
for (n, line) in enumerate(lines)
print(io, (n == 1 || isempty(line)) ? "" : " ", line)
n < length(lines) && println(io)
Expand All @@ -46,7 +46,7 @@ function plain(io::IO, list::List)
end

function plain(io::IO, q::BlockQuote)
s = sprint(buf -> plain(buf, q.content))
s = sprint(plain, q.content)
for line in split(rstrip(s), "\n")
println(io, isempty(line) ? ">" : "> ", line)
end
Expand All @@ -55,7 +55,7 @@ end

function plain(io::IO, f::Footnote)
print(io, "[^", f.id, "]:")
s = sprint(io -> plain(io, f.text))
s = sprint(plain, f.text)
lines = split(rstrip(s), "\n")
# Single line footnotes are printed on the same line as their label
# rather than taking up an additional line.
Expand All @@ -71,7 +71,7 @@ function plain(io::IO, f::Footnote)
end

function plain(io::IO, md::Admonition)
s = sprint(buf -> plain(buf, md.content))
s = sprint(plain, md.content)
title = md.title == ucfirst(md.category) ? "" : " \"$(md.title)\""
println(io, "!!! ", md.category, title)
for line in split(rstrip(s), "\n")
Expand Down
8 changes: 4 additions & 4 deletions base/markdown/render/rst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function rst(io::IO, list::List)
for (i, item) in enumerate(list.items)
bullet = isordered(list) ? "$(i + list.ordered - 1). " : "* "
print(io, bullet)
lines = split(rstrip(sprint(buf -> rst(buf, item))), '\n')
lines = split(rstrip(sprint(rst, item)), '\n')
for (n, line) in enumerate(lines)
print(io, (n == 1 || isempty(line)) ? "" : " "^length(bullet), line)
n < length(lines) && println(io)
Expand All @@ -50,7 +50,7 @@ function rst(io::IO, list::List)
end

function rst(io::IO, q::BlockQuote)
s = sprint(buf -> rst(buf, q.content))
s = sprint(rst, q.content)
for line in split(rstrip(s), "\n")
println(io, " ", line)
end
Expand All @@ -59,7 +59,7 @@ end

function rst(io::IO, f::Footnote)
print(io, ".. [", f.id, "]")
s = sprint(io -> rst(io, f.text))
s = sprint(rst, f.text)
lines = split(rstrip(s), "\n")
# Single line footnotes are printed on the same line as their label
# rather than taking up an additional line.
Expand All @@ -75,7 +75,7 @@ function rst(io::IO, f::Footnote)
end

function rst(io::IO, md::Admonition)
s = sprint(buf -> rst(buf, md.content))
s = sprint(rst, md.content)
title = md.title == ucfirst(md.category) ? "" : md.title
println(io, ".. ", md.category, "::", isempty(title) ? "" : " $title")
for line in split(rstrip(s), "\n")
Expand Down
6 changes: 3 additions & 3 deletions base/markdown/render/terminal/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function term(io::IO, md::Paragraph, columns)
end

function term(io::IO, md::BlockQuote, columns)
s = sprint(io->term(io, md.content, columns - 10))
s = sprint(term, md.content, columns - 10)
for line in split(rstrip(s), "\n")
println(io, " "^margin, "|", line)
end
Expand All @@ -34,7 +34,7 @@ function term(io::IO, md::Admonition, columns)
print(io, " "^margin, "| ")
with_output_format(:bold, print, io, isempty(md.title) ? md.category : md.title)
println(io, "\n", " "^margin, "|")
s = sprint(io -> term(io, md.content, columns - 10))
s = sprint(term, md.content, columns - 10)
for line in split(rstrip(s), "\n")
println(io, " "^margin, "|", line)
end
Expand All @@ -44,7 +44,7 @@ function term(io::IO, f::Footnote, columns)
print(io, " "^margin, "| ")
with_output_format(:bold, print, io, "[^$(f.id)]")
println(io, "\n", " "^margin, "|")
s = sprint(io -> term(io, f.text, columns - 10))
s = sprint(term, f.text, columns - 10)
for line in split(rstrip(s), "\n")
println(io, " "^margin, "|", line)
end
Expand Down
4 changes: 2 additions & 2 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,13 @@ WARNING: Beep Beep
warn(msg...; kw...) = warn(STDERR, msg...; kw...)

warn(io::IO, err::Exception; prefix="ERROR: ", kw...) =
warn(io, sprint(buf->showerror(buf, err)), prefix=prefix; kw...)
warn(io, sprint(showerror, err), prefix=prefix; kw...)

warn(err::Exception; prefix="ERROR: ", kw...) =
warn(STDERR, err, prefix=prefix; kw...)

info(io::IO, err::Exception; prefix="ERROR: ", kw...) =
info(io, sprint(buf->showerror(buf, err)), prefix=prefix; kw...)
info(io, sprint(showerror, err), prefix=prefix; kw...)

info(err::Exception; prefix="ERROR: ", kw...) =
info(STDERR, err, prefix=prefix; kw...)
Expand Down
4 changes: 2 additions & 2 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ for T in (Int64, Float64)
end

#showcompact
@test sprint(io -> showcompact(io,complex(1,0))) == "1+0im"
@test sprint(io -> show(io,complex(true,true))) == "Complex(true,true)"
@test sprint(showcompact, complex(1, 0)) == "1+0im"
@test sprint(show, complex(true, true)) == "Complex(true,true)"

@testset "arithmetic" begin
@testset for T in (Float16, Float32, Float64, BigFloat)
Expand Down
2 changes: 1 addition & 1 deletion test/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ let data = "1 2 3"
end

# test show with MIME types
@test sprint(io -> show(io, "text/csv", [1 2; 3 4])) == "1,2\n3,4\n"
@test sprint(show, "text/csv", [1 2; 3 4]) == "1,2\n3,4\n"

for writefunc in ((io,x) -> show(io, "text/csv", x),
(io,x) -> invoke(writedlm, Tuple{IO,Any,Any}, io, x, ","))
Expand Down
2 changes: 1 addition & 1 deletion test/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ let bstream = BufferStream()
@test isreadable(bstream)
@test iswritable(bstream)
@test nb_available(bstream) == 0
@test sprint(io -> show(io,bstream)) == "BufferStream() bytes waiting:$(nb_available(bstream.buffer)), isopen:true"
@test sprint(show, bstream) == "BufferStream() bytes waiting:$(nb_available(bstream.buffer)), isopen:true"
a = rand(UInt8,10)
write(bstream,a)
@test !eof(bstream)
Expand Down
2 changes: 1 addition & 1 deletion test/llvmcall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function declared_floor(x::Float64)
Float64, Tuple{Float64}, x)
end
@test declared_floor(4.2) 4.
ir = sprint(io->code_llvm(io, declared_floor, Tuple{Float64}))
ir = sprint(code_llvm, declared_floor, Tuple{Float64})
@test contains(ir, "call double @llvm.floor.f64") # should be inlined

function doubly_declared_floor(x::Float64)
Expand Down
2 changes: 1 addition & 1 deletion test/llvmcall2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function declared_floor(x::Float64)
return ccall("llvm.floor.f64", llvmcall, Float64, (Float64,), x)
end
@test declared_floor(4.2) == 4.0
ir = sprint(io->code_llvm(io, declared_floor, Tuple{Float64}))
ir = sprint(code_llvm, declared_floor, Tuple{Float64})
@test contains(ir, "call double @llvm.floor.f64") # should be inlined

function doubly_declared_floor(x::Float64)
Expand Down
10 changes: 5 additions & 5 deletions test/markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ let out =
* list1
* list2
"""
@test sprint(io -> show(io, "text/plain", book)) == out
@test sprint(io -> show(io, "text/markdown", book)) == out
@test sprint(show, "text/plain", book) == out
@test sprint(show, "text/markdown", book) == out
end
let out =
"""
Expand All @@ -332,7 +332,7 @@ let out =
</li>
</ul>
</div>"""
@test sprint(io -> show(io, "text/html", book)) == out
@test sprint(show, "text/html", book) == out
end
let out =
"""
Expand All @@ -354,7 +354,7 @@ let out =
\\end{itemize}
"""
@test sprint(io -> show(io, "text/latex", book)) == out
@test sprint(show, "text/latex", book) == out
end
let out =
"""
Expand All @@ -376,7 +376,7 @@ let out =
* list1
* list2
"""
@test sprint(io -> show(io, "text/rst", book)) == out
@test sprint(show, "text/rst", book) == out
end

# rst rendering
Expand Down
20 changes: 10 additions & 10 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
# Tests that do not really go anywhere else

# Test info
@test contains(sprint(io->info(io,"test")), "INFO:")
@test contains(sprint(io->info(io,"test")), "INFO: test")
@test contains(sprint(io->info(io,"test ",1,2,3)), "INFO: test 123")
@test contains(sprint(info, "test"), "INFO:")
@test contains(sprint(info, "test"), "INFO: test")
@test contains(sprint(info, "test ", 1, 2, 3), "INFO: test 123")
@test contains(sprint(io->info(io,"test", prefix="MYINFO: ")), "MYINFO: test")

# Test warn
@test contains(sprint(io->Base.warn_once(io,"test")), "WARNING: test")
@test isempty(sprint(io->Base.warn_once(io,"test")))
@test contains(sprint(Base.warn_once, "test"), "WARNING: test")
@test isempty(sprint(Base.warn_once, "test"))

@test contains(sprint(io->warn(io)), "WARNING:")
@test contains(sprint(io->warn(io, "test")), "WARNING: test")
@test contains(sprint(io->warn(io, "test ",1,2,3)), "WARNING: test 123")
@test contains(sprint(warn), "WARNING:")
@test contains(sprint(warn, "test"), "WARNING: test")
@test contains(sprint(warn, "test ", 1, 2, 3), "WARNING: test 123")
@test contains(sprint(io->warn(io, "test", prefix="MYWARNING: ")), "MYWARNING: test")
@test contains(sprint(io->warn(io, "testonce", once=true)), "WARNING: testonce")
@test isempty(sprint(io->warn(io, "testonce", once=true)))
@test !isempty(sprint(io->warn(io, "testonce", once=true, key=hash("testonce",hash("testanother")))))
let bt = backtrace()
ws = split(chomp(sprint(io->warn(io,"test", bt))), '\n')
bs = split(chomp(sprint(io->Base.show_backtrace(io,bt))), '\n')
ws = split(chomp(sprint(warn, "test", bt)), '\n')
bs = split(chomp(sprint(Base.show_backtrace, bt)), '\n')
@test contains(ws[1],"WARNING: test")
for (l,b) in zip(ws[2:end],bs)
@test contains(l, b)
Expand Down
4 changes: 2 additions & 2 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2744,8 +2744,8 @@ let
io = IOBuffer()
rational1 = Rational(1465, 8593)
rational2 = Rational(-4500, 9000)
@test sprint(io -> show(io, rational1)) == "1465//8593"
@test sprint(io -> show(io, rational2)) == "-1//2"
@test sprint(show, rational1) == "1465//8593"
@test sprint(show, rational2) == "-1//2"
let
io1 = IOBuffer()
write(io1, rational1)
Expand Down
2 changes: 1 addition & 1 deletion test/pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ temp_pkg_dir() do
@test isfile(joinpath(Pkg.dir(),"REQUIRE"))
@test isfile(joinpath(Pkg.dir(),"META_BRANCH"))
@test isempty(Pkg.installed())
@test sprint(io -> Pkg.status(io)) == "No packages installed\n"
@test sprint(Pkg.status) == "No packages installed\n"
@test !isempty(Pkg.available())

# 18325
Expand Down
4 changes: 2 additions & 2 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,8 @@ replstrmime(x) = sprint((io,x) -> show(IOContext(io, limit=true), MIME("text/pla
@test replstrmime(linspace(0,100, 10000)) == "0.0:0.010001000100010001:100.0"
@test replstrmime(LinSpace{Float64}(0,100, 10000)) == "10000-element LinSpace{Float64}:\n 0.0,0.010001,0.020002,0.030003,0.040004,…,99.95,99.96,99.97,99.98,99.99,100.0"

@test sprint(io -> show(io,UnitRange(1,2))) == "1:2"
@test sprint(io -> show(io,StepRange(1,2,5))) == "1:2:5"
@test sprint(show, UnitRange(1, 2)) == "1:2"
@test sprint(show, StepRange(1, 2, 5)) == "1:2:5"


# Issue 11049 and related
Expand Down
4 changes: 2 additions & 2 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,8 @@ end

# Issue #18883, code_llvm/code_native for generated functions
@generated f18883() = nothing
@test !isempty(sprint(io->code_llvm(io, f18883, Tuple{})))
@test !isempty(sprint(io->code_native(io, f18883, Tuple{})))
@test !isempty(sprint(code_llvm, f18883, Tuple{}))
@test !isempty(sprint(code_native, f18883, Tuple{}))

# PR #19964
@test isempty(subtypes(Float64))
Expand Down
10 changes: 5 additions & 5 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -399,22 +399,22 @@ end
@test Base.inbase(LinAlg)
@test !Base.inbase(Core)

let repr = sprint(io -> show(io,"text/plain", methods(Base.inbase)))
let repr = sprint(show, "text/plain", methods(Base.inbase))
@test contains(repr, "inbase(m::Module)")
end
let repr = sprint(io -> show(io,"text/html", methods(Base.inbase)))
let repr = sprint(show, "text/html", methods(Base.inbase))
@test contains(repr, "inbase(m::<b>Module</b>)")
end

f5971(x, y...; z=1, w...) = nothing
let repr = sprint(io -> show(io,"text/plain", methods(f5971)))
let repr = sprint(show, "text/plain", methods(f5971))
@test contains(repr, "f5971(x, y...; z, w...)")
end
let repr = sprint(io -> show(io,"text/html", methods(f5971)))
let repr = sprint(show, "text/html", methods(f5971))
@test contains(repr, "f5971(x, y...; <i>z, w...</i>)")
end
f16580(x, y...; z=1, w=y+x, q...) = nothing
let repr = sprint(io -> show(io,"text/html", methods(f16580)))
let repr = sprint(show, "text/html", methods(f16580))
@test contains(repr, "f16580(x, y...; <i>z, w, q...</i>)")
end

Expand Down

0 comments on commit f7a69bd

Please sign in to comment.