@@ -2839,11 +2839,13 @@ def test_recursive_multi(self):
28392839 self .assertEqual (list (x [0 ].attr .keys ()), [1 ])
28402840 self .assertIs (x [0 ].attr [1 ], x )
28412841
2842- def _test_recursive_collection_and_inst (self , factory , oldminproto = None ):
2842+ def _test_recursive_collection_and_inst (self , factory , oldminproto = None ,
2843+ minprotocol = 0 ):
28432844 if self .py_version < (3 , 0 ):
28442845 self .skipTest ('"classic" classes are not interoperable with Python 2' )
28452846 # Mutable object containing a collection containing the original
28462847 # object.
2848+ protocols = range (minprotocol , pickle .HIGHEST_PROTOCOL + 1 )
28472849 o = Object ()
28482850 o .attr = factory ([o ])
28492851 t = type (o .attr )
@@ -2883,6 +2885,11 @@ def test_recursive_tuple_and_inst(self):
28832885 def test_recursive_dict_and_inst (self ):
28842886 self ._test_recursive_collection_and_inst (dict .fromkeys , oldminproto = 0 )
28852887
2888+ def test_recursive_frozendict_and_inst (self ):
2889+ if self .py_version < (3 , 15 ):
2890+ self .skipTest ('need frozendict' )
2891+ self ._test_recursive_collection_and_inst (frozendict .fromkeys , minprotocol = 2 )
2892+
28862893 def test_recursive_set_and_inst (self ):
28872894 self ._test_recursive_collection_and_inst (set )
28882895
@@ -2904,6 +2911,42 @@ def test_recursive_set_subclass_and_inst(self):
29042911 def test_recursive_frozenset_subclass_and_inst (self ):
29052912 self ._test_recursive_collection_and_inst (MyFrozenSet )
29062913
2914+ def _test_recursive_collection_in_key (self , factory , minprotocol = 0 ):
2915+ protocols = range (minprotocol , pickle .HIGHEST_PROTOCOL + 1 )
2916+ key = Object ()
2917+ o = factory ({key : 1 })
2918+ key .attr = o
2919+ for proto in protocols :
2920+ with self .subTest (proto = proto ):
2921+ s = self .dumps (o , proto )
2922+ x = self .loads (s )
2923+ keys = list (x .keys ())
2924+ self .assertEqual (len (keys ), 1 )
2925+ self .assertIs (keys [0 ].attr , x )
2926+
2927+ def test_recursive_frozendict_in_key (self ):
2928+ self ._test_recursive_collection_in_key (frozendict , minprotocol = 2 )
2929+
2930+ def test_recursive_frozendict_subclass_in_key (self ):
2931+ self ._test_recursive_collection_in_key (MyFrozenDict )
2932+
2933+ def _test_recursive_collection_in_value (self , factory , minprotocol = 0 ):
2934+ protocols = range (minprotocol , pickle .HIGHEST_PROTOCOL + 1 )
2935+ o = factory (key = [])
2936+ o ['key' ].append (o )
2937+ for proto in protocols :
2938+ with self .subTest (proto = proto ):
2939+ s = self .dumps (o , proto )
2940+ x = self .loads (s )
2941+ self .assertEqual (len (x ['key' ]), 1 )
2942+ self .assertIs (x ['key' ][0 ], x )
2943+
2944+ def test_recursive_frozendict_in_value (self ):
2945+ self ._test_recursive_collection_in_value (frozendict , minprotocol = 2 )
2946+
2947+ def test_recursive_frozendict_subclass_in_value (self ):
2948+ self ._test_recursive_collection_in_value (MyFrozenDict )
2949+
29072950 def test_recursive_inst_state (self ):
29082951 # Mutable object containing itself.
29092952 y = REX_state ()
0 commit comments