
    a,                     0   d Z ddlZddlZddlZddlmZmZ d Zd Z G d de	      Z
d'dZd(d	Zd
 Zi ej                  dej                  dej                   dej"                  dej$                  dej&                  dej(                  dej*                  dej,                  dej.                  dej0                  dej2                  dej4                  dej6                  dej8                  dej:                  dej<                  dej>                  dej@                  dejB                  dejD                  dejF                  d iZ$ejJ                  d!ejL                  d"ejN                  d#ejP                  d$iZ) G d% d&ejT                        Z+y))z
Find intermediate evalutation results in assert statements through builtin AST.
This should replace _assertionold.py eventually.
    N)_format_explanationBuiltinAssertionErrorc                 6    t        | t        j                        S N)
isinstanceastexprnodes    8/usr/lib/python3/dist-packages/py/_code/_assertionnew.py_is_ast_exprr          dCHH%%    c                 6    t        | t        j                        S r   )r   r   stmtr
   s    r   _is_ast_stmtr      r   r   c                       e Zd ZdZddZy)Failurez#Error found while interpreting AST.c                 D    t        j                         | _        || _        y r   )sysexc_infocauseexplanation)selfr   s     r   __init__zFailure.__init__   s    \\^
&r   N) )__name__
__module____qualname____doc__r    r   r   r   r      s
    -'r   r   c                     t        j                  |       }t        |      }	 |j                  |       |ryy # t        $ r% t        j                         d   }t        |      cY S w xY w)N   z(assertion failed, but when it was re-run for printing intermediate values, it did not fail.  Suggestions: compute assert expression before the assert or use --no-assert))r   parseDebugInterpretervisitr   r   r   
getfailure)sourceframeshould_failmodvisitorfailures         r   	interpretr.      sg    
))F
Cu%G#c R   #,,.#'""#s   7 +A%$A%c                     |2t         j                  j                  t        j                  d            }t        | |      S )Nr#   )pycodeFramer   	_getframer.   )offending_liner)   s     r   runr5   (   s/    }cmmA./^U++r   c                 T   t        | j                        }| j                  d   }t        |      rD|j	                         }|s|j                  d       |dxx   d|z  cc<   dj                  |      }| j                  d   j                  d|}|j                  d      r|dd  }|S )	Nr#   r   r   z << 
z: zAssertionError: assert    )	r   r   r   str
splitlinesappendjoinr   
startswith)r-   r   valuelinestexts        r   r'   r'   -   s    %g&9&9:KMM!E
5z&&(LLa''ii&}}Q'00+>D01BCyKr   |^&z<<z>>+-*/z//%z==z!=<z<=>z>=**iszis notinznot inznot %sz~%sz-%sz+%sc                   l    e Zd ZdZd Zd ZddZd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zy)r%   z;Interpret AST nodes to gleam useful debugging information. c                     || _         y r   )r)   )r   r)   s     r   r   zDebugInterpreter.__init__`   s	    
r   c                    t        |      rat        j                  |      }| j                  |      }	 | j                  j                  |      }| j                  j                  |      }||fS t        |      rEt        j                  |g      }| j                  |d      }	 | j                  j                  |       yt        d|      # t        $ r t               w xY w# t        $ r t               w xY w)Nexec)NNzcan't handle )r   r   
Expression_compiler)   eval	Exceptionr   reprr   Moduleexec_AssertionError)r   r   r+   coresultr   s         r   generic_visitzDebugInterpreter.generic_visitc   s    ..&Cs#B , **//&1K&&$**dV$CsF+B 

  $  T!;<<   i    i s   C !C" C"C6c                     t        |d|      S )Nz<assertion interpretation>)compile)r   r(   modes      r   rS   zDebugInterpreter._compiley   s    v;TBBr   c                 8    | j                  |j                        S r   )r&   r>   )r   r	   s     r   
visit_ExprzDebugInterpreter.visit_Expr|   s    zz$**%%r   c                 H    |j                   D ]  }| j                  |        y r   )bodyr&   )r   r+   r   s      r   visit_ModulezDebugInterpreter.visit_Module   s!    HH 	DJJt	r   c                     | j                  |      \  }}|j                  d}| j                  |      }	 | j                  j	                  |      }|s|j                  |fS ||fS # t
        $ r d}Y !w xY w)Nz in locals() is not globals()F)r\   idrS   r)   rT   rU   )r   namer   r[   r(   rZ   locals          r   
visit_NamezDebugInterpreter.visit_Name   s|    "006V6:gg?]]6"	JJOOB'E 77F?"F""  	E	s   A% %A32A3c                 "   |j                   }| j                  |      \  }}t        |j                  |j                        D ]v  \  }}| j                  |      \  }}t
        |j                     }	|d|	d|}
d|	d}| j                  |      }	 | j                  j                  |||      }	 |s n	 ||}}x t        j                  j                  }|r |	|      }|r|}

fS # t        $ r t        |
      w xY w# t        $ r   Y  TxY w)N __exprinfo_left  __exprinfo_right__exprinfo_left__exprinfo_right)leftr&   zipopscomparatorsoperator_map	__class__rS   r)   rT   rU   r   KeyboardInterruptr0   r1   _reprcompare)r   comprq   left_explanationleft_resultopnext_opnext_explanationnext_result	op_symbolr   r(   rZ   r[   rcompress                   r   visit_ComparezDebugInterpreter.visit_Compare   s3   yy(,

4(8%+txx)9)9: 	JKB,0JJw,?)k$R\\2I(8)(8:K>GIFv&B+[:E ) G  -=kk'	J* $$	;<C!F""!  +k**+
 % s   C(/D (C= Dc                    t        |j                  t        j                        }g }|j                  D ].  }| j                  |      \  }}|j                  |       ||k(  s. n |xr dxs d}d|j                  |      z   dz   }|fS )Nz or z and ())r   r|   r   Orvaluesr&   r;   r<   )r   boolopis_orexplanationsoperandr   r[   rg   s           r   visit_BoolOpzDebugInterpreter.visit_BoolOp   s    699cff-}} 	G"&**W"5K,		
 *7DIIl33c9F""r   c                     t         |j                  j                     }| j                  |j                        \  }}||fz  }| j                  |dz        }	 | j                  j                  ||      }||fS # t        $ r t        |      w xY w)N__exprinfo_expr)
	unary_mapr|   rv   r&   r   rS   r)   rT   rU   r   )r   unarypatternoperand_explanationoperand_resultr   rZ   r[   s           r   visit_UnaryOpzDebugInterpreter.visit_UnaryOp   s    EHH../.2jj.G+^!4 66]]7%99:	'ZZ__R_HF F""  	'+&&	's   A8 8Bc                 p   | j                  |j                        \  }}| j                  |j                        \  }}t        |j                  j
                     }d|d|d|d}d|d}| j                  |      }		 | j                  j                  |	||      }
||
fS # t        $ r t        |      w xY w)Nr   rk   r   rl   rm   rn   )r&   rq   rightru   r|   rv   rS   r)   rT   rU   r   )r   binoprz   r{   right_explanationright_resultsymbolr   r(   rZ   r[   s              r   visit_BinOpzDebugInterpreter.visit_BinOp   s    (,

5::(>%+*.**U[[*A'<ehh001&6&79:@B]]6"	'ZZ__R6B % DF F""  	'+&&	's   >B   B5c                    | j                  |j                        \  }}g }d|i}g }|j                  D ]K  }| j                  |      \  }}	dt        |      }
|	||
<   |j	                  |
       |j	                  |       M |j
                  D ]l  }| j                  |j                        \  }}	dt        |      }
|	||
<   d|j                  z  }|j	                  ||
fz         |j	                  ||fz         n |j                  rM| j                  |j                        \  }}	d}
|	||
<   |j	                  d|
       |j	                  d|       |j                  rM| j                  |j                        \  }}	d}
|	||
<   |j	                  d|
       |j	                  d|       dj                  |      }|d	|d
}dj                  |      }d|d
}| j                  |      }	  | j                  j                  |fi |}d}| j                  j!                  |      }||||fz  }||fS # t        $ r t        |      w xY w)N__exprinfo_func__exprinfo_z%s=%%s__exprinfo_starrF   __exprinfo_kwdsrK   z, r   r   z__exprinfo_func(%s
{%s = %s
})r&   funcargslenr;   keywordsr>   argstarargskwargsr<   rS   r)   rT   rU   r   rV   )r   callfunc_explanationr   arg_explanationsns	argumentsr   arg_explanation
arg_resultarg_namekeywordkeyword_sourceargs_explainedr   r   r(   rZ   r[   r   reps                        r   
visit_CallzDebugInterpreter.visit_Call   sK   !%DII!6$$'	99 	5C*.**S/'OZ*-b'3H%BxLX&##O4	5 }} 	IG*.**W]]*C'OZ*-b'3H%BxL%5N^xk9:##No5G$GH	I ==*.**T]]*C'OZ(H%BxLh01##_$>?;;*.**T[[*A'OZ(H%BxLx12##o$?@#34"2NCyy#*.0]]6"	'$TZZ__R.2.F $jjoof%c; 77F""  	'+&&	's   0H6 6Ic                     d}||j                   |j                   fz  }| j                  |      }	 | j                  j                  |      S # t        $ r Y yw xY w)Nz*%r not in globals() and %r not in locals()F)rf   rS   r)   rT   rU   )r   rg   r   r(   rZ   s        r   _is_builtin_namez!DebugInterpreter._is_builtin_name  sV    >DGGTWW--]]6"	::??2&& 		s   A 	AAc                    t        |j                  t        j                        s| j	                  |      S | j                  |j                        \  }}|d|j                  }d|j                  }| j                  |      }	 | j                  j                  ||      }| j                  j                  |      d| j                  j                  |      d|d|j                  d}d}||j                  fz  }| j                  |      }	 | j                  j                  ||      }|r%| j                  j                  |      }	d	}
|
|	|	|fz  }||fS # t        $ r t        |      w xY w# t        $ r d}Y Pw xY w)
N.z__exprinfo_expr.r   z
{z = z
}z.%r in getattr(__exprinfo_expr, '__dict__', {})Tr   )r   ctxr   Loadr\   r&   r>   attrrS   r)   rT   rU   r   rV   )r   r   source_explanationsource_resultr   r(   rZ   r[   from_instancer   r   s              r   visit_Attributez DebugInterpreter.visit_Attribute  s]   $((CHH-%%d++,0JJtzz,B)M!3TYY?)-4]]6"	'ZZ__R_GF /3jjoof.E.2jjoof.E.@$))M B499,&]]6"	! JJOOBONM **//&)C'G!S#{$;;KF""#  	'+&&	'  	! M	!s   E E1 E.1E?>E?c                     | j                  |j                        \  }}|j                  d      r|j                  d      r|dd }d|}|s	 t        ||fS # t
        $ r t        |      w xY w)NzFalse
{False =r7      zassert )r&   testr=   endswithr   rU   r   )r   assrttest_explanationtest_resultr   s        r   visit_AssertzDebugInterpreter.visit_Assert'  s    (,

5::(>%+&&'89 ))$//26%57+++ K''  +k**+s   A A-c                     | j                  |j                        \  }}d|}t        j                  dt        j                         |j                  j
                  |j                  j                        }t        j                  |j                  ||j
                  |j                        }t        j                  |g      }| j                  |d      }	 | j                  j                  ||       ||fS # t        $ r t        |      w xY w)Nz... = r   )lineno
col_offsetrQ   r   )r&   r>   r   Namer   r   r   AssigntargetsrW   rS   r)   rX   rU   r   )	r   assignvalue_explanationvalue_resultr   rg   
new_assignr+   rZ   s	            r   visit_AssignzDebugInterpreter.visit_Assign4  s    *.**V\\*B'<$57xx)388:%||22#)<<#:#:< ZZV]]+1+<+<>
jj*&]]3'	'JJR> L((  	'+&&	's   C8 8DN)rT   )r   r   r   r    r   r\   rS   ra   rd   ri   r   r   r   r   r   r   r   r   r   r!   r   r   r%   r%   ]   sR    E=,C&##>
#	##*#X#8()r   r%   )Fr   ),r    r   r   r0   py._code.assertionr   r   r   r   rU   r   r.   r5   r'   BitOrBitXorBitAndLShiftRShiftAddSubMultDivFloorDivModEqNotEqLtLtEGtGtEPowIsIsNotInNotInru   NotInvertUSubUAddr   NodeVisitorr%   r!   r   r   <module>r      s  
  
 	 I&&'i 'S,
IIJJ JJ JJ	
 JJ GGc GGc HHs GGc LL4 GGc FFT II FFS GGd  FFS!" GGd#$ GGdFFTIIFFTII-4 GGhJJHHuHHu		e)s e)r   