Skip to content

Commit 6af2303

Browse files
doerwaltermiss-islington
authored andcommitted
bpo-2661: Make mapping tests better usable for custom mapping classes. (GH-11157)
In test_fromkeys() the derived test class now supports all arguments in its constructor so that the class to be tested can use its own constructor in its fromkeys() implementation. In test_mutatingiteration() the test fails as soon as iterating over a dictionary with one entry and adding new entries in the loop iterates more than once (to avoid endless loops in faulty implementations). https://bugs.python.org/issue2661
1 parent cb65202 commit 6af2303

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

Lib/test/mapping_tests.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def __new__(cls):
448448
class Exc(Exception): pass
449449

450450
class baddict1(self.type2test):
451-
def __init__(self):
451+
def __init__(self, *args, **kwargs):
452452
raise Exc()
453453

454454
self.assertRaises(Exc, baddict1.fromkeys, [1])
@@ -595,12 +595,14 @@ def test_mutatingiteration(self):
595595
d = self._empty_mapping()
596596
d[1] = 1
597597
try:
598+
count = 0
598599
for i in d:
599600
d[i+1] = 1
601+
if count >= 1:
602+
self.fail("changing dict size during iteration doesn't raise Error")
603+
count += 1
600604
except RuntimeError:
601605
pass
602-
else:
603-
self.fail("changing dict size during iteration doesn't raise Error")
604606

605607
def test_repr(self):
606608
d = self._empty_mapping()

0 commit comments

Comments
 (0)