Skip to content

Commit 9c0dc16

Browse files
author
Christopher Doris
committed
easier specification of signature and defaults in pyfunc
1 parent a526e89 commit 9c0dc16

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/jlwrap/callback.jl

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@ function pyjlcallback_call(self, args_::Py, kwargs_::Py)
1010
args = pyconvert(Vector{Py}, args_)
1111
kwargs = pyconvert(Dict{Symbol,Py}, kwargs_)
1212
Py(self(args...; kwargs...))
13-
elseif pylen(args_) > 0
13+
elseif (nargs = pylen(args_)) > 0
1414
args = pyconvert(Vector{Py}, args_)
15-
if length(args) == 1
15+
@assert length(args) == nargs
16+
if nargs == 1
1617
Py(self(args[1]))
17-
elseif length(args) == 2
18+
elseif nargs == 2
1819
Py(self(args[1], args[2]))
19-
elseif length(args) == 3
20+
elseif nargs == 3
2021
Py(self(args[1], args[2], args[3]))
22+
elseif nargs == 4
23+
Py(self(args[1], args[2], args[3], args[4]))
24+
elseif nargs == 5
25+
Py(self(args[1], args[2], args[3], args[4], args[5]))
2126
else
2227
Py(self(args...))
2328
end
@@ -53,7 +58,7 @@ end
5358
pyjlcallback(f) = pyjl(pyjlcallbacktype, f)
5459

5560
"""
56-
pyfunc(f; name=nothing, qualname=name, doc=nothing, signature=nothing)
61+
pyfunc(f; [name], [qualname], [doc], [signature])
5762
5863
Wrap the callable `f` as an ordinary Python function.
5964
@@ -63,10 +68,18 @@ The name, qualname, docstring or signature can optionally be set with `name`, `q
6368
Unlike `Py(f)` (or `pyjl(f)`), the arguments passed to `f` are always of type `Py`, i.e.
6469
they are never converted.
6570
"""
66-
function pyfunc(f; name=nothing, qualname=name, doc=nothing, signature=nothing)
71+
function pyfunc(f; name=nothing, qualname=name, doc=nothing, signature=nothing, wrap=pywrapcallback)
6772
f2 = ispy(f) ? f : pyjlcallback(f)
68-
f3 = pywrapcallback(f2)
69-
pydel!(f2)
73+
if wrap isa Pair
74+
wrapargs, wrapfunc = wrap
75+
else
76+
wrapargs, wrapfunc = (), wrap
77+
end
78+
if wrapfunc isa AbstractString
79+
f3 = pybuiltins.eval(wrapfunc, pydict())(f2, wrapargs...)
80+
else
81+
f3 = wrapfunc(f2, wrapargs...)
82+
end
7083
f3.__name__ = name === nothing ? "<lambda>" : name
7184
f3.__qualname__ = name === nothing ? "<lambda>" : qualname
7285
if doc !== nothing

0 commit comments

Comments
 (0)