标签:peter evel crash into about gen plain slots utils
https://www.python.org/dev/peps/pep-0442/
PEP: | 442 |
---|---|
Title: | Safe object finalization |
Author: | Antoine Pitrou <solipsis at pitrou.net> |
BDFL-Delegate: | Benjamin Peterson <benjamin at python.org> |
Status: | Final |
Type: | Standards Track |
Created: | 2013-05-18 |
Python-Version: | 3.4 |
Post-History: | 2013-05-18 |
Resolution: | https://mail.python.org/pipermail/python-dev/2013-June/126746.html |
Contents
This PEP proposes to deal with the current limitations of object finalization. The goal is to be able to define and run finalizers for any object, regardless of their position in the object graph.
This PEP doesn‘t call for any change in Python code. Objects with existing finalizers will benefit automatically.
While this PEP discusses CPython-specific implementation details, the change in finalization semantics is expected to affect the Python ecosystem as a whole. In particular, this PEP obsoletes the current guideline that "objects with a __del__ method should not be part of a reference cycle".
The primary benefits of this PEP regard objects with finalizers, such as objects with a __del__ method and generators with a finally block. Those objects can now be reclaimed when they are part of a reference cycle.
The PEP also paves the way for further benefits:
The PEP doesn‘t change the semantics of:
In normal reference-counted disposal, an object‘s finalizer is called just before the object is deallocated. If the finalizer resurrects the object, deallocation is aborted.
However, if the object was already finalized, then the finalizer isn‘t called. This prevents us from finalizing zombies (see below).
Cyclic isolates are first detected by the garbage collector, and then disposed of. The detection phase doesn‘t change and won‘t be described here. Disposal of a CI traditionally works in the following order:
This PEP proposes to turn CI disposal into the following sequence (new steps are in bold):
Note
The GC doesn‘t recalculate the CI after step 2 above, hence the need for step 3 to check that the whole subgraph is still isolated.
Type objects get a new tp_finalize slot to which __del__ methods are mapped (and reciprocally). Generators are modified to use this slot, rather than tp_del. A tp_finalize function is a normal C function which will be called with a valid and alive PyObjectas its only argument. It doesn‘t need to manipulate the object‘s reference count, as this will be done by the caller. However, it must ensure that the original exception state is restored before returning to the caller.
For compatibility, tp_del is kept in the type structure. Handling of objects with a non-NULL tp_del is unchanged: when part of a CI, they are not finalized and end up in gc.garbage. However, a non-NULL tp_del is not encountered anymore in the CPython source tree (except for testing purposes).
Two new C API functions are provided to ease calling of tp_finalize, especially from custom deallocators.
On the internal side, a bit is reserved in the GC header for GC-managed objects to signal that they were finalized. This helps avoid finalizing an object twice (and, especially, finalizing a CT object after it was broken by the GC).
Note
Objects which are not GC-enabled can also have a tp_finalize slot. They don‘t need the additional bit since their tp_finalize function can only be called from the deallocator: it therefore cannot be called twice, except when resurrected.
Following this scheme, an object‘s finalizer is always called exactly once, even if it was resurrected afterwards.
For CI objects, the order in which finalizers are called (step 2 above) is undefined.
It is important to explain why the proposed change is safe. There are two aspects to be discussed:
Let‘s discuss the first issue. We will divide possible cases in two categories:
Now for the second issue. There are three potential cases:
An implementation is available in branch finalize of the repository at http://hg.python.org/features/finalize/.
Besides running the normal Python test suite, the implementation adds test cases for various finalization possibilities including reference cycles, object resurrection and legacy tp_del slots.
The implementation has also been checked to not produce any regressions on the following test suites:
Notes about reference cycle collection and weak reference callbacks:http://hg.python.org/cpython/file/4e687d53b645/Modules/gc_weakref.txt
Generator memory leak: http://bugs.python.org/issue17468
Allow objects to decide if they can be collected by GC: http://bugs.python.org/issue9141
Module shutdown procedure based on GC http://bugs.python.org/issue812369
This document has been placed in the public domain.
Source: https://github.com/python/peps/blob/master/pep-0442.txt
PEP 442 -- Safe object finalization
标签:peter evel crash into about gen plain slots utils
原文地址:https://www.cnblogs.com/yuanjiangw/p/11006281.html