Fix XML import of Unicode objects on business templates on Zope 2.12

Zope 2.12 uses the python2.6 built-in expat which, when combined with our custom
export of unicode objects was causing UnicodeDecodeErrors.

In the future, we need to push all these xml import fixes upstream.



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@30469 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent fdc0c139
......@@ -61,13 +61,13 @@ from cStringIO import StringIO
from copy import deepcopy
from zExceptions import BadRequest
import OFS.XMLExportImport
from Products.ERP5Type.patches.ppml import importXML
customImporters={
XMLExportImport.magic: XMLExportImport.importXML,
XMLExportImport.magic: importXML,
}
from zLOG import LOG, WARNING, PROBLEM
from warnings import warn
from OFS.ObjectManager import customImporters
from gzip import GzipFile
from xml.dom.minidom import parse
from xml.sax.saxutils import escape
......@@ -699,6 +699,8 @@ class ObjectTemplateItem(BaseTemplateItem):
else:
obj = connection.importFile(file_obj, customImporters=customImporters)
else:
# FIXME: Why not use the importXML function directly? Are there any BT5s
# with actual .zexp files on the wild?
obj = connection.importFile(file_obj, customImporters=customImporters)
self.removeProperties(obj)
self._objects[file_name[:-4]] = obj
......
......@@ -771,8 +771,41 @@ class xmlPickler(NoBlanks, xyap):
'persistent': save_persis,
}
# FIXME: Leo: Do we still need to replace ppml.xmlPickler now that we're
# using our own xmlPickler in our own importXML function below? Do we support
# any other use of xmlPickler except for Business Template export/import?
ppml.xmlPickler = xmlPickler
class Tuple(Sequence): pass
ppml.Tuple = Tuple
# not a patch, but imported and used directly by
# Products.ERP5.Document.BusinessTemplate. It is a copy of
# from OFS.XMLExportImport.importXML from Zope 2.12
def importXML(jar, file, clue=''):
from OFS.XMLExportImport import save_record, save_zopedata, start_zopedata
from tempfile import TemporaryFile
import xml.parsers.expat
if type(file) is str:
file=open(file, 'rb')
outfile=TemporaryFile()
data=file.read()
F=xmlPickler()
F.end_handlers['record'] = save_record
F.end_handlers['ZopeData'] = save_zopedata
F.start_handlers['ZopeData'] = start_zopedata
F.binary=1
F.file=outfile
# Our BTs XML files don't declare encoding but have accented chars in them
# So we have to declare an encoding but not use unicode, so the unpickler
# can deal with the utf-8 strings directly
p=xml.parsers.expat.ParserCreate('utf-8')
p.returns_unicode = False
p.CharacterDataHandler=F.handle_data
p.StartElementHandler=F.unknown_starttag
p.EndElementHandler=F.unknown_endtag
r=p.Parse(data)
outfile.seek(0)
return jar.importFile(outfile,clue)
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