Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib/python3.5/site-packages/joblib/test/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/joblib/test/__pycache__/test_memory.cpython-35.pyc



��X�T�@s�dZddlZddlZddlZddlmZddlZddlZddlZddl	Z	ddl
Z
ddlZddlm
Z
mZmZmZddlmZmZddlmZmZddlmZddlmZd	d
d�Ze�Zdd
�Zdd�Zdd�Zdd�Z dd�Z!dd�Z"dd�Z#dd�Z$dd�Z%dd�Z&d d!�Z'd"d#�Z(d$d%�Z)gd&d'�Z*d(d)�Z+ed*d+��Z,ed,d-��Z-d.d/�Z.d0d1�Z/d2d3�Z0d4d5�Z1d6d7�Z2d8d9�Z3d:d;�Z4d<d=�Z5d>d?�Z6d@dA�Z7dBdC�Z8dDdE�Z9dFdG�Z:dHdI�Z;er�e<dJ�dKdL�Z=dMdN�Z>dS)Oz
Test the memory module.
�N)�mkdtemp)�Memory�
MemorizedFunc�NotMemorizedFunc�MemorizedResult)�NotMemorizedResult�_FUNCTION_HASHES)�
with_numpy�np)�assert_raises_regex)�PY3_OR_LATER�cCs|d|S)z3 A module-level function for testing purposes.
    ��)�x�yrr�H/opt/alt/python35/lib/python3.5/site-packages/joblib/test/test_memory.py�fsrcCsdt�}|td<tjj|�r2tj|�tdd�tdtd�tdd�dS)z Test setup.
    �dir�P�_ztest_memory setup (%s)N)r�env�os�path�exists�shutil�rmtree�print)�cachedirrrr�setup_module)s	

rcCsNtdd�td|�td|�td|d�tdd�dS)N�!�Ozos function failed: %rzfile to be removed: %szexception was: %rr
)r)�funcrZexcinforrr�_rmtree_onerror6s
r#cCsItjtddt�tdd�tdtd�tdd�dS)z Test teardown.
    rFrrztest_memory teardown (%s)N)rrrr#rrrrr�teardown_module>sr$ccs�tdtddd�}|jdd�|j|�}xctd�D]U}xLtd�D]>}tjj||�|fVtjjt|�|d	fVqXWqEWd
S)z� Given a function and an accumulator (a list that grows every
        time the function is called), check that the function can be
        decorated by memory to be a lazy identity.
    rr�verboser�warnF�rr
N)	rr�clear�cache�range�nose�tools�assert_equal�len)r"�accumulator�memory�irrrr�check_identity_lazyIsr2c
#s�t���fdd�}xt|��D]}|Vq+WxidD]a}xXdD]P}z�tj}tj}tr�tj�t_tj�t_ntj�t_tj�t_t	dt
dd	d
d|d|�}tjt
dd
d�|j
|�}|d�|jdd�t��}|d�}	Wd|t_|t_Xtjjt��|dfVtjj|j|d�|	fVtjjt��|dfVqNWqAWd|_t	dt
dd	d�}|j
|�d�dS)z, Simple test of memory lazy evaluation.
    cs�jd�|S)Nr
)�append)�l)r/rrrcs
z"test_memory_integration.<locals>.fFT�rNrrr%�
�	mmap_mode�compress�
ignore_errorsr
r&�__main__r)FT)r5N)�listr2�sys�stdoutr�io�StringIO�stderr�BytesIOrrrrr)r(r.r+r,r-�eval�
__module__)
r�testr8r7�orig_stdoutZorig_stderrr0�g�current_accumulator�outr)r/r�test_memory_integration[s@		

		
	
 	rIc#s�t���fdd�}tdddd�}|j|�}xHtd�D]:}t��}|d�tjjt��|dfVqLWdS)	z, Test memory with cachedir=None: no memoize cs�jd�|S)Nr
)r3)r4)r/rr�ff�s
ztest_no_memory.<locals>.ffrNr%r�r
)r;rr)r*r.r+r,r-)rJ�memZggrrGr)r/r�test_no_memory�s	
rMc#s�t��dd�fdd�}xt|��D]}|Vq1Wtdtddd�}|j|�}tjj|d	d
dd�d
�dS)
z4 Test memory with a function with keyword arguments.Nr
cs�jd�|S)Nr
)r3)r4�m)r/rrrF�s
ztest_memory_kwarg.<locals>.grrr%rr4�rNr)r;r2rrr)r+r,r-)rFrDr0r)r/r�test_memory_kwarg�s		rPc#sPt���fdd���fdd�}xt|��D]}|Vq=WdS)z+ Test memory with a function with a lambda.cs�jd�|S)z4 A helper function to define l as a lambda.
        r
)r3)r)r/rr�helper�s
z"test_memory_lambda.<locals>.helpercs
�|�S)Nr)r)rQrr�<lambda>�sz$test_memory_lambda.<locals>.<lambda>N)r;r2)r4rDr)r/rQr�test_memory_lambda�s
	rSccs�tdtddd�}|jdd��}|}|jdd��}|}ttd�sbd	Stjd
d��v}tjd�tjd
t�|d�|d�tj	j
t|�dfVtj	jdt
|dj�kfVWd	QRXd	S)z> Check that name collisions with functions will raise warningsrrr%rcSs|S)z0 A first function called name_collision
        r)rrrr�name_collision�sz2test_memory_name_collision.<locals>.name_collisioncSs|S)z1 A second function called name_collision
        r)rrrrrT�s�catch_warningsN�recordT�always�ignorer
Z	collision���)rrr)�hasattr�warningsrU�simplefilter�DeprecationWarningr+r,r-r.�assert_true�str�message)r0rT�a�b�wrrr�test_memory_name_collision�s


rdcCs�tdtddd�}|j�dd�}|j|�}dd�}|j|�}tjdd	��o}tjd
�tjdt�tj	j
d|d��tj	j
d|d
��tj	j
d
|d
��WdQRXtj	j
t|�d�dS)Nrrr%rcSs|S)Nr)rrrrrR�sz7test_memory_warning_lambda_collisions.<locals>.<lambda>cSs|dS)Nr
r)rrrrrR�srVTrWrXrr
rK)rrr(r)r[rUr\r]r+r,r-r.)r0rarbrcrrr�%test_memory_warning_lambda_collisions�s

 reccstdtddd�}|j�td�}|j|�}td�}|j|�}ttd�sldStjdd	���}tjd
�tjdt	�|d�|d�|d�t
jjt
|�d
fVt
jjdt|dj�j�kfVWdQRXdS)Nrrr%rzlambda x: xz
lambda x: x+1rUrVTrWrXr
rz
cannot detectrY)rrr(rBr)rZr[rUr\r]r+r,r-r.r^r_r`�lower)r0�a1�b1rcrrr�'test_memory_warning_collision_detections"




	ric#s\t���fdd�}ddl}|j|d�}xt|��D]}|VqIWdS)z$ Test memory with functools.partial.cs�jd�|S)z4 A helper function to define l as a lambda.
        r
)r3)rr)r/rrr".s
z!test_memory_partial.<locals>.funcrNr
)r;�	functools�partialr2)r"rj�functionrDr)r/r�test_memory_partial*s	rmccsRtdtddd�}td�}|j|�}tjjd|d�fVdS)zF Smoke test memory with a function with a function defined in an eval.rrr%rzlambda x: xr
N)rrrBr)r+r,r-)r0rN�mmrrr�test_memory_eval;srocCst|�}|jd�|S)zx A function with a side effect in its arguments.

        Return the lenght of its argument and append one element.
    N)r.r3)rZlen_xrrr�count_and_appendEs
rpcCsVtdtddd�}|jt�}|�dks=t�|�dksRt�dS)zx Check that if a function has a side effect in its arguments, it
        should use the hash of changing arguments.
    rrr%rr
N)rrr)rp�AssertionError)rLr"rrr�test_argument_changeOsrrc#sx�dD]�}t��d�fdd�}tdtdd|dd	�}|jd
d�|j|�}tjjd	�}x~td�D]p}|j	d�}xXtd�D]J}t
jjtj
||�|k�fVt
jjt��|dfVq�Wq�WqWdS)z/ Test memory with a function with numpy arrays.Nr5cs�jd�|S)Nr
)r3)r4)r/rr�nds
ztest_memory_numpy.<locals>.nrrr7r%rr&Fr'r6r
)Nr5)r6r6)r;rrr(r)r
�random�RandomStater*�
random_sampler+r,r^�allr-r.)r7rsr0Zcached_nZrndr1rarr)r/r�test_memory_numpy]s
		&rxcCs�tdtddddd�}|jdd�|j�d	d
��}tjd�}||�}||�}tjjt	|tj
��tjj|jd�tjjt	|tj
��tjj|jd�dS)
z8Check that mmap_mode is respected even at the first callrrr7r5r%rr&FcSs|dS)Nrr)rarrr�twice|sz0test_memory_numpy_check_mmap_mode.<locals>.twicer'N)
rrr(r)r
�onesr+r,r^�
isinstance�memmapr-�mode)r0ryrarb�crrr�!test_memory_numpy_check_mmap_modeusrc#s�tdtddd�}Gdd�dt��|jd�fdd��}|�x+td	�D]}tjj�|d
fVqaWdS)z1 Smoketest the exception handling of Memory.
    rrr%rc@seZdZdS)z*test_memory_exception.<locals>.MyExceptionN)�__name__rC�__qualname__rrrr�MyException�sr�cs|r��dS)Nr)�exc)r�rr�h�sz test_memory_exception.<locals>.hr'r
N)rr�	Exceptionr)r*r+r,�
assert_raises)r0r�rr)r�r�test_memory_exception�sr�c#s�tdtddd�}t��|jddg�d�fdd	��}tjj|jdgfV|ddd�tjjt��dfV|ddd�tjjt��dfV|ddd
�tjjt��dfVdS)z# Test the ignore feature of memory rrr%rrXrr
cs�jd�dS)Nr
)r3)rr)r/rr�z�sztest_memory_ignore.<locals>.zrN)	rrr;r)r+r,r-rXr.)r0r�r)r/r�test_memory_ignore�s	r�ccs�tdtddd�}dgddfgdd	fg}x�|D]~\}}}|jd
|d|d|�dd
��}tjj|j|fVtjj|j|fVtjj|j|fVqAWd	S)z7Check cache may be called with kwargs before decoratingrrr%rr�dr5r6NrXr7cSsdS)Nr)rrrrr��sz"test_partial_decoration.<locals>.z)	rrr)r+r,r-rX�_verboser7)r0Ztest_valuesrXr%r7r�rrr�test_partial_decoration�s*r�ccs}tdtddd�}|j�tjd�}|jd�tjjtdd|�}|j	t
�}tjj
|j�|fVtjjtjj|�fVtj�tjj|j�fVtjjtjjtjj|d��fVtjj|j�fV|jd	�\}}|d	�}tjjtjj|�fVtjtjj|d
��tjj
||d	�fVdS)Nrrr%r�.r�joblibzfunc_code.pyr
z
output.pkl)rrr(r��splitr3rr�joinr)rr+r,r-�
_get_func_dirr^rrZassert_false�_check_previous_func_code�get_output_dir�remove)r0rrFrrrarrr�
test_func_dir�s(


		#	r�ccstdtddd�}|jt�}|d�}tjtj|��}|jd�\}}tj	j
||j|�fVtjtj|��}tj	j
|j|jfVtdddd�}tjtj|��|jt�}tjtj|��}|d�dS)Nrrr%rr
)
rrr)r�pickle�loads�dumpsr�r+r,r-�load_outputr)r0rF�outputr��
output_dirrZmemory2Zgprrr�test_persistence�sr�cCs�x�ttttd�tt�tdtd�jt�tdd�jt�fttttf�D]�\}}t	j
j|d�d�|jd�}t	j
j
t||��t	j
j|j�d�|j�t	j
jt|j�|j�qcWdS)z8Test MemorizedFunc outputting a reference to cache.
    rrNr�)�ziprrrrrr)rrr+r,r-�call_and_shelver^r{�getr(r��KeyError)r"ZResult�resultrrr�test_call_and_shelve�s	
r�cCs�x�tttd�tt�fD]�}tjjtdd�}|jd�}t|d��}t	j
||�WdQRXt|d��}t	j|�}WdQRXtj
j|j�|j��tj|�q WdS)Nrzpickling_test.datr�wb�rb)rrrrrrr�r��openr��dump�loadr+r,r-r�r�)r"�filenamer��fp�result2rrr�test_memorized_picklings&r�cCs�tttd�}|jd�}tttd�}|jd�}tjj|j�|j��tjjt|�t|��|j	d�|j
d�tt�}t|�t|jd��tttddddtj��}|jd�}|j�tttddd�}|jd�}|j�tttddddtj��}|jd�}|j�tttddd�}|jd�}|j�dS)Nrrr%��	timestampr�)
rrrr�r+r,r-r��repr�format_signature�format_callr�time)r"r�Zfunc2r�rrr�test_memorized_reprs.


%

%
r�c	Cs&tjjtdd�}tjj|�s8tj|�tjj|d�}d}t|d��}|j|�WdQRXtjj	|�ddl
}tdtddd�}|j|j
�}tj}tr�tj�}ntj�}z|t_|d	�|d
�|d	�t|d��}|jd|�WdQRX|d	�|d	�tj|�tj|�d}t|d��}|j|�WdQRX|d	�|d	�|jd
�tjjd�ddl
}|j|j
�}|d	�|d	�Wd|t_Xtjj|j�d�dS)NrZ
tmp_importztmp_joblib_.pyz$def f(x):
    print(x)
    return x
rcrrr%r
rz

z-def f(x):
    print("x=%s" % x)
    return x
z
Reloading
�tmp_joblib_z1
2
Reloading
x=1
)rrr�rr�mkdirr��writer<r3r�rr)rr=rr>r?rArr�modules�popr+r,r-�getvalue)	�dir_namer��content�module_file�tmprLrrEZ	my_stdoutrrr�test_memory_file_modification@sN
		











r�cCsdS)Nr)rarbrrr�_function_to_cache�sr�cCs||S)Nr)rarbrrr�_sum�sr�cCs||S)Nr)rarbrrr�_product�sr�cCs�tjt_tdtddd�}|jt�}tjj|dd�d�tjj|dd�d�t	j
dd	��Vt	jd
�tjt_tjj|dd�d�tjj|dd�d�WdQRXdS)Nrrr%rr
rr'rVTrW)
r��__code__r�rrr)r+r,r-r[rUr\r�)rLrrrr�*test_memory_in_memory_function_code_change�s
r�cCstdd�}|j�dS)Nr)rr()rLrrr�$test_clear_memory_with_none_cachedir�sr�z�
def func_with_kwonly_args(a, b, *, kw1='kw1', kw2='kw2'):
    return a, b, kw1, kw2

def func_with_signature(a: int, b: float) -> float:
    return a + b
c	Cstdtddd�}|jt�}tjj|dddd�d�ttd
|dddd	di�|ddddd	d�ttd
|dddd	di�|jtdd	g�}tjj|ddddd	d�d�tjj|ddddd	d
�d�dS)Nrrr%rr
rZkw1r'�kw2z?Keyword-only parameter 'kw1' was passed as positional parameterrKrX�ignored)r
rr'r�)r
rr'rK)r
rr'rK)	rrr)Zfunc_with_kwonly_argsr+r,r-r�
ValueError)rL�func_cachedrrr�!test_memory_func_with_kwonly_args�s""(r�cCsHtdtddd�}|jt�}tjj|dd�d�dS)Nrrr%rr
g@g@)rrr)Zfunc_with_signaturer+r,r-)rLr�rrr�test_memory_func_with_signature�sr�)?�__doc__rr�os.path�tempfilerr�r[r>r<r�r+Z
joblib.memoryrrrrrrZjoblib.test.commonr	r
Zjoblib.testingrZjoblib._compatrr�dictrrr#r$r2rIrMrPrSrdrerirmrorprrrxrr�r�r�r�r�r�r�r�r�r�r�r�r�r��execr�r�rrrr�<module>sj"	
>&

 $E