Skip to content

Remove _env instance attribute from Element #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions branca/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class Element:
def __init__(self, template=None, template_name=None):
self._name = "Element"
self._id = hexlify(urandom(16)).decode()
self._env = ENV
self._children = OrderedDict()
self._parent = None
self._template_str = template
Expand All @@ -67,19 +66,16 @@ def __init__(self, template=None, template_name=None):

def __getstate__(self):
"""Modify object state when pickling the object.
jinja2 Environment cannot be pickled, so set
the ._env attribute to None. This will be added back
when unpickling (see __setstate__)

jinja2 Templates cannot be pickled, so remove the instance attribute
if it exists. It will be added back when unpickling (see __setstate__).
"""
state: dict = self.__dict__.copy()
state["_env"] = None
state.pop("_template", None)
return state

def __setstate__(self, state: dict):
"""Re-add ._env attribute when unpickling"""
state["_env"] = ENV

"""Re-add _template instance attribute when unpickling"""
if state["_template_str"] is not None:
state["_template"] = Template(state["_template_str"])
elif state["_template_name"] is not None:
Expand Down