← index #1878Issue #3627
Related · high · value 0.756
QUERY · ISSUE

py: __del__ special method not implemented for user-defined classes

openby israelg99opened 2016-03-07updated 2024-12-05
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

openby adritiumopened 2018-02-21updated 2018-02-23
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;
}

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied