cpython: 7f3695701724 (original) (raw)
Mercurial > cpython
changeset 93770:7f3695701724
Issue #22696: Add function :func:`sys.is_finalizing` to know about interpreter shutdown. [#22696]
Antoine Pitrou solipsis@pitrou.net | |
---|---|
date | Sun, 07 Dec 2014 01:28:27 +0100 |
parents | 628acf7dbc8d |
children | ee095a2e2b35 |
files | Doc/glossary.rst Doc/library/gc.rst Doc/library/sys.rst Doc/library/weakref.rst Lib/test/test_sys.py Misc/NEWS Python/sysmodule.c |
diffstat | 7 files changed, 62 insertions(+), 6 deletions(-)[+] [-] Doc/glossary.rst 13 Doc/library/gc.rst 6 Doc/library/sys.rst 8 Doc/library/weakref.rst 6 Lib/test/test_sys.py 21 Misc/NEWS 3 Python/sysmodule.c 11 |
line wrap: on
line diff
--- a/Doc/glossary.rst
+++ b/Doc/glossary.rst
@@ -402,6 +402,19 @@ Glossary
than compiled ones, though their programs generally also run more
slowly. See also :term:interactive
.
- interpreter shutdown
When asked to shut down, the Python interpreter enters a special phase[](#l1.8)
where it gradually releases all allocated resources, such as modules[](#l1.9)
and various critical internal structures. It also makes several calls[](#l1.10)
to the :term:`garbage collector <garbage collection>`. This can trigger[](#l1.11)
the execution of code in user-defined destructors or weakref callbacks.[](#l1.12)
Code executed during the shutdown phase can encounter various[](#l1.13)
exceptions as the resources it relies on may not function anymore[](#l1.14)
(common examples are library modules or the warnings machinery).[](#l1.15)
The main reason for interpreter shutdown is that the ``__main__`` module[](#l1.17)
or the script being run has finished executing.[](#l1.18)
+
iterable
An object capable of returning its members one at a time. Examples of
iterables include all sequence types (such as :class:list
, :class:str
,
--- a/Doc/library/gc.rst +++ b/Doc/library/gc.rst @@ -186,7 +186,7 @@ values but should not rebind them): added to this list rather than freed. .. versionchanged:: 3.2
If this list is non-empty at interpreter shutdown, a[](#l2.7)
If this list is non-empty at :term:`interpreter shutdown`, a[](#l2.8) :exc:`ResourceWarning` is emitted, which is silent by default. If[](#l2.9) :const:`DEBUG_UNCOLLECTABLE` is set, in addition all uncollectable objects[](#l2.10) are printed.[](#l2.11)
@@ -252,8 +252,8 @@ The following constants are provided for
to the garbage
list.
.. versionchanged:: 3.2
Also print the contents of the :data:`garbage` list at interpreter[](#l2.16)
shutdown, if it isn't empty.[](#l2.17)
Also print the contents of the :data:`garbage` list at[](#l2.18)
:term:`interpreter shutdown`, if it isn't empty.[](#l2.19)
--- a/Doc/library/sys.rst
+++ b/Doc/library/sys.rst
@@ -718,6 +718,14 @@ always available.
value of :func:intern
around to benefit from it.
+.. function:: is_finalizing()
+
- Return :const:
True
if the Python interpreter is - :term:
shutting down <interpreter shutdown>
, :const:False
otherwise. + - .. versionadded:: 3.5 +
+ .. data:: last_type last_value last_traceback
--- a/Doc/library/weakref.rst +++ b/Doc/library/weakref.rst @@ -258,7 +258,7 @@ These method have the same issues as the are called in reverse order of creation. A finalizer will never invoke its callback during the later part of
- the :term:
interpreter shutdown
when module globals are liable to have been replaced by :const:None
. .. method:: call() @@ -527,8 +527,8 @@ follows::
Starting with Python 3.4, :meth:__del__
methods no longer prevent
reference cycles from being garbage collected, and module globals are
-no longer forced to :const:None
during interpreter shutdown. So this
-code should work without any issues on CPython.
+no longer forced to :const:None
during :term:interpreter shutdown
.
+So this code should work without any issues on CPython.
However, handling of :meth:__del__
methods is notoriously implementation
specific, since it depends on internal details of the interpreter's garbage
--- 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)[](#l5.8)
# Don't use the atexit module because _Py_Finalizing is only set[](#l5.9)
# after calling atexit callbacks[](#l5.10)
code = """if 1:[](#l5.11)
import sys[](#l5.12)
class AtExit:[](#l5.14)
is_finalizing = sys.is_finalizing[](#l5.15)
print = print[](#l5.16)
def __del__(self):[](#l5.18)
self.print(self.is_finalizing(), flush=True)[](#l5.19)
# Keep a reference in the __main__ module namespace, so the[](#l5.21)
# AtExit destructor will be called at Python exit[](#l5.22)
ref = AtExit()[](#l5.23)
"""[](#l5.24)
rc, stdout, stderr = assert_python_ok('-c', code)[](#l5.25)
self.assertEqual(stdout.rstrip(), b'True')[](#l5.26)
+ @test.support.cpython_only class SizeofTest(unittest.TestCase):
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -194,6 +194,9 @@ Core and Builtins
Library
-------
+- Issue #22696: Add function :func:sys.is_finalizing
to know about
- Issue #16043: Add a default limit for the amount of data xmlrpclib.gzip_decode will return. This resolves CVE-2013-1753.
--- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1121,6 +1121,16 @@ PyDoc_STRVAR(sys_clear_type_cache__doc__ "_clear_type_cache() -> None\n[](#l7.4) Clear the internal type lookup cache."); +static PyObject +sys_is_finalizing(PyObject self, PyObject* args) +{
+} + +PyDoc_STRVAR(is_finalizing_doc, +"is_finalizing()\n[](#l7.14) +Return True if Python is exiting."); + static PyMethodDef sys_methods[] = { /* Might as well keep this in alphabetic order / @@ -1167,6 +1177,7 @@ static PyMethodDef sys_methods[] = { getwindowsversion_doc}, #endif / MS_WINDOWS */ {"intern", sys_intern, METH_VARARGS, intern_doc},
#ifdef USE_MALLOPT {"mdebug", sys_mdebug, METH_VARARGS}, #endif