Skip to content

Commit c3ad30d

Browse files
Médéric RIBREUXm-kuhn
Médéric RIBREUX
authored andcommitted
Fix subprocess unclosed file
1 parent 5ef16bf commit c3ad30d

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

python/plugins/processing/algs/grass7/Grass7Utils.py

+25-25
Original file line numberDiff line numberDiff line change
@@ -254,26 +254,26 @@ def executeGrass7(commands, progress, outputCommands=None):
254254
loglines.append(Grass7Utils.tr('GRASS GIS 7 execution console output'))
255255
grassOutDone = False
256256
command, grassenv = Grass7Utils.prepareGrass7Execution(commands)
257-
proc = subprocess.Popen(
257+
with subprocess.Popen(
258258
command,
259259
shell=True,
260260
stdout=subprocess.PIPE,
261261
stdin=subprocess.DEVNULL,
262262
stderr=subprocess.STDOUT,
263263
universal_newlines=True,
264264
env=grassenv
265-
).stdout
266-
for line in iter(proc.readline, ''):
267-
if 'GRASS_INFO_PERCENT' in line:
268-
try:
269-
progress.setPercentage(int(line[len('GRASS_INFO_PERCENT') + 2:]))
270-
except:
271-
pass
272-
else:
273-
if 'r.out' in line or 'v.out' in line:
274-
grassOutDone = True
275-
loglines.append(line)
276-
progress.setConsoleInfo(line)
265+
) as proc:
266+
for line in iter(proc.stdout.readline, ''):
267+
if 'GRASS_INFO_PERCENT' in line:
268+
try:
269+
progress.setPercentage(int(line[len('GRASS_INFO_PERCENT') + 2:]))
270+
except:
271+
pass
272+
else:
273+
if 'r.out' in line or 'v.out' in line:
274+
grassOutDone = True
275+
loglines.append(line)
276+
progress.setConsoleInfo(line)
277277

278278
# Some GRASS scripts, like r.mapcalculator or r.fillnulls, call
279279
# other GRASS scripts during execution. This may override any
@@ -283,25 +283,25 @@ def executeGrass7(commands, progress, outputCommands=None):
283283

284284
if not grassOutDone and outputCommands:
285285
command, grassenv = Grass7Utils.prepareGrass7Execution(outputCommands)
286-
proc = subprocess.Popen(
286+
with subprocess.Popen(
287287
command,
288288
shell=True,
289289
stdout=subprocess.PIPE,
290290
stdin=subprocess.DEVNULL,
291291
stderr=subprocess.STDOUT,
292292
universal_newlines=True,
293293
env=grassenv
294-
).stdout
295-
for line in iter(proc.readline, ''):
296-
if 'GRASS_INFO_PERCENT' in line:
297-
try:
298-
progress.setPercentage(int(
299-
line[len('GRASS_INFO_PERCENT') + 2:]))
300-
except:
301-
pass
302-
else:
303-
loglines.append(line)
304-
progress.setConsoleInfo(line)
294+
) as proc:
295+
for line in iter(proc.stdout.readline, ''):
296+
if 'GRASS_INFO_PERCENT' in line:
297+
try:
298+
progress.setPercentage(int(
299+
line[len('GRASS_INFO_PERCENT') + 2:]))
300+
except:
301+
pass
302+
else:
303+
loglines.append(line)
304+
progress.setConsoleInfo(line)
305305

306306
if ProcessingConfig.getSetting(Grass7Utils.GRASS_LOG_CONSOLE):
307307
ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)

0 commit comments

Comments
 (0)