Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c6a951

Browse files
committed
Add cleanups to stdout/stderr pipes to remove ResourceWarnings.
1 parent b5d8204 commit 3c6a951

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

Lib/test/test_subprocess.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ def test_stdin_none(self):
121121
# .stdin is None when not redirected
122122
p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
123123
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
124+
self.addCleanup(p.stdout.close)
125+
self.addCleanup(p.stderr.close)
124126
p.wait()
125127
self.assertEqual(p.stdin, None)
126128

@@ -131,13 +133,17 @@ def test_stdout_none(self):
131133
'test of stdout in a different '
132134
'process ...")'],
133135
stdin=subprocess.PIPE, stderr=subprocess.PIPE)
136+
self.addCleanup(p.stdin.close)
137+
self.addCleanup(p.stderr.close)
134138
p.wait()
135139
self.assertEqual(p.stdout, None)
136140

137141
def test_stderr_none(self):
138142
# .stderr is None when not redirected
139143
p = subprocess.Popen([sys.executable, "-c", 'print("banana")'],
140144
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
145+
self.addCleanup(p.stdout.close)
146+
self.addCleanup(p.stdin.close)
141147
p.wait()
142148
self.assertEqual(p.stderr, None)
143149

@@ -200,6 +206,7 @@ def test_stdout_pipe(self):
200206
p = subprocess.Popen([sys.executable, "-c",
201207
'import sys; sys.stdout.write("orange")'],
202208
stdout=subprocess.PIPE)
209+
self.addCleanup(p.stdout.close)
203210
self.assertEqual(p.stdout.read(), b"orange")
204211

205212
def test_stdout_filedes(self):
@@ -230,6 +237,7 @@ def test_stderr_pipe(self):
230237
p = subprocess.Popen([sys.executable, "-c",
231238
'import sys; sys.stderr.write("strawberry")'],
232239
stderr=subprocess.PIPE)
240+
self.addCleanup(p.stderr.close)
233241
self.assertStderrEqual(p.stderr.read(), b"strawberry")
234242

235243
def test_stderr_filedes(self):
@@ -264,6 +272,7 @@ def test_stdout_stderr_pipe(self):
264272
'sys.stderr.write("orange")'],
265273
stdout=subprocess.PIPE,
266274
stderr=subprocess.STDOUT)
275+
self.addCleanup(p.stdout.close)
267276
self.assertStderrEqual(p.stdout.read(), b"appleorange")
268277

269278
def test_stdout_stderr_file(self):
@@ -300,6 +309,7 @@ def test_cwd(self):
300309
'sys.stdout.write(os.getcwd())'],
301310
stdout=subprocess.PIPE,
302311
cwd=tmpdir)
312+
self.addCleanup(p.stdout.close)
303313
normcase = os.path.normcase
304314
self.assertEqual(normcase(p.stdout.read().decode("utf-8")),
305315
normcase(tmpdir))
@@ -312,6 +322,7 @@ def test_env(self):
312322
'sys.stdout.write(os.getenv("FRUIT"))'],
313323
stdout=subprocess.PIPE,
314324
env=newenv)
325+
self.addCleanup(p.stdout.close)
315326
self.assertEqual(p.stdout.read(), b"orange")
316327

317328
def test_communicate_stdin(self):
@@ -427,6 +438,7 @@ def test_universal_newlines(self):
427438
'sys.stdout.write("\\nline6");'],
428439
stdout=subprocess.PIPE,
429440
universal_newlines=1)
441+
self.addCleanup(p.stdout.close)
430442
stdout = p.stdout.read()
431443
self.assertEqual(stdout, "line1\nline2\nline3\nline4\nline5\nline6")
432444

@@ -699,6 +711,7 @@ def test_preexec(self):
699711
'sys.stdout.write(os.getenv("FRUIT"))'],
700712
stdout=subprocess.PIPE,
701713
preexec_fn=lambda: os.putenv("FRUIT", "apple"))
714+
self.addCleanup(p.stdout.close)
702715
self.assertEqual(p.stdout.read(), b"apple")
703716

704717
def test_preexec_exception(self):
@@ -787,6 +800,7 @@ def test_shell_sequence(self):
787800
p = subprocess.Popen(["echo $FRUIT"], shell=1,
788801
stdout=subprocess.PIPE,
789802
env=newenv)
803+
self.addCleanup(p.stdout.close)
790804
self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
791805

792806
def test_shell_string(self):
@@ -796,6 +810,7 @@ def test_shell_string(self):
796810
p = subprocess.Popen("echo $FRUIT", shell=1,
797811
stdout=subprocess.PIPE,
798812
env=newenv)
813+
self.addCleanup(p.stdout.close)
799814
self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
800815

801816
def test_call_string(self):
@@ -828,6 +843,7 @@ def test_specific_shell(self):
828843
for sh in shells:
829844
p = subprocess.Popen("echo $0", executable=sh, shell=True,
830845
stdout=subprocess.PIPE)
846+
self.addCleanup(p.stdout.close)
831847
self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii'))
832848

833849
def _kill_process(self, method, *args):

0 commit comments

Comments
 (0)