Skip to content

Commit c3e2431

Browse files
committed
chore: resolve review comment
1 parent c0a20b2 commit c3e2431

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

Lib/test/test_ordered_dict.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ class MyOD(self.OrderedDict):
994994
def __getitem__(self, key):
995995
self.call_count += 1
996996
if self.call_count == 1:
997-
del self[next(iter(self))]
997+
del self[3]
998998
return super().__getitem__(key)
999999

10001000
od = MyOD([(1, 'one'), (2, 'two'), (3, 'three')])
@@ -1006,7 +1006,7 @@ class MyOD(self.OrderedDict):
10061006
def __getitem__(self, key):
10071007
self.call_count += 1
10081008
if self.call_count == 1:
1009-
self.pop(next(iter(self)))
1009+
self.pop(3)
10101010
return super().__getitem__(key)
10111011

10121012
od = MyOD([(1, 'one'), (2, 'two'), (3, 'three')])
@@ -1015,33 +1015,41 @@ def __getitem__(self, key):
10151015
def test_copy_concurrent_deletion_and_set_in__getitem__(self):
10161016
class MyOD(self.OrderedDict):
10171017
call_count = 0
1018+
instance_count = 0
1019+
1020+
def __init__(self, *args, **kwargs):
1021+
super().__init__(*args, **kwargs)
1022+
MyOD.instance_count += 1
1023+
10181024
def __getitem__(self, key):
10191025
self.call_count += 1
10201026
if self.call_count == 1:
1021-
del self[next(iter(self))]
1027+
del self[4]
10221028
elif self.call_count == 2:
10231029
self['new_key'] = 'new_value'
10241030
return super().__getitem__(key)
10251031

10261032
od = MyOD([(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')])
10271033
self.assertRaises(RuntimeError, od.copy)
1034+
self.assertEqual(MyOD.instance_count, 2)
10281035

10291036
def test_copy_concurrent_mutation_in__setitem__(self):
10301037
od = None
10311038
class MyOD(self.OrderedDict):
1032-
_instance_count = 0
1039+
instance_count = 0
10331040

10341041
def __init__(self, *args, **kwargs):
10351042
super().__init__(*args, **kwargs)
1036-
MyOD._instance_count += 1
1043+
MyOD.instance_count += 1
10371044

10381045
def __setitem__(self, key, value):
1039-
if self._instance_count == 2 and len(od) > 1:
1046+
if self.instance_count == 2 and len(od) > 1:
10401047
del od[next(iter(od))]
10411048
return super().__setitem__(key, value)
10421049

10431050
od = MyOD([(1, 'one'), (2, 'two'), (3, 'three')])
10441051
self.assertRaises(RuntimeError, od.copy)
1052+
self.assertEqual(MyOD.instance_count, 2)
10451053

10461054

10471055
class PurePythonOrderedDictSubclassTests(PurePythonOrderedDictTests):

Objects/odictobject.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,12 +1274,15 @@ OrderedDict_copy_impl(PyObject *od)
12741274
PyObject *value = PyObject_GetItem(od, key);
12751275
if (value == NULL) {
12761276
Py_DECREF(key);
1277-
if (self->od_state != state) {
1278-
goto invalid_state;
1279-
}
12801277
goto fail;
12811278
}
12821279

1280+
if (self->od_state != state) {
1281+
Py_DECREF(key);
1282+
Py_DECREF(value);
1283+
goto invalid_state; // 成功获取值但状态改变
1284+
}
1285+
12831286
int rc = PyObject_SetItem(od_copy, key, value);
12841287
Py_DECREF(key);
12851288
Py_DECREF(value);

0 commit comments

Comments
 (0)