Commit 3751a82b authored by Jérome Perrin's avatar Jérome Perrin

XMLExportImport: close files to fix ResourceWarning

parent 701e3d89
...@@ -132,6 +132,8 @@ class TrashTool(BaseTool): ...@@ -132,6 +132,8 @@ class TrashTool(BaseTool):
LOG("Trash Tool backupObject", WARNING, LOG("Trash Tool backupObject", WARNING,
"Can't backup object %s" % object_path) "Can't backup object %s" % object_path)
return {} return {}
finally:
copy.close()
subobjects_dict = {} subobjects_dict = {}
......
...@@ -186,4 +186,5 @@ class BusinessTemplateInfoDir(BusinessTemplateInfoBase): ...@@ -186,4 +186,5 @@ class BusinessTemplateInfoDir(BusinessTemplateInfoBase):
return fileinfo return fileinfo
def readFileInfo(self, fileinfo): def readFileInfo(self, fileinfo):
return open(fileinfo).read() with open(fileinfo) as f:
return f.read()
...@@ -411,30 +411,32 @@ def save_record(parser, tag, data): ...@@ -411,30 +411,32 @@ def save_record(parser, tag, data):
import xml.parsers.expat import xml.parsers.expat
def importXML(jar, file, clue=''): def importXML(jar, file, clue=''):
if type(file) is str: if isinstance(file, str):
file=open(file, 'rb') with open(file, 'rb') as f:
outfile=TemporaryFile() data = f.read()
data=file.read() else:
F=ppml.xmlPickler() data = file.read()
F.end_handlers['record'] = save_record with TemporaryFile() as outfile:
F.end_handlers['ZopeData'] = save_zopedata F=ppml.xmlPickler()
F.start_handlers['ZopeData'] = start_zopedata F.end_handlers['record'] = save_record
F.binary=1 F.end_handlers['ZopeData'] = save_zopedata
F.file=outfile F.start_handlers['ZopeData'] = start_zopedata
# <patch> F.binary=1
# Our BTs XML files don't declare encoding but have accented chars in them F.file=outfile
# So we have to declare an encoding but not use unicode, so the unpickler # <patch>
# can deal with the utf-8 strings directly # Our BTs XML files don't declare encoding but have accented chars in them
p=xml.parsers.expat.ParserCreate('utf-8') # So we have to declare an encoding but not use unicode, so the unpickler
if six.PY2: # can deal with the utf-8 strings directly
p.returns_unicode = False p=xml.parsers.expat.ParserCreate('utf-8')
# </patch> if six.PY2:
p.CharacterDataHandler=F.handle_data p.returns_unicode = False
p.StartElementHandler=F.unknown_starttag # </patch>
p.EndElementHandler=F.unknown_endtag p.CharacterDataHandler=F.handle_data
r=p.Parse(data) p.StartElementHandler=F.unknown_starttag
outfile.seek(0) p.EndElementHandler=F.unknown_endtag
return jar.importFile(outfile,clue) r=p.Parse(data)
outfile.seek(0)
return jar.importFile(outfile, clue)
customImporters = { customImporters = {
magic: importXML magic: importXML
......
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