Update test_dictcomps, test_userdict

This commit is contained in:
ShaharNaveh committed 2026-02-13 13:36:33 +02:00
1 parent a9687f01ee
commit 0dd14d0bdf
2 files changed
+42 -9

No files matched your search

+39 -2
View File
@@ -1,5 +1,8 @@
import traceback
import unittest
from test.support import BrokenIter
# For scope testing.
g = "Global variable"
@@ -72,8 +75,7 @@ class DictComprehensionTest(unittest.TestCase):
self.assertEqual(actual, expected)
self.assertEqual(v, "Local variable")
# TODO: RUSTPYTHON
@unittest.expectedFailure
@unittest.expectedFailure # TODO: RUSTPYTHON
def test_illegal_assignment(self):
with self.assertRaisesRegex(SyntaxError, "cannot assign"):
compile("{x: y for y, x in ((1, 2), (3, 4))} = 5", "<test>",
@@ -129,6 +131,41 @@ class DictComprehensionTest(unittest.TestCase):
self.assertEqual({i: i*i for i in [*range(4)]}, expected)
self.assertEqual({i: i*i for i in (*range(4),)}, expected)
def test_exception_locations(self):
# The location of an exception raised from __init__ or
# __next__ should should be the iterator expression
def init_raises():
try:
{x:x for x in BrokenIter(init_raises=True)}
except Exception as e:
return e
def next_raises():
try:
{x:x for x in BrokenIter(next_raises=True)}
except Exception as e:
return e
def iter_raises():
try:
{x:x for x in BrokenIter(iter_raises=True)}
except Exception as e:
return e
for func, expected in [(init_raises, "BrokenIter(init_raises=True)"),
(next_raises, "BrokenIter(next_raises=True)"),
(iter_raises, "BrokenIter(iter_raises=True)"),
]:
with self.subTest(func):
exc = func()
f = traceback.extract_tb(exc.__traceback__)[0]
indent = 16
co = func.__code__
self.assertEqual(f.lineno, co.co_firstlineno + 2)
self.assertEqual(f.end_lineno, co.co_firstlineno + 2)
self.assertEqual(f.line[f.colno - indent : f.end_colno - indent],
expected)
if __name__ == "__main__":
unittest.main()
+3 -7
View File
@@ -1,6 +1,6 @@
# Check every path through every method of UserDict
from test import mapping_tests, support
from test import mapping_tests
import unittest
import collections
@@ -166,7 +166,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
def test_missing(self):
# Make sure UserDict doesn't have a __missing__ method
self.assertEqual(hasattr(collections.UserDict, "__missing__"), False)
self.assertNotHasAttr(collections.UserDict, "__missing__")
# Test several cases:
# (D) subclass defines __missing__ method returning a value
# (E) subclass defines __missing__ method raising RuntimeError
@@ -213,11 +213,7 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
else:
self.fail("g[42] didn't raise KeyError")
# Decorate existing test with recursion limit, because
# the test is for C structure, but `UserDict` is a Python structure.
test_repr_deep = support.infinite_recursion(25)(
mapping_tests.TestHashMappingProtocol.test_repr_deep,
)
test_repr_deep = mapping_tests.TestHashMappingProtocol.test_repr_deep
if __name__ == "__main__":