Interface FinalizablePyObject
-
- All Known Implementing Classes:
PyBZ2File
,PyBZ2FileDerived
,PyInstance
public interface FinalizablePyObject
This interface allows
PyObject
s to have finalizers. Alternatively one can useFinalizableBuiltin
.The difference is that
__del__()
can be overridden by a new-style subclass's__del__
-method on Python-side, whileFinalizableBuiltin.__del_builtin__()
is always called. If a Python-side finalizer exists,FinalizableBuiltin.__del_builtin__()
will be called after the Python-side finalizer has been processed.One can even implement both interfaces. If both interfaces are implemented, the
FinalizeTrigger
will call__del__()
first and thenFinalizableBuiltin.__del_builtin__()
. If a new-style subclass has an own, Python-side__del__
-method, this overrides the Java-implemented__del__()
, but notFinalizableBuiltin.__del_builtin__()
, which will be called after the Python-side finalizer.If you are writing a custom built-in that shall directly extend
PyObject
or some other not-yet-finalizable builtin and have a finalizer, follow the instructions below.-
Let your subclass implement
FinalizablePyObject
(orFinalizableBuiltin
). -
In every constructor call
FinalizeTrigger.ensureFinalizer(this);
-
Write your
__del__()
-method however you intend it. (orFinalizableBuiltin.__del_builtin__()
ifFinalizableBuiltin
was used) -
(optional)
If your finalizer resurrects the object (Python allows this) and you wish the finalizer to run again on next collection of the object:
In the block where the resurrection occurs, let your__del__()
- orFinalizableBuiltin.__del_builtin__()
-method call
FinalizeTrigger.ensureFinalizer(this);
. If you implement__del__
in Python and need this functionality, you can simply callsomeObject.__ensure_finalizer__()
Note that this is Jython-specific and should be surrounded by atry/except
-block to ensure compatibility with other Python implementations.
Note: Regarding to object resurrection, Jython currently behaves like CPython >= 3.4. That means the finalizer
__del__()
orFinalizableBuiltin.__del_builtin__()
is called only the first time an object gets gc'ed. If pre-3.4.-behavior is required for some reason (i.e. have the finalizer called repeatedly on every collection after a resurrection), one can achieve this manually via step 5).The built-in function
__ensure_finalizer__
is also useful if a class acquires a finalizer after instances have already been created. Usually only those instances that were created after their class acquired the finalizer will actually be finalized (in contrast to CPython). However, one can manually tell earlier created instances to become finalizable by calling__ensure_finalizer__()
on them. As mentioned above, it is recommended to surround this with atry/except
-block to ensure compatibility with other Python implementations.Note that it is not possible to override
__ensure_finalizer__
on Python side. If one overrides__ensure_finalizer__
on Python side, Jython will ignore the override-implementation and still call the original one.It is possible to switch finalization on and off at any desired time for a certain object. This can be helpful if it is only necessary to have
__del__()
orFinalizableBuiltin.__del_builtin__()
called for certain configurations of an object.To turn off the finalizer, call
((FinalizeTrigger) JyAttribute.getAttr(this, JyAttribute.FINALIZE_TRIGGER_ATTR)).clear();
To turn it on again, call((FinalizeTrigger) JyAttribute.getAttr(this, JyAttribute.FINALIZE_TRIGGER_ATTR)).trigger(this);
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
__del__()
-