Skip to content

Commit 3b115ee

Browse files
author
Konstantinos Bairaktaris
committed
Don't do mutations to TNode during render
https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/#thread-safety-considerations Copying from the code's comments: > `self` is not supposed to mutate between invocations of `render` > because Django may parse the template once per thread and reuse the > nodes between renders ("parse" = "process text into nodes").
1 parent 03606ea commit 3b115ee

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

transifex/native/django/templatetags/transifex.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,22 @@ def render(self, context):
224224
source_icu_template, locale,
225225
escape=False)
226226

227+
# `self` is not supposed to mutate between invocations of `render`
228+
# because Django may parse the template once per thread and reuse the
229+
# nodes between renders ("parse" = "process text into nodes"). To that
230+
# end, let's safekeep the old value of `self.source_string.var` to put
231+
# it back in place before returning from render.
232+
# https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/#thread-safety-considerations # noqa
233+
old_source_string_var = self.source_string.var
234+
227235
# Now we resolve the full source filter expression, after having
228236
# replaced its text with the outcome of the translation, in order to
229237
# apply the expression's filters to the translation. The translation is
230238
# marked as safe in order to prevent further escaping attempts that
231239
# would introduce the danger of double escaping (eg `<` => `&amp;lt;`)
232240
self.source_string.var = mark_safe(result)
233241
result = self.source_string.resolve(context)
242+
self.source_string.var = old_source_string_var
234243

235244
if self.asvar is not None:
236245
# Save the translation outcome to a context variable

0 commit comments

Comments
 (0)