[PATCH 15 of 17 seda] Make it possible to serialize as unicode in SEDA export adapter

Denis Laxalde denis.laxalde at logilab.fr
Wed Oct 2 16:06:43 CEST 2019

This is useful in tests because the doctest comparison engine from lxml
works better with text objects. The new _encoding argument is
essentially used a "encoding" argument of lxml's etree.tostring() which
will hence serialize to a unicode string. When doing so, we must also
avoid any XML declaration (otherwise, lxml messes up when loading the
content, not sure why), so we insert the "standalone" flag only when
dumping to bytes.

diff --git a/cubicweb_seda/entities/profile_generation.py b/cubicweb_seda/entities/profile_generation.py
index 68211e4..4d779c0 100644
--- a/cubicweb_seda/entities/profile_generation.py
+++ b/cubicweb_seda/entities/profile_generation.py
@@ -278,10 +278,19 @@ class SEDA2ExportAdapter(EntityAdapter):
         return attributes
-    def dump(self):
-        """Return an schema string for the adapted SEDA profile."""
+    def dump(self, _encoding=None):
+        """Return an schema string for the adapted SEDA profile
+        _encoding will be used as "encoding" argument of lxml's tostring, in
+        order to retrieve a unicode string. This is useful for tests.
+        """
         root = self.dump_etree()
-        return etree.tostring(root, encoding=self.encoding, pretty_print=True, standalone=False)
+        kwargs = {}
+        if _encoding is None:
+            # We only want the XML declaration at all if _encoding is not specified.
+            kwargs['standalone'] = False
+        kwargs['encoding'] = _encoding or self.encoding
+        return etree.tostring(root, pretty_print=True, **kwargs)
     def dump_etree(self):
         """Return an XSD etree for the adapted SEDA profile."""

More information about the saem-devel mailing list