o
    ADiz?                     @  sd  d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	 ddl
mZ ddlmZmZ ddlmZmZmZmZmZ z
ddlZddlZW n eyO   dZY nw dd	lmZ g d
ZdZdZdZdZeeed f ZeG dd dZeG dd dZ eG dd dZ!eG dd dZ"d8ddZ#			d9d:d$d%Z$d&eedd&dd'd;d4d5Z%G d6d7 d7Z&dS )<z
Extract a structured traceback from an exception.

Based on work by Will McGugan
<https://github.com/hynek/structlog/pull/407#issuecomment-1150926246>`_ from
`rich.traceback
<https://github.com/Textualize/rich/blob/972dedff/rich/traceback.py>`_.
    )annotationsN)asdict	dataclassfield)walk_tb)
ModuleTypeTracebackType)AnyIterableSequenceTupleUnion   )ExcInfo)ExceptionDictTransformerFrameStackSyntaxError_Traceextractsafe_strto_reprT
   P   2   )NNNc                   @  s6   e Zd ZU dZded< ded< ded< dZded	< dS )
r   z*
    Represents a single stack frame.
    strfilenameintlinenonameNzdict[str, str] | Nonelocals)__name__
__module____qualname____doc____annotations__r     r&   r&   M/var/www/Datamplify/venv/lib/python3.10/site-packages/structlog/tracebacks.pyr   8   s   
 r   c                   @  s:   e Zd ZU dZded< ded< ded< ded< ded< d	S )
r   zL
    Contains detailed information about :exc:`SyntaxError` exceptions.
    r   offsetr   r   liner   msgNr!   r"   r#   r$   r%   r&   r&   r&   r'   r   D   s   
 r   c                   @  s|   e Zd ZU dZded< ded< eedZded< dZd	ed
< dZ	ded< eedZ
ded< dZded< eedZded< dS )r   z
    Represents an exception and a list of stack frames.

    .. versionchanged:: 25.2.0
       Added the *exc_notes* field.

    .. versionchanged:: 25.4.0
       Added the *is_group* and *exceptions* fields.
    r   exc_type	exc_value)default_factoryz	list[str]	exc_notesNzSyntaxError_ | Nonesyntax_errorFboolis_causezlist[Frame]framesis_groupzlist[Trace]
exceptions)r!   r"   r#   r$   r%   r   listr/   r0   r2   r3   r4   r5   r&   r&   r&   r'   r   Q   s   
 
r   c                   @  s   e Zd ZU dZded< dS )r   z/
    Container for a list of stack traces.
    zlist[Stack]stacksNr+   r&   r&   r&   r'   r   g   s   
 r   _objectr	   returnr   c              
   C  s@   zt | W S  ty } zdt |dW  Y d}~S d}~ww )z1Don't allow exceptions from __str__ to propagate.z<str-error >N)r   	Exception)r8   errorr&   r&   r'   r   p   s   
r   obj
max_length
int | None
max_stringuse_richr1   c              
   C  s  |rt durt jj| ||d }|S zTt| ttfr@|dur9t| |kr9t| | }| d| d| }W |S t| }W |S t| }|durbt||kret|| }|d| d| }W |S W |S W |S  t	y } zdt|d}W Y d}~|S d}~ww )a  
    Get repr string for an object, but catch errors.

    :func:`repr()` is used for strings, too, so that secret wrappers that
    inherit from :func:`str` and overwrite ``__repr__()`` are handled correctly
    (i.e. secrets are not logged in plain text).

    Args:
        obj: Object to get a string representation for.

        max_length: Maximum length of containers before abbreviating, or
            ``None`` for no abbreviation.

        max_string: Maximum length of string before truncating, or ``None`` to
            disable truncating.

        use_rich: If ``True`` (the default), use rich_ to compute the repr.
            If ``False`` or if rich_ is not installed, fall back to a simpler
            algorithm.

    Returns:
        The string representation of *obj*.

    .. versionchanged:: 24.3.0
       Added *max_length* argument.  Use :program:`rich` to render locals if it
       is available.  Call :func:`repr()` on strings in fallback
       implementation.
    N)r>   r@   +z<repr-error r:   )
richprettytraverserender
isinstancer   byteslenreprr;   )r=   r>   r@   rA   obj_repr	truncatedr<   r&   r&   r'   r   x   s:   "
	r   Fshow_localslocals_max_lengthlocals_max_stringlocals_hide_dunderlocals_hide_sunderrA   r,   type[BaseException]r-   BaseException	tracebackTracebackType | NonerN   rO   r   rP   rQ   rR   c                  s  g }	d}
	 t t| jt|dd t|ddD |
d}tjdkrDt|ttfrDd|_	|j
D ]}|j
tt|||j| d		 q-t|tr`t|jpNd
|jpRd|jpVd
|jpZd|jd|_|	| |jj}d fdd}t|D ]7\}}|jj}|r|dstj|}t|pd||jj |rfdd||j!" D ndd}|| qut|dd}|r|jr|j#} |}|j}d}
q|j$}|r|jrt|dds|j#} |}|j}d}
q	 t%|	dS )a  
    Extract traceback information.

    Args:
        exc_type: Exception type.

        exc_value: Exception value.

        traceback: Python Traceback object.

        show_locals: Enable display of local variables. Defaults to False.

        locals_max_length:
            Maximum length of containers before abbreviating, or ``None`` for
            no abbreviation.

        locals_max_string:
            Maximum length of string before truncating, or ``None`` to disable
            truncating.

        locals_hide_dunder:
            Hide locals prefixed with double underscore.
            Defaults to True.

        locals_hide_sunder:
            Hide locals prefixed with single underscore.
            This implies hiding *locals_hide_dunder*.
            Defaults to False.

        use_rich: If ``True`` (the default), use rich_ to compute the repr.
            If ``False`` or if rich_ is not installed, fall back to a simpler
            algorithm.

    Returns:
        A Trace instance with structured information about all exceptions.

    .. versionadded:: 22.1.0

    .. versionchanged:: 24.3.0
       Added *locals_max_length*, *locals_hide_sunder*, *locals_hide_dunder*
       and *use_rich* arguments.

    .. versionchanged:: 25.4.0
       Handle exception groups.
    FTc                 S  s   g | ]}t |qS r&   )r   ).0noter&   r&   r'   
<listcomp>   s    zextract.<locals>.<listcomp>	__notes__r&   )r,   r-   r/   r2   )      rM   r   ? )r(   r   r   r)   r*   iter_localsIterable[tuple[str, object]]r9   c                 3  sT     ss| E dH  dS | D ]\}} r| drqr"| dr"q||fV  qdS )z-Extract locals from an iterator of key pairs.N___
startswith)r_   keyvalue)rQ   rR   r&   r'   
get_locals  s   
zextract.<locals>.get_locals<c              	     s"   i | ]\}}|t | d qS ))r>   r@   rA   )r   )rW   re   rf   )rO   rP   rA   r&   r'   
<dictcomp>6  s    zextract.<locals>.<dictcomp>N)r   r   r   r    	__cause____suppress_context__)r7   )r_   r`   r9   r`   )&r   r   r!   getattrsysversion_inforG   BaseExceptionGroupExceptionGroupr4   r5   appendr   type__traceback__SyntaxErrorr   r(   r   r   textr*   r0   r3   r   f_codeco_filenamerd   ospathabspathr   co_namef_localsitems	__class____context__r   )r,   r-   rU   rN   rO   rP   rQ   rR   rA   r7   r2   stack	exceptionrq   rg   frame_summaryline_nor   framecauser&   )rQ   rR   rO   rP   rA   r'   r      s   :

	






r   c                	   @  sB   e Zd ZdZeeedddedddddZd ddZ	d!ddZ
dS )"r   a,  
    Return a list of exception stack dictionaries for an exception.

    These dictionaries are based on :class:`Stack` instances generated by
    :func:`extract()` and can be dumped to JSON.

    Args:
        show_locals:
            Whether or not to include the values of a stack frame's local
            variables.

        locals_max_length:
            Maximum length of containers before abbreviating, or ``None`` for
            no abbreviation.

        locals_max_string:
            Maximum length of string before truncating, or ``None`` to disable
            truncating.

        locals_hide_dunder:
            Hide locals prefixed with double underscore.
            Defaults to True.

        locals_hide_sunder:
            Hide locals prefixed with single underscore.
            This implies hiding *locals_hide_dunder*.
            Defaults to False.

        suppress:
            Optional sequence of modules or paths for which to suppress the
            display of locals even if *show_locals* is ``True``.

        max_frames:
            Maximum number of frames in each stack.  Frames are removed from
            the inside out.  The idea is, that the first frames represent your
            code responsible for the exception and last frames the code where
            the exception actually happened.  With larger web frameworks, this
            does not always work, so you should stick with the default.

        use_rich: If ``True`` (the default), use rich_ to compute the repr of
            locals.  If ``False`` or if rich_ is not installed, fall back to
            a simpler algorithm.

    .. seealso::
        :doc:`exceptions` for a broader explanation of *structlog*'s exception
        features.

    .. versionchanged:: 24.3.0
       Added *locals_max_length*, *locals_hide_sunder*, *locals_hide_dunder*,
       *suppress* and *use_rich* arguments.

    .. versionchanged:: 25.1.0
       *locals_max_length* and *locals_max_string* may be None to disable
       truncation.

    .. versionchanged:: 25.4.0
       Handle exception groups.
    TFr&   )rN   rO   rP   rQ   rR   suppress
max_framesrA   rN   r1   rO   r   rP   rQ   rR   r   Iterable[str | ModuleType]r   rA   r9   Nonec                C  s   |d ur|dk rd| }	t |	|d ur"|dk r"d| }	t |	|dk r/d| }	t |	|| _|| _|| _|| _|| _g | _|D ]0}
t|
tsa|
j	d u rYd|
d}	t |	t
j|
j	}n|
}t
jt
j|}| j| qC|| _|| _d S )Nr   z""locals_max_length" must be >= 0: z""locals_max_string" must be >= 0:    z"max_frames" must be >= 2: z"suppress" item z+ must be a module with '__file__' attribute)
ValueErrorrN   rO   rP   rQ   rR   r   rG   r   __file__rx   ry   dirnamenormpathrz   rq   r   rA   )selfrN   rO   rP   rQ   rR   r   r   rA   r*   suppress_entityry   r&   r&   r'   __init__  s6   






z!ExceptionDictTransformer.__init__exc_infor   list[dict[str, Any]]c              	   C  s   t || j| j| j| j| j| jd}|jD ]9}t|j	| j
kr q| j
d }tdddt|j	d|   d}g |j	d | ||j	| d  |j	d d < q| |S )NrM   r   r^   zSkipped frames: )r   r   r   )r   rN   rO   rP   rQ   rR   rA   r7   rI   r3   r   r   _as_dict)r   r   tracer   half
fake_framer&   r&   r'   __call__  s6   


z!ExceptionDictTransformer.__call__r   r   c                   s|   g }|j D ]6}t|}|d D ]  d d u s#t fddjD r& d= q|jr6fdd|jD |d< || q|S )Nr3   r    c                 3  s    | ]
} d   |V  qdS )r   Nrc   )rW   ry   )
frame_dictr&   r'   	<genexpr>  s
    
z4ExceptionDictTransformer._as_dict.<locals>.<genexpr>c                   s   g | ]}  |qS r&   )r   )rW   t)r   r&   r'   rY     s    
z5ExceptionDictTransformer._as_dict.<locals>.<listcomp>r5   )r7   r   anyr   r4   r5   rq   )r   r   stack_dictsr   
stack_dictr&   )r   r   r'   r     s   


z!ExceptionDictTransformer._as_dictN)rN   r1   rO   r   rP   r   rQ   r1   rR   r1   r   r   r   r   rA   r1   r9   r   )r   r   r9   r   )r   r   r9   r   )r!   r"   r#   r$   SHOW_LOCALSLOCALS_MAX_LENGTHLOCALS_MAX_STRING
MAX_FRAMESr   r   r   r&   r&   r&   r'   r   a  s    >
+r   )r8   r	   r9   r   )NNT)
r=   r	   r>   r?   r@   r?   rA   r1   r9   r   )r,   rS   r-   rT   rU   rV   rN   r1   rO   r   rP   r   rQ   r1   rR   r1   rA   r1   r9   r   )'r$   
__future__r   rx   os.pathrm   dataclassesr   r   r   rU   r   typesr   r   typingr	   r
   r   r   r   rC   rich.prettyImportErrorr   __all__r   r   r   r   
OptExcInfor   r   r   r   r   r   r   r   r&   r&   r&   r'   <module>   sX   	

C ,