o
    Y۷i%                     @  s   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dZG dd dZe Zd'ddZd(ddZee
_ee
_		d)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              	   C  sz   d}g }|  dD ]1}t|| }|r:z|d}t|d}|d}||||f W q	 ty9   Y q	w q	|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func r   C/home/ubuntu/vllm_env/lib/python3.10/site-packages/tvm_ffi/error.py_parse_backtrace   s   

r   c                   @  s8   e Zd ZdZdddZdddZdddZdddZdS )TracebackManagerz&Helper to manage traceback generation.r   Nonec                 C  s
   i | _ dS )z/Initialize the traceback manager and its cache.N)_code_cache)selfr   r   r   __init__?   s   
zTracebackManager.__init__r   r   r   r   r   types.CodeTypec                 C  s   |||f}|| j v r| j | S tjd|dd}t|D ]}t|dr&d|_t|dr.d|_qt||d}|j||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_objectr   r   r   _get_cached_code_objectC   s   





z(TracebackManager._get_cached_code_objecttypes.FrameTypec                 C  s$   |  |||}dtji}t|||S )z:Create a frame object from the filename, lineno, and func.	_getframe)r4   sysr6   r$   )r!   r   r   r   r3   contextr   r   r   _create_frameY   s   
zTracebackManager._create_frametbtypes.TracebackType | Nonetypes.TracebackTypec                 C  s    d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:   r;   framer5   r   r   r   r<   c                 S  s   t | ||j|S )N)typesTracebackTypef_lasti)r:   r=   r   r   r   r   create   s   z1TracebackManager.append_traceback.<locals>.createN)r:   r;   r=   r5   r   r   r   r<   )r9   )r!   r:   r   r   r   rA   r   r   r   append_tracebackb   s   
 z!TracebackManager.append_tracebackN)r   r   )r   r   r   r   r   r   r   r#   )r   r   r   r   r   r   r   r5   )
r:   r;   r   r   r   r   r   r   r   r<   )__name__
__module____qualname____doc__r"   r4   r9   rB   r   r   r   r   r   <   s    


	r   py_errorBaseExceptionc                 C  sB   | j }zt|D ]\}}}t||||}q| |W ~ ~S ~ ~w )z3Append the backtrace to the py_error and return it.)__traceback__r   _TRACEBACK_MANAGERrB   with_traceback)rG   r   r:   r   r   r   r   r   r   _with_append_backtrace   s   
rL   r:   r;   c              	   C  sb   g }| dur*| j }| j}|jj}|jj}|d| d| d| d | j} | dusd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:   linesr=   r   r   funcnamer   r   r   _traceback_to_backtrace_str   s   	rW   name_or_clsstr | type | Noneclstype | Noner   c                   s6   t  tr
 }|j d fdd}|du r|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                   s,   t  tr n| j}| tj|< |tj| < | S )z0Register the error class name with the FFI core.)
isinstancer   rC   r   ERROR_NAME_TO_TYPEERROR_TYPE_TO_NAME)r\   err_namerX   r   r   register   s   

z register_error.<locals>.registerN)r\   r]   r   r]   )r^   r]   rC   )rX   rZ   rc   r   rb   r   register_error   s   
&rd   RuntimeErrorr   	TypeErrorAttributeErrorKeyError
IndexErrorAssertionErrorMemoryError)r   r   r   r	   )rG   rH   r   r   r   rH   )r:   r;   r   r   )NN)rX   rY   rZ   r[   r   r   )rF   
__future__r   r*   r   r7   r>   typingr   rM   r   r   r   rJ   rL   rW   _WITH_APPEND_BACKTRACE_TRACEBACK_TO_BACKTRACE_STRrd   re   r   rf   rg   rh   ri   rj   rk   r   r   r   r   <module>   s4   
N

-
6





