Skip to content

Commit

Permalink
fix error handling in printf.jl (#25382)
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausC authored and fredrikekre committed Jan 9, 2018
1 parent 9624f10 commit 9b5eed2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
6 changes: 5 additions & 1 deletion base/printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ function gen_s(flags::String, width::Int, precision::Int, c::Char)
#
# flags:
# (-): left justify
# (#): use `show`/`repr` instead of `print`/`string`
#
@gensym x
blk = Expr(:block)
Expand Down Expand Up @@ -684,6 +685,9 @@ function gen_p(flags::String, width::Int, precision::Int, c::Char)
# print pointer:
# [p]: the only option
#
# flags:
# (-): left justify
#
@gensym x
blk = Expr(:block)
ptrwidth = Sys.WORD_SIZE>>2
Expand Down Expand Up @@ -1183,7 +1187,7 @@ function _printf(macroname, io, fmt, args)
quote
G = $(esc(x))
if length(G) != $(length(sym_args))
throw(ArgumentError($macroname,": wrong number of arguments (",length(G),") should be (",$(length(sym_args)),")"))
throw(ArgumentError(string($macroname,": wrong number of arguments (",length(G),") should be (",$(length(sym_args)),")")))
end
end
)
Expand Down
30 changes: 18 additions & 12 deletions stdlib/Printf/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

using Test, Printf

macro test_throws(ty, ex)
# this macro tests for exceptions thrown at macro expansion
macro test_me(ty, ex)
return quote
Test.@test_throws $(esc(ty)) try
@test_throws $(esc(ty)) try
$(esc(ex))
catch err
@test err isa LoadError
Expand Down Expand Up @@ -204,14 +205,14 @@ end
# escape %
@test (@sprintf "%%") == "%"
@test (@sprintf "%%s") == "%s"
@test_throws ArgumentError("invalid printf format string: \"%\"") @macroexpand(@sprintf "%") #" (fixes syntax highlighting)
@test_me ArgumentError("invalid printf format string: \"%\"") @macroexpand(@sprintf "%") #" (fixes syntax highlighting)

# argument count
@test_throws ArgumentError("@sprintf: wrong number of arguments (0) should be (1)") @macroexpand(@sprintf "%s")
@test_throws ArgumentError("@sprintf: wrong number of arguments (2) should be (1)") @macroexpand(@sprintf "%s" "1" "2")
@test_me ArgumentError("@sprintf: wrong number of arguments (0) should be (1)") @macroexpand(@sprintf "%s")
@test_me ArgumentError("@sprintf: wrong number of arguments (2) should be (1)") @macroexpand(@sprintf "%s" "1" "2")

# no interpolation
@test_throws ArgumentError("@sprintf: format must be a plain static string (no interpolation or prefix)") @macroexpand(@sprintf "$n")
@test_me ArgumentError("@sprintf: format must be a plain static string (no interpolation or prefix)") @macroexpand(@sprintf "$n")

# type width specifier parsing (ignored)
@test (@sprintf "%llf" 1.2) == "1.200000"
Expand Down Expand Up @@ -257,7 +258,7 @@ end
# invalid format specifiers, not "diouxXDOUeEfFgGaAcCsSpn"
for c in "bBhHIjJkKlLmMNPqQrRtTvVwWyYzZ"
fmt_str = string("%", c)
@test_throws ArgumentError("@sprintf: first argument must be a format string") @macroexpand(@sprintf $fmt_str 1)
@test_me ArgumentError("@sprintf: first argument must be a format string") @macroexpand(@sprintf $fmt_str 1)
end

# combo
Expand All @@ -270,7 +271,7 @@ end
@test (@sprintf "%s %s %s %d %d %d %f %f %f" Any[10^x+y for x=1:3,y=1:3 ]...) == "11 101 1001 12 102 1002 13.000000 103.000000 1003.000000"

# @printf
@test_throws ArgumentError("@printf: first or second argument must be a format string") @macroexpand(@printf 1)
@test_me ArgumentError("@printf: first or second argument must be a format string") @macroexpand(@printf 1)

# Check bug with trailing nul printing BigFloat
@test (@sprintf("%.330f", BigFloat(1)))[end] != '\0'
Expand All @@ -285,7 +286,12 @@ end
@test (@sprintf("%d\u0f00%d", 1, 2)) == "1\u0f002"
@test (@sprintf("%d\U0001ffff%d", 1, 2)) == "1\U0001ffff2"
@test (@sprintf("%d\u2203%d\u0203", 1, 2)) == "1\u22032\u0203"
@test_throws ArgumentError @macroexpand(@sprintf("%y%d", 1, 2))
@test_throws ArgumentError @macroexpand(@sprintf("%\u00d0%d", 1, 2))
@test_throws ArgumentError @macroexpand(@sprintf("%\u0f00%d", 1, 2))
@test_throws ArgumentError @macroexpand(@sprintf("%\U0001ffff%d", 1, 2))
@test_me ArgumentError @macroexpand(@sprintf("%y%d", 1, 2))
@test_me ArgumentError @macroexpand(@sprintf("%\u00d0%d", 1, 2))
@test_me ArgumentError @macroexpand(@sprintf("%\u0f00%d", 1, 2))
@test_me ArgumentError @macroexpand(@sprintf("%\U0001ffff%d", 1, 2))

# test at macro execution time
@test_throws ArgumentError("@sprintf: wrong number of arguments (2) should be (3)") (@sprintf "%d%d%d" 1:2...)


0 comments on commit 9b5eed2

Please sign in to comment.