-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
py/objtype: Implement __del__ data model method for user classes. #18005
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
Open
AJMansfield
wants to merge
5
commits into
micropython:master
Choose a base branch
from
AJMansfield:del
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #18005 +/- ##
=======================================
Coverage 98.38% 98.39%
=======================================
Files 171 171
Lines 22296 22314 +18
=======================================
+ Hits 21937 21955 +18
Misses 359 359 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Code size report:
|
Cleans up 2423493, and fixes builds that include LFS but not MICROPY_ENABLE_FINALISER. Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
Remaining TODO:
|
Other than for freeing resources obtained through FFI, what use case would it have in MicroPython? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements the required functionality to allow the garbage collector to run user-defined
__del__
methods as finalisers.Testing
I've added full-coverage tests of this feature that I've tested for sensitivity across both the Unix port and my RP2040 and RP2350 boards. All tests pass on my RP2 boards, and with one very difficult exception noted below all tests also pass on unix.
tests/basics/class_del_postadded.py
only passes on the unix builds I've built withDEBUG=1
; but fails on myDEBUG=0
builds. I suspect that this is an underlying garbage-collector bug, rather than a bug with this implementation of__del__
specifically. So far I've failed to discover exactly why it is this occurs, though I've confirmed using the debugger on a non-debug non-stripped build that everything up to and including the call tomp_obj_malloc_var_with_finaliser
does run as expected.Trade-offs and Alternatives
This PR claims two additional bits from the
mp_obj_type_t.flags
field, forMP_TYPE_FLAG_IS_INSTANCED
andMP_TYPE_FLAG_HAS_FINALISER
. The latter bit is required in order to track whether instances should be allocated using the_with_finaliser
allocators, while the former is required to track whether or it's safe to allow the__del__
method to be inserted into a class after-the-fact. It's possible thatMP_TYPE_FLAG_IS_INSTANCED
could be eliminated by forbidding this type of insertion entirely.Alternatively, almost all of the code complexity in this PR could be eliminated by unconditionally allocating objects from user-defined classes with
mp_obj_malloc_var_with_finaliser
; however the performance penalty that this would introduce on classes that don't use it leads me to favor this more-complex approach.