Skip to content

Commit 799f38b

Browse files
authored
Merge pull request #5978 from ShaharNaveh/update-test-exception
Update `test_exception*.py` from 3.13.5
2 parents 1fcb656 + 4515c61 commit 799f38b

File tree

7 files changed

+4984
-974
lines changed

7 files changed

+4984
-974
lines changed

Lib/test/support/__init__.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -840,11 +840,27 @@ def python_is_optimized():
840840
return final_opt not in ('', '-O0', '-Og')
841841

842842

843-
_header = 'nP'
843+
# From CPython 3.13.5
844+
Py_GIL_DISABLED = bool(sysconfig.get_config_var('Py_GIL_DISABLED'))
845+
846+
# From CPython 3.13.5
847+
def requires_gil_enabled(msg="needs the GIL enabled"):
848+
"""Decorator for skipping tests on the free-threaded build."""
849+
return unittest.skipIf(Py_GIL_DISABLED, msg)
850+
851+
# From CPython 3.13.5
852+
def expected_failure_if_gil_disabled():
853+
"""Expect test failure if the GIL is disabled."""
854+
if Py_GIL_DISABLED:
855+
return unittest.expectedFailure
856+
return lambda test_case: test_case
857+
858+
# From CPython 3.13.5
859+
if Py_GIL_DISABLED:
860+
_header = 'PHBBInP'
861+
else:
862+
_header = 'nP'
844863
_align = '0n'
845-
if hasattr(sys, "getobjects"):
846-
_header = '2P' + _header
847-
_align = '0P'
848864
_vheader = _header + 'n'
849865

850866
def calcobjsize(fmt):
@@ -2617,6 +2633,10 @@ def exceeds_recursion_limit():
26172633
'skipped on s390x')
26182634
HAVE_ASAN_FORK_BUG = check_sanitizer(address=True)
26192635

2636+
# From CPython 3.13.5
2637+
Py_TRACE_REFS = hasattr(sys, 'getobjects')
2638+
2639+
26202640
# From Cpython 3.13.5
26212641
@contextlib.contextmanager
26222642
def no_color():
@@ -2641,6 +2661,21 @@ def wrapper(*args, **kwargs):
26412661
return wrapper
26422662

26432663

2664+
# From Cpython 3.13.5
2665+
def force_not_colorized_test_class(cls):
2666+
"""Force the terminal not to be colorized for the entire test class."""
2667+
original_setUpClass = cls.setUpClass
2668+
2669+
@classmethod
2670+
@functools.wraps(cls.setUpClass)
2671+
def new_setUpClass(cls):
2672+
cls.enterClassContext(no_color())
2673+
original_setUpClass()
2674+
2675+
cls.setUpClass = new_setUpClass
2676+
return cls
2677+
2678+
26442679
# From python 3.12.8
26452680
class BrokenIter:
26462681
def __init__(self, init_raises=False, next_raises=False, iter_raises=False):

Lib/test/test_exception_group.py

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import collections.abc
22
import types
33
import unittest
4-
from test.support import C_RECURSION_LIMIT
4+
from test.support import get_c_recursion_limit
55

66
class TestExceptionGroupTypeHierarchy(unittest.TestCase):
77
def test_exception_group_types(self):
@@ -300,17 +300,33 @@ def assertMatchesTemplate(self, exc, exc_type, template):
300300
self.assertEqual(type(exc), type(template))
301301
self.assertEqual(exc.args, template.args)
302302

303+
class Predicate:
304+
def __init__(self, func):
305+
self.func = func
306+
307+
def __call__(self, e):
308+
return self.func(e)
309+
310+
def method(self, e):
311+
return self.func(e)
303312

304313
class ExceptionGroupSubgroupTests(ExceptionGroupTestBase):
305314
def setUp(self):
306315
self.eg = create_simple_eg()
307316
self.eg_template = [ValueError(1), TypeError(int), ValueError(2)]
308317

318+
# TODO: RUSTPYTHON
319+
@unittest.expectedFailure
309320
def test_basics_subgroup_split__bad_arg_type(self):
321+
class C:
322+
pass
323+
310324
bad_args = ["bad arg",
325+
C,
311326
OSError('instance not type'),
312327
[OSError, TypeError],
313-
(OSError, 42)]
328+
(OSError, 42),
329+
]
314330
for arg in bad_args:
315331
with self.assertRaises(TypeError):
316332
self.eg.subgroup(arg)
@@ -342,10 +358,14 @@ def test_basics_subgroup_by_type__match(self):
342358
self.assertMatchesTemplate(subeg, ExceptionGroup, template)
343359

344360
def test_basics_subgroup_by_predicate__passthrough(self):
345-
self.assertIs(self.eg, self.eg.subgroup(lambda e: True))
361+
f = lambda e: True
362+
for callable in [f, Predicate(f), Predicate(f).method]:
363+
self.assertIs(self.eg, self.eg.subgroup(callable))
346364

347365
def test_basics_subgroup_by_predicate__no_match(self):
348-
self.assertIsNone(self.eg.subgroup(lambda e: False))
366+
f = lambda e: False
367+
for callable in [f, Predicate(f), Predicate(f).method]:
368+
self.assertIsNone(self.eg.subgroup(callable))
349369

350370
def test_basics_subgroup_by_predicate__match(self):
351371
eg = self.eg
@@ -356,9 +376,12 @@ def test_basics_subgroup_by_predicate__match(self):
356376
((ValueError, TypeError), self.eg_template)]
357377

358378
for match_type, template in testcases:
359-
subeg = eg.subgroup(lambda e: isinstance(e, match_type))
360-
self.assertEqual(subeg.message, eg.message)
361-
self.assertMatchesTemplate(subeg, ExceptionGroup, template)
379+
f = lambda e: isinstance(e, match_type)
380+
for callable in [f, Predicate(f), Predicate(f).method]:
381+
with self.subTest(callable=callable):
382+
subeg = eg.subgroup(f)
383+
self.assertEqual(subeg.message, eg.message)
384+
self.assertMatchesTemplate(subeg, ExceptionGroup, template)
362385

363386

364387
class ExceptionGroupSplitTests(ExceptionGroupTestBase):
@@ -405,14 +428,18 @@ def test_basics_split_by_type__match(self):
405428
self.assertIsNone(rest)
406429

407430
def test_basics_split_by_predicate__passthrough(self):
408-
match, rest = self.eg.split(lambda e: True)
409-
self.assertMatchesTemplate(match, ExceptionGroup, self.eg_template)
410-
self.assertIsNone(rest)
431+
f = lambda e: True
432+
for callable in [f, Predicate(f), Predicate(f).method]:
433+
match, rest = self.eg.split(callable)
434+
self.assertMatchesTemplate(match, ExceptionGroup, self.eg_template)
435+
self.assertIsNone(rest)
411436

412437
def test_basics_split_by_predicate__no_match(self):
413-
match, rest = self.eg.split(lambda e: False)
414-
self.assertIsNone(match)
415-
self.assertMatchesTemplate(rest, ExceptionGroup, self.eg_template)
438+
f = lambda e: False
439+
for callable in [f, Predicate(f), Predicate(f).method]:
440+
match, rest = self.eg.split(callable)
441+
self.assertIsNone(match)
442+
self.assertMatchesTemplate(rest, ExceptionGroup, self.eg_template)
416443

417444
def test_basics_split_by_predicate__match(self):
418445
eg = self.eg
@@ -426,20 +453,22 @@ def test_basics_split_by_predicate__match(self):
426453
]
427454

428455
for match_type, match_template, rest_template in testcases:
429-
match, rest = eg.split(lambda e: isinstance(e, match_type))
430-
self.assertEqual(match.message, eg.message)
431-
self.assertMatchesTemplate(
432-
match, ExceptionGroup, match_template)
433-
if rest_template is not None:
434-
self.assertEqual(rest.message, eg.message)
456+
f = lambda e: isinstance(e, match_type)
457+
for callable in [f, Predicate(f), Predicate(f).method]:
458+
match, rest = eg.split(callable)
459+
self.assertEqual(match.message, eg.message)
435460
self.assertMatchesTemplate(
436-
rest, ExceptionGroup, rest_template)
461+
match, ExceptionGroup, match_template)
462+
if rest_template is not None:
463+
self.assertEqual(rest.message, eg.message)
464+
self.assertMatchesTemplate(
465+
rest, ExceptionGroup, rest_template)
437466

438467

439468
class DeepRecursionInSplitAndSubgroup(unittest.TestCase):
440469
def make_deep_eg(self):
441470
e = TypeError(1)
442-
for i in range(C_RECURSION_LIMIT + 1):
471+
for i in range(get_c_recursion_limit() + 1):
443472
e = ExceptionGroup('eg', [e])
444473
return e
445474

Lib/test/test_exception_hierarchy.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ def test_windows_error(self):
127127
else:
128128
self.assertNotIn('winerror', dir(OSError))
129129

130-
@unittest.skip("TODO: RUSTPYTHON")
131130
def test_posix_error(self):
132131
e = OSError(EEXIST, "File already exists", "foo.txt")
133132
self.assertEqual(e.errno, EEXIST)

0 commit comments

Comments
 (0)