
    `in%                    ~   d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
 d"dZ G d d          Z e            Zd#dZd$dZee
_        ee
_        	 	 d%d&dZ ede            ede            ede            ede            ede            ede            ed e            ed!e           dS )'zError handling.    )annotationsN)Any   )core	backtracestrreturnlist[tuple[str, int, str]]c                |   d}g }|                      d          D ]}t          j        ||                                          }|rv	 |                    d          }t          |                    d                    }|                    d          }|                    |||f           # t          $ r Y w xY w|S )zParse the backtrace string into a list of (filename, lineno, func).

    Parameters
    ----------
    backtrace
        The backtrace string.

    Returns
    -------
    result
        The list of (filename, lineno, func)

    z!File "(.+?)", line (\d+), in (.+)
r         )splitrematchstripgroupintappend
ValueError)r   patternresultliner   filenamelinenofuncs           a/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/tvm_ffi/error.py_parse_backtracer      s     3GF%% 	 	$**,,// 	 ;;q>>U[[^^,,{{1~~x67777   	 Ms   A$B++
B87B8c                  2    e Zd ZdZddZddZddZddZdS )TracebackManagerz&Helper to manage traceback generation.r	   Nonec                    i | _         dS )z/Initialize the traceback manager and its cache.N)_code_cache)selfs    r   __init__zTracebackManager.__init__?   s    GI    r   r   r   r   r   types.CodeTypec                X   |||f}|| j         v r| j         |         S t          j        d|d          }t          j        |          D ]0}t	          |d          rd|_        t	          |d          rd|_        1t          ||d          }|                    ||          }|| j         |<   |S )Nz_getframe()eval)r   mode
col_offsetr   end_col_offset)co_nameco_firstlineno)	r#   astparsewalkhasattrr+   r,   compilereplace)r$   r   r   r   keytreenodecode_objects           r   _get_cached_code_objectz(TracebackManager._get_cached_code_objectC   s     &$"""#C(( yGGGHTNN 	( 	(Dt\** $"#t-.. (&'#dHf55!))$v)NN +r&   types.FrameTypec                n    |                      |||          }dt          j        i}t          |||          S )z:Create a frame object from the filename, lineno, and func.	_getframe)r9   sysr<   r)   )r$   r   r   r   r8   contexts         r   _create_framezTracebackManager._create_frameY   s9    228VTJJ .K'222r&   tbtypes.TracebackType | Nonetypes.TracebackTypec                N    d
d	} |||                      |||          |          S )a  Append a traceback to the given traceback.

        Parameters
        ----------
        tb
            The traceback to append to.
        filename
            The filename of the traceback
        lineno
            The line number of the traceback
        func
            The function name of the traceback

        Returns
        -------
        new_tb
            The new traceback with the appended frame.

        r@   rA   framer:   r   r   r	   rB   c                :    t          j        | ||j        |          S )N)typesTracebackTypef_lasti)r@   rD   r   s      r   createz1TracebackManager.append_traceback.<locals>.create   s     &r5%-HHHr&   )r@   rA   rD   r:   r   r   r	   rB   )r?   )r$   r@   r   r   r   rI   s         r   append_tracebackz!TracebackManager.append_tracebackb   sC    @	I 	I 	I 	I
 vb$,,XvtDDfMMMr&   N)r	   r!   )r   r   r   r   r   r   r	   r'   )r   r   r   r   r   r   r	   r:   )
r@   rA   r   r   r   r   r   r   r	   rB   )__name__
__module____qualname____doc__r%   r9   r?   rJ    r&   r   r    r    <   st        00J J J J   ,3 3 3 3%N %N %N %N %N %Nr&   r    py_errorBaseExceptionc                    | j         }	 t          |          D ]#\  }}}t                              ||||          }$|                     |          ~ ~S # ~ ~w xY w)z3Append the backtrace to the py_error and return it.)__traceback__r   _TRACEBACK_MANAGERrJ   with_traceback)rP   r   r@   r   r   r   s         r   _with_append_backtracerV      sw    > 
	B
&6y&A&A 	Q 	Q"Hfd#44R64PPBB&&r** bbHbs   AA Ar@   rA   c           	         g }| N| j         }| j        }|j        j        }|j        j        }|                    d| d| d| d           | j        } | Nd                    t          |                    S )z"Convert the traceback to a string.Nz  File "z", line z, in r    )	tb_frame	tb_linenof_codeco_filenamer-   r   tb_nextjoinreversed)r@   linesrD   r   r   funcnames         r   _traceback_to_backtrace_strrb      s    E
.<+<'KKK&KKxKKKLLLZ . 778E??###r&   name_or_clsstr | type | Noneclstype | Noner   c                ^     t                     r	 }|j         d fd}||S  ||          S )a  Register an error class so it can be recognized by the ffi error handler.

    Parameters
    ----------
    name_or_cls
        The name of the error class.

    cls
        The class to register.

    Returns
    -------
    fregister
        Register function if f is not specified.

    Examples
    --------
    .. code-block:: python

        import tvm_ffi


        # Register a custom Python exception so tvm_ffi.Error maps to it
        @tvm_ffi.error.register_error
        class MyError(RuntimeError):
            pass


        # Convert a Python exception to an FFI Error and back
        ffi_err = tvm_ffi.convert(MyError("boom"))
        py_err = ffi_err.py_error()
        assert isinstance(py_err, MyError)

    myclstyper	   c                    t          t                    rn| j        }| t          j        |<   |t          j        | <   | S )z0Register the error class name with the FFI core.)
isinstancer   rK   r   ERROR_NAME_TO_TYPEERROR_TYPE_TO_NAME)rh   err_namerc   s     r   registerz register_error.<locals>.register   s<    ",[#">">R;;EN,1))1&r&   N)rh   ri   r	   ri   )callablerK   )rc   re   ro   s   `  r   register_errorrq      sZ    L  #l      {8C==r&   RuntimeErrorr   	TypeErrorAttributeErrorKeyError
IndexErrorAssertionErrorMemoryError)r   r   r	   r
   )rP   rQ   r   r   r	   rQ   )r@   rA   r	   r   )NN)rc   rd   re   rf   r	   r   )rN   
__future__r   r/   r   r=   rF   typingr   rX   r   r   r    rT   rV   rb   _WITH_APPEND_BACKTRACE_TRACEBACK_TO_BACKTRACE_STRrq   rr   r   rs   rt   ru   rv   rw   rx   rO   r&   r   <module>r}      s  $   " " " " " " 



 				 



                :KN KN KN KN KN KN KN KN\ &%'' * * * *Z$ $ $ $ 5 #>   &*3 3 3 3 3l ~| , , , |Z ( ( ( {I & & &  0 0 0 z8 $ $ $ |Z ( ( (  0 0 0 }k * * * * *r&   