From a8d66b4ff6f2d4115ccd355ab1b3bc8b263bde90 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sun, 7 Dec 2014 01:28:27 +0100 Subject: Issue #22696: Add function :func:`sys.is_finalizing` to know about interpreter shutdown. --- Lib/test/test_sys.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'Lib/test/test_sys.py') diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 9ac105fe38..ec2eaf3bab 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -744,6 +744,27 @@ class SysModuleTest(unittest.TestCase): c = sys.getallocatedblocks() self.assertIn(c, range(b - 50, b + 50)) + def test_is_finalizing(self): + self.assertIs(sys.is_finalizing(), False) + # Don't use the atexit module because _Py_Finalizing is only set + # after calling atexit callbacks + code = """if 1: + import sys + + class AtExit: + is_finalizing = sys.is_finalizing + print = print + + def __del__(self): + self.print(self.is_finalizing(), flush=True) + + # Keep a reference in the __main__ module namespace, so the + # AtExit destructor will be called at Python exit + ref = AtExit() + """ + rc, stdout, stderr = assert_python_ok('-c', code) + self.assertEqual(stdout.rstrip(), b'True') + @test.support.cpython_only class SizeofTest(unittest.TestCase): -- cgit v1.2.1