summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Rabert <ar@nullsum.net>2019-12-13 16:24:50 -0500
committerDavid Lord <davidism@gmail.com>2020-01-08 11:14:52 -0800
commitb23a0dec2da3036310852ac82d25a1b115aa0995 (patch)
tree765abb527afdb0f10145df0ced49bda1743f517c
parent715f357abc1d3966f1059f59d7a0ae5a7b6b5905 (diff)
downloadjinja2-b23a0dec2da3036310852ac82d25a1b115aa0995.tar.gz
TemplateSyntaxError can be pickled
-rw-r--r--CHANGES.rst2
-rw-r--r--jinja2/exceptions.py7
-rw-r--r--tests/test_debug.py7
3 files changed, 16 insertions, 0 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index d0f5d6d..889fd9a 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -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
--------------
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 @@ 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
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 @@ 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(
[