This repository was archived by the owner on Feb 13, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +12
-4
lines changed Expand file tree Collapse file tree 3 files changed +12
-4
lines changed Original file line number Diff line number Diff line change @@ -1746,10 +1746,6 @@ def test_iterators(self):
1746
1746
self .assertEqual (list (reversed (od .items ())), list (reversed (pairs )))
1747
1747
1748
1748
def test_detect_deletion_during_iteration (self ):
1749
- # XXX This test should also work under cOrderedDict.
1750
- if self .module is c_coll :
1751
- raise unittest .SkipTest ("only valid for pure Python OrderedDict" )
1752
-
1753
1749
OrderedDict = self .module .OrderedDict
1754
1750
od = OrderedDict .fromkeys ('abc' )
1755
1751
it = iter (od )
Original file line number Diff line number Diff line change @@ -19,6 +19,8 @@ Library
19
19
20
20
- Issue #24348: Drop superfluous incref/decref.
21
21
22
+ - Issue #24359: Check for changed OrderedDict size during iteration.
23
+
22
24
23
25
What's New in Python 3.5.0 beta 2?
24
26
==================================
Original file line number Diff line number Diff line change @@ -1796,6 +1796,7 @@ typedef struct {
1796
1796
PyObject_HEAD
1797
1797
int kind ;
1798
1798
PyODictObject * di_odict ;
1799
+ Py_ssize_t di_size ;
1799
1800
PyObject * di_current ;
1800
1801
PyObject * di_result ; /* reusable result tuple for iteritems */
1801
1802
} odictiterobject ;
@@ -1835,6 +1836,14 @@ odictiter_nextkey(odictiterobject *di)
1835
1836
if (di -> di_current == NULL )
1836
1837
goto done ; /* We're already done. */
1837
1838
1839
+ /* Check for unsupported changes. */
1840
+ if (di -> di_size != PyODict_SIZE (di -> di_odict )) {
1841
+ PyErr_SetString (PyExc_RuntimeError ,
1842
+ "OrderedDict changed size during iteration" );
1843
+ di -> di_size = -1 ; /* Make this state sticky */
1844
+ return NULL ;
1845
+ }
1846
+
1838
1847
/* Get the key. */
1839
1848
node = _odict_find_node (di -> di_odict , di -> di_current );
1840
1849
if (node == NULL ) {
@@ -2033,6 +2042,7 @@ odictiter_new(PyODictObject *od, int kind)
2033
2042
node = reversed ? _odict_LAST (od ) : _odict_FIRST (od );
2034
2043
di -> di_current = node ? _odictnode_KEY (node ) : NULL ;
2035
2044
Py_XINCREF (di -> di_current );
2045
+ di -> di_size = PyODict_SIZE (od );
2036
2046
di -> di_odict = od ;
2037
2047
Py_INCREF (od );
2038
2048
You can’t perform that action at this time.
0 commit comments