Skip to content

Commit

Permalink
XML: Fix @parent.setter for finalized instances
Browse files Browse the repository at this point in the history
Meaning XML objects that are not in their instantiation process anymore,
during which the property setter is implictly used
  • Loading branch information
purplezimmermann committed May 16, 2019
1 parent 6d68ca8 commit ec14bc2
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions morexml/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,18 +337,20 @@ def parent(self):
@parent.setter
def parent(self, parentxml):
tag = self.element.tag
# check if tag prefix needs to be exchanged with namespace URI
prefix = self._prefix
if prefix is not None:
uri = self.xmlns().get(prefix)
if uri is None and parentxml is not None:
uri = parentxml.xmlns().get(prefix)
if uri is None:
raise NSLookupError(
"Unknown prefix {!r} in XML tag {!r}"
.format(prefix, ":".join((prefix, tag))))

self.element.tag = "{{{}}}{}".format(uri, tag)
if not tag.startswith('{'):
# check if temporarily stored namespace prefix from instantiation
# needs to be exchanged with namespace URI
prefix = self._prefix
if prefix is not None:
uri = self.xmlns().get(prefix)
if uri is None and parentxml is not None:
uri = parentxml.xmlns().get(prefix)
if uri is None:
raise NSLookupError(
"Unknown prefix {!r} in XML tag {!r}"
.format(prefix, ":".join((prefix, tag))))

self.element.tag = "{{{}}}{}".format(uri, tag)
if parentxml is not None:
self._parent = parentxml
parentxml.sub._list.append(self)
Expand Down

0 comments on commit ec14bc2

Please sign in to comment.