bpo-31901: atexit callbacks should be run at subinterpreter shutdown (#4611)

Change atexit behavior and PEP-489 multiphase init support.
This commit is contained in:
Marcel Plch 2017-12-20 11:17:58 +01:00 committed by Antoine Pitrou
parent 1976086362
commit 776407fe89
10 changed files with 91 additions and 46 deletions

View file

@ -2,6 +2,7 @@ import sys
import unittest
import io
import atexit
import os
from test import support
from test.support import script_helper
@ -203,6 +204,24 @@ class SubinterpreterTest(unittest.TestCase):
self.assertEqual(ret, 0)
self.assertEqual(atexit._ncallbacks(), n)
def test_callback_on_subinterpreter_teardown(self):
# This tests if a callback is called on
# subinterpreter teardown.
expected = b"The test has passed!"
r, w = os.pipe()
code = r"""if 1:
import os
import atexit
def callback():
os.write({:d}, b"The test has passed!")
atexit.register(callback)
""".format(w)
ret = support.run_in_subinterp(code)
os.close(w)
self.assertEqual(os.read(r, len(expected)), expected)
os.close(r)
if __name__ == "__main__":
unittest.main()