]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
TemplateSyntaxError can be pickled 1117/head
authorAndrew Rabert <ar@nullsum.net>
Fri, 13 Dec 2019 21:24:50 +0000 (16:24 -0500)
committerDavid Lord <davidism@gmail.com>
Wed, 8 Jan 2020 19:14:52 +0000 (11:14 -0800)
CHANGES.rst
jinja2/exceptions.py
tests/test_debug.py

index d0f5d6dc66add0a90f86463bb75c12aaeedd05f1..889fd9a102dbc537d647fa95ac5f7ececf6154bf 100644 (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
 --------------
index 36e4716a31948c74c33094f96e40065f9305d261..c01483b4f617f39c1d6aa1be3af853cd203abe0d 100644 (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
index eaeb25364464756247a733a1d88aadbcd80a443f..459e90802832b0f8060aad9001771d2f4628bc19 100644 (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(
             [