QUERY · ISSUE
py: __del__ special method not implemented for user-defined classes
py-core
Example:
import gc
class Foo():
def __del__(self):
print('__del__')
f = Foo()
del f
gc.collect()
According to this post #1802, gc.collect() should collect the object after the del and call the finaliser, but the finaliser is never called, any ideas?
I know that a good programming practice is to assume that __del__ may never be called, but hey, this still should work :)
CANDIDATE · ISSUE
raising exception in __del__ finaliser results in deadlock with multithread enabled
bug
Original question is on uPy forum
call gc_alloc()
GC_ENTER()
...
GC_EXIT()
call gc_collect()
call gc_collect_start()
GC_ENTER()
call gc_collect_end()
call gc_sweep()
call "__del__" method of object if it exists (in this example an old file pointer)
file_close() throws a "file not open" OS Exception
OS Exception calls gc_alloc()
gc_alloc() calls GC_ENTER()
Result: The thread waits for the mutex to be released by itself.
The naming of m_malloc_maybe indicates it's supposed to be a conditional i.e. non-blocking allocation attempt at allocation but its implementation does not actually provide that feature; it just calls malloc and does a debug print.
void *m_malloc_maybe(size_t num_bytes) {
void *ptr = malloc(num_bytes);
#if MICROPY_MEM_STATS
MP_STATE_MEM(total_bytes_allocated) += num_bytes;
MP_STATE_MEM(current_bytes_allocated) += num_bytes;
UPDATE_PEAK();
#endif
DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
return ptr;
}