o
    ®Ÿ?eé  ã                   @   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   ú`/home/www/facesmatcher.com/pyenv/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   Zunused_typeZunused_valueZ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   Z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   r-   M   s   z)StackTraceMapper.get_effective_source_mapN)	r!   r"   r#   r$   Ú_source_mapper_stacksr   r)   r   r-   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   r-   W   ó   z'SentinelMapper.get_effective_source_mapN)r!   r"   r#   r-   r   r   r   r   r   U   ó    r   c                   @   r%   )	ÚStackTraceFilterzFAllows filtering traceback information by removing superfluous frames.c                 C   r'   r   )r   ZPyBindFileSetÚinternal_setr   r   r   r   r)   _   r*   zStackTraceFilter.__init__c                 C   s   | j  t|  ¡ ƒ¡ d S r   )r7   r+   ÚsetÚget_filtered_filenamesr   r   r   r   r   b   s   zStackTraceFilter.updatec                 C   r   r   r   r   r   r   r   r9   e   r    z'StackTraceFilter.get_filtered_filenamesN)	r!   r"   r#   r$   Ú_source_filter_stacksr   r)   r   r9   r   r   r   r   r6   [   r0   r6   c                   @   r1   )r   c                 C   r2   r   )Ú	EMPTY_SETr   r   r   r   r9   n   r4   z%SentinelFilter.get_filtered_filenamesN)r!   r"   r#   r9   r   r   r   r   r   l   r5   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ÚgetsourcefileÚ	_filenameÚ_cached_set)r   Zfilter_filenameZ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   )rC   Ú	frozensetrB   r   r9   )r   Zfiltered_filenamesr   r   r   r9   ‹   s   

z*CurrentModuleFilter.get_filtered_filenames)r!   r"   r#   r$   r)   r9   Ú__classcell__r   r   rE   r   r<   r   s    r<   é   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_stackr/   r(   r:   r7   )Ú
stacklevelZ
thread_keyr   r   r   rJ   –   s   ýrJ   c                 C   s   t  |  ¡ ¡S r   )r   ÚLoadTracesFromDebugInfoÚSerializeToString)Ú
debug_infor   r   r   rL   ©   r*   rL   c                       s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )ÚGraphDebugInfoBuilderc                    s   |  ¡ }tƒ  ||¡ d S r   )rM   r=   ÚAppendGraphDebugInfo)r   Úfn_nameZfn_debug_infoÚdebug_info_strrE   r   r   rP   ¯   s   z*GraphDebugInfoBuilder.AppendGraphDebugInfoc                    s    t ƒ  ¡ }t ¡ }| |¡ |S r   )r=   ÚBuildr   ZGraphDebugInfoZParseFromString)r   rR   rN   rE   r   r   rS   ³   s   

zGraphDebugInfoBuilder.Build)r!   r"   r#   rP   rS   rH   r   r   rE   r   rO   ­   s    rO   )rI   )r$   Úcollectionsr>   Ú	threadingZtensorflow.core.frameworkr   Ztensorflow.python.utilr   Ú	get_identr   Údefaultdictr/   r:   Úobjectr   r&   r3   r   r6   rG   r;   r   r<   rJ   rL   rO   Z
StackTraceÚStackSummaryZ
StackFrameÚFrameSummaryr   r   r   r   Ú<module>   s,   
$
