11import unittest
22from weakref import WeakSet
3+ import copy
34import string
45from collections import UserString as ustr
56from collections .abc import Set , MutableSet
@@ -15,6 +16,12 @@ class RefCycle:
1516 def __init__ (self ):
1617 self .cycle = self
1718
19+ class WeakSetSubclass (WeakSet ):
20+ pass
21+
22+ class WeakSetWithSlots (WeakSet ):
23+ __slots__ = ('x' , 'y' )
24+
1825
1926class TestWeakSet (unittest .TestCase ):
2027
@@ -455,6 +462,30 @@ def test_abc(self):
455462 self .assertIsInstance (self .s , Set )
456463 self .assertIsInstance (self .s , MutableSet )
457464
465+ def test_copying (self ):
466+ for cls in WeakSet , WeakSetWithSlots :
467+ s = cls (self .items )
468+ s .x = ['x' ]
469+ s .z = ['z' ]
470+
471+ dup = copy .copy (s )
472+ self .assertIsInstance (dup , cls )
473+ self .assertEqual (dup , s )
474+ self .assertIsNot (dup , s )
475+ self .assertIs (dup .x , s .x )
476+ self .assertIs (dup .z , s .z )
477+ self .assertFalse (hasattr (dup , 'y' ))
478+
479+ dup = copy .deepcopy (s )
480+ self .assertIsInstance (dup , cls )
481+ self .assertEqual (dup , s )
482+ self .assertIsNot (dup , s )
483+ self .assertEqual (dup .x , s .x )
484+ self .assertIsNot (dup .x , s .x )
485+ self .assertEqual (dup .z , s .z )
486+ self .assertIsNot (dup .z , s .z )
487+ self .assertFalse (hasattr (dup , 'y' ))
488+
458489
459490if __name__ == "__main__" :
460491 unittest .main ()
0 commit comments