mirror of
https://github.com/virt-manager/virt-manager.git
synced 2024-10-27 10:25:25 +03:00
xmlbuilder: Simplify free'ing xpath contexts
This commit is contained in:
parent
02f35628fb
commit
ed826315a5
@ -39,6 +39,16 @@ class _DocCleanupWrapper(object):
|
||||
self._doc.freeDoc()
|
||||
|
||||
|
||||
class _CtxCleanupWrapper(object):
|
||||
def __init__(self, ctx):
|
||||
self._ctx = ctx
|
||||
def __del__(self):
|
||||
self._ctx.xpathFreeContext()
|
||||
self._ctx = None
|
||||
def __getattr__(self, attrname):
|
||||
return getattr(self._ctx, attrname)
|
||||
|
||||
|
||||
def _tuplify_lists(*args):
|
||||
"""
|
||||
Similar to zip(), but use None if lists aren't long enough, and
|
||||
@ -508,9 +518,6 @@ class XMLBuilder(object):
|
||||
if parsexml or parsexmlnode:
|
||||
self._parsexml(parsexml, parsexmlnode)
|
||||
|
||||
def __del__(self):
|
||||
if hasattr(self, "_xml_ctx") and self._xml_ctx:
|
||||
self._xml_ctx.xpathFreeContext()
|
||||
|
||||
def _cache(self):
|
||||
"""
|
||||
@ -522,9 +529,6 @@ class XMLBuilder(object):
|
||||
|
||||
|
||||
def copy(self):
|
||||
# Otherwise we can double free XML info
|
||||
if self._is_parse():
|
||||
return self
|
||||
self._cache()
|
||||
return copy.copy(self)
|
||||
|
||||
@ -553,10 +557,8 @@ class XMLBuilder(object):
|
||||
|
||||
def _set_xml_context(self):
|
||||
doc = self._xml_node.doc
|
||||
ctx = doc.xpathNewContext()
|
||||
ctx = _CtxCleanupWrapper(doc.xpathNewContext())
|
||||
ctx.setContextNode(self._xml_node)
|
||||
if self._xml_ctx:
|
||||
self._xml_ctx.xpathFreeContext()
|
||||
self._xml_ctx = ctx
|
||||
|
||||
def _parsexml(self, xml, node):
|
||||
@ -587,8 +589,6 @@ class XMLBuilder(object):
|
||||
finally:
|
||||
self._xml_root_doc = None
|
||||
self._xml_node = None
|
||||
if self._xml_ctx:
|
||||
self._xml_ctx.xpathFreeContext()
|
||||
self._xml_ctx = None
|
||||
|
||||
def set_defaults(self):
|
||||
|
Loading…
Reference in New Issue
Block a user