o
    h                     @   sJ  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deee ef fddZd!dededejfddZde	e dee fddZdede jdeeee f fddZ		d"dedededee j dee jeef f
dd ZdS )#    N)bisect_right)Iterable)Iterator)List)Optional)overload)Tuple)Unionc                   @   s
  e Zd ZdZd"deddfddZdedefdd	ZdZe	d
e
defddZe	d
edd fddZd
ee
ef deed f fddZdee fddZde
fddZd#ddZd$dedd fddZde
dd fddZde
dee
e
f fddZd#ddZdef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 | _ d S t|tr|j | _ d S t|ttfr%tdd |D | _ d S t|tr4t|d| _ d S zt|}t	
|}W n tyM   t	
|}Y nw t|d| _ d S )Nc                 s   s    | ]}| d V  qdS 
Nrstrip.0x r   J/var/www/html/ai/venv/lib/python3.10/site-packages/_pytest/_code/source.py	<genexpr>   s    z"Source.__init__.<locals>.<genexpr>r   )lines
isinstancer
   tuplelistdeindentstrsplit
getrawcodeinspect	getsource	TypeError)selfr   rawcodesrcr   r   r   __init__   s   


zSource.__init__otherc                 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__keyc                 C      d S r'   r   r"   r*   r   r   r   __getitem__0      zSource.__getitem__c                 C   r+   r'   r   r,   r   r   r   r-   4   r.   c                 C   sD   t |tr
| j| S |jdvrtdt }| j|j|j |_|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   


c                 C   
   t | jS r'   )iterr   r"   r   r   r   __iter__B      
zSource.__iter__c                 C   r6   r'   )lenr   r8   r   r   r   __len__E   r:   zSource.__len__c                 C   s   dt | }}||k r!| j|  s!|d7 }||k r!| j|  r||kr?| j|d   s?|d8 }||kr?| j|d   r.t }| j|| |jdd< |S )zGReturn new Source object with trailing and leading blank lines removed.r   r/   N)r;   r   stripr
   )r"   r3   endsourcer   r   r   r=   H   s   zSource.strip    indentc                    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rA   r   r   
<listcomp>W       z!Source.indent.<locals>.<listcomp>)r
   r   )r"   rA   r5   r   rD   r   rA   S   s   zSource.indentlinenoc                 C   s   |  |\}}| || S )zUReturn Source statement which contains the given linenumber
        (counted from 0).)getstatementrange)r"   rG   r3   r>   r   r   r   getstatementZ   s   zSource.getstatementc                 C   s@   d|  krt | k std 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)r;   r2   getstatementrange_ast)r"   rG   astr3   r>   r   r   r   rH   `   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"   r5   r   r   r   r   h   s   zSource.deindentc                 C   s   d | jS Nr   )joinr   r8   r   r   r   __str__n   s   zSource.__str__r'   )r   r
   )r@   )__name__
__module____qualname____doc__objectr%   boolr)   __hash__r   r0   r   r-   slicer	   r   r9   r<   r=   rA   rI   r   rH   r   rN   r   r   r   r   r
      s$    "


r
   r   c                 C   sF   z	t | \}}W n
 ty   Y dS w t }dd |D |_||fS )N)Nc                 S   s   g | ]}|  qS r   r   rB   r   r   r   rE   }   rF   zfindsource.<locals>.<listcomp>)r   
findsource	Exceptionr
   r   )r   sourcelinesrG   r?   r   r   r   rX   w   s   rX   Tr   trycallc                 C   sT   z| j W S  ty   Y nw |r#t| dd}|r#t| ts#t|ddS td| )z&Return code object for given function.__call__NF)r[   zcould not get code object for )__code__AttributeErrorgetattrr   typer   r!   )r   r[   callr   r   r   r      s   r   r   c                 C   s   t d|  S rL   )textwrapdedentrM   
splitlines)r   r   r   r   r      s   r   rG   nodec           
      C   s   g }t |D ]E}t|t jt jfrLt|t jt jt jfr+|jD ]
}|	|j
d  q |	|j
d  dD ]}t||d }|rK|	|d j
d d  q5q|  t|| }||d  }|t|krhd }	||	fS || }	||	fS )Nr/   )	finalbodyorelser   )rK   walkr   stmtExceptHandlerClassDefFunctionDefAsyncFunctionDefdecorator_listappendrG   r_   sortr   r;   )
rG   re   valuesr   dnamevalinsert_indexr3   r>   r   r   r   get_statement_startend2   s(   

rv   Fr?   	assertionastnodec           
   	      sH  |d u r(t |}t  td t|dd}W d    n1 s#w   Y  t| |\}}|d u r8t|j}||d krt	
 }|j| d  |_dd |j|| D  zt fdd	D ]}|j|  qbW n t	jtfy|   |j| }Y n	 ty   Y nw |r|j|d   }	|	d
s|	s|d8 }nn|s|||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>   s    z'getstatementrange_ast.<locals>.<lambda>#)r   warningscatch_warningssimplefilterrK   parserv   r;   r   r   BlockFinderisspacestartedtokenizegenerate_tokens
tokeneater
EndOfBlockIndentationErrorlastrY   lstrip
startswith)
rG   r?   rw   rx   contentr3   r>   block_findertokrC   r   r|   r   rJ      s:   




rJ   )T)FN)rK   r   rb   r   typesr   bisectr   typingr   r   r   r   r   r   r	   r
   r0   rX   rS   rT   CodeTyper   r   r   ASTrv   rJ   r   r   r   r   <module>   s@    f
$