Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG with autoreload: modify readonly __closure__ attribute #14525

Open
phihung opened this issue Sep 28, 2024 · 3 comments
Open

BUG with autoreload: modify readonly __closure__ attribute #14525

phihung opened this issue Sep 28, 2024 · 3 comments

Comments

@phihung
Copy link

phihung commented Sep 28, 2024

The update_function in the autoreload module tries to copy the __closure__ attribute from the new version of the function to the old one (presumably for reloading decorated functions).

However, __closure__ is a read-only attribute, so the copy never actually takes place. The error is caught and ignored.

This may be the cause of the long-standing issue "autoreload does not work with decorators": link and link.

The fix should be quite simple.

old_closure, new_closure = old.__closure__, new.__closure__
if len(old_closure) != len(new_closure):
    # may need more check for variable names
    return
for o, n in zip(old_closure, new_closure):
    o.cell_contents = n.cell_contents

Have I just found a 16-year-old bug? : )

@krassowski
Copy link
Member

Would it be solved by #14500?

@phihung
Copy link
Author

phihung commented Sep 29, 2024

Yes.
I did the tests. The DeduperReloader implementation correctly updates closure attribute.
But as I understand, there will be two modes of reloading: the new DeduperReloader and the old one ?
If that is the case, the old one still needs to be fixed

@smacke
Copy link
Contributor

smacke commented Oct 16, 2024

That's correct that deduperreload could fall back to the original algorithm if it can't isolate module changes to individual functions.

The fix should be quite simple.

Unfortunately the proposed fix only works if the old and new closures have the same length. Deduperreload works around this by doing some magic to patch the readonly __closure__ attribute at the level of the C struct; we could potentially do the same thing in the original autoreload algorithm as well.

Have I just found a 16-year-old bug? : )

It's hard to say whether it's a bug or a limitation -- I think autoreload is one of those "best-effort" things and it's pretty easy to hit a case with undefined behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants