Simplify using subprocess.DEVNULL

Co-Authored-By: Jon Dufresne <jon.dufresne@gmail.com>
This commit is contained in:
Hugo 2019-10-08 11:24:36 +03:00
parent 6cd99fc3cf
commit 84e53e3757
5 changed files with 20 additions and 23 deletions

View File

@ -321,11 +321,10 @@ def command_succeeds(cmd):
Runs the command, which must be a list of strings. Returns True if the
command succeeds, or False if an OSError was raised by subprocess.Popen.
"""
with open(os.devnull, "wb") as f:
try:
subprocess.call(cmd, stdout=f, stderr=subprocess.STDOUT)
except OSError:
return False
try:
subprocess.call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
except OSError:
return False
return True

View File

@ -164,10 +164,10 @@ def _find_library_dirs_ldconfig():
expr = r".* => (.*)"
env = {}
null = open(os.devnull, "wb")
try:
with null:
p = subprocess.Popen(args, stderr=null, stdout=subprocess.PIPE, env=env)
p = subprocess.Popen(
args, stderr=subprocess.DEVNULL, stdout=subprocess.PIPE, env=env
)
except OSError: # E.g. command not found
return []
[data, _] = p.communicate()

View File

@ -57,8 +57,7 @@ def has_ghostscript():
return True
if not sys.platform.startswith("win"):
try:
with open(os.devnull, "wb") as devnull:
subprocess.check_call(["gs", "--version"], stdout=devnull)
subprocess.check_call(["gs", "--version"], stdout=subprocess.DEVNULL)
return True
except OSError:
# No Ghostscript

View File

@ -623,20 +623,20 @@ def _save_netpbm(im, fp, filename):
with open(filename, "wb") as f:
if im.mode != "RGB":
with open(os.devnull, "wb") as devnull:
subprocess.check_call(["ppmtogif", tempfile], stdout=f, stderr=devnull)
subprocess.check_call(
["ppmtogif", tempfile], stdout=f, stderr=subprocess.DEVNULL
)
else:
# Pipe ppmquant output into ppmtogif
# "ppmquant 256 %s | ppmtogif > %s" % (tempfile, filename)
quant_cmd = ["ppmquant", "256", tempfile]
togif_cmd = ["ppmtogif"]
with open(os.devnull, "wb") as devnull:
quant_proc = subprocess.Popen(
quant_cmd, stdout=subprocess.PIPE, stderr=devnull
)
togif_proc = subprocess.Popen(
togif_cmd, stdin=quant_proc.stdout, stdout=f, stderr=devnull
)
quant_proc = subprocess.Popen(
quant_cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL
)
togif_proc = subprocess.Popen(
togif_cmd, stdin=quant_proc.stdout, stdout=f, stderr=subprocess.DEVNULL
)
# Allow ppmquant to receive SIGPIPE if ppmtogif exits
quant_proc.stdout.close()

View File

@ -336,10 +336,9 @@ def _save(im, fp, filename):
# iconutil -c icns -o {} {}
convert_cmd = ["iconutil", "-c", "icns", "-o", filename, iconset]
with open(os.devnull, "wb") as devnull:
convert_proc = subprocess.Popen(
convert_cmd, stdout=subprocess.PIPE, stderr=devnull
)
convert_proc = subprocess.Popen(
convert_cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL
)
convert_proc.stdout.close()