changeset 948:cccbcbd33e90 stable-0.6.x

Merge r1172 from trunk (add .copy method to Context objects, see #249).
author hodgestar
date Fri, 02 Sep 2011 20:21:50 +0000
parents 6492fab2229a
children 40415173f513
files genshi/template/base.py genshi/template/tests/base.py
diffstat 2 files changed, 30 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/genshi/template/base.py
+++ b/genshi/template/base.py
@@ -247,6 +247,18 @@
     def pop(self):
         """Pop the top-most scope from the stack."""
 
+    def copy(self):
+        """Create a copy of this Context object."""
+        # required to make f_locals a dict-like object
+        # See http://genshi.edgewall.org/ticket/249 for
+        # example use case in Twisted tracebacks
+        ctxt = Context()
+        ctxt.frames.pop()  # pop empty dummy context
+        ctxt.frames.extend(self.frames)
+        ctxt._match_templates.extend(self._match_templates)
+        ctxt._choice_stack.extend(self._choice_stack)
+        return ctxt
+
 
 def _apply_directives(stream, directives, ctxt, vars):
     """Apply the given directives to the stream.
--- a/genshi/template/tests/base.py
+++ b/genshi/template/tests/base.py
@@ -14,11 +14,28 @@
 import doctest
 import unittest
 
-from genshi.template.base import Template
+from genshi.template.base import Template, Context
+
+
+class ContextTestCase(unittest.TestCase):
+    def test_copy(self):
+        # create a non-trivial context with some dummy
+        # frames, match templates and py:choice stacks.
+        orig_ctxt = Context(a=5, b=6)
+        orig_ctxt.push({'c': 7})
+        orig_ctxt._match_templates.append(object())
+        orig_ctxt._choice_stack.append(object())
+        ctxt = orig_ctxt.copy()
+        self.assertNotEqual(id(orig_ctxt), id(ctxt))
+        self.assertEqual(repr(orig_ctxt), repr(ctxt))
+        self.assertEqual(orig_ctxt._match_templates, ctxt._match_templates)
+        self.assertEqual(orig_ctxt._choice_stack, ctxt._choice_stack)
+
 
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(doctest.DocTestSuite(Template.__module__))
+    suite.addTest(unittest.makeSuite(ContextTestCase, 'test'))
     return suite
 
 if __name__ == '__main__':
Copyright (C) 2012-2017 Edgewall Software