-
-
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
base: master
Are you sure you want to change the base?
Conversation
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? |
What use cases do finalisers have in MicroPython at all? Anything you could want to do with them in C, you could have a perfectly valid reason to want to do with python. The original impetus for this feature was related to @agatti's current work implementing I've got another library I've been developing that would benefit from this, that's for managing I2C devices --- with efficient transaction queueing, caching, and automatic fusion of adjacent transactions. When a sqlite returns a handle to a stored procedure it's memoized, if you're using a python-level ORM object to manage that procedure, it ought to be able to set a More broadly, this applies to any sorts of code that uses ECS/SoA data layout, i.e. with user-defined type with a non-pointer value indexing some kind of table; or when doing any kind of database that needs to track reach-ability across non-trivial foreign-key relationships. In many cases, context managers are the "clean code approved" way to manage external lifetimes --- but if every problem was one with lifetime boundaries that were that simple, we'd already all just be writing Rust instead of Python. Plus, to implement a context manager that's robust against bugs like e.g. #2621, that too also needs to make use of I'm currently working on an implementation of the |
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.