
^Yc           @` s%  d  d l  m Z m Z m Z m Z d  d l m Z d  d l Z d  d l Z d  d l	 Z	 d  d l
 Z
 e Z y  d  d l Z d  d l m Z Wn e k
 r d  Z d Z n Xd e f d     YZ d d e j d  d  Z d	   Z d
   Z d   Z d d  Z d   Z e d d  Z e d  Z d S(   i    (   t   absolute_importt   divisiont
   generatorst   print_function(   t   bisect_rightN(   t   PyCF_ONLY_ASTt   Sourcec           B` s   e  Z d  Z d Z d   Z d   Z d Z d   Z d   Z	 d   Z
 d d d d
  Z d d  Z e d  Z e d  Z d d  Z e d  Z d   Z d d e j d d d  Z RS(   sY    a immutable object holding a source code fragment,
        possibly deindenting it.
    i    c   	      O` s<  g  |  _  } | j d t  } | j d t  } x| D] } | sM g  } n  t | t  rh | j  } n t | t t f  r g  | D] } | j d  ^ q } np t | t j	 j
  r | j d  } | rx+ | r | d j   r Pn  | j   q Wqn t | d | j  } | r't |  } n  | j |  q8 Wd  S(   Nt   deindentt   rstrips   
i(   t   linest   gett   Truet
   isinstanceR   t   tuplet   listR   t   pyt   builtint   _basestringt   splitt   stript   popt	   getsourceR   t   extend(	   t   selft   partst   kwargsR	   t   deR   t   partt	   partlinest   x(    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyt   __init__   s*    	%	c         C` sL   y |  j  | j  k SWn1 t k
 rG t | t  rC t |   | k St SXd  S(   N(   R	   t   AttributeErrorR   t   strt   False(   R   t   other(    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyt   __eq__.   s    c         C` sb   t  | t  r |  j | S| j d k r8 t d   n  t   } |  j | j | j !| _ | Sd  S(   Ni   s!   cannot slice a Source with a step(   Ni   (	   R   t   intR	   t   stept   Nonet
   IndexErrorR   t   startt   stop(   R   t   keyt	   newsource(    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyt   __getitem__8   s    	c         C` s   t  |  j  S(   N(   t   lenR	   (   R   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyt   __len__B   s    c         C` s   d t  |   } } x. | | k  rC |  j | j   rC | d 7} q Wx2 | | k rx |  j | d j   rx | d 8} qG Wt   } |  j | | !| j (| S(   s]    return new source object with trailing
            and leading blank lines removed.
        i    i   (   R-   R	   R   R   (   R   R(   t   endt   source(    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyR   E   s    #'	t    t    i   c         C` s\   t  |  } t  |  } t    } g  |  j D] } | | ^ q+ } | j | | j | _ | S(   se    return a copy of the source object with
            'before' and 'after' wrapped around it.
        (   R   R	   (   R   t   beforet   aftert   indentR+   t   lineR	   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyt	   putaroundR   s    	 c         C` s0   t    } g  |  j D] } | | ^ q | _ | S(   sl    return a copy of the source object with
            all lines indented by the given indent-string.
        (   R   R	   (   R   R5   R+   R6   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyR5   ]   s    	#c         C` s#   |  j  | |  \ } } |  | | !S(   sc    return Source statement which contains the
            given linenumber (counted from 0).
        (   t   getstatementrange(   R   t   linenot	   assertionR(   R/   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyt   getstatemente   s    c         C` sS   d | k o t  |   k  n s1 t d   n  t | |   \ } } } | | f S(   s{    return (start, end) tuple which spans the minimal
            statement region which containing the given lineno.
        i    s   lineno out of range(   R-   R'   t   getstatementrange_ast(   R   R9   R:   t   astR(   R/   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyR8   l   s    "c         C` s#   t    } t |  j |  | j (| S(   s:   return a new source object deindented by offset.
            If offset is None then guess an indentation offset from
            the first non-blank line.  Subsequent lines which have a
            lower indentation offset will be copied verbatim as
            they are assumed to be part of multilines.
        (   R   R   R	   (   R   t   offsetR+   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyR   u   s    		c         C` s   y d d l  } Wn t k
 r, d   } n
 X| j } | rQ t |  j    } n t |   } y | | d  Wn% t k
 r   n t k
 r t SXt Sd S(   sb    return True if source is parseable, heuristically
            deindenting it by default.
        i    Nc         S` s   t  |  d d  S(   Nt   asdt   exec(   t   compile(   R   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyt   <lambda>   s    s   
(	   t   parsert   ImportErrort   suiteR    R   t   KeyboardInterruptt	   ExceptionR!   R   (   R   R   RC   t   syntax_checkerR0   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyt   isparseable   s    	c         C` s   d j  |  j  S(   Ns   
(   t   joinR	   (   R   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyt   __str__   s    R@   c         C` s  | s% t  j j |  j d d  r | d k rC t j d  } n  | j j | j	 } } d |  j
 } |  j j
 d 7_
 | s | d | | f } q | d | | | f } n  d j |  j  d }	 y t |	 | | |  }
 Wn t k
 rt j   d } |  j | j  } | j r2| j d | j d	  n  | j d
 |  t d j |   } | j | _ | j | _ | j | _ |  nR X| t @r|
 Sg  |  j D] } | d ^ q} d d | | f t  j j j | <|
 Sd S(   s    return compiled code object. if filename is None
            invent an artificial filename which displays
            the source/line position of the caller frame.
        t   filei    i   s   <%d-codegen s   %s:%d>s	   %r %s:%d>s   
R2   t   ^s*   (code was compiled probably from here: %s)N(   R   t   patht   localt   checkR&   t   syst	   _getframet   f_codet   co_filenamet   f_linenot   _compilecountert	   __class__RJ   R	   t   cpy_compilet   SyntaxErrort   exc_infoR9   R>   t   appendt   textt	   _AST_FLAGt   stdt	   linecachet   cache(   R   t   filenamet   modet   flagt   dont_inheritt	   _genframet   fnR9   t   baseR0   t   cot   ext   msglinest   newexR   R	   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyRA      s8    %	

 Ns       s       (   t   __name__t
   __module__t   __doc__RV   R   R#   R&   t   __hash__R,   R.   R   R7   R5   R!   R;   R8   R   R   RI   RK   R   t   compiler_flagRA   (    (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyR      s$   			
				R@   c         C` sn   t  d k	 r4 t |  t  j  r4 t |  | | | |  St j d  } t |   } | j | | | d | } | S(   s    compile the given source to a raw code object,
        and maintain an internal cache which allows later
        retrieval of the source code for the code object
        and any recursively created code objects.
    i   Re   N(	   t   _astR&   R   t   ASTRX   RQ   RR   R   RA   (   R0   Ra   Rb   t   flagsRd   Re   t   sRh   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyt   compile_   s    c         C` s  d d l  } y | j j |   } Wn t k
 r y. t j j j |   p\ t j j j |   } Wn t k
 rt d SX| r t j	 j
 |  p d } d } | r y t |   \ } } Wq t k
 r q Xq n X| j	 } | j } t | t  s t  | | f S(   sx    Return source location (path, lineno) for the given object.
    If the source cannot be determined return ("", -1)
    i    NR1   i(   R1   i(   t   _pytest._codet   _codet   Codet	   TypeErrorR   R^   t   inspectt   getsourcefilet   getfileRN   RO   R&   t
   findsourcet   IOErrort   firstlinenoR   R$   t   AssertionError(   t   objt   _pytestt   codeRf   t   fspathR9   t   _(    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyt   getfslineno   s(    		c         C` sx   y t  j j j |   \ } } Wn! t  j j k
 r;   n d SXt   } g  | D] } | j   ^ qS | _	 | | f S(   Ni(   Ni(
   R   R^   Rz   R}   R   t   _sysexR&   R   R   R	   (   R   t   sourcelinesR9   R0   R6   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyR}      s    	"c         K` sm   d d  l  } | j j |   }  y t j |   } Wn t k
 rJ d } n Xt | t  s` t  t	 | |  S(   Ni    s<   "Buggy python version consider upgrading, cannot get source"(
   Rv   Rw   t
   getrawcodeRz   R   t   IndentationErrorR   R    R   R   (   R   R   R   t   strsrc(    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyR     s    
c   
      ` s  | d  k r^ xO |  D]> } | j   } | j   } | r t |  t |  } Pq q Wd } n  | d k rt t |   Sg  } d   } | |     y x t j   f d    D] \ } } \ } } \ } } } | t |   k r Pn  | t |  k rA|  | d j   } | j   r1| |  j   r1| | } n  | j |  n  x( t	 | |  D] }	 | j |  |	  qQWq WWn t
 t j f k
 rn X| j |  t |   | S(   Ni    c         s` s0   x |  D] } | d Vq Wx t  r+ d Vq Wd  S(   Ns   
R1   (   R   (   R	   R6   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyt   readline_generator  s    	c           ` s
   t     S(   N(   t   next(    (   t   it(    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyRB   &  s    i   (   R&   t
   expandtabst   lstripR-   R   t   tokenizet   generate_tokenst   isspaceR[   t   rangeR   t
   TokenErrorR   (
   R	   R>   R6   Rt   t   newlinesR   R   t   slinet   elinet   i(    (   R   s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyR     s8    	
	:c   
      C` s  d d  l  } g  } x | j |  D] } t | t j  sL t | t j  r" | j | j d  xH d D]= } t | | d   } | rg | j | d j d d  qg qg Wq" q" W| j
   t | |   } | | d } | t |  k r d  }	 n
 | | }	 | |	 f S(   Ni    i   t	   finalbodyt   orelse(   R   R   (   R=   t   walkR   Rq   t   stmtt   ExceptHandlerR[   R9   t   getattrR&   t   sortR   R-   (
   R9   t   nodeR=   t   lR   t   namet   valt   insert_indexR(   R/   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyt   get_statement_startend2:  s     $*
	
c   
      ` s  | d  k r t |  } t j d k  r4 | d 7} n  y t | d d d  } Wq t k
 r t |  | |  \ } } d  | | f SXn  t |  |  \ } } | d  k r t | j	  } n  | | d k rzt
 j   } | j	 | d j   | _ d	   | j	 | | !D   y4 x- t j   f d
    D] } | j |   q%WWqzt
 j t f k
 rf| j | } qzt k
 rvqzXn  xE | r| j	 | d j   }	 |	 j d  s|	 r| d 8} q}Pq}W| | | f S(   Ni   i   s   
R0   R@   i   i   i    c         s` s   |  ] } | d  Vq d S(   s   
N(    (   t   .0R   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pys	   <genexpr>i  s    c           ` s
   t     S(   N(   R   (    (   R   (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyRB   k  s    t   #(   i   i   (   R&   R    RQ   t   version_infoRA   t
   ValueErrort   getstatementrange_oldR   R-   R	   Rz   t   BlockFinderR   t   startedR   R   t
   tokeneatert
   EndOfBlockR   t   lastRG   R   t
   startswith(
   R9   R0   R:   t   astnodet   contentR(   R/   t   block_findert   tokR6   (    (   R   s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyR<   Q  s:    	c   	      C` sW  d d l  m } x-t |  d d  D]} | r | j | } d | k ro d | k ro d | k ro t d   n  d | k r d	 | k r q# q n  | j | |  d
 !} | j d d  d j |  } y | |  Wn t t t	 f k
 r q# n XxH t |  d
 t
 |  d
  D]) } | | | !} | j   r| | f SqWq# Wt d |  f   d S(   s    return (start, end) tuple which spans the minimal
        statement region which containing the given lineno.
        raise an IndexError if no such statementrange can be found.
    i    (   t   compile_commandit   superR   R   s   likely a subclasst   assertt   raisei   s
   def xxx():s   
 s%   no valid source range around line %d N(   t   codeopR   R   R	   R'   t   insertRJ   RY   t   OverflowErrorR   R-   RI   (	   R9   R0   R:   R   R(   R6   t   trylinest	   trysourceR/   (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyR   |  s(    $	$(   t
   __future__R    R   R   R   t   bisectR   RQ   Rz   R   R   RA   RX   Rq   R   R]   RD   R&   t   objectR   Rp   Ru   R   R}   R   R   R   R!   R<   R   (    (    (    s4   /tmp/pip-build-hU8Cw8/pytest/_pytest/_code/source.pyt   <module>   s*   "
			+	+