Skip to content

Commit 0dd14d0

Browse files
committed
Update test_dictcomps, test_userdict
1 parent a9687f0 commit 0dd14d0

2 files changed

Lines changed: 42 additions & 9 deletions

File tree

Lib/test/test_dictcomps.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import traceback
12
import unittest
23

4+
from test.support import BrokenIter
5+
36
# For scope testing.
47
g = "Global variable"
58

@@ -72,8 +75,7 @@ def test_local_visibility(self):
7275
self.assertEqual(actual, expected)
7376
self.assertEqual(v, "Local variable")
7477

75-
# TODO: RUSTPYTHON
76-
@unittest.expectedFailure
78+
@unittest.expectedFailure # TODO: RUSTPYTHON
7779
def test_illegal_assignment(self):
7880
with self.assertRaisesRegex(SyntaxError, "cannot assign"):
7981
compile("{x: y for y, x in ((1, 2), (3, 4))} = 5", "<test>",
@@ -129,6 +131,41 @@ def test_star_expression(self):
129131
self.assertEqual({i: i*i for i in [*range(4)]}, expected)
130132
self.assertEqual({i: i*i for i in (*range(4),)}, expected)
131133

134+
def test_exception_locations(self):
135+
# The location of an exception raised from __init__ or
136+
# __next__ should should be the iterator expression
137+
def init_raises():
138+
try:
139+
{x:x for x in BrokenIter(init_raises=True)}
140+
except Exception as e:
141+
return e
142+
143+
def next_raises():
144+
try:
145+
{x:x for x in BrokenIter(next_raises=True)}
146+
except Exception as e:
147+
return e
148+
149+
def iter_raises():
150+
try:
151+
{x:x for x in BrokenIter(iter_raises=True)}
152+
except Exception as e:
153+
return e
154+
155+
for func, expected in [(init_raises, "BrokenIter(init_raises=True)"),
156+
(next_raises, "BrokenIter(next_raises=True)"),
157+
(iter_raises, "BrokenIter(iter_raises=True)"),
158+
]:
159+
with self.subTest(func):
160+
exc = func()
161+
f = traceback.extract_tb(exc.__traceback__)[0]
162+
indent = 16
163+
co = func.__code__
164+
self.assertEqual(f.lineno, co.co_firstlineno + 2)
165+
self.assertEqual(f.end_lineno, co.co_firstlineno + 2)
166+
self.assertEqual(f.line[f.colno - indent : f.end_colno - indent],
167+
expected)
168+
132169

133170
if __name__ == "__main__":
134171
unittest.main()

Lib/test/test_userdict.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Check every path through every method of UserDict
22

3-
from test import mapping_tests, support
3+
from test import mapping_tests
44
import unittest
55
import collections
66

@@ -166,7 +166,7 @@ def test_update(self):
166166

167167
def test_missing(self):
168168
# Make sure UserDict doesn't have a __missing__ method
169-
self.assertEqual(hasattr(collections.UserDict, "__missing__"), False)
169+
self.assertNotHasAttr(collections.UserDict, "__missing__")
170170
# Test several cases:
171171
# (D) subclass defines __missing__ method returning a value
172172
# (E) subclass defines __missing__ method raising RuntimeError
@@ -213,11 +213,7 @@ class G(collections.UserDict):
213213
else:
214214
self.fail("g[42] didn't raise KeyError")
215215

216-
# Decorate existing test with recursion limit, because
217-
# the test is for C structure, but `UserDict` is a Python structure.
218-
test_repr_deep = support.infinite_recursion(25)(
219-
mapping_tests.TestHashMappingProtocol.test_repr_deep,
220-
)
216+
test_repr_deep = mapping_tests.TestHashMappingProtocol.test_repr_deep
221217

222218

223219
if __name__ == "__main__":

0 commit comments

Comments
 (0)