Commit 58840432 authored by Victor Stinner's avatar Victor Stinner Committed by GitHub

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().
parent 066e5b1a
...@@ -1639,23 +1639,35 @@ class _PosixSpawnMixin: ...@@ -1639,23 +1639,35 @@ class _PosixSpawnMixin:
os.environ, setsigmask=[signal.NSIG, os.environ, setsigmask=[signal.NSIG,
signal.NSIG+1]) signal.NSIG+1])
@unittest.skipIf(True, def test_setsid(self):
"FIXME: bpo-35537: test fails is setsid is supported") rfd, wfd = os.pipe()
def test_start_new_session(self): self.addCleanup(os.close, rfd)
# For code coverage of calling setsid(). We don't care if we get an
# EPERM error from it depending on the test execution environment, that
# still indicates that it was called.
code = "import os; print(os.getpgid(os.getpid()))"
try: try:
self.spawn_func(sys.executable, os.set_inheritable(wfd, True)
[sys.executable, "-c", code],
os.environ, setsid=True) code = textwrap.dedent(f"""
except NotImplementedError as exc: import os
self.skipTest("setsid is not supported: %s" % exc) fd = {wfd}
else: sid = os.getsid(0)
parent_pgid = os.getpgid(os.getpid()) os.write(fd, str(sid).encode())
child_pgid = int(output) """)
self.assertNotEqual(parent_pgid, child_pgid)
try:
pid = self.spawn_func(sys.executable,
[sys.executable, "-c", code],
os.environ, setsid=True)
except NotImplementedError as exc:
self.skipTest(f"setsid is not supported: {exc!r}")
except PermissionError as exc:
self.skipTest(f"setsid failed with: {exc!r}")
finally:
os.close(wfd)
self.assertEqual(os.waitpid(pid, 0), (pid, 0))
output = os.read(rfd, 100)
child_sid = int(output)
parent_sid = os.getsid(os.getpid())
self.assertNotEqual(parent_sid, child_sid)
@unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'),
'need signal.pthread_sigmask()') 'need signal.pthread_sigmask()')
......
...@@ -1705,16 +1705,15 @@ class POSIXProcessTestCase(BaseTestCase): ...@@ -1705,16 +1705,15 @@ class POSIXProcessTestCase(BaseTestCase):
# still indicates that it was called. # still indicates that it was called.
try: try:
output = subprocess.check_output( output = subprocess.check_output(
[sys.executable, "-c", [sys.executable, "-c", "import os; print(os.getsid(0))"],
"import os; print(os.getpgid(os.getpid()))"],
start_new_session=True) start_new_session=True)
except OSError as e: except OSError as e:
if e.errno != errno.EPERM: if e.errno != errno.EPERM:
raise raise
else: else:
parent_pgid = os.getpgid(os.getpid()) parent_sid = os.getsid(0)
child_pgid = int(output) child_sid = int(output)
self.assertNotEqual(parent_pgid, child_pgid) self.assertNotEqual(parent_sid, child_sid)
def test_run_abort(self): def test_run_abort(self):
# returncode handles signal termination # returncode handles signal termination
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment