o
    Ô2úhã  ã                   @   sô   d Z ddlZddlZddlZddlmZ ddlmZ ejZ	e 
dd„ ¡Ze 
dd„ ¡ZG dd	„ d	eƒZG d
d„ deƒZi ZG dd„ deƒZG dd„ deƒZeƒ ZG dd„ deƒZG dd„ deƒZddd„Zdd„ ZG dd„ dejƒZejZejZdS )zGFunctions used to extract and analyze stacks.  Faster than Python libs.é    N)Úgraph_debug_info_pb2)Ú	_tf_stackc                   C   ó   t ƒ gS ©N)ÚSentinelMapper© r   r   úZ/var/www/html/chatgem/venv/lib/python3.10/site-packages/tensorflow/python/util/tf_stack.pyÚ<lambda>#   ó    r	   c                   C   r   r   )ÚSentinelFilterr   r   r   r   r	   $   r
   c                   @   s0   e Zd ZdZdZdZdd„ Zdd„ Zdd„ ZdS )	ÚStackTraceTransformz4Base class for stack trace transformation functions.Nc                 C   sT   | j d u r
tƒ | _ n
| j tƒ ksJ dƒ‚| j| j  }|d | _| | ¡ |  ¡  | S )NzShared across threads?éÿÿÿÿ)Ú_thread_keyÚ_get_thread_keyÚ_stack_dictÚparentÚappendÚupdate)ÚselfÚstackr   r   r   Ú	__enter__-   s   



zStackTraceTransform.__enter__c                 C   s$   | j | j  ¡ }|| u sJ dƒ‚d S )NzConcurrent access?)r   r   Úpop)r   Úunused_typeÚunused_valueÚunused_tracebackÚtopr   r   r   Ú__exit__;   s   zStackTraceTransform.__exit__c                 C   ó   t dƒ‚©Nú subclasses need to override this©ÚNotImplementedError©r   r   r   r   r   ?   ó   zStackTraceTransform.update)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r   '   s    r   c                   @   ó,   e Zd ZdZeZdd„ Zdd„ Zdd„ ZdS )	ÚStackTraceMapperz@Allows remapping traceback information to different source code.c                 C   ó   t  ¡ | _d S r   )r   ÚPyBindSourceMapÚinternal_mapr"   r   r   r   Ú__init__G   ó   zStackTraceMapper.__init__c                 C   s   | j  t|  ¡  ¡ ƒ¡ d S r   )r,   Ú	update_toÚtupleÚget_effective_source_mapÚitemsr"   r   r   r   r   J   s   zStackTraceMapper.updatec                 C   r   )zFReturns a map (filename, lineno) -> (filename, lineno, function_name).r   r    r"   r   r   r   r1   M   s   z)StackTraceMapper.get_effective_source_mapN)	r$   r%   r&   r'   Ú_source_mapper_stacksr   r-   r   r1   r   r   r   r   r)   C   ó    r)   c                   @   ó   e Zd Zdd„ ZdS )r   c                 C   ó   t S r   )Ú
EMPTY_DICTr"   r   r   r   r1   W   ó   z'SentinelMapper.get_effective_source_mapN)r$   r%   r&   r1   r   r   r   r   r   U   ó    r   c                   @   r(   )	ÚStackTraceFilterzFAllows filtering traceback information by removing superfluous frames.c                 C   r*   r   )r   ÚPyBindFileSetÚinternal_setr"   r   r   r   r-   _   r.   zStackTraceFilter.__init__c                 C   s   | j  t|  ¡ ƒ¡ d S r   )r<   r/   ÚsetÚget_filtered_filenamesr"   r   r   r   r   b   s   zStackTraceFilter.updatec                 C   r   r   r    r"   r   r   r   r>   e   r#   z'StackTraceFilter.get_filtered_filenamesN)	r$   r%   r&   r'   Ú_source_filter_stacksr   r-   r   r>   r   r   r   r   r:   [   r4   r:   c                   @   r5   )r   c                 C   r6   r   )Ú	EMPTY_SETr"   r   r   r   r>   n   r8   z%SentinelFilter.get_filtered_filenamesN)r$   r%   r&   r>   r   r   r   r   r   l   r9   r   c                       s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )ÚCurrentModuleFilterzFFilters stack frames from the module where this is used (best effort).c                    sX   t ƒ  ¡  d }d }t ¡ }z|d ur|j}|d urt |¡}|| _d | _W ~~d S ~~w r   )Úsuperr-   ÚinspectÚcurrentframeÚf_backÚgetfileÚ	_filenameÚ_cached_set)r   Úfilter_filenameÚouter_fÚf©Ú	__class__r   r   r-   u   s   

ÿzCurrentModuleFilter.__init__c                 C   s>   | j d ur| j S t| jfƒ}| jd ur|| j ¡ O }|| _ |S r   )rH   Ú	frozensetrG   r   r>   )r   Úfiltered_filenamesr   r   r   r>   ‹   s   

z*CurrentModuleFilter.get_filtered_filenames)r$   r%   r&   r'   r-   r>   Ú__classcell__r   r   rL   r   rA   r   s    rA   é   c                 C   s(   t ƒ }t t| d jt| d j| ¡S )aw  An eager-friendly alternative to traceback.extract_stack.

  Args:
    stacklevel: number of initial frames to skip when producing the stack.

  Returns:
    A list-like FrameSummary containing StackFrame-like objects, which are
    namedtuple-like objects with the following fields: filename, lineno, name,
    line, meant to masquerade as traceback.FrameSummary objects.
  r   )r   r   Úextract_stackr3   r,   r?   r<   )Ú
stacklevelÚ
thread_keyr   r   r   rR   –   s   ýrR   c                 C   s   t  |  ¡ ¡S r   )r   ÚLoadTracesFromDebugInfoÚSerializeToString)Ú
debug_infor   r   r   rU   ©   r.   rU   c                       s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )ÚGraphDebugInfoBuilderc                    s   |  ¡ }tƒ  ||¡ d S r   )rV   rB   ÚAppendGraphDebugInfo)r   Úfn_nameÚfn_debug_infoÚdebug_info_strrL   r   r   rY   ¯   s   z*GraphDebugInfoBuilder.AppendGraphDebugInfoc                    s    t ƒ  ¡ }t ¡ }| |¡ |S r   )rB   ÚBuildr   ÚGraphDebugInfoÚParseFromString)r   r\   rW   rL   r   r   r]   ³   s   

zGraphDebugInfoBuilder.Build)r$   r%   r&   rY   r]   rP   r   r   rL   r   rX   ­   s    rX   )rQ   )r'   ÚcollectionsrC   Ú	threadingÚtensorflow.core.frameworkr   Útensorflow.python.utilr   Ú	get_identr   Údefaultdictr3   r?   Úobjectr   r)   r7   r   r:   rN   r@   r   rA   rR   rU   rX   Ú
StackTraceÚStackSummaryÚ
StackFrameÚFrameSummaryr   r   r   r   Ú<module>   s,   
$
