Skip to content

Commit e696b15

Browse files
bpo-35537: Rewrite setsid test for os.posix_spawn (GH-11721)
bpo-35537, bpo-35876: Fix also test_start_new_session() of test_subprocess: use os.getsid() rather than os.getpgid(). (cherry picked from commit 5884043) Co-authored-by: Victor Stinner <vstinner@redhat.com>
1 parent 3b976d1 commit e696b15

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

Lib/test/test_posix.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,23 +1639,35 @@ def test_setsigmask_wrong_type(self):
16391639
os.environ, setsigmask=[signal.NSIG,
16401640
signal.NSIG+1])
16411641

1642-
@unittest.skipIf(True,
1643-
"FIXME: bpo-35537: test fails is setsid is supported")
1644-
def test_start_new_session(self):
1645-
# For code coverage of calling setsid(). We don't care if we get an
1646-
# EPERM error from it depending on the test execution environment, that
1647-
# still indicates that it was called.
1648-
code = "import os; print(os.getpgid(os.getpid()))"
1642+
def test_setsid(self):
1643+
rfd, wfd = os.pipe()
1644+
self.addCleanup(os.close, rfd)
16491645
try:
1650-
self.spawn_func(sys.executable,
1651-
[sys.executable, "-c", code],
1652-
os.environ, setsid=True)
1653-
except NotImplementedError as exc:
1654-
self.skipTest("setsid is not supported: %s" % exc)
1655-
else:
1656-
parent_pgid = os.getpgid(os.getpid())
1657-
child_pgid = int(output)
1658-
self.assertNotEqual(parent_pgid, child_pgid)
1646+
os.set_inheritable(wfd, True)
1647+
1648+
code = textwrap.dedent(f"""
1649+
import os
1650+
fd = {wfd}
1651+
sid = os.getsid(0)
1652+
os.write(fd, str(sid).encode())
1653+
""")
1654+
1655+
try:
1656+
pid = self.spawn_func(sys.executable,
1657+
[sys.executable, "-c", code],
1658+
os.environ, setsid=True)
1659+
except NotImplementedError as exc:
1660+
self.skipTest(f"setsid is not supported: {exc!r}")
1661+
except PermissionError as exc:
1662+
self.skipTest(f"setsid failed with: {exc!r}")
1663+
finally:
1664+
os.close(wfd)
1665+
1666+
self.assertEqual(os.waitpid(pid, 0), (pid, 0))
1667+
output = os.read(rfd, 100)
1668+
child_sid = int(output)
1669+
parent_sid = os.getsid(os.getpid())
1670+
self.assertNotEqual(parent_sid, child_sid)
16591671

16601672
@unittest.skipUnless(hasattr(signal, 'pthread_sigmask'),
16611673
'need signal.pthread_sigmask()')

Lib/test/test_subprocess.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,16 +1705,15 @@ def test_start_new_session(self):
17051705
# still indicates that it was called.
17061706
try:
17071707
output = subprocess.check_output(
1708-
[sys.executable, "-c",
1709-
"import os; print(os.getpgid(os.getpid()))"],
1708+
[sys.executable, "-c", "import os; print(os.getsid(0))"],
17101709
start_new_session=True)
17111710
except OSError as e:
17121711
if e.errno != errno.EPERM:
17131712
raise
17141713
else:
1715-
parent_pgid = os.getpgid(os.getpid())
1716-
child_pgid = int(output)
1717-
self.assertNotEqual(parent_pgid, child_pgid)
1714+
parent_sid = os.getsid(0)
1715+
child_sid = int(output)
1716+
self.assertNotEqual(parent_sid, child_sid)
17181717

17191718
def test_run_abort(self):
17201719
# returncode handles signal termination

0 commit comments

Comments
 (0)