[jhbuild] Move sxml from bytes output to text.



commit 5acc1f58b2ba27772bea305b0576d672f5742736
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Mon Sep 23 09:54:10 2019 +0200

    Move sxml from bytes output to text.
    
    We can always encode the xml later on.
    This makes things work with py 2 and 3.

 jhbuild/commands/snapshot.py |  6 +++---
 jhbuild/utils/__init__.py    |  4 ++--
 jhbuild/utils/misc.py        | 10 ++++++++++
 jhbuild/utils/sxml.py        | 26 +++-----------------------
 4 files changed, 18 insertions(+), 28 deletions(-)
---
diff --git a/jhbuild/commands/snapshot.py b/jhbuild/commands/snapshot.py
index 53b4d835..650fa00a 100644
--- a/jhbuild/commands/snapshot.py
+++ b/jhbuild/commands/snapshot.py
@@ -23,7 +23,7 @@ from __future__ import print_function
 
 import jhbuild.moduleset
 from jhbuild.commands import Command, register_command
-from jhbuild.utils import N_
+from jhbuild.utils import N_, bprint
 from jhbuild.utils.sxml import sxml, sxml_to_string
 
 
@@ -52,7 +52,7 @@ class cmd_snapshot(Command):
              + [m.to_sxml() for m in checked_out_mods]
              + [m.to_sxml() for m in meta])
 
-        print('<?xml version="1.0"?>\n')
-        print(sxml_to_string(x))
+        bprint(b'<?xml version="1.0"?>\n')
+        bprint(sxml_to_string(x).encode("utf-8") + b'\n')
 
 register_command(cmd_snapshot)
diff --git a/jhbuild/utils/__init__.py b/jhbuild/utils/__init__.py
index 0749b317..c3f58622 100644
--- a/jhbuild/utils/__init__.py
+++ b/jhbuild/utils/__init__.py
@@ -17,6 +17,6 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-from .misc import inpath, try_import_module, udecode, uprint, N_, _, install_translation, uinput, open_text
+from .misc import inpath, try_import_module, udecode, uprint, N_, _, install_translation, uinput, open_text, 
bprint
 
-inpath, try_import_module, udecode, uprint, N_, _, install_translation, uinput, open_text
\ No newline at end of file
+inpath, try_import_module, udecode, uprint, N_, _, install_translation, uinput, open_text, bprint
\ No newline at end of file
diff --git a/jhbuild/utils/misc.py b/jhbuild/utils/misc.py
index 4acb1a41..b2d23030 100644
--- a/jhbuild/utils/misc.py
+++ b/jhbuild/utils/misc.py
@@ -70,6 +70,16 @@ def udecode(s):
     else:
         return s
 
+def bprint(data):
+    '''Write some binary data as is to stdout'''
+
+    assert isinstance(data, bytes)
+    if PY2:
+        sys.stdout.write(data)
+    else:
+        sys.stdout.flush()
+        sys.stdout.buffer.write(data)
+
 def uprint(*args, **kwargs):
     '''Print Unicode string encoded for the terminal'''
 
diff --git a/jhbuild/utils/sxml.py b/jhbuild/utils/sxml.py
index 878c8aec..c0c8a445 100644
--- a/jhbuild/utils/sxml.py
+++ b/jhbuild/utils/sxml.py
@@ -17,12 +17,8 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-from .compat import string_types, text_type
-
-
 """
-An s-expression syntax for XML documents, together with a serializer to
-UTF-8.
+An s-expression syntax for XML documents.
 
 Use like this:
 
@@ -38,22 +34,6 @@ Use like this:
 
 __all__ = ['sxml', 'sxml_to_string']
 
-
-# from Django, originally. used to make sure xml is utf-8.
-def smart_str(s, encoding='utf-8', errors='strict'):
-    # Returns a bytestring version of 's', encoded as specified in 'encoding'.
-    if not isinstance(s, string_types):
-        try:
-            return str(s)
-        except UnicodeEncodeError:
-            return text_type(s).encode(encoding, errors)
-    elif isinstance(s, text_type):
-        return s.encode(encoding, errors)
-    elif s and encoding != 'utf-8':
-        return s.decode('utf-8', errors).encode(encoding, errors)
-    else:
-        return s
-
 def quote(s):
     quoted = {'"': '&quot;',
               '&': '&amp;',
@@ -63,10 +43,10 @@ def quote(s):
 
 def sxml_to_string(expr):
     if not isinstance(expr, list):
-        return smart_str(quote(expr))
+        return quote(expr)
     operator = expr[0]
     args = [sxml_to_string(arg) for arg in expr[1:]]
-    return smart_str(operator(args))
+    return operator(args)
 
 class sxml:
     def __getattr__(self, attr):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]