U
    /eD                     @   s   d dl Z d dlZd dlmZ dZdZe dZG dd deZ	G dd	 d	e
ZG d
d de
ZG dd dZG dd dZG dd dZdS )    N)CodeTypez2.0.0)	TracebackTracebackParseErrorFrameCodezO^\s*File "(?P<co_filename>.+)", line (?P<tb_lineno>\d+)(, in (?P<co_name>.+))?$c                   @   s   e Zd ZdZdd ZdS )	_AttrDict c                 C   s.   z
| | W S  t k
r(   t|d Y nX d S N)KeyErrorAttributeError)selfnamer   r   2/tmp/pip-unpacked-wheel-764a0gsu/tblib/__init__.py__getattr__   s    
z_AttrDict.__getattr__N)__name__
__module____qualname__	__slots__r   r   r   r   r   r      s   r   c                   @   s   e Zd ZdS )__traceback_makerNr   r   r   r   r   r   r   r      s   r   c                   @   s   e Zd ZdS )r   Nr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdZdd ZdS )r   zw
    Class that replicates just enough of the builtin Code object to enable serialization and traceback rendering.
    Nc                 C   s>   |j | _ |j| _d| _d| _d| _d| _d| _d| _d| _d S )Nr   r   @   )	co_filenameco_nameco_argcountco_kwonlyargcountco_varnames
co_nlocalsco_stacksizeco_flagsco_firstlineno)r   coder   r   r   __init__%   s    zCode.__init__)r   r   r   __doc__co_coder!   r   r   r   r   r      s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   zx
    Class that replicates just enough of the builtin Frame object to enable serialization and traceback rendering.
    c                 C   s4   i | _ dd |j D | _t|j| _|j| _d S )Nc                 S   s   i | ]\}}|d kr||qS )__file__r   r   ).0kvr   r   r   
<dictcomp>8   s       z"Frame.__init__.<locals>.<dictcomp>)f_locals	f_globalsitemsr   f_codef_lineno)r   framer   r   r   r!   6   s    zFrame.__init__c                 C   s   dS )z
        For compatibility with PyPy 3.5;
        clear() was added to frame in Python 3.4
        and is called by traceback.clear_frames(), which
        in turn is called by unittest.TestCase.assertRaises
        Nr   )r   r   r   r   clear<   s    zFrame.clearN)r   r   r   r"   r!   r0   r   r   r   r   r   1   s   r   c                   @   sN   e Zd ZdZdZdd Zdd ZeZdd ZeZ	e
d	d
 Ze
dddZdS )r   z5
    Class that wraps builtin Traceback objects.
    Nc                 C   sj   t |j| _t|j| _|j}| }t| }|d k	rft|}t |j|_t|j|_||_|}|j}q*d S r	   )r   tb_frameint	tb_linenotb_nexttypeobject__new__)r   tbZprev_tracebackcls	tracebackr   r   r   r!   L   s    
zTraceback.__init__c                 C   s  | }d}d}|r|j j}td|jd  d |j jjd}t|dr\|jd|j|jddd	}n<td|j	|j
|j|j|j|j|j|j|j|j|j|jdd}zt|t|j ji  W nB tk
r   t d
 j}|dkr|}|dk	r||_|}~Y nX |j}qz|W S ~~X dS )zm
        Convert to a builtin Traceback object that is usable for raising or rendering a stacktrace.
        N
   zraise __traceback_makerexecreplacer   r   )r   r   r   co_freevarsco_cellvars   )r1   r-   compiler3   r   hasattrr>   r   r   r   r   r   r   r#   	co_constsco_namesr   r   	co_lnotabr=   dictr+   	Exceptionsysexc_infor4   )r   currentZtop_tbr8   r-   r    Znext_tbr   r   r   as_traceback]   sP     
zTraceback.as_tracebackc                 C   sR   | j dkrd}n
| j  }| jjj| jjjd}| jj|| jjd}|| j|dS )z
        Converts to a dictionary representation. You can serialize the result to JSON as it only has
        builtin objects like dicts, lists, ints or strings.
        Nr   r   r+   r-   r.   r1   r3   r4   )	r4   to_dictr1   r-   r   r   r+   r.   r3   )r   r4   r    r/   r   r   r   as_dict   s    

zTraceback.as_dictc                 C   sx   |d r|  |d }nd}t|d d d |d d d d}t|d d ||d d	 d
}t||d |d}| |S )zj
        Creates an instance from a dictionary with the same structure as ``.as_dict()`` returns.
        r4   Nr1   r-   r   r   rM   r+   r.   rN   r3   rO   )	from_dictr   )r9   dctr4   r    r/   r8   r   r   r   rR      s$    

zTraceback.from_dictTc           	   
   C   s   g }|}|  D ]P}| }|r.|dkrd}qt|}|rL||  q|drXq|r qbq|rd}t|D ]6}t|t|t|d ddt|t	|d d	|d
}qr| |S t
d| dS )z
        Creates an instance by parsing a stacktrace. Strict means that parsing stops when lines are not indented by at least two spaces
        anymore.
        z"Traceback (most recent call last):Fz  Nr   ?r$   r3   rN   )r1   r4   z Could not find any frames in %r.)
splitlinesrstripFRAME_REmatchappend	groupdict
startswithreversedr   r2   r   )	r9   stringstrictframesheaderlineZframe_matchpreviousr/   r   r   r   from_string   sB    


	zTraceback.from_string)T)r   r   r   r"   r4   r!   rL   Zto_tracebackrQ   rP   classmethodrR   rc   r   r   r   r   r   E   s   3
r   )rerI   typesr   __version____all__rB   rW   rG   r   rH   r   r   r   r   r   r   r   r   r   <module>   s   
