[PATCH 05 of 17 seda] [py3] Adjust xsd2yams code and tests

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


* Use io.BytesIO instead of StringIO.StringIO.
* Explicitly encode strings before writing to stream.
* Decode results before string comparisons in tests.

diff --git a/cubicweb_seda/xsd2yams.py b/cubicweb_seda/xsd2yams.py
index 111337e..74fa03b 100644
--- a/cubicweb_seda/xsd2yams.py
+++ b/cubicweb_seda/xsd2yams.py
@@ -515,7 +515,7 @@ class CodeGenerator(object):
         """Generator entry point: write generated code for :class:`XSYMapping` into the given stream
         """
         if with_header:
-            stream.write(_PY_HEADER)
+            stream.write(_PY_HEADER.encode('utf-8'))
         self._generate(mapping, stream)
 
     def _generate(self, mapping, stream):
@@ -538,7 +538,7 @@ class YamsSchemaGenerator(CodeGenerator)
     """Yams schema generator"""
 
     def _generate(self, mapping, stream):
-        stream.write('''from yams.buildobjs import EntityType, RelationDefinition
+        stream.write(b'''from yams.buildobjs import EntityType, RelationDefinition
 from yams.buildobjs import String, Boolean
 from cubicweb.schema import RQLConstraint
 from cubicweb_seda.schema import seda_profile_element
diff --git a/test/test_xsd2yams.py b/test/test_xsd2yams.py
index 14e534c..5fa2414 100644
--- a/test/test_xsd2yams.py
+++ b/test/test_xsd2yams.py
@@ -14,7 +14,7 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
-from StringIO import StringIO
+from io import BytesIO
 try:
     import unittest2 as unittest
 except ImportError:
@@ -43,9 +43,9 @@ class CodeGenerationTC(unittest.TestCase
 
     def test_base(self):
         mapping = xsy_mapping('DataObjectPackage')
-        stream = StringIO()
+        stream = BytesIO()
         YamsSchemaGenerator().generate(mapping, stream)
-        code = stream.getvalue()
+        code = stream.getvalue().decode('utf-8')
         # assert this is valid python
         compile(code, '<generated schema>', mode='exec')
         # assert there is no duplicated classes



More information about the saem-devel mailing list