Skip to content

Commit

Permalink
fix BoundsError of args[1] when args is empty (#1014)
Browse files Browse the repository at this point in the history
* fix BoundsError of args[1] when args is empty

if `args` on L101 is empty, then `args[1]` in `isexpr()` would throw `BoundsError: attempt to access 0-element Vector{Any} at index [1]`

* add unit tests for empty arguments

* Update src/pyfncall.jl

* Update src/pyfncall.jl

Co-authored-by: Steven G. Johnson <[email protected]>
  • Loading branch information
szcf-weiya and stevengj authored Jan 9, 2023
1 parent 3b0a0f4 commit e379499
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/pyfncall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ macro pycall(ex)
end
func = ex.args[1].args[1]
args, kwargs = ex.args[1].args[2:end], []
if isexpr(args[1],:parameters)
if !isempty(args) && isexpr(args[1],:parameters)
kwargs, args = args[1], args[2:end]
end
T = ex.args[2]
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,8 @@ const PyInt = pyversion < v"3" ? Int : Clonglong

# @pycall macro expands correctly
_pycall = GlobalRef(PyCall,:pycall)
@test macroexpand(@__MODULE__, :(@pycall foo()::T)) == :($(_pycall)(foo, T))
@test macroexpand(@__MODULE__, :(@pycall foo(; kwargs...)::T)) == :($(_pycall)(foo, T; kwargs...))
@test macroexpand(@__MODULE__, :(@pycall foo(bar)::T)) == :($(_pycall)(foo, T, bar))
@test macroexpand(@__MODULE__, :(@pycall foo(bar, args...)::T)) == :($(_pycall)(foo, T, bar, args...))
@test macroexpand(@__MODULE__, :(@pycall foo(bar; kwargs...)::T)) == :($(_pycall)(foo, T, bar; kwargs...))
Expand Down

0 comments on commit e379499

Please sign in to comment.