@contextmanager
def catch_warnings():
 warnings.simplefilter('default', category=DeprecationWarning)
 filters = warnings.filters
 warnings.filters = filters[:]
 old_showwarning = warnings.showwarning
 log = []
 def showwarning(message, category, filename, lineno, file=None, line=None):
  log.append(locals())
 try:
  warnings.showwarning = showwarning
  (yield log)
 finally:
  warnings.filters = filters
  warnings.showwarning = old_showwarning

@ contextmanager def catch_warnings ( ) : warnings . simplefilter ( 'default' , category = DeprecationWarning ) filters = warnings . filters warnings . filters = filters [ : ] old_showwarning = warnings . showwarning log = [ ] def showwarning ( message , category , filename , lineno , file = None , line = None ) : log . append ( locals ( ) ) try : warnings . showwarning = showwarning ( yield log ) finally : warnings . filters = filters warnings . showwarning = old_showwarning
catch warnings in a with block in a list .