summaryrefslogtreecommitdiff
path: root/giscanner/dumper.py
diff options
context:
space:
mode:
authorDieter Verfaillie <dieterv@optionexplicit.be>2015-07-05 10:54:16 +0200
committerEmmanuele Bassi <ebassi@gnome.org>2015-08-21 13:03:01 +0100
commitebe658542217c510f186dfc68956d51528d81fd6 (patch)
treec55847c5418430a9b4e509713bb487f358545820 /giscanner/dumper.py
parent23e5dd8d6821e269feec3346c5f95c390a4ff009 (diff)
downloadgobject-introspection-ebe658542217c510f186dfc68956d51528d81fd6.tar.gz
scanner: use open() as os.fdopen as context managers
Ensures files are correctly and immediately closed. https://bugzilla.gnome.org/show_bug.cgi?id=751926
Diffstat (limited to 'giscanner/dumper.py')
-rw-r--r--giscanner/dumper.py75
1 files changed, 36 insertions, 39 deletions
diff --git a/giscanner/dumper.py b/giscanner/dumper.py
index 94231771..3a7ced6f 100644
--- a/giscanner/dumper.py
+++ b/giscanner/dumper.py
@@ -110,44 +110,42 @@ class DumpCompiler(object):
'gdump.c')
if not os.path.isfile(gdump_path):
raise SystemExit("Couldn't find %r" % (gdump_path, ))
- gdump_file = open(gdump_path)
- gdump_contents = gdump_file.read()
- gdump_file.close()
+ with open(gdump_path) 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')
- f = open(c_path, 'w')
- f.write(_PROGRAM_TEMPLATE % tpl_args)
-
- # We need to reference our get_type and error_quark functions
- # to make sure they are pulled in at the linking stage if the
- # library is a static library rather than a shared library.
- if len(self._get_type_functions) > 0:
- for func in self._get_type_functions:
- f.write("extern GType " + func + "(void);\n")
- f.write("GType (*GI_GET_TYPE_FUNCS_[])(void) = {\n")
- first = True
- for func in self._get_type_functions:
- if first:
- first = False
- else:
- f.write(",\n")
- f.write(" " + func)
- f.write("\n};\n")
- if len(self._error_quark_functions) > 0:
- for func in self._error_quark_functions:
- f.write("extern GQuark " + func + "(void);\n")
- f.write("GQuark (*GI_ERROR_QUARK_FUNCS_[])(void) = {\n")
- first = True
- for func in self._error_quark_functions:
- if first:
- first = False
- else:
- f.write(",\n")
- f.write(" " + func)
- f.write("\n};\n")
- f.close()
+ with open(c_path, 'w') as f:
+ f.write(_PROGRAM_TEMPLATE % tpl_args)
+
+ # We need to reference our get_type and error_quark functions
+ # to make sure they are pulled in at the linking stage if the
+ # library is a static library rather than a shared library.
+ if len(self._get_type_functions) > 0:
+ for func in self._get_type_functions:
+ f.write("extern GType " + func + "(void);\n")
+ f.write("GType (*GI_GET_TYPE_FUNCS_[])(void) = {\n")
+ first = True
+ for func in self._get_type_functions:
+ if first:
+ first = False
+ else:
+ f.write(",\n")
+ f.write(" " + func)
+ f.write("\n};\n")
+ if len(self._error_quark_functions) > 0:
+ for func in self._error_quark_functions:
+ f.write("extern GQuark " + func + "(void);\n")
+ f.write("GQuark (*GI_ERROR_QUARK_FUNCS_[])(void) = {\n")
+ first = True
+ for func in self._error_quark_functions:
+ if first:
+ first = False
+ else:
+ f.write(",\n")
+ f.write(" " + func)
+ f.write("\n};\n")
# Microsoft compilers generate intermediate .obj files
# during compilation, unlike .o files like GCC and others
@@ -307,11 +305,10 @@ class DumpCompiler(object):
# Create a temporary script file that
# runs the command we want
tf, tf_name = tempfile.mkstemp()
- f = os.fdopen(tf, 'wb')
- shellcontents = ' '.join([x.replace('\\', '/') for x in args])
- fcontents = '#!/bin/sh\nunset PWD\n{}\n'.format(shellcontents)
- f.write(fcontents)
- f.close()
+ with os.fdopen(tf, 'wb') as f:
+ shellcontents = ' '.join([x.replace('\\', '/') for x in args])
+ fcontents = '#!/bin/sh\nunset PWD\n{}\n'.format(shellcontents)
+ f.write(fcontents)
shell = utils.which(shell)
args = [shell, tf_name.replace('\\', '/')]
try: