Skip to content

Commit b234688

Browse files
committed
added changed size while iterating tests
1 parent 39e97ce commit b234688

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_dict.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,4 +853,49 @@ def __eq__(self, o):
853853
raised = True
854854
assert raised
855855

856-
assert 'kw' not in d
856+
assert 'kw' not in d
857+
858+
def test_iter_changed_size():
859+
def just_iterate(it):
860+
for i in it:
861+
pass
862+
863+
def iterate_and_update(it):
864+
for i in it:
865+
d.update({3:3})
866+
867+
# dict
868+
d = {1:1, 2:2}
869+
it = iter(d)
870+
del d[1]
871+
assert_raises(RuntimeError, just_iterate, it)
872+
873+
d = {1:1, 2:2}
874+
assert_raises(RuntimeError, iterate_and_update, d)
875+
876+
# keys
877+
d = {1:1, 2:2}
878+
it = iter(d.keys())
879+
del d[1]
880+
assert_raises(RuntimeError, just_iterate, it)
881+
882+
d = {1:1}
883+
assert_raises(RuntimeError, iterate_and_update, d.keys())
884+
885+
# values
886+
d = {1:1, 2:2}
887+
it = iter(d.values())
888+
del d[1]
889+
assert_raises(RuntimeError, just_iterate, it)
890+
891+
d = {1:1}
892+
assert_raises(RuntimeError, iterate_and_update, d.values())
893+
894+
# items
895+
d = {1:1, 2:2}
896+
it = iter(d.items())
897+
del d[1]
898+
assert_raises(RuntimeError, just_iterate, it)
899+
900+
d = {1:1}
901+
assert_raises(RuntimeError, iterate_and_update, d.items())

graalpython/com.oracle.graal.python.test/src/tests/test_mappingproxy.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -129,3 +129,16 @@ def test_iter():
129129
mp_keys = set([k for k in mp])
130130
assert d.keys() == mp_keys
131131

132+
def test_iter_changed_size():
133+
class A:
134+
pass
135+
136+
def foo():
137+
pass
138+
139+
try:
140+
for i in A.__dict__:
141+
setattr(A, 'foo', foo)
142+
except RuntimeError:
143+
raised = True
144+
assert raised

graalpython/com.oracle.graal.python.test/src/tests/test_set.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,4 +460,21 @@ def __len__(self):
460460
assert {10, 11}.isdisjoint(NonSetWithIter())
461461
assert {10, 11, 12, 13, 14}.isdisjoint(NonSetWithIter())
462462
assert not {1, 2}.isdisjoint(NonSetWithIter())
463-
assert not {1, 2, 3, 4, 5}.isdisjoint(NonSetWithIter())
463+
assert not {1, 2, 3, 4, 5}.isdisjoint(NonSetWithIter())
464+
465+
def test_iter_changed_size():
466+
def just_iterate(it):
467+
for i in it:
468+
pass
469+
470+
def iterate_and_update(it):
471+
for i in it:
472+
s.add(3)
473+
474+
s = {1, 2}
475+
it = iter(s)
476+
s.pop()
477+
assert_raises(RuntimeError, just_iterate, it)
478+
479+
s = {1, 2}
480+
assert_raises(RuntimeError, iterate_and_update, s)

0 commit comments

Comments
 (0)