From 34fc9f57e486cf7499e552e0e07a1c7bae5dc1d9 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 18 May 2017 18:37:01 +0300 Subject: [PATCH 1/2] bpo-30399: Get rid of trailing comma in the repr of BaseException. --- Lib/test/test_baseexception.py | 2 +- Lib/test/test_httplib.py | 2 +- Lib/test/test_yield_from.py | 18 +++++++++--------- Misc/NEWS | 3 +++ Objects/exceptions.c | 6 +++++- 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_baseexception.py b/Lib/test/test_baseexception.py index 27d514fe2ee517..c055ee3d83c347 100644 --- a/Lib/test/test_baseexception.py +++ b/Lib/test/test_baseexception.py @@ -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): diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py index 68f6946a3a12b6..5fcb40418145c0 100644 --- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -496,7 +496,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, diff --git a/Lib/test/test_yield_from.py b/Lib/test/test_yield_from.py index d1da838ac715f9..ce21c3df814037 100644 --- a/Lib/test/test_yield_from.py +++ b/Lib/test/test_yield_from.py @@ -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", ]) @@ -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): @@ -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): diff --git a/Misc/NEWS b/Misc/NEWS index c6aed7f48c8dcb..16543c0e11e8cf 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1? Core and Builtins ----------------- +- bpo-30399: Standard repr() of BaseException with a single argument no longer + contains redundant trailing comma. + - bpo-30039: If a KeyboardInterrupt happens when the interpreter is in the middle of resuming a chain of nested 'yield from' or 'await' calls, it's now correctly delivered to the innermost frame. diff --git a/Objects/exceptions.c b/Objects/exceptions.c index 858eff5fc26c34..ff2bee5cf16e79 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -121,7 +121,11 @@ BaseException_repr(PyBaseExceptionObject *self) dot = (const char *) strrchr(name, '.'); if (dot != NULL) name = dot+1; - 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 */ From 17c4b9eee25378cacfcdceee0abeaeb594d59dca Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 12 Oct 2017 22:21:48 +0300 Subject: [PATCH 2/2] Move the NEWS entry to NEWS.d/. --- Misc/NEWS | 3 --- .../Core and Builtins/2017-10-12-22-21-01.bpo-30399.45f1gv.rst | 2 ++ 2 files changed, 2 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2017-10-12-22-21-01.bpo-30399.45f1gv.rst diff --git a/Misc/NEWS b/Misc/NEWS index 16543c0e11e8cf..c6aed7f48c8dcb 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,9 +10,6 @@ What's New in Python 3.7.0 alpha 1? Core and Builtins ----------------- -- bpo-30399: Standard repr() of BaseException with a single argument no longer - contains redundant trailing comma. - - bpo-30039: If a KeyboardInterrupt happens when the interpreter is in the middle of resuming a chain of nested 'yield from' or 'await' calls, it's now correctly delivered to the innermost frame. diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-10-12-22-21-01.bpo-30399.45f1gv.rst b/Misc/NEWS.d/next/Core and Builtins/2017-10-12-22-21-01.bpo-30399.45f1gv.rst new file mode 100644 index 00000000000000..ccd1575ec949a7 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2017-10-12-22-21-01.bpo-30399.45f1gv.rst @@ -0,0 +1,2 @@ +Standard repr() of BaseException with a single argument no longer contains +redundant trailing comma.