Skip to content

Commit 49a45f6

Browse files
[3.11] gh-110184: Fix subprocess test_pipesize_default() (GH-110465) (#110472)
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 88223f1 commit 49a45f6

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

Lib/test/test_subprocess.py

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

776781
def test_env(self):
777782
newenv = os.environ.copy()

0 commit comments

Comments
 (0)