@@ -26,6 +26,19 @@ def gen():
2626 yield 'c'
2727
2828
29+ class FrozenDictSubclass (frozendict ):
30+ pass
31+
32+
33+ DICT_TYPES = (dict , DictSubclass , OrderedDict )
34+ FROZENDICT_TYPES = (frozendict , FrozenDictSubclass )
35+ ANYDICT_TYPES = DICT_TYPES + FROZENDICT_TYPES
36+ MAPPING_TYPES = (UserDict ,)
37+ NOT_FROZENDICT_TYPES = DICT_TYPES + MAPPING_TYPES
38+ NOT_ANYDICT_TYPES = MAPPING_TYPES
39+ OTHER_TYPES = (lambda : [1 ], lambda : 42 , object ) # (list, int, object)
40+
41+
2942class CAPITest (unittest .TestCase ):
3043
3144 def test_dict_check (self ):
@@ -545,6 +558,61 @@ def test_dict_popstring(self):
545558 # CRASHES dict_popstring({}, NULL)
546559 # CRASHES dict_popstring({"a": 1}, NULL)
547560
561+ def test_frozendict_check (self ):
562+ # Test PyFrozenDict_Check()
563+ check = _testcapi .frozendict_check
564+ for dict_type in FROZENDICT_TYPES :
565+ self .assertTrue (check (dict_type (x = 1 )))
566+ for dict_type in NOT_FROZENDICT_TYPES + OTHER_TYPES :
567+ self .assertFalse (check (dict_type ()))
568+ # CRASHES check(NULL)
569+
570+ def test_frozendict_checkexact (self ):
571+ # Test PyFrozenDict_CheckExact()
572+ check = _testcapi .frozendict_checkexact
573+ for dict_type in FROZENDICT_TYPES :
574+ self .assertEqual (check (dict_type (x = 1 )), dict_type == frozendict )
575+ for dict_type in NOT_FROZENDICT_TYPES + OTHER_TYPES :
576+ self .assertFalse (check (dict_type ()))
577+ # CRASHES check(NULL)
578+
579+ def test_anydict_check (self ):
580+ # Test PyAnyDict_Check()
581+ check = _testcapi .anydict_check
582+ for dict_type in ANYDICT_TYPES :
583+ self .assertTrue (check (dict_type ({1 : 2 })))
584+ for test_type in NOT_ANYDICT_TYPES + OTHER_TYPES :
585+ self .assertFalse (check (test_type ()))
586+ # CRASHES check(NULL)
587+
588+ def test_anydict_checkexact (self ):
589+ # Test PyAnyDict_CheckExact()
590+ check = _testcapi .anydict_checkexact
591+ for dict_type in ANYDICT_TYPES :
592+ self .assertEqual (check (dict_type (x = 1 )),
593+ dict_type in (dict , frozendict ))
594+ for test_type in NOT_ANYDICT_TYPES + OTHER_TYPES :
595+ self .assertFalse (check (test_type ()))
596+ # CRASHES check(NULL)
597+
598+ def test_frozendict_new (self ):
599+ # Test PyFrozenDict_New()
600+ frozendict_new = _testcapi .frozendict_new
601+
602+ for dict_type in ANYDICT_TYPES :
603+ dct = frozendict_new (dict_type ({'x' : 1 }))
604+ self .assertEqual (dct , frozendict (x = 1 ))
605+ self .assertIs (type (dct ), frozendict )
606+
607+ dct = frozendict_new ([('x' , 1 ), ('y' , 2 )])
608+ self .assertEqual (dct , frozendict (x = 1 , y = 2 ))
609+ self .assertIs (type (dct ), frozendict )
610+
611+ # PyFrozenDict_New(NULL) creates an empty dictionary
612+ dct = frozendict_new (NULL )
613+ self .assertEqual (dct , frozendict ())
614+ self .assertIs (type (dct ), frozendict )
615+
548616
549617if __name__ == "__main__" :
550618 unittest .main ()
0 commit comments