Avoid circular references in Macro class

Just as we store a weakref to the stack in the Function class, we must
also do the same with the template in the Macro class. Otherwise we end
up with circular references that don't get cleaned up until the garbage
collector runs.

Change-Id: Ia615de961f52e9fc5ef90e0668720b42c864a605
Closes-Bug: #1623706
This commit is contained in:
Zane Bitter 2016-09-14 18:30:15 -04:00
parent 0f2383c937
commit 90d8ab6c2d
1 changed files with 11 additions and 1 deletions

View File

@ -140,9 +140,19 @@ class Macro(Function):
def __init__(self, stack, fn_name, raw_args, parse_func, template):
"""Initialise with the argument syntax tree and parser function."""
super(Macro, self).__init__(stack, fn_name, raw_args)
self.template = template
self._tmplref = weakref.ref(template) if template is not None else None
self.parsed = self.parse_args(parse_func)
@property
def template(self):
ref = self._tmplref
if ref is None:
return None
tmpl = ref()
assert tmpl is not None, "Need a reference to the Template object"
return tmpl
@abc.abstractmethod
def parse_args(self, parse_func):
"""Parse the macro using the supplied parsing function.