[PATCH] [utils] Backport of the changeset 9d62d9fed785 to support the fonctionnality in cubicweb 3.26.14

Guillaume Vandevelde guillaume.vandevelde at logilab.fr
Mon Feb 10 16:57:16 CET 2020


# HG changeset patch
# User Guillaume Vandevelde <gvandevelde at logilab.fr>
# Date 1581350162 -3600
#      Mon Feb 10 16:56:02 2020 +0100
# Branch 3.26
# Node ID 4326c02fe59635fba6bd3160bc35aa73e9807c6b
# Parent  9fa65579520f0c2dba413cfa791e0419fbd6105e
[utils] Backport of the changeset 9d62d9fed785 to support the fonctionnality in cubicweb 3.26.14

diff -r 9fa65579520f -r 4326c02fe596 cubicweb/utils.py
--- a/cubicweb/utils.py	Tue Aug 27 20:16:01 2019 +0200
+++ b/cubicweb/utils.py	Mon Feb 10 16:56:02 2020 +0100
@@ -191,6 +191,21 @@
         self._size -= 1
 
 
+def handle_writing_constraints(method):
+    def wrapper(self, value):
+        assert isinstance(value, text_type), u"unicode required not %s : %s"\
+            % (type(value).__name__, repr(value))
+        if self.tracewrites:
+            from traceback import format_stack
+            stack = format_stack(None)[:-1]
+            escaped_stack = xml_escape(json_dumps(u'\n'.join(stack)))
+            escaped_html = xml_escape(value).replace('\n', '<br/>\n')
+            tpl = u'<span onclick="alert(%s)">%s</span>'
+            value = tpl % (escaped_stack, escaped_html)
+        method(self, value)
+    return wrapper
+
+
 class UStringIO(list):
     """a file wrapper which automatically encode unicode string to an encoding
     specifed in the constructor
@@ -205,18 +220,14 @@
 
     __nonzero__ = __bool__
 
+    @handle_writing_constraints
     def write(self, value):
-        assert isinstance(value, text_type), u"unicode required not %s : %s"\
-                                     % (type(value).__name__, repr(value))
-        if self.tracewrites:
-            from traceback import format_stack
-            stack = format_stack(None)[:-1]
-            escaped_stack = xml_escape(json_dumps(u'\n'.join(stack)))
-            escaped_html = xml_escape(value).replace('\n', '<br/>\n')
-            tpl = u'<span onclick="alert(%s)">%s</span>'
-            value = tpl % (escaped_stack, escaped_html)
         self.append(value)
 
+    @handle_writing_constraints
+    def write_front(self, value):
+        self.insert(0, value)
+
     def getvalue(self):
         return u''.join(self)
 



More information about the cubicweb-devel mailing list