From e3794994c7ae90feffe1e0e5d7d5e2d6007ccc03 Mon Sep 17 00:00:00 2001 From: szcf-weiya Date: Mon, 9 Jan 2023 16:52:50 -0500 Subject: [PATCH] fix BoundsError of args[1] when args is empty (#1014) * 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 --- src/pyfncall.jl | 2 +- test/runtests.jl | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pyfncall.jl b/src/pyfncall.jl index fc07d8e8..11e81287 100644 --- a/src/pyfncall.jl +++ b/src/pyfncall.jl @@ -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] diff --git a/test/runtests.jl b/test/runtests.jl index 5cc1b5ce..2e5c915a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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...))