summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Reiter <reiter.christoph@gmail.com>2020-11-07 08:54:50 +0100
committerChristoph Reiter <reiter.christoph@gmail.com>2020-11-07 09:35:02 +0100
commitdd378ee46082862f711629d40ed33f944d1e3259 (patch)
tree4cf8e3f30f534cf4bc481d5eb1bb8977fe940d1f
parent5f966b0b8d61e2abf003439b2f93a9bd19be798c (diff)
downloadgobject-introspection-dd378ee46082862f711629d40ed33f944d1e3259.tar.gz
Always close files
This means flushing changes and closing the fd. Otherwise this is done by the GC eventually.. Detected using PYTHONTRACEMALLOC=1 PYTHONDEVMODE=1
-rw-r--r--giscanner/cachestore.py20
-rw-r--r--giscanner/scannermain.py14
2 files changed, 18 insertions, 16 deletions
diff --git a/giscanner/cachestore.py b/giscanner/cachestore.py
index e3b76058..3512badc 100644
--- a/giscanner/cachestore.py
+++ b/giscanner/cachestore.py
@@ -169,12 +169,14 @@ class CacheStore(object):
return None
else:
raise
- if not self._cache_is_valid(store_filename, filename):
- return None
- try:
- data = pickle.load(fd)
- except Exception:
- # Broken cache entry, remove it
- self._remove_filename(store_filename)
- data = None
- return data
+
+ with fd:
+ if not self._cache_is_valid(store_filename, filename):
+ return None
+ try:
+ data = pickle.load(fd)
+ except Exception:
+ # Broken cache entry, remove it
+ self._remove_filename(store_filename)
+ data = None
+ return data
diff --git a/giscanner/scannermain.py b/giscanner/scannermain.py
index f80b2cfe..957ba0b7 100644
--- a/giscanner/scannermain.py
+++ b/giscanner/scannermain.py
@@ -472,6 +472,10 @@ def write_output(data, options):
"""Write encoded XML 'data' to the filename specified in 'options'."""
if options.output == "-":
output = sys.stdout
+ try:
+ output.write(data)
+ except IOError as e:
+ _error("while writing output: %s" % (e.strerror, ))
elif options.reparse_validate_gir:
main_f, main_f_name = tempfile.mkstemp(suffix='.gir')
@@ -500,14 +504,10 @@ def write_output(data, options):
return 0
else:
try:
- output = open(options.output, 'wb')
+ with open(options.output, 'wb') as output:
+ output.write(data)
except IOError as e:
- _error("opening output for writing: %s" % (e.strerror, ))
-
- try:
- output.write(data)
- except IOError as e:
- _error("while writing output: %s" % (e.strerror, ))
+ _error("opening/writing output: %s" % (e.strerror, ))
def get_source_root_dirs(options, filenames):