@contextmanager
def warnings_state(module):
 global __warningregistry__
 for to_clear in (sys, warning_tests):
  try:
   to_clear.__warningregistry__.clear()
  except AttributeError:
   pass
 try:
  __warningregistry__.clear()
 except NameError:
  pass
 original_warnings = warning_tests.warnings
 try:
  warning_tests.warnings = module
  (yield)
 finally:
  warning_tests.warnings = original_warnings

@ contextmanager def warnings_state ( module ) : global __warningregistry__ for to_clear in ( sys , warning_tests ) : try : to_clear . __warningregistry__ . clear ( ) except AttributeError : pass try : __warningregistry__ . clear ( ) except NameError : pass original_warnings = warning_tests . warnings try : warning_tests . warnings = module ( yield ) finally : warning_tests . warnings = original_warnings
use a specific warnings implementation in warning_tests .