diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-02-20 19:33:55 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-02-20 19:33:55 -0500 |
commit | 6c06a3270b6fca6d959fff4c81cf932e17a475d2 (patch) | |
tree | 8aa687367177c91438b31a51c3ca43528c5841ad | |
parent | 4737c269f5658fdab4190504cd5744e95bae1688 (diff) | |
download | mako-6c06a3270b6fca6d959fff4c81cf932e17a475d2.tar.gz |
- the keys() in the Context, as well as
it's internal _data dictionary, now
include just what was specified to
render() as well as Mako builtins
'caller', 'capture'. The contents
of __builtin__ are no longer copied.
-rw-r--r-- | CHANGES | 7 | ||||
-rw-r--r-- | mako/runtime.py | 19 | ||||
-rw-r--r-- | test/test_template.py | 14 |
3 files changed, 26 insertions, 14 deletions
@@ -16,6 +16,13 @@ if the 'template' and 'module' attributes are specified at the same time. +- the keys() in the Context, as well as + it's internal _data dictionary, now + include just what was specified to + render() as well as Mako builtins + 'caller', 'capture'. The contents + of __builtin__ are no longer copied. + 0.3.6 - Documentation is on Sphinx. [ticket:126] diff --git a/mako/runtime.py b/mako/runtime.py index c13de21..f57087f 100644 --- a/mako/runtime.py +++ b/mako/runtime.py @@ -22,12 +22,7 @@ class Context(object): def __init__(self, buffer, **data): self._buffer_stack = [buffer] - # original data, minus the builtins - self._orig = data - - # the context data which includes builtins - self._data = __builtin__.__dict__.copy() - self._data.update(data) + self._data = data self._kwargs = data.copy() self._with_template = None self._outputting_as_unicode = None @@ -75,7 +70,10 @@ class Context(object): return self._data.keys() def __getitem__(self, key): - return self._data[key] + if key in self._data: + return self._data[key] + else: + return __builtin__.__dict__[key] def _push_writer(self): """push a capturing buffer onto this Context and return @@ -107,7 +105,9 @@ class Context(object): def get(self, key, default=None): """Return a value from this :class:`.Context`.""" - return self._data.get(key, default) + return self._data.get(key, + __builtin__.__dict__.get(key, default) + ) def write(self, string): """Write a string to this :class:`.Context` object's @@ -124,7 +124,6 @@ class Context(object): c = Context.__new__(Context) c._buffer_stack = self._buffer_stack c._data = self._data.copy() - c._orig = self._orig c._kwargs = self._kwargs c._with_template = self._with_template c._outputting_as_unicode = self._outputting_as_unicode @@ -585,7 +584,7 @@ def _include_file(context, uri, calling_uri, **kwargs): (callable_, ctx) = _populate_self_namespace( context._clean_inheritance_tokens(), template) - callable_(ctx, **_kwargs_for_include(callable_, context._orig, **kwargs)) + callable_(ctx, **_kwargs_for_include(callable_, context._data, **kwargs)) def _inherit_from(context, uri, calling_uri): """called by the _inherit method in template modules to set diff --git a/test/test_template.py b/test/test_template.py index ba47885..2ac4ad3 100644 --- a/test/test_template.py +++ b/test/test_template.py @@ -3,11 +3,13 @@ from mako.template import Template, ModuleTemplate from mako.lookup import TemplateLookup from mako.ext.preprocessors import convert_comments -from mako import exceptions, util -import re, os +from mako import exceptions, util, runtime +import re +import os from util import flatten_result, result_lines import codecs -from test import TemplateTest, eq_, template_base, module_base, skip_if, assert_raises +from test import TemplateTest, eq_, template_base, module_base, \ + skip_if, assert_raises class EncodingTest(TemplateTest): def test_unicode(self): @@ -442,7 +444,11 @@ class PageArgsTest(TemplateTest): template_args={'variable':'var', 'bar':'bar', 'foo':'foo'} ) - + + def test_context_small(self): + ctx = runtime.Context([].append, x=5, y=4) + eq_(sorted(ctx.keys()), ['caller', 'capture', 'x', 'y']) + def test_with_context(self): template = Template(""" <%page args="x, y, z=7"/> |