@@ -477,23 +477,46 @@ def test_dict_pop(self):
477
477
def test_dict_popstring (self ):
478
478
# Test PyDict_PopString()
479
479
dict_popstring = _testcapi .dict_popstring
480
+ dict_popstring_null = _testcapi .dict_popstring_null
480
481
481
- # key present
482
+ # key present, get removed value
482
483
mydict = {"key" : "value" , "key2" : "value2" }
483
484
self .assertEqual (dict_popstring (mydict , "key" ), (1 , "value" ))
484
485
self .assertEqual (mydict , {"key2" : "value2" })
485
486
self .assertEqual (dict_popstring (mydict , "key2" ), (1 , "value2" ))
486
487
self .assertEqual (mydict , {})
487
488
489
+ # key present, ignore removed value
490
+ mydict = {"key" : "value" , "key2" : "value2" }
491
+ self .assertEqual (dict_popstring_null (mydict , "key" ), 1 )
492
+ self .assertEqual (mydict , {"key2" : "value2" })
493
+ self .assertEqual (dict_popstring_null (mydict , "key2" ), 1 )
494
+ self .assertEqual (mydict , {})
495
+
488
496
# key missing; empty dict has a fast path
489
497
self .assertEqual (dict_popstring ({}, "key" ), (0 , NULL ))
498
+ self .assertEqual (dict_popstring_null ({}, "key" ), 0 )
490
499
self .assertEqual (dict_popstring ({"a" : 1 }, "key" ), (0 , NULL ))
500
+ self .assertEqual (dict_popstring_null ({"a" : 1 }, "key" ), 0 )
501
+
502
+ # non-ASCII key
503
+ non_ascii = '\U0001f40d '
504
+ dct = {'\U0001f40d ' : 123 }
505
+ self .assertEqual (dict_popstring (dct , '\U0001f40d ' .encode ()), (1 , 123 ))
506
+ dct = {'\U0001f40d ' : 123 }
507
+ self .assertEqual (dict_popstring_null (dct , '\U0001f40d ' .encode ()), 1 )
491
508
492
509
# dict error
493
510
not_dict = UserDict ({1 : 2 })
494
511
self .assertRaises (SystemError , dict_popstring , not_dict , "key" )
512
+ self .assertRaises (SystemError , dict_popstring_null , not_dict , "key" )
513
+
514
+ # key error
515
+ self .assertRaises (UnicodeDecodeError , dict_popstring , {1 : 2 }, INVALID_UTF8 )
516
+ self .assertRaises (UnicodeDecodeError , dict_popstring_null , {1 : 2 }, INVALID_UTF8 )
495
517
496
518
# CRASHES dict_popstring(NULL, "key")
519
+ # CRASHES dict_popstring({}, NULL)
497
520
# CRASHES dict_popstring({"a": 1}, NULL)
498
521
499
522
0 commit comments