[utils] speedup a little bit UStringIO

authorAlain Leufroy <alain@leufroy.fr>
changesetaa228e6e1549
branchdefault
phasedraft
hiddenyes
parent revision#480187dd66b3 [web] move AbstractAuthManager near its immediate concrete subclass
child revision#69c68c05d44f [utils] UStringIO inherites from io.StringIO now
files modified by this revision
utils.py
# HG changeset patch
# User Alain Leufroy <alain@leufroy.fr>
# Date 1430858872 -7200
# Tue May 05 22:47:52 2015 +0200
# Node ID aa228e6e154975d5f089d725ef4f054a1b0677b5
# Parent 480187dd66b3c5b0eebe922e9cba8d4f3a34efaa
[utils] speedup a little bit UStringIO

diff --git a/utils.py b/utils.py
@@ -220,24 +220,28 @@
1      """
2 
3      def __init__(self, tracewrites=False, *args, **kwargs):
4          self.tracewrites = tracewrites
5          super(UStringIO, self).__init__(*args, **kwargs)
6 +        if self.tracewrites:
7 +            self.write = self.trace
8 
9      def __nonzero__(self):
10          return True
11 
12      def write(self, value):
13          assert isinstance(value, unicode), u"unicode required not %s : %s"\
14                                       % (type(value).__name__, repr(value))
15 -        if self.tracewrites:
16 -            from traceback import format_stack
17 -            stack = format_stack(None)[:-1]
18 -            escaped_stack = xml_escape(json_dumps(u'\n'.join(stack)))
19 -            escaped_html = xml_escape(value).replace('\n', '<br/>\n')
20 -            tpl = u'<span onclick="alert(%s)">%s</span>'
21 -            value = tpl % (escaped_stack, escaped_html)
22 +        self.append(value)
23 +
24 +    def trace(self, value):
25 +        from traceback import format_stack
26 +        stack = format_stack(None)[:-1]
27 +        escaped_stack = xml_escape(json_dumps(u'\n'.join(stack)))
28 +        escaped_html = xml_escape(value).replace('\n', '<br/>\n')
29 +        tpl = u'<span onclick="alert(%s)">%s</span>'
30 +        value = tpl % (escaped_stack, escaped_html)
31          self.append(value)
32 
33      def getvalue(self):
34          return u''.join(self)
35