Skip to content

Commit

Permalink
Merge pull request #208 from oesteban/fix/close-fds
Browse files Browse the repository at this point in the history
[FIX] Close file descriptors
  • Loading branch information
oesteban authored Oct 31, 2017
2 parents 66c0aed + e797409 commit a1c98e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions niworkflows/viz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def which(cmd):
"""

try:
subprocess.run([cmd], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
subprocess.run([cmd], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
close_fds=True)
except OSError as e:
from errno import ENOENT
if e.errno == ENOENT:
Expand All @@ -63,7 +64,8 @@ def which(cmd):
setattr(subprocess, 'DEVNULL', -3)

if not hasattr(subprocess, 'run'):
def _run(args, input=None, stdout=None, stderr=None, shell=False, check=False):
def _run(args, input=None, stdout=None, stderr=None, shell=False, check=False,
close_fds=False):
from collections import namedtuple

devnull = open(os.devnull, 'r+')
Expand All @@ -75,7 +77,8 @@ def _run(args, input=None, stdout=None, stderr=None, shell=False, check=False):
if stderr == subprocess.DEVNULL:
stderr = devnull

proc = subprocess.Popen(args, stdout=stdout, shell=shell, stdin=stdin)
proc = subprocess.Popen(args, stdout=stdout, shell=shell, stdin=stdin,
close_fds=close_fds)
result = namedtuple('CompletedProcess', 'stdout stderr')
res = result(*proc.communicate(input=input))

Expand Down Expand Up @@ -144,7 +147,7 @@ def svg_compress(image, compress='auto'):
cmd = 'svgo -i - -o - -q -p 3 --pretty --disable=cleanupNumericValues'
try:
pout = subprocess.run(cmd, input=image.encode('utf-8'), stdout=subprocess.PIPE,
shell=True, check=True).stdout
shell=True, check=True, close_fds=True).stdout
except OSError as e:
from errno import ENOENT
if compress is True and e.errno == ENOENT:
Expand Down Expand Up @@ -172,8 +175,9 @@ def svg_compress(image, compress='auto'):
right = '"' + '"'.join(right.split('"')[1:])

cmd = "cwebp -quiet -noalpha -q 80 -o - -- -"
pout = subprocess.run(cmd, input=base64.b64decode(png_b64), shell=True,
stdout=subprocess.PIPE, check=True).stdout
pout = subprocess.run(
cmd, input=base64.b64decode(png_b64), shell=True,
stdout=subprocess.PIPE, check=True, close_fds=True).stdout
webpimg = base64.b64encode(pout).decode('utf-8')
new_lines.append(left + webpimg + right)
else:
Expand Down

0 comments on commit a1c98e4

Please sign in to comment.