From 025c9808bc8045e04a7d7441638a9619e955fbaf Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 6 Apr 2017 01:29:49 +0300 Subject: [PATCH 1/6] bpo-29999: repr() of ImportError now contains attributes name and path. --- Lib/test/test_exceptions.py | 30 +++++++++++++++++++++ Misc/NEWS | 2 ++ Objects/exceptions.c | 53 ++++++++++++++++++++++++++++++++----- 3 files changed, 78 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 2cac6c530ae7d1..34c440064b4f48 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -1126,6 +1126,36 @@ def test_non_str_argument(self): exc = ImportError(arg) self.assertEqual(str(arg), str(exc)) + def test_repr(self): + exc = ImportError() + self.assertEqual(repr(exc), "ImportError()") + + exc = ImportError('test') + self.assertEqual(repr(exc), "ImportError('test')") + + exc = ImportError('test', 'case') + self.assertEqual(repr(exc), "ImportError('test', 'case')") + + exc = ImportError(name='somemodule') + self.assertEqual(repr(exc), "ImportError(name='somemodule')") + + exc = ImportError('test', name='somemodule') + self.assertEqual(repr(exc), "ImportError('test', name='somemodule')") + + exc = ImportError(path='somepath') + self.assertEqual(repr(exc), "ImportError(path='somepath')") + + exc = ImportError('test', path='somepath') + self.assertEqual(repr(exc), "ImportError('test', path='somepath')") + + exc = ImportError(name='somename', path='somepath') + self.assertEqual(repr(exc), + "ImportError(name='somename', path='somepath')") + + exc = ImportError('test', name='somename', path='somepath') + self.assertEqual(repr(exc), + "ImportError('test', name='somename', path='somepath')") + if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS b/Misc/NEWS index 396ada182e197b..23ae8b2ae78552 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,8 @@ What's New in Python 3.7.0 alpha 1? Core and Builtins ----------------- +- bpo-29999: repr() of ImportError now contains attributes name and path. + - bpo-29949: Fix memory usage regression of set and frozenset object. - bpo-29935: Fixed error messages in the index() method of tuple, list and deque diff --git a/Objects/exceptions.c b/Objects/exceptions.c index e5e59b4c8bc5ee..2786393c3ba245 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 */ @@ -682,6 +686,30 @@ ImportError_str(PyImportErrorObject *self) } } +static PyObject * +ImportError_repr(PyImportErrorObject *self) +{ + int hasargs = PyTuple_GET_SIZE(((PyBaseExceptionObject *)self)->args) != 0; + PyObject *r = BaseException_repr((PyBaseExceptionObject *)self); + if (r && (self->name || self->path)) { + /* remove ')' */ + Py_SETREF(r, PyUnicode_Substring(r, 0, PyUnicode_GET_LENGTH(r) - 1)); + if (r && self->name) { + Py_SETREF(r, PyUnicode_FromFormat("%U%sname=%R", + r, hasargs ? ", " : "", self->name)); + hasargs = 1; + } + if (r && self->path) { + Py_SETREF(r, PyUnicode_FromFormat("%U%spath=%R", + r, hasargs ? ", " : "", self->path)); + } + if (r) { + Py_SETREF(r, PyUnicode_FromFormat("%U)", r)); + } + } + return r; +} + static PyMemberDef ImportError_members[] = { {"msg", T_OBJECT, offsetof(PyImportErrorObject, msg), 0, PyDoc_STR("exception message")}, @@ -696,12 +724,23 @@ static PyMethodDef ImportError_methods[] = { {NULL} }; -ComplexExtendsException(PyExc_Exception, ImportError, - ImportError, 0 /* new */, - ImportError_methods, ImportError_members, - 0 /* getset */, ImportError_str, - "Import can't find module, or can't find name in " - "module."); +static PyTypeObject _PyExc_ImportError = { + PyVarObject_HEAD_INIT(NULL, 0) + "ImportError", + sizeof(PyImportErrorObject), 0, + (destructor)ImportError_dealloc, 0, 0, 0, 0, + (reprfunc)ImportError_repr, 0, 0, 0, 0, 0, + (reprfunc)ImportError_str, 0, 0, 0, + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, + PyDoc_STR("Import can't find module, or can't find name in " + "module."), + (traverseproc)ImportError_traverse, + (inquiry)ImportError_clear, 0, 0, 0, 0, ImportError_methods, + ImportError_members, 0, &_PyExc_Exception, + 0, 0, 0, offsetof(PyImportErrorObject, dict), + (initproc)ImportError_init, +}; +PyObject *PyExc_ImportError = (PyObject *)&_PyExc_ImportError; /* * ModuleNotFoundError extends ImportError From 318a5d8b7a28f137363ebe50530107cc6b8f2f30 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 7 Apr 2017 10:04:39 +0300 Subject: [PATCH 2/6] Fixed tests for repr() of exceptions with single argument. --- Lib/test/test_baseexception.py | 2 +- Lib/test/test_httplib.py | 2 +- Lib/test/test_yield_from.py | 18 +++++++++--------- 3 files changed, 11 insertions(+), 11 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): From 60cde7b1ef5d8968463acdd29c134586c771407f Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Sun, 12 Feb 2023 22:12:10 +0400 Subject: [PATCH 3/6] Use GitHub Actions version of Bedevere --- .github/workflows/trigger-bots.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .github/workflows/trigger-bots.yml diff --git a/.github/workflows/trigger-bots.yml b/.github/workflows/trigger-bots.yml new file mode 100644 index 00000000000000..995ca2658bf173 --- /dev/null +++ b/.github/workflows/trigger-bots.yml @@ -0,0 +1,20 @@ +name: Bots + +on: + # PR synchronization is a push into a branch associated with the PR + pull_request: + types: [opened, edited, synchronize, labeled, unlabeled] + issue_comment: + types: [created, edited] + pull_request_review_comment: + types: [created, edited] + +jobs: + call_bedevere: + name: Call Bedevere + runs-on: ubuntu-latest + steps: + - uses: arhadthedev/bedevere@c51d1d2d5c7a9cec31ce16af209f42a5905070dd + with: + token: ${{ secrets.BEDEVERE_PAT }} + event: ${{ toJSON(github.event) }} From dcaccba58f418ea776820ab7a4b0e6b7239af202 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Mon, 13 Feb 2023 21:13:14 +0400 Subject: [PATCH 4/6] Take a news entry from an outdated position --- Misc/NEWS | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Misc/NEWS b/Misc/NEWS index 76b6f923fb6722..9b5150c834a854 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,10 +10,6 @@ What's New in Python 3.7.0 alpha 1? Core and Builtins ----------------- -- bpo-29999: repr() of ImportError now contains attributes name and path. - Standard repr() of BaseException with single argument no longer contains - trailing comma. - - bpo-29914: Fixed default implementations of __reduce__ and __reduce_ex__(). object.__reduce__() no longer takes arguments, object.__reduce_ex__() now requires one argument. From 57f5024cb8a3161bf5fe74c7940e88a60221ae3e Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Mon, 13 Feb 2023 21:27:04 +0400 Subject: [PATCH 5/6] Add the news entry back --- .../2023-02-13-21-26-54.gh-issue-74185.Sk3O7m.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-02-13-21-26-54.gh-issue-74185.Sk3O7m.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-02-13-21-26-54.gh-issue-74185.Sk3O7m.rst b/Misc/NEWS.d/next/Core and Builtins/2023-02-13-21-26-54.gh-issue-74185.Sk3O7m.rst new file mode 100644 index 00000000000000..3a992bfe6b8413 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-02-13-21-26-54.gh-issue-74185.Sk3O7m.rst @@ -0,0 +1,2 @@ +:meth:`repr` of :class:`ImportError` now contains attributes name and path. +Patch by Serhiy Storchaka. From 918b68d35c0a6edb38d641e9556269639f77e2e9 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Mon, 13 Feb 2023 21:28:52 +0400 Subject: [PATCH 6/6] Remove an accidentally included untracked file --- .github/workflows/trigger-bots.yml | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 .github/workflows/trigger-bots.yml diff --git a/.github/workflows/trigger-bots.yml b/.github/workflows/trigger-bots.yml deleted file mode 100644 index 995ca2658bf173..00000000000000 --- a/.github/workflows/trigger-bots.yml +++ /dev/null @@ -1,20 +0,0 @@ -name: Bots - -on: - # PR synchronization is a push into a branch associated with the PR - pull_request: - types: [opened, edited, synchronize, labeled, unlabeled] - issue_comment: - types: [created, edited] - pull_request_review_comment: - types: [created, edited] - -jobs: - call_bedevere: - name: Call Bedevere - runs-on: ubuntu-latest - steps: - - uses: arhadthedev/bedevere@c51d1d2d5c7a9cec31ce16af209f42a5905070dd - with: - token: ${{ secrets.BEDEVERE_PAT }} - event: ${{ toJSON(github.event) }} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy