Skip to content

Commit 9e31b39

Browse files
authored
bpo-41031: Match C and Python code formatting of unprintable exceptions and exceptions in the __main__ module. (GH-28139)
1 parent b01fd53 commit 9e31b39

File tree

6 files changed

+55
-14
lines changed

6 files changed

+55
-14
lines changed

Lib/test/test_sys.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,19 +1071,29 @@ def __del__(self):
10711071
self.assertTrue(report.endswith("\n"))
10721072

10731073
def test_original_unraisablehook_exception_qualname(self):
1074+
# See bpo-41031, bpo-45083.
1075+
# Check that the exception is printed with its qualified name
1076+
# rather than just classname, and the module names appears
1077+
# unless it is one of the hard-coded exclusions.
10741078
class A:
10751079
class B:
10761080
class X(Exception):
10771081
pass
10781082

1079-
with test.support.captured_stderr() as stderr, \
1080-
test.support.swap_attr(sys, 'unraisablehook',
1081-
sys.__unraisablehook__):
1082-
expected = self.write_unraisable_exc(
1083-
A.B.X(), "msg", "obj");
1084-
report = stderr.getvalue()
1085-
testName = 'test_original_unraisablehook_exception_qualname'
1086-
self.assertIn(f"{testName}.<locals>.A.B.X", report)
1083+
for moduleName in 'builtins', '__main__', 'some_module':
1084+
with self.subTest(moduleName=moduleName):
1085+
A.B.X.__module__ = moduleName
1086+
with test.support.captured_stderr() as stderr, \
1087+
test.support.swap_attr(sys, 'unraisablehook',
1088+
sys.__unraisablehook__):
1089+
expected = self.write_unraisable_exc(
1090+
A.B.X(), "msg", "obj");
1091+
report = stderr.getvalue()
1092+
self.assertIn(A.B.X.__qualname__, report)
1093+
if moduleName in ['builtins', '__main__']:
1094+
self.assertNotIn(moduleName + '.', report)
1095+
else:
1096+
self.assertIn(moduleName + '.', report)
10871097

10881098
def test_original_unraisablehook_wrong_type(self):
10891099
exc = ValueError(42)

Lib/test/test_traceback.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def __str__(self):
172172
1/0
173173
err = traceback.format_exception_only(X, X())
174174
self.assertEqual(len(err), 1)
175-
str_value = '<unprintable %s object>' % X.__name__
175+
str_value = '<exception str() failed>'
176176
if X.__module__ in ('__main__', 'builtins'):
177177
str_name = X.__qualname__
178178
else:
@@ -1171,19 +1171,45 @@ def test_syntax_error_various_offsets(self):
11711171
exp = "\n".join(expected)
11721172
self.assertEqual(exp, err)
11731173

1174-
def test_format_exception_only_qualname(self):
1174+
def test_exception_qualname(self):
11751175
class A:
11761176
class B:
11771177
class X(Exception):
11781178
def __str__(self):
11791179
return "I am X"
1180-
pass
1180+
11811181
err = self.get_report(A.B.X())
11821182
str_value = 'I am X'
11831183
str_name = '.'.join([A.B.X.__module__, A.B.X.__qualname__])
11841184
exp = "%s: %s\n" % (str_name, str_value)
11851185
self.assertEqual(exp, err)
11861186

1187+
def test_exception_modulename(self):
1188+
class X(Exception):
1189+
def __str__(self):
1190+
return "I am X"
1191+
1192+
for modulename in '__main__', 'builtins', 'some_module':
1193+
X.__module__ = modulename
1194+
with self.subTest(modulename=modulename):
1195+
err = self.get_report(X())
1196+
str_value = 'I am X'
1197+
if modulename in ['builtins', '__main__']:
1198+
str_name = X.__qualname__
1199+
else:
1200+
str_name = '.'.join([X.__module__, X.__qualname__])
1201+
exp = "%s: %s\n" % (str_name, str_value)
1202+
self.assertEqual(exp, err)
1203+
1204+
def test_exception_bad__str__(self):
1205+
class X(Exception):
1206+
def __str__(self):
1207+
1/0
1208+
err = self.get_report(X())
1209+
str_value = '<exception str() failed>'
1210+
str_name = '.'.join([X.__module__, X.__qualname__])
1211+
self.assertEqual(err, f"{str_name}: {str_value}\n")
1212+
11871213

11881214
class PyExcReportingTests(BaseExceptionReportingTests, unittest.TestCase):
11891215
#

Lib/traceback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def _some_str(value):
169169
try:
170170
return str(value)
171171
except:
172-
return '<unprintable %s object>' % type(value).__name__
172+
return '<exception str() failed>'
173173

174174
# --
175175

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Match C and Python code formatting of unprintable exceptions and exceptions in the :mod:`__main__` module.

Python/errors.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern char *strerror(int);
2525
extern "C" {
2626
#endif
2727

28+
_Py_IDENTIFIER(__main__);
2829
_Py_IDENTIFIER(__module__);
2930
_Py_IDENTIFIER(builtins);
3031
_Py_IDENTIFIER(stderr);
@@ -1297,7 +1298,8 @@ write_unraisable_exc_file(PyThreadState *tstate, PyObject *exc_type,
12971298
}
12981299
}
12991300
else {
1300-
if (!_PyUnicode_EqualToASCIIId(modulename, &PyId_builtins)) {
1301+
if (!_PyUnicode_EqualToASCIIId(modulename, &PyId_builtins) &&
1302+
!_PyUnicode_EqualToASCIIId(modulename, &PyId___main__)) {
13011303
if (PyFile_WriteObject(modulename, file, Py_PRINT_RAW) < 0) {
13021304
Py_DECREF(modulename);
13031305
return -1;

Python/pythonrun.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#endif
3636

3737

38+
_Py_IDENTIFIER(__main__);
3839
_Py_IDENTIFIER(builtins);
3940
_Py_IDENTIFIER(excepthook);
4041
_Py_IDENTIFIER(flush);
@@ -974,7 +975,8 @@ print_exception(PyObject *f, PyObject *value)
974975
err = PyFile_WriteString("<unknown>", f);
975976
}
976977
else {
977-
if (!_PyUnicode_EqualToASCIIId(modulename, &PyId_builtins))
978+
if (!_PyUnicode_EqualToASCIIId(modulename, &PyId_builtins) &&
979+
!_PyUnicode_EqualToASCIIId(modulename, &PyId___main__))
978980
{
979981
err = PyFile_WriteObject(modulename, f, Py_PRINT_RAW);
980982
err += PyFile_WriteString(".", f);

0 commit comments

Comments
 (0)