From 27cecf4c84d3e64901d3c0eaff9db0f87a2bc434 Mon Sep 17 00:00:00 2001 From: Elizaveta Shashkova Date: Tue, 10 Apr 2018 14:49:21 +0300 Subject: [PATCH] "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. --- .../helpers/pydev/_pydev_bundle/pydev_import_hook.py | 11 +++++++---- python/helpers/pydev/pydev_ipython/matplotlibtools.py | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/python/helpers/pydev/_pydev_bundle/pydev_import_hook.py b/python/helpers/pydev/_pydev_bundle/pydev_import_hook.py index 01199bff9501..e393b09367ec 100644 --- a/python/helpers/pydev/_pydev_bundle/pydev_import_hook.py +++ b/python/helpers/pydev/_pydev_bundle/pydev_import_hook.py @@ -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 diff --git a/python/helpers/pydev/pydev_ipython/matplotlibtools.py b/python/helpers/pydev/pydev_ipython/matplotlibtools.py index f61529523040..f2fc0d561835 100644 --- a/python/helpers/pydev/pydev_ipython/matplotlibtools.py +++ b/python/helpers/pydev/pydev_ipython/matplotlibtools.py @@ -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