
^Yc           @   s#  d  d l  m Z d  d l m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l m	 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
   generators(   t   bisect_rightN(   t
   ModuleType(   t   PyCF_ONLY_ASTi    t   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(    (    s+   /tmp/pip-build-hU8Cw8/py/py/_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(    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyt   __eq__/   s    c         C   sR   t  | t  r |  j | S| j d k r8 t d   n  |  j | j | j  Sd  S(   Ni   s!   cannot slice a Source with a step(   Ni   (	   R
   t   intR   t   stept   Nonet
   IndexErrort   __getslice__t   startt   stop(   R   t   key(    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyt   __getitem__7   s
    c         C   s   t  |  j  S(   N(   t   lenR   (   R   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyt   __len__?   s    c         C   s    t    } |  j | | !| _ | S(   N(   R   R   (   R   R'   t   endt	   newsource(    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyR&   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'   R-   t   source(    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyR   G   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   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyt	   putaroundT   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   R4   R.   R5   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyR4   _   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-   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyt   getstatementg   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   R8   R9   t   astR'   R-   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyR7   n   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.   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyR   w   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.
        iNc         S   s   t  |  d d  S(   Nt   asdt   exec(   t   compile(   R   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyt   <lambda>   s    s   
(	   t   parsert   ImportErrort   suiteR   R   t   KeyboardInterruptt	   ExceptionR   R	   (   R   R   RB   t   syntax_checkerR/   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyt   isparseable   s    	c         C   s   d j  |  j  S(   Ns   
(   t   joinR   (   R   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_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   
R1   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__RI   R   t   cpy_compilet   SyntaxErrort   exc_infoR8   R=   t   appendt   textt	   _AST_FLAGt   stdt	   linecachet   cache(   R   t   filenamet   modet   flagt   dont_inheritt	   _genframet   fnR8   t   baseR/   t   cot   ext   msglinest   newexR   R   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyR@      s8    %	

 s       s       N(   t   __name__t
   __module__t   __doc__RU   R   R!   R*   R,   R&   R   R6   R4   R   R:   R7   R$   R   R	   RH   RJ   R    t   compiler_flagR@   (    (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_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   Rd   N(	   t   _astR$   R
   t   ASTRW   RP   RQ   R   R@   (   R/   R`   Ra   t   flagsRc   Rd   t   sRg   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyt   compile_   s    c         C   s   y t  j j |   } Wn t k
 r y. t  j j j |   pP t  j j j |   } Wn t k
 rh 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)
    R0   i(   R0   iN(   R   t   codet   Codet	   TypeErrorR]   t   inspectt   getsourcefilet   getfileRM   RN   R$   t
   findsourcet   IOErrort   firstlinenoR
   R"   t   AssertionError(   t   objRt   Re   t   fspathR8   t   _(    (    s+   /tmp/pip-build-hU8Cw8/py/py/_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]   Rw   Rz   R   t   _sysexR$   R   R   R   (   R~   t   sourcelinesR8   R/   R5   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyRz      s    	"c         K   sa   t  j j |   }  y t j |   } Wn t k
 r> d } n Xt | t  sT t  t	 | |  S(   Ns<   "Buggy python version consider upgrading, cannot get source"(
   R   Rt   t
   getrawcodeRw   R   t   IndentationErrorR
   R   R}   R   (   R~   R   t   strsrc(    (    s+   /tmp/pip-build-hU8Cw8/py/py/_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   
R0   (   R	   (   R   R5   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyt   readline_generator  s    	c              s
   t     S(   N(   t   next(    (   t   it(    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyRA   #  s    i   (   R$   t
   expandtabst   lstripR+   R   t   tokenizet   generate_tokenst   isspaceRZ   t   rangeR   t
   TokenErrorR   (
   R   R=   R5   Rr   t   newlinesR   R   t   slinet   elinet   i(    (   R   s+   /tmp/pip-build-hU8Cw8/py/py/_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(   Nii   t	   finalbodyt   orelsei    (   R   R   (   R<   t   walkR
   Ro   t   stmtt   ExceptHandlerRZ   R8   t   getattrR$   t   sortR   R+   (
   R8   t   nodeR<   t   lR   t   namet   valt   insert_indexR'   R-   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyt   get_statement_startend27  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   
R/   R?   i   i   i    c         s   s   |  ] } | d  Vq d S(   s   
N(    (   t   .0R   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pys	   <genexpr>f  s    c              s
   t     S(   N(   R   (    (   R   (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyRA   h  s    t   #(   i   i   (   R$   R   RP   t   version_infoR@   t
   ValueErrort   getstatementrange_oldR   R+   R   Rw   t   BlockFinderR   t   startedR   R   t
   tokeneatert
   EndOfBlockR   t   lastRF   R   t
   startswith(
   R8   R/   R9   t   astnodet   contentR'   R-   t   block_findert   tokR5   (    (   R   s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyR;   N  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_commandt   superR   R   s   likely a subclasst   assertt   raisei   i    s
   def xxx():s   
 s%   no valid source range around line %d N(   t   codeopR   R   R   R%   t   insertRI   RX   t   OverflowErrorR   R+   RH   (	   R8   R/   R9   R   R'   R5   t   trylinest	   trysourceR-   (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyR   y  s(    $	$(   t
   __future__R    t   bisectR   RP   Rw   R   R   t   typesR   R@   RW   Ro   R   R\   RC   R$   t   objectR   Rn   Rs   R   Rz   R   R   R   R   R;   R   (    (    (    s+   /tmp/pip-build-hU8Cw8/py/py/_code/source.pyt   <module>   s,   
				*	+