Skip to content

Commit 7e07eca

Browse files
[3.12] gh-110184: Fix subprocess test_pipesize_default() (GH-110465) (#110471)
gh-110184: Fix subprocess test_pipesize_default() (GH-110465) For proc.stdin, get the size of the read end of the test pipe. Use subprocess context manager ("with proc:"). (cherry picked from commit d023d41) Co-authored-by: Victor Stinner <[email protected]>
1 parent eed92e4 commit 7e07eca

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

Lib/test/test_subprocess.py

+23-18
Original file line numberDiff line numberDiff line change
@@ -749,31 +749,36 @@ def test_pipesizes(self):
749749
@unittest.skipUnless(fcntl and hasattr(fcntl, 'F_GETPIPE_SZ'),
750750
'fcntl.F_GETPIPE_SZ required for test.')
751751
def test_pipesize_default(self):
752-
p = subprocess.Popen(
752+
proc = subprocess.Popen(
753753
[sys.executable, "-c",
754754
'import sys; sys.stdin.read(); sys.stdout.write("out"); '
755755
'sys.stderr.write("error!")'],
756756
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
757757
stderr=subprocess.PIPE, pipesize=-1)
758-
try:
759-
fp_r, fp_w = os.pipe()
758+
759+
with proc:
760760
try:
761-
default_pipesize = fcntl.fcntl(fp_w, fcntl.F_GETPIPE_SZ)
762-
for fifo in [p.stdin, p.stdout, p.stderr]:
763-
self.assertEqual(
764-
fcntl.fcntl(fifo.fileno(), fcntl.F_GETPIPE_SZ),
765-
default_pipesize)
761+
fp_r, fp_w = os.pipe()
762+
try:
763+
default_read_pipesize = fcntl.fcntl(fp_r, fcntl.F_GETPIPE_SZ)
764+
default_write_pipesize = fcntl.fcntl(fp_w, fcntl.F_GETPIPE_SZ)
765+
finally:
766+
os.close(fp_r)
767+
os.close(fp_w)
768+
769+
self.assertEqual(
770+
fcntl.fcntl(proc.stdin.fileno(), fcntl.F_GETPIPE_SZ),
771+
default_read_pipesize)
772+
self.assertEqual(
773+
fcntl.fcntl(proc.stdout.fileno(), fcntl.F_GETPIPE_SZ),
774+
default_write_pipesize)
775+
self.assertEqual(
776+
fcntl.fcntl(proc.stderr.fileno(), fcntl.F_GETPIPE_SZ),
777+
default_write_pipesize)
778+
# On other platforms we cannot test the pipe size (yet). But above
779+
# code using pipesize=-1 should not crash.
766780
finally:
767-
os.close(fp_r)
768-
os.close(fp_w)
769-
# On other platforms we cannot test the pipe size (yet). But above
770-
# code using pipesize=-1 should not crash.
771-
p.stdin.close()
772-
p.stdout.close()
773-
p.stderr.close()
774-
finally:
775-
p.kill()
776-
p.wait()
781+
proc.kill()
777782

778783
def test_env(self):
779784
newenv = os.environ.copy()

0 commit comments

Comments
 (0)