Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 3cf939d

Browse files
author
Carlos Pascual
committed
Ensure that (internal) files are closed in {save,load}ConfigFile
The {save,load}ConfigFile methods accept either a file name or a file object as their argument. If a name is passed, a file object is internally created and opened, but it is not closed. This may cause trouble. Make sure the file is closed if it was internally created. Note that, in order not to maintain backwards compatibility, the file won't be closed if it was passed already as a file object (the responsibility for closing lies in the opener)
1 parent 1971088 commit 3cf939d

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

lib/taurus/qt/qtcore/configuration/configuration.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,13 @@ def saveConfigFile(self, ofile=None):
444444
)
445445
if not ofile:
446446
return
447-
if isinstance(ofile, string_types):
448-
ofile = open(ofile, 'wb')
449-
configdict = self.createConfig(allowUnpickable=False)
450447
self.info("Saving current settings in '%s'" % ofile.name)
451-
pickle.dump(configdict, ofile)
448+
configdict = self.createConfig(allowUnpickable=False)
449+
if isinstance(ofile, string_types):
450+
with open(ofile, 'wb') as ofile:
451+
pickle.dump(configdict, ofile)
452+
else:
453+
pickle.dump(configdict, ofile)
452454
return ofile.name
453455

454456
def loadConfigFile(self, ifile=None):
@@ -466,8 +468,9 @@ def loadConfigFile(self, ifile=None):
466468
if not ifile:
467469
return
468470
if isinstance(ifile, string_types):
469-
ifile = open(ifile, 'rb')
470-
471-
configdict = pickle.load(ifile)
471+
with open(ifile, 'rb') as ifile:
472+
configdict = pickle.load(ifile)
473+
else:
474+
configdict = pickle.load(ifile)
472475
self.applyConfig(configdict)
473476
return ifile.name

0 commit comments

Comments
 (0)