Skip to content

Commit c49770c

Browse files
authoredAug 30, 2024··
[NFC] Prefer subprocess.DEVNULL over os.devnull (#106500)
There is no need to support Python 2.7 anymore, Python 3.3+ has `subprocess.DEVNULL`. This is good practice and also prevents file handles from staying open unnecessarily. Also remove a couple unused or unneeded `__future__` imports.
1 parent 0717898 commit c49770c

File tree

10 files changed

+25
-45
lines changed

10 files changed

+25
-45
lines changed
 

‎clang-tools-extra/clang-tidy/tool/run-clang-tidy.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,10 @@ async def main() -> None:
511511
)
512512
invocation.append("-list-checks")
513513
invocation.append("-")
514-
if args.quiet:
515-
# Even with -quiet we still want to check if we can call clang-tidy.
516-
with open(os.devnull, "w") as dev_null:
517-
subprocess.check_call(invocation, stdout=dev_null)
518-
else:
519-
subprocess.check_call(invocation)
514+
# Even with -quiet we still want to check if we can call clang-tidy.
515+
subprocess.check_call(
516+
invocation, stdout=subprocess.DEVNULL if args.quiet else None
517+
)
520518
except:
521519
print("Unable to run clang-tidy.", file=sys.stderr)
522520
sys.exit(1)

‎clang/docs/tools/generate_formatted_state.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ def get_style(count, passed):
7878
- {style2}`{percent}%`
7979
"""
8080

81-
FNULL = open(os.devnull, "w")
82-
8381

8482
with open(DOC_FILE, "wb") as output:
8583
cleanfiles = open(CLEAN_FILE, "wb")
@@ -101,8 +99,8 @@ def get_style(count, passed):
10199
# interested in it, just the return code.
102100
git_check = subprocess.Popen(
103101
["git", "ls-files", "--error-unmatch", act_sub_dir],
104-
stdout=FNULL,
105-
stderr=FNULL,
102+
stdout=subprocess.DEVNULL,
103+
stderr=subprocess.DEVNULL,
106104
)
107105
if git_check.wait() != 0:
108106
print("Skipping directory: ", act_sub_dir)

‎clang/tools/scan-view/share/startfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def _invoke(self, cmdline):
4848
or sys.platform[:3] == "win"
4949
or sys.platform == "darwin"
5050
):
51-
inout = file(os.devnull, "r+")
51+
inout = subprocess.DEVNULL
5252
else:
5353
# for TTY programs, we need stdin/out
5454
inout = None

‎clang/utils/creduce-clang-crash.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*.test.sh -- interestingness test for C-Reduce
99
"""
1010

11-
from __future__ import print_function
1211
from argparse import ArgumentParser, RawTextHelpFormatter
1312
import os
1413
import re
@@ -228,8 +227,7 @@ def check_interestingness(self):
228227
testfile = os.path.abspath(self.testfile)
229228

230229
# Check that the test considers the original file interesting
231-
with open(os.devnull, "w") as devnull:
232-
returncode = subprocess.call(testfile, stdout=devnull)
230+
returncode = subprocess.call(testfile, stdout=subprocess.DEVNULL)
233231
if returncode:
234232
sys.exit("The interestingness test does not pass for the original file.")
235233

‎lldb/bindings/interface/SBErrorDocstrings.i

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ For example (from test/python_api/hello_world/TestHelloWorld.py), ::
1010
1111
# Spawn a new process and don't display the stdout if not in TraceOn() mode.
1212
import subprocess
13-
popen = subprocess.Popen([self.exe, 'abc', 'xyz'],
14-
stdout = open(os.devnull, 'w') if not self.TraceOn() else None)
13+
popen = subprocess.Popen(
14+
[self.exe, 'abc', 'xyz'],
15+
stdout=subprocess.DEVNULL if not self.TraceOn() else None,
16+
)
1517
1618
listener = lldb.SBListener('my.attach.listener')
1719
error = lldb.SBError()

‎lldb/packages/Python/lldbsuite/test/decorators.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,8 @@ def should_skip_simulator_test():
467467
if lldbplatformutil.getHostPlatform() not in ["darwin", "macosx"]:
468468
return "simulator tests are run only on darwin hosts."
469469
try:
470-
DEVNULL = open(os.devnull, "w")
471470
output = subprocess.check_output(
472-
["xcodebuild", "-showsdks"], stderr=DEVNULL
471+
["xcodebuild", "-showsdks"], stderr=subprocess.DEVNULL
473472
).decode("utf-8")
474473
if re.search("%ssimulator" % platform, output):
475474
return None
@@ -1094,9 +1093,8 @@ def skipUnlessFeature(feature):
10941093
def is_feature_enabled():
10951094
if platform.system() == "Darwin":
10961095
try:
1097-
DEVNULL = open(os.devnull, "w")
10981096
output = subprocess.check_output(
1099-
["/usr/sbin/sysctl", feature], stderr=DEVNULL
1097+
["/usr/sbin/sysctl", feature], stderr=subprocess.DEVNULL
11001098
).decode("utf-8")
11011099
# If 'feature: 1' was output, then this feature is available and
11021100
# the test should not be skipped.

‎lldb/packages/Python/lldbsuite/test/lldbtest.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import abc
3232
from functools import wraps
3333
import gc
34-
import glob
3534
import io
3635
import json
3736
import os.path
@@ -416,7 +415,7 @@ def launch(self, executable, args, extra_env):
416415

417416
self._proc = Popen(
418417
[executable] + args,
419-
stdout=open(os.devnull) if not self._trace_on else None,
418+
stdout=DEVNULL if not self._trace_on else None,
420419
stdin=PIPE,
421420
env=env,
422421
)

‎llvm/utils/UpdateTestChecks/common.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
from __future__ import print_function
2-
31
import argparse
42
import bisect
53
import collections
64
import copy
75
import glob
8-
import itertools
96
import os
107
import re
118
import subprocess
@@ -517,12 +514,13 @@ def invoke_tool(exe, cmd_args, ir, preprocess_cmd=None, verbose=False):
517514
sep="",
518515
file=sys.stderr,
519516
)
520-
# Python 2.7 doesn't have subprocess.DEVNULL:
521-
with open(os.devnull, "w") as devnull:
522-
pp = subprocess.Popen(
523-
preprocess_cmd, shell=True, stdin=devnull, stdout=subprocess.PIPE
524-
)
525-
ir_file = pp.stdout
517+
pp = subprocess.Popen(
518+
preprocess_cmd,
519+
shell=True,
520+
stdin=subprocess.DEVNULL,
521+
stdout=subprocess.PIPE,
522+
)
523+
ir_file = pp.stdout
526524

527525
if isinstance(cmd_args, list):
528526
args = [applySubstitutions(a, substitutions) for a in cmd_args]

‎llvm/utils/git/pre-push.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"""
2828

2929
import argparse
30-
import os
3130
import shutil
3231
import subprocess
3332
import sys
@@ -70,14 +69,6 @@ def ask_confirm(prompt):
7069
return query.lower() == "y"
7170

7271

73-
def get_dev_null():
74-
"""Lazily create a /dev/null fd for use in shell()"""
75-
global dev_null_fd
76-
if dev_null_fd is None:
77-
dev_null_fd = open(os.devnull, "w")
78-
return dev_null_fd
79-
80-
8172
def shell(
8273
cmd,
8374
strip=True,
@@ -95,10 +86,8 @@ def shell(
9586
cwd_msg = " in %s" % cwd
9687
log_verbose("Running%s: %s" % (cwd_msg, " ".join(quoted_cmd)))
9788

98-
err_pipe = subprocess.PIPE
99-
if ignore_errors:
100-
# Silence errors if requested.
101-
err_pipe = get_dev_null()
89+
# Silence errors if requested.
90+
err_pipe = subprocess.DEVNULL if ignore_errors else subprocess.PIPE
10291

10392
start = time.time()
10493
p = subprocess.Popen(

‎llvm/utils/gn/gn.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def main():
4242
if (
4343
subprocess.call(
4444
"gn --version",
45-
stdout=open(os.devnull, "w"),
45+
stdout=subprocess.DEVNULL,
4646
stderr=subprocess.STDOUT,
4747
shell=True,
4848
)

0 commit comments

Comments
 (0)
Please sign in to comment.