Skip to content

Commit 1a1e2a0

Browse files
author
Christopher Doris
committed
tweak pyjl
1 parent a534cf4 commit 1a1e2a0

File tree

12 files changed

+36
-16
lines changed

12 files changed

+36
-16
lines changed

docs/src/conversion-to-python.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ PythonCall.getpy
4343
PythonCall.ispy
4444
```
4545

46+
Alternatively, if you define a wrapper type (a subtype of
47+
[`juliacall.AnyValue`](#juliacall.AnyValue)) then you may instead define `pyjltype(::T)` to
48+
be that type.
49+
50+
```@docs
51+
PythonCall.pyjltype
52+
```
53+
4654
## [Wrapper types](@id julia-wrappers)
4755

4856
Apart from a few fundamental immutable types, all Julia values are by default converted into Python to some [`AnyValue`](#juliacall.AnyValue) object, which wraps the original value, but giving it a Pythonic interface.

src/jlwrap/any.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ function init_jlwrap_any()
301301
end
302302

303303
"""
304-
pyjl([t], x)
304+
pyjl([t=pyjltype(x)], x)
305305
306306
Create a Python object wrapping the Julia object `x`.
307307
@@ -311,5 +311,18 @@ Its Python type is normally inferred from the type of `x`, but can be specified
311311
312312
For example if `x` is an `AbstractVector` then the object will have type `juliacall.VectorValue`.
313313
This object will satisfy the Python sequence interface, so for example uses 0-up indexing.
314+
315+
To define a custom conversion for your type `T`, overload `pyjltype(::T)`.
316+
"""
317+
pyjl(v) = pyjl(pyjltype(v), v)
318+
export pyjl
319+
320+
"""
321+
pyjltype(x)
322+
323+
The subtype of `juliacall.AnyValue` which the Julia object `x` is wrapped as by `pyjl(x)`.
324+
325+
Overload `pyjltype(::T)` to define a custom conversion for your type `T`.
314326
"""
315-
pyjl(v) = pyjl(pyjlanytype, v)
327+
pyjltype(::Any) = pyjlanytype
328+
export pyjltype

src/jlwrap/array.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,4 @@ function init_jlwrap_array()
334334
pycopy!(pyjlarraytype, jl.ArrayValue)
335335
end
336336

337-
pyjl(v::AbstractArray) = pyjl(pyjlarraytype, v)
337+
pyjltype(::AbstractArray) = pyjlarraytype

src/jlwrap/base.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ _pyjl_getvalue(x) = @autopy x C.PyJuliaValue_GetValue(getptr(x_))
55
_pyjl_setvalue!(x, v) = @autopy x C.PyJuliaValue_SetValue(getptr(x_), v)
66

77
pyjl(t, v) = pynew(errcheck(@autopy t C.PyJuliaValue_New(getptr(t_), v)))
8-
export pyjl
98

109
"""
1110
pyisjl(x)

src/jlwrap/dict.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,4 @@ function init_jlwrap_dict()
104104
pycopy!(pyjldicttype, jl.DictValue)
105105
end
106106

107-
pyjl(v::AbstractDict) = pyjl(pyjldicttype, v)
107+
pyjltype(::AbstractDict) = pyjldicttype

src/jlwrap/io.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,4 @@ Wrap `io` as a Python text IO object.
322322
pytextio(v::IO) = pyjl(pyjltextiotype, v)
323323
export pytextio
324324

325-
pyjl(v::IO) = pybinaryio(v)
325+
pyjltype(::IO) = pyjlbinaryiotype

src/jlwrap/iter.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ function init_jlwrap_iter()
4040
pycopy!(pyjlitertype, jl.IteratorValue)
4141
end
4242

43-
pyjl(v::Iterator) = pyjl(pyjlitertype, v)
43+
pyjltype(::Iterator) = pyjlitertype

src/jlwrap/module.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ function init_jlwrap_module()
2828
pycopy!(pyjlmoduletype, jl.ModuleValue)
2929
end
3030

31-
pyjl(v::Module) = pyjl(pyjlmoduletype, v)
31+
pyjltype(::Module) = pyjlmoduletype

src/jlwrap/number.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ function init_jlwrap_number()
224224
pycopy!(pyjlintegertype, jl.IntegerValue)
225225
end
226226

227-
pyjl(v::Number) = pyjl(pyjlnumbertype, v)
228-
pyjl(v::Complex) = pyjl(pyjlcomplextype, v)
229-
pyjl(v::Real) = pyjl(pyjlrealtype, v)
230-
pyjl(v::Rational) = pyjl(pyjlrationaltype, v)
231-
pyjl(v::Integer) = pyjl(pyjlintegertype, v)
227+
pyjltype(::Number) = pyjlnumbertype
228+
pyjltype(::Complex) = pyjlcomplextype
229+
pyjltype(::Real) = pyjlrealtype
230+
pyjltype(::Rational) = pyjlrationaltype
231+
pyjltype(::Integer) = pyjlintegertype

src/jlwrap/set.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,4 @@ function init_jlwrap_set()
121121
pycopy!(pyjlsettype, jl.SetValue)
122122
end
123123

124-
pyjl(v::AbstractSet) = pyjl(pyjlsettype, v)
124+
pyjltype(::AbstractSet) = pyjlsettype

0 commit comments

Comments
 (0)