@@ -2911,6 +2911,54 @@ def test_recursive_set_subclass_and_inst(self):
29112911 def test_recursive_frozenset_subclass_and_inst (self ):
29122912 self ._test_recursive_collection_and_inst (MyFrozenSet )
29132913
2914+ def _test_recursive_collection_in_key (self , factory , minprotocol = 0 ):
2915+ protocols = range (minprotocol , pickle .HIGHEST_PROTOCOL + 1 )
2916+ key = DictKey ()
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_dict_in_key (self ):
2928+ self ._test_recursive_collection_in_key (dict )
2929+
2930+ def test_recursive_dict_subclass_in_key (self ):
2931+ self ._test_recursive_collection_in_key (MyDict )
2932+
2933+ def test_recursive_frozendict_in_key (self ):
2934+ self ._test_recursive_collection_in_key (frozendict , minprotocol = 2 )
2935+
2936+ def test_recursive_frozendict_subclass_in_key (self ):
2937+ self ._test_recursive_collection_in_key (MyFrozenDict )
2938+
2939+ def _test_recursive_collection_in_value (self , factory , minprotocol = 0 ):
2940+ protocols = range (minprotocol , pickle .HIGHEST_PROTOCOL + 1 )
2941+ o = factory (key = [])
2942+ o ['key' ].append (o )
2943+ for proto in protocols :
2944+ with self .subTest (proto = proto ):
2945+ s = self .dumps (o , proto )
2946+ x = self .loads (s )
2947+ self .assertEqual (len (x ['key' ]), 1 )
2948+ self .assertIs (x ['key' ][0 ], x )
2949+
2950+ def test_recursive_dict_in_value (self ):
2951+ self ._test_recursive_collection_in_value (dict )
2952+
2953+ def test_recursive_dict_subclass_in_value (self ):
2954+ self ._test_recursive_collection_in_value (MyDict )
2955+
2956+ def test_recursive_frozendict_in_value (self ):
2957+ self ._test_recursive_collection_in_value (frozendict , minprotocol = 2 )
2958+
2959+ def test_recursive_frozendict_subclass_in_value (self ):
2960+ self ._test_recursive_collection_in_value (MyFrozenDict )
2961+
29142962 def test_recursive_inst_state (self ):
29152963 # Mutable object containing itself.
29162964 y = REX_state ()
0 commit comments