From ecb59cc6579402b68ddfd4499bf30edacf5963be Mon Sep 17 00:00:00 2001 From: Alex Hill Date: Wed, 9 Mar 2016 11:35:39 +0800 Subject: Fixed #26306 -- Fixed memory leak in cached template loader. --- django/template/backends/django.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'django/template/backends') diff --git a/django/template/backends/django.py b/django/template/backends/django.py index cc795aae1c..afe1a7cd23 100644 --- a/django/template/backends/django.py +++ b/django/template/backends/django.py @@ -68,13 +68,24 @@ class Template(object): reraise(exc, self.backend) -def reraise(exc, backend): +def copy_exception(exc, backend=None): """ - Reraise TemplateDoesNotExist while maintaining template debug information. + Create a new TemplateDoesNotExist. Preserve its declared attributes and + template debug data but discard __traceback__, __context__, and __cause__ + to make this object suitable for keeping around (in a cache, for example). """ - new = exc.__class__(*exc.args, tried=exc.tried, backend=backend) + backend = backend or exc.backend + new = exc.__class__(*exc.args, tried=exc.tried, backend=backend, chain=exc.chain) if hasattr(exc, 'template_debug'): new.template_debug = exc.template_debug + return new + + +def reraise(exc, backend): + """ + Reraise TemplateDoesNotExist while maintaining template debug information. + """ + new = copy_exception(exc, backend) six.reraise(exc.__class__, new, sys.exc_info()[2]) -- cgit v1.3