pyo3 icon indicating copy to clipboard operation
pyo3 copied to clipboard

Support `__del__`

Open samuelcolvin opened this issue 3 years ago • 2 comments
trafficstars

I'd like to be able to use a __del__ method in #[pymethods], however it's not being called.

There are two other related issue #402 and #774. #774 seems most relevant but the OP get's told they shouldn't be using __del__. AFAIK issuing a ResourceWarning is a legitimate use of __del__.

I could print a warning in Drop but that won't be a python warning and will confuse people, without support for __del__ I don't see another way around this.

Related PR https://github.com/samuelcolvin/watchfiles/pull/164

samuelcolvin avatar Jun 25 '22 11:06 samuelcolvin

Sounds reasonable to add this!

Some notes for implementer (probably me eventually, unless someone else is interested in picking this up). I think the underlying C API "slot" is tp_finalize. See implementation used for Python classes at https://github.com/python/cpython/blob/4e08fbcfdfa57ea94091aabdd09413708e3fb2bf/Objects/typeobject.c#L8033

It looks like the tp_dealloc in that file also calls tp_finalize in some circumstances; we might need to check carefully that we reproduce the Python behaviour correctly.

davidhewitt avatar Jun 25 '22 12:06 davidhewitt

Great, thank you.

No particular hurry from me, it's a possibly overkill warning in a semi-private class.

samuelcolvin avatar Jun 25 '22 12:06 samuelcolvin