From: Ben Darnell Date: Sun, 28 Apr 2013 22:31:57 +0000 (-0400) Subject: Prefix all "reserved" names used by the template system with _tt_ X-Git-Tag: v3.1.0~93 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1a1b0498e9cddfc89721e950c2882709a23570a5;p=thirdparty%2Ftornado.git Prefix all "reserved" names used by the template system with _tt_ to avoid collisions with user-defined names. Inspired by #768. --- diff --git a/tornado/template.py b/tornado/template.py index 8e1bfbae3..212f9bbdd 100644 --- a/tornado/template.py +++ b/tornado/template.py @@ -84,6 +84,9 @@ hand, but instead use the `~.RequestHandler.render` and `tornado.web.RequestHandler`, which load templates automatically based on the ``template_path`` `.Application` setting. +Variable names beginning with ``_tt_`` are reserved by the template +system and should not be used by application code. + Syntax Reference ---------------- @@ -252,8 +255,8 @@ class Template(object): "squeeze": escape.squeeze, "linkify": escape.linkify, "datetime": datetime, - "_utf8": escape.utf8, # for internal use - "_string_types": (unicode_type, bytes_type), + "_tt_utf8": escape.utf8, # for internal use + "_tt_string_types": (unicode_type, bytes_type), # __name__ and __loader__ allow the traceback mechanism to find # the generated source code. "__name__": self.name.replace('.', '_'), @@ -262,7 +265,7 @@ class Template(object): namespace.update(self.namespace) namespace.update(kwargs) exec_in(self.compiled, namespace) - execute = namespace["_execute"] + execute = namespace["_tt_execute"] # Clear the traceback module's cache of source data now that # we've generated a new template (mainly for this module's # unittests, where different tests reuse the same name). @@ -403,12 +406,12 @@ class _File(_Node): self.line = 0 def generate(self, writer): - writer.write_line("def _execute():", self.line) + writer.write_line("def _tt_execute():", self.line) with writer.indent(): - writer.write_line("_buffer = []", self.line) - writer.write_line("_append = _buffer.append", self.line) + writer.write_line("_tt_buffer = []", self.line) + writer.write_line("_tt_append = _tt_buffer.append", self.line) self.body.generate(writer) - writer.write_line("return _utf8('').join(_buffer)", self.line) + writer.write_line("return _tt_utf8('').join(_tt_buffer)", self.line) def each_child(self): return (self.body,) @@ -477,15 +480,15 @@ class _ApplyBlock(_Node): return (self.body,) def generate(self, writer): - method_name = "apply%d" % writer.apply_counter + method_name = "_tt_apply%d" % writer.apply_counter writer.apply_counter += 1 writer.write_line("def %s():" % method_name, self.line) with writer.indent(): - writer.write_line("_buffer = []", self.line) - writer.write_line("_append = _buffer.append", self.line) + writer.write_line("_tt_buffer = []", self.line) + writer.write_line("_tt_append = _tt_buffer.append", self.line) self.body.generate(writer) - writer.write_line("return _utf8('').join(_buffer)", self.line) - writer.write_line("_append(_utf8(%s(%s())))" % ( + writer.write_line("return _tt_utf8('').join(_tt_buffer)", self.line) + writer.write_line("_tt_append(_tt_utf8(%s(%s())))" % ( self.method, method_name), self.line) @@ -533,21 +536,21 @@ class _Expression(_Node): self.raw = raw def generate(self, writer): - writer.write_line("_tmp = %s" % self.expression, self.line) - writer.write_line("if isinstance(_tmp, _string_types):" - " _tmp = _utf8(_tmp)", self.line) - writer.write_line("else: _tmp = _utf8(str(_tmp))", self.line) + writer.write_line("_tt_tmp = %s" % self.expression, self.line) + writer.write_line("if isinstance(_tt_tmp, _tt_string_types):" + " _tt_tmp = _tt_utf8(_tt_tmp)", self.line) + writer.write_line("else: _tt_tmp = _tt_utf8(str(_tt_tmp))", self.line) if not self.raw and writer.current_template.autoescape is not None: # In python3 functions like xhtml_escape return unicode, # so we have to convert to utf8 again. - writer.write_line("_tmp = _utf8(%s(_tmp))" % + writer.write_line("_tt_tmp = _tt_utf8(%s(_tt_tmp))" % writer.current_template.autoescape, self.line) - writer.write_line("_append(_tmp)", self.line) + writer.write_line("_tt_append(_tt_tmp)", self.line) class _Module(_Expression): def __init__(self, expression, line): - super(_Module, self).__init__("_modules." + expression, line, + super(_Module, self).__init__("_tt_modules." + expression, line, raw=True) @@ -567,7 +570,7 @@ class _Text(_Node): value = re.sub(r"(\s*\n\s*)", "\n", value) if value: - writer.write_line('_append(%r)' % escape.utf8(value), self.line) + writer.write_line('_tt_append(%r)' % escape.utf8(value), self.line) class ParseError(Exception): diff --git a/tornado/test/template_test.py b/tornado/test/template_test.py index 67cad59b1..f3a9e0590 100644 --- a/tornado/test/template_test.py +++ b/tornado/test/template_test.py @@ -199,7 +199,7 @@ three{%end%} loader = DictLoader({ "base.html": "{% module Template('sub.html') %}", "sub.html": "{{1/0}}", - }, namespace={"_modules": ObjectDict({"Template": lambda path, **kwargs: loader.load(path).generate(**kwargs)})}) + }, namespace={"_tt_modules": ObjectDict({"Template": lambda path, **kwargs: loader.load(path).generate(**kwargs)})}) try: loader.load("base.html").generate() except ZeroDivisionError: diff --git a/tornado/web.py b/tornado/web.py index 6abf42c09..7b368763a 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -131,14 +131,15 @@ class RequestHandler(object): self.path_kwargs = None self.ui = ObjectDict((n, self._ui_method(m)) for n, m in application.ui_methods.items()) - # UIModules are available as both `modules` and `_modules` in the + # UIModules are available as both `modules` and `_tt_modules` in the # template namespace. Historically only `modules` was available # but could be clobbered by user additions to the namespace. - # The template {% module %} directive looks in `_modules` to avoid + # The template {% module %} directive looks in `_tt_modules` to avoid # possible conflicts. - self.ui["_modules"] = ObjectDict((n, self._ui_module(n, m)) for n, m in - application.ui_modules.items()) - self.ui["modules"] = self.ui["_modules"] + self.ui["_tt_modules"] = ObjectDict( + (n, self._ui_module(n, m)) for n, m in + application.ui_modules.items()) + self.ui["modules"] = self.ui["_tt_modules"] self.clear() # Check since connection is not available in WSGI if getattr(self.request, "connection", None):