Your IP : 216.73.216.52


Current Path : /opt/alt/python35/lib64/python3.5/test/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/test/__pycache__/test_weakref.cpython-35.pyc



��Yf��@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
ddlmZda
dZGdd�d�ZGdd�d�Zd	d
�Zdd�ZGd
d�d�ZGdd�d�ZGdd�dej�Zejddd��ZGdd�de�ZGdd�de�ZGdd�dej�ZGdd�de�Zddl	mZGdd �d ej�ZGd!d"�d"ej�Z Gd#d$�d$ej�Z!d%Z"d&e"iZ#d'd(�Z$e%d)kre$�dS)*�N)�support)�
script_helperZfoobarc@seZdZdd�ZdS)�CcCsdS)N�)�selfrr�6/opt/alt/python35/lib64/python3.5/test/test_weakref.py�methodszC.methodN)�__name__�
__module__�__qualname__rrrrrrsrc@s"eZdZdZdd�ZdS)�CallableNcCs
||_dS)N)�bar)r�xrrr�__call__szCallable.__call__)r	r
rr
rrrrrrsrcCsdd�}|S)NcSsdS)Nrrrrr�f"szcreate_function.<locals>.fr)rrrr�create_function!srcCs
t�jS)N)rrrrrr�create_bound_method%src@sdeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)�ObjectcCs
||_dS)N)�arg)rrrrr�__init__*szObject.__init__cCsd|jS)Nz<Object %r>)r)rrrr�__repr__,szObject.__repr__cCs#t|t�r|j|jkStS)N)�
isinstancerr�NotImplemented)r�otherrrr�__eq__.sz
Object.__eq__cCs#t|t�r|j|jkStS)N)rrrr)rrrrr�__lt__2sz
Object.__lt__cCs
t|j�S)N)�hashr)rrrr�__hash__6szObject.__hash__cCsdS)N�r)rrrr�some_method8szObject.some_methodcCsdS)N�r)rrrr�other_method:szObject.other_methodN)
r	r
rrrrrrrr!rrrrr)src@seZdZdd�ZdS)�RefCyclecCs
||_dS)N)�cycle)rrrrr?szRefCycle.__init__N)r	r
rrrrrrr">sr"c@s(eZdZdd�Zdd�ZdS)�TestBasecCs
d|_dS)Nr)�cbcalled)rrrr�setUpEszTestBase.setUpcCs|jd7_dS)N�)r%)r�refrrr�callbackHszTestBase.callbackN)r	r
rr&r)rrrrr$Csr$g-C��6?c#s{tjd�}d���fdd�}tj��>|jd|�}|j�z	dVWdd�|j�XWdQRXdS)zR
    Ensure GC collections happen in a different thread, at a high frequency.
    �	threadingFcs(x!�s#tj��tj�qWdS)N)�timeZsleep�gc�collectr)�period�please_stoprrr-Ts	
z"collect_in_thread.<locals>.collect�targetNT)r�
import_moduleZ
disable_gcZThread�start�join)r.r*r-�tr)r.r/r�collect_in_threadLs

	r5c@sVeZdZdd�Zdd�Zejdd��Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�Zd5d6�Zd7d8�Z d9d:�Z!d;d<�Z"d=d>�Z#ej$d?d@��Z%dAdB�Z&dCdD�Z'dEdF�Z(dGdH�Z)dIdJ�Z*dKdL�Z+dMdN�Z,dOdP�Z-dQdR�Z.dSdT�Z/dUdV�Z0dWdX�Z1dYdZ�Z2d[d\�Z3d]d^�Z4d_S)`�ReferencesTestCasecCsZ|jt�|jt�|jt�t�}tj|�}t|�~t|�dS)N)�check_basic_refrrr�weakrefr(�repr)r�o�wrrrr�test_basic_refes


	
z!ReferencesTestCase.test_basic_refcCs+|jt�|jt�|jt�dS)N)�check_basic_callbackrrr)rrrr�test_basic_callbackss

z&ReferencesTestCase.test_basic_callbackcCsqddl}|j}|�}tj|�}|j|�|�~|j|��|j|�|j|�dS)Nr)�	_testcapi�create_cfunctionr8r(�assertIs�assertIsNoner7r=)rr?r@rr;rrr�test_cfunctionxs		
z!ReferencesTestCase.test_cfunctioncCsvt�}tj||j�}tj||j�}~|j|�d�|j|�d�|j|jdd�dS)Nz$expected reference to be invalidated�z-callback not called the right number of times)rr8r(r)rB�assertEqualr%)rr:�ref1�ref2rrr�test_multiple_callbacks�s	z*ReferencesTestCase.test_multiple_callbackscCsF|dd�}t�}tj||�|_tj||�}~dS)NcSs|j�dS)N)r()�objectrrrrr)�szDReferencesTestCase.test_multiple_selfref_callbacks.<locals>.callback)rr8r()rr)�crFrrr�test_multiple_selfref_callbacks�s
	z2ReferencesTestCase.test_multiple_selfref_callbackscCs)t�}|jttj|dd�dS)Nr))r�assertRaises�	TypeErrorr8r()rrJrrr�test_constructor_kwargs�s	z*ReferencesTestCase.test_constructor_kwargscCs�t�}d|_tj||j�}tj||j�}~dd�}|jt||�|jt||�|jtttjt���|j|j	d�dS)Nr'cSs|jdS)N)r
)�proxyrrr�check�sz0ReferencesTestCase.test_proxy_ref.<locals>.checkrD)
rr
r8rOr)rL�ReferenceError�boolrEr%)rr:rFrGrPrrr�test_proxy_ref�s		z!ReferencesTestCase.test_proxy_refcCsK|�}tj|�}|j|�d�|�}|j||d�dS)Nz,weak reference to live object should be livez-<ref>() should return original object if live)r8r(�assertIsNotNonerA)r�factoryr:r(�o2rrrr7�s		z"ReferencesTestCase.check_basic_refcCsWd|_|�}tj||j�}~|j|jdd�|j|�d�dS)Nrr'z(callback did not properly set 'cbcalled'z3ref2 should be dead after deleting object reference)r%r8r(r)rErB)rrUr:r(rrrr=�s		z'ReferencesTestCase.check_basic_callbackcCs�t�}tj|�}tj|�}tj|�}|j||d�t�}tj|�}tj|�}tj|�}|j||d�|jtj|�dd�~|jtj|�dd�dS)Nz1reference object w/out callback should be re-usedrDzwrong weak ref count for objectr'z4wrong weak ref count for object after deleting proxy)rr8r(rOrArE�getweakrefcount)rr:rFrOrGrrr�test_ref_reuse�s"		z!ReferencesTestCase.test_ref_reusecCsMt�}tj|�}tj|�}tj|�}|j||d�dS)Nz4proxy object w/out callback should have been re-used)rr8rOr(rA)rr:�proxy1r(�proxy2rrr�test_proxy_reuse�s	z#ReferencesTestCase.test_proxy_reusecCs�t�}|j|tj|��tj�}tj|�}|j|d�|jd�|jt	|�d�|j
|d�ddg|dd�<|jt	|�d�|jt	|�d�|jd|d�d|d<|j|dd�|j|dd�tj|�}tj|�}|j||�tjtd	��}tj|�}|j|dd�|dd��|j|dd�|dd��|j|dd�|dd��|j|dd�|dd��dS)
Nz(proxy for empty UserList should be false�r'z+proxy for non-empty UserList should be truerD�z,proxy didn't support __contains__() properlyr �
)
r�check_proxyr8rO�collections�UserList�assertFalse�appendrE�len�
assertTrue�assertIn�range)rr:�L�pZL2�p2ZL3Zp3rrr�test_basic_proxy�s0	

$$$z#ReferencesTestCase.test_basic_proxycCsaGdd�dt�}|�}|jdttj|���|jttj|��d�dS)Nc@s(eZdZdd�Zdd�ZdS)z0ReferencesTestCase.test_proxy_unicode.<locals>.CcSsdS)N�stringr)rrrr�__str__sz8ReferencesTestCase.test_proxy_unicode.<locals>.C.__str__cSsdS)Nsbytesr)rrrr�	__bytes__sz:ReferencesTestCase.test_proxy_unicode.<locals>.C.__bytes__N)r	r
rrmrnrrrrrsrrnsbytes)rIrf�dirr8rOrE�bytes)rr�instancerrr�test_proxy_unicodes	z%ReferencesTestCase.test_proxy_unicodecCsHGdd�d�}|�}tj|�}|jtj|�d�dS)Nc@seZdZdd�ZdS)z.ReferencesTestCase.test_proxy_index.<locals>.CcSsdS)Nr^r)rrrr�	__index__
sz8ReferencesTestCase.test_proxy_index.<locals>.C.__index__N)r	r
rrsrrrrrsrr^)r8rOrE�operator�index)rrr:rirrr�test_proxy_indexs	z#ReferencesTestCase.test_proxy_indexcCs]Gdd�d�}|�}tj|�}|j|dd�|d}|j|d�dS)Nc@s(eZdZdd�Zdd�ZdS)z,ReferencesTestCase.test_proxy_div.<locals>.CcSsdS)N�*r)rrrrr�__floordiv__sz9ReferencesTestCase.test_proxy_div.<locals>.C.__floordiv__cSsdS)N�r)rrrrr�
__ifloordiv__sz:ReferencesTestCase.test_proxy_div.<locals>.C.__ifloordiv__N)r	r
rrxrzrrrrrsrr rwry)r8rOrE)rrr:rirrr�test_proxy_divs	
z!ReferencesTestCase.test_proxy_divcCs|jtj�dS)N)�check_shared_without_callbackr8r()rrrr� test_shared_ref_without_callback'sz3ReferencesTestCase.test_shared_ref_without_callbackcCs|jtj�dS)N)r|r8rO)rrrr�"test_shared_proxy_without_callback*sz5ReferencesTestCase.test_shared_proxy_without_callbackcCs�td�}||d�}||d�}|j||d�~~||�}||d�}|j||d�~~||�}||�}|j||d�~~||d�}||�}|j||d�dS)Nr'z%both callbacks were None in the C APIz&callbacks were NULL, None in the C APIz%both callbacks were NULL in the C APIz&callbacks were None, NULL in the C API)rrA)r�makerefr:Zp1rjrrrr|-s z0ReferencesTestCase.check_shared_without_callbackcCs�t�}tj|�}|j||�|jt|�tjd�|d�|j|jdd�|dd�|j|jdd�|j	t
|�|j	t
|ddd�dS)	Nzproxy is not of callable typez	twinkies!z1call through proxy not passed through to originalrzSplat.r'rDr])rr8rOr_rA�type�CallableProxyTyperEr
rLrM)rr:rFrrr�test_callable_proxy?s	

z&ReferencesTestCase.test_callable_proxycCs�d|_|j|jdd�d|_|j|jdd�|`|jt|d�d�d|_|j|jdd�d|_|j|jdd�|`|jt|d�d	�dS)
Nr'z)proxy does not reflect attribute additionrDz-proxy does not reflect attribute modification�fooz(proxy does not reflect attribute removalz4object does not reflect attribute addition via proxyz8object does not reflect attribute modification via proxyz3object does not reflect attribute removal via proxy)r�rErb�hasattr)rr:rOrrrr_Ts$				zReferencesTestCase.check_proxycCsIGdd�d�}|�}tj|�}|d=|j|jd�dS)Nc@s"eZdZdZdd�ZdS)z3ReferencesTestCase.test_proxy_deletion.<locals>.FooNcSs
||_dS)N)�result)rZaccessorrrr�__delitem__msz?ReferencesTestCase.test_proxy_deletion.<locals>.Foo.__delitem__)r	r
rr�r�rrrr�Fooksr�r)r8rOrEr�)rr��grrrr�test_proxy_deletionis
	z&ReferencesTestCase.test_proxy_deletioncCsHGdd�dt�}|�}|jttj|��t|��dS)Nc@seZdZdS)z0ReferencesTestCase.test_proxy_bool.<locals>.ListN)r	r
rrrrr�Listvsr�)�listrErRr8rO)rr�Zlystrrr�test_proxy_boolts	z"ReferencesTestCase.test_proxy_boolcCs�t�}tj|�}tj||j�}|jtj|�dd�tj|�}tj||j�}|jtj|�dd�~~~~|jtj|�dd�|jtjd�dd�dS)NrDz*got wrong number of weak reference objectsrrzAweak reference objects not unlinked from referent when discarded.r'z2got wrong number of weak reference objects for int)rr8r(r)rErWrO)rr:rFrGrYrZrrr�test_getweakrefcountzs	z'ReferencesTestCase.test_getweakrefcountcCs�t�}tj||j�}tj||j�}~|jtj|�|gd�t�}tj||j�}tj||j�}~|jtj|�|gd�~|jtj|�gd�|jtjd�gd�dS)Nzlist of refs does not matchzlist of refs not clearedr'z#list of refs does not match for int)rr8r(r)rE�getweakrefs)rr:rFrGrrr�test_getweakrefs�s"		z#ReferencesTestCase.test_getweakrefscCs]Gdd�dt�}|d�}tj|�}|j|dd�|jd|d�dS)Nc@seZdZdS)z6ReferencesTestCase.test_newstyle_number_ops.<locals>.FN)r	r
rrrrr�F�sr�g@g�?g@)�floatr8rOrE)rr�rrirrr�test_newstyle_number_ops�s
z+ReferencesTestCase.test_newstyle_number_opscs�Gdd�dt��i��fdd�����fdd�}y|�Wn�k
reYnX|jd�y|�Wn�k
r�YnX|jd�dS)Nc@seZdZdS)z?ReferencesTestCase.test_callbacks_protected.<locals>.BogusErrorN)r	r
rrrrr�
BogusError�sr�cs�|=dS)Nr)�k)�datarr�remove�sz;ReferencesTestCase.test_callbacks_protected.<locals>.removecs,dd�}d�tj|��<��dS)NcSsfS)Nrrrrr�<lambda>�szRReferencesTestCase.test_callbacks_protected.<locals>.encapsulate.<locals>.<lambda>)r8r()r)r�r�r�rr�encapsulate�sz@ReferencesTestCase.test_callbacks_protected.<locals>.encapsulatezexception not properly restored)�	ExceptionZfail)rr�r)r�r�r�r�test_callbacks_protected�s


z+ReferencesTestCase.test_callbacks_protectedcs�ddl�Gdd�dt�}|�}tj|�fdd��}~~|�}|�|_tj|j�fdd��}|�}||_~~dS)Nrc@seZdZdS)z0ReferencesTestCase.test_sf_bug_840829.<locals>.CN)r	r
rrrrrr�srcs
�j�S)N)r-)�ignore)r,rrr��sz7ReferencesTestCase.test_sf_bug_840829.<locals>.<lambda>cs
�j�S)N)r-)r�)r,rrr��s)r,rIr8r(�i�c1)rrrJr;r��c2r)r,r�test_sf_bug_840829�s		!			z%ReferencesTestCase.test_sf_bug_840829cCsyddl}Gdd�dt�}Gdd�dt�}|�}||_tj||j�|_~~~|j�dS)Nrc@seZdZdS)z6ReferencesTestCase.test_callback_in_cycle_1.<locals>.JN)r	r
rrrrr�J�sr�c@seZdZdd�ZdS)z7ReferencesTestCase.test_callback_in_cycle_1.<locals>.IIcSs|jdS)N)r�)rr�rrr�	acallback�szAReferencesTestCase.test_callback_in_cycle_1.<locals>.II.acallbackN)r	r
rr�rrrr�II�sr�)r,rIr�r8r(r�r;r-)rr,r�r��Irrr�test_callback_in_cycle_1�s			z+ReferencesTestCase.test_callback_in_cycle_1cCsvddl}Gdd�dt�}Gdd�d�}|�}||_tj||j�|_~~~|j�dS)Nrc@seZdZdS)z6ReferencesTestCase.test_callback_in_cycle_2.<locals>.JN)r	r
rrrrrr�&sr�c@seZdZdd�ZdS)z7ReferencesTestCase.test_callback_in_cycle_2.<locals>.IIcSs|jdS)N)r�)rr�rrrr�*szAReferencesTestCase.test_callback_in_cycle_2.<locals>.II.acallbackN)r	r
rr�rrrrr�)sr�)r,rIr�r8r(r�r;r-)rr,r�r�r�rrr�test_callback_in_cycle_2s			z+ReferencesTestCase.test_callback_in_cycle_2cCspddl}Gdd�d�}|�|�}}||_||_tj||j�|_~~|j�dS)Nrc@seZdZdd�ZdS)z6ReferencesTestCase.test_callback_in_cycle_3.<locals>.CcSs|j|j|jdS)N)�mer�r;)rr�rrr�cb>sz9ReferencesTestCase.test_callback_in_cycle_3.<locals>.C.cbN)r	r
rr�rrrrr=sr)r,r�r�r8r(r�r;r-)rr,rr�r�rrr�test_callback_in_cycle_34s		z+ReferencesTestCase.test_callback_in_cycle_3cCs�ddl}Gdd�dt�}Gdd�d�}|�|�}}||_||_tj||j�|_~~~~|j�dS)Nrc@seZdZdd�ZdS)z6ReferencesTestCase.test_callback_in_cycle_4.<locals>.CcSs|j|j|jdS)N)r�r�r;)rr�rrrr�Wsz9ReferencesTestCase.test_callback_in_cycle_4.<locals>.C.cbN)r	r
rr�rrrrrVsrc@seZdZdS)z6ReferencesTestCase.test_callback_in_cycle_4.<locals>.DN)r	r
rrrrr�D\sr�)	r,rIr�r�r8r(r�r;r-)rr,rr�r�r�rrr�test_callback_in_cycle_4Ls			z+ReferencesTestCase.test_callback_in_cycle_4cs!ddl}g�G�fdd�dt�}|d�|d�}}||_||_tj||j�|_tj||j�|_�fdd�}tj||�}~~~|j�g�|j�|j�dg�|j|�d��dd�=|j�|j�g�dS)	Nrcs.eZdZdd�Z�fdd�ZdS)zAReferencesTestCase.test_callback_in_cycle_resurrection.<locals>.CcSs
||_dS)N)Z	attribute)r�valuerrrrwszJReferencesTestCase.test_callback_in_cycle_resurrection.<locals>.C.__init__cs�j|j�dS)N)rcrJ)rr�)�alistrrr�zszKReferencesTestCase.test_callback_in_cycle_resurrection.<locals>.C.acallbackN)r	r
rrr�r)r�rrrvsrr'rDcs�jd�dS)NzC went away)rc)r�)r�rr�C_went_away�szKReferencesTestCase.test_callback_in_cycle_resurrection.<locals>.C_went_awayzC went away)	r,rIrJr8r(r�r;rEr-)rr,rr�r�r�r;r)r�r�#test_callback_in_cycle_resurrectionhs$			


z6ReferencesTestCase.test_callback_in_cycle_resurrectioncs7ddl}g��fdd�}G�fdd�dt�}|�|�}}||_||_|j}tj||�|_tj||j�|_tj||�}|j|�|�~~~~|j�g�|j	�|j�dg�|j|�d��dd�=|j	�|j�g�dS)Nrcs�jd�dS)Nzsafe_callback called)rc)r�)r�rr�
safe_callback�szDReferencesTestCase.test_callbacks_on_callback.<locals>.safe_callbackcs"eZdZ�fdd�ZdS)z8ReferencesTestCase.test_callbacks_on_callback.<locals>.Ccs�jd�dS)Nz	cb called)rc)rr�)r�rrr��sz;ReferencesTestCase.test_callbacks_on_callback.<locals>.C.cbN)r	r
rr�r)r�rrr�srzsafe_callback called)
r,rIrr�r8r(r;rArEr-)rr,r�rrJ�dr)Zexternal_wrr)r�r�test_callbacks_on_callback�s(				


z-ReferencesTestCase.test_callbacks_on_callbackcCs|jtj�dS)N)�check_gc_during_creationr8r()rrrr�test_gc_during_ref_creation�sz.ReferencesTestCase.test_gc_during_ref_creationcCs|jtj�dS)N)r�r8rO)rrrr�test_gc_during_proxy_creation�sz0ReferencesTestCase.test_gc_during_proxy_creationcCs�tj�}tjddd�tj�Gdd�d�}dd�}|�}|�}||_||�|_z|�}tj||�Wdtj|�XdS)Nr'c@seZdZdS)z6ReferencesTestCase.check_gc_during_creation.<locals>.AN)r	r
rrrrr�A�sr�cWsdS)Nr)�argsrrrr)�sz=ReferencesTestCase.check_gc_during_creation.<locals>.callback)r,�
get_threshold�
set_thresholdr-�ar;r8r()rrZ
thresholdsr�r)Z
referencedr�rrrr��s
				z+ReferencesTestCase.check_gc_during_creationcCs#Gdd�dt�}|�}dS)Nc@seZdZdd�ZdS)z>ReferencesTestCase.test_ref_created_during_del.<locals>.TargetcSstj|�adS)N)r8r(�ref_from_del)rrrr�__del__�szFReferencesTestCase.test_ref_created_during_del.<locals>.Target.__del__N)r	r
rr�rrrr�Target�sr�)rI)rr��wrrr�test_ref_created_during_del�sz.ReferencesTestCase.test_ref_created_during_delcCs?tjt�}|jt|jddddd�tj�dS)Nr)r8r(r�rLrMrr,r-)r�rrrr�	test_init�s"zReferencesTestCase.test_initcCsxGdd�dt�}g}tjt�tj||j�}d}tj�|j|�d�|j||g�dS)Nc@seZdZdS)z*ReferencesTestCase.test_classes.<locals>.AN)r	r
rrrrrr��sr�)rIr8r(�intrcr,r-rE)rr��lr�rrr�test_classes�s

zReferencesTestCase.test_classesc	Cs�td�}td�}td�}tj|�}tj|�}tj|�}tj|�}|j||k�|j||k�|j||k�|j||k�|j||k�|j||k�~~~tj�x*|||fD]}|j|�d�q�W|j||k�|j||k�|j||k�|j||k�|j||k||k�|j||k||k	�dS)Nr'rD)	rr8r(rerbr,r-rArE)	rr�y�zr��brJr�r�rrr�
test_equality�s.	
z ReferencesTestCase.test_equalitycCs�tjtjtjtjg}td�}td�}tj|�}tj|�}x$|D]}|jt	|||�q[W~~t
j�x$|D]}|jt	|||�q�WdS)Nr')rt�lt�gt�le�gerr8r(rLrMr,r-)rZopsrr�r�r��oprrr�
test_orderings


z ReferencesTestCase.test_orderingcCs�td�}td�}tj|�}tj|�}|jt|�td��~~tj�|jt|�td��|jtt|�dS)Nrw)	rr8r(rErr,r-rLrM)rrr�r�r�rrr�test_hashing,s
zReferencesTestCase.test_hashingcCskGdd�d�}tj�}|d�}}x(td�D]}||�||<}q<W~tj�dS)Nc@seZdZdd�ZdS)z1ReferencesTestCase.test_trashcan_16602.<locals>.CcsD|s
dStj|���fdd�}tj||�|_dS)Ncs
��}dS)Nr)�wparentr:)�wselfrrr�EszFReferencesTestCase.test_trashcan_16602.<locals>.C.__init__.<locals>.cb)r8r(r�)r�parentr�r)r�rrAs
z:ReferencesTestCase.test_trashcan_16602.<locals>.C.__init__N)r	r
rrrrrrr@sr�d)r8�WeakKeyDictionaryrgr,r-)rrr��rootrJ�nrrr�test_trashcan_16602;s	z&ReferencesTestCase.test_trashcan_16602cCs`td�}dd�}tj||�}|j|j|�tj|�}|j|j�dS)Nr'cSsdS)Nr)r(rrrr�Rsz<ReferencesTestCase.test_callback_attribute.<locals>.<lambda>)rr8r(rA�__callback__rB)rrr)rFrGrrr�test_callback_attributePsz*ReferencesTestCase.test_callback_attributecCsRtd�}tj||j�}|j|j�~tj�|j|j�dS)Nr')	rr8r(r)rTr�r�
gc_collectrB)rrr(rrr�&test_callback_attribute_after_deletionYs
z9ReferencesTestCase.test_callback_attribute_after_deletionc	CsTtd�}dd�}tj||�}|jt��dd�|_WdQRXdS)Nr'cSsdS)Nr)r(rrrr�csz@ReferencesTestCase.test_set_callback_attribute.<locals>.<lambda>cSsdS)Nr)r(rrrr�fs)rr8r(rL�AttributeErrorr�)rrr)rFrrr�test_set_callback_attributeas
z.ReferencesTestCase.test_set_callback_attributecCsKGdd�dt�}|d�}tj|dd��}~tj�dS)Nc@seZdZdd�ZdS)z;ReferencesTestCase.test_callback_gcs.<locals>.ObjectWithDelcSsdS)Nr)rrrrr�jszCReferencesTestCase.test_callback_gcs.<locals>.ObjectWithDel.__del__N)r	r
rr�rrrr�
ObjectWithDelisr�r'cSs
tj�S)N)rr�)r(rrrr�lsz6ReferencesTestCase.test_callback_gcs.<locals>.<lambda>)rr8r(rr�)rr�rrFrrr�test_callback_gcshs
z$ReferencesTestCase.test_callback_gcsN)5r	r
rr<r>r�cpython_onlyrCrHrKrNrSr7r=rXr[rkrrrvr{r}r~r|r�r_r�r�r�r�r�r�r�r�r�r�r�Zrequires_type_collectingr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr6cs^
	
((/&!	r6c@sLeZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�SubclassableWeakrefTestCasecCs�Gdd�dtj�}td�}||dd�}|j|�|�|j|j�|j|jd�~|j|��|j|j�dS)Ncs:eZdZdd�fdd�Z�fdd�Z�S)z=SubclassableWeakrefTestCase.test_subclass_refs.<locals>.MyRefNrwcs ||_t�j||�dS)N)r��superr)r�obr)r�)�	__class__rrrus	zFSubclassableWeakrefTestCase.test_subclass_refs.<locals>.MyRef.__init__csd|_t�j�S)NT)�calledr�r)r)r�rrrxs	zFSubclassableWeakrefTestCase.test_subclass_refs.<locals>.MyRef.__call__)r	r
rrrrr)r�r�MyReftsr�r�r��)	r8r(rrArer�rEr�rB)rr�r:Zmrrrr�test_subclass_refsssz.SubclassableWeakrefTestCase.test_subclass_refscCsGdd�dtj�}td�}||�}tj|�}|j||�|jtj|�||g�|jtj|�d�||�}|jtj|�d�tj|�}|jt|�d�|j||d�|j	||dd��|j	||dd��dS)Nc@seZdZdS)zXSubclassableWeakrefTestCase.test_subclass_refs_dont_replace_standard_refs.<locals>.MyRefN)r	r
rrrrrr��sr�rwrDr]rr')
r8r(r�assertIsNotrEr�rWrdrArf)rr�r:�r1�r2Zr3�refsrrr�-test_subclass_refs_dont_replace_standard_refs�szISubclassableWeakrefTestCase.test_subclass_refs_dont_replace_standard_refscCs�Gdd�dtj�}td�}||t�}||t�}|j||�tj|�}|j||�|j||�dS)Nc@seZdZdS)zUSubclassableWeakrefTestCase.test_subclass_refs_dont_conflate_callbacks.<locals>.MyRefN)r	r
rrrrrr��sr�rw)r8r(r�id�strr�r�rf)rr�r:r�r�r�rrr�*test_subclass_refs_dont_conflate_callbacks�szFSubclassableWeakrefTestCase.test_subclass_refs_dont_conflate_callbackscCs�Gdd�dtj�}td�}||ddd�}|j|jd�|j|jd�|j|j�d�|jt|d��dS)Nc@s:eZdZd
Zdd�Zdd�Zdd�Zd	S)zHSubclassableWeakrefTestCase.test_subclass_refs_with_slots.<locals>.MyRef�slot1�slot2cSstjj|||�S)N)r8r(�__new__)r�r�r)r�r�rrrr��szPSubclassableWeakrefTestCase.test_subclass_refs_with_slots.<locals>.MyRef.__new__cSs||_||_dS)N)r�r�)rr�r)r�r�rrrr�s	zQSubclassableWeakrefTestCase.test_subclass_refs_with_slots.<locals>.MyRef.__init__cSs|j|jS)N)r�r�)rrrr�meth�szMSubclassableWeakrefTestCase.test_subclass_refs_with_slots.<locals>.MyRef.methN)r�r�)r	r
r�	__slots__r�rr�rrrrr��sr�rw�abc�defZabcdef�__dict__)	r8r(rrEr�r�r�rbr�)rr�r:r�rrr�test_subclass_refs_with_slots�s	z9SubclassableWeakrefTestCase.test_subclass_refs_with_slotscs�Gdd�dtj�}�fdd�}t�}|||�}||_~~�j�jd�t�}|||�}|||�}||_||_~~~�j�jd�dS)Nc@seZdZdS)zHSubclassableWeakrefTestCase.test_subclass_refs_with_cycle.<locals>.MyRefN)r	r
rrrrrr��sr�cs�jd7_dS)Nr')r%)r�)rrrr)�szKSubclassableWeakrefTestCase.test_subclass_refs_with_cycle.<locals>.callbackr)r8r(rr:rEr%r�)rr�r)r:r�r�r)rr�test_subclass_refs_with_cycle�s"					z9SubclassableWeakrefTestCase.test_subclass_refs_with_cycleN)r	r
rr�r�r�r�r�rrrrr�qs
r�c@s�eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Ze	j
d
d��Zdd�Zdd�Z
dS)�WeakMethodTestCasecCsGdd�dt�}|S)z3Return an Object subclass overriding `some_method`.c@seZdZdd�ZdS)z'WeakMethodTestCase._subclass.<locals>.CcSsdS)N�r)rrrrr�sz3WeakMethodTestCase._subclass.<locals>.C.some_methodN)r	r
rrrrrrr�sr)r)rrrrr�	_subclass�szWeakMethodTestCase._subclasscCs�td�}tj|j�}|j|tj�|j|�t|j��|j|�j|�|j|�j	|jj	�|j
|��d�dS)Nr'r)rr8�
WeakMethodrZassertIsInstance�
ReferenceTyper�rA�__self__�__func__rE)rr:r�rrr�
test_alive�szWeakMethodTestCase.test_alivecCsBtd�}tj|j�}~tj�|j|�d�dS)Nr')rr8r�rr,r-rA)rr:r�rrr�test_object_dead�s

z#WeakMethodTestCase.test_object_deadcCsQ|j�}|d�}tj|j�}|`tj�|j|�d�dS)Nr')r�r8r�rr,r-rA)rrr:r�rrr�test_method_dead�s
z#WeakMethodTestCase.test_method_deadcs�|j�}g��fdd�}|d�}tj|j|�}~tj�|j�|g�tj|_tj�|j�|g�dS)Ncs�j|�dS)N)rc)r)�callsrrr��sz=WeakMethodTestCase.test_callback_when_object_dead.<locals>.cbr')r�r8r�rr,r-rEr)rrr�r:r�r)rr�test_callback_when_object_dead�s

z1WeakMethodTestCase.test_callback_when_object_deadcs�|j�}g��fdd�}|d�}tj|j|�}|`tj�|j�|g�~tj�|j�|g�dS)Ncs�j|�dS)N)rc)r)rrrr�
sz=WeakMethodTestCase.test_callback_when_method_dead.<locals>.cbr')r�r8r�rr,r-rE)rrr�r:r�r)rr�test_callback_when_method_dead	s

z1WeakMethodTestCase.test_callback_when_method_deadcCsVtd�}dd�}tj|j|�}tj|�}~|j|�d�dS)Nr'cSsdS)Nr)�_rrrr�sz-WeakMethodTestCase.test_no_cycles.<locals>.cb)rr8r�rr(rA)rr:r�r�r;rrr�test_no_cyclessz!WeakMethodTestCase.test_no_cyclescs��fdd�}�fdd�}td�}td�}tj|j�}tj|j�}tj|j�}tj|j�}|||�|||�|||�|||�|||�|||�td�}	tj|	j�}
tj|	j�}|||
�|||�|||
�|||�~~~	tj�|||||
|f}xW|D]O}
xF|D]>}�j|
|k|
|k��j|
|k|
|k	�quWqhWdS)Ncs*�j||k��j||k�dS)N)rerb)r�r�)rrr�_eq%sz-WeakMethodTestCase.test_equality.<locals>._eqcs*�j||k��j||k�dS)N)rerb)r�r�)rrr�_ne(sz-WeakMethodTestCase.test_equality.<locals>._ner'rD)rr8r�rr!r,r-rE)rr	r
rr�r�r�rJr�r��err��qr�r)rrr�$s8









	


z WeakMethodTestCase.test_equalitycCs�td�}td�}tj|j�}tj|j�}tj|j�}|jt|�t|��t|�}~~tj�|jt|�|�|jt|�|�|j	t
t|�dS)Nr')rr8r�rr!rErr,r-rLrM)rrr�r�r�rJZharrrr�Js
zWeakMethodTestCase.test_hashingN)r	r
rr�rrrrrrr�rr�r�rrrrr��s	&r�c@seZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Zd:d;�Z d<d=�Z!d>d?�Z"d@dA�Z#dBdC�Z$dDdE�Z%dFdG�Z&dHdI�Z'dJdK�Z(dLdM�Z)dNdO�Z*dPdQ�Z+dRdS�Z,dTS)U�MappingTestCaser^c	s�d}dd�t|�D�}|�fdd�|D��}|j�}yt|�Wntk
rlYnX~tj�t|�}~tj�t|�}|j|d�|j|d�dS)	N�cSsg|]}t��qSr)r")�.0r�rrr�
<listcomp>ds	z4MappingTestCase.check_len_cycles.<locals>.<listcomp>c3s|]}�|�VqdS)Nr)rr:)�consrr�	<genexpr>esz3MappingTestCase.check_len_cycles.<locals>.<genexpr>rr')rr')	rg�items�next�
StopIterationr,r-rdrfrE)	r�	dict_typer�Nr�dct�it�n1�n2r)rr�check_len_cyclesbs 


z MappingTestCase.check_len_cyclescCs|jtjdd��dS)NcSs
|dfS)Nr'r)r�rrrr�wsz<MappingTestCase.test_weak_keyed_len_cycles.<locals>.<lambda>)rr8r�)rrrr�test_weak_keyed_len_cyclesvsz*MappingTestCase.test_weak_keyed_len_cyclescCs|jtjdd��dS)NcSs
d|fS)Nr'r)r�rrrr�zsz=MappingTestCase.test_weak_valued_len_cycles.<locals>.<lambda>)rr8�WeakValueDictionary)rrrr�test_weak_valued_len_cyclesysz+MappingTestCase.test_weak_valued_len_cyclesc
s"|jtjtj��xtdd�D]�}d}tjd�tj|||�dd�t|�D�}|�fdd�|D��}~|j�}yt|�Wntk
r�YnXt	|�}~t	|�}	|j
|d�|j||�|j
|	d�|j|	|�q)WdS)	Nr'r�rrcSsg|]}t��qSr)r")rr�rrrr�s	z2MappingTestCase.check_len_race.<locals>.<listcomp>c3s|]}�|�VqdS)Nr)rr:)rrrr�sz1MappingTestCase.check_len_race.<locals>.<genexpr>)Z
addCleanupr,r�r�rgr-rrrrdZassertGreaterEqualZassertLessEqual)
rrrZthrrrrrrr)rr�check_len_race|s(

zMappingTestCase.check_len_racecCs|jtjdd��dS)NcSs
|dfS)Nr'r)r�rrrr��sz:MappingTestCase.test_weak_keyed_len_race.<locals>.<lambda>)r r8r�)rrrr�test_weak_keyed_len_race�sz(MappingTestCase.test_weak_keyed_len_racecCs|jtjdd��dS)NcSs
d|fS)Nr'r)r�rrrr��sz;MappingTestCase.test_weak_valued_len_race.<locals>.<lambda>)r r8r)rrrr�test_weak_valued_len_race�sz)MappingTestCase.test_weak_valued_len_racecCsU|j�\}}xA|D]9}|jtj|�d�|j|||jd�qWt|j��}t|j�j��}|j	�|j	�|j||d�~~|jt
|�|j�|d=|jt
|�|jdd�~~|jt
|�dd�tj�}|j
t|jd�t�|d<|j
t|jd�dS)Nr'z#wrong object returned by weak dict!z/cloning of weak-valued dictionary did not work!rz/deleting object did not cause dictionary updatez0deleting the values did not clear the dictionaryrD)�make_weak_valued_dictrEr8rWrArr�r�copy�sortrd�COUNTrrL�KeyError�__getitem__r)r�dict�objectsr:�items1�items2rrr�test_weak_values�s.



z MappingTestCase.test_weak_valuescCs9|j�\}}xH|D]@}|jtj|�dd|�|j|j||d�qW|j�}|j�j�}|jt|�t|�d�~~|jt	|�|j
�|d=|jt	|�|j
dd�~~|jt	|�dd�td�}d	||<|j||�|j
d
|�dS)Nr'z&wrong number of weak references to %r!z#wrong object returned by weak dict!z.cloning of weak-keyed dictionary did not work!rz/deleting object did not cause dictionary updatez.deleting the keys did not clear the dictionaryrwz$What is the meaning of the universe?�")�make_weak_keyed_dictrEr8rWrArrr$�setrdr&rrf�assertNotIn)rr)r*r:r+r,rrr�test_weak_keys�s,

zMappingTestCase.test_weak_keyscCsy|j�\}}|j|�|j�}|jt|�t|��t|�}x[|D]S}|�}|j||�|j||�|j|j||�|j|�qZW|jt|�d�t|�}|jtt|j���t|��xa|j�D]S}|�}|j||�|j||�|j|j||�|j|�qW|jt|�d�dS)Nr)	r/�check_iters�keyrefsrErdr�rfrr�)rr)r*r��objects2r;r�rrr�test_weak_keyed_iters�s*

	(	z%MappingTestCase.test_weak_keyed_iterscCss|j�\}}|j|�|j�}|jt|�t|��t|�}xX|D]P}|�}|j|||j�|j|j||jj�|j|�qZW|jt|�d�t|�}|jtt|j���t|��x^|j�D]P}|�}|j|||j�|j|j||jj�|j|�qW|jt|�d�dS)Nr)	r#r3�	valuerefsrErdr�rr��
itervaluerefs)rr)r*r�r5r;r�rrr�test_weak_valued_iters�s&

	(	z&MappingTestCase.test_weak_valued_iterscCst|j��}x!|j�D]}|j|�qW|j|d�t|j��}x|D]}|j|�q_W|j|d�t|j��}x!|j�D]}|j|�q�W|j|d�t|j��}x!|j�D]}|j|�q�W|j|d�dS)Nzitems() did not touch all itemsz!__iter__() did not touch all keysz!iterkeys() did not touch all keysz%itervalues() did not touch all values)r�rr�rb�keys�values)rr)r�itemr:r�r;�vrrrr3s"
	zMappingTestCase.check_iterscCs�t|�}tt||���}t|�|d=tj�|jtt|��t|�t|�dg�~|jt|�|d�dS)Nr'���)	rd�iter�getattrrr,r-rfr�rE)rr)r*Z	iter_namer�rrrr�"check_weak_destroy_while_iterating!s

2z2MappingTestCase.check_weak_destroy_while_iteratingc-Cs`|��\}}|j||�WdQRX|��#\}}|jt|j|�WdQRX|j||�|��#\}}|jt|j|�WdQRX|j||�|��\}}|||<WdQRX|j|||�tj|�}|��\}}|j|�WdQRX|j||�|��\}}|j�WdQRX|jt	|�d�dS)Nr)
r1rLr'r��poprEr$�update�clearrd)rr)�testcontextr�r=Zddictrrr�-check_weak_destroy_and_mutate_while_iterating.s$z=MappingTestCase.check_weak_destroy_and_mutate_while_iteratingc&Cs�td�}|��Ut|�}|j�|jt|�|d�|||<|jt|�|�WdQRX|��R|jt|�|d�|jt|j���|jt|�|d�WdQRX|��L|jt|�|d�|t|j��=|jt|�|d�WdQRX|��C|jt|�|d�|j�|jt|�|d�WdQRX|��%|j�|jt|�d�WdQRX|jt|�d�dS)	Ni@�r'rDr]rr r�r)rrd�popitemrErBrr:rD)rr)rEr:r�rrr�&check_weak_del_and_len_while_iteratingGs.



!
!

!

z6MappingTestCase.check_weak_del_and_len_while_iteratingcs�|j�\��|j��d�|j��d�|j��d�|j��d�|j�\��tj��fdd��}|j�|�|j�\��|j�|�dS)Nr:rr;r4c
3sbzJt�j��}t|��j�j}tj�t|�|fVWdd}tj�XdS)N)r?rrrBrr,r-r)rr=)r)r*rrrEms

zKMappingTestCase.test_weak_keys_destroy_while_iterating.<locals>.testcontext)r/rA�
contextlib�contextmanagerrFrH)rrEr)r)r*r�&test_weak_keys_destroy_while_iteratingesz6MappingTestCase.test_weak_keys_destroy_while_iteratingcs�|j�\��|j��d�|j��d�|j��d�|j��d�|j��d�|j�\��tj��fdd��}|j�|�|j�\��|j�|�dS)Nr:rr;r8r7c
3sbzJt�j��}t|��j�j}tj�|t|�fVWdd}tj�XdS)N)r?rrrBrr,r-r)rr�)r)r*rrrE�s

zMMappingTestCase.test_weak_values_destroy_while_iterating.<locals>.testcontext)r#rArIrJrFrH)rrEr)r)r*r�(test_weak_values_destroy_while_iteratingsz8MappingTestCase.test_weak_values_destroy_while_iteratingcCs9td�}tj|di�}|j||d�dS)Nr]il)rr8r�rE)rr:r)rrr�#test_make_weak_keyed_dict_from_dict�sz3MappingTestCase.test_make_weak_keyed_dict_from_dictcCsHtd�}tj|di�}tj|�}|j||d�dS)Nr]il)rr8r�rE)rr:r)�dict2rrr�.test_make_weak_keyed_dict_from_weak_keyed_dict�sz>MappingTestCase.test_make_weak_keyed_dict_from_weak_keyed_dictcCsRtj�}tttt|j���}x|D]}|j||<q1W||fS)N)r8r�r��maprrgr&r)rr)r*r:rrrr/�s

z$MappingTestCase.make_weak_keyed_dictcCs9td�}tjd|i�}|j|d|�dS)Nr]il)rr8rrE)rr:r)rrr�$test_make_weak_valued_dict_from_dict�sz4MappingTestCase.test_make_weak_valued_dict_from_dictcCsHtd�}tjd|i�}tj|�}|j|d|�dS)Nr]il)rr8rrE)rr:r)rNrrr�0test_make_weak_valued_dict_from_weak_valued_dict�sz@MappingTestCase.test_make_weak_valued_dict_from_weak_valued_dictcCs�|jttjj�|jttjii�|jttjff�td�}xVdD]N}tj||i�}|jt|j��|g�|j|||�q[WdS)Nr]rr)r�iterable)rr)rrS)	rLrMr8rrrrEr�r:)rr:�kwr�rrr�test_make_weak_valued_dict_misc�s
z/MappingTestCase.test_make_weak_valued_dict_misccCsRtj�}tttt|j���}x|D]}|||j<q1W||fS)N)r8rr�rPrrgr&r)rr)r*r:rrrr#�s

z%MappingTestCase.make_weak_valued_dictc	Cs�|�}|||<|||<|jt|�d�|j�\}}|jt|�d�||krz|j||�n|j||�|j�\}}|jt|�d�||kr�|j||�n|j||�dS)NrDr'r)rErdrGrA)	r�klass�key1�value1�key2�value2�weakdictr�r=rrr�
check_popitem�s	

zMappingTestCase.check_popitemcCs&|jtjdt�dt��dS)NrWrY)r\r8rr)rrrr�test_weak_valued_dict_popitem�sz-MappingTestCase.test_weak_valued_dict_popitemcCs&|jtjt�dt�d�dS)Nzvalue 1zvalue 2)r\r8r�r)rrrr�test_weak_keyed_dict_popitem�sz,MappingTestCase.test_weak_keyed_dict_popitemcCs�|j||d�|�}|j||�}|j||�|j||�|j|j|�|�|j|||�|j||�}|j||�|j||�|j|j|�|�|j|||�dS)Nz9invalid test -- value parameters must be distinct objects)r��
setdefaultrArf�get)rrV�keyrXrZr[r:rrr�check_setdefault�s	z MappingTestCase.check_setdefaultcCs#|jtjdt�t��dS)Nra)rbr8rr)rrrr� test_weak_valued_dict_setdefault�sz0MappingTestCase.test_weak_valued_dict_setdefaultcCs |jtjt�dd�dS)Nzvalue 1zvalue 2)rbr8r�r)rrrr�test_weak_keyed_dict_setdefault�sz/MappingTestCase.test_weak_keyed_dict_setdefaultcCs�|�}|j|�|jt|�t|��xc|j�D]U}|j||d�|j|�}|j|||�|j||j|��q?Wx^|j�D]P}|j||d�||}|j|||�|j||j|��q�WdS)Nz(mysterious new key appeared in weak dictz%original key disappeared in weak dict)rCrErdr:rfr`rA)rrVr)r[r�r=rrr�check_update�s	

zMappingTestCase.check_updatecCs|jtjdt�dt�t�t�i�|jttjj�tj�}|jt|jii�|jt|jff�|jt|j	��g�t
d�}x`dD]X}tj�}|j||i�|jt|j	��|g�|j|||�q�WdS)	Nr'r�r]rr)rrS)rr)rrS)rer8rrrLrMrCrEr�r:r)rr�r:rTrrr�test_weak_valued_dict_updates%
z,MappingTestCase.test_weak_valued_dict_updatecCs2|jtjt�dt�dt�di�dS)Nr'rDr])rer8r�r)rrrr�test_weak_keyed_dict_updatesz+MappingTestCase.test_weak_keyed_dict_updatecCs�tj�}td�}td�}d||<d||<|jt|�d�||=|jt|�d�|jt|j��|g�dS)N�1�2�	somethingrDr')r8r�rrErdr�r:)rr��o1rVrrr�test_weak_keyed_delitem#s

z'MappingTestCase.test_weak_keyed_delitemcCs�tj�}td�}td�}||d<||d<|jt|�d�|d=|jt|�d�|jt|j��d|fg�dS)Nrhrirjzsomething elserDr')r8rrrErdr�r)rr�rkrVrrr�test_weak_valued_delitem.s

z(MappingTestCase.test_weak_valued_delitemcCs�tj�}td�}|jt|j|�|jt|j|�|jt|jd�|jt|jd�|jt|jdd�dS)Nrh�
)	r8r�rrLr'r�r(rM�__setitem__)rr�r:rrr�test_weak_keyed_bad_delitem9sz+MappingTestCase.test_weak_keyed_bad_delitemcs�tj�}d�G��fdd�dt���fdd�td�D��x�D]}|j||<qWW~t|j����j�d�d}x�D]}|d	7}||=q�W|jt	|�d�|j|d
�dS)NFcs=eZdZdd�Zdd�Z��fdd�ZdS)z<MappingTestCase.test_weak_keyed_cascading_deletes.<locals>.CcSs
||_dS)N)r�)rr�rrrrPszEMappingTestCase.test_weak_keyed_cascading_deletes.<locals>.C.__init__cSs
t|j�S)N)rr�)rrrrrRszEMappingTestCase.test_weak_keyed_cascading_deletes.<locals>.C.__hash__cs�r
�d=|j|jkS)Nr'r>)r�)rr)�mutate�objsrrrTszCMappingTestCase.test_weak_keyed_cascading_deletes.<locals>.C.__eq__N)r	r
rrrrr)rqrrrrrOsrcsg|]}�|��qSrr)rr�)rrrr[s	zEMappingTestCase.test_weak_keyed_cascading_deletes.<locals>.<listcomp>rTrr'rD)
r8r�rIrgr�r�r:�reverserErd)rr�r:�countr)rrqrrr�!test_weak_keyed_cascading_deletesGs 

	

z1MappingTestCase.test_weak_keyed_cascading_deletescCs&tj�}|jt|�d�dS)Nz<WeakValueDictionary at 0x.*>)r8r�assertRegexr9)rr)rrr�test_make_weak_valued_dict_reprtsz/MappingTestCase.test_make_weak_valued_dict_reprcCs&tj�}|jt|�d�dS)Nz<WeakKeyDictionary at 0x.*>)r8r�rvr9)rr)rrr�test_make_weak_keyed_dict_reprxsz.MappingTestCase.test_make_weak_keyed_dict_reprcCs`tj�}t��Dx<td�D].}|jdt��}|j|d�~q#WWdQRXdS)Ni��r^)r8rr5rgr_r"r�)rr�r�rrrr�$test_threaded_weak_valued_setdefault|s
z4MappingTestCase.test_threaded_weak_valued_setdefaultcCsgtj�}t��KxCtd�D]5}t�|d<|jdd�}|j|d�q#WWdQRXdS)Ni��r^)r8rr5rgr"rBr�)rr�r�rrrr�test_threaded_weak_valued_pop�s

z-MappingTestCase.test_threaded_weak_valued_popcCsgtj�}t��KxCtd�D]5}t�}||d<|jt|�d�d}q#WWdQRXdS)Ni@
r^r')r8rr5rgr"rErd)rr�r�r:rrr�%test_threaded_weak_valued_consistency�s
	
z5MappingTestCase.test_threaded_weak_valued_consistencyN)-r	r
rr&rrrr r!r"r-r2r6r9r3rArFrHrKrLrMrOr/rQrRrUr#r\r]r^rbrcrdrerfrgrlrmrprurwrxryrzr{rrrrr
^sT
-r
)�
mapping_testsc@sUeZdZdZded�ded�ded�iZejZdd	�Z	d
S)�WeakValueDictionaryTestCasez?Check that WeakValueDictionary conforms to the mapping protocolrWr'rYrD�key3r]cCs
|jj�S)N)�!_WeakValueDictionaryTestCase__refr$)rrrr�
_reference�sz&WeakValueDictionaryTestCase._referenceN)
r	r
r�__doc__rrr8r�	type2testr�rrrrr}�s*	r}c@sUeZdZdZed�ded�ded�diZejZdd	�Z	d
S)�WeakKeyDictionaryTestCasez=Check that WeakKeyDictionary conforms to the mapping protocolrWr'rYrDr~r]cCs
|jj�S)N)�_WeakKeyDictionaryTestCase__refr$)rrrrr��sz$WeakKeyDictionaryTestCase._referenceN)
r	r
rr�rr�r8r�r�r�rrrrr��s*	r�c@sqeZdZGdd�d�Zdd�Zdd�Zdd�Zd	d
�Zedd��Z	d
d�Z
dS)�FinalizeTestCasec@seZdZdS)zFinalizeTestCase.AN)r	r
rrrrrr��sr�cCs tjjdkrtj�dS)NZcpython)�sys�implementation�namerr�)rrrr�_collect_if_necessary�sz&FinalizeTestCase._collect_if_necessaryc	s��fdd�}|j�}g�tj||dddd�}|j|jd�|j|j�||dddif�|j|�d�|j|�d�|j|�d�|j|j�d�|j|j�d�|j|jd	�|j�dg�g�tj||ddd�}|j|j�||d
if�|j|j�||dif�|j|�d�|j|�d�|j|j�d�|j|j�d�|j|jd	�|j�g�g�tj||d
ddddd�}~|j�|j|�d�|j|�d�|j|j�d�|j|j�d�|j|jd	�|j�dg�dS)Ncs!�j|||�|||S)N)rc)rr�r�)�resrr�add�sz+FinalizeTestCase.test_finalize.<locals>.add�C�+r��YT��Frr�)r�r�)r�r�r�)r�r�r�)r�r8�finalizerE�alive�peek�detachr�)rr�r�rr)r�r�
test_finalize�sB(""$
zFinalizeTestCase.test_finalizec	Cs�|j�}g}tj||jd�}tj||jd�}tj||jd�}tj||jd�}tj||jd�}~~|j|j�|j|j�|j|j�|j|j��|j|j�|�|jd�|�|j|j�|jd�|�|jd�~|j�|j|j�|jd	�|�dddddddd	g}|j	||�dS)
N�f1�f2�f3�f4�f5r��Brr�)
r�r8r�rcrer�r�rbr�rE)	rr�r�r�r�r�r�r�Zexpectedrrr�
test_order�s6




zFinalizeTestCase.test_ordercs�Gdd�dtj�}|j�}g��fdd�}|||�}tj|�}tj|�}~~|j|��|j|��~|j�|j|��|j|��|j�dg�dS)Nc@seZdZdS)z4FinalizeTestCase.test_all_freed.<locals>.MyFinalizerN)r	r
rrrrr�MyFinalizersr�cs�jd�dS)N�{)rcr)r�rrr)sz1FinalizeTestCase.test_all_freed.<locals>.callbackr�)r8r�r�r(rTr�rBrE)rr�r�r)rZwr_callbackZwr_fr)r�r�test_all_freeds
zFinalizeTestCase.test_all_freedcs��fdd�}tj�tdt�}tj�tdt�}tj�|�}tj�tdt�}|jdks�t�d|_|jdks�t�|jdks�t�dS)Ncs+tj�td�}td�dddS)N�g1zf3 errorr'r)r8r��print)r�)�clsrr�errors
z,FinalizeTestCase.run_in_child.<locals>.errorr�r�r�TF)r8r�r��_global_var�atexit�AssertionError)r�r�r�r�r�r�r)r�r�run_in_childs	zFinalizeTestCase.run_in_childcCsmdd}tjd|�\}}}|jd�j�}|j|ddddg�|jd	|k�dS)
Nz/from test.test_weakref import FinalizeTestCase;zFinalizeTestCase.run_in_child()z-c�asciiz	f4 foobarzf3 errorr�z	f1 foobarsZeroDivisionError)rZassert_python_ok�decode�
splitlinesrEre)r�prog�rc�out�errrrr�test_atexit1szFinalizeTestCase.test_atexitN)r	r
rr�r�r�r�r��classmethodr�r�rrrrr��s)'r�a� Doctest for examples in the library reference: weakref.rst

>>> import weakref
>>> class Dict(dict):
...     pass
...
>>> obj = Dict(red=1, green=2, blue=3)   # this object is weak referencable
>>> r = weakref.ref(obj)
>>> print(r() is obj)
True

>>> import weakref
>>> class Object:
...     pass
...
>>> o = Object()
>>> r = weakref.ref(o)
>>> o2 = r()
>>> o is o2
True
>>> del o, o2
>>> print(r())
None

>>> import weakref
>>> class ExtendedRef(weakref.ref):
...     def __init__(self, ob, callback=None, **annotations):
...         super().__init__(ob, callback)
...         self.__counter = 0
...         for k, v in annotations.items():
...             setattr(self, k, v)
...     def __call__(self):
...         '''Return a pair containing the referent and the number of
...         times the reference has been called.
...         '''
...         ob = super().__call__()
...         if ob is not None:
...             self.__counter += 1
...             ob = (ob, self.__counter)
...         return ob
...
>>> class A:   # not in docs from here, just testing the ExtendedRef
...     pass
...
>>> a = A()
>>> r = ExtendedRef(a, foo=1, bar="baz")
>>> r.foo
1
>>> r.bar
'baz'
>>> r()[1]
1
>>> r()[1]
2
>>> r()[0] is a
True


>>> import weakref
>>> _id2obj_dict = weakref.WeakValueDictionary()
>>> def remember(obj):
...     oid = id(obj)
...     _id2obj_dict[oid] = obj
...     return oid
...
>>> def id2obj(oid):
...     return _id2obj_dict[oid]
...
>>> a = A()             # from here, just testing
>>> a_id = remember(a)
>>> id2obj(a_id) is a
True
>>> del a
>>> try:
...     id2obj(a_id)
... except KeyError:
...     print('OK')
... else:
...     print('WeakValueDictionary error')
OK

�
libreftestcCs7tjttttttt�tj	t
jt�dS)N)
rZrun_unittestr6r�r
r}r�r�r�Zrun_doctestr��modulesr	rrrr�	test_main�sr��__main__)&r,r�Zunittestr`r8rtrIr$r+ZtestrZtest.supportrr�r�rrrrrr"ZTestCaser$rJr5r6r�r�r
r|ZBasicTestMappingProtocolr}r�r�r�Z__test__r�r	rrrr�<module>sN	���g���=�