Commit 4f423de4 authored by Jérome Perrin's avatar Jérome Perrin

XMLExportImport: more support pickle protocol 3 🚧 ( repair python2 )

parent 184b66c8
...@@ -30,7 +30,7 @@ import re ...@@ -30,7 +30,7 @@ import re
from marshal import loads as mloads from marshal import loads as mloads
from .xyap import NoBlanks from .xyap import NoBlanks
from .xyap import xyap from .xyap import xyap
from Products.ERP5Type.Utils import str2bytes from Products.ERP5Type.Utils import str2bytes, bytes2str
from marshal import dumps as mdumps from marshal import dumps as mdumps
#from zLOG import LOG #from zLOG import LOG
...@@ -94,7 +94,9 @@ def convert(S): ...@@ -94,7 +94,9 @@ def convert(S):
### readable output. ### readable output.
try: try:
if not isinstance(S, six.text_type): if not isinstance(S, six.text_type):
S = S.decode('utf8') decoded = S.decode('utf8')
if six.PY3:
S = decoded
except UnicodeDecodeError: except UnicodeDecodeError:
return 'base64', base64_encodebytes(S)[:-1] return 'base64', base64_encodebytes(S)[:-1]
else: else:
...@@ -185,7 +187,7 @@ class String(Scalar): ...@@ -185,7 +187,7 @@ class String(Scalar):
# be converted. # be converted.
encoding = 'base64' encoding = 'base64'
v = base64_encodebytes(self._v)[:-1] v = base64_encodebytes(self._v)[:-1]
self._v = self.mapping.convertBase64(v).decode() self._v = bytes2str(self.mapping.convertBase64(v))
else: else:
encoding, self._v = convert(self._v) encoding, self._v = convert(self._v)
self.encoding = encoding self.encoding = encoding
...@@ -675,8 +677,8 @@ class ToXMLUnpickler(Unpickler): ...@@ -675,8 +677,8 @@ class ToXMLUnpickler(Unpickler):
k = self.marker() k = self.marker()
args = Tuple(self.id_mapping, v=self.stack[k+1:]) args = Tuple(self.id_mapping, v=self.stack[k+1:])
del self.stack[k:] del self.stack[k:]
module = self.readline()[:-1].decode() module = bytes2str(self.readline()[:-1])
name = self.readline()[:-1].decode() name = bytes2str(self.readline()[:-1])
value=Object(Global(module, name, self.id_mapping), args, self.id_mapping) value=Object(Global(module, name, self.id_mapping), args, self.id_mapping)
self.append(value) self.append(value)
if six.PY2: if six.PY2:
...@@ -709,8 +711,8 @@ class ToXMLUnpickler(Unpickler): ...@@ -709,8 +711,8 @@ class ToXMLUnpickler(Unpickler):
dispatch[NEWOBJ[0]] = load_newobj dispatch[NEWOBJ[0]] = load_newobj
def load_global(self): def load_global(self):
module = self.readline()[:-1].decode() module = bytes2str(self.readline()[:-1])
name = self.readline()[:-1].decode() name = bytes2str(self.readline()[:-1])
self.append(Global(module, name, self.id_mapping)) self.append(Global(module, name, self.id_mapping))
if six.PY2: if six.PY2:
dispatch[GLOBAL] = load_global dispatch[GLOBAL] = load_global
...@@ -861,7 +863,7 @@ def save_string(self, tag, data): ...@@ -861,7 +863,7 @@ def save_string(self, tag, data):
try: try:
v.decode('utf-8') v.decode('utf-8')
# XXX maybe check with repr_re ? # XXX maybe check with repr_re ?
op = BINUNICODE op = BINUNICODE if six.PY3 else BINSTRING
v = op + struct.pack('<i', l) + v v = op + struct.pack('<i', l) + v
return save_put(self, v, a) return save_put(self, v, a)
except UnicodeDecodeError: except UnicodeDecodeError:
...@@ -874,7 +876,7 @@ def save_string(self, tag, data): ...@@ -874,7 +876,7 @@ def save_string(self, tag, data):
v.decode('ascii') v.decode('ascii')
# XXX zope4py3 we could also create an unpickler with encoding utf-8 ? # XXX zope4py3 we could also create an unpickler with encoding utf-8 ?
except UnicodeDecodeError: except UnicodeDecodeError:
op = BINUNICODE op = BINUNICODE if six.PY3 else BINSTRING
v = op + struct.pack('<i', l) + v v = op + struct.pack('<i', l) + v
return save_put(self, v, a) return save_put(self, v, a)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment