Skip to content

Commit

Permalink
Avoid circular reference by weak referencing kid's parent
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrilPeponnet committed Sep 22, 2014
1 parent cae7df0 commit 96eff7d
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions xmpp/simplexml.py
Expand Up @@ -18,6 +18,7 @@
I'm personally using it in many other separate projects. It is designed to be as standalone as possible.""" I'm personally using it in many other separate projects. It is designed to be as standalone as possible."""


import xml.parsers.expat import xml.parsers.expat
import weakref


def XMLescape(txt): def XMLescape(txt):
"""Returns provided string with symbols & < > " replaced by their respective XML entities.""" """Returns provided string with symbols & < > " replaced by their respective XML entities."""
Expand Down Expand Up @@ -160,8 +161,8 @@ def addChild(self, name=None, attrs={}, payload=[], namespace=None, node=None):
raise AttributeError("Use namespace=x instead of attrs={'xmlns':x}") raise AttributeError("Use namespace=x instead of attrs={'xmlns':x}")
if node: if node:
newnode=node newnode=node
node.parent = self node.parent = weakref.proxy(self)
else: newnode=Node(tag=name, parent=self, attrs=attrs, payload=payload) else: newnode=Node(tag=name, parent=weakref.proxy(self), attrs=attrs, payload=payload)
if namespace: if namespace:
newnode.setNamespace(namespace) newnode.setNamespace(namespace)
self.kids.append(newnode) self.kids.append(newnode)
Expand Down Expand Up @@ -267,7 +268,7 @@ def setNamespace(self, namespace):
def setParent(self, node): def setParent(self, node):
""" Sets node's parent to "node". WARNING: do not checks if the parent already present """ Sets node's parent to "node". WARNING: do not checks if the parent already present
and not removes the node from the list of childs of previous parent. """ and not removes the node from the list of childs of previous parent. """
self.parent = node self.parent = weakref.proxy(node) if node else None
def setPayload(self,payload,add=0): def setPayload(self,payload,add=0):
""" Sets node payload according to the list specified. WARNING: completely replaces all node's """ Sets node payload according to the list specified. WARNING: completely replaces all node's
previous content. If you wish just to add child or CDATA - use addData or addChild methods. """ previous content. If you wish just to add child or CDATA - use addData or addChild methods. """
Expand Down

0 comments on commit 96eff7d

Please sign in to comment.