Fixed SyntaxError connected with nonlocal Python3-specific keyword.

This commit is contained in:
Max P 2018-11-07 17:09:04 -08:00
parent bc9820e7cf
commit 2420a560e3

View File

@ -175,18 +175,16 @@ class DocxTemplate(object):
"""Escape strings for an XML Word document """Escape strings for an XML Word document
which may contain <, >, &, ', and ". which may contain <, >, &, ', and ".
""" """
def escape_recursively(d): def escape_recursively(d, identities):
"""Escape string values of the passed :dict: `d` in-place """Escape string values of the passed :dict: `d` in-place
including nested dictionaries. including nested dictionaries of any depth.
""" """
nonlocal hash_values
for k, v in d.items(): for k, v in d.items():
if isinstance(v, dict): if isinstance(v, dict):
hash_value = id(v) identity = id(v)
if hash_value not in hash_values: if identity not in identities:
hash_values.add(hash_value) identities.add(identity)
escape_recursively(v) escape_recursively(v, identities)
else: else:
# Avoid dict, Listing, InlineImage, RichText, etc. classes # Avoid dict, Listing, InlineImage, RichText, etc. classes
# by comparing v to str. Do not use try-except. # by comparing v to str. Do not use try-except.
@ -195,10 +193,10 @@ class DocxTemplate(object):
d[k] = escape(unescape(v)) d[k] = escape(unescape(v))
# Avoid RecursionError (if back edges, i.e. cycles, exist) # Avoid RecursionError (if back edges, i.e. cycles, exist)
# by using a set of hash values of iterated dictionaries. # by using a set of identities of iterated dictionaries.
hash_values = {id(context), } initial_identities = {id(context)}
escape_recursively(context) escape_recursively(context, initial_identities)
def render(self, context, jinja_env=None, autoescape=False): def render(self, context, jinja_env=None, autoescape=False):
if sys.version_info >= (3, 0) and autoescape: if sys.version_info >= (3, 0) and autoescape: