-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Inherit weakref from handle #4925
Conversation
I can only look more tomorrow (US west coast time). Two quick simple things:
|
@rwgk Thank you. The motivation was that static analyzer reported some results of object::released calls were discarded. After digging into the code base, I found the weakref usage pattern. According to the CPython doc, the returned handle should be released in the callback. But this indicated that weakref shouldn't be considered as pybind::object. |
Collecting some background info:
It inherited from It still inherits from Looking in the Google codebase (which includes many 3rd-party packages), I only see a handful of My judgement call: I agree, it's better if |
After some investigation, I decided to revert the change. If the callback is passed in PyWeakref_NewRef, then the caller may want to dec_ref the weakref handle in the callback, then release should be used, otherwise, if the callback is null, then the weakref should be managed as a pybind11::object. In addition, the former pattern is used in pybind11 mainly as a caching mechanism relying on the GC strategy. Will it be more efficient to use a proper GC API? |
Oh, wow, I was wondering about that. I think it would definitely be good to explain that in a source code comment. The weakref documentation isn't clear about that (at least not to me).
I'd ask first: what problem are we trying to solve? If it's just to squeeze out a little bit more efficiency, is that worth the human brain cycles? IIUC you started here because of static analyzer warnings? Could we fix those with |
@rwgk I have another confusion, suppose that a callback is provided and we do normal ref-counting on the weakref object, will the callback receive a null weakref or un-triggered? The doc is unclear about this.. |
I don't know, but similar questions crossed my mind when I was looking at the weakref documentation yesterday. I was thinking it's necessary to inspect the Python sources, and usually I validate my conclusions by writing tests/experiments, but I totally don't have the free bandwidth for that. |
The design and uses of weakref in CPython API and pybind11 show that weakref should be better treated as handle and avoid the intentional release()