11import unittest
22from collections import OrderedDict
3+ from test import support
34from test .support import import_helper
45
56_testcapi = import_helper .import_module ('_testcapi' )
@@ -109,8 +110,18 @@ def test_object_hasattr(self):
109110 self .assertFalse (xhasattr (obj , 'b' ))
110111 self .assertTrue (xhasattr (obj , '\U0001f40d ' ))
111112
112- self .assertFalse (xhasattr (obj , 'evil' ))
113- self .assertFalse (xhasattr (obj , 1 ))
113+ with support .catch_unraisable_exception () as cm :
114+ self .assertFalse (xhasattr (obj , 'evil' ))
115+ self .assertEqual (cm .unraisable .exc_type , RuntimeError )
116+ self .assertEqual (str (cm .unraisable .exc_value ),
117+ 'do not get evil' )
118+
119+ with support .catch_unraisable_exception () as cm :
120+ self .assertFalse (xhasattr (obj , 1 ))
121+ self .assertEqual (cm .unraisable .exc_type , TypeError )
122+ self .assertEqual (str (cm .unraisable .exc_value ),
123+ "attribute name must be string, not 'int'" )
124+
114125 # CRASHES xhasattr(obj, NULL)
115126 # CRASHES xhasattr(NULL, 'a')
116127
@@ -123,8 +134,18 @@ def test_object_hasattrstring(self):
123134 self .assertFalse (hasattrstring (obj , b'b' ))
124135 self .assertTrue (hasattrstring (obj , '\U0001f40d ' .encode ()))
125136
126- self .assertFalse (hasattrstring (obj , b'evil' ))
127- self .assertFalse (hasattrstring (obj , b'\xff ' ))
137+ with support .catch_unraisable_exception () as cm :
138+ self .assertFalse (hasattrstring (obj , b'evil' ))
139+ self .assertEqual (cm .unraisable .exc_type , RuntimeError )
140+ self .assertEqual (str (cm .unraisable .exc_value ),
141+ 'do not get evil' )
142+
143+ with support .catch_unraisable_exception () as cm :
144+ self .assertFalse (hasattrstring (obj , b'\xff ' ))
145+ self .assertEqual (cm .unraisable .exc_type , UnicodeDecodeError )
146+ self .assertRegex (str (cm .unraisable .exc_value ),
147+ "'utf-8' codec can't decode" )
148+
128149 # CRASHES hasattrstring(obj, NULL)
129150 # CRASHES hasattrstring(NULL, b'a')
130151
@@ -342,12 +363,41 @@ def test_mapping_haskey(self):
342363
343364 self .assertTrue (haskey (['a' , 'b' , 'c' ], 1 ))
344365
345- self .assertFalse (haskey (42 , 'a' ))
346- self .assertFalse (haskey ({}, [])) # unhashable
347- self .assertFalse (haskey ({}, NULL ))
348- self .assertFalse (haskey ([], 1 ))
349- self .assertFalse (haskey ([], 'a' ))
350- self .assertFalse (haskey (NULL , 'a' ))
366+ with support .catch_unraisable_exception () as cm :
367+ self .assertFalse (haskey (42 , 'a' ))
368+ self .assertEqual (cm .unraisable .exc_type , TypeError )
369+ self .assertEqual (str (cm .unraisable .exc_value ),
370+ "'int' object is not subscriptable" )
371+
372+ with support .catch_unraisable_exception () as cm :
373+ self .assertFalse (haskey ({}, []))
374+ self .assertEqual (cm .unraisable .exc_type , TypeError )
375+ self .assertEqual (str (cm .unraisable .exc_value ),
376+ "unhashable type: 'list'" )
377+
378+ with support .catch_unraisable_exception () as cm :
379+ self .assertFalse (haskey ([], 1 ))
380+ self .assertEqual (cm .unraisable .exc_type , IndexError )
381+ self .assertEqual (str (cm .unraisable .exc_value ),
382+ 'list index out of range' )
383+
384+ with support .catch_unraisable_exception () as cm :
385+ self .assertFalse (haskey ([], 'a' ))
386+ self .assertEqual (cm .unraisable .exc_type , TypeError )
387+ self .assertEqual (str (cm .unraisable .exc_value ),
388+ 'list indices must be integers or slices, not str' )
389+
390+ with support .catch_unraisable_exception () as cm :
391+ self .assertFalse (haskey ({}, NULL ))
392+ self .assertEqual (cm .unraisable .exc_type , SystemError )
393+ self .assertEqual (str (cm .unraisable .exc_value ),
394+ 'null argument to internal routine' )
395+
396+ with support .catch_unraisable_exception () as cm :
397+ self .assertFalse (haskey (NULL , 'a' ))
398+ self .assertEqual (cm .unraisable .exc_type , SystemError )
399+ self .assertEqual (str (cm .unraisable .exc_value ),
400+ 'null argument to internal routine' )
351401
352402 def test_mapping_haskeystring (self ):
353403 haskeystring = _testcapi .mapping_haskeystring
@@ -360,11 +410,35 @@ def test_mapping_haskeystring(self):
360410 self .assertTrue (haskeystring (dct2 , b'a' ))
361411 self .assertFalse (haskeystring (dct2 , b'b' ))
362412
363- self .assertFalse (haskeystring (42 , b'a' ))
364- self .assertFalse (haskeystring ({}, b'\xff ' ))
365- self .assertFalse (haskeystring ({}, NULL ))
366- self .assertFalse (haskeystring ([], b'a' ))
367- self .assertFalse (haskeystring (NULL , b'a' ))
413+ with support .catch_unraisable_exception () as cm :
414+ self .assertFalse (haskeystring (42 , b'a' ))
415+ self .assertEqual (cm .unraisable .exc_type , TypeError )
416+ self .assertEqual (str (cm .unraisable .exc_value ),
417+ "'int' object is not subscriptable" )
418+
419+ with support .catch_unraisable_exception () as cm :
420+ self .assertFalse (haskeystring ({}, b'\xff ' ))
421+ self .assertEqual (cm .unraisable .exc_type , UnicodeDecodeError )
422+ self .assertRegex (str (cm .unraisable .exc_value ),
423+ "'utf-8' codec can't decode" )
424+
425+ with support .catch_unraisable_exception () as cm :
426+ self .assertFalse (haskeystring ({}, NULL ))
427+ self .assertEqual (cm .unraisable .exc_type , SystemError )
428+ self .assertEqual (str (cm .unraisable .exc_value ),
429+ "null argument to internal routine" )
430+
431+ with support .catch_unraisable_exception () as cm :
432+ self .assertFalse (haskeystring ([], b'a' ))
433+ self .assertEqual (cm .unraisable .exc_type , TypeError )
434+ self .assertEqual (str (cm .unraisable .exc_value ),
435+ 'list indices must be integers or slices, not str' )
436+
437+ with support .catch_unraisable_exception () as cm :
438+ self .assertFalse (haskeystring (NULL , b'a' ))
439+ self .assertEqual (cm .unraisable .exc_type , SystemError )
440+ self .assertEqual (str (cm .unraisable .exc_value ),
441+ "null argument to internal routine" )
368442
369443 def test_mapping_haskeywitherror (self ):
370444 haskey = _testcapi .mapping_haskeywitherror
0 commit comments