U
    .d8                     @   s   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	dZ
dd ZefddZdd	 Zd
d ZdddZdddZdd ZdddZdddZdS )a"  
Represent an exception with a lot of information.

Provides 2 useful functions:

format_exc: format an exception into a complete traceback, with full
            debugging instruction.

format_outer_frames: format the current position in the stack call.

Adapted from IPython's VerboseTB.
    Nz        c                 C   s   zt j| W S  tk
r$    Y n   zt| W  Y S  tk
rN    Y n   zLt| dd}|rxt|W  Y  Y S t| dd}|rdt| W  Y  Y S W n& tk
r    Y n   Y Y Y dS X Y nX Y nX dS )z(Hopefully pretty robust repr equivalent.__name__N	__class__z%s instancezUNRECOVERABLE REPR FAILURE)pydoctextreprKeyboardInterruptgetattr	safe_repr)valuenameklass r   C/tmp/pip-unpacked-wheel-ho8k9mhl/joblib/_deprecated_format_stack.pyr	   &   s*    r	   c                 C   s   d||  S )Nz=%sr   )r
   r   r   r   r   eq_reprC   s    r   c                 C   s4   g }t  }| D ] }||kr|| || q|S )a  uniq_stable(elems) -> list

    Return from an iterable, a list of all the unique elements in the input,
    but maintaining the order in which they first appear.

    A naive solution to this problem which just makes a dictionary with the
    elements as keys fails to respect the stability condition, since
    dictionaries are unsorted by nature.

    Note: All elements in the input must be hashable.
    )setappendadd)elemsuniqueZ
unique_setnnr   r   r   uniq_stableH   s    
r   c           	   	   C   sP   g }| D ]B\}}}}}}|j dd}t|tr4|}|||||||f q|S )zTry to fix the filenames in each record from inspect.getinnerframes().

    Particularly, modules loaded from within zip files have useless filenames
    attached to their code object, and inspect.getinnerframes() just uses it.
    __file__N)	f_globalsget
isinstancestrr   )	recordsZfixed_recordsframefilenameline_no	func_namelinesindex	better_fnr   r   r   fix_frame_records_filenames^   s    
r$      c                 C   s  d\}}}t t| |}||d  }z(|d d }|dksF|drL|W S W n tk
rb   Y nX t| }	t|t|	kstt	|	D ]|\}
\}}}}|d |d  }t
|d}|| }t||| }t||
 }|||< |d | ||< |||< t|||
< q||d  S )N         r   r%   z<ipython console>z<string>r'   )r$   inspectgetinnerframesendswith
IndexError	traceback
extract_tblenAssertionError	enumeratemax	linecachegetlineslisttuple)etbcontext	tb_offsetLNUM_POS	LINES_POS	INDEX_POSr   Z	rec_checkrnameZauxifilelnum_maybe_startstartendr!   bufr   r   r   _fixed_getframess   s,    



rG   c                 C   s   d}g }| | }|D ]}|| krx|t t| }|dkrJd|d  d }	n |dkrXd}	n|dkrfd}	nd	}	|	t| }
nd
||f }
d|
|f }|| |r|| kr||d  |d }q|S )N      -z-> r'   z> r%   > z%*sz%s %s
)r0   r   r   )rA   r"   r!   lvalsZnumbers_widthresr?   linepadmarkernumr   r   r   _format_traceback_lines   s*    

rT   c                    sb  g }t jj}| D ]J\}}}}}}z|r0||p2d}W n tk
rJ   Y nX |drf|d d d }|}	t|\}
}}}|dkrd}nFz d|tj|
|||tdf }W n$ t	k
r   t
d d	| }Y nX g   fd
dd_||gtjfdd}zt|D ]}|  qW nT tttfk
r6   Y n: tjk
rn } zd||f }t
| W 5 d }~X Y nX t }g }|D ]r}|ddd }||jjkr|| krztt||}W n   d}Y nX nd}|}|d||f  q|rdtdt |f }nd}dd|	|f }|d kr:|| q|d|dt||||f  q|S )N?.pyc.pyrL   zin %s%s)formatvaluez+
Joblib's exception reporting continues...
z'in %s(***failed resolving arguments***)c                    s   |dkr:z d  d7  < d_ W dS  tk
r8   Y nX | tjkrx|tjkrxj rl d  |7  < d_ q | n| tjkrtdS )a  Stateful tokeneater which builds dotted names.

            The list of names it appends to (from the enclosing scope) can
            contain repeated composite names.  This is unavoidable, since
            there is no way to disambiguate partial dotted structures until
            the full list is known.  The caller is responsible for pruning
            the final list of duplicates before using it..TNF)	name_contr-   tokenizeNAMEkeywordkwlistr   NEWLINE)
token_typetokenrD   rE   rP   names
tokeneaterr   r   rf      s    

z"format_records.<locals>.tokeneaterFc                 S   s"   || |d }|d  d7  < |S )Nr   r%   r   )r@   rA   getlinerP   r   r   r   
linereader   s    z"format_records.<locals>.linereaderzAn unexpected error occurred while tokenizing input file %s
The following traceback may be corrupted or invalid
The error message is: %s
rZ   r%   r   Z	undefinedz%s = %sz%s%sz
%sz	%s
%s %s
zK...........................................................................)ospathabspathOSErrorr,   r*   getargvaluesformatargvaluesr   KeyErrorprintr\   r4   rg   r]   generate_tokensr-   UnicodeDecodeErrorSyntaxError
TokenErrorr   splitf_codeco_varnameskeysr	   evalr   INDENTjoinrT   )r   framesrk   r   r@   rA   funcr!   r"   linkargsvarargsvarkwlocalscallrh   rc   msg_mZunique_namesrN   Z	name_fullZ	name_baser
   r   levelr   rd   r   format_records   sx    

  #
r   r)   c              
   C   s.  z
| j } W n tk
r   Y nX dtj d  d tj }tt }dt	  }d| ddt
t|  t
|  ||ddt
t| t
|  |f }t|||}	ztt| |f\}
}W n< tk
r   tt dd	  } }tt| |f\}
}Y nX d
|
|f g}t|	}d|d|d|d f S )a   Return a nice text document describing the traceback.

        Parameters
        -----------
        etype, evalue, etb: as returned by sys.exc_info
        context: number of lines of the source file to plot
        tb_offset: the number of stack frame not to use (0 = use all)

    zPython r   z: zPID: %iz%s%s%s
%s%s%s K   Nr'   z%s: %sz%s
%s
%srM   rL   )r   AttributeErrorsysversionru   
executabletimectimeri   getpidr0   r   rG   mapBaseExceptionexc_infor   r{   )etypeZevaluer8   r9   r:   pyverdatepidheadr   Z	etype_strZ
evalue_str	exceptionr|   r   r   r   
format_excB  s0    
   r   Tc                 C   s  d\}}}t t  }t }t|D ]\}	\}
}}}}}|
jdd }t|trp|}|	drp|d d d }|rt
j|dkr|dkr q|d | d	  }t|d
}||  }t||| }t||	 }|||< |d | ||< |||< |t| q&dt|||d S )Nr&   r   rV   rW   rX   )ziplib.pyzpy3compat.py)ZexecfileZsafe_execfileZruncoder%   r'   r   rM   r[   )r*   getouterframescurrentframer6   r2   r   r   r   r   r,   ri   rj   basenamer3   r4   r5   r   r7   r{   r   )r9   Zstack_startZ	stack_endZignore_ipythonr;   r<   r=   r   outputr?   r   r   r   r    r!   r"   r#   rC   rD   rE   rF   r   r   r   format_outer_framesm  s2    



r   )r%   r   )N)r)   r   )r)   NNT)__doc__r*   r_   r4   ri   r   r   r   r]   r.   rz   r	   r   r   r$   rG   rT   r   r   r   r   r   r   r   <module>   s,   

 
+  