U
    42in                     @  s   d dl mZ d dl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
 d dl	mZ d dl	mZ d dlZG dd dZd	d
ddZd%ddddddZdddddZddddddZd&dddd d!d"d#d$ZdS )'    )annotationsN)bisect_right)Iterable)Iterator)overloadc                   @  s   e Zd ZdZd,dddddZddd	d
dZdZedddddZedd dddZdddddZddddZ	ddddZ
d dddZd-dd dd d!Zdd d"d#d$Zdd%d"d&d'Zd dd(d)Zddd*d+ZdS ).SourcezvAn immutable object holding a source code fragment.

    When using Source(...), the source lines are deindented.
    NobjectNone)objreturnc                 C  s   |sg | _ nt|tr |j | _ nt|ttfrDtdd |D | _ nbt|tr`t|d| _ nFzt|}t	
|}W n tk
r   t	
|}Y nX t|d| _ d S )Nc                 s  s   | ]}| d V  qdS 
Nrstrip.0x r   8/tmp/pip-unpacked-wheel-7n2p7kht/_pytest/_code/source.py	<genexpr>   s     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__bool)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__intr   )keyr   c                 C  s   d S r&   r   r!   r*   r   r   r   __getitem__/   s    zSource.__getitem__slicec                 C  s   d S r&   r   r+   r   r   r   r,   2   s    zint | slicezstr | Sourcec                 C  sH   t |tr| j| S |jdkr&tdt }| j|j|j |_|S d S )N)N   z!cannot slice a Source with a step)r   r)   r   step
IndexErrorr   startstop)r!   r*   	newsourcer   r   r   r,   5   s    


zIterator[str]r   c                 C  s
   t | jS r&   )iterr   r!   r   r   r   __iter__?   s    zSource.__iter__c                 C  s
   t | jS r&   )lenr   r6   r   r   r   __len__B   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:   E   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>T   s     z!Source.indent.<locals>.<listcomp>)r   r   )r!   r>   r3   r   rA   r   r>   P   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   getstatementW   s    zSource.getstatementztuple[int, int]c                 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   e   s    zSource.deindentc                 C  s   d | jS Nr   )joinr   r6   r   r   r   __str__k   s    zSource.__str__)N)r=   )__name__
__module____qualname____doc__r#   r(   __hash__r   r,   r7   r9   r:   r>   rE   rD   r   rJ   r   r   r   r   r      s"   
r   ztuple[Source | None, int]r4   c                 C  sH   zt | \}}W n tk
r(   Y dS X t }dd |D |_||fS )N)Nc                 S  s   g | ]}|  qS r   r   r?   r   r   r   rB   z   s     zfindsource.<locals>.<listcomp>)r   
findsource	Exceptionr   r   )r
   sourcelinesrC   r<   r   r   r   rQ   t   s    rQ   Tr   r$   ztypes.CodeType)r
   trycallr   c                 C  sZ   z| j W S  tk
r   Y nX |rHt| dd}|rHt| tsHt|ddS td| dS )z&Return code object for given function.__call__NF)rT   zcould not get code object for )__code__AttributeErrorgetattrr   typer   r    )r
   rT   callr   r   r   r   ~   s    r   zIterable[str]z	list[str])r   r   c                 C  s   t d|  S rH   )textwrapdedentrI   
splitlines)r   r   r   r   r      s    r   r)   zast.ASTztuple[int, int | None])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.   )Z	finalbodyZorelser   )rG   walkr   ZstmtZExceptHandlerZClassDefZFunctionDefZAsyncFunctionDefZdecorator_listappendrC   rX   sortr   r8   )
rC   r^   valuesr   dnamevalZinsert_indexr1   r;   r   r   r   get_statement_startend2   s$    

rf   Fzast.AST | Noneztuple[ast.AST, int, int])rC   r<   	assertionastnoder   c           
   	     sR  |d kr<t |}t  td t|dd}W 5 Q R X t| |\}}|d kr\t|j}||d krt	
 }t|j| o|j| d  |_dd |j|| D  z&t fdd	D ]}|j|  qW n8 t	jtfk
r   |j| }Y n tk
r
   Y nX |rH|j|d   }	|	d
s6|	sH|d8 }nqHq|||fS )Nignorer<   execr.   r   c                 s  s   | ]}|d  V  qdS r   r   r   r   r   r   r      s     z(getstatementrange_ast.<locals>.<genexpr>c                     s   t  S r&   )nextr   itr   r   <lambda>       z'getstatementrange_ast.<locals>.<lambda>#)r   warningscatch_warningssimplefilterrG   parserf   r8   r   r   BlockFinderr$   isspacestartedtokenizegenerate_tokens
tokeneater
EndOfBlockIndentationErrorlastrR   lstrip
startswith)
rC   r<   rg   rh   contentr1   r;   Zblock_findertokr@   r   rl   r   rF      s4    



rF   )T)FN)
__future__r   rG   bisectr   r   r[   rx   typestypingr   r   r   rq   r   rQ   r   r   rf   rF   r   r   r   r   <module>   s$   d
  