Edgewall Software

Changeset 1172


Ignore:
Timestamp:
Sep 2, 2011, 10:20:21 PM (12 years ago)
Author:
hodgestar
Message:

Add .copy() function to Context objects. Fixes #249.

Location:
trunk/genshi/template
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/genshi/template/base.py

    r1160 r1172  
    247247    def pop(self):
    248248        """Pop the top-most scope from the stack."""
     249
     250    def copy(self):
     251        """Create a copy of this Context object."""
     252        # required to make f_locals a dict-like object
     253        # See http://genshi.edgewall.org/ticket/249 for
     254        # example use case in Twisted tracebacks
     255        ctxt = Context()
     256        ctxt.frames.pop()  # pop empty dummy context
     257        ctxt.frames.extend(self.frames)
     258        ctxt._match_templates.extend(self._match_templates)
     259        ctxt._choice_stack.extend(self._choice_stack)
     260        return ctxt
    249261
    250262
  • trunk/genshi/template/tests/base.py

    r500 r1172  
    1515import unittest
    1616
    17 from genshi.template.base import Template
     17from genshi.template.base import Template, Context
     18
     19
     20class ContextTestCase(unittest.TestCase):
     21    def test_copy(self):
     22        # create a non-trivial context with some dummy
     23        # frames, match templates and py:choice stacks.
     24        orig_ctxt = Context(a=5, b=6)
     25        orig_ctxt.push({'c': 7})
     26        orig_ctxt._match_templates.append(object())
     27        orig_ctxt._choice_stack.append(object())
     28        ctxt = orig_ctxt.copy()
     29        self.assertNotEqual(id(orig_ctxt), id(ctxt))
     30        self.assertEqual(repr(orig_ctxt), repr(ctxt))
     31        self.assertEqual(orig_ctxt._match_templates, ctxt._match_templates)
     32        self.assertEqual(orig_ctxt._choice_stack, ctxt._choice_stack)
     33
    1834
    1935def suite():
    2036    suite = unittest.TestSuite()
    2137    suite.addTest(doctest.DocTestSuite(Template.__module__))
     38    suite.addTest(unittest.makeSuite(ContextTestCase, 'test'))
    2239    return suite
    2340
Note: See TracChangeset for help on using the changeset viewer.