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 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. command succeeds, or False if an OSError was raised by subprocess.Popen.
""" """
with open(os.devnull, "wb") as f: try:
try: subprocess.call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
subprocess.call(cmd, stdout=f, stderr=subprocess.STDOUT) except OSError:
except OSError: return False
return False
return True return True

View File

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

View File

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

View File

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

View File

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