<s> """<STR_LIT>""" <EOL> import ast <EOL> import sys <EOL> import _pytest . _code <EOL> import py <EOL> from _pytest . assertion import util <EOL> u = py . builtin . _totext <EOL> class AssertionError ( util . BuiltinAssertionError ) : <EOL> def __init__ ( self , * args ) : <EOL> util . BuiltinAssertionError . __init__ ( self , * args ) <EOL> if args : <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> toprint = args <EOL> else : <EOL> toprint = args [ <NUM_LIT:0> ] <EOL> try : <EOL> self . msg = u ( toprint ) <EOL> except Exception : <EOL> self . msg = u ( <EOL> "<STR_LIT>" <EOL> % ( toprint . __class__ , id ( toprint ) ) ) <EOL> else : <EOL> f = _pytest . _code . Frame ( sys . _getframe ( <NUM_LIT:1> ) ) <EOL> try : <EOL> source = f . code . fullsource <EOL> if source is not None : <EOL> try : <EOL> source = source . getstatement ( f . lineno , assertion = True ) <EOL> except IndexError : <EOL> source = None <EOL> else : <EOL> source = str ( source . deindent ( ) ) . strip ( ) <EOL> except py . error . ENOENT : <EOL> source = None <EOL> if source : <EOL> self . msg = reinterpret ( source , f , should_fail = True ) <EOL> else : <EOL> self . msg = "<STR_LIT>" <EOL> if not self . args : <EOL> self . args = ( self . msg , ) <EOL> if sys . version_info > ( <NUM_LIT:3> , <NUM_LIT:0> ) : <EOL> AssertionError . __module__ = "<STR_LIT>" <EOL> if sys . platform . startswith ( "<STR_LIT>" ) : <EOL> _exprs = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:Name>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ) <EOL> _stmts = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> _expr_nodes = set ( getattr ( ast , name ) for name in _exprs ) <EOL> _stmt_nodes = set ( getattr ( ast , name ) for name in _stmts ) <EOL> def _is_ast_expr ( node ) : <EOL> return node . __class__ in _expr_nodes <EOL> def _is_ast_stmt ( node ) : <EOL> return node . __class__ in _stmt_nodes <EOL> else : <EOL> def _is_ast_expr ( node ) : <EOL> return isinstance ( node , ast . expr ) <EOL> def _is_ast_stmt ( node ) : <EOL> return isinstance ( node , ast . stmt ) <EOL> try : <EOL> _Starred = ast . Starred <EOL> except AttributeError : <EOL> class _Starred ( object ) : pass <EOL> class Failure ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , explanation = "<STR_LIT>" ) : <EOL> self . cause = sys . exc_info ( ) <EOL> self . explanation = explanation <EOL> def reinterpret ( source , frame , should_fail = False ) : <EOL> mod = ast . parse ( source ) <EOL> visitor = DebugInterpreter ( frame ) <EOL> try : <EOL> visitor . visit ( mod ) <EOL> except Failure : <EOL> failure = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> return getfailure ( failure ) <EOL> if should_fail : <EOL> return ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def run ( offending_line , frame = None ) : <EOL> if frame is None : <EOL> frame = _pytest . _code . Frame ( sys . _getframe ( <NUM_LIT:1> ) ) <EOL> return reinterpret ( offending_line , frame ) <EOL> def getfailure ( e ) : <EOL> explanation = util . format_explanation ( e . explanation ) <EOL> value = e . cause [ <NUM_LIT:1> ] <EOL> if str ( value ) : <EOL> lines = explanation . split ( '<STR_LIT:\n>' ) <EOL> lines [ <NUM_LIT:0> ] += "<STR_LIT>" % ( value , ) <EOL> explanation = '<STR_LIT:\n>' . join ( lines ) <EOL> text = "<STR_LIT>" % ( e . cause [ <NUM_LIT:0> ] . __name__ , explanation ) <EOL> if text . startswith ( '<STR_LIT>' ) : <EOL> text = text [ <NUM_LIT:16> : ] <EOL> return text <EOL> operator_map = { <EOL> ast . BitOr : "<STR_LIT:|>" , <EOL> ast . BitXor : "<STR_LIT>" , <EOL> ast . BitAnd : "<STR_LIT:&>" , <EOL> ast . LShift : "<STR_LIT>" , <EOL> ast . RShift : "<STR_LIT>" , <EOL> ast . Add : "<STR_LIT:+>" , <EOL> ast . Sub : "<STR_LIT:->" , <EOL> ast . Mult : "<STR_LIT:*>" , <EOL> ast . Div : "<STR_LIT:/>" , <EOL> ast . FloorDiv : "<STR_LIT>" , <EOL> ast . Mod : "<STR_LIT:%>" , <EOL> ast . Eq : "<STR_LIT>" , <EOL> ast . NotEq : "<STR_LIT>" , <EOL> ast . Lt : "<STR_LIT:<>" , <EOL> ast . LtE : "<STR_LIT>" , <EOL> ast . Gt : "<STR_LIT:>>" , <EOL> ast . GtE : "<STR_LIT>" , <EOL> ast . Pow : "<STR_LIT>" , <EOL> ast . Is : "<STR_LIT>" , <EOL> ast . IsNot : "<STR_LIT>" , <EOL> ast . In : "<STR_LIT>" , <EOL> ast . NotIn : "<STR_LIT>" <EOL> } <EOL> unary_map = { <EOL> ast . Not : "<STR_LIT>" , <EOL> ast . Invert : "<STR_LIT>" , <EOL> ast . USub : "<STR_LIT>" , <EOL> ast . UAdd : "<STR_LIT>" <EOL> } <EOL> class DebugInterpreter ( ast . NodeVisitor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , frame ) : <EOL> self . frame = frame <EOL> def generic_visit ( self , node ) : <EOL> if _is_ast_expr ( node ) : <EOL> mod = ast . Expression ( node ) <EOL> co = self . _compile ( mod ) <EOL> try : <EOL> result = self . frame . eval ( co ) <EOL> except Exception : <EOL> raise Failure ( ) <EOL> explanation = self . frame . repr ( result ) <EOL> return explanation , result <EOL> elif _is_ast_stmt ( node ) : <EOL> mod = ast . Module ( [ node ] ) <EOL> co = self . _compile ( mod , "<STR_LIT>" ) <EOL> try : <EOL> self . frame . exec_ ( co ) <EOL> except Exception : <EOL> raise Failure ( ) <EOL> return None , None <EOL> else : <EOL> raise AssertionError ( "<STR_LIT>" % ( node , ) ) <EOL> def _compile ( self , source , mode = "<STR_LIT>" ) : <EOL> return compile ( source , "<STR_LIT>" , mode ) <EOL> def visit_Expr ( self , expr ) : <EOL> return self . visit ( expr . value ) <EOL> def visit_Module ( self , mod ) : <EOL> for stmt in mod . body : <EOL> self . visit ( stmt ) <EOL> def visit_Name ( self , name ) : <EOL> explanation , result = self . generic_visit ( name ) <EOL> source = "<STR_LIT>" % ( name . id , ) <EOL> co = self . _compile ( source ) <EOL> try : <EOL> local = self . frame . eval ( co ) <EOL> except Exception : <EOL> local = None <EOL> if local is None or not self . frame . is_true ( local ) : <EOL> return name . id , result <EOL> return explanation , result <EOL> def visit_Compare ( self , comp ) : <EOL> left = comp . left <EOL> left_explanation , left_result = self . visit ( left ) <EOL> for op , next_op in zip ( comp . ops , comp . comparators ) : <EOL> next_explanation , next_result = self . visit ( next_op ) <EOL> op_symbol = operator_map [ op . __class__ ] <EOL> explanation = "<STR_LIT>" % ( left_explanation , op_symbol , <EOL> next_explanation ) <EOL> source = "<STR_LIT>" % ( op_symbol , ) <EOL> co = self . _compile ( source ) <EOL> try : <EOL> result = self . frame . eval ( co , __exprinfo_left = left_result , <EOL> __exprinfo_right = next_result ) <EOL> except Exception : <EOL> raise Failure ( explanation ) <EOL> try : <EOL> if not self . frame . is_true ( result ) : <EOL> break <EOL> except KeyboardInterrupt : <EOL> raise <EOL> except : <EOL> break <EOL> left_explanation , left_result = next_explanation , next_result <EOL> if util . _reprcompare is not None : <EOL> res = util . _reprcompare ( op_symbol , left_result , next_result ) <EOL> if res : <EOL> explanation = res <EOL> return explanation , result <EOL> def visit_BoolOp ( self , boolop ) : <EOL> is_or = isinstance ( boolop . op , ast . Or ) <EOL> explanations = [ ] <EOL> for operand in boolop . values : <EOL> explanation , result = self . visit ( operand ) <EOL> explanations . append ( explanation ) <EOL> if result == is_or : <EOL> break <EOL> name = is_or and "<STR_LIT>" or "<STR_LIT>" <EOL> explanation = "<STR_LIT:(>" + name . join ( explanations ) + "<STR_LIT:)>" <EOL> return explanation , result <EOL> def visit_UnaryOp ( self , unary ) : <EOL> pattern = unary_map [ unary . op . __class__ ] <EOL> operand_explanation , operand_result = self . visit ( unary . operand ) <EOL> explanation = pattern % ( operand_explanation , ) <EOL> co = self . _compile ( pattern % ( "<STR_LIT>" , ) ) <EOL> try : <EOL> result = self . frame . eval ( co , __exprinfo_expr = operand_result ) <EOL> except Exception : <EOL> raise Failure ( explanation ) <EOL> return explanation , result <EOL> def visit_BinOp ( self , binop ) : <EOL> left_explanation , left_result = self . visit ( binop . left ) <EOL> right_explanation , right_result = self . visit ( binop . right ) <EOL> symbol = operator_map [ binop . op . __class__ ] <EOL> explanation = "<STR_LIT>" % ( left_explanation , symbol , <EOL> right_explanation ) <EOL> source = "<STR_LIT>" % ( symbol , ) <EOL> co = self . _compile ( source ) <EOL> try : <EOL> result = self . frame . eval ( co , __exprinfo_left = left_result , <EOL> __exprinfo_right = right_result ) <EOL> except Exception : <EOL> raise Failure ( explanation ) <EOL> return explanation , result <EOL> def visit_Call ( self , call ) : <EOL> func_explanation , func = self . visit ( call . func ) <EOL> arg_explanations = [ ] <EOL> ns = { "<STR_LIT>" : func } <EOL> arguments = [ ] <EOL> for arg in call . args : <EOL> arg_explanation , arg_result = self . visit ( arg ) <EOL> if isinstance ( arg , _Starred ) : <EOL> arg_name = "<STR_LIT>" <EOL> ns [ arg_name ] = arg_result <EOL> arguments . append ( "<STR_LIT>" % ( arg_name , ) ) <EOL> arg_explanations . append ( "<STR_LIT>" % ( arg_explanation , ) ) <EOL> else : <EOL> arg_name = "<STR_LIT>" % ( len ( ns ) , ) <EOL> ns [ arg_name ] = arg_result <EOL> arguments . append ( arg_name ) <EOL> arg_explanations . append ( arg_explanation ) <EOL> for keyword in call . keywords : <EOL> arg_explanation , arg_result = self . visit ( keyword . value ) <EOL> if keyword . arg : <EOL> arg_name = "<STR_LIT>" % ( len ( ns ) , ) <EOL> keyword_source = "<STR_LIT>" % ( keyword . arg ) <EOL> arguments . append ( keyword_source % ( arg_name , ) ) <EOL> arg_explanations . append ( keyword_source % ( arg_explanation , ) ) <EOL> else : <EOL> arg_name = "<STR_LIT>" <EOL> arguments . append ( "<STR_LIT>" % ( arg_name , ) ) <EOL> arg_explanations . append ( "<STR_LIT>" % ( arg_explanation , ) ) <EOL> ns [ arg_name ] = arg_result <EOL> if getattr ( call , '<STR_LIT>' , None ) : <EOL> arg_explanation , arg_result = self . visit ( call . starargs ) <EOL> arg_name = "<STR_LIT>" <EOL> ns [ arg_name ] = arg_result <EOL> arguments . append ( "<STR_LIT>" % ( arg_name , ) ) <EOL> arg_explanations . append ( "<STR_LIT>" % ( arg_explanation , ) ) <EOL> if getattr ( call , '<STR_LIT>' , None ) : <EOL> arg_explanation , arg_result = self . visit ( call . kwargs ) <EOL> arg_name = "<STR_LIT>" <EOL> ns [ arg_name ] = arg_result <EOL> arguments . append ( "<STR_LIT>" % ( arg_name , ) ) <EOL> arg_explanations . append ( "<STR_LIT>" % ( arg_explanation , ) ) <EOL> args_explained = "<STR_LIT:U+002CU+0020>" . join ( arg_explanations ) <EOL> explanation = "<STR_LIT>" % ( func_explanation , args_explained ) <EOL> args = "<STR_LIT:U+002CU+0020>" . join ( arguments ) <EOL> source = "<STR_LIT>" % ( args , ) <EOL> co = self . _compile ( source ) <EOL> try : <EOL> result = self . frame . eval ( co , ** ns ) <EOL> except Exception : <EOL> raise Failure ( explanation ) <EOL> pattern = "<STR_LIT>" <EOL> rep = self . frame . repr ( result ) <EOL> explanation = pattern % ( rep , rep , explanation ) <EOL> return explanation , result <EOL> def _is_builtin_name ( self , name ) : <EOL> pattern = "<STR_LIT>" <EOL> source = pattern % ( name . id , name . id ) <EOL> co = self . _compile ( source ) <EOL> try : <EOL> return self . frame . eval ( co ) <EOL> except Exception : <EOL> return False <EOL> def visit_Attribute ( self , attr ) : <EOL> if not isinstance ( attr . ctx , ast . Load ) : <EOL> return self . generic_visit ( attr ) <EOL> source_explanation , source_result = self . visit ( attr . value ) <EOL> explanation = "<STR_LIT>" % ( source_explanation , attr . attr ) <EOL> source = "<STR_LIT>" % ( attr . attr , ) <EOL> co = self . _compile ( source ) <EOL> try : <EOL> try : <EOL> result = self . frame . eval ( co , __exprinfo_expr = source_result ) <EOL> except AttributeError : <EOL> if not attr . attr . startswith ( "<STR_LIT>" ) or attr . attr . endswith ( "<STR_LIT>" ) : <EOL> raise <EOL> source = "<STR_LIT>" <EOL> co = self . _compile ( source ) <EOL> class_name = self . frame . eval ( co , __exprinfo_expr = source_result ) <EOL> mangled_attr = "<STR_LIT:_>" + class_name + attr . attr <EOL> source = "<STR_LIT>" % ( mangled_attr , ) <EOL> co = self . _compile ( source ) <EOL> result = self . frame . eval ( co , __exprinfo_expr = source_result ) <EOL> except Exception : <EOL> raise Failure ( explanation ) <EOL> explanation = "<STR_LIT>" % ( self . frame . repr ( result ) , <EOL> self . frame . repr ( result ) , <EOL> source_explanation , attr . attr ) <EOL> source = "<STR_LIT>" <EOL> source = source % ( attr . attr , ) <EOL> co = self . _compile ( source ) <EOL> try : <EOL> from_instance = self . frame . eval ( co , __exprinfo_expr = source_result ) <EOL> except Exception : <EOL> from_instance = None <EOL> if from_instance is None or self . frame . is_true ( from_instance ) : <EOL> rep = self . frame . repr ( result ) <EOL> pattern = "<STR_LIT>" <EOL> explanation = pattern % ( rep , rep , explanation ) <EOL> return explanation , result <EOL> def visit_Assert ( self , assrt ) : <EOL> test_explanation , test_result = self . visit ( assrt . test ) <EOL> explanation = "<STR_LIT>" % ( test_explanation , ) <EOL> if not self . frame . is_true ( test_result ) : <EOL> try : <EOL> raise util . BuiltinAssertionError <EOL> except Exception : <EOL> raise Failure ( explanation ) <EOL> return explanation , test_result <EOL> def visit_Assign ( self , assign ) : <EOL> value_explanation , value_result = self . visit ( assign . value ) <EOL> explanation = "<STR_LIT>" % ( value_explanation , ) <EOL> name = ast . Name ( "<STR_LIT>" , ast . Load ( ) , <EOL> lineno = assign . value . lineno , <EOL> col_offset = assign . value . col_offset ) <EOL> new_assign = ast . Assign ( assign . targets , name , lineno = assign . lineno , <EOL> col_offset = assign . col_offset ) <EOL> mod = ast . Module ( [ new_assign ] ) <EOL> co = self . _compile ( mod , "<STR_LIT>" ) <EOL> try : <EOL> self . frame . exec_ ( co , __exprinfo_expr = value_result ) <EOL> except Exception : <EOL> raise Failure ( explanation ) <EOL> return explanation , value_result </s>
