TemplateSyntaxError can be pickled
diff --git a/CHANGES.rst b/CHANGES.rst
index d0f5d6d..889fd9a 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -99,6 +99,8 @@
``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
--------------
diff --git a/jinja2/exceptions.py b/jinja2/exceptions.py
index 36e4716..c01483b 100644
--- a/jinja2/exceptions.py
+++ b/jinja2/exceptions.py
@@ -141,6 +141,13 @@
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
diff --git a/tests/test_debug.py b/tests/test_debug.py
index eaeb253..459e908 100644
--- a/tests/test_debug.py
+++ b/tests/test_debug.py
@@ -10,6 +10,7 @@
"""
import pytest
+import pickle
import re
import sys
from traceback import format_exception
@@ -71,6 +72,12 @@
(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(
[