[Dynamo] Fixes for exceptions (#153966)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/153966
Approved by: https://github.com/Lucaskabela
This commit is contained in:
Guilherme Leobas
2025-10-14 13:35:18 -03:00
committed by PyTorch MergeBot
parent 13b621d87c
commit e6f766c7d7
12 changed files with 705 additions and 150 deletions

View File

@ -1,5 +1,5 @@
diff --git a/test/dynamo/cpython/3_13/test_baseexception.py b/test/dynamo/cpython/3_13/test_baseexception.py
index e599b02c17d..750d7a84fb4 100644
index e599b02c17d..057b6ec01b9 100644
--- a/test/dynamo/cpython/3_13/test_baseexception.py
+++ b/test/dynamo/cpython/3_13/test_baseexception.py
@@ -1,10 +1,64 @@
@ -78,7 +78,27 @@ index e599b02c17d..750d7a84fb4 100644
self.assertEqual(len(exc_set), 0, "%s not accounted for" % exc_set)
interface_tests = ("length", "args", "str", "repr")
@@ -142,7 +193,7 @@ class ExceptionClassTests(unittest.TestCase):
@@ -122,12 +173,13 @@ class ExceptionClassTests(unittest.TestCase):
# in PyObject_SetAttr.
import gc
d = {}
- class HashThisKeyWillClearTheDict(str):
- def __hash__(self) -> int:
- d.clear()
- return super().__hash__()
- class Value(str):
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class HashThisKeyWillClearTheDict(str):
+ def __hash__(self) -> int:
+ d.clear()
+ return super().__hash__()
+ class Value(str):
+ pass
exc = Exception()
d[HashThisKeyWillClearTheDict()] = Value() # refcount of Value() is 1 now
@@ -142,7 +194,7 @@ class ExceptionClassTests(unittest.TestCase):
gc.collect()
@ -87,7 +107,31 @@ index e599b02c17d..750d7a84fb4 100644
"""Test usage of exceptions"""
@@ -208,5 +259,5 @@ class UsageTests(unittest.TestCase):
@@ -182,8 +234,9 @@ class UsageTests(unittest.TestCase):
# BaseException; the ability was not possible until BaseException's
# introduction so no need to support new-style objects that do not
# inherit from it.
- class NewStyleClass(object):
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class NewStyleClass(object):
+ pass
self.raise_fails(NewStyleClass)
self.raise_fails(NewStyleClass())
@@ -194,8 +247,9 @@ class UsageTests(unittest.TestCase):
def test_catch_non_BaseException(self):
# Trying to catch an object that does not inherit from BaseException
# is not allowed.
- class NonBaseException(object):
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class NonBaseException(object):
+ pass
self.catch_fails(NonBaseException)
self.catch_fails(NonBaseException())
@@ -208,5 +262,5 @@ class UsageTests(unittest.TestCase):
self.catch_fails("spam")

View File

@ -173,12 +173,13 @@ class ExceptionClassTests(__TestCase):
# in PyObject_SetAttr.
import gc
d = {}
class HashThisKeyWillClearTheDict(str):
def __hash__(self) -> int:
d.clear()
return super().__hash__()
class Value(str):
pass
with torch._dynamo.error_on_graph_break(False):
class HashThisKeyWillClearTheDict(str):
def __hash__(self) -> int:
d.clear()
return super().__hash__()
class Value(str):
pass
exc = Exception()
d[HashThisKeyWillClearTheDict()] = Value() # refcount of Value() is 1 now
@ -233,8 +234,9 @@ class UsageTests(__TestCase):
# BaseException; the ability was not possible until BaseException's
# introduction so no need to support new-style objects that do not
# inherit from it.
class NewStyleClass(object):
pass
with torch._dynamo.error_on_graph_break(False):
class NewStyleClass(object):
pass
self.raise_fails(NewStyleClass)
self.raise_fails(NewStyleClass())
@ -245,8 +247,9 @@ class UsageTests(__TestCase):
def test_catch_non_BaseException(self):
# Trying to catch an object that does not inherit from BaseException
# is not allowed.
class NonBaseException(object):
pass
with torch._dynamo.error_on_graph_break(False):
class NonBaseException(object):
pass
self.catch_fails(NonBaseException)
self.catch_fails(NonBaseException())

View File

@ -1,5 +1,5 @@
diff --git a/test/dynamo/cpython/3_13/test_exceptions.py b/test/dynamo/cpython/3_13/test_exceptions.py
index c91f6662948..0ded70db3c7 100644
index c91f6662948..3a62dec411c 100644
--- a/test/dynamo/cpython/3_13/test_exceptions.py
+++ b/test/dynamo/cpython/3_13/test_exceptions.py
@@ -1,3 +1,59 @@
@ -71,7 +71,305 @@ index c91f6662948..0ded70db3c7 100644
def raise_catch(self, exc, excname):
with self.subTest(exc=exc, excname=excname):
@@ -1844,7 +1900,7 @@ class ExceptionTests(unittest.TestCase):
@@ -343,12 +399,13 @@ class ExceptionTests(unittest.TestCase):
# test that setting an exception at the C level works even if the
# exception object can't be constructed.
- class BadException(Exception):
- def __init__(self_):
- raise RuntimeError("can't instantiate BadException")
+ with torch._dynamo.error_on_graph_break(False):
+ class BadException(Exception):
+ def __init__(self_):
+ raise RuntimeError("can't instantiate BadException")
- class InvalidException:
- pass
+ class InvalidException:
+ pass
@unittest.skipIf(_testcapi is None, "requires _testcapi")
def test_capi1():
@@ -636,8 +693,9 @@ class ExceptionTests(unittest.TestCase):
self.assertIsInstance(e, IndexError)
self.assertEqual(e.__traceback__, tb)
- class MyException(Exception):
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class MyException(Exception):
+ pass
e = MyException().with_traceback(tb)
self.assertIsInstance(e, MyException)
@@ -696,8 +754,9 @@ class ExceptionTests(unittest.TestCase):
self.assertIsNone(e.__context__)
self.assertIsNone(e.__cause__)
- class MyException(OSError):
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class MyException(OSError):
+ pass
e = MyException()
self.assertIsNone(e.__context__)
@@ -726,10 +785,11 @@ class ExceptionTests(unittest.TestCase):
# but user-defined subclasses can if they want
self.assertRaises(TypeError, BaseException, a=1)
- class DerivedException(BaseException):
- def __init__(self, fancy_arg):
- BaseException.__init__(self)
- self.fancy_arg = fancy_arg
+ with torch._dynamo.error_on_graph_break(False):
+ class DerivedException(BaseException):
+ def __init__(self, fancy_arg):
+ BaseException.__init__(self)
+ self.fancy_arg = fancy_arg
x = DerivedException(fancy_arg=42)
self.assertEqual(x.fancy_arg, 42)
@@ -779,11 +839,12 @@ class ExceptionTests(unittest.TestCase):
# Make sure exception state is cleaned up as soon as the except
# block is left. See #2507
- class MyException(Exception):
- def __init__(self, obj):
- self.obj = obj
- class MyObj:
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class MyException(Exception):
+ def __init__(self, obj):
+ self.obj = obj
+ class MyObj:
+ pass
def inner_raising_func():
# Create some references in exception value and traceback
@@ -881,11 +942,12 @@ class ExceptionTests(unittest.TestCase):
self.assertIsNone(obj)
# Inside an exception-silencing "with" block
- class Context:
- def __enter__(self):
- return self
- def __exit__ (self, exc_type, exc_value, exc_tb):
- return True
+ with torch._dynamo.error_on_graph_break(False):
+ class Context:
+ def __enter__(self):
+ return self
+ def __exit__ (self, exc_type, exc_value, exc_tb):
+ return True
obj = MyObj()
wr = weakref.ref(obj)
with Context():
@@ -1027,11 +1089,12 @@ class ExceptionTests(unittest.TestCase):
def _check_generator_cleanup_exc_state(self, testfunc):
# Issue #12791: exception state is cleaned up as soon as a generator
# is closed (reference cycles are broken).
- class MyException(Exception):
- def __init__(self, obj):
- self.obj = obj
- class MyObj:
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class MyException(Exception):
+ def __init__(self, obj):
+ self.obj = obj
+ class MyObj:
+ pass
def raising_gen():
try:
@@ -1090,10 +1153,11 @@ class ExceptionTests(unittest.TestCase):
def test_3114(self):
# Bug #3114: in its destructor, MyObject retrieves a pointer to
# obsolete and/or deallocated objects.
- class MyObject:
- def __del__(self):
- nonlocal e
- e = sys.exception()
+ with torch._dynamo.error_on_graph_break(False):
+ class MyObject:
+ def __del__(self):
+ nonlocal e
+ e = sys.exception()
e = ()
try:
raise Exception(MyObject())
@@ -1103,12 +1167,13 @@ class ExceptionTests(unittest.TestCase):
self.assertIsNone(e)
def test_raise_does_not_create_context_chain_cycle(self):
- class A(Exception):
- pass
- class B(Exception):
- pass
- class C(Exception):
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class A(Exception):
+ pass
+ class B(Exception):
+ pass
+ class C(Exception):
+ pass
# Create a context chain:
# C -> B -> A
@@ -1164,12 +1229,13 @@ class ExceptionTests(unittest.TestCase):
def test_no_hang_on_context_chain_cycle2(self):
# See issue 25782. Cycle at head of context chain.
- class A(Exception):
- pass
- class B(Exception):
- pass
- class C(Exception):
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class A(Exception):
+ pass
+ class B(Exception):
+ pass
+ class C(Exception):
+ pass
# Context cycle:
# +-----------+
@@ -1200,16 +1266,17 @@ class ExceptionTests(unittest.TestCase):
def test_no_hang_on_context_chain_cycle3(self):
# See issue 25782. Longer context chain with cycle.
- class A(Exception):
- pass
- class B(Exception):
- pass
- class C(Exception):
- pass
- class D(Exception):
- pass
- class E(Exception):
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class A(Exception):
+ pass
+ class B(Exception):
+ pass
+ class C(Exception):
+ pass
+ class D(Exception):
+ pass
+ class E(Exception):
+ pass
# Context cycle:
# +-----------+
@@ -1364,11 +1431,12 @@ class ExceptionTests(unittest.TestCase):
def test_badisinstance(self):
# Bug #2542: if issubclass(e, MyException) raises an exception,
# it should be ignored
- class Meta(type):
- def __subclasscheck__(cls, subclass):
- raise ValueError()
- class MyException(Exception, metaclass=Meta):
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class Meta(type):
+ def __subclasscheck__(cls, subclass):
+ raise ValueError()
+ class MyException(Exception, metaclass=Meta):
+ pass
with captured_stderr() as stderr:
try:
@@ -1602,8 +1670,9 @@ class ExceptionTests(unittest.TestCase):
self.assertTrue(issubclass(error3, error2))
# test with explicit base tuple
- class C(object):
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class C(object):
+ pass
error4 = _testcapi.make_exception_with_doc("_testcapi.error4", doc4,
(error3, C))
self.assertTrue(issubclass(error4, error3))
@@ -1623,8 +1692,9 @@ class ExceptionTests(unittest.TestCase):
# Issue #5437: preallocated MemoryError instances should not keep
# traceback objects alive.
from _testcapi import raise_memoryerror
- class C:
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class C:
+ pass
wr = None
def inner():
nonlocal wr
@@ -1644,8 +1714,9 @@ class ExceptionTests(unittest.TestCase):
@no_tracing
def test_recursion_error_cleanup(self):
# Same test as above, but with "recursion exceeded" errors
- class C:
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class C:
+ pass
wr = None
def inner():
nonlocal wr
@@ -1670,11 +1741,12 @@ class ExceptionTests(unittest.TestCase):
def test_unraisable(self):
# Issue #22836: PyErr_WriteUnraisable() should give sensible reports
- class BrokenDel:
- def __del__(self):
- exc = ValueError("del is broken")
- # The following line is included in the traceback report:
- raise exc
+ with torch._dynamo.error_on_graph_break(False):
+ class BrokenDel:
+ def __del__(self):
+ exc = ValueError("del is broken")
+ # The following line is included in the traceback report:
+ raise exc
obj = BrokenDel()
with support.catch_unraisable_exception() as cm:
@@ -1728,11 +1800,12 @@ class ExceptionTests(unittest.TestCase):
def test_yield_in_nested_try_excepts(self):
#Issue #25612
- class MainError(Exception):
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class MainError(Exception):
+ pass
- class SubError(Exception):
- pass
+ class SubError(Exception):
+ pass
def main():
try:
@@ -1807,8 +1880,9 @@ class ExceptionTests(unittest.TestCase):
# subclass object. Finally, it checks that creating a new MemoryError
# succeeds, proving that the freelist is not corrupted.
- class TestException(MemoryError):
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class TestException(MemoryError):
+ pass
try:
raise MemoryError
@@ -1844,7 +1918,7 @@ class ExceptionTests(unittest.TestCase):
self.assertIn(b'MemoryError', err)
@ -80,7 +378,18 @@ index c91f6662948..0ded70db3c7 100644
def test_name_error_has_name(self):
try:
bluch
@@ -1894,7 +1950,7 @@ class NameErrorTests(unittest.TestCase):
@@ -1886,15 +1960,16 @@ class NameErrorTests(unittest.TestCase):
def test_gh_111654(self):
def f():
- class TestClass:
- TestClass
+ with torch._dynamo.error_on_graph_break(False):
+ class TestClass:
+ TestClass
self.assertRaises(NameError, f)
# Note: name suggestion tests live in `test_traceback`.
@ -89,7 +398,33 @@ index c91f6662948..0ded70db3c7 100644
def test_attributes(self):
# Setting 'attr' should not be a problem.
exc = AttributeError('Ouch!')
@@ -1937,7 +1993,7 @@ class AttributeErrorTests(unittest.TestCase):
@@ -1907,8 +1982,9 @@ class AttributeErrorTests(unittest.TestCase):
self.assertIs(exc.obj, sentinel)
def test_getattr_has_name_and_obj(self):
- class A:
- blech = None
+ with torch._dynamo.error_on_graph_break(False):
+ class A:
+ blech = None
obj = A()
try:
@@ -1923,9 +1999,10 @@ class AttributeErrorTests(unittest.TestCase):
self.assertEqual(obj, exc.obj)
def test_getattr_has_name_and_obj_for_method(self):
- class A:
- def blech(self):
- return
+ with torch._dynamo.error_on_graph_break(False):
+ class A:
+ def blech(self):
+ return
obj = A()
try:
@@ -1937,7 +2014,7 @@ class AttributeErrorTests(unittest.TestCase):
# Note: name suggestion tests live in `test_traceback`.
@ -98,7 +433,7 @@ index c91f6662948..0ded70db3c7 100644
def test_attributes(self):
# Setting 'name' and 'path' should not be a problem.
@@ -2024,7 +2080,7 @@ def run_script(source):
@@ -2024,7 +2101,7 @@ def run_script(source):
_rc, _out, err = script_helper.assert_python_failure('-Wd', '-X', 'utf8', TESTFN)
return err.decode('utf-8').splitlines()
@ -107,7 +442,7 @@ index c91f6662948..0ded70db3c7 100644
def tearDown(self):
unlink(TESTFN)
@@ -2159,7 +2215,7 @@ class AssertionErrorTests(unittest.TestCase):
@@ -2159,7 +2236,7 @@ class AssertionErrorTests(unittest.TestCase):
@support.force_not_colorized_test_class
@ -116,7 +451,19 @@ index c91f6662948..0ded70db3c7 100644
maxDiff = None
@force_not_colorized
@@ -2290,6 +2346,7 @@ class SyntaxErrorTests(unittest.TestCase):
@@ -2254,8 +2331,9 @@ class SyntaxErrorTests(unittest.TestCase):
the_exception = exc
def test_subclass(self):
- class MySyntaxError(SyntaxError):
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class MySyntaxError(SyntaxError):
+ pass
try:
raise MySyntaxError("bad bad", ("bad.py", 1, 2, "abcdefg", 1, 7))
@@ -2290,6 +2368,7 @@ class SyntaxErrorTests(unittest.TestCase):
err = run_script(b"\x89")
self.assertIn("SyntaxError: Non-UTF-8 code starting with '\\x89' in file", err[-1])
@ -124,7 +471,7 @@ index c91f6662948..0ded70db3c7 100644
def test_string_source(self):
def try_compile(source):
with self.assertRaises(SyntaxError) as cm:
@@ -2405,7 +2462,7 @@ class SyntaxErrorTests(unittest.TestCase):
@@ -2405,7 +2484,7 @@ class SyntaxErrorTests(unittest.TestCase):
self.assertRaises(TypeError, SyntaxError, "bad bad", args)
@ -133,7 +480,7 @@ index c91f6662948..0ded70db3c7 100644
def test_except_star_invalid_exception_type(self):
with self.assertRaises(TypeError):
try:
@@ -2420,7 +2477,7 @@ class TestInvalidExceptionMatcher(unittest.TestCase):
@@ -2420,7 +2499,7 @@ class TestInvalidExceptionMatcher(unittest.TestCase):
pass
@ -142,7 +489,42 @@ index c91f6662948..0ded70db3c7 100644
def lineno_after_raise(self, f, *expected):
try:
@@ -2529,5 +2586,5 @@ class PEP626Tests(unittest.TestCase):
@@ -2499,11 +2578,12 @@ class PEP626Tests(unittest.TestCase):
self.lineno_after_raise(in_finally_except, 4)
def test_lineno_after_with(self):
- class Noop:
- def __enter__(self):
- return self
- def __exit__(self, *args):
- pass
+ with torch._dynamo.error_on_graph_break(False):
+ class Noop:
+ def __enter__(self):
+ return self
+ def __exit__(self, *args):
+ pass
def after_with():
with Noop():
1/0
@@ -2518,16 +2598,17 @@ class PEP626Tests(unittest.TestCase):
self.lineno_after_raise(f, None)
def test_lineno_after_raise_in_with_exit(self):
- class ExitFails:
- def __enter__(self):
- return self
- def __exit__(self, *args):
- raise ValueError
+ with torch._dynamo.error_on_graph_break(False):
+ class ExitFails:
+ def __enter__(self):
+ return self
+ def __exit__(self, *args):
+ raise ValueError
def after_with():
with ExitFails():
1/0
self.lineno_after_raise(after_with, 1, 1)

View File

@ -399,12 +399,13 @@ class ExceptionTests(__TestCase):
# test that setting an exception at the C level works even if the
# exception object can't be constructed.
class BadException(Exception):
def __init__(self_):
raise RuntimeError("can't instantiate BadException")
with torch._dynamo.error_on_graph_break(False):
class BadException(Exception):
def __init__(self_):
raise RuntimeError("can't instantiate BadException")
class InvalidException:
pass
class InvalidException:
pass
@unittest.skipIf(_testcapi is None, "requires _testcapi")
def test_capi1():
@ -692,8 +693,9 @@ class ExceptionTests(__TestCase):
self.assertIsInstance(e, IndexError)
self.assertEqual(e.__traceback__, tb)
class MyException(Exception):
pass
with torch._dynamo.error_on_graph_break(False):
class MyException(Exception):
pass
e = MyException().with_traceback(tb)
self.assertIsInstance(e, MyException)
@ -752,8 +754,9 @@ class ExceptionTests(__TestCase):
self.assertIsNone(e.__context__)
self.assertIsNone(e.__cause__)
class MyException(OSError):
pass
with torch._dynamo.error_on_graph_break(False):
class MyException(OSError):
pass
e = MyException()
self.assertIsNone(e.__context__)
@ -782,10 +785,11 @@ class ExceptionTests(__TestCase):
# but user-defined subclasses can if they want
self.assertRaises(TypeError, BaseException, a=1)
class DerivedException(BaseException):
def __init__(self, fancy_arg):
BaseException.__init__(self)
self.fancy_arg = fancy_arg
with torch._dynamo.error_on_graph_break(False):
class DerivedException(BaseException):
def __init__(self, fancy_arg):
BaseException.__init__(self)
self.fancy_arg = fancy_arg
x = DerivedException(fancy_arg=42)
self.assertEqual(x.fancy_arg, 42)
@ -835,11 +839,12 @@ class ExceptionTests(__TestCase):
# Make sure exception state is cleaned up as soon as the except
# block is left. See #2507
class MyException(Exception):
def __init__(self, obj):
self.obj = obj
class MyObj:
pass
with torch._dynamo.error_on_graph_break(False):
class MyException(Exception):
def __init__(self, obj):
self.obj = obj
class MyObj:
pass
def inner_raising_func():
# Create some references in exception value and traceback
@ -937,11 +942,12 @@ class ExceptionTests(__TestCase):
self.assertIsNone(obj)
# Inside an exception-silencing "with" block
class Context:
def __enter__(self):
return self
def __exit__ (self, exc_type, exc_value, exc_tb):
return True
with torch._dynamo.error_on_graph_break(False):
class Context:
def __enter__(self):
return self
def __exit__ (self, exc_type, exc_value, exc_tb):
return True
obj = MyObj()
wr = weakref.ref(obj)
with Context():
@ -1083,11 +1089,12 @@ class ExceptionTests(__TestCase):
def _check_generator_cleanup_exc_state(self, testfunc):
# Issue #12791: exception state is cleaned up as soon as a generator
# is closed (reference cycles are broken).
class MyException(Exception):
def __init__(self, obj):
self.obj = obj
class MyObj:
pass
with torch._dynamo.error_on_graph_break(False):
class MyException(Exception):
def __init__(self, obj):
self.obj = obj
class MyObj:
pass
def raising_gen():
try:
@ -1146,10 +1153,11 @@ class ExceptionTests(__TestCase):
def test_3114(self):
# Bug #3114: in its destructor, MyObject retrieves a pointer to
# obsolete and/or deallocated objects.
class MyObject:
def __del__(self):
nonlocal e
e = sys.exception()
with torch._dynamo.error_on_graph_break(False):
class MyObject:
def __del__(self):
nonlocal e
e = sys.exception()
e = ()
try:
raise Exception(MyObject())
@ -1159,12 +1167,13 @@ class ExceptionTests(__TestCase):
self.assertIsNone(e)
def test_raise_does_not_create_context_chain_cycle(self):
class A(Exception):
pass
class B(Exception):
pass
class C(Exception):
pass
with torch._dynamo.error_on_graph_break(False):
class A(Exception):
pass
class B(Exception):
pass
class C(Exception):
pass
# Create a context chain:
# C -> B -> A
@ -1220,12 +1229,13 @@ class ExceptionTests(__TestCase):
def test_no_hang_on_context_chain_cycle2(self):
# See issue 25782. Cycle at head of context chain.
class A(Exception):
pass
class B(Exception):
pass
class C(Exception):
pass
with torch._dynamo.error_on_graph_break(False):
class A(Exception):
pass
class B(Exception):
pass
class C(Exception):
pass
# Context cycle:
# +-----------+
@ -1256,16 +1266,17 @@ class ExceptionTests(__TestCase):
def test_no_hang_on_context_chain_cycle3(self):
# See issue 25782. Longer context chain with cycle.
class A(Exception):
pass
class B(Exception):
pass
class C(Exception):
pass
class D(Exception):
pass
class E(Exception):
pass
with torch._dynamo.error_on_graph_break(False):
class A(Exception):
pass
class B(Exception):
pass
class C(Exception):
pass
class D(Exception):
pass
class E(Exception):
pass
# Context cycle:
# +-----------+
@ -1420,11 +1431,12 @@ class ExceptionTests(__TestCase):
def test_badisinstance(self):
# Bug #2542: if issubclass(e, MyException) raises an exception,
# it should be ignored
class Meta(type):
def __subclasscheck__(cls, subclass):
raise ValueError()
class MyException(Exception, metaclass=Meta):
pass
with torch._dynamo.error_on_graph_break(False):
class Meta(type):
def __subclasscheck__(cls, subclass):
raise ValueError()
class MyException(Exception, metaclass=Meta):
pass
with captured_stderr() as stderr:
try:
@ -1658,8 +1670,9 @@ class ExceptionTests(__TestCase):
self.assertTrue(issubclass(error3, error2))
# test with explicit base tuple
class C(object):
pass
with torch._dynamo.error_on_graph_break(False):
class C(object):
pass
error4 = _testcapi.make_exception_with_doc("_testcapi.error4", doc4,
(error3, C))
self.assertTrue(issubclass(error4, error3))
@ -1679,8 +1692,9 @@ class ExceptionTests(__TestCase):
# Issue #5437: preallocated MemoryError instances should not keep
# traceback objects alive.
from _testcapi import raise_memoryerror
class C:
pass
with torch._dynamo.error_on_graph_break(False):
class C:
pass
wr = None
def inner():
nonlocal wr
@ -1700,8 +1714,9 @@ class ExceptionTests(__TestCase):
@no_tracing
def test_recursion_error_cleanup(self):
# Same test as above, but with "recursion exceeded" errors
class C:
pass
with torch._dynamo.error_on_graph_break(False):
class C:
pass
wr = None
def inner():
nonlocal wr
@ -1726,11 +1741,12 @@ class ExceptionTests(__TestCase):
def test_unraisable(self):
# Issue #22836: PyErr_WriteUnraisable() should give sensible reports
class BrokenDel:
def __del__(self):
exc = ValueError("del is broken")
# The following line is included in the traceback report:
raise exc
with torch._dynamo.error_on_graph_break(False):
class BrokenDel:
def __del__(self):
exc = ValueError("del is broken")
# The following line is included in the traceback report:
raise exc
obj = BrokenDel()
with support.catch_unraisable_exception() as cm:
@ -1784,11 +1800,12 @@ class ExceptionTests(__TestCase):
def test_yield_in_nested_try_excepts(self):
#Issue #25612
class MainError(Exception):
pass
with torch._dynamo.error_on_graph_break(False):
class MainError(Exception):
pass
class SubError(Exception):
pass
class SubError(Exception):
pass
def main():
try:
@ -1863,8 +1880,9 @@ class ExceptionTests(__TestCase):
# subclass object. Finally, it checks that creating a new MemoryError
# succeeds, proving that the freelist is not corrupted.
class TestException(MemoryError):
pass
with torch._dynamo.error_on_graph_break(False):
class TestException(MemoryError):
pass
try:
raise MemoryError
@ -1942,8 +1960,9 @@ class NameErrorTests(__TestCase):
def test_gh_111654(self):
def f():
class TestClass:
TestClass
with torch._dynamo.error_on_graph_break(False):
class TestClass:
TestClass
self.assertRaises(NameError, f)
@ -1963,8 +1982,9 @@ class AttributeErrorTests(__TestCase):
self.assertIs(exc.obj, sentinel)
def test_getattr_has_name_and_obj(self):
class A:
blech = None
with torch._dynamo.error_on_graph_break(False):
class A:
blech = None
obj = A()
try:
@ -1979,9 +1999,10 @@ class AttributeErrorTests(__TestCase):
self.assertEqual(obj, exc.obj)
def test_getattr_has_name_and_obj_for_method(self):
class A:
def blech(self):
return
with torch._dynamo.error_on_graph_break(False):
class A:
def blech(self):
return
obj = A()
try:
@ -2310,8 +2331,9 @@ class SyntaxErrorTests(__TestCase):
the_exception = exc
def test_subclass(self):
class MySyntaxError(SyntaxError):
pass
with torch._dynamo.error_on_graph_break(False):
class MySyntaxError(SyntaxError):
pass
try:
raise MySyntaxError("bad bad", ("bad.py", 1, 2, "abcdefg", 1, 7))
@ -2556,11 +2578,12 @@ class PEP626Tests(__TestCase):
self.lineno_after_raise(in_finally_except, 4)
def test_lineno_after_with(self):
class Noop:
def __enter__(self):
return self
def __exit__(self, *args):
pass
with torch._dynamo.error_on_graph_break(False):
class Noop:
def __enter__(self):
return self
def __exit__(self, *args):
pass
def after_with():
with Noop():
1/0
@ -2575,11 +2598,12 @@ class PEP626Tests(__TestCase):
self.lineno_after_raise(f, None)
def test_lineno_after_raise_in_with_exit(self):
class ExitFails:
def __enter__(self):
return self
def __exit__(self, *args):
raise ValueError
with torch._dynamo.error_on_graph_break(False):
class ExitFails:
def __enter__(self):
return self
def __exit__(self, *args):
raise ValueError
def after_with():
with ExitFails():

View File

@ -1,5 +1,5 @@
diff --git a/test/dynamo/cpython/3_13/test_raise.py b/test/dynamo/cpython/3_13/test_raise.py
index 6d26a61bee4..042d1ae3d7c 100644
index 6d26a61bee4..ce748433d28 100644
--- a/test/dynamo/cpython/3_13/test_raise.py
+++ b/test/dynamo/cpython/3_13/test_raise.py
@@ -1,3 +1,58 @@
@ -70,7 +70,35 @@ index 6d26a61bee4..042d1ae3d7c 100644
def test_invalid_reraise(self):
try:
raise
@@ -148,7 +203,7 @@ class TestRaise(unittest.TestCase):
@@ -120,9 +175,10 @@ class TestRaise(unittest.TestCase):
self.assertRaises(StopIteration, lambda: next(g))
def test_erroneous_exception(self):
- class MyException(Exception):
- def __init__(self):
- raise RuntimeError()
+ with torch._dynamo.error_on_graph_break(False):
+ class MyException(Exception):
+ def __init__(self):
+ raise RuntimeError()
try:
raise MyException
@@ -133,9 +189,10 @@ class TestRaise(unittest.TestCase):
def test_new_returns_invalid_instance(self):
# See issue #11627.
- class MyException(Exception):
- def __new__(cls, *args):
- return object()
+ with torch._dynamo.error_on_graph_break(False):
+ class MyException(Exception):
+ def __new__(cls, *args):
+ return object()
with self.assertRaises(TypeError):
raise MyException
@@ -148,7 +205,7 @@ class TestRaise(unittest.TestCase):
@ -79,7 +107,37 @@ index 6d26a61bee4..042d1ae3d7c 100644
def testCauseSyntax(self):
try:
@@ -221,7 +276,7 @@ class TestCause(unittest.TestCase):
@@ -186,10 +243,11 @@ class TestCause(unittest.TestCase):
self.fail("No exception raised")
def test_class_cause_nonexception_result(self):
- class ConstructsNone(BaseException):
- @classmethod
- def __new__(*args, **kwargs):
- return None
+ with torch._dynamo.error_on_graph_break(False):
+ class ConstructsNone(BaseException):
+ @classmethod
+ def __new__(*args, **kwargs):
+ return None
try:
raise IndexError from ConstructsNone
except TypeError as e:
@@ -209,9 +267,10 @@ class TestCause(unittest.TestCase):
self.fail("No exception raised")
def test_erroneous_cause(self):
- class MyException(Exception):
- def __init__(self):
- raise RuntimeError()
+ with torch._dynamo.error_on_graph_break(False):
+ class MyException(Exception):
+ def __init__(self):
+ raise RuntimeError()
try:
raise IndexError from MyException
@@ -221,7 +280,7 @@ class TestCause(unittest.TestCase):
self.fail("No exception raised")
@ -88,7 +146,7 @@ index 6d26a61bee4..042d1ae3d7c 100644
def test_sets_traceback(self):
try:
@@ -242,7 +297,7 @@ class TestTraceback(unittest.TestCase):
@@ -242,7 +301,7 @@ class TestTraceback(unittest.TestCase):
self.fail("No exception raised")
@ -97,7 +155,7 @@ index 6d26a61bee4..042d1ae3d7c 100644
def raiser(self):
raise ValueError
@@ -308,7 +363,7 @@ class TestTracebackType(unittest.TestCase):
@@ -308,7 +367,7 @@ class TestTracebackType(unittest.TestCase):
types.TracebackType(other_tb, frame, 1, "nuh-uh")
@ -106,7 +164,45 @@ index 6d26a61bee4..042d1ae3d7c 100644
def test_instance_context_instance_raise(self):
context = IndexError()
try:
@@ -498,7 +553,7 @@ class TestContext(unittest.TestCase):
@@ -392,11 +451,12 @@ class TestContext(unittest.TestCase):
self.fail("No exception raised")
def test_context_manager(self):
- class ContextManager:
- def __enter__(self):
- pass
- def __exit__(self, t, v, tb):
- xyzzy
+ with torch._dynamo.error_on_graph_break(False):
+ class ContextManager:
+ def __enter__(self):
+ pass
+ def __exit__(self, t, v, tb):
+ xyzzy
try:
with ContextManager():
1/0
@@ -471,12 +531,13 @@ class TestContext(unittest.TestCase):
import gc
# A re-raised exception in a __del__ caused the __context__
# to be cleared
- class C:
- def __del__(self):
- try:
- 1/0
- except:
- raise
+ with torch._dynamo.error_on_graph_break(False):
+ class C:
+ def __del__(self):
+ try:
+ 1/0
+ except:
+ raise
def f():
x = C()
@@ -498,7 +559,7 @@ class TestContext(unittest.TestCase):
self.assertEqual(ZeroDivisionError, cm.unraisable.exc_type)
@ -115,7 +211,7 @@ index 6d26a61bee4..042d1ae3d7c 100644
def test_tuples(self):
try:
raise (IndexError, KeyError) # This should be a tuple!
@@ -517,4 +572,4 @@ class TestRemovedFunctionality(unittest.TestCase):
@@ -517,4 +578,4 @@ class TestRemovedFunctionality(unittest.TestCase):
if __name__ == "__main__":

View File

@ -175,9 +175,10 @@ class TestRaise(__TestCase):
self.assertRaises(StopIteration, lambda: next(g))
def test_erroneous_exception(self):
class MyException(Exception):
def __init__(self):
raise RuntimeError()
with torch._dynamo.error_on_graph_break(False):
class MyException(Exception):
def __init__(self):
raise RuntimeError()
try:
raise MyException
@ -188,9 +189,10 @@ class TestRaise(__TestCase):
def test_new_returns_invalid_instance(self):
# See issue #11627.
class MyException(Exception):
def __new__(cls, *args):
return object()
with torch._dynamo.error_on_graph_break(False):
class MyException(Exception):
def __new__(cls, *args):
return object()
with self.assertRaises(TypeError):
raise MyException
@ -241,10 +243,11 @@ class TestCause(__TestCase):
self.fail("No exception raised")
def test_class_cause_nonexception_result(self):
class ConstructsNone(BaseException):
@classmethod
def __new__(*args, **kwargs):
return None
with torch._dynamo.error_on_graph_break(False):
class ConstructsNone(BaseException):
@classmethod
def __new__(*args, **kwargs):
return None
try:
raise IndexError from ConstructsNone
except TypeError as e:
@ -264,9 +267,10 @@ class TestCause(__TestCase):
self.fail("No exception raised")
def test_erroneous_cause(self):
class MyException(Exception):
def __init__(self):
raise RuntimeError()
with torch._dynamo.error_on_graph_break(False):
class MyException(Exception):
def __init__(self):
raise RuntimeError()
try:
raise IndexError from MyException
@ -447,11 +451,12 @@ class TestContext(__TestCase):
self.fail("No exception raised")
def test_context_manager(self):
class ContextManager:
def __enter__(self):
pass
def __exit__(self, t, v, tb):
xyzzy
with torch._dynamo.error_on_graph_break(False):
class ContextManager:
def __enter__(self):
pass
def __exit__(self, t, v, tb):
xyzzy
try:
with ContextManager():
1/0
@ -526,12 +531,13 @@ class TestContext(__TestCase):
import gc
# A re-raised exception in a __del__ caused the __context__
# to be cleared
class C:
def __del__(self):
try:
1/0
except:
raise
with torch._dynamo.error_on_graph_break(False):
class C:
def __del__(self):
try:
1/0
except:
raise
def f():
x = C()

View File

@ -3249,7 +3249,7 @@ class ReproTests(torch._dynamo.test_case.TestCase):
def test_rewrite_assert_with_non_string_msg(self):
def f(x):
b = x.sin()
assert x[0] == 2, x.size()
assert x[0] == 2, f"Error {x}: {x.size()}"
return x.cos() + b
torch._dynamo.utils.counters.clear()