Commits

Mike Bayer committed 73c8011

- UNDEFINED is a reserved word too, fix docs, add tests

  • Participants
  • Parent commits 66300eb
  • Tags rel_0_7_0

Comments (0)

Files changed (4)

   names, that is names which are never pulled
   from the context and cannot be passed to
   the template.render() method.  Current names
-  are "context", "loop".
+  are "context", "loop", "UNDEFINED".
 
 - [feature] The html_error_template() will now
   apply Pygments highlighting to the source

File doc/build/runtime.rst

 ``context.write()`` and content will always go to the topmost
 buffer.
 
+.. _context_vars:
+
 Context Variables
 ------------------
 
 A one-stop shop for all the names Mako defines. Most of these
 names are instances of :class:`.Namespace`, which are described
 in the next section, :ref:`namespaces_toplevel`. Also, most of
-these names other than :class:`.Context` and ``UNDEFINED`` are
-also present *within* the :class:`.Context` itself.   There are only 
-two names, ``context`` and ``loop``, that are themselves not defined
-in the context and can't be replaced - see the section :ref:`reserved_names`.
+these names other than ``context``, ``UNDEFINED``, and ``loop`` are
+also present *within* the :class:`.Context` itself.   The names
+``context``, ``loop`` and ``UNDEFINED`` themselves can't be passed
+to the context and can't be substituted  - see the section :ref:`reserved_names`.
 
 * ``context`` - this is the :class:`.Context` object, introduced
   at :ref:`context`.
 Reserved names
 --------------
 
-Mako has two words that are considered to be "reserved" and can't be used
+Mako has a few names that are considered to be "reserved" and can't be used
 as variable names.   As of 0.7, Mako raises an error if these words are found
 passed to the template as context arguments, whereas in previous versions they'd be silently
 ignored or lead to other error messages.  
 
 * ``context`` - see :ref:`context`
+* ``UNDEFINED`` - see :ref:`context_vars`
 * ``loop`` - see :ref:`loop_context`.  Note this can be disabled for legacy templates
   via the ``enable_loop=False`` argument; see :ref:`migrating_loop`.
 

File mako/codegen.py

 # names which are hardwired into the 
 # template and are not accessed via the 
 # context itself
-RESERVED_NAMES = set(['context', 'loop'])
+RESERVED_NAMES = set(['context', 'loop', 'UNDEFINED'])
 
 def compile(node, 
                 uri, 

File test/test_template.py

             result_lines(t.render(t="T")),
             ['t is: T', 'a,b,c']
         )
- 
+
+class ReservedNameTest(TemplateTest):
+    def test_names_on_context(self):
+        for name in ('context', 'loop', 'UNDEFINED'):
+            assert_raises_message(
+                exceptions.NameConflictError,
+                r"Reserved words passed to render\(\): %s" % name,
+                Template("x").render, **{name:'foo'}
+            )
+
+    def test_names_in_template(self):
+        for name in ('context', 'loop', 'UNDEFINED'):
+            assert_raises_message(
+                exceptions.NameConflictError,
+                r"Reserved words declared in template: %s" % name,
+                Template, "<%% %s = 5 %%>" % name
+            )
+
+    def test_exclude_loop_context(self):
+        self._do_memory_test(
+            "loop is ${loop}",
+            "loop is 5",
+            template_args=dict(loop=5),
+            enable_loop=False
+        )
+
+    def test_exclude_loop_template(self):
+        self._do_memory_test(
+            "<% loop = 12 %>loop is ${loop}",
+            "loop is 12",
+            enable_loop=False
+        )
  
 class ControlTest(TemplateTest):
     def test_control(self):