Skip to content

Commit 5c78acc

Browse files
committed
Check if can really use can_use_pidfd() in unit tests
1 parent e1da996 commit 5c78acc

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

Lib/test/test_subprocess.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4096,11 +4096,28 @@ def test_broken_pipe_cleanup(self):
40964096
self.assertTrue(proc.stdin.closed)
40974097

40984098

4099+
# ---
4100+
4101+
def can_use_pidfd():
4102+
# Availability: Linux >= 5.3
4103+
if not hasattr(os, "pidfd_open"):
4104+
return False
4105+
try:
4106+
pidfd = os.pidfd_open(os.getpid(), 0)
4107+
except OSError as err:
4108+
# blocked by security policy like SECCOMP
4109+
return False
4110+
else:
4111+
os.close(pidfd)
4112+
return True
4113+
4114+
4115+
40994116
class FastWaitTestCase(BaseTestCase):
41004117
"""Tests for efficient (pidfd_open() + poll() / kqueue()) process
41014118
waiting in subprocess.Popen.wait().
41024119
"""
4103-
CAN_USE_PIDFD_OPEN = hasattr(os, "pidfd_open")
4120+
CAN_USE_PIDFD_OPEN = can_use_pidfd()
41044121
CAN_USE_KQUEUE = subprocess._CAN_USE_KQUEUE
41054122

41064123
def assert_fast_waitpid_error(self, patch_point):
@@ -4115,7 +4132,7 @@ def assert_fast_waitpid_error(self, patch_point):
41154132
self.assertEqual(p.wait(timeout=support.SHORT_TIMEOUT), 0)
41164133
assert m.called
41174134

4118-
@unittest.skipIf(not CAN_USE_PIDFD_OPEN, reason="LINUX only")
4135+
@unittest.skipIf(not CAN_USE_PIDFD_OPEN, reason="pidfd_open not supported")
41194136
def test_wait_pidfd_open_error(self):
41204137
self.assert_fast_waitpid_error("os.pidfd_open")
41214138

@@ -4162,7 +4179,7 @@ def wrapper(*args, **kwargs):
41624179
assert m.called
41634180
self.assertEqual(status, 0)
41644181

4165-
@unittest.skipIf(not CAN_USE_PIDFD_OPEN, reason="LINUX only")
4182+
@unittest.skipIf(not CAN_USE_PIDFD_OPEN, reason="pidfd_open not supported")
41664183
def test_pidfd_open_race(self):
41674184
self.assert_wait_race_condition("os.pidfd_open", os.pidfd_open)
41684185

@@ -4192,8 +4209,8 @@ def waitpid_wrapper(pid, flags):
41924209
assert m1.called
41934210
assert m2.called
41944211

4195-
@unittest.skipIf(not CAN_USE_PIDFD_OPEN, reason="LINUX only")
4196-
def test_poll_notification_without_immediate_reap(self):
4212+
@unittest.skipIf(not CAN_USE_PIDFD_OPEN, reason="pidfd_open not supported")
4213+
def test_pidfd_open_notification_without_immediate_reap(self):
41974214
self.assert_notification_without_immediate_reap("_wait_pidfd")
41984215

41994216
@unittest.skipIf(not CAN_USE_KQUEUE, reason="macOS / BSD only")

0 commit comments

Comments
 (0)