Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make untyped mutating callbacks work #531

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/callback.jl
Original file line number Diff line number Diff line change
@@ -15,9 +15,22 @@

# convert Python args to Julia; overridden below for a FuncWrapper type
# that allows the user to specify the argument types.
julia_args(f, args) = convert(PyAny, args)
julia_args(f, args) =
[_julia_arg(_getindex(args, i, PyObject)) for i in 1:length(args)]
julia_kwarg(f, kw, arg) = convert(PyAny, arg)

# For mutating functions such as fill! to mutate Python objects,
# arguments have to be wrapped by an appropriate wrapper (e.g.,
# PyArray) rather than copying to a Julia object.
function _julia_arg(arg)
if haskey(PyCall.npy_api, :PyArray_Type) &&
pyisinstance(arg, PyCall.npy_api[:PyArray_Type])
return convert(PyArray, arg)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it makes sense to move the code above inside convert(::Type{PyAny}, o::PyObject)?

else
return convert(PyAny, arg)
end
end

function _pyjlwrap_call(f, args_::PyPtr, kw_::PyPtr)
args = PyObject(args_) # don't need pyincref because of finally clause below
try
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -151,6 +151,13 @@ const PyInt = pyversion < v"3" ? Int : Clonglong
@test x == ["bar"]
end

py"""
def apply(f, *args):
f(*args)
return args
"""
@test py"apply"(fill!, zeros(3), 10)[1] == [10, 10, 10]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should have called it apply_inplace or sth to avoid confusion with Python 2 builtin apply.


@test roundtripeq(Dates.Date(2012,3,4))
@test roundtripeq(Dates.DateTime(2012,3,4, 7,8,9,11))
@test roundtripeq(Dates.Millisecond(typemax(Int32)))