| 1 |
"""This test checks for correct wait3() behavior. |
| 2 |
""" |
| 3 |
|
| 4 |
import os |
| 5 |
import subprocess |
| 6 |
import sys |
| 7 |
import unittest |
| 8 |
from test.fork_wait import ForkWait |
| 9 |
from test import support |
| 10 |
|
| 11 |
if not support.has_fork_support: |
| 12 |
raise unittest.SkipTest("requires working os.fork()") |
| 13 |
|
| 14 |
if not hasattr(os, 'wait3'): |
| 15 |
raise unittest.SkipTest("os.wait3 not defined") |
| 16 |
|
| 17 |
class Wait3Test(ForkWait): |
| 18 |
def wait_impl(self, cpid, *, exitcode): |
| 19 |
# This many iterations can be required, since some previously run |
| 20 |
# tests (e.g. test_ctypes) could have spawned a lot of children |
| 21 |
# very quickly. |
| 22 |
for _ in support.sleeping_retry(support.SHORT_TIMEOUT): |
| 23 |
# wait3() shouldn't hang, but some of the buildbots seem to hang |
| 24 |
# in the forking tests. This is an attempt to fix the problem. |
| 25 |
spid, status, rusage = os.wait3(os.WNOHANG) |
| 26 |
if spid == cpid: |
| 27 |
break |
| 28 |
|
| 29 |
self.assertEqual(spid, cpid) |
| 30 |
self.assertEqual(os.waitstatus_to_exitcode(status), exitcode) |
| 31 |
self.assertTrue(rusage) |
| 32 |
|
| 33 |
def test_wait3_rusage_initialized(self): |
| 34 |
# Ensure a successful wait3() call where no child was ready to report |
| 35 |
# its exit status does not return uninitialized memory in the rusage |
| 36 |
# structure. See bpo-36279. |
| 37 |
args = [sys.executable, '-c', 'import sys; sys.stdin.read()'] |
| 38 |
proc = subprocess.Popen(args, stdin=subprocess.PIPE) |
| 39 |
try: |
| 40 |
pid, status, rusage = os.wait3(os.WNOHANG) |
| 41 |
self.assertEqual(0, pid) |
| 42 |
self.assertEqual(0, status) |
| 43 |
self.assertEqual(0, sum(rusage)) |
| 44 |
finally: |
| 45 |
proc.stdin.close() |
| 46 |
proc.wait() |
| 47 |
|
| 48 |
|
| 49 |
def tearDownModule(): |
| 50 |
support.reap_children() |
| 51 |
|
| 52 |
if __name__ == "__main__": |
| 53 |
unittest.main() |