summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_traceback.py
diff options
context:
space:
mode:
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>2021-09-05 15:54:13 (GMT)
committerGitHub <noreply@github.com>2021-09-05 15:54:13 (GMT)
commit9e31b3952f6101ef71ec029481b972169ab0e0f1 (patch)
treeb325ae97e170eafbdb9b58a5cc1755446c5667b5 /Lib/test/test_traceback.py
parentb01fd533fef78b088674bad73267b89bea98e904 (diff)
downloadcpython-9e31b3952f6101ef71ec029481b972169ab0e0f1.zip
cpython-9e31b3952f6101ef71ec029481b972169ab0e0f1.tar.gz
cpython-9e31b3952f6101ef71ec029481b972169ab0e0f1.tar.bz2
bpo-41031: Match C and Python code formatting of unprintable exceptions and exceptions in the __main__ module. (GH-28139)
Diffstat (limited to 'Lib/test/test_traceback.py')
-rw-r--r--Lib/test/test_traceback.py32
1 files changed, 29 insertions, 3 deletions
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 949adef..363165d 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -172,7 +172,7 @@ class TracebackCases(unittest.TestCase):
1/0
err = traceback.format_exception_only(X, X())
self.assertEqual(len(err), 1)
- str_value = '<unprintable %s object>' % X.__name__
+ str_value = '<exception str() failed>'
if X.__module__ in ('__main__', 'builtins'):
str_name = X.__qualname__
else:
@@ -1171,19 +1171,45 @@ class BaseExceptionReportingTests:
exp = "\n".join(expected)
self.assertEqual(exp, err)
- def test_format_exception_only_qualname(self):
+ def test_exception_qualname(self):
class A:
class B:
class X(Exception):
def __str__(self):
return "I am X"
- pass
+
err = self.get_report(A.B.X())
str_value = 'I am X'
str_name = '.'.join([A.B.X.__module__, A.B.X.__qualname__])
exp = "%s: %s\n" % (str_name, str_value)
self.assertEqual(exp, err)
+ def test_exception_modulename(self):
+ class X(Exception):
+ def __str__(self):
+ return "I am X"
+
+ for modulename in '__main__', 'builtins', 'some_module':
+ X.__module__ = modulename
+ with self.subTest(modulename=modulename):
+ err = self.get_report(X())
+ str_value = 'I am X'
+ if modulename in ['builtins', '__main__']:
+ str_name = X.__qualname__
+ else:
+ str_name = '.'.join([X.__module__, X.__qualname__])
+ exp = "%s: %s\n" % (str_name, str_value)
+ self.assertEqual(exp, err)
+
+ def test_exception_bad__str__(self):
+ class X(Exception):
+ def __str__(self):
+ 1/0
+ err = self.get_report(X())
+ str_value = '<exception str() failed>'
+ str_name = '.'.join([X.__module__, X.__qualname__])
+ self.assertEqual(err, f"{str_name}: {str_value}\n")
+
class PyExcReportingTests(BaseExceptionReportingTests, unittest.TestCase):
#