PageRenderTime 32ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/notify_user/pymodules/python2.7/lib/python/matplotlib-1.5.1-py2.7-linux-x86_64.egg/matplotlib/testing/__init__.py

https://gitlab.com/pooja043/Globus_Docker_4
Python | 107 lines | 83 code | 6 blank | 18 comment | 1 complexity | 9ab60a9f48eab369f0ab7472442b272a MD5 | raw file
  1. from __future__ import (absolute_import, division, print_function,
  2. unicode_literals)
  3. import warnings
  4. from contextlib import contextmanager
  5. from matplotlib.cbook import is_string_like, iterable
  6. from matplotlib import rcParams, rcdefaults, use
  7. def _is_list_like(obj):
  8. """Returns whether the obj is iterable and not a string"""
  9. return not is_string_like(obj) and iterable(obj)
  10. # stolen from pandas
  11. @contextmanager
  12. def assert_produces_warning(expected_warning=Warning, filter_level="always",
  13. clear=None):
  14. """
  15. Context manager for running code that expects to raise (or not raise)
  16. warnings. Checks that code raises the expected warning and only the
  17. expected warning. Pass ``False`` or ``None`` to check that it does *not*
  18. raise a warning. Defaults to ``exception.Warning``, baseclass of all
  19. Warnings. (basically a wrapper around ``warnings.catch_warnings``).
  20. >>> import warnings
  21. >>> with assert_produces_warning():
  22. ... warnings.warn(UserWarning())
  23. ...
  24. >>> with assert_produces_warning(False):
  25. ... warnings.warn(RuntimeWarning())
  26. ...
  27. Traceback (most recent call last):
  28. ...
  29. AssertionError: Caused unexpected warning(s): ['RuntimeWarning'].
  30. >>> with assert_produces_warning(UserWarning):
  31. ... warnings.warn(RuntimeWarning())
  32. Traceback (most recent call last):
  33. ...
  34. AssertionError: Did not see expected warning of class 'UserWarning'.
  35. ..warn:: This is *not* thread-safe.
  36. """
  37. with warnings.catch_warnings(record=True) as w:
  38. if clear is not None:
  39. # make sure that we are clearning these warnings
  40. # if they have happened before
  41. # to guarantee that we will catch them
  42. if not _is_list_like(clear):
  43. clear = [clear]
  44. for m in clear:
  45. try:
  46. m.__warningregistry__.clear()
  47. except:
  48. pass
  49. saw_warning = False
  50. warnings.simplefilter(filter_level)
  51. yield w
  52. extra_warnings = []
  53. for actual_warning in w:
  54. if (expected_warning and issubclass(actual_warning.category,
  55. expected_warning)):
  56. saw_warning = True
  57. else:
  58. extra_warnings.append(actual_warning.category.__name__)
  59. if expected_warning:
  60. assert saw_warning, ("Did not see expected warning of class %r."
  61. % expected_warning.__name__)
  62. assert not extra_warnings, ("Caused unexpected warning(s): %r."
  63. % extra_warnings)
  64. def setup():
  65. # The baseline images are created in this locale, so we should use
  66. # it during all of the tests.
  67. import locale
  68. import warnings
  69. from matplotlib.backends import backend_agg, backend_pdf, backend_svg
  70. try:
  71. locale.setlocale(locale.LC_ALL, str('en_US.UTF-8'))
  72. except locale.Error:
  73. try:
  74. locale.setlocale(locale.LC_ALL, str('English_United States.1252'))
  75. except locale.Error:
  76. warnings.warn(
  77. "Could not set locale to English/United States. "
  78. "Some date-related tests may fail")
  79. use('Agg', warn=False) # use Agg backend for these tests
  80. # These settings *must* be hardcoded for running the comparison
  81. # tests and are not necessarily the default values as specified in
  82. # rcsetup.py
  83. rcdefaults() # Start with all defaults
  84. rcParams['font.family'] = 'Bitstream Vera Sans'
  85. rcParams['text.hinting'] = False
  86. rcParams['text.hinting_factor'] = 8
  87. # Clear the font caches. Otherwise, the hinting mode can travel
  88. # from one test to another.
  89. backend_agg.RendererAgg._fontd.clear()
  90. backend_pdf.RendererPdf.truetype_font_cache.clear()
  91. backend_svg.RendererSVG.fontd.clear()