@@ -545,6 +545,63 @@ def test_dict_popstring(self):
545545 # CRASHES dict_popstring({}, NULL)
546546 # CRASHES dict_popstring({"a": 1}, NULL)
547547
548+ def test_dict_fromkeysandvalues (self ):
549+ # Test PyDict_FromKeysAndValues()
550+ dict_fromkeysandvalues = _testcapi .dict_fromkeysandvalues
551+
552+ d = dict_fromkeysandvalues ((), ())
553+ self .assertEqual (d , {})
554+
555+ d = dict_fromkeysandvalues (tuple (range (1 , 4 )), tuple ('abc' ))
556+ self .assertEqual (d , {1 : 'a' , 2 : 'b' , 3 : 'c' })
557+
558+ # test unicode keys
559+ d = dict_fromkeysandvalues (tuple ('abc' ), tuple (range (1 , 4 )))
560+ self .assertEqual (d , {'a' : 1 , 'b' : 2 , 'c' : 3 })
561+
562+ # test "large" dict (1024 items)
563+ d = dict_fromkeysandvalues (tuple (range (1024 )),
564+ tuple (map (str , range (1024 ))))
565+ self .assertEqual (d , {i : str (i ) for i in range (1024 )})
566+
567+ # Test PyDict_FromItems(NULL, NULL, 0)
568+ d = dict_fromkeysandvalues ()
569+ self .assertEqual (d , {})
570+
571+ errmsg = "length must be greater than or equal to 0"
572+ with self .assertRaisesRegex (ValueError , errmsg ):
573+ dict_fromkeysandvalues (tuple (range (1 , 4 )), tuple ('abc' ), - 1 )
574+
575+ def test_dict_fromitems (self ):
576+ # Test PyDict_FromItems()
577+ dict_fromitems = _testcapi .dict_fromitems
578+
579+ d = dict_fromitems (())
580+ self .assertEqual (d , {})
581+
582+ d = dict_fromitems ((1 , 'a' , 2 , 'b' , 3 , 'c' ))
583+ self .assertEqual (d , {1 : 'a' , 2 : 'b' , 3 : 'c' })
584+
585+ # test unicode keys
586+ d = dict_fromitems (('a' , 1 , 'b' , 2 , 'c' , 3 ))
587+ self .assertEqual (d , {'a' : 1 , 'b' : 2 , 'c' : 3 })
588+
589+ # test "large" dict (1024 items)
590+ items = []
591+ for key , value in zip (range (1024 ), map (str , range (1024 ))):
592+ items .extend ((key , value ))
593+ d = dict_fromitems (tuple (items ))
594+ self .assertEqual (d , {i : str (i ) for i in range (1024 )})
595+
596+ # Test PyDict_FromItems(NULL, 0)
597+ d = dict_fromitems ()
598+ self .assertEqual (d , {})
599+
600+ # test invalid arguments
601+ errmsg = "length must be greater than or equal to 0"
602+ with self .assertRaisesRegex (ValueError , errmsg ):
603+ dict_fromitems (('x' , 1 ), - 1 )
604+
548605
549606if __name__ == "__main__" :
550607 unittest .main ()
0 commit comments