
    a6                         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	 d dlZd dlmZ  G d de      Zddej$                  d fd	Zd
 Zd Zd ZddZd ZddZddZy# e$ r d ZdZY Aw xY w)    )
generators)bisect_rightN)
ModuleType)PyCF_ONLY_ASTc                       e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
dd	Zdd
ZddZddZddZddZd Zddej&                  ddfdZy)SourcezY a immutable object holding a source code fragment,
        possibly deindenting it.
    r   c                 N   g x| _         }|j                  dd      }|j                  dd      }|D ]  }|sg }t        |t              r|j                   }nt        |t        t
        f      r|D cg c]  }|j                  d       }}nxt        |t        j                  j                        r=|j                  d      }|rA|r?|d   j                         rn+|j                          |r'nt        ||      j                   }|rt        |      }|j                  |        y c c}w )NdeindentTrstrip
)r
   )linesget
isinstancer   tuplelistr   pybuiltin_basestringsplitstrippop	getsourcer
   extend)	selfpartskwargsr   der   part	partlinesxs	            1/usr/lib/python3/dist-packages/py/_code/source.py__init__zSource.__init__   s    
UZZ
D)Hd+ 	$D	$' JJ	D5$-059:QXXd^:	:D"**"8"89 JJt,	#$R=..0!! $
 &dR8>>	$Y/	LL#%	$ ;s   .D"c                     	 | j                   |j                   k(  S # t        $ r# t        |t              rt        |       |k(  cY S Y yw xY w)NF)r   AttributeErrorr   str)r   others     r"   __eq__zSource.__eq__/   sE    	::,, 	%%4yE))	s    'AAc                     t        |t              r| j                  |   S |j                  dvrt	        d      | j                  |j                  |j                        S )N)N   z!cannot slice a Source with a step)r   intr   step
IndexError__getslice__startstop)r   keys     r"   __getitem__zSource.__getitem__7   sM    c3::c?"xxy( !DEE$$SYY99    c                 ,    t        | j                        S N)lenr   r   s    r"   __len__zSource.__len__?   s    4::r3   c                 B    t               }| j                  || |_        |S r5   r   r   )r   r/   end	newsources       r"   r.   zSource.__getslice__B   s!    H	**U3/	r3   c                    dt        |       }}||k  rE| j                  |   j                         s(|dz  }||k  r| j                  |   j                         s(||kD  rK| j                  |dz
     j                         s+|dz  }||kD  r!| j                  |dz
     j                         s+t               }| j                  || |j                  dd |S )z] return new source object with trailing
            and leading blank lines removed.
        r   r*   N)r6   r   r   r   )r   r/   r;   sources       r"   r   zSource.stripG   s     D	sck$**U"3"9"9";QJE ck$**U"3"9"9";Ek$**SU"3"9"9";1HC Ek$**SU"3"9"9";**U3/Qr3   c                     t        |      }t        |      }t               }| j                  D cg c]  }||z   	 }}|j                  |z   |j                  z   |_        |S c c}w )ze return a copy of the source object with
            'before' and 'after' wrapped around it.
        r:   )r   beforeafterindentr<   liner   s          r"   	putaroundzSource.putaroundT   s\     uH	.2jj9d6D=99 ,,.%++=	 :s   A c                 h    t               }| j                  D cg c]  }||z   	 c}|_        |S c c}w )zl return a copy of the source object with
            all lines indented by the given indent-string.
        r:   )r   rB   r<   rC   s       r"   rB   zSource.indent_   s1     H	59ZZ@TF4K@	 As   /c                 6    | j                  ||      \  }}| || S )zc return Source statement which contains the
            given linenumber (counted from 0).
        )getstatementrange)r   lineno	assertionr/   r;   s        r"   getstatementzSource.getstatementg   s&     ++FI>
sE#r3   c                     d|cxk  rt        |       k  st        d       t        d      t        ||       \  }}}||fS )z{ return (start, end) tuple which spans the minimal
            statement region which containing the given lineno.
        r   zlineno out of range)r6   r-   getstatementrange_ast)r   rH   rI   astr/   r;   s         r"   rG   zSource.getstatementrangen   sN     V'c$i'233 (233/=UCczr3   Nc                 `    t               }t        | j                  |      |j                  dd |S )a:   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.
        N)r   r
   r   )r   offsetr<   s      r"   r
   zSource.deindentw   s*     H	%djj&9	r3   c                     	 ddl }|j                  }|rt        | j	                               }nt        |       }	  ||dz          y# t        $ r d }Y Bw xY w# t
        $ r  t        $ r Y yw xY w)zb return True if source is parseable, heuristically
            deindenting it by default.
        r   Nc                     t        | dd      S )Nasdexec)compile)r!   s    r"   <lambda>z$Source.isparseable.<locals>.<lambda>   s    wq%'@ r3   r   TF)parsersuiteImportErrorr&   r
   KeyboardInterrupt	Exception)r   r
   rV   syntax_checkerr>   s        r"   isparseablezSource.isparseable   s}    	* $\\N)FYF	6$;' #  	A@N	A ! 	 		s"   A A AAA-,A-c                 8    dj                  | j                        S )Nr   )joinr   r7   s    r"   __str__zSource.__str__   s    yy$$r3   rS   c                    |r/t         j                  j                  |      j                  d      r|t	        j
                  d      }|j                  j                  |j                  }}d| j                  z  }| j                  xj                  dz  c_	        |s|d||fz  z   }n|d|||fz  z   }dj                  | j                        dz   }		 t        |	|||      }
|t        z  r|
S | j                  D cg c]  }|dz   	 }}ddl}dd||f|j                   |<   |
S c c}w # t"        $ r t	        j$                         d   }| j                  d|j&                   }|j(                  r!|j+                  d	|j(                  z  d
z          |j+                  d|z         t#        dj                  |            }|j(                  |_        |j&                  |_        |j,                  |_        |w xY w)z return compiled code object. if filename is None
            invent an artificial filename which displays
            the source/line position of the caller frame.
        r   )fileNr*   z<%d-codegen z%s:%d>z	%r %s:%d>r    ^z*(code was compiled probably from here: %s))r   pathlocalchecksys	_getframef_codeco_filenamef_lineno_compilecounter	__class__r^   r   cpy_compile	_AST_FLAG	linecachecacheSyntaxErrorexc_inforH   rO   appendtext)r   filenamemodeflagdont_inherit	_genframefnrH   baser>   cor!   r   rp   exmsglinesnewexs                    r"   rT   zSource.compile   s    277==288a8@ MM!,	!((44i6H6HvB!D$8$88DNN**a/*(b&\"99+2v0F"FF4::&-	VXtT:B i	)-4Aa$h4E4)*D%(BIOOH%I 5  	"Bzz*299-HyyBII 34OOH8ST		( 34E99EL99ELEJK	s   D# 7D#C
G-) r       )r   Fr5   )T)__name__
__module____qualname____doc__rl   r#   r(   r2   r8   r.   r   rD   rB   rJ   rG   r
   r\   r_   r   compiler_flagrT    r3   r"   r   r      si     O$0:
	2%  $&--$&r3   r   rS   c                     t         )t        | t         j                        rt        | ||||      S t	        j
                  d      }t        |       }|j                  ||||      }|S )z 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.
    r*   )rz   )_astr   ASTrn   rg   rh   r   rT   )r>   rv   rw   flagsry   rz   sr}   s           r"   compile_r      s\     Jvtxx868T5,GGa IvA	
8T5I	>BIr3   c                    	 t         j                  j                  |       }|j                  }|j                  }t        |t              sJ ||fS # t
        $ r 	 t        j                  |       xs t        j                  |       }n# t
        $ r Y Y yw xY w|xr t         j                  j                  |      xs d}d}|r	 t        |       \  }}n# t        $ r Y nw xY wY w xY w)zx Return source location (path, lineno) for the given object.
    If the source cannot be determined return ("", -1)
    )r   r   Nr   )r   codeCoderd   firstlineno	TypeErrorinspectgetsourcefilegetfilere   
findsourceIOErrorr   r+   )objr   fspathrH   r{   _s         r"   getfslinenor      s    "ww||C   !!fc"""6>%  	'', '//#&  		 )b)1T&sO	6 sY   A 	C#,BC#	BC#B.C#CC#	CC#CC#"C#c                     	 t        j                  |       \  }}t               }|D cg c]  }|j                          c}|_        ||fS # t        j                  j                  $ r   Y yxY wc c}w )N)Nr   )r   r   r   r   _sysexr   r   r   )r   sourcelinesrH   r>   rC   s        r"   r   r      sj    %005V
 XF.9:dDKKM:FL6> :: :s   A A/A,c                     t         j                  j                  |       } 	 t        j                  |       }t        |t              sJ t        |fi |S # t
        $ r d}Y +w xY w)Nz<"Buggy python version consider upgrading, cannot get source")	r   r   
getrawcoder   r   IndentationErrorr   r&   r   )r   r   strsrcs      r"   r   r     sf    
''

S
!CR""3' fc"""&#F##  RQRs   A A"!A"c                   
 |D| D ]=  }|j                         }|j                         }|s&t        |      t        |      z
  } n d}|dk(  rt        |       S g }d } ||       
	 t	        j
                  
fd      D ]  \  }}\  }}\  }}}|t        |       kD  r n|t        |      kD  rO| |dz
     j                         }|j                         r|d | j                         r||d  }|j                  |       t        ||      D ]  }	|j                  | |	            |j                  | t        |      d         |S # t        t        j                  f$ r Y :w xY w)Nr   c              3   2   K   | D ]	  }|dz     	 d w)Nr   r   r   )r   rC   s     r"   readline_generatorz$deindent.<locals>.readline_generator  s,      	D+	H s   c                      t               S r5   nextits   r"   rU   zdeindent.<locals>.<lambda>$  s    PTUWPX r3   r*   )
expandtabslstripr6   r   tokenizegenerate_tokensisspacert   ranger   
TokenErrorr   )r   rO   rC   r   newlinesr   r   slineelineir   s             @r"   r
   r
     so   ~ 	D??$DAT3q6)	 F{E{H 
E	"B/7/G/GHX/Y 	*+Aq*5!jua!s5z!s8}$UQY'224;;=T'6]%:%:%<=D%5%( * a)*	*  OOE#h-.)*O	 h112 s   &B;E   EEc                    dd l }g }|j                  |      D ]  }t        |t        j                        st        |t        j
                        s8|j                  |j                  dz
         dD ]6  }t        ||d       }|s|j                  |d   j                  dz
  dz
         8  |j                          t        ||       }||dz
     }|t        |      k\  rd }	||	fS ||   }	||	fS )Nr   r*   )	finalbodyorelse)rM   walkr   r   stmtExceptHandlerrt   rH   getattrsortr   r6   )
rH   noderM   lr!   namevalinsert_indexr/   r;   s
             r"   get_statement_startend2r   8  s     	AXXd^ 4a#z!T5G5G'HHHQXX\"- 4at,HHSV]]Q.23	44 FFH6*LlQEs1v #: o#:r3   c                   
 |t        |      }	 t        |ddd      }t	        | |      \  }}|t        |j                        }||dz   kD  r}t        j                         }|j                  |   d   j                         |_
        d |j                  || D        
	 t        j                  
fd      D ]  } |j                  |   	 |r=|j                  |dz
     j%                         }	|	j'                  d      s|	s|dz  }nn|r=|||fS # t        $ r t        | ||      \  }}d ||fcY S w xY w# t        j                  t        f$ r |j                   |z   }Y t"        $ r Y w xY w)	Nr>   rS   i   r*   r   c              3   &   K   | ]	  }|d z     yw)r   Nr   ).0r!   s     r"   	<genexpr>z(getstatementrange_ast.<locals>.<genexpr>e  s     :Qq4x:s   c                      t               S r5   r   r   s   r"   rU   z'getstatementrange_ast.<locals>.<lambda>g  s    R r3   #)r&   rT   
ValueErrorgetstatementrange_oldr   r6   r   r   BlockFinderr   startedr   r   
tokeneater
EndOfBlockr   lastrZ   r   
startswith)rH   r>   rI   astnodecontentr/   r;   block_findertokrC   r   s             @r"   rL   rL   O  s   f+	$gx>G )9JE3
 {&,,
UQY **,%||E215==?:&,,uS"9:	//0@A .'''-. ||C!G$++-??3t1HC  E3C  	$.vvyIJE3##	$* ""$45 	,##e+C 		s)   D ,D1 D.-D.1(E&E&%E&c                    ddl m} t        | dd      D ]  }|r/|j                  |   }d|v rd|v rd|v rt	        d      d|vrd	|vr4|j                  || d
z    }|j                  dd       dj                  |      }	  ||       t        | d
z   t        |      d
z         D ]  }||| }|j                         s||fc c S   t        d| fz        # t        t        t        f$ r Y w xY w)z 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.
    r   )compile_commandr   superr   r#   zlikely a subclassassertraiser*   z
def xxx():z
 z%no valid source range around line %d )codeopr   r   r   r-   insertr^   rr   OverflowErrorr   r6   r\   )	rH   r>   rI   r   r/   rC   trylines	trysourcer;   s	            r"   r   r   x  s    'vr2& "<<&D$6T>jD6H !455t#t(;<<fQh/ 	<(JJx(		I&
 3v;q=1 	"CuS)I$$&cz!	")"0 =	I
JJ ]J7 		s   >CC+*C+r5   )FNr   )
__future__r   bisectr   rg   r   r   r   typesr   rT   rn   r   r   ro   rX   objectr   r   r   r   r   r   r
   r   rL   r   r   r3   r"   <module>r      s    !  
  	 /tV tt #$$1 <	$'T.&R KW  IDs   
A& &	A21A2