"Matplotlib support failed" when importing matplotlib subpackages (PY-29260)

Sometimes activation function can be called, when matplotlib module isn't fully imported. Do not remove activation function until the module is imported properly.
This commit is contained in:
Elizaveta Shashkova
2018-04-16 16:54:03 +03:00
parent f5e8df4013
commit 27cecf4c84
2 changed files with 13 additions and 4 deletions
@@ -1,4 +1,3 @@
import sys
import traceback
from types import ModuleType
@@ -16,19 +15,23 @@ class ImportHookManager(ModuleType):
def do_import(self, name, *args, **kwargs):
activate_func = None
if name in self._modules_to_patch:
activate_func = self._modules_to_patch.pop(name)
activate_func = self._modules_to_patch[name]
module = self._system_import(name, *args, **kwargs)
try:
if activate_func:
activate_func() #call activate function
succeeded = activate_func()
if succeeded and name in self._modules_to_patch:
# Remove if only it was executed correctly
self._modules_to_patch.pop(name)
except:
sys.stderr.write("Matplotlib support failed\n")
traceback.print_exc()
return module
if sys.version_info[0] >= 3:
import builtins # py3
import builtins # py3
else:
import __builtin__ as builtins
@@ -90,6 +90,9 @@ def activate_matplotlib(enable_gui_function):
enable_gui_function - Function which enables gui, should be run in the main thread.
"""
matplotlib = sys.modules['matplotlib']
if not hasattr(matplotlib, 'rcParams'):
# matplotlib module wasn't fully imported, try later
return False
gui, backend = find_gui_and_backend()
is_interactive = is_interactive_backend(backend)
if is_interactive:
@@ -103,6 +106,7 @@ def activate_matplotlib(enable_gui_function):
matplotlib.interactive(False)
patch_use(enable_gui_function)
patch_is_interactive()
return True
def flag_calls(func):
@@ -139,6 +143,7 @@ def activate_pylab():
# We need to detect at runtime whether show() is called by the user.
# For this, we wrap it into a decorator which adds a 'called' flag.
pylab.draw_if_interactive = flag_calls(pylab.draw_if_interactive)
return True
def activate_pyplot():
@@ -147,3 +152,4 @@ def activate_pyplot():
# We need to detect at runtime whether show() is called by the user.
# For this, we wrap it into a decorator which adds a 'called' flag.
pyplot.draw_if_interactive = flag_calls(pyplot.draw_if_interactive)
return True