<s> import sys <EOL> import traceback <EOL> import linecache <EOL> from collections import namedtuple <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class ExceptionCauseMixin ( Exception ) : <EOL> """<STR_LIT>""" <EOL> cause = None <EOL> def __new__ ( cls , * args , ** kw ) : <EOL> cause = None <EOL> if args and isinstance ( args [ <NUM_LIT:0> ] , Exception ) : <EOL> cause , args = args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> : ] <EOL> ret = super ( ExceptionCauseMixin , cls ) . __new__ ( cls , * args , ** kw ) <EOL> ret . cause = cause <EOL> if cause is None : <EOL> return ret <EOL> root_cause = getattr ( cause , '<STR_LIT>' , None ) <EOL> if root_cause is None : <EOL> ret . root_cause = cause <EOL> else : <EOL> ret . root_cause = root_cause <EOL> full_trace = getattr ( cause , '<STR_LIT>' , None ) <EOL> if full_trace is not None : <EOL> ret . full_trace = list ( full_trace ) <EOL> ret . _tb = list ( cause . _tb ) <EOL> ret . _stack = list ( cause . _stack ) <EOL> return ret <EOL> try : <EOL> exc_type , exc_value , exc_tb = sys . exc_info ( ) <EOL> if exc_type is None and exc_value is None : <EOL> return ret <EOL> if cause is exc_value or root_cause is exc_value : <EOL> ret . _tb = _extract_from_tb ( exc_tb ) <EOL> ret . _stack = _extract_from_frame ( exc_tb . tb_frame ) <EOL> ret . full_trace = ret . _stack [ : - <NUM_LIT:1> ] + ret . _tb <EOL> finally : <EOL> del exc_tb <EOL> return ret <EOL> def get_str ( self ) : <EOL> """<STR_LIT>""" <EOL> ret = [ ] <EOL> trace_str = self . _get_trace_str ( ) <EOL> if trace_str : <EOL> ret . extend ( [ '<STR_LIT>' , trace_str ] ) <EOL> ret . append ( self . _get_exc_str ( ) ) <EOL> return '<STR_LIT>' . join ( ret ) <EOL> def _get_message ( self ) : <EOL> args = getattr ( self , '<STR_LIT:args>' , [ ] ) <EOL> if self . cause : <EOL> args = args [ <NUM_LIT:1> : ] <EOL> if args and args [ <NUM_LIT:0> ] : <EOL> return args [ <NUM_LIT:0> ] <EOL> return '<STR_LIT>' <EOL> def _get_trace_str ( self ) : <EOL> if not self . cause : <EOL> return super ( ExceptionCauseMixin , self ) . __repr__ ( ) <EOL> if self . full_trace : <EOL> return '<STR_LIT>' . join ( traceback . format_list ( self . full_trace ) ) <EOL> return '<STR_LIT>' <EOL> def _get_exc_str ( self , incl_name = True ) : <EOL> cause_str = _format_exc ( self . root_cause ) <EOL> message = self . _get_message ( ) <EOL> ret = [ ] <EOL> if incl_name : <EOL> ret = [ self . __class__ . __name__ , '<STR_LIT>' ] <EOL> if message : <EOL> ret . extend ( [ message , '<STR_LIT>' , cause_str , '<STR_LIT:)>' ] ) <EOL> else : <EOL> ret . extend ( [ '<STR_LIT>' , cause_str ] ) <EOL> return '<STR_LIT>' . join ( ret ) <EOL> def __str__ ( self ) : <EOL> if not self . cause : <EOL> return super ( ExceptionCauseMixin , self ) . __str__ ( ) <EOL> trace_str = self . _get_trace_str ( ) <EOL> ret = [ ] <EOL> if trace_str : <EOL> message = self . _get_message ( ) <EOL> if message : <EOL> ret . extend ( [ message , '<STR_LIT>' ] ) <EOL> ret . extend ( [ '<STR_LIT>' , <EOL> trace_str , <EOL> self . _get_exc_str ( incl_name = True ) ] ) <EOL> return '<STR_LIT>' . join ( ret ) <EOL> else : <EOL> return self . _get_exc_str ( incl_name = False ) <EOL> def _format_exc ( exc , message = None ) : <EOL> if message is None : <EOL> message = exc <EOL> exc_str = traceback . _format_final_exc_line ( exc . __class__ . __name__ , message ) <EOL> return exc_str . rstrip ( ) <EOL> _BaseTBItem = namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class _TBItem ( _BaseTBItem ) : <EOL> def __repr__ ( self ) : <EOL> ret = super ( _TBItem , self ) . __repr__ ( ) <EOL> ret += '<STR_LIT>' % self . frame_id <EOL> return ret <EOL> class _DeferredLine ( object ) : <EOL> def __init__ ( self , filename , lineno , module_globals = None ) : <EOL> self . filename = filename <EOL> self . lineno = lineno <EOL> module_globals = module_globals or { } <EOL> self . module_globals = dict ( [ ( k , v ) for k , v in module_globals . items ( ) <EOL> if k in ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> def __eq__ ( self , other ) : <EOL> return ( self . lineno , self . filename ) == ( other . lineno , other . filename ) <EOL> def __ne__ ( self , other ) : <EOL> return ( self . lineno , self . filename ) != ( other . lineno , other . filename ) <EOL> def __str__ ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . _line <EOL> linecache . checkcache ( self . filename ) <EOL> line = linecache . getline ( self . filename , <EOL> self . lineno , <EOL> self . module_globals ) <EOL> if line : <EOL> line = line . strip ( ) <EOL> else : <EOL> line = None <EOL> self . _line = line <EOL> return line <EOL> def __repr__ ( self ) : <EOL> return repr ( str ( self ) ) <EOL> def __len__ ( self ) : <EOL> return len ( str ( self ) ) <EOL> def strip ( self ) : <EOL> return str ( self ) . strip ( ) <EOL> def _extract_from_frame ( f = None , limit = None ) : <EOL> ret = [ ] <EOL> if f is None : <EOL> f = sys . _getframe ( <NUM_LIT:1> ) <EOL> if limit is None : <EOL> limit = getattr ( sys , '<STR_LIT>' , <NUM_LIT:1000> ) <EOL> n = <NUM_LIT:0> <EOL> while f is not None and n < limit : <EOL> filename = f . f_code . co_filename <EOL> lineno = f . f_lineno <EOL> name = f . f_code . co_name <EOL> line = _DeferredLine ( filename , lineno , f . f_globals ) <EOL> item = _TBItem ( filename , lineno , name , line ) <EOL> item . frame_id = id ( f ) <EOL> ret . append ( item ) <EOL> f = f . f_back <EOL> n += <NUM_LIT:1> <EOL> ret . reverse ( ) <EOL> return ret <EOL> def _extract_from_tb ( tb , limit = None ) : <EOL> ret = [ ] <EOL> if limit is None : <EOL> limit = getattr ( sys , '<STR_LIT>' , <NUM_LIT:1000> ) <EOL> n = <NUM_LIT:0> <EOL> while tb is not None and n < limit : <EOL> filename = tb . tb_frame . f_code . co_filename <EOL> lineno = tb . tb_lineno <EOL> name = tb . tb_frame . f_code . co_name <EOL> line = _DeferredLine ( filename , lineno , tb . tb_frame . f_globals ) <EOL> item = _TBItem ( filename , lineno , name , line ) <EOL> item . frame_id = id ( tb . tb_frame ) <EOL> ret . append ( item ) <EOL> tb = tb . tb_next <EOL> n += <NUM_LIT:1> <EOL> return ret <EOL> class MathError ( ExceptionCauseMixin , ValueError ) : <EOL> pass <EOL> def whoops_math ( ) : <EOL> return <NUM_LIT:1> / <NUM_LIT:0> <EOL> def math_lol ( n = <NUM_LIT:0> ) : <EOL> if n < <NUM_LIT:3> : <EOL> return math_lol ( n = n + <NUM_LIT:1> ) <EOL> try : <EOL> return whoops_math ( ) <EOL> except ZeroDivisionError as zde : <EOL> exc = MathError ( zde , '<STR_LIT>' ) <EOL> raise exc <EOL> def main ( ) : <EOL> try : <EOL> math_lol ( ) <EOL> except ValueError as me : <EOL> exc = MathError ( me , '<STR_LIT>' ) <EOL> raise exc <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> try : <EOL> main ( ) <EOL> except Exception : <EOL> import pdb ; pdb . post_mortem ( ) <EOL> raise </s>
