Skip to content
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

ElementTree.Element.extend: bad error message when error occurs in generator #89849

Open
goodmami mannequin opened this issue Nov 2, 2021 · 1 comment
Open

ElementTree.Element.extend: bad error message when error occurs in generator #89849

goodmami mannequin opened this issue Nov 2, 2021 · 1 comment
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes topic-XML type-bug An unexpected behavior, bug, or error

Comments

@goodmami
Copy link
Mannequin

goodmami mannequin commented Nov 2, 2021

BPO 45686
Nosy @goodmami

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2021-11-02.00:50:51.579>
labels = ['expert-XML', 'type-bug', '3.9', '3.10', '3.11']
title = 'ElementTree.Element.extend: bad error message when error occurs in generator'
updated_at = <Date 2022-01-15.16:31:14.321>
user = 'https://github.com/goodmami'

bugs.python.org fields:

activity = <Date 2022-01-15.16:31:14.321>
actor = 'iritkatriel'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['XML']
creation = <Date 2021-11-02.00:50:51.579>
creator = 'goodmami'
dependencies = []
files = []
hgrepos = []
issue_num = 45686
keywords = []
message_count = 1.0
messages = ['405476']
nosy_count = 1.0
nosy_names = ['goodmami']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue45686'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

@goodmami
Copy link
Mannequin Author

goodmami mannequin commented Nov 2, 2021

Both the Python and C versions of xml.etree.ElementTree allow Element.extend() to accept a sequence or an iterable, such as a generator expression (although the docs only mention "sequence"; that's a separate problem):

>> from xml.etree import ElementTree as ET
>> elem = ET.Element('root')
>> elem.extend(ET.Element('child') for _ in range(3))
>> pyelem = ET._Element_Py('root')
>> pyelem.extend(ET._Element_Py('child') for _ in range(3))

If the generator expression raises an Exception (for instance, by omitting the tag name as below), then the Python implementation passes up the error:

>>> pyelem.extend(ET._Element_Py() for _ in range(3))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/xml/etree/ElementTree.py", line 253, in extend
    for element in elements:
  File "<stdin>", line 1, in <genexpr>
TypeError: __init__() missing 1 required positional argument: 'tag'

But the C implementation hides it with a misleading exception of its own:

>>> elem.extend(ET.Element() for _ in range(3))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected sequence, not "generator"

Is it possible to pass up the original exception, as in the Python implementation? In the C code, the exception is raised when PySequence_Fast returns something false:

    seq = PySequence_Fast(elements, "");
    if (!seq) {
        PyErr_Format(
            PyExc_TypeError,
            "expected sequence, not \"%.200s\"", Py_TYPE(elements)->tp_name
            );
        return NULL;
    }

Otherwise, while it would be good if the error message didn't incorrectly state that Element.extend() requires a sequence and not a generator, it currently leads the programmer to the actual error. That is, when the programmer tries to make the argument a sequence by creating a list, the error will be propagated before Element.extend() is called:

>>> elem.extend([ET.Element() for _ in range(3)])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <listcomp>
TypeError: Element() takes at least 1 argument (0 given)

@goodmami goodmami mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.10 only security fixes 3.11 only security fixes 3.9 only security fixes topic-XML type-bug An unexpected behavior, bug, or error labels Nov 2, 2021
@iritkatriel iritkatriel removed 3.7 (EOL) end of life 3.8 only security fixes labels Jan 15, 2022
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes topic-XML type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant