Skip to content

Commit e6f766c

Browse files
guilhermeleobaspytorchmergebot
authored andcommitted
[Dynamo] Fixes for exceptions (pytorch#153966)
Pull Request resolved: pytorch#153966 Approved by: https://github.com/Lucaskabela
1 parent 13b621d commit e6f766c

12 files changed

+705
-150
lines changed

test/dynamo/cpython/3_13/test_baseexception.diff

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/test/dynamo/cpython/3_13/test_baseexception.py b/test/dynamo/cpython/3_13/test_baseexception.py
2-
index e599b02c17d..750d7a84fb4 100644
2+
index e599b02c17d..057b6ec01b9 100644
33
--- a/test/dynamo/cpython/3_13/test_baseexception.py
44
+++ b/test/dynamo/cpython/3_13/test_baseexception.py
55
@@ -1,10 +1,64 @@
@@ -78,7 +78,27 @@ index e599b02c17d..750d7a84fb4 100644
7878
self.assertEqual(len(exc_set), 0, "%s not accounted for" % exc_set)
7979

8080
interface_tests = ("length", "args", "str", "repr")
81-
@@ -142,7 +193,7 @@ class ExceptionClassTests(unittest.TestCase):
81+
@@ -122,12 +173,13 @@ class ExceptionClassTests(unittest.TestCase):
82+
# in PyObject_SetAttr.
83+
import gc
84+
d = {}
85+
- class HashThisKeyWillClearTheDict(str):
86+
- def __hash__(self) -> int:
87+
- d.clear()
88+
- return super().__hash__()
89+
- class Value(str):
90+
- pass
91+
+ with torch._dynamo.error_on_graph_break(False):
92+
+ class HashThisKeyWillClearTheDict(str):
93+
+ def __hash__(self) -> int:
94+
+ d.clear()
95+
+ return super().__hash__()
96+
+ class Value(str):
97+
+ pass
98+
exc = Exception()
99+
100+
d[HashThisKeyWillClearTheDict()] = Value() # refcount of Value() is 1 now
101+
@@ -142,7 +194,7 @@ class ExceptionClassTests(unittest.TestCase):
82102
gc.collect()
83103

84104

@@ -87,7 +107,31 @@ index e599b02c17d..750d7a84fb4 100644
87107

88108
"""Test usage of exceptions"""
89109

90-
@@ -208,5 +259,5 @@ class UsageTests(unittest.TestCase):
110+
@@ -182,8 +234,9 @@ class UsageTests(unittest.TestCase):
111+
# BaseException; the ability was not possible until BaseException's
112+
# introduction so no need to support new-style objects that do not
113+
# inherit from it.
114+
- class NewStyleClass(object):
115+
- pass
116+
+ with torch._dynamo.error_on_graph_break(False):
117+
+ class NewStyleClass(object):
118+
+ pass
119+
self.raise_fails(NewStyleClass)
120+
self.raise_fails(NewStyleClass())
121+
122+
@@ -194,8 +247,9 @@ class UsageTests(unittest.TestCase):
123+
def test_catch_non_BaseException(self):
124+
# Trying to catch an object that does not inherit from BaseException
125+
# is not allowed.
126+
- class NonBaseException(object):
127+
- pass
128+
+ with torch._dynamo.error_on_graph_break(False):
129+
+ class NonBaseException(object):
130+
+ pass
131+
self.catch_fails(NonBaseException)
132+
self.catch_fails(NonBaseException())
133+
134+
@@ -208,5 +262,5 @@ class UsageTests(unittest.TestCase):
91135
self.catch_fails("spam")
92136

93137

test/dynamo/cpython/3_13/test_baseexception.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,13 @@ def test_setstate_refcount_no_crash(self):
173173
# in PyObject_SetAttr.
174174
import gc
175175
d = {}
176-
class HashThisKeyWillClearTheDict(str):
177-
def __hash__(self) -> int:
178-
d.clear()
179-
return super().__hash__()
180-
class Value(str):
181-
pass
176+
with torch._dynamo.error_on_graph_break(False):
177+
class HashThisKeyWillClearTheDict(str):
178+
def __hash__(self) -> int:
179+
d.clear()
180+
return super().__hash__()
181+
class Value(str):
182+
pass
182183
exc = Exception()
183184

184185
d[HashThisKeyWillClearTheDict()] = Value() # refcount of Value() is 1 now
@@ -233,8 +234,9 @@ def test_raise_new_style_non_exception(self):
233234
# BaseException; the ability was not possible until BaseException's
234235
# introduction so no need to support new-style objects that do not
235236
# inherit from it.
236-
class NewStyleClass(object):
237-
pass
237+
with torch._dynamo.error_on_graph_break(False):
238+
class NewStyleClass(object):
239+
pass
238240
self.raise_fails(NewStyleClass)
239241
self.raise_fails(NewStyleClass())
240242

@@ -245,8 +247,9 @@ def test_raise_string(self):
245247
def test_catch_non_BaseException(self):
246248
# Trying to catch an object that does not inherit from BaseException
247249
# is not allowed.
248-
class NonBaseException(object):
249-
pass
250+
with torch._dynamo.error_on_graph_break(False):
251+
class NonBaseException(object):
252+
pass
250253
self.catch_fails(NonBaseException)
251254
self.catch_fails(NonBaseException())
252255

0 commit comments

Comments
 (0)