<s> """<STR_LIT>""" <EOL> import sys <EOL> import warnings <EOL> class WarningMessage ( object ) : <EOL> """<STR_LIT>""" <EOL> _WARNING_DETAILS = ( "<STR_LIT:message>" , "<STR_LIT>" , "<STR_LIT:filename>" , "<STR_LIT>" , "<STR_LIT:file>" , <EOL> "<STR_LIT>" ) <EOL> def __init__ ( self , message , category , filename , lineno , file = None , <EOL> line = None ) : <EOL> local_values = locals ( ) <EOL> for attr in self . _WARNING_DETAILS : <EOL> setattr ( self , attr , local_values [ attr ] ) <EOL> if category : <EOL> self . _category_name = category . __name__ <EOL> else : <EOL> self . _category_name = None <EOL> def __str__ ( self ) : <EOL> return ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( self . message , self . _category_name , <EOL> self . filename , self . lineno , self . line ) ) <EOL> class WarningManager : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , record = False , module = None ) : <EOL> self . _record = record <EOL> if module is None : <EOL> self . _module = sys . modules [ '<STR_LIT>' ] <EOL> else : <EOL> self . _module = module <EOL> self . _entered = False <EOL> def __enter__ ( self ) : <EOL> if self . _entered : <EOL> raise RuntimeError ( "<STR_LIT>" % self ) <EOL> self . _entered = True <EOL> self . _filters = self . _module . filters <EOL> self . _module . filters = self . _filters [ : ] <EOL> self . _showwarning = self . _module . showwarning <EOL> if self . _record : <EOL> log = [ ] <EOL> def showwarning ( * args , ** kwargs ) : <EOL> log . append ( WarningMessage ( * args , ** kwargs ) ) <EOL> self . _module . showwarning = showwarning <EOL> return log <EOL> else : <EOL> return None <EOL> def __exit__ ( self ) : <EOL> if not self . _entered : <EOL> raise RuntimeError ( "<STR_LIT>" % self ) <EOL> self . _module . filters = self . _filters <EOL> self . _module . showwarning = self . _showwarning <EOL> def assert_warns ( warning_class , func , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> ctx = WarningManager ( record = True ) <EOL> l = ctx . __enter__ ( ) <EOL> warnings . simplefilter ( '<STR_LIT>' ) <EOL> try : <EOL> func ( * args , ** kw ) <EOL> if not len ( l ) > <NUM_LIT:0> : <EOL> raise AssertionError ( "<STR_LIT>" <EOL> % func . __name__ ) <EOL> if not l [ <NUM_LIT:0> ] . category is warning_class : <EOL> raise AssertionError ( "<STR_LIT>" "<STR_LIT>" % ( func . __name__ , warning_class , l [ <NUM_LIT:0> ] ) ) <EOL> finally : <EOL> ctx . __exit__ ( ) </s>
