a
    –¥»a*  ã                   @   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ÚOptionalÚTupleÚListÚ
NamedTuple)ÚErrorReport)ÚSourceRangeFactory)ÚobjÚ	error_msgÚreturnc              
   C   sv   d}zt  | ¡}t  | ¡\}}W nJ tyj } z2d| › d�}|rL|d| 7 }t|ƒ|‚W Y d}~n
d}~0 0 |||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   úH/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/_sources.pyÚget_source_lines_and_file	   s    	
 r   )r   r   c                    s”   dd„ ‰ t | ƒD ]\}}| ¡  d¡r|} q0q| | }| 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_prefix/   s    z-normalize_source_lines.<locals>.remove_prefixÚdefr   c                    s   g | ]}ˆˆ |ˆƒ ‘qS r   r   ©Ú.0Ús©r   Ú
whitespacer   r   Ú
<listcomp>=   ó    z*normalize_source_lines.<locals>.<listcomp>Nc                    s   g | ]}ˆˆ |ˆƒ ‘qS r   r   r!   r$   r   r   r&   >   r'   é   )Ú	enumerateÚlstripr   ÚsplitÚappend)r   ÚiÚlÚidxZfn_defZaligned_prefixZaligned_suffixr   r$   r   Únormalize_source_lines!   s     
r0   c                       s   e Zd Zd‡ fdd„	Z‡  ZS )ÚSourceContextTc                    s&   t t| ƒ ||||¡ || _|| _d S r   )Úsuperr1   Ú__init__Úuses_true_divisionr   )ÚselfÚsourcer   r   Úleading_whitespace_lenr4   ©Ú	__class__r   r   r3   H   s    zSourceContext.__init__)T)Ú__name__Ú
__module__Ú__qualname__r3   Ú__classcell__r   r   r8   r   r1   G   s   r1   )Úmaxsizec                  G   s   t | Ž S r   )r1   )Úargsr   r   r   Úmake_source_contextN   s    r@   c                   C   s   t dd ddƒ dd¡S )NÚ r   r(   )r1   Zmake_raw_ranger   r   r   r   Ú
fake_rangeS   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__r1   Ústrr   Úintr   r   r   r   rC   W   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ƒ}t|||||ƒS )NrA   r(   r   z&Expected a single top-level function: ú:r   T)r   r   Z
call_stackr0   Újoinr   rD   Úparser   ÚbodyÚ
isinstanceÚFunctionDefÚRuntimeErrorr+   r@   rC   )	Úfnr   r   r   r6   Z
dedent_srcZpy_astr7   rE   r   r   r   Ú	parse_def^   s    

 (rR   )N)rD   Ú	functoolsr   Útextwrapr   Útypingr   r   r   r   r   Ztorch._Cr   Ztorch._C._jit_tree_viewsr	   rH   rI   r   r0   r1   Ú	lru_cacher@   rB   rC   rR   r   r   r   r   Ú<module>   s&    þý&

