Skip to content

bpo-30399: Get rid of trailing comma in the repr of BaseException. #1650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/test/test_baseexception.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_interface_single_arg(self):
exc = Exception(arg)
results = ([len(exc.args), 1], [exc.args[0], arg],
[str(exc), str(arg)],
[repr(exc), exc.__class__.__name__ + repr(exc.args)])
[repr(exc), '%s(%r)' % (exc.__class__.__name__, arg)])
self.interface_test_driver(results)

def test_interface_multi_arg(self):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def test_status_lines(self):

def test_bad_status_repr(self):
exc = client.BadStatusLine('')
self.assertEqual(repr(exc), '''BadStatusLine("\'\'",)''')
self.assertEqual(repr(exc), '''BadStatusLine("''")''')

def test_partial_reads(self):
# if we have Content-Length, HTTPResponse knows when to close itself,
Expand Down
18 changes: 9 additions & 9 deletions Lib/test/test_yield_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def g2(v = None):
"Yielded g2 spam",
"Yielded g2 more spam",
"Finishing g2",
"g2 returned StopIteration(3,)",
"g2 returned StopIteration(3)",
"Yielded g1 eggs",
"Finishing g1",
])
Expand Down Expand Up @@ -696,15 +696,15 @@ def g(r):
"g starting",
"f resuming g",
"g returning 1",
"f caught StopIteration(1,)",
"f caught StopIteration(1)",
"g starting",
"f resuming g",
"g returning (2,)",
"f caught StopIteration((2,),)",
"f caught StopIteration((2,))",
"g starting",
"f resuming g",
"g returning StopIteration(3,)",
"f caught StopIteration(StopIteration(3,),)",
"g returning StopIteration(3)",
"f caught StopIteration(StopIteration(3))",
])

def test_send_and_return_with_value(self):
Expand Down Expand Up @@ -741,17 +741,17 @@ def g(r):
"f sending spam to g",
"g received 'spam'",
"g returning 1",
'f caught StopIteration(1,)',
'f caught StopIteration(1)',
'g starting',
'f sending spam to g',
"g received 'spam'",
'g returning (2,)',
'f caught StopIteration((2,),)',
'f caught StopIteration((2,))',
'g starting',
'f sending spam to g',
"g received 'spam'",
'g returning StopIteration(3,)',
'f caught StopIteration(StopIteration(3,),)'
'g returning StopIteration(3)',
'f caught StopIteration(StopIteration(3))'
])

def test_catching_exception_from_subgen_and_returning(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Standard repr() of BaseException with a single argument no longer contains
redundant trailing comma.
6 changes: 5 additions & 1 deletion Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ static PyObject *
BaseException_repr(PyBaseExceptionObject *self)
{
const char *name = _PyType_Name(Py_TYPE(self));
return PyUnicode_FromFormat("%s%R", name, self->args);
if (PyTuple_GET_SIZE(self->args) == 1)
return PyUnicode_FromFormat("%s(%R)", name,
PyTuple_GET_ITEM(self->args, 0));
else
return PyUnicode_FromFormat("%s%R", name, self->args);
}

/* Pickling support */
Expand Down