request: C finalizers
First thank for the huge work already done !
regarding changelog :
- misc.h: add m_new_obj_var_with_finaliser()
and :
https://github.com/micropython/micropython/issues/245
in help to fix : https://github.com/micropython/micropython/issues/1878
i think an addition on how to use __del__ C finalizer with m_new_obj_var_with_finaliser would be great.
Sorry, somehow I have overlooked the issue. But it is not quite clear to me, what exactly is requested here. Could you, please, elaborate a bit?
the C __del__ method is actually undocumented, the code demonstrated by @stinos in https://github.com/micropython/micropython/issues/1878#issuecomment-534911108 is enough to cover its use and implement finalizers for a usermod ( i've tested the snippet on unix port, it works very well).
it allows garbage collections for user created objects without hooking into root pointers ( internals, a bit messy and subject to changes ). On the other hand m_new_obj_var_with_finaliser() is likely to stay in the future.
Thanks for the clarification! I can include that in the documentation, no problem. Would the discussion on classes be a good place for that?
Actually, if you feel like it, you can also send a pull request.
the C
__del__method is actually undocumented, the code demonstrated by @stinos in micropython/micropython#1878 (comment) is enough to cover its use and implement finalizers for a usermod ( i've tested the snippet on unix port, it works very well).it allows garbage collections for user created objects without hooking into root pointers ( internals, a bit messy and subject to changes ). On the other hand m_new_obj_var_with_finaliser() is likely to stay in the future.
I must admit, I am still a bit confused: the snippet that you quoted is a modification of the garbage collector code, so, certainly not part of a user-defined module. On the other hand, your second comment indicates otherwise.
In other words, are you saying that the __del__ special method of a class should be discussed somewhere in the manual? The discussion on the cited thread was rather diverse, thus, I am not so sure, what exactly pertains to the issue at hand.
I must admit, I am still a bit confused: the snippet that you quoted is a modification of the garbage collector code, so, certainly not part of a user-defined module.
In the example code I posted there, functionality for a callback for a finalizer gets registered as part of the gc module but it doesn't have to be: it could also be part of a custom C module so without modifications to modgc.c. I probably just did it that way because it was easier to test.
any update on this? I'm a usermod noob and don't understand how usermods can work reliably without del functions to clean up peripherals & other state upon a soft reboot (otherwise interrupts continue to call callbacks that no longer exist and crash the system).