a
    ˆÏ?e  ã                	   @   s4  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 d dlm	Z	 d dlm
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ G d
d„ dƒZeee ef dœdd„Zdeeejdœdd„Ze	e ee dœdd„Zee jeeee f dœdd„Zdeeeee j ee jeef dœdd„ZdS )é    N)Úbisect_right)ÚIterable)ÚIterator)ÚList)ÚOptional)Úoverload)ÚTuple)ÚUnionc                   @   s  e Zd ZdZd"eddœdd„Zeedœdd„ZdZe	e
ed	œd
d„ƒZe	ed d	œdd„ƒZee
ef eed f d	œdd„Zee dœdd„Ze
dœdd„Zd dœdd„Zd#ed dœdd„Ze
d dœdd„Ze
ee
e
f dœdd„Zd dœdd„Zedœd d!„ZdS )$ÚSourcezvAn immutable object holding a source code fragment.

    When using Source(...), the source lines are deindented.
    N)ÚobjÚreturnc                 C   s¨   |sg | _ n˜t|tƒr |j | _ n„t|ttfƒrDtdd„ |D ƒƒ| _ n`t|tƒr`t| d¡ƒ| _ nDzt|ƒ}t	 
|¡}W n ty’   t	 
|¡}Y n0 t| d¡ƒ| _ d S )Nc                 s   s   | ]}|  d ¡V  qdS ©Ú
N©Úrstrip©Ú.0Úx© r   úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/_pytest/_code/source.pyÚ	<genexpr>   ó    z"Source.__init__.<locals>.<genexpr>r   )ÚlinesÚ
isinstancer
   ÚtupleÚlistÚdeindentÚstrÚsplitÚ
getrawcodeÚinspectÚ	getsourceÚ	TypeError)Úselfr   ZrawcodeÚsrcr   r   r   Ú__init__   s    


zSource.__init__)Úotherr   c                 C   s   t |tƒstS | j|jkS ©N)r   r
   ÚNotImplementedr   )r#   r&   r   r   r   Ú__eq__(   s    
zSource.__eq__)Úkeyr   c                 C   s   d S r'   r   ©r#   r*   r   r   r   Ú__getitem__0   s    zSource.__getitem__c                 C   s   d S r'   r   r+   r   r   r   r,   4   s    c                 C   sH   t |tƒr| j| S |jdvr&tdƒ‚tƒ }| j|j|j… |_|S d S )N)Né   z!cannot slice a Source with a step)r   Úintr   ÚstepÚ
IndexErrorr
   ÚstartÚstop)r#   r*   Ú	newsourcer   r   r   r,   8   s    


©r   c                 C   s
   t | jƒS r'   )Úiterr   ©r#   r   r   r   Ú__iter__B   s    zSource.__iter__c                 C   s
   t | jƒS r'   )Úlenr   r6   r   r   r   Ú__len__E   s    zSource.__len__c                 C   st   dt | ƒ }}||k r.| j|  ¡ s.|d7 }q||krR| j|d   ¡ sR|d8 }q.tƒ }| j||… |jdd…< |S )zGReturn new Source object with trailing and leading blank lines removed.r   r-   N)r8   r   Ústripr
   )r#   r1   ÚendÚsourcer   r   r   r:   H   s    

zSource.stripú    )Úindentr   c                    s    t ƒ }‡ fdd„| jD ƒ|_|S )z^Return a copy of the source object with all lines indented by the
        given indent-string.c                    s   g | ]}ˆ | ‘qS r   r   ©r   Úline©r>   r   r   Ú
<listcomp>W   r   z!Source.indent.<locals>.<listcomp>)r
   r   )r#   r>   r3   r   rA   r   r>   S   s    zSource.indent)Úlinenor   c                 C   s   |   |¡\}}| ||… S )zUReturn Source statement which contains the given linenumber
        (counted from 0).)Úgetstatementrange)r#   rC   r1   r;   r   r   r   ÚgetstatementZ   s    zSource.getstatementc                 C   s:   d|  krt | ƒk s"n tdƒ‚t|| ƒ\}}}||fS )zmReturn (start, end) tuple which spans the minimal statement region
        which containing the given lineno.r   zlineno out of range)r8   r0   Úgetstatementrange_ast)r#   rC   Úastr1   r;   r   r   r   rD   `   s    zSource.getstatementrangec                 C   s   t ƒ }t| jƒ|jdd…< |S )z&Return a new Source object deindented.N)r
   r   r   )r#   r3   r   r   r   r   h   s    zSource.deindentc                 C   s   d  | j¡S ©Nr   )Újoinr   r6   r   r   r   Ú__str__n   s    zSource.__str__)N)r=   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úobjectr%   Úboolr)   Ú__hash__r   r.   r   r,   Úslicer	   r   r7   r9   r:   r>   rE   r   rD   r   rJ   r   r   r   r   r
      s"    
r
   r4   c                 C   sF   zt  | ¡\}}W n ty&   Y dS 0 tƒ }dd„ |D ƒ|_||fS )N)Néÿÿÿÿc                 S   s   g | ]}|  ¡ ‘qS r   r   r?   r   r   r   rB   }   r   zfindsource.<locals>.<listcomp>)r    Ú
findsourceÚ	Exceptionr
   r   )r   ÚsourcelinesrC   r<   r   r   r   rT   w   s    rT   T)r   Útrycallr   c                 C   sX   z| j W S  ty   Y n0 |rFt| ddƒ}|rFt| tƒsFt|dd�S td| ›�ƒ‚dS )z&Return code object for given function.Ú__call__NF)rW   zcould not get code object for )Ú__code__ÚAttributeErrorÚgetattrr   Útyper   r"   )r   rW   Úcallr   r   r   r   �   s    r   )r   r   c                 C   s   t  d | ¡¡ ¡ S rH   )ÚtextwrapÚdedentrI   Ú
splitlines)r   r   r   r   r   Ž   s    r   )rC   Únoder   c           
      C   sÚ   g }t  |¡D ]Š}t|t jt jfƒrt|t jt jt jfƒrV|jD ]}| 	|j
d ¡ q@| 	|j
d ¡ dD ],}t||d ƒ}|rj| 	|d j
d d ¡ qjq| ¡  t|| ƒ}||d  }|t|ƒkrÊd }	n|| }	||	fS )Nr-   )Ú	finalbodyÚorelser   )rG   Úwalkr   ÚstmtÚExceptHandlerÚClassDefÚFunctionDefÚAsyncFunctionDefÚdecorator_listÚappendrC   r[   Úsortr   r8   )
rC   ra   Úvaluesr   ÚdÚnameÚvalZinsert_indexr1   r;   r   r   r   Úget_statement_startend2’   s$    

rq   F)rC   r<   Ú	assertionÚastnoder   c           
   	      sT  |d u rPt |ƒ}t ¡ �( t d¡ t |dd¡}W d   ƒ n1 sF0    Y  t| |ƒ\}}|d u rpt|jƒ}||d k�rt	 
¡ }|j| d  ¡ |_dd„ |j||… D ƒ‰ z&t ‡ fdd	„¡D ]}|j|Ž  qÆW n4 t	jtfyú   |j| }Y n t�y   Y n0 |�rJ|j|d   ¡ }	|	 d
¡�s8|	�sJ|d8 }n�qJ�q|||fS )NÚignorer<   Úexecr-   r   c                 s   s   | ]}|d  V  qdS r   r   r   r   r   r   r   É   r   z(getstatementrange_ast.<locals>.<genexpr>c                      s   t ˆ ƒS r'   )Únextr   ©Úitr   r   Ú<lambda>Ë   r   z'getstatementrange_ast.<locals>.<lambda>ú#)r   ÚwarningsÚcatch_warningsÚsimplefilterrG   Úparserq   r8   r   r    ÚBlockFinderÚisspaceÚstartedÚtokenizeÚgenerate_tokensÚ
tokeneaterÚ
EndOfBlockÚIndentationErrorÚlastrU   ÚlstripÚ
startswith)
rC   r<   rr   rs   Úcontentr1   r;   Zblock_finderÚtokr@   r   rw   r   rF   ­   s2    

,

rF   )T)FN)rG   r    r^   r‚   Útypesr{   Úbisectr   Útypingr   r   r   r   r   r   r	   r
   r.   rT   rO   rP   ÚCodeTyper   r   r   ÚASTrq   rF   r   r   r   r   Ú<module>   s6   f
   üû