-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Weakref callback #488
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
Weakref callback #488
Conversation
I faced the exact same issue when implementing dictionaries. There was no way to refer to the virtual machine from within the |
The documentation for weakref states: |
references alive. Also run rustfmt.
I have gone with approach 3 of, putting objects that are about to be deleted into a queue and processing them after every frame and del statement. This is the simplest approach to get working. |
Codecov Report
@@ Coverage Diff @@
## master #488 +/- ##
==========================================
- Coverage 57.38% 53.21% -4.17%
==========================================
Files 67 68 +1
Lines 12300 13167 +867
Branches 2906 3198 +292
==========================================
- Hits 7058 7007 -51
- Misses 3457 4349 +892
- Partials 1785 1811 +26
Continue to review full report at Codecov.
|
vm/src/pyobject.rs
Outdated
@@ -697,6 +801,7 @@ impl PyContext { | |||
pub struct PyObject { | |||
pub payload: PyObjectPayload, | |||
pub typ: Option<PyObjectRef>, | |||
weak_refs: Vec<PyObjectWeakRef>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's quite a heavy approach. AFAIK In CPython, weakref support is optional (stored in payload, signaled at type object level and explicitly checked only in destructors of classes that opted into it), and disabled for most builtin types to save on memory and time.
Guessing that's related to #371 too though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have moved the attribute into the 'Instance' payload as, as you said not every object needs to be weak referenceable. We need to keep in mind for #371 as to how to keep track of these weak references.
It's only costly in terms of space as checking for the presence of weak references would just be checking the length of the vector, which is directly in the PyObject.
I think it is fair to deviate here from cpython. This is pretty unacceptable to silently take up an error. At least the interpreter should crash in some clean way. Maybe by propagating the exception to top loop? |
For exceptions like SystemExit and KeyboardInterrupt they need to be propagated, so that they can tell the vm to shutdown. A consequence of propagating an exception from Shutting the whole vm down due to an exception in A correction is that sys.excepthook is actually what is called to handle the uncaught exception from |
This is now a bit outdated with the recent changes to the payload, so I will close this, and reimplement it in a series of a couple of PRs, the first of which is #636. |
I have made changes to allow weak references to have callbacks, but I have ran into a problem about how to run these callbacks during drop as we have no reference in the virual machine within the drop method.
I see three possible solutions to this: