TemplateSyntaxError can be pickled

This commit is contained in:
Andrew Rabert 2019-12-13 16:24:50 -05:00 committed by David Lord
parent 715f357abc
commit b23a0dec2d
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
3 changed files with 16 additions and 0 deletions

View File

@ -99,6 +99,8 @@ Unreleased
``UndefinedError`` consistently. ``select_template`` will show the
undefined message in the list of attempts rather than the empty
string. :issue:`1037`
- ``TemplateSyntaxError`` can be pickled. :pr:`1117`
Version 2.10.3
--------------

View File

@ -141,6 +141,13 @@ class TemplateSyntaxError(TemplateError):
return u'\n'.join(lines)
def __reduce__(self):
# https://bugs.python.org/issue1692335 Exceptions that take
# multiple required arguments have problems with pickling.
# Without this, raises TypeError: __init__() missing 1 required
# positional argument: 'lineno'
return self.__class__, (self.message, self.lineno, self.name, self.filename)
class TemplateAssertionError(TemplateSyntaxError):
"""Like a template syntax error, but covers cases where something in the

View File

@ -10,6 +10,7 @@
"""
import pytest
import pickle
import re
import sys
from traceback import format_exception
@ -71,6 +72,12 @@ ZeroDivisionError: (int(eger)? )?division (or modulo )?by zero
(jinja2\.exceptions\.)?TemplateSyntaxError: wtf
line 42''')
def test_pickleable_syntax_error(self, fs_env):
original = TemplateSyntaxError("bad template", 42, "test", "test.txt")
unpickled = pickle.loads(pickle.dumps(original))
assert str(original) == str(unpickled)
assert original.name == unpickled.name
def test_include_syntax_error_source(self, filesystem_loader):
e = Environment(loader=ChoiceLoader(
[