Skip to content

Commit 4c5d9c7

Browse files
committed
Close stdout in finally block
If there was an exception, the stdout resource was not closed. This patch also moves the process creation out of the try/catch block, reducing the scope of this huge try block. Fixes following ignored exception with python3: Exception ignored in: <_io.FileIO name=6 mode='rb' closefd=True> ResourceWarning: unclosed file <_io.BufferedReader name=6>
1 parent 1bc25ba commit 4c5d9c7

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

pygccxml/parser/source_reader.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,19 +300,20 @@ def create_xml_file(self, source_file, destination=None):
300300
utils.remove_file_no_raise(xml_file, self.__config)
301301
else:
302302
xml_file = utils.create_temp_file_name(suffix='.xml')
303-
try:
304-
ffname = source_file
305-
if not os.path.isabs(ffname):
306-
ffname = self.__file_full_name(source_file)
307-
command_line = self.__create_command_line(ffname, xml_file)
308-
309-
process = subprocess.Popen(
310-
args=command_line,
311-
shell=True,
312-
stdin=subprocess.PIPE,
313-
stdout=subprocess.PIPE)
314-
process.stdin.close()
315303

304+
ffname = source_file
305+
if not os.path.isabs(ffname):
306+
ffname = self.__file_full_name(source_file)
307+
command_line = self.__create_command_line(ffname, xml_file)
308+
309+
process = subprocess.Popen(
310+
args=command_line,
311+
shell=True,
312+
stdin=subprocess.PIPE,
313+
stdout=subprocess.PIPE)
314+
process.stdin.close()
315+
316+
try:
316317
gccxml_reports = []
317318
while process.poll() is None:
318319
line = process.stdout.readline()
@@ -344,10 +345,11 @@ def create_xml_file(self, source_file, destination=None):
344345
"Error occurred while running " +
345346
self.__config.xml_generator.upper() +
346347
": %s status:%s" % (gccxml_msg, exit_status))
347-
348348
except Exception:
349349
utils.remove_file_no_raise(xml_file, self.__config)
350350
raise
351+
finally:
352+
process.stdout.close()
351353
return xml_file
352354

353355
def create_xml_file_from_string(self, content, destination=None):

0 commit comments

Comments
 (0)