Skip to content

class instance destructors are not called #146

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

Closed
dlashua opened this issue Jan 16, 2021 · 2 comments
Closed

class instance destructors are not called #146

dlashua opened this issue Jan 16, 2021 · 2 comments

Comments

@dlashua
Copy link
Contributor

dlashua commented Jan 16, 2021

Test code:

class TestClass:
    def __init__(self):
        log.info('I am here')

    def __del__(self):
        log.info('I am gone')


@time_trigger('startup')
def startup():
    a = TestClass()
    task.sleep(3)
    del a

"I am gone" is never displayed.

Compare to python:

import time

class TestClass:
    def __init__(self):
        print('I am here')

    def __del__(self):
        print('I am gone')



a = TestClass()
time.sleep(3)
del a
@craigbarratt
Copy link
Member

Good catch. Part of the issue is the __del__ method defined in pyscript is async, so the python destructor can't call it. But even with a @pyscript_compile decorator it still doesn't work, which I'm still investigating.

@craigbarratt
Copy link
Member

It should work now. The __del__ function requires the @pyscript_compile decorator, since it needs to be a regular function. That means it can't use any pyscript features. To confirm __del__ is called I modified a global list, eg:

called = []

class TestClass:
    def __init__(self):
        log.info('I am here')

    @pyscript_compile
    def __del__(self):
        called.append("__del__")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants