-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', '�')
+