Skip to content

Commit 3892cca

Browse files
authored
implement property interface to LazyPyModule (#473)
Fixes #437
1 parent 5e1a6db commit 3892cca

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/plot3d.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,26 @@ mutable struct LazyPyModule
66
o::PyObject
77
LazyPyModule(n) = new(n, PyNULL())
88
end
9-
PyObject(m::LazyPyModule) = ispynull(m.o) ? copy!(m.o, pyimport(m.name)) : m.o
9+
PyObject(m::LazyPyModule) = ispynull(getfield(m, :o)) ? copy!(getfield(m, :o), pyimport(getfield(m, :name))) : getfield(m, :o)
1010
pycall(m::LazyPyModule, args...; kws...) = pycall(PyObject(m), args...; kws...)
1111
(m::LazyPyModule)(args...; kws...) = pycall(PyObject(m), PyAny, args...; kws...)
1212
Base.Docs.doc(m::LazyPyModule) = Base.Docs.doc(PyObject(m))
13+
14+
# define each of these separately for Symbol and AbstractString to avoid method ambiguity:
15+
Base.getproperty(m::LazyPyModule, x::Symbol) = getproperty(PyObject(m), x)
16+
Base.getproperty(m::LazyPyModule, x::AbstractString) = getproperty(PyObject(m), x)
17+
Base.setproperty!(m::LazyPyModule, x::Symbol, v) = setproperty!(PyObject(m), x, v)
18+
Base.setproperty!(m::LazyPyModule, x::AbstractString, v) = setproperty!(PyObject(m), x, v)
19+
PyCall.hasproperty(m::LazyPyModule, x::Symbol) = PyCall.hasproperty(PyObject(m), x)
20+
PyCall.hasproperty(m::LazyPyModule, x::AbstractString) = PyCall.hasproperty(PyObject(m), x)
21+
22+
Base.propertynames(m::LazyPyModule) = propertynames(PyObject(m))
23+
keys(m::LazyPyModule) = keys(PyObject(m))
24+
25+
# deprecated methods:
1326
getindex(m::LazyPyModule, x) = getindex(PyObject(m), x)
1427
setindex!(m::LazyPyModule, v, x) = setindex!(PyObject(m), v, x)
1528
haskey(m::LazyPyModule, x) = haskey(PyObject(m), x)
16-
keys(m::LazyPyModule) = keys(PyObject(m))
1729

1830
###########################################################################
1931
# Lazily load mplot3d modules. This (slightly) improves load time of PyPlot,

0 commit comments

Comments
 (0)