diff --git a/CHANGES b/CHANGES index 51189eb0a3310fa262f3cc6d39cdad4f0be9c761..e74bb6283646e606355eeea9526f5407551f082c 100644 --- a/CHANGES +++ b/CHANGES @@ -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 c13de211fffccadc9b0eaa98333ece4f1e766fa9..f57087fd5599106ca81c337d1a54a49190924727 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 ba478856b40955d1734f73ba7d8566e4a6daa31c..2ac4ad36e3d56671d01c2106a19efafa5d75e6b4 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"/>