Use context blocks for open() calls in packaging
Two unclosed file descriptors in packaging are raising
ResourceWarning on termination in newer Python interpreters. Make
sure they're closed as soon as we're done with them.
@@ -581,7 +581,8 @@ class LocalEggInfo(egg_info.egg_info):
else:
log.info("[pbr] Reusing existing SOURCES.txt")
self.filelist = egg_info.FileList()
- for entry in open(manifest_filename, 'r').read().split('\n'):
+ with open(manifest_filename, 'r') as fil:
+ for entry in fil.read().split('\n'):
self.filelist.append(entry)
@@ -823,12 +824,9 @@ def _get_version_from_pkg_metadata(package_name):
pkg_metadata = {}
for filename in pkg_metadata_filenames:
try:
- pkg_metadata_file = open(filename, 'r')
- except (IOError, OSError):
- continue
- try:
+ with open(filename, 'r') as pkg_metadata_file:
pkg_metadata = email.message_from_file(pkg_metadata_file)
- except email.errors.MessageError:
+ except (IOError, OSError, email.errors.MessageError):
continue
# Check to make sure we're in our own dir
