the big merge
[nepi.git] / src / nepi / util / parsers / xml_parser.py
index c11782d..d82ea69 100644 (file)
@@ -32,22 +32,44 @@ BOOL = "bool"
 INTEGER = "integer"
 DOUBLE = "float"
 
-def xmlencode(s):
-    if isinstance(s, str):
-        rv = s.decode("latin1")
-    if isinstance(s, datetime.datetime):
-        rv = tsformat(s)
-    elif not isinstance(s, unicode):
-        rv = unicode(s)
-    else:
-        rv = s
-    return rv.replace(u'\x00',u'�')
-
+from six import PY2
+
+if PY2:
+    # xxx old py2 code had a hack, that had 'latin1' hardcoded
+    # as the encoding for 8-byte strings
+    # this is very wrong; I keep it for now
+    # but will probably remove it altogether some day
+    def xmlencode(s):
+        """xml encoder for python2"""
+        if isinstance(s, str):
+            rv = s.decode("latin1")
+        if isinstance(s, datetime.datetime):
+            rv = tsformat(s)
+        elif not isinstance(s, unicode):
+            rv = unicode(s)
+        else:
+            rv = s
+        return rv.replace(u'\x00',u'�')
+else:
+    # use sys.getdefaultencoding() to decode bytes into string
+    def xmlencode(s):
+        """xml encoder for python3"""
+        if isinstance(s, datetime.datetime):
+            rv = tsformat(s)
+        elif isinstance(s, bytes):
+            rv = s.decode(sys.getdefaultencoding())
+        else:
+            rv = s
+        return rv.replace('\x00', '�')
+        
 def xmldecode(s, cast = str):
-    ret = s.replace(u'&#0000',u'\x00').encode("ascii")
-    ret = cast(ret)
-    if s == "None":
+    if s is None:
         return None
+    if PY2:
+        ret = s.replace(u'&#0000', u'\x00').encode("ascii")
+    else:
+        ret = s.replace('&#0000', '\x00')
+    ret = cast(ret)
     return ret
 
 def from_type(value):