11import unittest
22from test import support
3- from test .support import os_helper , warnings_helper
4- # import gc
3+ from test .support import warnings_helper
4+ import gc
55import weakref
66import operator
77import copy
@@ -318,20 +318,6 @@ def test_cyclical_repr(self):
318318 name = repr (s ).partition ('(' )[0 ] # strip class name
319319 self .assertEqual (repr (s ), '%s({%s(...)})' % (name , name ))
320320
321- def test_cyclical_print (self ):
322- w = ReprWrapper ()
323- s = self .thetype ([w ])
324- w .value = s
325- fo = open (os_helper .TESTFN , "w" )
326- try :
327- fo .write (str (s ))
328- fo .close ()
329- fo = open (os_helper .TESTFN , "r" )
330- self .assertEqual (fo .read (), repr (s ))
331- finally :
332- fo .close ()
333- os_helper .unlink (os_helper .TESTFN )
334-
335321 # TODO: RUSTPYTHON
336322 @unittest .expectedFailure
337323 def test_do_not_rehash_dict_keys (self ):
@@ -376,17 +362,14 @@ class TestSet(TestJointOps, unittest.TestCase):
376362 thetype = set
377363 basetype = set
378364
379- def test_contains (self ):
380- super ().test_contains ()
381-
382365 def test_init (self ):
383366 s = self .thetype ()
384367 s .__init__ (self .word )
385368 self .assertEqual (s , set (self .word ))
386369 s .__init__ (self .otherword )
387370 self .assertEqual (s , set (self .otherword ))
388- self .assertRaises (TypeError , s .__init__ , s , 2 );
389- self .assertRaises (TypeError , s .__init__ , 1 );
371+ self .assertRaises (TypeError , s .__init__ , s , 2 )
372+ self .assertRaises (TypeError , s .__init__ , 1 )
390373
391374 def test_constructor_identity (self ):
392375 s = self .thetype (range (3 ))
@@ -618,6 +601,7 @@ def test_weakref(self):
618601 p = weakref .proxy (s )
619602 self .assertEqual (str (p ), str (s ))
620603 s = None
604+ support .gc_collect () # For PyPy or other GCs.
621605 self .assertRaises (ReferenceError , str , p )
622606
623607 def test_rich_compare (self ):
@@ -668,17 +652,12 @@ class TestSetSubclass(TestSet):
668652 thetype = SetSubclass
669653 basetype = set
670654
671- def test_pickling (self ):
672- super ().test_pickling ()
673-
674- def test_cyclical_repr (self ):
675- super ().test_cyclical_repr ()
676-
677655class SetSubclassWithKeywordArgs (set ):
678656 def __init__ (self , iterable = [], newarg = None ):
679657 set .__init__ (self , iterable )
680658
681659class TestSetSubclassWithKeywordArgs (TestSet ):
660+
682661 def test_keywords_in_subclass (self ):
683662 'SF bug #1486663 -- this used to erroneously raise a TypeError'
684663 SetSubclassWithKeywordArgs (newarg = 1 )
@@ -692,15 +671,6 @@ def test_init(self):
692671 s .__init__ (self .otherword )
693672 self .assertEqual (s , set (self .word ))
694673
695- def test_singleton_empty_frozenset (self ):
696- f = frozenset ()
697- efs = [frozenset (), frozenset ([]), frozenset (()), frozenset ('' ),
698- frozenset (), frozenset ([]), frozenset (()), frozenset ('' ),
699- frozenset (range (0 )), frozenset (frozenset ()),
700- frozenset (f ), f ]
701- # All of the empty frozensets should have just one id()
702- self .assertEqual (len (set (map (id , efs ))), 1 )
703-
704674 def test_constructor_identity (self ):
705675 s = self .thetype (range (3 ))
706676 t = self .thetype (s )
@@ -774,9 +744,6 @@ class TestFrozenSetSubclass(TestFrozenSet):
774744 thetype = FrozenSetSubclass
775745 basetype = frozenset
776746
777- def test_pickling (self ):
778- super ().test_pickling ()
779-
780747 def test_constructor_identity (self ):
781748 s = self .thetype (range (3 ))
782749 t = self .thetype (s )
@@ -825,17 +792,6 @@ def check_repr_against_values(self):
825792 sorted_repr_values .sort ()
826793 self .assertEqual (result , sorted_repr_values )
827794
828- def test_print (self ):
829- try :
830- fo = open (os_helper .TESTFN , "w" )
831- fo .write (str (self .set ))
832- fo .close ()
833- fo = open (os_helper .TESTFN , "r" )
834- self .assertEqual (fo .read (), repr (self .set ))
835- finally :
836- fo .close ()
837- os_helper .unlink (os_helper .TESTFN )
838-
839795 def test_length (self ):
840796 self .assertEqual (len (self .set ), self .length )
841797
@@ -917,6 +873,12 @@ def test_pickling(self):
917873 self .assertEqual (self .set , copy ,
918874 "%s != %s" % (self .set , copy ))
919875
876+ def test_issue_37219 (self ):
877+ with self .assertRaises (TypeError ):
878+ set ().difference (123 )
879+ with self .assertRaises (TypeError ):
880+ set ().difference_update (123 )
881+
920882#------------------------------------------------------------------------------
921883
922884class TestBasicOpsEmpty (TestBasicOps , unittest .TestCase ):
@@ -1576,36 +1538,24 @@ class TestCopyingSingleton(TestCopying, unittest.TestCase):
15761538 def setUp (self ):
15771539 self .set = set (["hello" ])
15781540
1579- def test_deep_copy (self ):
1580- super ().test_deep_copy ()
1581-
15821541#------------------------------------------------------------------------------
15831542
15841543class TestCopyingTriple (TestCopying , unittest .TestCase ):
15851544 def setUp (self ):
15861545 self .set = set (["zero" , 0 , None ])
15871546
1588- def test_deep_copy (self ):
1589- super ().test_deep_copy ()
1590-
15911547#------------------------------------------------------------------------------
15921548
15931549class TestCopyingTuple (TestCopying , unittest .TestCase ):
15941550 def setUp (self ):
15951551 self .set = set ([(1 , 2 )])
15961552
1597- def test_deep_copy (self ):
1598- super ().test_deep_copy ()
1599-
16001553#------------------------------------------------------------------------------
16011554
16021555class TestCopyingNested (TestCopying , unittest .TestCase ):
16031556 def setUp (self ):
16041557 self .set = set ([((1 , 2 ), (3 , 4 ))])
16051558
1606- def test_deep_copy (self ):
1607- super ().test_deep_copy ()
1608-
16091559#==============================================================================
16101560
16111561class TestIdentities (unittest .TestCase ):
@@ -1828,6 +1778,192 @@ def __eq__(self, o):
18281778 s = {0 }
18291779 s .update (other )
18301780
1781+
1782+ class TestOperationsMutating :
1783+ """Regression test for bpo-46615"""
1784+
1785+ constructor1 = None
1786+ constructor2 = None
1787+
1788+ def make_sets_of_bad_objects (self ):
1789+ class Bad :
1790+ def __eq__ (self , other ):
1791+ if not enabled :
1792+ return False
1793+ if randrange (20 ) == 0 :
1794+ set1 .clear ()
1795+ if randrange (20 ) == 0 :
1796+ set2 .clear ()
1797+ return bool (randrange (2 ))
1798+ def __hash__ (self ):
1799+ return randrange (2 )
1800+ # Don't behave poorly during construction.
1801+ enabled = False
1802+ set1 = self .constructor1 (Bad () for _ in range (randrange (50 )))
1803+ set2 = self .constructor2 (Bad () for _ in range (randrange (50 )))
1804+ # Now start behaving poorly
1805+ enabled = True
1806+ return set1 , set2
1807+
1808+ def check_set_op_does_not_crash (self , function ):
1809+ for _ in range (100 ):
1810+ set1 , set2 = self .make_sets_of_bad_objects ()
1811+ try :
1812+ function (set1 , set2 )
1813+ except RuntimeError as e :
1814+ # Just make sure we don't crash here.
1815+ self .assertIn ("changed size during iteration" , str (e ))
1816+
1817+
1818+ class TestBinaryOpsMutating (TestOperationsMutating ):
1819+
1820+ def test_eq_with_mutation (self ):
1821+ self .check_set_op_does_not_crash (lambda a , b : a == b )
1822+
1823+ def test_ne_with_mutation (self ):
1824+ self .check_set_op_does_not_crash (lambda a , b : a != b )
1825+
1826+ def test_lt_with_mutation (self ):
1827+ self .check_set_op_does_not_crash (lambda a , b : a < b )
1828+
1829+ def test_le_with_mutation (self ):
1830+ self .check_set_op_does_not_crash (lambda a , b : a <= b )
1831+
1832+ def test_gt_with_mutation (self ):
1833+ self .check_set_op_does_not_crash (lambda a , b : a > b )
1834+
1835+ def test_ge_with_mutation (self ):
1836+ self .check_set_op_does_not_crash (lambda a , b : a >= b )
1837+
1838+ def test_and_with_mutation (self ):
1839+ self .check_set_op_does_not_crash (lambda a , b : a & b )
1840+
1841+ def test_or_with_mutation (self ):
1842+ self .check_set_op_does_not_crash (lambda a , b : a | b )
1843+
1844+ def test_sub_with_mutation (self ):
1845+ self .check_set_op_does_not_crash (lambda a , b : a - b )
1846+
1847+ def test_xor_with_mutation (self ):
1848+ self .check_set_op_does_not_crash (lambda a , b : a ^ b )
1849+
1850+ def test_iadd_with_mutation (self ):
1851+ def f (a , b ):
1852+ a &= b
1853+ self .check_set_op_does_not_crash (f )
1854+
1855+ def test_ior_with_mutation (self ):
1856+ def f (a , b ):
1857+ a |= b
1858+ self .check_set_op_does_not_crash (f )
1859+
1860+ def test_isub_with_mutation (self ):
1861+ def f (a , b ):
1862+ a -= b
1863+ self .check_set_op_does_not_crash (f )
1864+
1865+ def test_ixor_with_mutation (self ):
1866+ def f (a , b ):
1867+ a ^= b
1868+ self .check_set_op_does_not_crash (f )
1869+
1870+ def test_iteration_with_mutation (self ):
1871+ def f1 (a , b ):
1872+ for x in a :
1873+ pass
1874+ for y in b :
1875+ pass
1876+ def f2 (a , b ):
1877+ for y in b :
1878+ pass
1879+ for x in a :
1880+ pass
1881+ def f3 (a , b ):
1882+ for x , y in zip (a , b ):
1883+ pass
1884+ self .check_set_op_does_not_crash (f1 )
1885+ self .check_set_op_does_not_crash (f2 )
1886+ self .check_set_op_does_not_crash (f3 )
1887+
1888+
1889+ class TestBinaryOpsMutating_Set_Set (TestBinaryOpsMutating , unittest .TestCase ):
1890+ constructor1 = set
1891+ constructor2 = set
1892+
1893+ class TestBinaryOpsMutating_Subclass_Subclass (TestBinaryOpsMutating , unittest .TestCase ):
1894+ constructor1 = SetSubclass
1895+ constructor2 = SetSubclass
1896+
1897+ class TestBinaryOpsMutating_Set_Subclass (TestBinaryOpsMutating , unittest .TestCase ):
1898+ constructor1 = set
1899+ constructor2 = SetSubclass
1900+
1901+ class TestBinaryOpsMutating_Subclass_Set (TestBinaryOpsMutating , unittest .TestCase ):
1902+ constructor1 = SetSubclass
1903+ constructor2 = set
1904+
1905+
1906+ class TestMethodsMutating (TestOperationsMutating ):
1907+
1908+ def test_issubset_with_mutation (self ):
1909+ self .check_set_op_does_not_crash (set .issubset )
1910+
1911+ def test_issuperset_with_mutation (self ):
1912+ self .check_set_op_does_not_crash (set .issuperset )
1913+
1914+ def test_intersection_with_mutation (self ):
1915+ self .check_set_op_does_not_crash (set .intersection )
1916+
1917+ def test_union_with_mutation (self ):
1918+ self .check_set_op_does_not_crash (set .union )
1919+
1920+ def test_difference_with_mutation (self ):
1921+ self .check_set_op_does_not_crash (set .difference )
1922+
1923+ def test_symmetric_difference_with_mutation (self ):
1924+ self .check_set_op_does_not_crash (set .symmetric_difference )
1925+
1926+ def test_isdisjoint_with_mutation (self ):
1927+ self .check_set_op_does_not_crash (set .isdisjoint )
1928+
1929+ def test_difference_update_with_mutation (self ):
1930+ self .check_set_op_does_not_crash (set .difference_update )
1931+
1932+ def test_intersection_update_with_mutation (self ):
1933+ self .check_set_op_does_not_crash (set .intersection_update )
1934+
1935+ def test_symmetric_difference_update_with_mutation (self ):
1936+ self .check_set_op_does_not_crash (set .symmetric_difference_update )
1937+
1938+ def test_update_with_mutation (self ):
1939+ self .check_set_op_does_not_crash (set .update )
1940+
1941+
1942+ class TestMethodsMutating_Set_Set (TestMethodsMutating , unittest .TestCase ):
1943+ constructor1 = set
1944+ constructor2 = set
1945+
1946+ class TestMethodsMutating_Subclass_Subclass (TestMethodsMutating , unittest .TestCase ):
1947+ constructor1 = SetSubclass
1948+ constructor2 = SetSubclass
1949+
1950+ class TestMethodsMutating_Set_Subclass (TestMethodsMutating , unittest .TestCase ):
1951+ constructor1 = set
1952+ constructor2 = SetSubclass
1953+
1954+ class TestMethodsMutating_Subclass_Set (TestMethodsMutating , unittest .TestCase ):
1955+ constructor1 = SetSubclass
1956+ constructor2 = set
1957+
1958+ class TestMethodsMutating_Set_Dict (TestMethodsMutating , unittest .TestCase ):
1959+ constructor1 = set
1960+ constructor2 = dict .fromkeys
1961+
1962+ class TestMethodsMutating_Set_List (TestMethodsMutating , unittest .TestCase ):
1963+ constructor1 = set
1964+ constructor2 = list
1965+
1966+
18311967# Application tests (based on David Eppstein's graph recipes ====================================
18321968
18331969def powerset (U ):
0 commit comments