diff --git a/PlotsBase/ext/PythonPlotExt.jl b/PlotsBase/ext/PythonPlotExt.jl index 8b69b054b..1a3ec1279 100644 --- a/PlotsBase/ext/PythonPlotExt.jl +++ b/PlotsBase/ext/PythonPlotExt.jl @@ -15,9 +15,6 @@ end const mpl_toolkits = PythonCall.pynew() const numpy = PythonCall.pynew() const mpl = PythonCall.pynew() -const Gcf = PythonCall.pynew() -const orig_gcf = PythonCall.pynew() -const orig_figure = PythonCall.pynew() using PlotsBase.Annotations using PlotsBase.DataSeries @@ -48,7 +45,7 @@ function PlotsBase.extension_init(::PythonPlotBackend) backend_gui = PythonPlot.find_backend(mpl) PythonPlot.backend = backend_gui[1] PythonPlot.gui = backend_gui[2] - PythonCall.pycopy!(PythonPlot.pyplot, PythonCall.pyimport("matplotlib.pyplot")) # raw Python module + PythonCall.pycopy!(PythonPlot.pyplot, PythonCall.pyimport("matplotlib.pyplot")) # raw Python module PythonCall.pycopy!(PythonPlot.Gcf, PythonCall.pyimport("matplotlib._pylab_helpers").Gcf) PythonCall.pycopy!(PythonPlot.orig_gcf, PythonPlot.pyplot.gcf) PythonCall.pycopy!(PythonPlot.orig_figure, PythonPlot.pyplot.figure) @@ -79,6 +76,21 @@ function PlotsBase.extension_init(::PythonPlotBackend) end end +function PlotsBase.extension_cleanup(::PythonPlotBackend) + mpl_toolkits = PythonCall.pynew() + numpy = PythonCall.pynew() + mpl = PythonCall.pynew() + PythonPlot.pyplot.gcf = PythonCall.pynew() + PythonPlot.pyplot.figure = PythonCall.pynew() + PythonPlot.pyplot = PythonCall.pynew() + PythonPlot.Gcf = PythonCall.pynew() + PythonPlot.orig_gcf = PythonCall.pynew() + PythonPlot.orig_figure = PythonCall.pynew() + PythonPlot.backend = PythonCall.pynew() + PythonPlot.gui = PythonCall.pynew() + nothing +end + PlotsBase.@extension_static PythonPlotBackend pythonplot const _pythonplot_attrs = PlotsBase.merge_with_base_supported([ diff --git a/PlotsBase/src/backends.jl b/PlotsBase/src/backends.jl index d64c2915c..db3553b80 100644 --- a/PlotsBase/src/backends.jl +++ b/PlotsBase/src/backends.jl @@ -183,6 +183,8 @@ end "extra init step for an extension" extension_init(::AbstractBackend) = nothing +extension_cleanup(::AbstractBackend) = nothing + "generate extension `__init__` function, and common defines" macro extension_static(be_type, be) be_sym = QuoteNode(be) diff --git a/PlotsBase/src/init.jl b/PlotsBase/src/init.jl index cf908dd55..c40c0bb2d 100644 --- a/PlotsBase/src/init.jl +++ b/PlotsBase/src/init.jl @@ -111,12 +111,12 @@ function diagnostics(io::IO = stdout) end macro precompile_backend(backend_package) + abstract_backend = Symbol(backend_package, :Backend) quote PrecompileTools.@setup_workload begin using PlotsBase # for extensions - backend($(Symbol(backend_package, :Backend))) - __init__() # must call init !! - # PlotsBase.extension_init($be_type()) # because `__init__` has `ccall(:jl_generating_output, Cint, ()) == 1 && return` + backend($abstract_backend()) + __init__() # call extension module init !! @debug PlotsBase.backend_package_name() n = length(PlotsBase._examples) imports = sizehint!(Expr[], n) @@ -164,9 +164,10 @@ macro precompile_backend(backend_package) PrecompileTools.@compile_workload begin eval.(imports) eval.(examples) + PlotsBase.CURRENT_PLOT.nullableplot = nothing + PlotsBase.extension_cleanup($abstract_backend()) end end - PlotsBase.CURRENT_PLOT.nullableplot = nothing end end |> esc end