U
    .d                     @   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   A/tmp/pip-unpacked-wheel-ho8k9mhl/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k
r   t d } t| }dd |D }t|}|D ]}d|ksLtqLd}t|d |d	 tj	s~tt|d
 |d tj	std|d kstd|d kstt|d |d tj	stY nX 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   AssertionErrorresearch	MULTILINE)etbrecordsZpyc_recordsformatted_recordsZfmt_recarrow_regexr   r   r   test_format_records/   s8    

r-   c              	   C   s  t j| jd}ddg}d|}t|d}|| W 5 Q R X td|}t|dsbt	
d z|  W n tk
r   t d	 }t|d
d}|d \}}	}
}}}dd |D |kstt|}d	}t|d  }||t| kstd}t|d |d tjstY nX 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   s     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skipr1   ZeroDivisionErrorr#   r$   r   r%   r   len
splitlinesr&   r'   r(   )Ztmpdirr   Z
code_linescodefr0   r)   r*   frameZtb_filenameline	func_namer4   _r+   Z
len_headerZnb_lines_formatted_recordsr,   r   r   r   5test_format_records_file_with_less_lines_than_contextQ   s0    



rJ   c               	   C   s\   zt jd W nF tk
rV   t \} }}t| ||dd}d}t||sRt	Y nX d S )NZinvalid_valuer2   r3   zmtrand[a-z0-9._-]*\.(so|pyd))
r   randomuniform	Exceptionr#   r$   r   r&   r'   r%   )exc_type	exc_valueexc_tracebackZformatted_excpatternr   r   r   "test_format_exc_with_compiled_codet   s     rR   )__doc__r=   r8   r&   r#   r?   Zjoblib.format_stackr   r   r   r   Zjoblib.test.commonr   r   objectr   r   r   r   r-   rJ   rR   r   r   r   r   <module>   s    	"#