(self)
| 195 | # _PyRefchain_Trace() on memory allocation error. |
| 196 | @unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build') |
| 197 | def test_atexit_with_low_memory(self): |
| 198 | # gh-140080: Test that setting low memory after registering an atexit |
| 199 | # callback doesn't cause an infinite loop during finalization. |
| 200 | code = textwrap.dedent(""" |
| 201 | import atexit |
| 202 | import _testcapi |
| 203 | |
| 204 | def callback(): |
| 205 | print("hello") |
| 206 | |
| 207 | atexit.register(callback) |
| 208 | # Simulate low memory condition |
| 209 | _testcapi.set_nomemory(0) |
| 210 | """) |
| 211 | |
| 212 | with os_helper.temp_dir() as temp_dir: |
| 213 | script = script_helper.make_script(temp_dir, 'test_atexit_script', code) |
| 214 | with SuppressCrashReport(): |
| 215 | with script_helper.spawn_python(script, |
| 216 | stderr=subprocess.PIPE) as proc: |
| 217 | proc.wait() |
| 218 | stdout = proc.stdout.read() |
| 219 | stderr = proc.stderr.read() |
| 220 | |
| 221 | self.assertIn(proc.returncode, (0, 1)) |
| 222 | self.assertNotIn(b"hello", stdout) |
| 223 | self.assertIn(b"MemoryError", stderr) |
| 224 | |
| 225 | |
| 226 | if __name__ == "__main__": |
nothing calls this directly
no test coverage detected