Skip to content

Commit 786addd

Browse files
authored
bpo-41586: Attempt to make the pipesize tests more robust. (GH-22839)
Several buildbots are failing on these, likely due to an inability to set the pipe size to the desired test value.
1 parent 7cdf30f commit 786addd

File tree

2 files changed

+71
-43
lines changed

2 files changed

+71
-43
lines changed

Lib/test/test_fcntl.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,24 @@ def test_fcntl_f_getpath(self):
190190
res = fcntl.fcntl(self.f.fileno(), fcntl.F_GETPATH, bytes(len(expected)))
191191
self.assertEqual(expected, res)
192192

193-
@unittest.skipIf(not (hasattr(fcntl, "F_SETPIPE_SZ") and hasattr(fcntl, "F_GETPIPE_SZ")),
194-
"F_SETPIPE_SZ and F_GETPIPE_SZ are not available on all unix platforms.")
193+
@unittest.skipUnless(
194+
hasattr(fcntl, "F_SETPIPE_SZ") and hasattr(fcntl, "F_GETPIPE_SZ"),
195+
"F_SETPIPE_SZ and F_GETPIPE_SZ are not available on all platforms.")
195196
def test_fcntl_f_pipesize(self):
196197
test_pipe_r, test_pipe_w = os.pipe()
197-
# Get the default pipesize with F_GETPIPE_SZ
198-
pipesize_default = fcntl.fcntl(test_pipe_w, fcntl.F_GETPIPE_SZ)
199-
# Multiply the default with 2 to get a new value.
200-
fcntl.fcntl(test_pipe_w, fcntl.F_SETPIPE_SZ, pipesize_default * 2)
201-
self.assertEqual(fcntl.fcntl(test_pipe_w, fcntl.F_GETPIPE_SZ), pipesize_default * 2)
202-
os.close(test_pipe_r)
203-
os.close(test_pipe_w)
198+
try:
199+
# Get the default pipesize with F_GETPIPE_SZ
200+
pipesize_default = fcntl.fcntl(test_pipe_w, fcntl.F_GETPIPE_SZ)
201+
pipesize = pipesize_default // 2 # A new value to detect change.
202+
if pipesize < 512: # the POSIX minimum
203+
raise unittest.SkitTest(
204+
'default pipesize too small to perform test.')
205+
fcntl.fcntl(test_pipe_w, fcntl.F_SETPIPE_SZ, pipesize)
206+
self.assertEqual(fcntl.fcntl(test_pipe_w, fcntl.F_GETPIPE_SZ),
207+
pipesize)
208+
finally:
209+
os.close(test_pipe_r)
210+
os.close(test_pipe_w)
204211

205212

206213
def test_main():

Lib/test/test_subprocess.py

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -666,45 +666,66 @@ def test_stdin_devnull(self):
666666
p.wait()
667667
self.assertEqual(p.stdin, None)
668668

669+
@unittest.skipUnless(fcntl and hasattr(fcntl, 'F_GETPIPE_SZ'),
670+
'fcntl.F_GETPIPE_SZ required for test.')
669671
def test_pipesizes(self):
670-
# stdin redirection
671-
pipesize = 16 * 1024
672-
p = subprocess.Popen([sys.executable, "-c",
673-
'import sys; sys.stdin.read(); sys.stdout.write("out"); sys.stderr.write("error!")'],
674-
stdin=subprocess.PIPE,
675-
stdout=subprocess.PIPE,
676-
stderr=subprocess.PIPE,
677-
pipesize=pipesize)
678-
# We only assert pipe size has changed on platforms that support it.
679-
if sys.platform != "win32" and hasattr(fcntl, "F_GETPIPE_SZ"):
672+
test_pipe_r, test_pipe_w = os.pipe()
673+
try:
674+
# Get the default pipesize with F_GETPIPE_SZ
675+
pipesize_default = fcntl.fcntl(test_pipe_w, fcntl.F_GETPIPE_SZ)
676+
finally:
677+
os.close(test_pipe_r)
678+
os.close(test_pipe_w)
679+
pipesize = pipesize_default // 2
680+
if pipesize < 512: # the POSIX minimum
681+
raise unittest.SkitTest(
682+
'default pipesize too small to perform test.')
683+
p = subprocess.Popen(
684+
[sys.executable, "-c",
685+
'import sys; sys.stdin.read(); sys.stdout.write("out"); '
686+
'sys.stderr.write("error!")'],
687+
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
688+
stderr=subprocess.PIPE, pipesize=pipesize)
689+
try:
680690
for fifo in [p.stdin, p.stdout, p.stderr]:
681-
self.assertEqual(fcntl.fcntl(fifo.fileno(), fcntl.F_GETPIPE_SZ), pipesize)
682-
# Windows pipe size can be acquired with the GetNamedPipeInfoFunction
683-
# https://docs.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-getnamedpipeinfo
684-
# However, this function is not yet in _winapi.
685-
p.stdin.write(b"pear")
686-
p.stdin.close()
687-
p.wait()
691+
self.assertEqual(
692+
fcntl.fcntl(fifo.fileno(), fcntl.F_GETPIPE_SZ),
693+
pipesize)
694+
# Windows pipe size can be acquired via GetNamedPipeInfoFunction
695+
# https://docs.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-getnamedpipeinfo
696+
# However, this function is not yet in _winapi.
697+
p.stdin.write(b"pear")
698+
p.stdin.close()
699+
finally:
700+
p.kill()
701+
p.wait()
688702

703+
@unittest.skipUnless(fcntl and hasattr(fcntl, 'F_GETPIPE_SZ'),
704+
'fcntl.F_GETPIPE_SZ required for test.')
689705
def test_pipesize_default(self):
690-
p = subprocess.Popen([sys.executable, "-c",
691-
'import sys; sys.stdin.read(); sys.stdout.write("out");'
692-
' sys.stderr.write("error!")'],
693-
stdin=subprocess.PIPE,
694-
stdout=subprocess.PIPE,
695-
stderr=subprocess.PIPE,
696-
pipesize=-1)
697-
# UNIX tests using fcntl
698-
if sys.platform != "win32" and hasattr(fcntl, "F_GETPIPE_SZ"):
706+
p = subprocess.Popen(
707+
[sys.executable, "-c",
708+
'import sys; sys.stdin.read(); sys.stdout.write("out"); '
709+
'sys.stderr.write("error!")'],
710+
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
711+
stderr=subprocess.PIPE, pipesize=-1)
712+
try:
699713
fp_r, fp_w = os.pipe()
700-
default_pipesize = fcntl.fcntl(fp_w, fcntl.F_GETPIPE_SZ)
701-
for fifo in [p.stdin, p.stdout, p.stderr]:
702-
self.assertEqual(
703-
fcntl.fcntl(fifo.fileno(), fcntl.F_GETPIPE_SZ), default_pipesize)
704-
# On other platforms we cannot test the pipe size (yet). But above code
705-
# using pipesize=-1 should not crash.
706-
p.stdin.close()
707-
p.wait()
714+
try:
715+
default_pipesize = fcntl.fcntl(fp_w, fcntl.F_GETPIPE_SZ)
716+
for fifo in [p.stdin, p.stdout, p.stderr]:
717+
self.assertEqual(
718+
fcntl.fcntl(fifo.fileno(), fcntl.F_GETPIPE_SZ),
719+
default_pipesize)
720+
finally:
721+
os.close(fp_r)
722+
os.close(fp_w)
723+
# On other platforms we cannot test the pipe size (yet). But above
724+
# code using pipesize=-1 should not crash.
725+
p.stdin.close()
726+
finally:
727+
p.kill()
728+
p.wait()
708729

709730
def test_env(self):
710731
newenv = os.environ.copy()

0 commit comments

Comments
 (0)