Skip to content

Commit 96eff7d

Browse files
committed
Avoid circular reference by weak referencing kid's parent
1 parent cae7df0 commit 96eff7d

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

xmpp/simplexml.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
I'm personally using it in many other separate projects. It is designed to be as standalone as possible."""
1919

2020
import xml.parsers.expat
21+
import weakref
2122

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

0 commit comments

Comments
 (0)