From 187bde1aff75cfc41cb345cd7798d5500900a3c0 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Thu, 1 May 2008 18:19:16 +0200 Subject: [PATCH] added cache_clear function --HG-- branch : trunk --- docs/api.rst | 2 ++ jinja2/__init__.py | 5 +++-- jinja2/debug.py | 4 +++- jinja2/lexer.py | 2 +- jinja2/utils.py | 12 ++++++++++++ 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index 3ad17ac..a8a6779 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -211,6 +211,8 @@ functions to a Jinja2 environment. in HTML. This function will not escaped objects that do have an HTML representation such as already escaped data. +.. autofunction:: jinja2.utils.clear_caches + .. autoclass:: jinja2.utils.Markup diff --git a/jinja2/__init__.py b/jinja2/__init__.py index 0ab7423..cd720a6 100644 --- a/jinja2/__init__.py +++ b/jinja2/__init__.py @@ -49,7 +49,8 @@ from jinja2.exceptions import TemplateError, UndefinedError, \ # decorators and public utilities from jinja2.filters import environmentfilter, contextfilter -from jinja2.utils import Markup, escape, environmentfunction, contextfunction +from jinja2.utils import Markup, escape, clear_caches, \ + environmentfunction, contextfunction __all__ = [ 'Environment', 'Template', 'BaseLoader', 'FileSystemLoader', @@ -58,5 +59,5 @@ __all__ = [ 'TemplateError', 'UndefinedError', 'TemplateNotFound', 'TemplateSyntaxError', 'TemplateAssertionError', 'environmentfilter', 'contextfilter', 'Markup', 'escape', 'environmentfunction', - 'contextfunction' + 'contextfunction', 'clear_caches' ] diff --git a/jinja2/debug.py b/jinja2/debug.py index a9b4439..9209054 100644 --- a/jinja2/debug.py +++ b/jinja2/debug.py @@ -3,7 +3,9 @@ jinja2.debug ~~~~~~~~~~~~ - Implements the debug interface for Jinja. + Implements the debug interface for Jinja. This module does some pretty + ugly stuff with the Python traceback system in order to achieve tracebacks + with correct line numbers, locals and contents. :copyright: Copyright 2008 by Armin Ronacher. :license: BSD. diff --git a/jinja2/lexer.py b/jinja2/lexer.py index 772dee2..b8a363f 100644 --- a/jinja2/lexer.py +++ b/jinja2/lexer.py @@ -23,7 +23,7 @@ from jinja2.utils import LRUCache # cache for the lexers. Exists in order to be able to have multiple # environments with the same lexer -_lexer_cache = LRUCache(10) +_lexer_cache = LRUCache(50) # static regular expressions whitespace_re = re.compile(r'\s+(?um)') diff --git a/jinja2/utils.py b/jinja2/utils.py index 6a5c9a0..e8f6fd7 100644 --- a/jinja2/utils.py +++ b/jinja2/utils.py @@ -47,6 +47,18 @@ def environmentfunction(f): return f +def clear_caches(): + """Jinja2 keeps internal caches for environments and lexers. These are + used so that Jinja2 doesn't have to recreate environments and lexers all + the time. Normally you don't have to care about that but if you are + messuring memory consumption you may want to clean the caches. + """ + from jinja2.environment import _spontaneous_environments + from jinja2.lexer import _lexer_cache + _spontaneous_environments.clear() + _lexer_cache.clear() + + def import_string(import_name, silent=False): """Imports an object based on a string. This use useful if you want to use import paths as endpoints or something similar. An import path can -- 2.26.2