From 7f78c6fc18e71f61646046fbc44cabd8aab924b3 Mon Sep 17 00:00:00 2001 From: "bar.harel" Date: Thu, 22 Aug 2024 09:04:42 +0300 Subject: [PATCH 1/9] et fix --- Modules/_elementtree.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 3818e20b4f0f28..6a3821f5acafe1 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -1213,12 +1213,8 @@ _elementtree_Element_extend_impl(ElementObject *self, PyTypeObject *cls, PyObject* seq; Py_ssize_t i; - seq = PySequence_Fast(elements, ""); + seq = PySequence_Fast(elements, "argument must be an iterable"); if (!seq) { - PyErr_Format( - PyExc_TypeError, - "expected sequence, not \"%.200s\"", Py_TYPE(elements)->tp_name - ); return NULL; } @@ -1918,12 +1914,8 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value) } /* A new slice is actually being assigned */ - seq = PySequence_Fast(value, ""); + seq = PySequence_Fast(value, "assignment expects an iterable"); if (!seq) { - PyErr_Format( - PyExc_TypeError, - "expected sequence, not \"%.200s\"", Py_TYPE(value)->tp_name - ); return -1; } newlen = PySequence_Fast_GET_SIZE(seq); From 2adf84441b68feb17e949a7dcdf3f5aa6385764b Mon Sep 17 00:00:00 2001 From: "bar.harel" Date: Thu, 22 Aug 2024 09:18:21 +0300 Subject: [PATCH 2/9] added test --- Lib/test/test_xml_etree.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 930501633d1f38..96a8b528aed669 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -2423,6 +2423,13 @@ def test_39495_treebuilder_start(self): self.assertRaises(TypeError, ET.TreeBuilder().start, "tag") self.assertRaises(TypeError, ET.TreeBuilder().start, "tag", None) + def test_123213_correct_extend_exception(self): + # Does not hide the internal exception when extending the element + self.assertRaises(ZeroDivisionError, ET.Element('tag').extend, + (1/0 for i in range(2))) + # Still raises the TypeError when extending with a non-iterable + self.assertRaises(TypeError, ET.Element('tag').extend, None) + # -------------------------------------------------------------------- From 4d93a358736299d36cac78245eee8c81b256f6cc Mon Sep 17 00:00:00 2001 From: "bar.harel" Date: Thu, 22 Aug 2024 09:29:47 +0300 Subject: [PATCH 3/9] tests --- Lib/test/test_xml_etree.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 96a8b528aed669..e9274bfebd2e10 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -2423,7 +2423,7 @@ def test_39495_treebuilder_start(self): self.assertRaises(TypeError, ET.TreeBuilder().start, "tag") self.assertRaises(TypeError, ET.TreeBuilder().start, "tag", None) - def test_123213_correct_extend_exception(self): + def test_issue123213_correct_extend_exception(self): # Does not hide the internal exception when extending the element self.assertRaises(ZeroDivisionError, ET.Element('tag').extend, (1/0 for i in range(2))) @@ -3755,6 +3755,15 @@ def test_setslice_negative_steps(self): e[1::-sys.maxsize<<64] = [ET.Element('d')] self.assertEqual(self._subelem_tags(e), ['a0', 'd', 'a2', 'a3']) + def test_issue123213_setslice_exception(self): + e = ET.Element('tag') + # Does not hide the internal exception when assigning to the element + with self.assertRaises(ZeroDivisionError): + e[:1] = (1/0 for i in range(2)) + + # Still raises the TypeError when assigning with a non-iterable + with self.assertRaises(TypeError): + e[:1] = None class IOTest(unittest.TestCase): def test_encoding(self): From 337423f9a392f6e8dbe861e8241887aacee3e870 Mon Sep 17 00:00:00 2001 From: "bar.harel" Date: Thu, 22 Aug 2024 09:40:53 +0300 Subject: [PATCH 4/9] news --- .../Library/2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst diff --git a/Misc/NEWS.d/next/Library/2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst b/Misc/NEWS.d/next/Library/2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst new file mode 100644 index 00000000000000..364fda3556c32c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst @@ -0,0 +1,3 @@ +:meth:`xml.etree.ElementTree.Element.extend` and Element assignment no +longer hide the internal exception if an erronous generator is passed. Patch +by Bar Harel From 2077a1439c9f3081b7cc3ad958e47477e9515842 Mon Sep 17 00:00:00 2001 From: "bar.harel" Date: Thu, 22 Aug 2024 09:47:16 +0300 Subject: [PATCH 5/9] removed whitespace --- Lib/test/test_xml_etree.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index e9274bfebd2e10..0d03b849c02f7c 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -2427,6 +2427,7 @@ def test_issue123213_correct_extend_exception(self): # Does not hide the internal exception when extending the element self.assertRaises(ZeroDivisionError, ET.Element('tag').extend, (1/0 for i in range(2))) + # Still raises the TypeError when extending with a non-iterable self.assertRaises(TypeError, ET.Element('tag').extend, None) @@ -3760,7 +3761,7 @@ def test_issue123213_setslice_exception(self): # Does not hide the internal exception when assigning to the element with self.assertRaises(ZeroDivisionError): e[:1] = (1/0 for i in range(2)) - + # Still raises the TypeError when assigning with a non-iterable with self.assertRaises(TypeError): e[:1] = None From 5d0ab1a5367466569ed9581fd5b0018ecd99b634 Mon Sep 17 00:00:00 2001 From: Bar Harel Date: Thu, 22 Aug 2024 11:55:16 +0300 Subject: [PATCH 6/9] Update 2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst --- .../next/Library/2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst b/Misc/NEWS.d/next/Library/2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst index 364fda3556c32c..08b5a496ac185a 100644 --- a/Misc/NEWS.d/next/Library/2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst +++ b/Misc/NEWS.d/next/Library/2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst @@ -1,3 +1,3 @@ :meth:`xml.etree.ElementTree.Element.extend` and Element assignment no longer hide the internal exception if an erronous generator is passed. Patch -by Bar Harel +by Bar Harel. From 4e80848db60f0076e886ec1d1ab3ff8284414564 Mon Sep 17 00:00:00 2001 From: Bar Harel Date: Thu, 22 Aug 2024 13:53:42 +0300 Subject: [PATCH 7/9] Changed docs --- Doc/library/xml.etree.elementtree.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/xml.etree.elementtree.rst b/Doc/library/xml.etree.elementtree.rst index 51bf88313e5b7a..9fad463d936660 100644 --- a/Doc/library/xml.etree.elementtree.rst +++ b/Doc/library/xml.etree.elementtree.rst @@ -971,7 +971,7 @@ Element Objects .. method:: extend(subelements) - Appends *subelements* from a sequence object with zero or more elements. + Appends *subelements* from an iterable of elements. Raises :exc:`TypeError` if a subelement is not an :class:`Element`. .. versionadded:: 3.2 From 93c5b4a0ee48345127f028f5aeaaec949e880782 Mon Sep 17 00:00:00 2001 From: Bar Harel Date: Thu, 22 Aug 2024 23:34:30 +0300 Subject: [PATCH 8/9] Update Modules/_elementtree.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Modules/_elementtree.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 6a3821f5acafe1..ec999582d2fb9d 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -1213,7 +1213,7 @@ _elementtree_Element_extend_impl(ElementObject *self, PyTypeObject *cls, PyObject* seq; Py_ssize_t i; - seq = PySequence_Fast(elements, "argument must be an iterable"); + seq = PySequence_Fast(elements, "'elements' must be an iterable"); if (!seq) { return NULL; } From 555d5ef1dc84de67a3535ea051aaf499efb459af Mon Sep 17 00:00:00 2001 From: "bar.harel" Date: Thu, 22 Aug 2024 23:34:56 +0300 Subject: [PATCH 9/9] tests --- Lib/test/test_xml_etree.py | 15 +++++++++++++++ ...2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst | 6 +++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 0d03b849c02f7c..ae06a9cc11855f 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -2431,6 +2431,14 @@ def test_issue123213_correct_extend_exception(self): # Still raises the TypeError when extending with a non-iterable self.assertRaises(TypeError, ET.Element('tag').extend, None) + # Preserves the TypeError message when extending with a generator + def f(): + raise TypeError("mymessage") + + self.assertRaisesRegex( + TypeError, 'mymessage', + ET.Element('tag').extend, (f() for i in range(2))) + # -------------------------------------------------------------------- @@ -3766,6 +3774,13 @@ def test_issue123213_setslice_exception(self): with self.assertRaises(TypeError): e[:1] = None + # Preserve the original TypeError message when assigning. + def f(): + raise TypeError("mymessage") + + with self.assertRaisesRegex(TypeError, 'mymessage'): + e[:1] = (f() for i in range(2)) + class IOTest(unittest.TestCase): def test_encoding(self): # Test encoding issues. diff --git a/Misc/NEWS.d/next/Library/2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst b/Misc/NEWS.d/next/Library/2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst index 08b5a496ac185a..6bbd194b916ec4 100644 --- a/Misc/NEWS.d/next/Library/2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst +++ b/Misc/NEWS.d/next/Library/2024-08-22-09-37-48.gh-issue-123213.owmXnP.rst @@ -1,3 +1,3 @@ -:meth:`xml.etree.ElementTree.Element.extend` and Element assignment no -longer hide the internal exception if an erronous generator is passed. Patch -by Bar Harel. +:meth:`xml.etree.ElementTree.Element.extend` and +:class:`~xml.etree.ElementTree.Element` assignment no longer hide the internal +exception if an erronous generator is passed. Patch by Bar Harel.