summaryrefslogtreecommitdiff
path: root/giscanner
diff options
context:
space:
mode:
authorChristoph Reiter <reiter.christoph@gmail.com>2020-11-07 18:55:48 +0000
committerChristoph Reiter <reiter.christoph@gmail.com>2020-11-07 18:55:48 +0000
commit10b1a869da09d260e54b6f28a0d2c0e3015deee4 (patch)
tree51408ccc16a4a486c7d18254b41d9c6b2b3f9638 /giscanner
parent579e84b92cf3ec9cb10d9b9b0683181b05569cb1 (diff)
parentdd378ee46082862f711629d40ed33f944d1e3259 (diff)
downloadgobject-introspection-10b1a869da09d260e54b6f28a0d2c0e3015deee4.tar.gz
Merge branch 'open-cleanup' into 'master'
open cleanup: explicit encoding, close fds See merge request GNOME/gobject-introspection!250
Diffstat (limited to 'giscanner')
-rw-r--r--giscanner/cachestore.py24
-rw-r--r--giscanner/ccompiler.py2
-rw-r--r--giscanner/codegen.py4
-rw-r--r--giscanner/docmain.py2
-rw-r--r--giscanner/dumper.py4
-rw-r--r--giscanner/gdumpparser.py2
-rw-r--r--giscanner/scannermain.py16
-rw-r--r--giscanner/utils.py4
8 files changed, 30 insertions, 28 deletions
diff --git a/giscanner/cachestore.py b/giscanner/cachestore.py
index baaaf1ed..3512badc 100644
--- a/giscanner/cachestore.py
+++ b/giscanner/cachestore.py
@@ -65,7 +65,7 @@ class CacheStore(object):
current_hash = _get_versionhash()
version = os.path.join(self._directory, _CACHE_VERSION_FILENAME)
try:
- with open(version, 'r') as version_file:
+ with open(version, 'r', encoding='utf-8') as version_file:
cache_hash = version_file.read()
except (IOError, OSError) as e:
# File does not exist
@@ -81,7 +81,7 @@ class CacheStore(object):
tmp_fd, tmp_filename = tempfile.mkstemp(prefix='g-ir-scanner-cache-version-')
try:
- with os.fdopen(tmp_fd, 'w') as tmp_file:
+ with os.fdopen(tmp_fd, 'w', encoding='utf-8') as tmp_file:
tmp_file.write(current_hash)
# On Unix, this would just be os.rename() but Windows
@@ -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/ccompiler.py b/giscanner/ccompiler.py
index bd1aa78e..cb97e76f 100644
--- a/giscanner/ccompiler.py
+++ b/giscanner/ccompiler.py
@@ -387,7 +387,7 @@ class CCompiler(object):
output_flag = ['-out:' + tmp_filename]
proc = subprocess.call(args + [implib] + output_flag,
stdout=subprocess.PIPE)
- with open(tmp_filename, 'r') as tmp_fileobj:
+ with open(tmp_filename, 'r', encoding='utf-8') as tmp_fileobj:
for line in tmp_fileobj.read().splitlines():
if '__IMPORT_DESCRIPTOR_' in line:
diff --git a/giscanner/codegen.py b/giscanner/codegen.py
index 138acf72..9faf3cab 100644
--- a/giscanner/codegen.py
+++ b/giscanner/codegen.py
@@ -161,8 +161,8 @@ class CCodeGenerator(object):
self._function_bodies[node] = body
def codegen(self):
- self.out_h = open(self.out_h_filename, 'w')
- self.out_c = open(self.out_c_filename, 'w')
+ self.out_h = open(self.out_h_filename, 'w', encoding='utf-8')
+ self.out_c = open(self.out_c_filename, 'w', encoding='utf-8')
self._codegen_start()
diff --git a/giscanner/docmain.py b/giscanner/docmain.py
index 6ef1de4e..dab063ef 100644
--- a/giscanner/docmain.py
+++ b/giscanner/docmain.py
@@ -71,7 +71,7 @@ def doc_main(args):
if args.format == 'sections':
sections_file = generate_sections_file(transformer)
- with open(args.output, 'w') as fp:
+ with open(args.output, 'w', encoding='utf-8') as fp:
write_sections_file(fp, sections_file)
else:
writer = DocWriter(transformer, args.language, args.format)
diff --git a/giscanner/dumper.py b/giscanner/dumper.py
index e4b6ea03..f61c46c1 100644
--- a/giscanner/dumper.py
+++ b/giscanner/dumper.py
@@ -111,13 +111,13 @@ class DumpCompiler(object):
'gobject-introspection-1.0', 'gdump.c')
if not os.path.isfile(gdump_path):
raise SystemExit("Couldn't find %r" % (gdump_path, ))
- with open(gdump_path) as gdump_file:
+ with open(gdump_path, encoding='utf-8') as gdump_file:
gdump_contents = gdump_file.read()
tpl_args['gdump_include'] = gdump_contents
tpl_args['init_sections'] = "\n".join(self._options.init_sections)
c_path = self._generate_tempfile(tmpdir, '.c')
- with open(c_path, 'w') as f:
+ with open(c_path, 'w', encoding='utf-8') as f:
f.write(_PROGRAM_TEMPLATE % tpl_args)
# We need to reference our get_type and error_quark functions
diff --git a/giscanner/gdumpparser.py b/giscanner/gdumpparser.py
index e7ccf575..3d9720d0 100644
--- a/giscanner/gdumpparser.py
+++ b/giscanner/gdumpparser.py
@@ -144,7 +144,7 @@ class GDumpParser(object):
"""Load the library (or executable), returning an XML
blob containing data gleaned from GObject's primitive introspection."""
in_path = os.path.join(self._binary.tmpdir, 'functions.txt')
- with open(in_path, 'w') as f:
+ with open(in_path, 'w', encoding='utf-8') as f:
for func in self._get_type_functions:
f.write('get-type:')
f.write(func)
diff --git a/giscanner/scannermain.py b/giscanner/scannermain.py
index ca9065b2..957ba0b7 100644
--- a/giscanner/scannermain.py
+++ b/giscanner/scannermain.py
@@ -334,7 +334,7 @@ def extract_filelist(options):
filenames = []
if not os.path.exists(options.filelist):
_error('%s: no such filelist file' % (options.filelist, ))
- with open(options.filelist, "r") as filelist_file:
+ with open(options.filelist, "r", encoding=None) as filelist_file:
lines = filelist_file.readlines()
for line in lines:
# We don't support real C++ parsing yet, but we should be able
@@ -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):
diff --git a/giscanner/utils.py b/giscanner/utils.py
index 3983eb79..a49aca6b 100644
--- a/giscanner/utils.py
+++ b/giscanner/utils.py
@@ -84,7 +84,7 @@ _libtool_pat = re.compile("dlname='([A-z0-9\\.\\-\\+]+)'\n")
def _extract_dlname_field(la_file):
- with open(la_file) as f:
+ with open(la_file, encoding='utf-8') as f:
data = f.read()
m = _libtool_pat.search(data)
if m:
@@ -97,7 +97,7 @@ _libtool_libdir_pat = re.compile("libdir='([^']+)'")
def _extract_libdir_field(la_file):
- with open(la_file) as f:
+ with open(la_file, encoding='utf-8') as f:
data = f.read()
m = _libtool_libdir_pat.search(data)
if m: