Author pablogsal
Recipients BTaskaya, james, pablogsal, serhiy.storchaka, vstinner, xtreak
Date 2019-10-27.19:55:41
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1572206141.27.0.0281506609454.issue38530@roundup.psfhosted.org>
In-reply-to
Content
I think I am going to proceed to modify PR 16856 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.
History
Date User Action Args
2019-10-27 19:55:41pablogsalsetrecipients: + pablogsal, vstinner, serhiy.storchaka, james, xtreak, BTaskaya
2019-10-27 19:55:41pablogsalsetmessageid: <1572206141.27.0.0281506609454.issue38530@roundup.psfhosted.org>
2019-10-27 19:55:41pablogsallinkissue38530 messages
2019-10-27 19:55:41pablogsalcreate