Skip to content

Commit

Permalink
Check argument count before accessing expression arguments
Browse files Browse the repository at this point in the history
Fix #228
  • Loading branch information
yuyichao committed Jun 26, 2016
1 parent 7e63835 commit 3c8d28d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
39 changes: 22 additions & 17 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,34 +269,39 @@ function rewrite_pairs_to_tuples!(expr::Expr)
return expr
end

function is_quote_symbol(ex::ANY, val::Symbol)
if isa(ex, QuoteNode)
return (ex::QuoteNode).value === val
elseif isa(ex, Expr)
ex = ex::Expr
return ex.head === :quote && length(ex.args) == 1 && ex.args[1] === val
end
return false
end

# rewrites accesses to IOContext dicts
function rewrite_iocontext!(expr::Expr)
if expr.head == :call && expr.args[1] == :get
key = expr.args[3]
if (((isa(key, QuoteNode) && key.value == :limit) ||
(isa(key, Expr) && key.head == :quote && key.args[1] == :limit) ||
((isa(key, QuoteNode) && key.value == :compact) ||
(isa(key, Expr) && key.head == :quote && key.args[1] == :compact)))
&& expr.args[4] == false)
args = expr.args
nargs = length(args)
if nargs == 4 && expr.head === :call && args[1] === :get && args[4] === false
key = args[3]
if is_quote_symbol(key, :limit) || is_quote_symbol(key, :compact)
if VERSION >= v"0.5.0-dev+1936" && VERSION < v"0.5.0-dev+4305"
expr.args[1] = :(Base.limit_output)
deleteat!(expr.args, 3:4)
args[1] = :(Base.limit_output)
deleteat!(args, 3:4)
elseif VERSION < v"0.5.0-dev+1936"
expr.head = :quote
expr.args[1] = false
deleteat!(expr.args, 3:4)
args[1] = false
deleteat!(args, 3:4)
end
elseif (((isa(key, QuoteNode) && key.value == :multiline) ||
(isa(key, Expr) && key.head == :quote && key.args[1] == :multiline))
&& expr.args[4] == false)
elseif is_quote_symbol(key, :multiline)
if VERSION < v"0.5.0-dev+4305"
expr.head = :quote
expr.args[1] = false
deleteat!(expr.args, 3:4)
args[1] = false
deleteat!(args, 3:4)
end
end
end
expr
end

if VERSION < v"0.4.0-dev+707"
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,7 @@ io = IOBuffer()
@test @compat(get(io, :limit, false)) == false
@test @compat(get(io, :compact, false)) == false
@test @compat(get(io, :multiline, false)) == false

@test @compat(get(Nullable(1))) == 1

let
test_str = "test"
Expand Down

0 comments on commit 3c8d28d

Please sign in to comment.