U
    ,-eK                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
 d dlmZ d dlmZ dee	e e
ee ee	e f dddZee ee d	d
dZG dd deZejdddd Zdd ZG dd deZdd ZdS )    N)dedent)AnyList
NamedTupleOptionalTuple)ErrorReport)SourceRangeFactory)obj	error_msgreturnc              
   C   st   d}zt | }t | \}}W nH tk
rh } z*d|  d}|rN|d| 7 }t||W 5 d}~X Y nX |||fS )z{
    Wrapper around inspect.getsourcelines and inspect.getsourcefile.

    Returns: (sourcelines, file_lino, filename)
    NzCan't get source for zs. TorchScript requires source access in order to carry out compilation, make sure original .py files are available.
)inspectgetsourcefilegetsourcelinesOSError)r
   r   filenamesourcelinesfile_linenoemsg r   O/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/torch/_sources.pyget_source_lines_and_file   s    	

r   )r   r   c                    s   dd  d}t | D ]\}}| dr|} q4q|dkr@| S | | }|dd  fdd| d| D } fdd| |d	 d D }|| || S )
a   
    This helper function accepts a list of source lines. It finds the
    indentation level of the function definition (`def`), then it indents
    all lines in the function body to a point at or greater than that
    level. This allows for comments and continued string literals that
    are at a lower indentation than the rest of the code.
    Args:
        sourcelines: function source code, separated into lines by
                        the '
' character
    Returns:
        A list of source lines that have been correctly aligned
    c                 S   s   | |  |ot|d  S N)
startswithlen)textprefixr   r   r   remove_prefix3   s    z-normalize_source_lines.<locals>.remove_prefixNdefr   c                    s   g | ]} | qS r   r   .0sr   
whitespacer   r   
<listcomp>H   s    z*normalize_source_lines.<locals>.<listcomp>c                    s   g | ]} | qS r   r   r!   r$   r   r   r&   K   s       )	enumeratelstripr   splitappend)r   idxilZfn_defZaligned_prefixZaligned_suffixr   r$   r   normalize_source_lines%   s$    

r/   c                       s   e Zd Zd fdd	Z  ZS )SourceContextTNc                    s(   t  |||| || _|| _|| _d S r   )super__init__uses_true_divisionr   funcname)selfsourcer   r   leading_whitespace_lenr3   r4   	__class__r   r   r2   W   s    	zSourceContext.__init__)TN)__name__
__module____qualname__r2   __classcell__r   r   r8   r   r0   V   s     r0   )maxsizec                  G   s   t |  S r   )r0   )argsr   r   r   make_source_contextf   s    r@   c                   C   s   t dd ddddS )N r   r'   )r0   Zmake_raw_ranger   r   r   r   
fake_rangek   s    rB   c                   @   s<   e Zd ZU ejed< eed< eed< ee ed< e	ed< dS )	ParsedDefastctxr6   r   r   N)
r:   r;   r<   rD   Module__annotations__r0   strr   intr   r   r   r   rC   o   s
   

rC   c           	      C   s   t | t \}}}t|}d|}t|}t|}t|j	dksXt
|j	d tjsltd| d| t|ddd t|ddd  }t||||d| j}t|||||S )NrA   r'   r   z&Expected a single top-level function: :r   T)r   r   Z
call_stackr/   joinr   rD   parser   body
isinstanceFunctionDefRuntimeErrorr*   r@   r:   rC   )	fnr   r   r   r6   Z
dedent_srcZpy_astr7   rE   r   r   r   	parse_defw   s0     


      rR   )N)rD   	functoolsr   textwrapr   typingr   r   r   r   r   Ztorch._Cr   Ztorch._C._jit_tree_viewsr	   rH   rI   r   r/   r0   	lru_cacher@   rB   rC   rR   r   r   r   r   <module>   s&    1

