From: Andrew Rabert Date: Fri, 13 Dec 2019 21:24:50 +0000 (-0500) Subject: TemplateSyntaxError can be pickled X-Git-Tag: 2.11.0~10^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F1117%2Fhead;p=thirdparty%2Fjinja.git TemplateSyntaxError can be pickled --- diff --git a/CHANGES.rst b/CHANGES.rst index d0f5d6dc..889fd9a1 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 36e4716a..c01483b4 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 eaeb2536..459e9080 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( [