[gobject-introspection: 2/3] Always close files
- From: Christoph Reiter <creiter src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gobject-introspection: 2/3] Always close files
- Date: Sat, 7 Nov 2020 18:55:49 +0000 (UTC)
commit dd378ee46082862f711629d40ed33f944d1e3259
Author: Christoph Reiter <reiter christoph gmail com>
Date: Sat Nov 7 08:54:50 2020 +0100
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
giscanner/cachestore.py | 20 +++++++++++---------
giscanner/scannermain.py | 14 +++++++-------
2 files changed, 18 insertions(+), 16 deletions(-)
---
diff --git a/giscanner/cachestore.py b/giscanner/cachestore.py
index e3b76058d..3512badc8 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 f80b2cfe7..957ba0b7a 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):
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]