Skip to content

Commit

Permalink
Fix missing text in element error for Python (#443)
Browse files Browse the repository at this point in the history
Since the XMLPullParser doesn't guarantee that the attributes of a start
events are populated. When the parser has not yet encountered the text
event, this attribute will be None. Instead we additionally have to
check end events, as well.
  • Loading branch information
empwilli authored Feb 14, 2024
1 parent 14ef14f commit 330f391
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 31 deletions.
42 changes: 26 additions & 16 deletions aas_core_codegen/python/xmlization/_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2289,7 +2289,7 @@ def _raise_if_has_tail_or_attrib(
def _read_end_element(
{II}element: Element,
{II}iterator: Iterator[Tuple[str, Element]]
) -> None:
) -> Element:
{I}\"\"\"
{I}Read the end element corresponding to the start :paramref:`element`
{I}from :paramref:`iterator`.
Expand All @@ -2315,7 +2315,9 @@ def _read_end_element(
{III}f"but got the event {{next_event!r}} and element {{next_element.tag!r}}"
{II})
{I}_raise_if_has_tail_or_attrib(next_element)"""
{I}_raise_if_has_tail_or_attrib(next_element)
{I}return next_element"""
),
Stripped(
f"""\
Expand All @@ -2339,18 +2341,21 @@ def _read_text_from_element(
{I}\"\"\"
{I}_raise_if_has_tail_or_attrib(element)
{I}if element.text is None:
{II}raise DeserializationException(
{III}"Expected an element with text, but got an element with no text."
{II})
{I}text = element.text
{I}_read_end_element(
{I}end_element = _read_end_element(
{II}element,
{II}iterator
{II}iterator,
{I})
{I}if text is None:
{II}if end_element.text is None:
{III}raise DeserializationException(
{IIII}"Expected an element with text, but got an element with no text."
{III})
{II}text = end_element.text
{I}return text"""
),
Stripped(
Expand Down Expand Up @@ -2496,18 +2501,23 @@ def _read_str_from_element_text(
{I}# the ``element`` to contain *some* text. In contrast, this function
{I}# can also deal with empty text, in which case it returns an empty string.
{I}_raise_if_has_tail_or_attrib(element)
{I}result = (
{II}element.text
{II}if element.text is not None
{II}else ""
{I})
{I}text = element.text
{I}_read_end_element(
{I}end_element = _read_end_element(
{II}element,
{II}iterator
{I})
{I}if text is None:
{II}text = end_element.text
{I}_raise_if_has_tail_or_attrib(element)
{I}result = (
{II}text
{II}if text is not None
{II}else ""
{I})
{I}return result"""
),
Stripped(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10261,7 +10261,7 @@ def _raise_if_has_tail_or_attrib(
def _read_end_element(
element: Element,
iterator: Iterator[Tuple[str, Element]]
) -> None:
) -> Element:
"""
Read the end element corresponding to the start :paramref:`element`
from :paramref:`iterator`.
Expand Down Expand Up @@ -10289,6 +10289,8 @@ def _read_end_element(

_raise_if_has_tail_or_attrib(next_element)

return next_element


def _read_text_from_element(
element: Element,
Expand All @@ -10310,18 +10312,21 @@ def _read_text_from_element(
"""
_raise_if_has_tail_or_attrib(element)

if element.text is None:
raise DeserializationException(
"Expected an element with text, but got an element with no text."
)

text = element.text

_read_end_element(
end_element = _read_end_element(
element,
iterator
iterator,
)

if text is None:
if end_element.text is None:
raise DeserializationException(
"Expected an element with text, but got an element with no text."
)

text = end_element.text

return text


Expand Down Expand Up @@ -10461,18 +10466,23 @@ def _read_str_from_element_text(
# the ``element`` to contain *some* text. In contrast, this function
# can also deal with empty text, in which case it returns an empty string.

_raise_if_has_tail_or_attrib(element)
result = (
element.text
if element.text is not None
else ""
)
text = element.text

_read_end_element(
end_element = _read_end_element(
element,
iterator
)

if text is None:
text = end_element.text

_raise_if_has_tail_or_attrib(element)
result = (
text
if text is not None
else ""
)

return result


Expand Down

0 comments on commit 330f391

Please sign in to comment.