Commit 7438c612 authored by Victor Stinner's avatar Victor Stinner

Use "with popen:" in test_subprocess

Issue #26741.
parent 19ed27ec
...@@ -443,7 +443,7 @@ class ProcessTestCase(BaseTestCase): ...@@ -443,7 +443,7 @@ class ProcessTestCase(BaseTestCase):
p = subprocess.Popen([sys.executable, "-c", p = subprocess.Popen([sys.executable, "-c",
'import sys; sys.stdout.write("orange")'], 'import sys; sys.stdout.write("orange")'],
stdout=subprocess.PIPE) stdout=subprocess.PIPE)
self.addCleanup(p.stdout.close) with p:
self.assertEqual(p.stdout.read(), b"orange") self.assertEqual(p.stdout.read(), b"orange")
def test_stdout_filedes(self): def test_stdout_filedes(self):
...@@ -474,7 +474,7 @@ class ProcessTestCase(BaseTestCase): ...@@ -474,7 +474,7 @@ class ProcessTestCase(BaseTestCase):
p = subprocess.Popen([sys.executable, "-c", p = subprocess.Popen([sys.executable, "-c",
'import sys; sys.stderr.write("strawberry")'], 'import sys; sys.stderr.write("strawberry")'],
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
self.addCleanup(p.stderr.close) with p:
self.assertStderrEqual(p.stderr.read(), b"strawberry") self.assertStderrEqual(p.stderr.read(), b"strawberry")
def test_stderr_filedes(self): def test_stderr_filedes(self):
...@@ -530,7 +530,7 @@ class ProcessTestCase(BaseTestCase): ...@@ -530,7 +530,7 @@ class ProcessTestCase(BaseTestCase):
'sys.stderr.write("orange")'], 'sys.stderr.write("orange")'],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT)
self.addCleanup(p.stdout.close) with p:
self.assertStderrEqual(p.stdout.read(), b"appleorange") self.assertStderrEqual(p.stdout.read(), b"appleorange")
def test_stdout_stderr_file(self): def test_stdout_stderr_file(self):
...@@ -789,6 +789,7 @@ class ProcessTestCase(BaseTestCase): ...@@ -789,6 +789,7 @@ class ProcessTestCase(BaseTestCase):
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
universal_newlines=1) universal_newlines=1)
with p:
p.stdin.write("line1\n") p.stdin.write("line1\n")
p.stdin.flush() p.stdin.flush()
self.assertEqual(p.stdout.readline(), "line1\n") self.assertEqual(p.stdout.readline(), "line1\n")
...@@ -1434,7 +1435,7 @@ class POSIXProcessTestCase(BaseTestCase): ...@@ -1434,7 +1435,7 @@ class POSIXProcessTestCase(BaseTestCase):
'sys.stdout.write(os.getenv("FRUIT"))'], 'sys.stdout.write(os.getenv("FRUIT"))'],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
preexec_fn=lambda: os.putenv("FRUIT", "apple")) preexec_fn=lambda: os.putenv("FRUIT", "apple"))
self.addCleanup(p.stdout.close) with p:
self.assertEqual(p.stdout.read(), b"apple") self.assertEqual(p.stdout.read(), b"apple")
def test_preexec_exception(self): def test_preexec_exception(self):
...@@ -1583,7 +1584,7 @@ class POSIXProcessTestCase(BaseTestCase): ...@@ -1583,7 +1584,7 @@ class POSIXProcessTestCase(BaseTestCase):
p = subprocess.Popen(["echo $FRUIT"], shell=1, p = subprocess.Popen(["echo $FRUIT"], shell=1,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
env=newenv) env=newenv)
self.addCleanup(p.stdout.close) with p:
self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple") self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
def test_shell_string(self): def test_shell_string(self):
...@@ -1593,7 +1594,7 @@ class POSIXProcessTestCase(BaseTestCase): ...@@ -1593,7 +1594,7 @@ class POSIXProcessTestCase(BaseTestCase):
p = subprocess.Popen("echo $FRUIT", shell=1, p = subprocess.Popen("echo $FRUIT", shell=1,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
env=newenv) env=newenv)
self.addCleanup(p.stdout.close) with p:
self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple") self.assertEqual(p.stdout.read().strip(b" \t\r\n\f"), b"apple")
def test_call_string(self): def test_call_string(self):
...@@ -1626,7 +1627,7 @@ class POSIXProcessTestCase(BaseTestCase): ...@@ -1626,7 +1627,7 @@ class POSIXProcessTestCase(BaseTestCase):
for sh in shells: for sh in shells:
p = subprocess.Popen("echo $0", executable=sh, shell=True, p = subprocess.Popen("echo $0", executable=sh, shell=True,
stdout=subprocess.PIPE) stdout=subprocess.PIPE)
self.addCleanup(p.stdout.close) with p:
self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii')) self.assertEqual(p.stdout.read().strip(), bytes(sh, 'ascii'))
def _kill_process(self, method, *args): def _kill_process(self, method, *args):
...@@ -2450,7 +2451,7 @@ class Win32ProcessTestCase(BaseTestCase): ...@@ -2450,7 +2451,7 @@ class Win32ProcessTestCase(BaseTestCase):
p = subprocess.Popen(["set"], shell=1, p = subprocess.Popen(["set"], shell=1,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
env=newenv) env=newenv)
self.addCleanup(p.stdout.close) with p:
self.assertIn(b"physalis", p.stdout.read()) self.assertIn(b"physalis", p.stdout.read())
def test_shell_string(self): def test_shell_string(self):
...@@ -2460,7 +2461,7 @@ class Win32ProcessTestCase(BaseTestCase): ...@@ -2460,7 +2461,7 @@ class Win32ProcessTestCase(BaseTestCase):
p = subprocess.Popen("set", shell=1, p = subprocess.Popen("set", shell=1,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
env=newenv) env=newenv)
self.addCleanup(p.stdout.close) with p:
self.assertIn(b"physalis", p.stdout.read()) self.assertIn(b"physalis", p.stdout.read())
def test_call_string(self): def test_call_string(self):
...@@ -2480,9 +2481,7 @@ class Win32ProcessTestCase(BaseTestCase): ...@@ -2480,9 +2481,7 @@ class Win32ProcessTestCase(BaseTestCase):
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
self.addCleanup(p.stdout.close) with p:
self.addCleanup(p.stderr.close)
self.addCleanup(p.stdin.close)
# Wait for the interpreter to be completely initialized before # Wait for the interpreter to be completely initialized before
# sending any signal. # sending any signal.
p.stdout.read(1) p.stdout.read(1)
...@@ -2502,9 +2501,7 @@ class Win32ProcessTestCase(BaseTestCase): ...@@ -2502,9 +2501,7 @@ class Win32ProcessTestCase(BaseTestCase):
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
self.addCleanup(p.stdout.close) with p:
self.addCleanup(p.stderr.close)
self.addCleanup(p.stdin.close)
# Wait for the interpreter to be completely initialized before # Wait for the interpreter to be completely initialized before
# sending any signal. # sending any signal.
p.stdout.read(1) p.stdout.read(1)
...@@ -2602,7 +2599,7 @@ class CommandsWithSpaces (BaseTestCase): ...@@ -2602,7 +2599,7 @@ class CommandsWithSpaces (BaseTestCase):
def with_spaces(self, *args, **kwargs): def with_spaces(self, *args, **kwargs):
kwargs['stdout'] = subprocess.PIPE kwargs['stdout'] = subprocess.PIPE
p = subprocess.Popen(*args, **kwargs) p = subprocess.Popen(*args, **kwargs)
self.addCleanup(p.stdout.close) with p:
self.assertEqual( self.assertEqual(
p.stdout.read ().decode("mbcs"), p.stdout.read ().decode("mbcs"),
"2 [%r, 'ab cd']" % self.fname "2 [%r, 'ab cd']" % self.fname
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment