Fixes issue 1571:

Adding missing close for open.
If the "close()" call is missing after a "open(filename)" call, the filename isn't guaranteed to be closed before the interpreter exits.
This is generally a bad practice as explained here: https://stackoverflow.com/questions/7395542/is-explicitly-closing-files-important

Also replaced "fid=open(filename) fid.close()" statements for files with the safer
"with open(filename) as fid:" blocks. See https://www.python.org/dev/peps/pep-0343/
This commit is contained in:
Per A. Brodtkorb
2020-03-23 17:16:44 +01:00
parent 8e2627e8e3
commit e4e8bf8317
38 changed files with 230 additions and 266 deletions

View File

@@ -277,9 +277,8 @@ class TopicDefnDeserialString(ITopicDefnDeserializer):
import os, tempfile
creationDir = os.getcwd()
fileID, path = tempfile.mkstemp('.py', moduleNamePre, dir=creationDir)
stringFile = os.fdopen(fileID, 'w')
stringFile.write( dedent(source) )
stringFile.close()
with os.fdopen(fileID, 'w') as stringFile:
stringFile.write( dedent(source) )
return path, [creationDir]
self.__filename, searchPath = createTmpModule()
@@ -443,11 +442,11 @@ def exportTopicTreeSpec(moduleName = None, rootTopic=None, bak='bak', moduleDoc=
filename = '%s.py' % moduleName
if bak:
_backupIfExists(filename, bak)
moduleFile = open(filename, 'w')
try:
TopicTreeSpecPrinter(rootTopic, fileObj=moduleFile, treeDoc=moduleDoc)
finally:
moduleFile.close()
with open(filename, 'w') as moduleFile:
try:
TopicTreeSpecPrinter(rootTopic, fileObj=moduleFile, treeDoc=moduleDoc)
finally:
pass
##############################################################

View File

@@ -1,9 +1,9 @@
"""
Contributed by Joshua R English, adapted by Oliver Schoenborn to be
consistent with pubsub API.
Contributed by Joshua R English, adapted by Oliver Schoenborn to be
consistent with pubsub API.
An extension for pubsub (http://pubsub.sourceforge.net) so topic tree
specification can be encoded in XML format rather than pubsub's default
An extension for pubsub (http://pubsub.sourceforge.net) so topic tree
specification can be encoded in XML format rather than pubsub's default
Python nested class format.
To use:
@@ -32,7 +32,7 @@ These topic definitions are loaded through an XmlTopicDefnProvider:
pub.addTopicDefnProvider( XmlTopicDefnProvider(xml) )
The XmlTopicDefnProvider also accepts a filename instead of XML string:
The XmlTopicDefnProvider also accepts a filename instead of XML string:
provider = XmlTopicDefnProvider("path/to/XMLfile.xml", TOPIC_TREE_FROM_FILE)
pub.addTopicDefnProvider( provider )
@@ -72,7 +72,7 @@ __all__ = [
'TOPIC_TREE_FROM_FILE'
]
def _get_elem(elem):
"""Assume an ETree.Element object or a string representation.
Return the ETree.Element object"""
@@ -84,7 +84,7 @@ def _get_elem(elem):
raise ValueError("Cannot convert to element")
return elem
TOPIC_TREE_FROM_FILE = 'file'
@@ -93,12 +93,13 @@ class XmlTopicDefnProvider(ITopicDefnProvider):
class XmlParserError(RuntimeError): pass
class UnrecognizedSourceFormatError(ValueError): pass
def __init__(self, xml, format=TOPIC_TREE_FROM_STRING):
self._topics = {}
self._treeDoc = ''
if format == TOPIC_TREE_FROM_FILE:
self._parse_tree(_get_elem(open(xml,mode="r").read()))
with open(xml, mode="r") as fid:
self._parse_tree(_get_elem(fid.read()))
elif format == TOPIC_TREE_FROM_STRING:
self._parse_tree(_get_elem(xml))
else:
@@ -164,7 +165,7 @@ class XmlTopicDefnProvider(ITopicDefnProvider):
def getTreeDoc(self):
return self._treeDoc
class XmlVisitor(ITopicTreeVisitor):
def __init__(self, elem):
self.tree = elem
@@ -188,7 +189,7 @@ class XmlVisitor(ITopicTreeVisitor):
desc_elem = ET.SubElement(this_elem, 'description')
topicDesc = topicObj.getDescription()
if topicDesc:
desc_elem.text = ' '.join(topicDesc.split())
desc_elem.text = ' '.join(topicDesc.split())
else:
desc_elem.text = "UNDOCUMENTED"
argDescriptions = topicObj.getArgDescriptions()
@@ -228,7 +229,7 @@ class XmlVisitor(ITopicTreeVisitor):
def _endChildren(self):
self.roots.pop()
## http://infix.se/2007/02/06/gentlemen-indent-your-xml
def indent(elem, level=0):
i = "\n" + level*" "