code: def test_clear_and_catch_warnings():
      # Initial state of module, no warnings
      my_mod = get_fresh_mod(__name__)
      assert getattr(my_mod, "__warningregistry__", {}) == {}
      with clear_and_catch_warnings(modules=[my_mod]):
          warnings.simplefilter("ignore")
          warnings.warn("Some warning")
      assert my_mod.__warningregistry__ == {}
      # Without specified modules, don't clear warnings during context
      with clear_and_catch_warnings():
          warnings.warn("Some warning")
      assert_warn_len_equal(my_mod, 1)
      # Confirm that specifying module keeps old warning, does not add new
      with clear_and_catch_warnings(modules=[my_mod]):
          warnings.warn("Another warning")
      assert_warn_len_equal(my_mod, 1)
      # Another warning, no module spec does add to warnings dict, except on
      # Python 3 (see comments in `assert_warn_len_equal`)
      with clear_and_catch_warnings():
          warnings.warn("Another warning")
      assert_warn_len_equal(my_mod, 2)
  
file_path: nibabel/nibabel/tests/test_testing.py
imports: import os
  import sys
  import warnings
  import numpy
  import pytest
  from ..casting import sctypes
  from ..testing import assert_allclose_safely, assert_re_in, clear_and_catch_warnings, data_path, error_warnings, get_fresh_mod, get_test_data, suppress_warnings
label_desc: Use image node_id 3 for calling a global function with example usage: test_clear_and_catch_warnings() without return types