Date: Mon, 27 Apr 2020 21:43:45 -0600
Subject: [PATCH 16/16] Fix the "custom" exception tests.
---
Lib/test/test__xxsubinterpreters.py | 55 +++++++++++++++++++++++------
Modules/_xxsubinterpretersmodule.c | 1 +
2 files changed, 46 insertions(+), 10 deletions(-)
diff --git a/Lib/test/test__xxsubinterpreters.py b/Lib/test/test__xxsubinterpreters.py
index 30f42d8c400c11..e492edaba2df64 100644
--- a/Lib/test/test__xxsubinterpreters.py
+++ b/Lib/test/test__xxsubinterpreters.py
@@ -1210,10 +1210,15 @@ def expected_run_failure(self, expected):
yield caught
exc = caught.exception
+ modname = exctype.__module__
+ if modname == 'builtins' or modname == '__main__':
+ exctypename = exctype.__name__
+ else:
+ exctypename = f'{modname}.{exctype.__name__}'
if exctype is expected:
- self.assertEqual(str(exc).split(':')[0], exctype.__name__)
+ self.assertEqual(str(exc).split(':')[0], exctypename)
else:
- self.assertEqual(str(exc), f'{exctype.__name__}: {expected}')
+ self.assertEqual(str(exc), f'{exctypename}: {expected}')
self.assertExceptionsEqual(exc.__cause__, expected)
if exc.__cause__ is not None:
self.assertIsNotNone(exc.__cause__.__traceback__)
@@ -1241,7 +1246,7 @@ def test_builtin_exceptions(self):
with self.expected_run_failure(expected):
interpreters.run_string(interpid, script)
- def test_custom_exception(self):
+ def test_custom_exception_from___main__(self):
script = dedent("""
class SpamError(Exception):
def __init__(self, q):
@@ -1249,8 +1254,35 @@ def __init__(self, q):
self.q = q
raise SpamError('eggs')
""")
+ expected = Exception(f'SpamError: got {"eggs"}')
+
+ interpid = interpreters.create()
+ with self.assertRaises(interpreters.RunFailedError) as caught:
+ interpreters.run_string(interpid, script)
+ cause = caught.exception.__cause__
+
+ self.assertExceptionsEqual(cause, expected)
+
+ class SpamError(Exception):
+ # The normal Exception.__reduce__() produces a funny result
+ # here. So we have to use a custom __new__().
+ def __new__(cls, q):
+ if type(q) is SpamError:
+ return q
+ return super().__new__(cls, q)
+ def __init__(self, q):
+ super().__init__(f'got {q}')
+ self.q = q
+
+ def test_custom_exception(self):
+ script = dedent("""
+ import test.test__xxsubinterpreters
+ SpamError = test.test__xxsubinterpreters.RunFailedTests.SpamError
+ raise SpamError('eggs')
+ """)
try:
- exec(script, (ns := {'__name__': '__main__'}), ns)
+ ns = {}
+ exec(script, ns, ns)
except Exception as exc:
expected = exc
@@ -1258,14 +1290,17 @@ def __init__(self, q):
with self.expected_run_failure(expected):
interpreters.run_string(interpid, script)
+ class SpamReducedError(Exception):
+ def __init__(self, q):
+ super().__init__(f'got {q}')
+ self.q = q
+ def __reduce__(self):
+ return (type(self), (self.q,), {})
+
def test_custom___reduce__(self):
script = dedent("""
- class SpamError(Exception):
- def __init__(self, q=None):
- super().__init__(f'got {q}')
- self.q = q
- def __reduce__(self):
- return (type(self), (), {'q': self.q})
+ import test.test__xxsubinterpreters
+ SpamError = test.test__xxsubinterpreters.RunFailedTests.SpamReducedError
raise SpamError('eggs')
""")
try:
diff --git a/Modules/_xxsubinterpretersmodule.c b/Modules/_xxsubinterpretersmodule.c
index e5e0e6ec24c870..bd7a08f8018707 100644
--- a/Modules/_xxsubinterpretersmodule.c
+++ b/Modules/_xxsubinterpretersmodule.c
@@ -374,6 +374,7 @@ _objsnapshot_summarize(_objsnapshot *osn, _rawstring *rawbuf, const char *msg)
}
// ...else we'll proxy clsname as-is, so no need to allocate a buffer.
+ // XXX Use __qualname__ somehow?
char *buf = (char *)rawbuf->data;
if (modname != NULL) {
if (msg != NULL) {
--- a PPN by Garber Painting Akron. With Image Size Reduction included!Fetched URL: http://github.com/python/cpython/pull/19768.patch
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy