@@ -48,6 +48,22 @@ def create_bound_method():
4848 return C ().method
4949
5050
51+ def pickle_function ():
52+ return 42
53+
54+
55+ class ProxyLoop :
56+ # Holds a weak proxy to itself (gh-62268).
57+ setstate_calls = 0
58+
59+ def __init__ (self ):
60+ self .recursive = weakref .proxy (self )
61+
62+ def __setstate__ (self , state ):
63+ type (self ).setstate_calls += 1
64+ self .__dict__ .update (state )
65+
66+
5167class WeakValueDictionaryWithSlots (weakref .WeakValueDictionary ):
5268 __slots__ = ('x' , 'y' )
5369
@@ -1163,6 +1179,98 @@ def __del__(self):
11631179 gc .collect ()
11641180 self .assertEqual (errors , [])
11651181
1182+ def test_getreferent (self ):
1183+ from _weakref import _getreferent
1184+ obj = Object (1 )
1185+ r = weakref .ref (obj )
1186+ p = weakref .proxy (obj )
1187+ self .assertIs (_getreferent (r ), obj )
1188+ self .assertIs (_getreferent (p ), obj )
1189+ self .assertRaises (TypeError , _getreferent , obj )
1190+ del obj
1191+ gc_collect ()
1192+ self .assertIsNone (_getreferent (r ))
1193+ self .assertIsNone (_getreferent (p ))
1194+
1195+ def test_pickle_proxy (self ):
1196+ # A proxy is unpickled as a proxy to the restored object,
1197+ # regardless of pickling order.
1198+ obj = Object (1 )
1199+ p = weakref .proxy (obj )
1200+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
1201+ with self .subTest (proto = proto ):
1202+ obj2 , p2 = pickle .loads (pickle .dumps ([obj , p ], proto ))
1203+ self .assertIs (type (p2 ), weakref .ProxyType )
1204+ self .assertIs (p2 , weakref .proxy (obj2 ))
1205+ p2 , obj2 = pickle .loads (pickle .dumps ([p , obj ], proto ))
1206+ self .assertIs (p2 , weakref .proxy (obj2 ))
1207+
1208+ def test_pickle_callable_proxy (self ):
1209+ obj = pickle_function
1210+ p = weakref .proxy (obj )
1211+ self .assertIs (type (p ), weakref .CallableProxyType )
1212+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
1213+ with self .subTest (proto = proto ):
1214+ obj2 , p2 = pickle .loads (pickle .dumps ([obj , p ], proto ))
1215+ self .assertIs (type (p2 ), weakref .CallableProxyType )
1216+ self .assertIs (p2 , weakref .proxy (obj2 ))
1217+ self .assertEqual (p2 (), 42 )
1218+
1219+ def test_pickle_dead_proxy (self ):
1220+ # A dead proxy stays dead.
1221+ obj = Object (1 )
1222+ p = weakref .proxy (obj )
1223+ del obj
1224+ gc_collect ()
1225+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
1226+ with self .subTest (proto = proto ):
1227+ p2 = pickle .loads (pickle .dumps (p , proto ))
1228+ gc_collect ()
1229+ self .assertIsInstance (p2 , weakref .ProxyTypes )
1230+ self .assertRaises (ReferenceError , getattr , p2 , 'arg' )
1231+
1232+ def test_pickle_proxy_self_loop (self ):
1233+ # The object is restored once (gh-62268).
1234+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
1235+ with self .subTest (proto = proto ):
1236+ data = pickle .dumps (ProxyLoop (), proto )
1237+ ProxyLoop .setstate_calls = 0
1238+ obj = pickle .loads (data )
1239+ self .assertEqual (ProxyLoop .setstate_calls , 1 )
1240+ self .assertIs (obj .recursive , weakref .proxy (obj ))
1241+
1242+ def test_pickle_proxy_without_strong_ref (self ):
1243+ obj = Object (1 )
1244+ p = weakref .proxy (obj )
1245+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
1246+ with self .subTest (proto = proto ):
1247+ self .assertRaises (pickle .PicklingError , pickle .dumps , p , proto )
1248+
1249+ def test_copy_proxy (self ):
1250+ obj = Object (1 )
1251+ p = weakref .proxy (obj )
1252+ self .assertIs (copy .copy (p ), p )
1253+ del obj
1254+ gc_collect ()
1255+ self .assertIs (copy .copy (p ), p )
1256+
1257+ def test_deepcopy_proxy (self ):
1258+ # A deep copy refers to the deep copy of the object,
1259+ # so it must be deep-copied together with it.
1260+ obj = Object (1 )
1261+ p = weakref .proxy (obj )
1262+ obj2 , p2 = copy .deepcopy ([obj , p ])
1263+ self .assertIsNot (obj2 , obj )
1264+ self .assertIs (p2 , weakref .proxy (obj2 ))
1265+ # Otherwise the copy of the object is discarded.
1266+ p2 = copy .deepcopy (p )
1267+ gc_collect ()
1268+ self .assertRaises (ReferenceError , getattr , p2 , 'arg' )
1269+ # A dead proxy is not copied.
1270+ del obj
1271+ gc_collect ()
1272+ self .assertIs (copy .deepcopy (p ), p )
1273+
11661274
11671275class SubclassableWeakrefTestCase (TestBase ):
11681276
0 commit comments