a
    !f                  	   @   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 | _ nt|tr |j | _ nt|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   U/var/www/html/python-backend/venv/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)Nc                 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rd }	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|  qW 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
   