[py3] Adjust xsd2yams code and tests
authorDenis Laxalde <denis.laxalde@logilab.fr>
Wed, 02 Oct 2019 13:51:45 +0200
changeset 3037 12980f28dd32
parent 3036 56b3e27c5127
child 3038 33ad61a9f699
[py3] Adjust xsd2yams code and tests * Use io.BytesIO instead of StringIO.StringIO. * Explicitly encode strings before writing to stream. * Decode results before string comparisons in tests.
cubicweb_seda/xsd2yams.py
test/test_xsd2yams.py
--- a/cubicweb_seda/xsd2yams.py	Wed Oct 02 11:51:39 2019 +0200
+++ b/cubicweb_seda/xsd2yams.py	Wed Oct 02 13:51:45 2019 +0200
@@ -515,7 +515,7 @@
         """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 @@
     """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
--- a/test/test_xsd2yams.py	Wed Oct 02 11:51:39 2019 +0200
+++ b/test/test_xsd2yams.py	Wed Oct 02 13:51:45 2019 +0200
@@ -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 @@
 
     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