11import unittest
22from weakref import WeakSet
33import copy
4+ import pickle
45import string
56from collections import UserString as ustr
67from collections .abc import Set , MutableSet
@@ -463,12 +464,16 @@ def test_copying(self):
463464 dup = copy .copy (s )
464465 self .assertIsInstance (dup , cls )
465466 self .assertEqual (dup , s )
467+ self .assertEqual (sorted (dup ), sorted (s ))
466468 self .assertIsNot (dup , s )
467469 self .assertIs (dup .x , s .x )
468470 self .assertIs (dup .z , s .z )
469471 self .assertFalse (hasattr (dup , 'y' ))
472+ dup .remove (self .items [0 ])
473+ self .assertEqual (sorted (dup ), sorted (self .items [1 :]))
474+ self .assertEqual (sorted (s ), sorted (self .items ))
470475
471- dup = copy .deepcopy (s )
476+ dup , dupitems = copy .deepcopy ([ s , self . items ] )
472477 self .assertIsInstance (dup , cls )
473478 self .assertEqual (dup , s )
474479 self .assertIsNot (dup , s )
@@ -477,6 +482,45 @@ def test_copying(self):
477482 self .assertEqual (dup .z , s .z )
478483 self .assertIsNot (dup .z , s .z )
479484 self .assertFalse (hasattr (dup , 'y' ))
485+ dup .remove (self .items [0 ])
486+ self .assertEqual (sorted (dup ), sorted (self .items [1 :]))
487+ self .assertEqual (sorted (s ), sorted (self .items ))
488+ del dupitems
489+ support .gc_collect () # For PyPy or other GCs.
490+ self .assertEqual (list (dup ), [])
491+ self .assertEqual (sorted (s ), sorted (self .items ))
492+
493+ def test_copying_2 (self ):
494+ for copyfunc in copy .copy , :#copy.deepcopy:
495+ s = WeakSet ()
496+ dup = copyfunc (s )
497+ x = ustr ('x' )
498+ dup .add (x )
499+ self .assertEqual (len (dup ), 1 )
500+ self .assertEqual (len (s ), 0 )
501+ del x
502+ support .gc_collect () # For PyPy or other GCs.
503+ self .assertEqual (len (dup ), 0 )
504+
505+ def test_pickle (self ):
506+ for cls in WeakSet , WeakSetWithSlots :
507+ s = cls (self .items )
508+ s .x = ['x' ]
509+ s .z = ['z' ]
510+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
511+ with self .subTest (proto = proto , csl = cls ):
512+ pickled = pickle .dumps ([s , self .items ], protocol = proto )
513+ dup , dupitems = pickle .loads (pickled )
514+ self .assertIsInstance (dup , cls )
515+ self .assertEqual (dup , s )
516+ self .assertEqual (dup .x , s .x )
517+ self .assertEqual (dup .z , s .z )
518+ del dupitems [0 ]
519+ support .gc_collect () # For PyPy or other GCs.
520+ self .assertEqual (sorted (dup ), sorted (self .items [1 :]))
521+ del dupitems
522+ support .gc_collect () # For PyPy or other GCs.
523+ self .assertEqual (list (dup ), [])
480524
481525
482526if __name__ == "__main__" :
0 commit comments