
^Yc           @` s  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z e e	 d  r e	 j   d Z n` e e d  r d Z n e j d	 k rd
 Z n d Z e j Z d e e d e d f Z [ [ d e rSd pVd Z d e e Z e j d  dJ k oe j dK k  Z e j d d k  Z e j dL k re j Z  n	 d   Z  d e! f d     YZ" d   Z# d j$ d  Z% d j$ d  Z& e j' d  Z( d Z) d   Z* d    Z+ d!   d"  Z, d d d#  Z. d$   Z/ d d% l0 m1 Z2 d&   Z3 d'   Z4 d(   Z5 d)   Z6 i d* e j7 6d+ e j8 6d, e j9 6d- e j: 6Z; i d. e j< 6d/ e j= 6d0 e j> 6d1 e j? 6d2 e j@ 6d3 e jA 6d4 e jB 6d5 e jC 6d6 e jD 6d7 e jE 6d8 e jF 6d9 e jG 6d: e jH 6d; e jI 6d< e jJ 6d= e jK 6d> e jL 6d? e jM 6d@ e jN 6dA e jO 6dB e jP 6dC e jQ 6ZR y dD eR e jS <Wn eT k
 rn Xe e dE  re jU ZV n	 dF   ZV dG   ZW dH e jX f dI     YZY d S(M   s4   Rewrite assertion AST to produce nice error messagesi    (   t   absolute_importt   divisiont   print_functionN(   t   utilt   get_tags   -PYTESTt   pypy_version_infot   pypyt   javat   jythont   cpythons   %s-%s%s-PYTESTi   s   .pyt   ct   ot   .i   i   i   i   c         C` s   t  j |  | | d  d   S(   N(   t   astt   Callt   None(   t   at   bR
   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   <lambda>*   s    t   AssertionRewritingHookc           B` sq   e  Z d  Z d   Z d   Z d d  Z d   Z d   Z d   Z	 d   Z
 d   Z e d	    Z d
   Z RS(   s*   PEP302 Import hook which rewrites asserts.c         C` sS   | |  _  | j d  |  _ d  |  _ i  |  _ t   |  _ |  j   t   |  _	 d  S(   Nt   python_files(
   t   configt   getinit   fnpatsR   t   sessiont   modulest   sett   _rewritten_namest   _register_with_pkg_resourcest   _must_rewrite(   t   selfR   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   __init__0   s    			
c         C` s   | |  _  d  S(   N(   R   (   R   R   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   set_session9   s    c         C` sS  |  j  j } | j d |  | j d d  } | d } d  } | d  k	 ry t |  } t |  d k ry | d } qy n  | d  k rQy t j | |  \ } } }	 Wn t	 k
 r d  SX| d  k	 r | j
   n  |	 d }
 |
 t j k r;t t d  r.y t j |  } Wq8t k
 r*| d  } q8XqN| d  } qw|
 t j k rwd  Sn& t j j | | j d  d d  } t j j |  } |  j | | |  sd  S|  j j |  t j } t j j | j d	  } | ry t j |  Wqt k
 rt j   d j } | t j  k r q| t j! t j" g k rAt# } q| t j$ t j% t j& g k r|| j d
 | j  t# } q  qXn  | j' d  t( } t j j | |  } t) | | | j  } | d  k r(| j d | f  t* |  j  |  \ } } | d  k r	d  S| r<t+ | | | |  q<n | j d | f  | | f |  j, | <|  S(   Ns   find_module called for: %sR   i   ii    i   t   source_from_caches   .pyt   __pycache__s   read only directory: %ris   rewriting %rs!   found cached rewritten pyc for %r(-   R   t   _assertstatet   tracet   rsplitR   t   listt   lent   impt   find_modulet   ImportErrort   closet   PY_COMPILEDt   hasattrR!   t
   ValueErrort	   PY_SOURCEt   ost   patht   joint
   rpartitiont   pyt   localt   _should_rewriteR   t   addt   syst   dont_write_bytecodet   dirnamet   mkdirt   OSErrort   exc_infot   errnot   EEXISTt   ENOENTt   ENOTDIRt   Falset   EACCESt   EROFSt   EPERMt   basenamet   PYC_TAILt	   _read_pyct   _rewrite_testt   _make_rewritten_pycR   (   R   t   nameR1   t   statet   namest   lastnamet   ptht   fdt   fnt   desct   tpt	   fn_pypatht   writet	   cache_dirt   et
   cache_namet   pyct   cot   source_stat(    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyR)   <   sv    

&

		
c         C` s   t  |  } | j d k r3 | j d | f  t S|  j d  k	 ro |  j j |  ro | j d | f  t Sn  x8 |  j D]- } | j |  ry | j d | f  t Sqy Wx; |  j	 D]0 } | j
 |  r | j d | | f  t Sq Wt S(   Ns   conftest.pys   rewriting conftest file: %rs0   matched test file (was specified on cmdline): %rs   matched test file %rs    matched marked file %r (from %r)(   t   strRF   R$   t   TrueR   R   t
   isinitpathR   t   fnmatchR   t
   startswithRB   (   R   RK   RT   RL   RQ   t   patt   marked(    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyR6      s$    	c         G` sk   t  |  j t  t j   } | rW x0 | D]% } | |  j k r+ |  j |  q+ q+ Wn  |  j j |  d S(   s   Mark import names as needing to be re-written.

        The named module or package as well as any nested modules will
        be re-written on import.
        N(   R   t   intersectionR8   R   R   t   _warn_already_importedR   t   update(   R   RM   t   already_importedRK   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   mark_rewrite   s    c         C` s   |  j  j d d |  d  S(   Nt   P1s4   Module already imported so can not be re-written: %s(   R   t   warn(   R   RK   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyRd      s    	c         C` s   | t  j k r t  j | S|  j j |  \ } } t j |  } t  j | <y8 | j | _ | | _ |  | _ t	 j
 j | | j  Wn& | t  j k r t  j | =n    n Xt  j | S(   N(   R8   R   t   popR(   t
   new_modulet   co_filenamet   __file__t
   __cached__t
   __loader__R4   t   builtint   exec_t   __dict__(   R   RK   RZ   RY   t   mod(    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   load_module   s    		c         C` sa   y t  j |  \ } } } Wn t k
 r0 t SX| d  k	 rJ | j   n  | d } | t  j k S(   Ni   (   R(   R)   R*   RB   R   R+   t   PKG_DIRECTORY(   R   RK   RP   RQ   RR   RS   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt
   is_package   s    
c         C` sC   y d d l  } | j Wn t k
 r+ d SX| j |  | j  d S(   s   
        Ensure package resources can be loaded from this loader. May be called
        multiple times, as the operation is idempotent.
        i    N(   t   pkg_resourcest   __name__R*   t   register_loader_typet   DefaultProvider(   t   clsRw   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyR      s    c         C` s&   t  | d   } | j   SWd QXd S(   s&   Optional PEP302 get_data API.
        t   rbN(   t   opent   read(   R   t   pathnamet   f(    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   get_data   s    N(   Rx   t
   __module__t   __doc__R   R    R   R)   R6   Rg   Rd   Rt   Rv   t   classmethodR   R   (    (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyR   -   s   			Y					
c         C` s   y t  | d  } Wn< t k
 rQ t j   d j } |  j d | | f  t SXz_ | j t j	    t
 | j  } | j d @} | j t j d | |   t j | |  Wd  | j   Xt S(   Nt   wbi   s&   error writing pyc file at %s: errno=%sI    s   <ll(   R}   t   IOErrorR8   R=   R>   R$   RB   RU   R(   t	   get_magict   intt   mtimet   sizet   structt   packt   marshalt   dumpR+   R]   (   RL   RZ   R[   RY   t   fpt   errR   R   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt
   _write_pyc   s    s   
s   utf-8s   
s#   ^[ \t\f]*#.*coding[:=][ \t]*[-\w.]+s   ﻿c   	      C` s  |  j  } y | j   } | j d  } Wn t k
 r< d SXt r
| j d  } | j d | d  } | j t  r
t	 j
 | d | ! d k r
t	 j
 | | d | ! d k r
t | d  r d St | _ z* y | j d  Wn t k
 r d SXWd | ` Xq
n  t r)| j t t  t } n  y t j |  } Wn& t k
 rd| j d | f  d SXt | | |   y t | | j d	 d
 t } Wn& t k
 r| j d | f  d SX| | f S(   s8   Try to read and rewrite *fn* and return the code object.R|   s   
i   i    t	   _indecodet   asciiNs   failed to parse: %rt   exect   dont_inherits   failed to compile: %r(   NN(   NN(   NN(   NN(   NN(   R#   t   statR~   t   EnvironmentErrorR   t   ASCII_IS_DEFAULT_ENCODINGt   findR`   t   BOM_UTF8t	   cookie_ret   matchR-   R]   R   t   decodet   UnicodeDecodeErrort   REWRITE_NEWLINESt   replacet   RNt   NR   t   parset   SyntaxErrorR$   t   rewrite_assertst   compilet   strpath(	   R   RQ   RL   R   t   sourcet   end1t   end2t   treeRZ   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyRI     sF    	 		c         C` sn   t  j j d  r( t |  | | |  nB | d t t j    } t |  | | |  rj t j | |  n  d S(   s$   Try to dump rewritten code to *pyc*.t   winR   N(   R8   t   platformR`   R   R\   R0   t   getpidt   rename(   RL   R[   RY   RZ   t   proc_pyc(    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyRJ   W  s
    c         C` s   d  S(   N(   R   (   t   x(    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyR   d  s    c   	   	   C` sW  y t  | d  } Wn t k
 r' d	 SX| #y1 t |  j    } |  j   } | j d  } Wn( t k
 r } | d |  | f  d	 SXt |  d k s | d  t	 j
   k s t j d | d  | | f k r | d |   d	 Sy t j |  } Wn( t k
 r$} | d |  | f  d	 SXt | t j  sI| d |   d	 S| SWd	 QXd	 S(
   ss   Possibly read a pytest pyc containing rewritten code.

    Return rewritten code if successful or None if not.
    R|   i   s"   _read_pyc(%s): EnvironmentError %si   s   <lls)   _read_pyc(%s): invalid or out of date pycs$   _read_pyc(%s): marshal.load error %ss    _read_pyc(%s): not a code objectN(   R}   R   R   R   R   R   R~   R   R'   R(   R   R   t   unpackR   t   loadt	   Exceptiont
   isinstancet   typest   CodeType(	   R   RY   R$   R   R   R   t   dataRW   RZ   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyRH   d  s2    ("c         C` s   t  | |  j |   d S(   s%   Rewrite the assert statements in mod.N(   t   AssertionRewritert   run(   Rs   t   module_pathR   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyR     s    c         C` s[   t  j j |   } t  j j |  r3 t  j j } n t  j j } | j | d  | d   S(   s  Get a safe repr of an object for assertion error messages.

    The assertion formatting (util.format_explanation()) requires
    newlines to be escaped since they are a special character for it.
    Normally assertion.util.format_explanation() does this but for a
    custom repr it is possible to contain one of the special escape
    sequences, especially '
{' and '
}' are likely to be present in
    JSON reprs.

    s   
s   \n(   R4   t   iot   safereprRp   t   _istextt   textt   bytesR   (   t   objt   reprt   t(    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt	   _saferepr  s
    (   t   format_explanationc         C` s   t  j j |   s$ t  j j |   r3 |  } t } n t  j j |   } t } t  j j |  rl t  j j } n t  j j	 } | j
 | d  | d   j
 | d  | d   } | r | j
 | d  | d   } n  | S(   s   Format the custom assertion message given.

    For strings this simply replaces newlines with '
~' so that
    util.format_explanation() will preserve them instead of escaping
    newlines.  For other objects py.io.saferepr() is used first.

    s   
s   
~t   %s   %%s   \n(   R4   Rp   R   t   _isbytesRB   R   R   R]   R   R   R   (   R   t   st   is_reprR   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   _format_assertmsg  s    $	6!c         C` s!   t  |  d  o  t j j |   S(   NRx   (   R-   R4   Rp   t   callable(   R   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   _should_repr_global_name  s    c         C` sl   d | r d p d j  |   d } t j j |  rD t j j } n t j j } | j | d  | d   S(   Nt   (s    or s    and t   )R   s   %%(   R2   R4   Rp   R   R   R   R   (   t   explanationst   is_ort   explanationR   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   _format_boolop  s
    #c   	      C` s   x^ t  t t |    | |  D]> \ } } } y | } Wn t k
 rR t } n X| r Pq q Wt j d  k	 r t j |  | | | | | d  } | d  k	 r | Sn  | S(   Ni   (   t   zipt   rangeR'   R   R]   R   t   _reprcompareR   (	   t   opst   resultst   explst   each_objt   it   rest   explt   donet   custom(    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   _call_reprcompare  s    .
%s   not %ss   ~%ss   -%ss   +%st   |t   ^t   &s   <<s   >>t   +t   -t   *t   /s   //s   %%s   ==s   !=t   <s   <=t   >s   >=s   **t   iss   is nott   ins   not int   @t   NameConstantc         C` s   t  j t |   t  j    S(   N(   R   t   NameR\   t   Load(   R
   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   _NameConstant  s    c         ` s#     f d       |  | |  |  S(   s*   Set node location information recursively.c         ` sd   d |  j  k r | |  _ n  d |  j  k r6 | |  _ n  x' t j |   D] }   | | |  qF Wd  S(   Nt   linenot
   col_offset(   t   _attributesR   R   R   t   iter_child_nodes(   t   nodeR   R   t   child(   t   _fix(    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyR     s    (    (   R   R   R   (    (   R   s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   set_location  s    R   c           B` s   e  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 d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z e j d k r e Z n e Z d   Z d   Z RS(   s2	  Assertion rewriting implementation.

    The main entrypoint is to call .run() with an ast.Module instance,
    this will then find all the assert statements and re-write them to
    provide intermediate values and a detailed assertion error.  See
    http://pybites.blogspot.be/2011/07/behind-scenes-of-pytests-new-assertion.html
    for an overview of how this works.

    The entry point here is .run() which will iterate over all the
    statements in an ast.Module and for each ast.Assert statement it
    finds call .visit() with it.  Then .visit_Assert() takes over and
    is responsible for creating new ast statements to replace the
    original assert statement: it re-writes the test of an assertion
    to provide intermediate values and replace it with an if statement
    which raises an assertion error with a detailed explanation in
    case the expression is false.

    For this .visit_Assert() uses the visitor pattern to visit all the
    AST nodes of the ast.Assert.test field, each visit call returning
    an AST node and the corresponding explanation string.  During this
    state is kept in several instance attributes:

    :statements: All the AST statements which will replace the assert
       statement.

    :variables: This is populated by .variable() with each variable
       used by the statements so that they can all be set to None at
       the end of the statements.

    :variable_counter: Counter to create new unique variables needed
       by statements.  Variables are created using .variable() and
       have the form of "@py_assert0".

    :on_failure: The AST statements which will be executed if the
       assertion test fails.  This is the code which will construct
       the failure message and raises the AssertionError.

    :explanation_specifiers: A dict filled by .explanation_param()
       with %-formatting placeholders and their corresponding
       expressions to use in the building of an assertion message.
       This is used by .pop_format_context() to build a message.

    :stack: A stack of the explanation_specifiers dicts maintained by
       .push_format_context() and .pop_format_context() which allows
       to build another %-formatted string while already building one.

    This state is reset on every new assert statement visited and used
    by the other visitors.

    c         C` s)   t  t |   j   | |  _ | |  _ d  S(   N(   t   superR   R   R   R   (   R   R   R   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyR   B  s    	c         C` si  | j  s d St j t j j j d  t j d d  g } t } d } d } x | j  D] } | r t | t j	  r t | j
 t j  r | j
 j } d | k r d S| t |  d 7} t } n> t | t j  s | j d k s | j d k r | j } Pn  | d 7} qV Wg  | D]$ } t j | g d	 | d
 d ^ q}	 |	 | j  | | +| g }
 x
|
 rd|
 j   } x t j |  D] \ } } t | t  r(g  } xt t |  D]f \ } } t | t j  r| j |  j |   q| j |  t | t j  r|
 j |  qqWt | | |  q}t | t j  r}t | t j  r}|
 j |  q}q}Wq[Wd S(   s5   Find all assert statements in *mod* and rewrite them.Ns   @py_builtinss   _pytest.assertion.rewrites
   @pytest_ari    t   PYTEST_DONT_REWRITEi   t
   __future__R   R   (   t   bodyR   t   aliasR4   Rp   t   builtinsRx   R]   R   t   Exprt   valuet   StrR   R'   RB   t
   ImportFromt   levelt   moduleR   t   ImportRj   t   iter_fieldsR&   t	   enumeratet   Assertt   extendt   visitt   appendt   ASTt   setattrt   expr(   R   Rs   t   aliasest   expect_docstringt   posR   t   itemt   docR   t   importst   nodesR   RK   t   fieldt   newR   R   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyR   G  sN    		"	.		c         C` s-   d t  t |  j   } |  j j |  | S(   s   Get a new variable.s
   @py_assert(   R\   t   nextt   variable_countert	   variablesR
  (   R   RK   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   variablez  s    c         C` sS   |  j    } |  j j t j t j | t j    g |   t j | t j    S(   s   Give *expr* a name.(   R  t
   statementsR
  R   t   AssignR   t   StoreR   (   R   R  RK   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   assign  s    1c         C` s   |  j  d |  S(   s&   Call py.io.saferepr on the expression.R   (   t   helper(   R   R  (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   display  s    c         G` sM   t  j d t  j    } t  j | d | t  j    } t | t |  g   S(   s   Call a helper in this module.s
   @pytest_art   _(   R   R   R   t	   Attributet   ast_CallR&   (   R   RK   t   argst   py_namet   attr(    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyR    s    c         C` s1   t  j d t  j    } t  j | | t  j    S(   s!   Return the builtin called *name*.s   @py_builtins(   R   R   R   R"  (   R   RK   t   builtin_name(    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyRp     s    c         C` s2   d t  t |  j   } | |  j | <d | d S(   sK  Return a new named %-formatting placeholder for expr.

        This creates a %-formatting placeholder for expr in the
        current formatting context, e.g. ``%(py0)s``.  The placeholder
        and expr are placed in the current format context so that it
        can be used on the next call to .pop_format_context().

        R4   s   %(s   )s(   R\   R  R  t   explanation_specifiers(   R   R  t	   specifier(    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   explanation_param  s    	c         C` s    i  |  _  |  j j |  j   d S(   s  Create a new formatting context.

        The format context is used for when an explanation wants to
        have a variable value formatted in the assertion message.  In
        this case the value required can be added using
        .explanation_param().  Finally .pop_format_context() is used
        to format a string of %-formatted values as added by
        .explanation_param().

        N(   R(  t   stackR
  (   R   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   push_format_context  s    	c         C` s   |  j  j   } |  j  r+ |  j  d |  _ n  g  | j   D] } t j |  ^ q8 } t j | t | j     } t j	 | t j
   |  } d t t |  j   } |  j j t j t j | t j    g |   t j | t j    S(   sV  Format the %-formatted string with current format context.

        The expl_expr should be an ast.Str instance constructed from
        the %-placeholders created by .explanation_param().  This will
        add the required code to format said string to .on_failure and
        return the ast.Name instance of the formatted string.

        is
   @py_format(   R+  Rj   R(  t   keysR   R   t   DictR&   t   valuest   BinOpt   ModR\   R  R  t
   on_failureR
  R  R   R  R   (   R   t	   expl_exprt   currentt   keyR-  t   format_dictt   formRK   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   pop_format_context  s    		(1c         C` sC   t  | t j  s t  |  j |  } | |  j |  j |   f S(   s1   Handle expressions we don't have custom code for.(   R   R   R  t   AssertionErrorR  R*  R   (   R   R   R   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   generic_visit  s    c         C` sz  t  | j t j  rR |  j d k	 rR |  j | j f } |  j j d d d | n  g  |  _	 g  |  _
 t j   |  _ g  |  _ g  |  _ |  j   |  j | j  \ } } |  j } t j t j   |  } |  j	 j t j | | g    | j r|  j d | j  } d | } n t j d  } d | } t j | t j   t j |   } |  j |  }	 |  j d |	  }
 t j d	 t j    } t | |
 g g   } t j  d
 d k rt j! | d  } n t j! | d d  } | j |  |  j
 rIg  |  j
 D] } t j | t j"    ^ q} t j# | t$ d   } |  j	 j |  n  x' |  j	 D] } t% | | j | j&  qSW|  j	 S(   s0  Return the AST statements to replace the ast.Assert instance.

        This re-writes the test of an assertion to provide
        intermediate values and replace it with an if statement which
        raises an assertion error with a detailed explanation in case
        the expression is false.

        t   R1s5   assertion is always true, perhaps remove parentheses?t
   fslocationt   format_assertmsgs	   
>assert t    s   assert R   R9  i    i   N('   R   t   testR   t   TupleR   R   R   R   Ri   R  R  t	   itertoolst   countR  R+  R2  R,  R	  t   UnaryOpt   NotR
  t   Ift   msgR  R   R0  t   AddR8  R   R   R#  R8   t   version_infot   RaiseR  R  R   R   R   (   R   t   assert_R<  t   top_conditionR   R   t   negationt	   assertmsgt   templateRF  t   fmtt   err_namet   exct   raise_RK   R  t   cleart   stmt(    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   visit_Assert  sH    	$
				
		
$	+c         C` s   t  |  j d  g  g   } t j t j | j  t j   g | g  } |  j d |  } t j t j	   | | g  } t j
 | |  j |  t j | j   } | |  j |  f S(   Nt   localst   should_repr_global_name(   R#  Rp   R   t   CompareR   t   idt   InR  t   BoolOpt   Ort   IfExpR   R*  (   R   RK   t   locst   inlocst   doreprR?  R  (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt
   visit_Name  s    -*c         C` sL  |  j    } |  j t j g  t j     } t j | d t j    } t t | j t j	   } |  j
 } } |  j } t | j  d }	 |  j   xOt | j  D]>\ }
 } |
 r g  } |  j j t j | | g    | |  _ n  |  j   |  j |  \ } } | j t j t j | t j    g |   |  j t j |   } t | | g g   } |  j j t j |   |
 |	 k  r | } | rt j t j   |  } n  g  } |  j
 j t j | | g    | |  _
 } q q W| |  _
 | |  _ |  j d | t j |   } |  j |  } t j | t j    |  j |  f S(   NR
  i   t   format_boolop(   R  R  R   t   ListR   R"  R   R   t   opR\  R  R2  R'   R/  R,  R  R
  RE  R	  R  R   R  R8  R   R#  R   RC  RD  R  t   NumR*  (   R   t   boolopt   res_vart	   expl_listt   appR   R   t   savet	   fail_savet   levelsR   t   vt
   fail_innert   condR   R   t   expl_formatt   callt   innert   expl_template(    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   visit_BoolOp  s>    !	

.		c         C` sW   t  | j j } |  j | j  \ } } |  j t j | j |   } | | | f f S(   N(   t	   unary_mapRd  t	   __class__R	  t   operandR  R   RC  (   R   t   unaryt   patternt   operand_rest   operand_explR   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   visit_UnaryOp+  s    c   	      C` s~   t  | j j } |  j | j  \ } } |  j | j  \ } } d | | | f } |  j t j | | j |   } | | f S(   Ns
   (%s %s %s)(	   t	   binop_mapRd  Rv  R	  t   leftt   rightR  R   R0  (	   R   t   binopt   symbolt	   left_exprt	   left_explt
   right_exprt
   right_explR   R   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   visit_BinOp1  s    !c         C` s\  |  j  | j  \ } } g  } g  } g  } x@ | j D]5 } |  j  |  \ } }	 | j |	  | j |  q4 Wxz | j D]o }
 |  j  |
 j  \ } }	 | j t j |
 j |   |
 j r | j |
 j d |	  qw | j d |	  qw Wd | d j	 |  f }	 t j
 | | |  } |  j |  } |  j |  j |   } d | | |	 f } | | f S(   s?   
        visit `ast.Call` nodes on Python3.5 and after
        t   =s   **s   %s(%s)s   , s   %s
{%s = %s
}(   R	  t   funcR$  R
  t   keywordsR   R   t   keywordt   argR2   R   R  R*  R   (   R   Rq  t   new_funct	   func_explt	   arg_explst   new_argst
   new_kwargsR  R   R   R  t   new_callt   res_explt
   outer_expl(    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   visit_Call_359  s(    	c         C` s&   |  j  | j  \ } } | d | f S(   NR   (   R	  R   (   R   t   starredR   R   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   visit_StarredT  s    c         C` s  |  j  | j  \ } } g  } g  } g  } d } } x@ | j D]5 }	 |  j  |	  \ }
 } | j |
  | j |  q> Wx] | j D]R } |  j  | j  \ }
 } | j t j | j	 |
   | j | j	 d |  q W| j
 r|  j  | j
  \ } } | j d |  n  | j rA|  j  | j  \ } } | j d |  n  d | d j |  f } t j | | | | |  } |  j |  }
 |  j |  j |
   } d | | | f } |
 | f S(   s9   
        visit `ast.Call nodes on 3.4 and below`
        R  R   s   **s   %s(%s)s   , s   %s
{%s = %s
}N(   R	  R  R   R$  R
  R  R   R   R  R  t   starargst   kwargsR2   R   R  R*  R   (   R   Rq  R  R  R  R  R  t   new_start	   new_kwargR  R   R   R  R  R  R  (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   visit_Call_legacyY  s4    
		i   i   c         C` s   t  | j t j  s" |  j |  S|  j | j  \ } } |  j t j | | j	 t j     } |  j
 |  j |   } d } | | | | | j	 f } | | f S(   Ns   %s
{%s = %s.%s
}(   R   t   ctxR   R   R:  R	  R   R  R"  R&  R*  R   (   R   R&  R   t
   value_explR   R  Ra   R   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   visit_Attribute  s    'c         C` s  |  j    |  j | j  \ } } t | j t j t j f  rR d j |  } n  g  t t	 | j
   D] } |  j   ^ qh } g  | D] } t j | t j    ^ q } g  | D] } t j | t j    ^ q } t t t	 | j
   | j
 | j  }	 g  }
 g  } | g } x |	 D] \ } } } |  j |  \ } } t | t j t j f  rjd j |  } n  | j |  t | j } | j t j |   d | | | f } |
 j t j |   t j | | g | g  } |  j j t j | | g |   | | } } qW|  j d t j | t j    t j | t j    t j |
 t j    t j | t j     } t	 | j
  d k rt j t j   |  } n
 | d } | |  j |  j |   f S(   Ns   ({0})s   %s %s %st   call_reprcomparei   i    (   R,  R	  R~  R   t   _astRX  R[  t   formatR   R'   R   R  R   R   R   R  R   t   comparatorsR
  R}  Rv  R   R  R  R  R@  t   AndR*  R8  (   R   t   compt   left_resR  R   t   res_variablesRm  t
   load_namest   store_namest   itR   t   symsR   Rd  t   next_operandt   next_rest	   next_explt   symR   t   res_exprt	   expl_callR   (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   visit_Compare  s@    
.++'	#	
(   i   i   (   Rx   R   R   R   R   R  R  R   R  Rp   R*  R,  R8  R:  RU  Ra  Rt  R|  R  R  R  R  R8   RH  t
   visit_CallR  R  (    (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyR     s2   2		3										4	
	#					"		
(   i   i   (   i   i   (   i   i   (Z   R   R   R    R   R   R   R  R>   RA  R(   R   R0   t   reR   R8   R   R4   t   _pytest.assertionR   R-   R   t
   PYTEST_TAGt   implR   RH  t   vert	   __debug__t   PYC_EXTRG   R   R   R   R#  t   objectR   R   t   encodeR   R   R   R   R   RI   RJ   RH   R   R   R   t   _pytest.assertion.utilR   t   _format_explanationR   R   R   R   RD  t   Invertt   USubt   UAddRu  t   BitOrt   BitXort   BitAndt   LShiftt   RShiftRG  t   Subt   Multt   Divt   FloorDivR1  t   Eqt   NotEqt   Ltt   LtEt   Gtt   GtEt   Powt   Ist   IsNotRZ  t   NotInR}  t   MatMultt   AttributeErrorR   R   R   t   NodeVisitorR   (    (    (    s9   /tmp/pip-build-hU8Cw8/pytest/_pytest/assertion/rewrite.pyt   <module>   s   			"			9	!					























		