|
2 | 2 | # Copyright (C) 1996-2017 Python Software Foundation |
3 | 3 | # |
4 | 4 | # Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2 |
| 5 | +import os |
| 6 | +import shlex |
| 7 | +import tempfile |
| 8 | +import textwrap |
5 | 9 | import unittest |
6 | 10 | import sys |
| 11 | +from subprocess import CalledProcessError |
| 12 | +from tempfile import mkdtemp |
7 | 13 |
|
| 14 | +POSIX_BACKEND_IS_JAVA = sys.implementation.name == "graalpy" and __graalpython__.posix_module_backend != 'java' |
8 | 15 |
|
9 | 16 | def test_os_pipe(): |
10 | 17 | import os |
@@ -100,48 +107,77 @@ def test_waitpid(self): |
100 | 107 | p.kill() |
101 | 108 | p.wait() |
102 | 109 |
|
103 | | - # @unittest.skipIf(sys.platform == 'win32', "Posix-specific") |
104 | | - # Skipped because of transient: GR-66709 |
105 | | - @unittest.skip |
106 | | - def test_waitpid_group_child(self): |
107 | | - import os |
108 | | - p = subprocess.Popen([sys.executable, "-c", "import time; print('before'); time.sleep(0.1); print('after'); 42"], |
109 | | - stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
110 | | - stderr=subprocess.PIPE) |
111 | | - ps_list = 'not available' |
112 | | - if sys.implementation.name == "graalpy" and \ |
113 | | - __graalpython__.posix_module_backend != 'java' \ |
114 | | - and sys.platform.startswith("linux"): |
115 | | - ps_list = subprocess.check_output("ps", shell=True, text=True) |
116 | | - res = os.waitpid(0, 0) |
117 | | - msg = f"Spawned {p.pid=}, os.waitpid result={res}, output of ps:\n{ps_list}" |
| 110 | + def _run_in_new_group(self, code): |
| 111 | + def safe_decode(x): |
| 112 | + return '' if not x else x.decode().strip() |
| 113 | + filename = None |
118 | 114 | try: |
119 | | - stdout, stderr = p.communicate(timeout=5) |
120 | | - except subprocess.TimeoutExpired: |
121 | | - p.kill() |
122 | | - stdout, stderr = p.communicate() |
123 | | - msg += f"\n{stdout.decode().strip()=}, {stderr.decode().strip()=}" |
124 | | - assert res[1] == 0, msg |
| 115 | + with tempfile.NamedTemporaryFile(delete=False) as f: |
| 116 | + f.write(textwrap.dedent(code.strip(' ').strip('\n')).encode('ascii')) |
| 117 | + filename = f.name |
| 118 | + subprocess.check_output([sys.executable, filename], start_new_session=True) |
| 119 | + except CalledProcessError as e: |
| 120 | + print(f"ERROR running {code}. Stdout:") |
| 121 | + print(safe_decode(e.stdout)) |
| 122 | + print("===== stderr:") |
| 123 | + print(safe_decode(e.stderr)) |
| 124 | + print("=============") |
| 125 | + finally: |
| 126 | + if filename: |
| 127 | + os.remove(filename) |
| 128 | + pass |
125 | 129 |
|
126 | | - # @unittest.skipIf(sys.platform == 'win32', "Posix-specific") |
127 | | - # Skipped because of transient: https://jira.oci.oraclecorp.com/browse/GR-65714 |
128 | | - @unittest.skip |
| 130 | + @unittest.skipIf(sys.platform == 'win32' or POSIX_BACKEND_IS_JAVA, "Posix-specific") |
| 131 | + def test_waitpid_group_child(self): |
| 132 | + code = ''' |
| 133 | + import os |
| 134 | + import sys |
| 135 | + import subprocess |
| 136 | + p = subprocess.Popen([sys.executable, "-c", "import time; print('before'); time.sleep(0.1); print('after'); 42"], |
| 137 | + stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 138 | + stderr=subprocess.PIPE) |
| 139 | + ps_list = 'not available' |
| 140 | + if sys.implementation.name == "graalpy" and \\ |
| 141 | + and sys.platform.startswith("linux"): |
| 142 | + ps_list = subprocess.check_output("ps", shell=True, text=True) |
| 143 | + res = os.waitpid(0, 0) |
| 144 | + msg = f"Spawned {p.pid=}, os.waitpid result={res}, output of ps:\\n{ps_list}" |
| 145 | + try: |
| 146 | + stdout, stderr = p.communicate(timeout=5) |
| 147 | + except subprocess.TimeoutExpired: |
| 148 | + p.kill() |
| 149 | + stdout, stderr = p.communicate() |
| 150 | + msg += f"\\n{stdout.decode().strip()=}, {stderr.decode().strip()=}" |
| 151 | + assert res[1] == 0, msg |
| 152 | + ''' |
| 153 | + self._run_in_new_group(code) |
| 154 | + |
| 155 | + @unittest.skipIf(sys.platform == 'win32' or POSIX_BACKEND_IS_JAVA, "Posix-specific") |
129 | 156 | def test_waitpid_any_child(self): |
130 | | - import os |
131 | | - p = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(0.1); 42"]) |
132 | | - res = os.waitpid(-1, 0) |
133 | | - assert res[1] == 0, res |
| 157 | + code = ''' |
| 158 | + import os |
| 159 | + import sys |
| 160 | + import subprocess |
| 161 | + p = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(0.1); 42"]) |
| 162 | + res = os.waitpid(-1, 0) |
| 163 | + assert res[1] == 0, res |
| 164 | + ''' |
| 165 | + self._run_in_new_group(code) |
134 | 166 |
|
135 | 167 | # Skipped because of transient: GR-66709 |
136 | | - @unittest.skip |
| 168 | + @unittest.skipIf(sys.platform == 'win32', "Posix-specific") |
137 | 169 | def test_waitpid_no_child(self): |
138 | | - import os |
139 | | - try: |
140 | | - os.waitpid(-1, 0) |
141 | | - except ChildProcessError: |
142 | | - assert True |
143 | | - else: |
144 | | - assert False |
| 170 | + code = ''' |
| 171 | + import os |
| 172 | + try: |
| 173 | + os.waitpid(-1, 0) |
| 174 | + except ChildProcessError: |
| 175 | + assert True |
| 176 | + else: |
| 177 | + assert False |
| 178 | + ''' |
| 179 | + self._run_in_new_group(code) |
| 180 | + |
145 | 181 |
|
146 | 182 | def test_kill(self): |
147 | 183 | import os |
|
0 commit comments