8
8
import unittest
9
9
import weakref
10
10
from test import support
11
+ from test .support import import_helper
11
12
12
13
13
14
class DictTest (unittest .TestCase ):
@@ -896,6 +897,14 @@ def _tracked(self, t):
896
897
gc .collect ()
897
898
self .assertTrue (gc .is_tracked (t ), t )
898
899
900
+ def test_string_keys_can_track_values (self ):
901
+ # Test that this doesn't leak.
902
+ for i in range (10 ):
903
+ d = {}
904
+ for j in range (10 ):
905
+ d [str (j )] = j
906
+ d ["foo" ] = d
907
+
899
908
@support .cpython_only
900
909
def test_track_literals (self ):
901
910
# Test GC-optimization of dict literals
@@ -999,8 +1008,8 @@ class C:
999
1008
1000
1009
@support .cpython_only
1001
1010
def test_splittable_setdefault (self ):
1002
- """split table must be combined when setdefault()
1003
- breaks insertion order """
1011
+ """split table must keep correct insertion
1012
+ order when attributes are adding using setdefault() """
1004
1013
a , b = self .make_shared_key_dict (2 )
1005
1014
1006
1015
a ['a' ] = 1
@@ -1010,7 +1019,6 @@ def test_splittable_setdefault(self):
1010
1019
size_b = sys .getsizeof (b )
1011
1020
b ['a' ] = 1
1012
1021
1013
- self .assertGreater (size_b , size_a )
1014
1022
self .assertEqual (list (a ), ['x' , 'y' , 'z' , 'a' , 'b' ])
1015
1023
self .assertEqual (list (b ), ['x' , 'y' , 'z' , 'b' , 'a' ])
1016
1024
@@ -1025,7 +1033,6 @@ def test_splittable_del(self):
1025
1033
with self .assertRaises (KeyError ):
1026
1034
del a ['y' ]
1027
1035
1028
- self .assertGreater (sys .getsizeof (a ), orig_size )
1029
1036
self .assertEqual (list (a ), ['x' , 'z' ])
1030
1037
self .assertEqual (list (b ), ['x' , 'y' , 'z' ])
1031
1038
@@ -1036,16 +1043,12 @@ def test_splittable_del(self):
1036
1043
1037
1044
@support .cpython_only
1038
1045
def test_splittable_pop (self ):
1039
- """split table must be combined when d.pop(k)"""
1040
1046
a , b = self .make_shared_key_dict (2 )
1041
1047
1042
- orig_size = sys .getsizeof (a )
1043
-
1044
- a .pop ('y' ) # split table is combined
1048
+ a .pop ('y' )
1045
1049
with self .assertRaises (KeyError ):
1046
1050
a .pop ('y' )
1047
1051
1048
- self .assertGreater (sys .getsizeof (a ), orig_size )
1049
1052
self .assertEqual (list (a ), ['x' , 'z' ])
1050
1053
self .assertEqual (list (b ), ['x' , 'y' , 'z' ])
1051
1054
@@ -1080,34 +1083,21 @@ def test_splittable_popitem(self):
1080
1083
self .assertEqual (list (b ), ['x' , 'y' , 'z' ])
1081
1084
1082
1085
@support .cpython_only
1083
- def test_splittable_setattr_after_pop (self ):
1084
- """setattr() must not convert combined table into split table."""
1085
- # Issue 28147
1086
- import _testcapi
1087
-
1086
+ def test_splittable_update (self ):
1087
+ """dict.update(other) must preserve order in other."""
1088
1088
class C :
1089
- pass
1090
- a = C ()
1091
-
1092
- a .a = 1
1093
- self .assertTrue (_testcapi .dict_hassplittable (a .__dict__ ))
1094
-
1095
- # dict.pop() convert it to combined table
1096
- a .__dict__ .pop ('a' )
1097
- self .assertFalse (_testcapi .dict_hassplittable (a .__dict__ ))
1098
-
1099
- # But C should not convert a.__dict__ to split table again.
1100
- a .a = 1
1101
- self .assertFalse (_testcapi .dict_hassplittable (a .__dict__ ))
1089
+ def __init__ (self , order ):
1090
+ if order :
1091
+ self .a , self .b , self .c = 1 , 2 , 3
1092
+ else :
1093
+ self .c , self .b , self .a = 1 , 2 , 3
1094
+ o = C (True )
1095
+ o = C (False ) # o.__dict__ has reversed order.
1096
+ self .assertEqual (list (o .__dict__ ), ["c" , "b" , "a" ])
1102
1097
1103
- # Same for popitem()
1104
- a = C ()
1105
- a .a = 2
1106
- self .assertTrue (_testcapi .dict_hassplittable (a .__dict__ ))
1107
- a .__dict__ .popitem ()
1108
- self .assertFalse (_testcapi .dict_hassplittable (a .__dict__ ))
1109
- a .a = 3
1110
- self .assertFalse (_testcapi .dict_hassplittable (a .__dict__ ))
1098
+ d = {}
1099
+ d .update (o .__dict__ )
1100
+ self .assertEqual (list (d ), ["c" , "b" , "a" ])
1111
1101
1112
1102
def test_iterator_pickling (self ):
1113
1103
for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
@@ -1586,7 +1576,8 @@ class CAPITest(unittest.TestCase):
1586
1576
# Test _PyDict_GetItem_KnownHash()
1587
1577
@support .cpython_only
1588
1578
def test_getitem_knownhash (self ):
1589
- from _testcapi import dict_getitem_knownhash
1579
+ _testcapi = import_helper .import_module ('_testcapi' )
1580
+ dict_getitem_knownhash = _testcapi .dict_getitem_knownhash
1590
1581
1591
1582
d = {'x' : 1 , 'y' : 2 , 'z' : 3 }
1592
1583
self .assertEqual (dict_getitem_knownhash (d , 'x' , hash ('x' )), 1 )
0 commit comments