pybind / pybind/pybind11

[FEAT] Make enable_shared_from_this logic in custom holder types configurable

Open
#2,957 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

When defining a holder type that can be initialized from a raw pointer T* (https://pybind11.readthedocs.io/en/stable/advanced/smart_ptrs.html), pybind11 conventionally assumes that there is a constructor Holder(T*) which will let it steal a raw pointer reference to initialize into the older. However, sometimes, you want to define a holder type where the raw pointer constructor doesn't have the correct semantics (e.g., std::shared_ptr) or you want to omit the raw pointer constructor and explicitly require users to say if they want to steal or borrow from the passed in raw pointer.

Unfortunately, there is no way to currently do this; you must define another Wrapper class to work around this problem. Here is an example from our codebase, where c10::intrusive_ptr is a holder type but it doesn't support construction from raw pointer (instead it's a static method called reclaim) and so I have to write this boilerplate:

template <typename T>
class IntrusivePtrOkForPybind11 {
  c10::intrusive_ptr<T> impl_;
public:
  IntrusivePtrOkForPybind11() = default;
  IntrusivePtrOkForPybind11(const IntrusivePtrNoGilDestructor&) = default;
  IntrusivePtrOkForPybind11(IntrusivePtrNoGilDestructor&&) = default;
  IntrusivePtrOkForPybind11& operator=(const IntrusivePtrOkForPybind11&) = default;
  IntrusivePtrOkForPybind11& operator=(IntrusivePtrOkForPybind11&&) = default;
  IntrusivePtrOkForPybind11(c10::intrusive_ptr<T> impl) : impl_(std::move(impl)) {}
  IntrusivePtrOkForPybind11(T* impl) : impl_(c10::intrusive_ptr<T>::reclaim(impl)) {}
  T& operator*() const noexcept { return *impl_; }
  T* operator->() const noexcept { return impl_.get(); }
  T* get() const noexcept { return impl_.get(); }
  void reset() noexcept { impl_.reset(); }
  operator bool() const noexcept {
    return impl_;
  }
};

It would be nice if there was some way to configure similar logic to enable_shared_from_this for our own type so we can bypass the raw pointer constructor entirely.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the custom holder and enable_shared_from_this behavior described in the advanced smart pointers documentation, then trace the corresponding holder-handling implementation in pybind11. Done means custom holder types can configure this behavior without a wrapper class or required raw-pointer constructor, with coverage for the stated intrusive_ptr use case.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.