[Python-Dev] Revert #12085 fix for __del__ attribute error message
tjreedy at udel.edu
Sat Sep 21 23:16:41 CEST 2013
When an AttributeError is raised in a __del__ method, it is caught and
ignored, except that it is not completely ignored but is replaced by a
warning message sent to stderr. Example:
>>> class C():
def __del__(self): raise AttributeError
>>> del c
Exception AttributeError: AttributeError() in <bound method C.__del__ of
<__main__.C object at 0x000000000351A198>> ignored
The AttributeError is ignored, I presume, because one may be expected if
__new__ runs but __init__ does not, because of an exception.
AttributeErrors can also be expected during exit cleanup.
The message seems not to be a Warning. So it seems that is cannot be
suppressed. The OP of http://bugs.python.org/issue12085 encountered this
message with an unnecessary use of subprocesses.Popen. (There is better
code that avoids the problem.) He considered the message a problem as
it interfered with his output. He suggested the fix of changing
'self.attr' to "getattr(self. 'attr', False)".
Viktor Stinner applied this fix. I believe it should be reverted for
1. It results in TypeError on exit when gettattr is set to None before
__delete__ is called. http://bugs.python.org/issue19021. Even is that
is fixed with an alternate patch (creating a 'backup' class attribute),
I think it would still be wrong.
2. If we do not agree with the OP that putting output on stderr is bad,
then no fix is needed and there should be none.
3. If we do agree that the unconditional output is a problem, then there
should be a general fix. It is, in a sense, just happenstance that the
OP encountered this with Popen. Some possibilities:
a. Decide the the message is not needed; just silently ignore the
__del__ AttributeError. I am not sure what its intended use is.
b. Make the message a Warning that can be blocked, perhaps a
RuntimeWarning. Does this warning message predate the warnings module,
new in 2.1?
c. If the __del__ AttributeError occurs while processing the __init__
error, just chain it normally. But this would, I presume interfere with
catching the __init__ exception.
d. Add a missing attribute in cleanup message to end of the __init__
Terry Jan Reedy
More information about the Python-Dev