a
    feza                     @   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mZmZm	Z	 ddlm
Z
 ddlmZmZ G dd deZdd	 Zd
d Zdd Zdd Zdd Zedd ZdS )z/
Unit tests for the stack formatting utilities
    N)	safe_repr_fixed_getframesformat_records)
format_exc)
with_numpynpc                   @   s   e Zd Zdd ZdS )Viciousc                 C   s   t d S N
ValueError)self r   <lib/python3.9/site-packages/joblib/test/test_format_stack.py__repr__   s    zVicious.__repr__N)__name__
__module____qualname__r   r   r   r   r   r      s   r   c                   C   s   t t  d S r	   )r   r   r   r   r   r   test_safe_repr   s    r   c                 C   s2   | \}}}}}}| dr"|d7 }||||||fS )Nz.pyc)endswith)recordZ_1filenameZ_2Z_3Z_4Z_5r   r   r   _change_file_extensions_to_pyc   s    
r   c                 C   s   dd }|| | dS )z7Function that raises with a non trivial call stack
    c                 S   s   t dd S )NzNope, this can not workr
   )abr   r   r   helper)   s    z _raise_exception.<locals>.helperNr   )r   r   r   r   r   r   _raise_exception&   s    r   c                  C   s   zt dd W n ty   t d } t| }dd |D }t|}|D ]}d|v sJJ qJd}t|d |d	 tjs|J t|d
 |d tjsJ d|d v sJ d|d v sJ t|d |d tjsJ Y n0 d S )Nr   *      c                 S   s   g | ]}t |qS r   )r   ).0r   r   r   r   
<listcomp>7   s   z'test_format_records.<locals>.<listcomp>ztest_format_stack.py in^-+>\s+\d+\s+z_raise_exception\('a', 42\)r   zhelper\(a, b\)   za = 'a'zb = 42z-raise ValueError\('Nope, this can not work'\))	r   r   sysexc_infor   r   research	MULTILINE)etbrecordsZpyc_recordsformatted_recordsZfmt_recarrow_regexr   r   r   test_format_records/   s8    

r,   c              	   C   s2  t j| jd}ddg}d|}t|d}|| W d    n1 sL0    Y  td|}t|dsvt	
d z|  W n ty,   t d	 }t|d
d}|d \}}	}
}}}dd |D |ksJ t|}d	}t|d  }||t| ksJ d}t|d |d tjs(J Y n0 d S )Nzsmall_file.pyzdef func():z    1/0
w
small_filefuncz	PyPy bug?r   
   contextc                 S   s   g | ]}|  qS r   )rstrip)r   lr   r   r   r    e       zItest_format_records_file_with_less_lines_than_context.<locals>.<listcomp>r"   r!   z1/0)ospathjoinZstrpathopenwriteimpZload_sourcehasattrpytestskipr0   ZeroDivisionErrorr#   r$   r   r   len
splitlinesr%   r&   r'   )Ztmpdirr   Z
code_linescodefr/   r(   r)   frameZtb_filenamelineZ	func_namer3   _r*   Z
len_headerZnb_lines_formatted_recordsr+   r   r   r   5test_format_records_file_with_less_lines_than_contextQ   s0    
(


rI   c               	   C   sZ   zt jd W nD tyT   t \} }}t| ||dd}d}t||sPJ Y n0 d S )NZinvalid_valuer1   r2   zmtrand[a-z0-9._-]*\.(so|pyd))	r   ZrandomZuniform	Exceptionr#   r$   r   r%   r&   )exc_type	exc_valueexc_tracebackZformatted_excpatternr   r   r   "test_format_exc_with_compiled_codet   s    rO   )__doc__r=   r8   r%   r#   r?   Zjoblib.format_stackr   r   r   r   Zjoblib.test.commonr   r   objectr   r   r   r   r,   rI   rO   r   r   r   r   <module>   s    	"#