Pablo Galindo Salgado <pablog...@gmail.com> added the comment:

I think I am going to proceed modifying PR 16858 by adding the name and the 
object to the AttributeError exceptions.

This should not extend the lifetime of the object more than the current 
exception is doing as the exception keeps alive the whole frame stack in the 
__traceback__ attribute. Consider this code for example:

class Target:
    def __del__(self):
        print("The object is dead!")

def f():
    g()

def g():
    h()

def h():
    theobj = Target()
    theobj.thevalue

try:
    f()
except Exception as e:
    print(e.__traceback__.tb_next.tb_next.tb_next.tb_frame.f_locals["theobj"])
    print("End of except")
print("Begining of the next line")


This code prints:

<__main__.Target object at 0x7f064adbfe10>
End of except
The object is dead!
Beginning of the next line

We can notice two things:
   * The target objects are reachable from the exception.
   * When the exception dies, the target object dies.

Adding another reference to the target object to the exception won't change the 
current lifetime, neither will create reference cycles as the target object 
will not have (unless explicitly created) a reference to the exception. We can 
conclude that this change should be safe.

In the resolution email for PEP473 the council stated that "Discussions about 
adding more attributes to built-in exceptions can continue on the issue tracker 
on a per-exception basis". As I think this will be very beneficial for the 
feature discussed in the issue, I will proceed as indicated.

Notice that if we want to change all internal CPython code that raises 
AttributeError to use the new fields, this should be done in a different PR to 
keep this minimal. For this feature, we still need to intercept AttributeError 
raised by the user without these fields added.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue38530>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to