22
33from test .support import import_helper
44
5- # Skip this test if the _testcapi module isn 't available.
5+ # Skip this test if the _testcapi or _testinternalcapi modules aren 't available.
66_testcapi = import_helper .import_module ('_testcapi' )
7+ _testinternalcapi = import_helper .import_module ('_testinternalcapi' )
78
89class set_subclass (set ):
910 pass
@@ -12,13 +13,15 @@ class frozenset_subclass(frozenset):
1213 pass
1314
1415
15- class TestSetCAPI ( unittest . TestCase ) :
16+ class BaseSetTests :
1617 def assertImmutable (self , action , * args ):
1718 self .assertRaises (SystemError , action , frozenset (), * args )
1819 self .assertRaises (SystemError , action , frozenset ({1 }), * args )
1920 self .assertRaises (SystemError , action , frozenset_subclass (), * args )
2021 self .assertRaises (SystemError , action , frozenset_subclass ({1 }), * args )
2122
23+
24+ class TestSetCAPI (BaseSetTests , unittest .TestCase ):
2225 def test_set_check (self ):
2326 check = _testcapi .set_check
2427 self .assertTrue (check (set ()))
@@ -213,3 +216,50 @@ def test_clear(self):
213216 clear (object ())
214217 self .assertImmutable (clear )
215218 # CRASHES: clear(NULL)
219+
220+
221+ class TestInternalCAPI (BaseSetTests , unittest .TestCase ):
222+ def test_set_update (self ):
223+ update = _testinternalcapi .set_update
224+ for cls in (set , set_subclass ):
225+ for it in ('ab' , ('a' , 'b' ), ['a' , 'b' ],
226+ set ('ab' ), set_subclass ('ab' ),
227+ frozenset ('ab' ), frozenset_subclass ('ab' )):
228+ with self .subTest (cls = cls , it = it ):
229+ instance = cls ()
230+ self .assertEqual (update (instance , it ), 0 )
231+ self .assertEqual (instance , {'a' , 'b' })
232+ instance = cls (it )
233+ self .assertEqual (update (instance , it ), 0 )
234+ self .assertEqual (instance , {'a' , 'b' })
235+ with self .assertRaisesRegex (TypeError , 'object is not iterable' ):
236+ update (cls (), 1 )
237+ with self .assertRaisesRegex (TypeError , "unhashable type: 'dict'" ):
238+ update (cls (), [{}])
239+ with self .assertRaises (SystemError ):
240+ update (object (), 'ab' )
241+ self .assertImmutable (update , 'ab' )
242+ # CRASHES: update(NULL, object())
243+ # CRASHES: update(instance, NULL)
244+ # CRASHES: update(NULL, NULL)
245+
246+ def test_set_next_entry (self ):
247+ set_next = _testinternalcapi .set_next_entry
248+ for cls in (set , set_subclass , frozenset , frozenset_subclass ):
249+ with self .subTest (cls = cls ):
250+ instance = cls ('abc' )
251+ pos = 0
252+ items = []
253+ while True :
254+ res = set_next (instance , pos )
255+ if res is None :
256+ break
257+ rc , pos , hash_ , item = res
258+ items .append (item )
259+ self .assertEqual (rc , 1 )
260+ self .assertIn (item , instance )
261+ self .assertEqual (hash (item ), hash_ )
262+ self .assertEqual (items , list (instance ))
263+ with self .assertRaises (SystemError ):
264+ set_next (object (), 0 )
265+ # CRASHES: set_next(NULL, 0)
0 commit comments