U
    ªÊ¹jK  ã                   @   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   úM/var/www/html/bwave/python/venv/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

