hook-matplotlib.backends.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2013-2021, PyInstaller Development Team.
  3. #
  4. # Distributed under the terms of the GNU General Public License (version 2
  5. # or later) with exception for distributing the bootloader.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  10. #-----------------------------------------------------------------------------
  11. from PyInstaller.compat import is_darwin
  12. from PyInstaller.utils.hooks import eval_statement, exec_statement, logger
  13. def get_matplotlib_backend_module_names():
  14. """
  15. List the names of all matplotlib backend modules importable under the current Python installation.
  16. Returns
  17. ----------
  18. list
  19. List of the fully-qualified names of all such modules.
  20. """
  21. # Statement safely importing a single backend module.
  22. import_statement = """
  23. import os, sys
  24. # Preserve stdout.
  25. sys_stdout = sys.stdout
  26. try:
  27. # Redirect output printed by this importation to "/dev/null", preventing such output from being erroneously
  28. # interpreted as an error.
  29. with open(os.devnull, 'w') as dev_null:
  30. sys.stdout = dev_null
  31. __import__('%s')
  32. # If this is an ImportError, print this exception's message without a traceback. ImportError messages are human-readable
  33. # and require no additional context.
  34. except ImportError as exc:
  35. sys.stdout = sys_stdout
  36. print(exc)
  37. # Else, print this exception preceded by a traceback. traceback.print_exc() prints to stderr rather than stdout and must
  38. # not be called here!
  39. except Exception:
  40. sys.stdout = sys_stdout
  41. import traceback
  42. print(traceback.format_exc())
  43. """
  44. # List of the human-readable names of all available backends.
  45. backend_names = eval_statement('import matplotlib; print(matplotlib.rcsetup.all_backends)')
  46. # List of the fully-qualified names of all importable backend modules.
  47. module_names = []
  48. # If the current system is not OS X and the "CocoaAgg" backend is available, remove this backend from consideration.
  49. # Attempting to import this backend on non-OS X systems halts the current subprocess without printing output or
  50. # raising exceptions, preventing its reliable detection.
  51. if not is_darwin and 'CocoaAgg' in backend_names:
  52. backend_names.remove('CocoaAgg')
  53. # For safety, attempt to import each backend in a unique subprocess.
  54. for backend_name in backend_names:
  55. module_name = 'matplotlib.backends.backend_%s' % backend_name.lower()
  56. stdout = exec_statement(import_statement % module_name)
  57. # If no output was printed, this backend is importable.
  58. if not stdout:
  59. module_names.append(module_name)
  60. logger.info(' Matplotlib backend "%s": added' % backend_name)
  61. else:
  62. logger.info(' Matplotlib backend "%s": ignored\n %s' % (backend_name, stdout))
  63. return module_names
  64. # Freeze all importable backends, as PyInstaller is unable to determine exactly which backends are required by the
  65. # current program.
  66. hiddenimports = get_matplotlib_backend_module_names()